Chose option 1) for http.lua.

Need to fix everything to make sure it works with the new compat-5.1
This commit is contained in:
Diego Nehab 2006-03-14 09:04:15 +00:00
parent 6248b915cb
commit 09ad4b299c
4 changed files with 25 additions and 52 deletions

View File

@ -36,7 +36,7 @@ PASSWORD = "anonymous@anonymous.org"
local metat = { __index = {} } local metat = { __index = {} }
function open(server, port, create) function open(server, port, create)
local tp = socket.try(tp.connect(server, port or PORT, create, TIMEOUT)) local tp = socket.try(tp.connect(server, port or PORT, TIMEOUT, create))
local f = base.setmetatable({ tp = tp }, metat) local f = base.setmetatable({ tp = tp }, metat)
-- make sure everything gets closed in an exception -- make sure everything gets closed in an exception
f.try = socket.newtry(function() f:close() end) f.try = socket.newtry(function() f:close() end)

View File

@ -203,9 +203,11 @@ local function adjustheaders(reqt)
["connection"] = "close, TE", ["connection"] = "close, TE",
["te"] = "trailers" ["te"] = "trailers"
} }
-- if we are sending a body, tell server to let us know -- if we have authentication information, pass it along
-- if it this is a waste of time if reqt.user and reqt.password then
if reqt.source then lower["expect"] = "100-continue" end lower["authorization"] =
"Basic " .. (mime.b64(reqt.user .. ":" .. reqt.password))
end
-- override with user headers -- override with user headers
for i,v in base.pairs(reqt.headers or lower) do for i,v in base.pairs(reqt.headers or lower) do
lower[string.lower(i)] = v lower[string.lower(i)] = v
@ -246,14 +248,6 @@ local function shouldredirect(reqt, code, headers)
and (not reqt.nredirects or reqt.nredirects < 5) and (not reqt.nredirects or reqt.nredirects < 5)
end end
local function shouldauthorize(reqt, code)
-- if there has been an authorization attempt, it must have failed
if reqt.headers and reqt.headers["authorization"] then return nil end
-- if last attempt didn't fail due to lack of authentication,
-- or we don't have authorization information, we can't retry
return code == 401 and reqt.user and reqt.password
end
local function shouldreceivebody(reqt, code) local function shouldreceivebody(reqt, code)
if reqt.method == "HEAD" then return nil end if reqt.method == "HEAD" then return nil end
if code == 204 or code == 304 then return nil end if code == 204 or code == 304 then return nil end
@ -261,15 +255,8 @@ local function shouldreceivebody(reqt, code)
return 1 return 1
end end
-- forward declarations -- forward declarations
local trequest, tauthorize, tredirect local trequest, tredirect
function tauthorize(reqt)
local auth = "Basic " .. (mime.b64(reqt.user .. ":" .. reqt.password))
reqt.headers["authorization"] = auth
return trequest(reqt)
end
function tredirect(reqt, location) function tredirect(reqt, location)
local result, code, headers, status = trequest { local result, code, headers, status = trequest {
@ -300,42 +287,28 @@ function trequest(reqt)
local headers, status local headers, status
-- if there is a body, check for server status -- if there is a body, check for server status
if reqt.source then if reqt.source then
local ready = socket.select({h.c}, nil, TIMEOUT) h:sendbody(reqt.headers, reqt.source, reqt.step)
if ready[h.c] then
-- here the server sent us something
code, status = h:receivestatusline()
headers = h:receiveheaders()
end
-- if server is happy, send body
if code == 100 then
h:sendbody(reqt.headers, reqt.source, reqt.step)
-- can't send body again!
reqt.source = nil
end
end end
-- ignore all further 100-continue messages -- ignore any 100-continue messages
while code == 100 do while code == 100 do
code, status = h:receivestatusline() code, status = h:receivestatusline()
headers = h:receiveheaders() headers = h:receiveheaders()
end end
-- at this point we should have a honest reply from the server -- at this point we should have a honest reply from the server
if shouldredirect(reqt, code, headers) then -- we can't redirect if we already used the source, so we report the error
if shouldredirect(reqt, code, headers) and not reqt.source then
h:close() h:close()
return tredirect(reqt, headers.location) return tredirect(reqt, headers.location)
elseif shouldauthorize(reqt, code) then
h:close()
return tauthorize(reqt)
else
-- here we are finally done
if shouldreceivebody(reqt, code) then
h:receivebody(headers, reqt.sink, reqt.step)
end
h:close()
return 1, code, headers, status
end end
-- here we are finally done
if shouldreceivebody(reqt, code) then
h:receivebody(headers, reqt.sink, reqt.step)
end
h:close()
return 1, code, headers, status
end end
local function srequest(u, b, h) local function srequest(u, b)
local t = {} local t = {}
local reqt = { local reqt = {
url = u, url = u,
@ -343,10 +316,10 @@ local function srequest(u, b, h)
} }
if b then if b then
reqt.source = ltn12.source.string(b) reqt.source = ltn12.source.string(b)
reqt.headers = h or {} reqt.headers = {
reqt.headers["content-length"] = string.len(b) ["content-length"] = string.len(b),
reqt.headers["content-type"] = reqt.headers["content-type"] or ["content-type"] = "application/x-www-form-urlencoded"
"application/x-www-form-urlencoded" }
reqt.method = "POST" reqt.method = "POST"
end end
local code, headers, status = socket.skip(1, trequest(reqt)) local code, headers, status = socket.skip(1, trequest(reqt))

View File

@ -113,7 +113,7 @@ end
function open(server, port, create) function open(server, port, create)
local tp = socket.try(tp.connect(server or SERVER, port or PORT, local tp = socket.try(tp.connect(server or SERVER, port or PORT,
create, TIMEOUT)) TIMEOUT, create))
local s = base.setmetatable({tp = tp}, metat) local s = base.setmetatable({tp = tp}, metat)
-- make sure tp is closed if we get an exception -- make sure tp is closed if we get an exception
s.try = socket.newtry(function() s.try = socket.newtry(function()

View File

@ -109,8 +109,8 @@ function metat.__index:close()
end end
-- connect with server and return c object -- connect with server and return c object
function connect(host, port, create, timeout) function connect(host, port, timeout, create)
local c, e = (create or socket.tcp()) local c, e = (create or socket.tcp)()
if not c then return nil, e end if not c then return nil, e end
c:settimeout(timeout or TIMEOUT) c:settimeout(timeout or TIMEOUT)
local r, e = c:connect(host, port) local r, e = c:connect(host, port)