Skip to content

Commit

Permalink
Upgraded luasocket from 2.0.1 to 2.0.2
Browse files Browse the repository at this point in the history
  • Loading branch information
nickgammon committed Dec 21, 2011
1 parent b57100e commit f27a87a
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 38 deletions.
1 change: 1 addition & 0 deletions lua/ltn12.lua
Expand Up @@ -289,3 +289,4 @@ function pump.all(src, snk, step)
end
end
end

5 changes: 3 additions & 2 deletions lua/mime.lua
Expand Up @@ -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 $
-----------------------------------------------------------------------------

-----------------------------------------------------------------------------
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions lua/socket.lua
Expand Up @@ -130,3 +130,4 @@ end
sourcet["default"] = sourcet["until-closed"]

source = choose(sourcet)

9 changes: 5 additions & 4 deletions lua/socket/ftp.lua
Expand Up @@ -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 $
-----------------------------------------------------------------------------

-----------------------------------------------------------------------------
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -278,3 +278,4 @@ get = socket.protect(function(gett)
if base.type(gett) == "string" then return sget(gett)
else return tget(gett) end
end)

57 changes: 37 additions & 20 deletions lua/socket/http.lua
Expand Up @@ -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 $
-----------------------------------------------------------------------------

-----------------------------------------------------------------------------
Expand All @@ -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")

-----------------------------------------------------------------------------
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -271,38 +282,45 @@ 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

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
Expand Down Expand Up @@ -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)

27 changes: 16 additions & 11 deletions lua/socket/smtp.lua
Expand Up @@ -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 $
-----------------------------------------------------------------------------

-----------------------------------------------------------------------------
Expand Down Expand Up @@ -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
-----------------------------------------------------------------------------
Expand Down Expand Up @@ -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 .. '"'
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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()
Expand All @@ -243,4 +249,3 @@ send = socket.protect(function(mailt)
s:quit()
return s:close()
end)

1 change: 1 addition & 0 deletions lua/socket/tp.lua
Expand Up @@ -120,3 +120,4 @@ function connect(host, port, timeout, create)
end
return base.setmetatable({c = c}, metat)
end

1 change: 0 additions & 1 deletion lua/socket/url.lua
Expand Up @@ -295,4 +295,3 @@ function build_path(parsed, unsafe)
if parsed.is_absolute then path = "/" .. path end
return path
end

0 comments on commit f27a87a

Please sign in to comment.