From f27a87a760fa488ff5a42cd4ea3b5c75c89467fa Mon Sep 17 00:00:00 2001 From: Nick Gammon Date: Thu, 22 Dec 2011 08:40:04 +1100 Subject: [PATCH] Upgraded luasocket from 2.0.1 to 2.0.2 --- lua/ltn12.lua | 1 + lua/mime.lua | 5 ++-- lua/socket.lua | 1 + lua/socket/ftp.lua | 9 +++---- lua/socket/http.lua | 57 +++++++++++++++++++++++++++++---------------- lua/socket/smtp.lua | 27 ++++++++++++--------- lua/socket/tp.lua | 1 + lua/socket/url.lua | 1 - 8 files changed, 64 insertions(+), 38 deletions(-) diff --git a/lua/ltn12.lua b/lua/ltn12.lua index ca1f8421..b42689a3 100644 --- a/lua/ltn12.lua +++ b/lua/ltn12.lua @@ -289,3 +289,4 @@ function pump.all(src, snk, step) end end end + diff --git a/lua/mime.lua b/lua/mime.lua index 293f5f17..169eda29 100644 --- a/lua/mime.lua +++ b/lua/mime.lua @@ -2,7 +2,7 @@ -- MIME support for the Lua language. -- Author: Diego Nehab -- Conforming to RFCs 2045-2049 --- RCS ID: $Id: mime.lua,v 1.28 2005/11/22 08:33:29 diego Exp $ +-- RCS ID: $Id: mime.lua,v 1.29 2007/06/11 23:44:54 diego Exp $ ----------------------------------------------------------------------------- ----------------------------------------------------------------------------- @@ -27,7 +27,8 @@ local function choose(table) name, opt1, opt2 = "default", name, opt1 end local f = table[name or "nil"] - if not f then error("unknown key (" .. base.tostring(name) .. ")", 3) + if not f then + base.error("unknown key (" .. base.tostring(name) .. ")", 3) else return f(opt1, opt2) end end end diff --git a/lua/socket.lua b/lua/socket.lua index a98e9521..211adcd1 100644 --- a/lua/socket.lua +++ b/lua/socket.lua @@ -130,3 +130,4 @@ end sourcet["default"] = sourcet["until-closed"] source = choose(sourcet) + diff --git a/lua/socket/ftp.lua b/lua/socket/ftp.lua index d5aa326d..598f65dc 100644 --- a/lua/socket/ftp.lua +++ b/lua/socket/ftp.lua @@ -2,7 +2,7 @@ -- FTP support for the Lua language -- LuaSocket toolkit. -- Author: Diego Nehab --- RCS ID: $Id: ftp.lua,v 1.44 2006/03/14 09:04:15 diego Exp $ +-- RCS ID: $Id: ftp.lua,v 1.45 2007/07/11 19:25:47 diego Exp $ ----------------------------------------------------------------------------- ----------------------------------------------------------------------------- @@ -88,7 +88,7 @@ function metat.__index:port(ip, port) ip, port = self.try(self.tp:getcontrol():getsockname()) self.server = self.try(socket.bind(ip, 0)) ip, port = self.try(self.server:getsockname()) - self.try(server:settimeout(TIMEOUT)) + self.try(self.server:settimeout(TIMEOUT)) end local pl = math.mod(port, 256) local ph = (port - pl)/256 @@ -187,9 +187,9 @@ end ----------------------------------------------------------------------------- -- High level FTP API ----------------------------------------------------------------------------- -function override(t) +local function override(t) if t.url then - u = url.parse(t.url) + local u = url.parse(t.url) for i,v in base.pairs(t) do u[i] = v end @@ -278,3 +278,4 @@ get = socket.protect(function(gett) if base.type(gett) == "string" then return sget(gett) else return tget(gett) end end) + diff --git a/lua/socket/http.lua b/lua/socket/http.lua index 985bd464..c020d8e7 100644 --- a/lua/socket/http.lua +++ b/lua/socket/http.lua @@ -2,7 +2,7 @@ -- HTTP/1.1 client support for the Lua language. -- LuaSocket toolkit. -- Author: Diego Nehab --- RCS ID: $Id: http.lua,v 1.67 2006/04/03 03:10:56 diego Exp $ +-- RCS ID: $Id: http.lua,v 1.70 2007/03/12 04:08:40 diego Exp $ ----------------------------------------------------------------------------- ----------------------------------------------------------------------------- @@ -15,7 +15,6 @@ local mime = require("mime") local string = require("string") local base = _G local table = require("table") -local print = print module("socket.http") ----------------------------------------------------------------------------- @@ -108,7 +107,7 @@ local metat = { __index = {} } function open(host, port, create) -- create socket with user connect function, or with default - local c = socket.try(create or socket.tcp)() + local c = socket.try((create or socket.tcp)()) local h = base.setmetatable({ c = c }, metat) -- create finalized try h.try = socket.newtry(function() h:close() end) @@ -143,7 +142,12 @@ function metat.__index:sendbody(headers, source, step) end function metat.__index:receivestatusline() - local status = self.try(self.c:receive()) + local status = self.try(self.c:receive(5)) + -- identify HTTP/0.9 responses, which do not contain a status line + -- this is just a heuristic, but is what the RFC recommends + if status ~= "HTTP/" then return nil, status end + -- otherwise proceed reading a status line + status = self.try(self.c:receive("*l", status)) local code = socket.skip(2, string.find(status, "HTTP/%d*%.%d* (%d%d%d)")) return self.try(base.tonumber(code), status) end @@ -164,6 +168,12 @@ function metat.__index:receivebody(headers, sink, step) sink, step)) end +function metat.__index:receive09body(status, sink, step) + local source = ltn12.source.rewind(socket.source("until-closed", self.c)) + source(status) + return self.try(ltn12.pump.all(source, sink, step)) +end + function metat.__index:close() return self.c:close() end @@ -229,7 +239,8 @@ local function adjustrequest(reqt) -- explicit components override url for i,v in base.pairs(reqt) do nreqt[i] = v end if nreqt.port == "" then nreqt.port = 80 end - socket.try(nreqt.host, "invalid host '" .. base.tostring(nreqt.host) .. "'") + socket.try(nreqt.host and nreqt.host ~= "", + "invalid host '" .. base.tostring(nreqt.host) .. "'") -- compute uri if user hasn't overriden nreqt.uri = reqt.uri or adjusturi(nreqt) -- ajust host and port if there is a proxy @@ -262,7 +273,7 @@ function tredirect(reqt, location) local result, code, headers, status = trequest { -- the RFC says the redirect URL has to be absolute, but some -- servers do not respect that - url = url.absolute(reqt, location), + url = url.absolute(reqt.url, location), source = reqt.source, sink = reqt.sink, headers = reqt.headers, @@ -271,6 +282,7 @@ function tredirect(reqt, location) create = reqt.create } -- pass location header back as a hint we redirected + headers = headers or {} headers.location = headers.location or location return result, code, headers, status end @@ -278,31 +290,37 @@ end function trequest(reqt) -- we loop until we get what we want, or -- until we are sure there is no way to get it - reqt = adjustrequest(reqt) - local h = open(reqt.host, reqt.port, reqt.create) + local nreqt = adjustrequest(reqt) + local h = open(nreqt.host, nreqt.port, nreqt.create) -- send request line and headers - h:sendrequestline(reqt.method, reqt.uri) - h:sendheaders(reqt.headers) - local code = 100 - local headers, status - -- if there is a body, check for server status - if reqt.source then - h:sendbody(reqt.headers, reqt.source, reqt.step) + h:sendrequestline(nreqt.method, nreqt.uri) + h:sendheaders(nreqt.headers) + -- if there is a body, send it + if nreqt.source then + h:sendbody(nreqt.headers, nreqt.source, nreqt.step) + end + local code, status = h:receivestatusline() + -- if it is an HTTP/0.9 server, simply get the body and we are done + if not code then + h:receive09body(status, nreqt.sink, nreqt.step) + return 1, 200 end + local headers -- ignore any 100-continue messages while code == 100 do - code, status = h:receivestatusline() headers = h:receiveheaders() + code, status = h:receivestatusline() end + headers = h:receiveheaders() -- at this point we should have a honest reply from the server -- 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 + if shouldredirect(nreqt, code, headers) and not nreqt.source then h:close() return tredirect(reqt, headers.location) end -- here we are finally done - if shouldreceivebody(reqt, code) then - h:receivebody(headers, reqt.sink, reqt.step) + if shouldreceivebody(nreqt, code) then + h:receivebody(headers, nreqt.sink, nreqt.step) end h:close() return 1, code, headers, status @@ -330,4 +348,3 @@ request = socket.protect(function(reqt, body) if base.type(reqt) == "string" then return srequest(reqt, body) else return trequest(reqt) end end) - diff --git a/lua/socket/smtp.lua b/lua/socket/smtp.lua index 337867ad..8f3cfcff 100644 --- a/lua/socket/smtp.lua +++ b/lua/socket/smtp.lua @@ -2,7 +2,7 @@ -- SMTP client support for the Lua language. -- LuaSocket toolkit. -- Author: Diego Nehab --- RCS ID: $Id: smtp.lua,v 1.45 2006/03/14 09:04:15 diego Exp $ +-- RCS ID: $Id: smtp.lua,v 1.46 2007/03/12 04:08:40 diego Exp $ ----------------------------------------------------------------------------- ----------------------------------------------------------------------------- @@ -122,6 +122,15 @@ function open(server, port, create) return s end +-- convert headers to lowercase +local function lower_headers(headers) + local lower = {} + for i,v in base.pairs(headers or lower) do + lower[string.lower(i)] = v + end + return lower +end + --------------------------------------------------------------------------- -- Multipart message source ----------------------------------------------------------------------------- @@ -149,7 +158,7 @@ end local function send_multipart(mesgt) -- make sure we have our boundary and send headers local bd = newboundary() - local headers = mesgt.headers or {} + local headers = lower_headers(mesgt.headers or {}) headers['content-type'] = headers['content-type'] or 'multipart/mixed' headers['content-type'] = headers['content-type'] .. '; boundary="' .. bd .. '"' @@ -176,7 +185,7 @@ end -- yield message body from a source local function send_source(mesgt) -- make sure we have a content-type - local headers = mesgt.headers or {} + local headers = lower_headers(mesgt.headers or {}) headers['content-type'] = headers['content-type'] or 'text/plain; charset="iso-8859-1"' send_headers(headers) @@ -192,7 +201,7 @@ end -- yield message body from a string local function send_string(mesgt) -- make sure we have a content-type - local headers = mesgt.headers or {} + local headers = lower_headers(mesgt.headers or {}) headers['content-type'] = headers['content-type'] or 'text/plain; charset="iso-8859-1"' send_headers(headers) @@ -209,20 +218,17 @@ end -- set defaul headers local function adjust_headers(mesgt) - local lower = {} - for i,v in base.pairs(mesgt.headers or lower) do - lower[string.lower(i)] = v - end + local lower = lower_headers(mesgt.headers) lower["date"] = lower["date"] or os.date("!%a, %d %b %Y %H:%M:%S ") .. (mesgt.zone or ZONE) lower["x-mailer"] = lower["x-mailer"] or socket._VERSION -- this can't be overriden lower["mime-version"] = "1.0" - mesgt.headers = lower + return lower end function message(mesgt) - adjust_headers(mesgt) + mesgt.headers = adjust_headers(mesgt) -- create and return message source local co = coroutine.create(function() send_message(mesgt) end) return function() @@ -243,4 +249,3 @@ send = socket.protect(function(mailt) s:quit() return s:close() end) - diff --git a/lua/socket/tp.lua b/lua/socket/tp.lua index 98b58c62..06838694 100644 --- a/lua/socket/tp.lua +++ b/lua/socket/tp.lua @@ -120,3 +120,4 @@ function connect(host, port, timeout, create) end return base.setmetatable({c = c}, metat) end + diff --git a/lua/socket/url.lua b/lua/socket/url.lua index 7df8adab..0e31d8af 100644 --- a/lua/socket/url.lua +++ b/lua/socket/url.lua @@ -295,4 +295,3 @@ function build_path(parsed, unsafe) if parsed.is_absolute then path = "/" .. path end return path end -