diff options
| author | Diego Nehab <diego@tecgraf.puc-rio.br> | 2005-11-22 08:33:29 +0000 |
|---|---|---|
| committer | Diego Nehab <diego@tecgraf.puc-rio.br> | 2005-11-22 08:33:29 +0000 |
| commit | d55a5826e81136a9ecf65c4cd407152a56684dc2 (patch) | |
| tree | 109ad44c75cee890ad5e98583e12b15b5e65a18e /src/http.lua | |
| parent | a2b780bf7a78c66d54a248fa99b5fc862c12a127 (diff) | |
| download | luasocket-d55a5826e81136a9ecf65c4cd407152a56684dc2.tar.gz luasocket-d55a5826e81136a9ecf65c4cd407152a56684dc2.tar.bz2 luasocket-d55a5826e81136a9ecf65c4cd407152a56684dc2.zip | |
Few tweaks in installation, some missing files, etc.
Diffstat (limited to 'src/http.lua')
| -rw-r--r-- | src/http.lua | 32 |
1 files changed, 16 insertions, 16 deletions
diff --git a/src/http.lua b/src/http.lua index f08da07..1834061 100644 --- a/src/http.lua +++ b/src/http.lua | |||
| @@ -21,7 +21,7 @@ module("socket.http") | |||
| 21 | -- Program constants | 21 | -- Program constants |
| 22 | ----------------------------------------------------------------------------- | 22 | ----------------------------------------------------------------------------- |
| 23 | -- connection timeout in seconds | 23 | -- connection timeout in seconds |
| 24 | TIMEOUT = 60 | 24 | TIMEOUT = 60 |
| 25 | -- default port for document retrieval | 25 | -- default port for document retrieval |
| 26 | PORT = 80 | 26 | PORT = 80 |
| 27 | -- user agent field sent in request | 27 | -- user agent field sent in request |
| @@ -65,18 +65,18 @@ socket.sourcet["http-chunked"] = function(sock, headers) | |||
| 65 | return base.setmetatable({ | 65 | return base.setmetatable({ |
| 66 | getfd = function() return sock:getfd() end, | 66 | getfd = function() return sock:getfd() end, |
| 67 | dirty = function() return sock:dirty() end | 67 | dirty = function() return sock:dirty() end |
| 68 | }, { | 68 | }, { |
| 69 | __call = function() | 69 | __call = function() |
| 70 | -- get chunk size, skip extention | 70 | -- get chunk size, skip extention |
| 71 | local line, err = sock:receive() | 71 | local line, err = sock:receive() |
| 72 | if err then return nil, err end | 72 | if err then return nil, err end |
| 73 | local size = base.tonumber(string.gsub(line, ";.*", ""), 16) | 73 | local size = base.tonumber(string.gsub(line, ";.*", ""), 16) |
| 74 | if not size then return nil, "invalid chunk size" end | 74 | if not size then return nil, "invalid chunk size" end |
| 75 | -- was it the last chunk? | 75 | -- was it the last chunk? |
| 76 | if size > 0 then | 76 | if size > 0 then |
| 77 | -- if not, get chunk and skip terminating CRLF | 77 | -- if not, get chunk and skip terminating CRLF |
| 78 | local chunk, err, part = sock:receive(size) | 78 | local chunk, err, part = sock:receive(size) |
| 79 | if chunk then sock:receive() end | 79 | if chunk then sock:receive() end |
| 80 | return chunk, err | 80 | return chunk, err |
| 81 | else | 81 | else |
| 82 | -- if it was, read trailers into headers table | 82 | -- if it was, read trailers into headers table |
| @@ -91,7 +91,7 @@ socket.sinkt["http-chunked"] = function(sock) | |||
| 91 | return base.setmetatable({ | 91 | return base.setmetatable({ |
| 92 | getfd = function() return sock:getfd() end, | 92 | getfd = function() return sock:getfd() end, |
| 93 | dirty = function() return sock:dirty() end | 93 | dirty = function() return sock:dirty() end |
| 94 | }, { | 94 | }, { |
| 95 | __call = function(self, chunk, err) | 95 | __call = function(self, chunk, err) |
| 96 | if not chunk then return sock:send("0\r\n\r\n") end | 96 | if not chunk then return sock:send("0\r\n\r\n") end |
| 97 | local size = string.format("%X\r\n", string.len(chunk)) | 97 | local size = string.format("%X\r\n", string.len(chunk)) |
| @@ -115,11 +115,11 @@ function open(host, port, create) | |||
| 115 | h.try(c:settimeout(TIMEOUT)) | 115 | h.try(c:settimeout(TIMEOUT)) |
| 116 | h.try(c:connect(host, port or PORT)) | 116 | h.try(c:connect(host, port or PORT)) |
| 117 | -- here everything worked | 117 | -- here everything worked |
| 118 | return h | 118 | return h |
| 119 | end | 119 | end |
| 120 | 120 | ||
| 121 | function metat.__index:sendrequestline(method, uri) | 121 | function metat.__index:sendrequestline(method, uri) |
| 122 | local reqline = string.format("%s %s HTTP/1.1\r\n", method or "GET", uri) | 122 | local reqline = string.format("%s %s HTTP/1.1\r\n", method or "GET", uri) |
| 123 | return self.try(self.c:send(reqline)) | 123 | return self.try(self.c:send(reqline)) |
| 124 | end | 124 | end |
| 125 | 125 | ||
| @@ -133,7 +133,7 @@ function metat.__index:sendheaders(headers) | |||
| 133 | end | 133 | end |
| 134 | 134 | ||
| 135 | function metat.__index:sendbody(headers, source, step) | 135 | function metat.__index:sendbody(headers, source, step) |
| 136 | source = source or ltn12.source.empty() | 136 | source = source or ltn12.source.empty() |
| 137 | step = step or ltn12.pump.step | 137 | step = step or ltn12.pump.step |
| 138 | -- if we don't know the size in advance, send chunked and hope for the best | 138 | -- if we don't know the size in advance, send chunked and hope for the best |
| 139 | local mode = "http-chunked" | 139 | local mode = "http-chunked" |
| @@ -159,7 +159,7 @@ function metat.__index:receivebody(headers, sink, step) | |||
| 159 | local mode = "default" -- connection close | 159 | local mode = "default" -- connection close |
| 160 | if t and t ~= "identity" then mode = "http-chunked" | 160 | if t and t ~= "identity" then mode = "http-chunked" |
| 161 | elseif base.tonumber(headers["content-length"]) then mode = "by-length" end | 161 | elseif base.tonumber(headers["content-length"]) then mode = "by-length" end |
| 162 | return self.try(ltn12.pump.all(socket.source(mode, self.c, length), | 162 | return self.try(ltn12.pump.all(socket.source(mode, self.c, length), |
| 163 | sink, step)) | 163 | sink, step)) |
| 164 | end | 164 | end |
| 165 | 165 | ||
| @@ -185,7 +185,7 @@ local function adjusturi(reqt) | |||
| 185 | end | 185 | end |
| 186 | 186 | ||
| 187 | local function adjustproxy(reqt) | 187 | local function adjustproxy(reqt) |
| 188 | local proxy = reqt.proxy or PROXY | 188 | local proxy = reqt.proxy or PROXY |
| 189 | if proxy then | 189 | if proxy then |
| 190 | proxy = url.parse(proxy) | 190 | proxy = url.parse(proxy) |
| 191 | return proxy.host, proxy.port or 3128 | 191 | return proxy.host, proxy.port or 3128 |
| @@ -292,10 +292,10 @@ function trequest(reqt) | |||
| 292 | local code, headers, status | 292 | local code, headers, status |
| 293 | code, status = h:receivestatusline() | 293 | code, status = h:receivestatusline() |
| 294 | headers = h:receiveheaders() | 294 | headers = h:receiveheaders() |
| 295 | if shouldredirect(reqt, code, headers) then | 295 | if shouldredirect(reqt, code, headers) then |
| 296 | h:close() | 296 | h:close() |
| 297 | return tredirect(reqt, headers.location) | 297 | return tredirect(reqt, headers.location) |
| 298 | elseif shouldauthorize(reqt, code) then | 298 | elseif shouldauthorize(reqt, code) then |
| 299 | h:close() | 299 | h:close() |
| 300 | return tauthorize(reqt) | 300 | return tauthorize(reqt) |
| 301 | elseif shouldreceivebody(reqt, code) then | 301 | elseif shouldreceivebody(reqt, code) then |
| @@ -307,12 +307,12 @@ end | |||
| 307 | 307 | ||
| 308 | local function srequest(u, body) | 308 | local function srequest(u, body) |
| 309 | local t = {} | 309 | local t = {} |
| 310 | local reqt = { | 310 | local reqt = { |
| 311 | url = u, | 311 | url = u, |
| 312 | sink = ltn12.sink.table(t) | 312 | sink = ltn12.sink.table(t) |
| 313 | } | 313 | } |
| 314 | if body then | 314 | if body then |
| 315 | reqt.source = ltn12.source.string(body) | 315 | reqt.source = ltn12.source.string(body) |
| 316 | reqt.headers = { ["content-length"] = string.len(body) } | 316 | reqt.headers = { ["content-length"] = string.len(body) } |
| 317 | reqt.method = "POST" | 317 | reqt.method = "POST" |
| 318 | end | 318 | end |
