aboutsummaryrefslogtreecommitdiff
path: root/src/http.lua
diff options
context:
space:
mode:
authorDiego Nehab <diego@tecgraf.puc-rio.br>2004-06-18 21:41:44 +0000
committerDiego Nehab <diego@tecgraf.puc-rio.br>2004-06-18 21:41:44 +0000
commit7ed89c97f760600df238f9853ee453570935870f (patch)
treea17573a43815071e35f85557519fefca739ef7ef /src/http.lua
parentac4aac0909da26befaaeb6b415f66cf35b6980e0 (diff)
downloadluasocket-7ed89c97f760600df238f9853ee453570935870f.tar.gz
luasocket-7ed89c97f760600df238f9853ee453570935870f.tar.bz2
luasocket-7ed89c97f760600df238f9853ee453570935870f.zip
2.0 alpha RELEASED!
Diffstat (limited to 'src/http.lua')
-rw-r--r--src/http.lua23
1 files changed, 11 insertions, 12 deletions
diff --git a/src/http.lua b/src/http.lua
index d8889e1..9c568bc 100644
--- a/src/http.lua
+++ b/src/http.lua
@@ -32,11 +32,12 @@ local metat = { __index = {} }
32 32
33function open(host, port) 33function open(host, port)
34 local c = socket.try(socket.tcp()) 34 local c = socket.try(socket.tcp())
35 local h = setmetatable({ c = c }, metat)
35 -- make sure the connection gets closed on exception 36 -- make sure the connection gets closed on exception
36 local try = socket.newtry(function() c:close() end) 37 h.try = socket.newtry(function() h:close() end)
37 try(c:settimeout(TIMEOUT)) 38 h.try(c:settimeout(TIMEOUT))
38 try(c:connect(host, port or PORT)) 39 h.try(c:connect(host, port or PORT))
39 return setmetatable({ c = c, try = try }, metat) 40 return h
40end 41end
41 42
42function metat.__index:sendrequestline(method, uri) 43function metat.__index:sendrequestline(method, uri)
@@ -57,9 +58,8 @@ function metat.__index:sendbody(headers, source, step)
57 source = source or ltn12.source.empty() 58 source = source or ltn12.source.empty()
58 step = step or ltn12.pump.step 59 step = step or ltn12.pump.step
59 -- if we don't know the size in advance, send chunked and hope for the best 60 -- if we don't know the size in advance, send chunked and hope for the best
60 local mode 61 local mode = "http-chunked"
61 if headers["content-length"] then mode = "keep-open" 62 if headers["content-length"] then mode = "keep-open" end
62 else mode = "http-chunked" end
63 return self.try(ltn12.pump.all(source, socket.sink(mode, self.c), step)) 63 return self.try(ltn12.pump.all(source, socket.sink(mode, self.c), step))
64end 64end
65 65
@@ -99,10 +99,9 @@ function metat.__index:receivebody(headers, sink, step)
99 step = step or ltn12.pump.step 99 step = step or ltn12.pump.step
100 local length = tonumber(headers["content-length"]) 100 local length = tonumber(headers["content-length"])
101 local TE = headers["transfer-encoding"] 101 local TE = headers["transfer-encoding"]
102 local mode 102 local mode = "default" -- connection close
103 if TE and TE ~= "identity" then mode = "http-chunked" 103 if TE and TE ~= "identity" then mode = "http-chunked"
104 elseif tonumber(headers["content-length"]) then mode = "by-length" 104 elseif tonumber(headers["content-length"]) then mode = "by-length" end
105 else mode = "default" end
106 return self.try(ltn12.pump.all(socket.source(mode, self.c, length), 105 return self.try(ltn12.pump.all(socket.source(mode, self.c, length),
107 sink, step)) 106 sink, step))
108end 107end
@@ -191,9 +190,9 @@ function tauthorize(reqt)
191end 190end
192 191
193function tredirect(reqt, headers) 192function tredirect(reqt, headers)
194 -- the RFC says the redirect URL has to be absolute, but some
195 -- servers do not respect that
196 return trequest { 193 return trequest {
194 -- the RFC says the redirect URL has to be absolute, but some
195 -- servers do not respect that
197 url = url.absolute(reqt, headers["location"]), 196 url = url.absolute(reqt, headers["location"]),
198 source = reqt.source, 197 source = reqt.source,
199 sink = reqt.sink, 198 sink = reqt.sink,