Skip to content

Commit f27a87a

Browse files
committed
Upgraded luasocket from 2.0.1 to 2.0.2
1 parent b57100e commit f27a87a

File tree

8 files changed

+64
-38
lines changed

8 files changed

+64
-38
lines changed

lua/ltn12.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,3 +289,4 @@ function pump.all(src, snk, step)
289289
end
290290
end
291291
end
292+

lua/mime.lua

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
-- MIME support for the Lua language.
33
-- Author: Diego Nehab
44
-- Conforming to RFCs 2045-2049
5-
-- RCS ID: $Id: mime.lua,v 1.28 2005/11/22 08:33:29 diego Exp $
5+
-- RCS ID: $Id: mime.lua,v 1.29 2007/06/11 23:44:54 diego Exp $
66
-----------------------------------------------------------------------------
77

88
-----------------------------------------------------------------------------
@@ -27,7 +27,8 @@ local function choose(table)
2727
name, opt1, opt2 = "default", name, opt1
2828
end
2929
local f = table[name or "nil"]
30-
if not f then error("unknown key (" .. base.tostring(name) .. ")", 3)
30+
if not f then
31+
base.error("unknown key (" .. base.tostring(name) .. ")", 3)
3132
else return f(opt1, opt2) end
3233
end
3334
end

lua/socket.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,3 +130,4 @@ end
130130
sourcet["default"] = sourcet["until-closed"]
131131

132132
source = choose(sourcet)
133+

lua/socket/ftp.lua

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
-- FTP support for the Lua language
33
-- LuaSocket toolkit.
44
-- Author: Diego Nehab
5-
-- RCS ID: $Id: ftp.lua,v 1.44 2006/03/14 09:04:15 diego Exp $
5+
-- RCS ID: $Id: ftp.lua,v 1.45 2007/07/11 19:25:47 diego Exp $
66
-----------------------------------------------------------------------------
77

88
-----------------------------------------------------------------------------
@@ -88,7 +88,7 @@ function metat.__index:port(ip, port)
8888
ip, port = self.try(self.tp:getcontrol():getsockname())
8989
self.server = self.try(socket.bind(ip, 0))
9090
ip, port = self.try(self.server:getsockname())
91-
self.try(server:settimeout(TIMEOUT))
91+
self.try(self.server:settimeout(TIMEOUT))
9292
end
9393
local pl = math.mod(port, 256)
9494
local ph = (port - pl)/256
@@ -187,9 +187,9 @@ end
187187
-----------------------------------------------------------------------------
188188
-- High level FTP API
189189
-----------------------------------------------------------------------------
190-
function override(t)
190+
local function override(t)
191191
if t.url then
192-
u = url.parse(t.url)
192+
local u = url.parse(t.url)
193193
for i,v in base.pairs(t) do
194194
u[i] = v
195195
end
@@ -278,3 +278,4 @@ get = socket.protect(function(gett)
278278
if base.type(gett) == "string" then return sget(gett)
279279
else return tget(gett) end
280280
end)
281+

lua/socket/http.lua

Lines changed: 37 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
-- HTTP/1.1 client support for the Lua language.
33
-- LuaSocket toolkit.
44
-- Author: Diego Nehab
5-
-- RCS ID: $Id: http.lua,v 1.67 2006/04/03 03:10:56 diego Exp $
5+
-- RCS ID: $Id: http.lua,v 1.70 2007/03/12 04:08:40 diego Exp $
66
-----------------------------------------------------------------------------
77

88
-----------------------------------------------------------------------------
@@ -15,7 +15,6 @@ local mime = require("mime")
1515
local string = require("string")
1616
local base = _G
1717
local table = require("table")
18-
local print = print
1918
module("socket.http")
2019

2120
-----------------------------------------------------------------------------
@@ -108,7 +107,7 @@ local metat = { __index = {} }
108107

109108
function open(host, port, create)
110109
-- create socket with user connect function, or with default
111-
local c = socket.try(create or socket.tcp)()
110+
local c = socket.try((create or socket.tcp)())
112111
local h = base.setmetatable({ c = c }, metat)
113112
-- create finalized try
114113
h.try = socket.newtry(function() h:close() end)
@@ -143,7 +142,12 @@ function metat.__index:sendbody(headers, source, step)
143142
end
144143

145144
function metat.__index:receivestatusline()
146-
local status = self.try(self.c:receive())
145+
local status = self.try(self.c:receive(5))
146+
-- identify HTTP/0.9 responses, which do not contain a status line
147+
-- this is just a heuristic, but is what the RFC recommends
148+
if status ~= "HTTP/" then return nil, status end
149+
-- otherwise proceed reading a status line
150+
status = self.try(self.c:receive("*l", status))
147151
local code = socket.skip(2, string.find(status, "HTTP/%d*%.%d* (%d%d%d)"))
148152
return self.try(base.tonumber(code), status)
149153
end
@@ -164,6 +168,12 @@ function metat.__index:receivebody(headers, sink, step)
164168
sink, step))
165169
end
166170

171+
function metat.__index:receive09body(status, sink, step)
172+
local source = ltn12.source.rewind(socket.source("until-closed", self.c))
173+
source(status)
174+
return self.try(ltn12.pump.all(source, sink, step))
175+
end
176+
167177
function metat.__index:close()
168178
return self.c:close()
169179
end
@@ -229,7 +239,8 @@ local function adjustrequest(reqt)
229239
-- explicit components override url
230240
for i,v in base.pairs(reqt) do nreqt[i] = v end
231241
if nreqt.port == "" then nreqt.port = 80 end
232-
socket.try(nreqt.host, "invalid host '" .. base.tostring(nreqt.host) .. "'")
242+
socket.try(nreqt.host and nreqt.host ~= "",
243+
"invalid host '" .. base.tostring(nreqt.host) .. "'")
233244
-- compute uri if user hasn't overriden
234245
nreqt.uri = reqt.uri or adjusturi(nreqt)
235246
-- ajust host and port if there is a proxy
@@ -262,7 +273,7 @@ function tredirect(reqt, location)
262273
local result, code, headers, status = trequest {
263274
-- the RFC says the redirect URL has to be absolute, but some
264275
-- servers do not respect that
265-
url = url.absolute(reqt, location),
276+
url = url.absolute(reqt.url, location),
266277
source = reqt.source,
267278
sink = reqt.sink,
268279
headers = reqt.headers,
@@ -271,38 +282,45 @@ function tredirect(reqt, location)
271282
create = reqt.create
272283
}
273284
-- pass location header back as a hint we redirected
285+
headers = headers or {}
274286
headers.location = headers.location or location
275287
return result, code, headers, status
276288
end
277289

278290
function trequest(reqt)
279291
-- we loop until we get what we want, or
280292
-- until we are sure there is no way to get it
281-
reqt = adjustrequest(reqt)
282-
local h = open(reqt.host, reqt.port, reqt.create)
293+
local nreqt = adjustrequest(reqt)
294+
local h = open(nreqt.host, nreqt.port, nreqt.create)
283295
-- send request line and headers
284-
h:sendrequestline(reqt.method, reqt.uri)
285-
h:sendheaders(reqt.headers)
286-
local code = 100
287-
local headers, status
288-
-- if there is a body, check for server status
289-
if reqt.source then
290-
h:sendbody(reqt.headers, reqt.source, reqt.step)
296+
h:sendrequestline(nreqt.method, nreqt.uri)
297+
h:sendheaders(nreqt.headers)
298+
-- if there is a body, send it
299+
if nreqt.source then
300+
h:sendbody(nreqt.headers, nreqt.source, nreqt.step)
301+
end
302+
local code, status = h:receivestatusline()
303+
-- if it is an HTTP/0.9 server, simply get the body and we are done
304+
if not code then
305+
h:receive09body(status, nreqt.sink, nreqt.step)
306+
return 1, 200
291307
end
308+
local headers
292309
-- ignore any 100-continue messages
293310
while code == 100 do
294-
code, status = h:receivestatusline()
295311
headers = h:receiveheaders()
312+
code, status = h:receivestatusline()
296313
end
314+
headers = h:receiveheaders()
297315
-- at this point we should have a honest reply from the server
298316
-- we can't redirect if we already used the source, so we report the error
299-
if shouldredirect(reqt, code, headers) and not reqt.source then
317+
if shouldredirect(nreqt, code, headers) and not nreqt.source then
300318
h:close()
301319
return tredirect(reqt, headers.location)
302320
end
303321
-- here we are finally done
304-
if shouldreceivebody(reqt, code) then
305-
h:receivebody(headers, reqt.sink, reqt.step)
322+
if shouldreceivebody(nreqt, code) then
323+
h:receivebody(headers, nreqt.sink, nreqt.step)
306324
end
307325
h:close()
308326
return 1, code, headers, status
@@ -330,4 +348,3 @@ request = socket.protect(function(reqt, body)
330348
if base.type(reqt) == "string" then return srequest(reqt, body)
331349
else return trequest(reqt) end
332350
end)
333-

lua/socket/smtp.lua

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
-- SMTP client support for the Lua language.
33
-- LuaSocket toolkit.
44
-- Author: Diego Nehab
5-
-- RCS ID: $Id: smtp.lua,v 1.45 2006/03/14 09:04:15 diego Exp $
5+
-- RCS ID: $Id: smtp.lua,v 1.46 2007/03/12 04:08:40 diego Exp $
66
-----------------------------------------------------------------------------
77

88
-----------------------------------------------------------------------------
@@ -122,6 +122,15 @@ function open(server, port, create)
122122
return s
123123
end
124124

125+
-- convert headers to lowercase
126+
local function lower_headers(headers)
127+
local lower = {}
128+
for i,v in base.pairs(headers or lower) do
129+
lower[string.lower(i)] = v
130+
end
131+
return lower
132+
end
133+
125134
---------------------------------------------------------------------------
126135
-- Multipart message source
127136
-----------------------------------------------------------------------------
@@ -149,7 +158,7 @@ end
149158
local function send_multipart(mesgt)
150159
-- make sure we have our boundary and send headers
151160
local bd = newboundary()
152-
local headers = mesgt.headers or {}
161+
local headers = lower_headers(mesgt.headers or {})
153162
headers['content-type'] = headers['content-type'] or 'multipart/mixed'
154163
headers['content-type'] = headers['content-type'] ..
155164
'; boundary="' .. bd .. '"'
@@ -176,7 +185,7 @@ end
176185
-- yield message body from a source
177186
local function send_source(mesgt)
178187
-- make sure we have a content-type
179-
local headers = mesgt.headers or {}
188+
local headers = lower_headers(mesgt.headers or {})
180189
headers['content-type'] = headers['content-type'] or
181190
'text/plain; charset="iso-8859-1"'
182191
send_headers(headers)
@@ -192,7 +201,7 @@ end
192201
-- yield message body from a string
193202
local function send_string(mesgt)
194203
-- make sure we have a content-type
195-
local headers = mesgt.headers or {}
204+
local headers = lower_headers(mesgt.headers or {})
196205
headers['content-type'] = headers['content-type'] or
197206
'text/plain; charset="iso-8859-1"'
198207
send_headers(headers)
@@ -209,20 +218,17 @@ end
209218

210219
-- set defaul headers
211220
local function adjust_headers(mesgt)
212-
local lower = {}
213-
for i,v in base.pairs(mesgt.headers or lower) do
214-
lower[string.lower(i)] = v
215-
end
221+
local lower = lower_headers(mesgt.headers)
216222
lower["date"] = lower["date"] or
217223
os.date("!%a, %d %b %Y %H:%M:%S ") .. (mesgt.zone or ZONE)
218224
lower["x-mailer"] = lower["x-mailer"] or socket._VERSION
219225
-- this can't be overriden
220226
lower["mime-version"] = "1.0"
221-
mesgt.headers = lower
227+
return lower
222228
end
223229

224230
function message(mesgt)
225-
adjust_headers(mesgt)
231+
mesgt.headers = adjust_headers(mesgt)
226232
-- create and return message source
227233
local co = coroutine.create(function() send_message(mesgt) end)
228234
return function()
@@ -243,4 +249,3 @@ send = socket.protect(function(mailt)
243249
s:quit()
244250
return s:close()
245251
end)
246-

lua/socket/tp.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,3 +120,4 @@ function connect(host, port, timeout, create)
120120
end
121121
return base.setmetatable({c = c}, metat)
122122
end
123+

lua/socket/url.lua

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,4 +295,3 @@ function build_path(parsed, unsafe)
295295
if parsed.is_absolute then path = "/" .. path end
296296
return path
297297
end
298-

0 commit comments

Comments
 (0)