diff options
Diffstat (limited to 'src/http.lua')
| -rw-r--r-- | src/http.lua | 23 |
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 | ||
| 33 | function open(host, port) | 33 | function 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 |
| 40 | end | 41 | end |
| 41 | 42 | ||
| 42 | function metat.__index:sendrequestline(method, uri) | 43 | function 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)) |
| 64 | end | 64 | end |
| 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)) |
| 108 | end | 107 | end |
| @@ -191,9 +190,9 @@ function tauthorize(reqt) | |||
| 191 | end | 190 | end |
| 192 | 191 | ||
| 193 | function tredirect(reqt, headers) | 192 | function 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, |
