Simplified HTTP module.

This commit is contained in:
Diego Nehab 2004-06-16 21:56:23 +00:00
parent ba2f0b8c6b
commit 574708380f
3 changed files with 81 additions and 87 deletions

View File

@ -143,117 +143,111 @@ local function adjustheaders(headers, host)
return lower return lower
end end
local default = {
host = "",
port = PORT,
path ="/",
scheme = "http"
}
local function adjustrequest(reqt) local function adjustrequest(reqt)
-- parse url with default fields -- parse url if provided
local parsed = url.parse(reqt.url or "", { if reqt.url then
host = "", local parsed = url.parse(reqt.url, default)
port = PORT, -- explicit components override url
path ="/", for i,v in parsed do reqt[i] = reqt[i] or v end
scheme = "http" end
}) socket.try(reqt.host, "invalid host '" .. tostring(reqt.host) .. "'")
-- explicit info in reqt overrides that given by the URL socket.try(reqt.path, "invalid path '" .. tostring(reqt.path) .. "'")
for i,v in reqt do parsed[i] = v end
-- compute uri if user hasn't overriden -- compute uri if user hasn't overriden
parsed.uri = parsed.uri or uri(parsed) reqt.uri = reqt.uri or uri(reqt)
-- adjust headers in request -- adjust headers in request
parsed.headers = adjustheaders(parsed.headers, parsed.host) reqt.headers = adjustheaders(reqt.headers, reqt.host)
return parsed return reqt
end end
local function shouldredirect(reqt, respt) local function shouldredirect(reqt, code)
return (reqt.redirect ~= false) and return (reqt.redirect ~= false) and
(respt.code == 301 or respt.code == 302) and (code == 301 or code == 302) and
(not reqt.method or reqt.method == "GET" or reqt.method == "HEAD") (not reqt.method or reqt.method == "GET" or reqt.method == "HEAD")
and (not reqt.nredirects or reqt.nredirects < 5) and (not reqt.nredirects or reqt.nredirects < 5)
end end
local function shouldauthorize(reqt, respt) local function shouldauthorize(reqt, code)
-- if there has been an authorization attempt, it must have failed -- if there has been an authorization attempt, it must have failed
if reqt.headers and reqt.headers["authorization"] then return nil end if reqt.headers and reqt.headers["authorization"] then return nil end
-- if last attempt didn't fail due to lack of authentication, -- if last attempt didn't fail due to lack of authentication,
-- or we don't have authorization information, we can't retry -- or we don't have authorization information, we can't retry
return respt.code == 401 and reqt.user and reqt.password return code == 401 and reqt.user and reqt.password
end end
local function shouldreceivebody(reqt, respt) local function shouldreceivebody(reqt, code)
if reqt.method == "HEAD" then return nil end if reqt.method == "HEAD" then return nil end
local code = respt.code
if code == 204 or code == 304 then return nil end if code == 204 or code == 304 then return nil end
if code >= 100 and code < 200 then return nil end if code >= 100 and code < 200 then return nil end
return 1 return 1
end end
local requestp, authorizep, redirectp -- forward declarations
local trequest, tauthorize, tredirect
function requestp(reqt) function tauthorize(reqt)
local reqt = adjustrequest(reqt)
local respt = {}
local con = open(reqt.host, reqt.port)
con:sendrequestline(reqt.method, reqt.uri)
con:sendheaders(reqt.headers)
con:sendbody(reqt.headers, reqt.source, reqt.step)
respt.code, respt.status = con:receivestatusline()
respt.headers = con:receiveheaders()
if shouldredirect(reqt, respt) then
con:close()
return redirectp(reqt, respt)
elseif shouldauthorize(reqt, respt) then
con:close()
return authorizep(reqt, respt)
elseif shouldreceivebody(reqt, respt) then
con:receivebody(respt.headers, reqt.sink, reqt.step)
end
con:close()
return respt
end
function authorizep(reqt, respt)
local auth = "Basic " .. (mime.b64(reqt.user .. ":" .. reqt.password)) local auth = "Basic " .. (mime.b64(reqt.user .. ":" .. reqt.password))
reqt.headers["authorization"] = auth reqt.headers["authorization"] = auth
return requestp(reqt) return trequest(reqt)
end end
function redirectp(reqt, respt) function tredirect(reqt, headers)
-- we create a new table to get rid of anything we don't -- the RFC says the redirect URL has to be absolute, but some
-- absolutely need, including authentication info -- servers do not respect that
local redirt = { return trequest {
method = reqt.method, url = url.absolute(reqt, headers["location"]),
-- the RFC says the redirect URL has to be absolute, but some
-- servers do not respect that
url = url.absolute(reqt.url, respt.headers["location"]),
source = reqt.source, source = reqt.source,
sink = reqt.sink, sink = reqt.sink,
headers = reqt.headers, headers = reqt.headers,
proxy = reqt.proxy, proxy = reqt.proxy,
nredirects = (reqt.nredirects or 0) + 1 nredirects = (reqt.nredirects or 0) + 1
} }
respt = requestp(redirt)
-- we pass the location header as a clue we redirected
if respt.headers then respt.headers.location = redirt.url end
return respt
end end
request = socket.protect(requestp) function trequest(reqt)
reqt = adjustrequest(reqt)
local con = open(reqt.host, reqt.port)
con:sendrequestline(reqt.method, reqt.uri)
con:sendheaders(reqt.headers)
con:sendbody(reqt.headers, reqt.source, reqt.step)
local code, headers, status
code, status = con:receivestatusline()
headers = con:receiveheaders()
if shouldredirect(reqt, code) then
con:close()
return tredirect(reqt, headers)
elseif shouldauthorize(reqt, code) then
con:close()
return tauthorize(reqt)
elseif shouldreceivebody(reqt, code) then
con:receivebody(headers, reqt.sink, reqt.step)
end
con:close()
return 1, code, headers, status
end
get = socket.protect(function(u) local function srequest(u, body)
local t = {} local t = {}
local respt = requestp { local reqt = {
url = u, url = u,
sink = ltn12.sink.table(t) sink = ltn12.sink.table(t)
} }
return (table.getn(t) > 0 or nil) and table.concat(t), respt.headers, if body then
respt.code reqt.source = ltn12.source.string(body)
end) reqt.headers = { ["content-length"] = string.len(body) }
reqt.method = "POST"
end
local code, headers, status = socket.skip(1, trequest(reqt))
return table.concat(t), code, headers, status
end
post = socket.protect(function(u, body) request = socket.protect(function(reqt, body)
local t = {} if type(reqt) == "string" then return srequest(reqt, body)
local respt = requestp { else return trequest(reqt) end
url = u,
method = "POST",
source = ltn12.source.string(body),
sink = ltn12.sink.table(t),
headers = { ["content-length"] = string.len(body) }
}
return (table.getn(t) > 0 or nil) and table.concat(t),
respt.headers, respt.code
end) end)

View File

@ -190,7 +190,7 @@ end
-- corresponding absolute url -- corresponding absolute url
----------------------------------------------------------------------------- -----------------------------------------------------------------------------
function absolute(base_url, relative_url) function absolute(base_url, relative_url)
local base = parse(base_url) local base = type(base_url) == "table" and base_url or parse(base_url)
local relative = parse(relative_url) local relative = parse(relative_url)
if not base then return relative_url if not base then return relative_url
elseif not relative then return base_url elseif not relative then return base_url

View File

@ -55,12 +55,12 @@ end
local check_request = function(request, expect, ignore) local check_request = function(request, expect, ignore)
local t local t
if not request.sink then if not request.sink then request.sink, t = ltn12.sink.table() end
request.sink, t = ltn12.sink.table(t)
end
request.source = request.source or request.source = request.source or
(request.body and ltn12.source.string(request.body)) (request.body and ltn12.source.string(request.body))
local response = http.request(request) local response = {}
response.code, response.headers, response.status =
socket.skip(1, http.request(request))
if t and table.getn(t) > 0 then response.body = table.concat(t) end if t and table.getn(t) > 0 then response.body = table.concat(t) end
check_result(response, expect, ignore) check_result(response, expect, ignore)
end end
@ -68,8 +68,8 @@ end
------------------------------------------------------------------------ ------------------------------------------------------------------------
io.write("testing request uri correctness: ") io.write("testing request uri correctness: ")
local forth = cgiprefix .. "/request-uri?" .. "this+is+the+query+string" local forth = cgiprefix .. "/request-uri?" .. "this+is+the+query+string"
local back, h, c, e = http.get("http://" .. host .. forth) local back, c, h = http.request("http://" .. host .. forth)
if not back then fail(e) end if not back then fail(c) end
back = url.parse(back) back = url.parse(back)
if similar(back.query, "this+is+the+query+string") then print("ok") if similar(back.query, "this+is+the+query+string") then print("ok")
else fail(back.query) end else fail(back.query) end
@ -77,7 +77,7 @@ else fail(back.query) end
------------------------------------------------------------------------ ------------------------------------------------------------------------
io.write("testing query string correctness: ") io.write("testing query string correctness: ")
forth = "this+is+the+query+string" forth = "this+is+the+query+string"
back = http.get("http://" .. host .. cgiprefix .. back = http.request("http://" .. host .. cgiprefix ..
"/query-string?" .. forth) "/query-string?" .. forth)
if similar(back, forth) then print("ok") if similar(back, forth) then print("ok")
else fail("failed!") end else fail("failed!") end
@ -153,7 +153,7 @@ check_request(request, expect, ignore)
------------------------------------------------------------------------ ------------------------------------------------------------------------
io.write("testing simple post function: ") io.write("testing simple post function: ")
back = http.post("http://" .. host .. cgiprefix .. "/cat", index) back = http.request("http://" .. host .. cgiprefix .. "/cat", index)
assert(back == index) assert(back == index)
------------------------------------------------------------------------ ------------------------------------------------------------------------
@ -378,19 +378,19 @@ check_request(request, expect, ignore)
------------------------------------------------------------------------ ------------------------------------------------------------------------
local body local body
io.write("testing simple get function: ") io.write("testing simple request function: ")
body = http.get("http://" .. host .. prefix .. "/index.html") body = http.request("http://" .. host .. prefix .. "/index.html")
assert(body == index) assert(body == index)
print("ok") print("ok")
------------------------------------------------------------------------ ------------------------------------------------------------------------
io.write("testing HEAD method: ") io.write("testing HEAD method: ")
http.TIMEOUT = 1 http.TIMEOUT = 1
response = http.request { local r, c, h = http.request {
method = "HEAD", method = "HEAD",
url = "http://www.cs.princeton.edu/~diego/" url = "http://www.cs.princeton.edu/~diego/"
} }
assert(response and response.headers) assert(r and h and c == 200)
print("ok") print("ok")
------------------------------------------------------------------------ ------------------------------------------------------------------------
@ -398,7 +398,7 @@ io.write("testing host not found: ")
local c, e = socket.connect("wronghost", 80) local c, e = socket.connect("wronghost", 80)
local r, re = http.request{url = "http://wronghost/does/not/exist"} local r, re = http.request{url = "http://wronghost/does/not/exist"}
assert(r == nil and e == re) assert(r == nil and e == re)
r, re = http.get("http://wronghost/does/not/exist") r, re = http.request("http://wronghost/does/not/exist")
assert(r == nil and e == re) assert(r == nil and e == re)
print("ok") print("ok")
@ -407,7 +407,7 @@ io.write("testing invalid url: ")
local c, e = socket.connect("", 80) local c, e = socket.connect("", 80)
local r, re = http.request{url = host .. prefix} local r, re = http.request{url = host .. prefix}
assert(r == nil and e == re) assert(r == nil and e == re)
r, re = http.get(host .. prefix) r, re = http.request(host .. prefix)
assert(r == nil and e == re) assert(r == nil and e == re)
print("ok") print("ok")