aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDiego Nehab <diego@tecgraf.puc-rio.br>2006-03-14 09:04:15 +0000
committerDiego Nehab <diego@tecgraf.puc-rio.br>2006-03-14 09:04:15 +0000
commit09ad4b299c59c3c5c6c442f0ee99f3c9e8dbe8ce (patch)
tree3d56df0d8e1a22386877f8d5007c67fbd98777f7
parent6248b915cba040a3d04fedd100637e1465a43cc7 (diff)
downloadluasocket-09ad4b299c59c3c5c6c442f0ee99f3c9e8dbe8ce.tar.gz
luasocket-09ad4b299c59c3c5c6c442f0ee99f3c9e8dbe8ce.tar.bz2
luasocket-09ad4b299c59c3c5c6c442f0ee99f3c9e8dbe8ce.zip
Chose option 1) for http.lua.
Need to fix everything to make sure it works with the new compat-5.1
-rw-r--r--src/ftp.lua2
-rw-r--r--src/http.lua69
-rw-r--r--src/smtp.lua2
-rw-r--r--src/tp.lua4
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"
36local metat = { __index = {} } 36local metat = { __index = {} }
37 37
38function open(server, port, create) 38function open(server, port, create)
39 local tp = socket.try(tp.connect(server, port or PORT, create, TIMEOUT)) 39 local tp = socket.try(tp.connect(server, port or PORT, TIMEOUT, create))
40 local f = base.setmetatable({ tp = tp }, metat) 40 local f = base.setmetatable({ tp = tp }, metat)
41 -- make sure everything gets closed in an exception 41 -- make sure everything gets closed in an exception
42 f.try = socket.newtry(function() f:close() end) 42 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)
203 ["connection"] = "close, TE", 203 ["connection"] = "close, TE",
204 ["te"] = "trailers" 204 ["te"] = "trailers"
205 } 205 }
206 -- if we are sending a body, tell server to let us know 206 -- if we have authentication information, pass it along
207 -- if it this is a waste of time 207 if reqt.user and reqt.password then
208 if reqt.source then lower["expect"] = "100-continue" end 208 lower["authorization"] =
209 "Basic " .. (mime.b64(reqt.user .. ":" .. reqt.password))
210 end
209 -- override with user headers 211 -- override with user headers
210 for i,v in base.pairs(reqt.headers or lower) do 212 for i,v in base.pairs(reqt.headers or lower) do
211 lower[string.lower(i)] = v 213 lower[string.lower(i)] = v
@@ -246,14 +248,6 @@ local function shouldredirect(reqt, code, headers)
246 and (not reqt.nredirects or reqt.nredirects < 5) 248 and (not reqt.nredirects or reqt.nredirects < 5)
247end 249end
248 250
249local function shouldauthorize(reqt, code)
250 -- if there has been an authorization attempt, it must have failed
251 if reqt.headers and reqt.headers["authorization"] then return nil end
252 -- if last attempt didn't fail due to lack of authentication,
253 -- or we don't have authorization information, we can't retry
254 return code == 401 and reqt.user and reqt.password
255end
256
257local function shouldreceivebody(reqt, code) 251local function shouldreceivebody(reqt, code)
258 if reqt.method == "HEAD" then return nil end 252 if reqt.method == "HEAD" then return nil end
259 if code == 204 or code == 304 then return nil end 253 if code == 204 or code == 304 then return nil end
@@ -261,15 +255,8 @@ local function shouldreceivebody(reqt, code)
261 return 1 255 return 1
262end 256end
263 257
264
265-- forward declarations 258-- forward declarations
266local trequest, tauthorize, tredirect 259local trequest, tredirect
267
268function tauthorize(reqt)
269 local auth = "Basic " .. (mime.b64(reqt.user .. ":" .. reqt.password))
270 reqt.headers["authorization"] = auth
271 return trequest(reqt)
272end
273 260
274function tredirect(reqt, location) 261function tredirect(reqt, location)
275 local result, code, headers, status = trequest { 262 local result, code, headers, status = trequest {
@@ -300,42 +287,28 @@ function trequest(reqt)
300 local headers, status 287 local headers, status
301 -- if there is a body, check for server status 288 -- if there is a body, check for server status
302 if reqt.source then 289 if reqt.source then
303 local ready = socket.select({h.c}, nil, TIMEOUT) 290 h:sendbody(reqt.headers, reqt.source, reqt.step)
304 if ready[h.c] then
305 -- here the server sent us something
306 code, status = h:receivestatusline()
307 headers = h:receiveheaders()
308 end
309 -- if server is happy, send body
310 if code == 100 then
311 h:sendbody(reqt.headers, reqt.source, reqt.step)
312 -- can't send body again!
313 reqt.source = nil
314 end
315 end 291 end
316 -- ignore all further 100-continue messages 292 -- ignore any 100-continue messages
317 while code == 100 do 293 while code == 100 do
318 code, status = h:receivestatusline() 294 code, status = h:receivestatusline()
319 headers = h:receiveheaders() 295 headers = h:receiveheaders()
320 end 296 end
321 -- at this point we should have a honest reply from the server 297 -- at this point we should have a honest reply from the server
322 if shouldredirect(reqt, code, headers) then 298 -- 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
323 h:close() 300 h:close()
324 return tredirect(reqt, headers.location) 301 return tredirect(reqt, headers.location)
325 elseif shouldauthorize(reqt, code) then
326 h:close()
327 return tauthorize(reqt)
328 else
329 -- here we are finally done
330 if shouldreceivebody(reqt, code) then
331 h:receivebody(headers, reqt.sink, reqt.step)
332 end
333 h:close()
334 return 1, code, headers, status
335 end 302 end
303 -- here we are finally done
304 if shouldreceivebody(reqt, code) then
305 h:receivebody(headers, reqt.sink, reqt.step)
306 end
307 h:close()
308 return 1, code, headers, status
336end 309end
337 310
338local function srequest(u, b, h) 311local function srequest(u, b)
339 local t = {} 312 local t = {}
340 local reqt = { 313 local reqt = {
341 url = u, 314 url = u,
@@ -343,10 +316,10 @@ local function srequest(u, b, h)
343 } 316 }
344 if b then 317 if b then
345 reqt.source = ltn12.source.string(b) 318 reqt.source = ltn12.source.string(b)
346 reqt.headers = h or {} 319 reqt.headers = {
347 reqt.headers["content-length"] = string.len(b) 320 ["content-length"] = string.len(b),
348 reqt.headers["content-type"] = reqt.headers["content-type"] or 321 ["content-type"] = "application/x-www-form-urlencoded"
349 "application/x-www-form-urlencoded" 322 }
350 reqt.method = "POST" 323 reqt.method = "POST"
351 end 324 end
352 local code, headers, status = socket.skip(1, trequest(reqt)) 325 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
113 113
114function open(server, port, create) 114function open(server, port, create)
115 local tp = socket.try(tp.connect(server or SERVER, port or PORT, 115 local tp = socket.try(tp.connect(server or SERVER, port or PORT,
116 create, TIMEOUT)) 116 TIMEOUT, create))
117 local s = base.setmetatable({tp = tp}, metat) 117 local s = base.setmetatable({tp = tp}, metat)
118 -- make sure tp is closed if we get an exception 118 -- make sure tp is closed if we get an exception
119 s.try = socket.newtry(function() 119 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()
109end 109end
110 110
111-- connect with server and return c object 111-- connect with server and return c object
112function connect(host, port, create, timeout) 112function connect(host, port, timeout, create)
113 local c, e = (create or socket.tcp()) 113 local c, e = (create or socket.tcp)()
114 if not c then return nil, e end 114 if not c then return nil, e end
115 c:settimeout(timeout or TIMEOUT) 115 c:settimeout(timeout or TIMEOUT)
116 local r, e = c:connect(host, port) 116 local r, e = c:connect(host, port)