HTTP now has only one function.

This commit is contained in:
Diego Nehab 2004-06-16 22:51:04 +00:00
parent 574708380f
commit 9fc682a106
4 changed files with 24 additions and 33 deletions

View File

@ -8,8 +8,6 @@ local http = require("http")
local url = require("url") local url = require("url")
http.TIMEOUT = 10 http.TIMEOUT = 10
cache = {}
function readfile(path) function readfile(path)
path = url.unescape(path) path = url.unescape(path)
local file, error = io.open(path, "r") local file, error = io.open(path, "r")
@ -22,22 +20,14 @@ end
function getstatus(u) function getstatus(u)
local parsed = url.parse(u, {scheme = "file"}) local parsed = url.parse(u, {scheme = "file"})
if cache[u] then return cache[u] end
local res
if parsed.scheme == "http" then if parsed.scheme == "http" then
local request = {url = u, method = "HEAD"} local r, c, h, s = http.request{url = u, method = "HEAD"}
local response = http.request(request) if c ~= 200 then return s or c end
if response.code == 200 then res = nil
else res = response.status or response.error end
elseif parsed.scheme == "file" then elseif parsed.scheme == "file" then
local file, error = io.open(url.unescape(parsed.path), "r") local file, error = io.open(url.unescape(parsed.path), "r")
if file then if file then file:close()
file:close() else return error end
res = nil else return string.format("unhandled scheme '%s'", parsed.scheme) end
else res = error end
else res = string.format("unhandled scheme '%s'", parsed.scheme) end
cache[u] = res
return res
end end
function retrieve(u) function retrieve(u)
@ -45,10 +35,13 @@ function retrieve(u)
local body, headers, code, error local body, headers, code, error
local base = u local base = u
if parsed.scheme == "http" then if parsed.scheme == "http" then
body, headers, code, error = http.get(u) body, code, headers = http.request(u)
if code == 200 then if code == 200 then
base = base or headers.location base = base or headers.location
end end
if not body then
error = code
end
elseif parsed.scheme == "file" then elseif parsed.scheme == "file" then
body, error = readfile(parsed.path) body, error = readfile(parsed.path)
else error = string.format("unhandled scheme '%s'", parsed.scheme) end else error = string.format("unhandled scheme '%s'", parsed.scheme) end

View File

@ -87,9 +87,9 @@ end
-- determines the size of a http file -- determines the size of a http file
function gethttpsize(u) function gethttpsize(u)
local respt = http.request {method = "HEAD", url = u} local r, c, h = http.request {method = "HEAD", url = u}
if respt.code == 200 then if c == 200 then
return tonumber(respt.headers["content-length"]) return tonumber(h["content-length"])
end end
end end
@ -98,8 +98,8 @@ function getbyhttp(u, file)
local save = ltn12.sink.file(file or io.stdout) local save = ltn12.sink.file(file or io.stdout)
-- only print feedback if output is not stdout -- only print feedback if output is not stdout
if file then save = ltn12.sink.chain(stats(gethttpsize(u)), save) end if file then save = ltn12.sink.chain(stats(gethttpsize(u)), save) end
local respt = http.request {url = u, sink = save } local r, c, h, s = http.request {url = u, sink = save }
if respt.code ~= 200 then print(respt.status or respt.error) end if c ~= 200 then io.stderr:write(s or c, "\n") end
end end
-- downloads a file using the ftp protocol -- downloads a file using the ftp protocol

View File

@ -122,7 +122,7 @@ local function uri(reqt)
local u = reqt local u = reqt
if not reqt.proxy and not PROXY then if not reqt.proxy and not PROXY then
u = { u = {
path = reqt.path, path = socket.try(reqt.path, "invalid path 'nil'"),
params = reqt.params, params = reqt.params,
query = reqt.query, query = reqt.query,
fragment = reqt.fragment fragment = reqt.fragment
@ -152,18 +152,15 @@ local default = {
local function adjustrequest(reqt) local function adjustrequest(reqt)
-- parse url if provided -- parse url if provided
if reqt.url then local nreqt = reqt.url and url.parse(reqt.url, default) or {}
local parsed = url.parse(reqt.url, default) -- explicit components override url
-- explicit components override url for i,v in reqt do nreqt[i] = reqt[i] end
for i,v in parsed do reqt[i] = reqt[i] or v end socket.try(nreqt.host, "invalid host '" .. tostring(nreqt.host) .. "'")
end
socket.try(reqt.host, "invalid host '" .. tostring(reqt.host) .. "'")
socket.try(reqt.path, "invalid path '" .. tostring(reqt.path) .. "'")
-- compute uri if user hasn't overriden -- compute uri if user hasn't overriden
reqt.uri = reqt.uri or uri(reqt) nreqt.uri = nreqt.uri or uri(nreqt)
-- adjust headers in request -- adjust headers in request
reqt.headers = adjustheaders(reqt.headers, reqt.host) nreqt.headers = adjustheaders(nreqt.headers, nreqt.host)
return reqt return nreqt
end end
local function shouldredirect(reqt, code) local function shouldredirect(reqt, code)

View File

@ -115,7 +115,8 @@ end
----------------------------------------------------------------------------- -----------------------------------------------------------------------------
function parse(url, default) function parse(url, default)
-- initialize default parameters -- initialize default parameters
local parsed = default or {} local parsed = {}
for i,v in (default or parsed) do parsed[i] = v end
-- empty url is parsed to nil -- empty url is parsed to nil
if not url or url == "" then return nil, "invalid url" end if not url or url == "" then return nil, "invalid url" end
-- remove whitespace -- remove whitespace