From 09ad4b299c59c3c5c6c442f0ee99f3c9e8dbe8ce Mon Sep 17 00:00:00 2001 From: Diego Nehab Date: Tue, 14 Mar 2006 09:04:15 +0000 Subject: [PATCH] Chose option 1) for http.lua. Need to fix everything to make sure it works with the new compat-5.1 --- src/ftp.lua | 2 +- src/http.lua | 69 ++++++++++++++++------------------------------------ src/smtp.lua | 2 +- src/tp.lua | 4 +-- 4 files changed, 25 insertions(+), 52 deletions(-) diff --git a/src/ftp.lua b/src/ftp.lua index d3a48d4..18d22f7 100644 --- a/src/ftp.lua +++ b/src/ftp.lua @@ -36,7 +36,7 @@ PASSWORD = "anonymous@anonymous.org" local metat = { __index = {} } 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) -- make sure everything gets closed in an exception f.try = socket.newtry(function() f:close() end) diff --git a/src/http.lua b/src/http.lua index 91600e7..081e156 100644 --- a/src/http.lua +++ b/src/http.lua @@ -203,9 +203,11 @@ local function adjustheaders(reqt) ["connection"] = "close, TE", ["te"] = "trailers" } - -- if we are sending a body, tell server to let us know - -- if it this is a waste of time - if reqt.source then lower["expect"] = "100-continue" end + -- if we have authentication information, pass it along + if reqt.user and reqt.password then + lower["authorization"] = + "Basic " .. (mime.b64(reqt.user .. ":" .. reqt.password)) + end -- override with user headers for i,v in base.pairs(reqt.headers or lower) do lower[string.lower(i)] = v @@ -246,14 +248,6 @@ local function shouldredirect(reqt, code, headers) and (not reqt.nredirects or reqt.nredirects < 5) 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) if reqt.method == "HEAD" 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 end - -- forward declarations -local trequest, tauthorize, tredirect - -function tauthorize(reqt) - local auth = "Basic " .. (mime.b64(reqt.user .. ":" .. reqt.password)) - reqt.headers["authorization"] = auth - return trequest(reqt) -end +local trequest, tredirect function tredirect(reqt, location) local result, code, headers, status = trequest { @@ -300,42 +287,28 @@ function trequest(reqt) local headers, status -- if there is a body, check for server status if reqt.source then - local ready = socket.select({h.c}, nil, TIMEOUT) - 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 + h:sendbody(reqt.headers, reqt.source, reqt.step) end - -- ignore all further 100-continue messages + -- ignore any 100-continue messages while code == 100 do code, status = h:receivestatusline() headers = h:receiveheaders() end -- 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() 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 + -- 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 -local function srequest(u, b, h) +local function srequest(u, b) local t = {} local reqt = { url = u, @@ -343,10 +316,10 @@ local function srequest(u, b, h) } if b then reqt.source = ltn12.source.string(b) - reqt.headers = h or {} - reqt.headers["content-length"] = string.len(b) - reqt.headers["content-type"] = reqt.headers["content-type"] or - "application/x-www-form-urlencoded" + reqt.headers = { + ["content-length"] = string.len(b), + ["content-type"] = "application/x-www-form-urlencoded" + } reqt.method = "POST" end local code, headers, status = socket.skip(1, trequest(reqt)) diff --git a/src/smtp.lua b/src/smtp.lua index 72c4234..2257a69 100644 --- a/src/smtp.lua +++ b/src/smtp.lua @@ -113,7 +113,7 @@ end function open(server, port, create) local tp = socket.try(tp.connect(server or SERVER, port or PORT, - create, TIMEOUT)) + TIMEOUT, create)) local s = base.setmetatable({tp = tp}, metat) -- make sure tp is closed if we get an exception s.try = socket.newtry(function() diff --git a/src/tp.lua b/src/tp.lua index c6d60c8..2eacdc4 100644 --- a/src/tp.lua +++ b/src/tp.lua @@ -109,8 +109,8 @@ function metat.__index:close() end -- connect with server and return c object -function connect(host, port, create, timeout) - local c, e = (create or socket.tcp()) +function connect(host, port, timeout, create) + local c, e = (create or socket.tcp)() if not c then return nil, e end c:settimeout(timeout or TIMEOUT) local r, e = c:connect(host, port)