diff options
| author | Diego Nehab <diego@tecgraf.puc-rio.br> | 2004-11-27 07:58:04 +0000 |
|---|---|---|
| committer | Diego Nehab <diego@tecgraf.puc-rio.br> | 2004-11-27 07:58:04 +0000 |
| commit | 7c97e8e40aaa665226fb54449773dc3134e755b2 (patch) | |
| tree | 47888d4c924fc24bf3b355bf58120ea3cdc74bc4 /src/http.lua | |
| parent | eb0fc857ddea6f084d338589e2a33d3e7d4eade6 (diff) | |
| download | luasocket-7c97e8e40aaa665226fb54449773dc3134e755b2.tar.gz luasocket-7c97e8e40aaa665226fb54449773dc3134e755b2.tar.bz2 luasocket-7c97e8e40aaa665226fb54449773dc3134e755b2.zip | |
Almost ready for beta3
Diffstat (limited to 'src/http.lua')
| -rw-r--r-- | src/http.lua | 23 |
1 files changed, 14 insertions, 9 deletions
diff --git a/src/http.lua b/src/http.lua index b265650..a15ea69 100644 --- a/src/http.lua +++ b/src/http.lua | |||
| @@ -12,8 +12,10 @@ local socket = require("socket") | |||
| 12 | local url = require("socket.url") | 12 | local url = require("socket.url") |
| 13 | local ltn12 = require("ltn12") | 13 | local ltn12 = require("ltn12") |
| 14 | local mime = require("mime") | 14 | local mime = require("mime") |
| 15 | 15 | local string = require("string") | |
| 16 | module("socket.http") | 16 | local base = require("base") |
| 17 | local table = require("table") | ||
| 18 | local http = module("socket.http") | ||
| 17 | 19 | ||
| 18 | ----------------------------------------------------------------------------- | 20 | ----------------------------------------------------------------------------- |
| 19 | -- Program constants | 21 | -- Program constants |
| @@ -32,7 +34,7 @@ local metat = { __index = {} } | |||
| 32 | 34 | ||
| 33 | function open(host, port) | 35 | function open(host, port) |
| 34 | local c = socket.try(socket.tcp()) | 36 | local c = socket.try(socket.tcp()) |
| 35 | local h = setmetatable({ c = c }, metat) | 37 | local h = base.setmetatable({ c = c }, metat) |
| 36 | -- make sure the connection gets closed on exception | 38 | -- make sure the connection gets closed on exception |
| 37 | h.try = socket.newtry(function() h:close() end) | 39 | h.try = socket.newtry(function() h:close() end) |
| 38 | h.try(c:settimeout(TIMEOUT)) | 40 | h.try(c:settimeout(TIMEOUT)) |
| @@ -46,7 +48,7 @@ function metat.__index:sendrequestline(method, uri) | |||
| 46 | end | 48 | end |
| 47 | 49 | ||
| 48 | function metat.__index:sendheaders(headers) | 50 | function metat.__index:sendheaders(headers) |
| 49 | for i, v in pairs(headers) do | 51 | for i, v in base.pairs(headers) do |
| 50 | self.try(self.c:send(i .. ": " .. v .. "\r\n")) | 52 | self.try(self.c:send(i .. ": " .. v .. "\r\n")) |
| 51 | end | 53 | end |
| 52 | -- mark end of request headers | 54 | -- mark end of request headers |
| @@ -66,7 +68,7 @@ end | |||
| 66 | function metat.__index:receivestatusline() | 68 | function metat.__index:receivestatusline() |
| 67 | local status = self.try(self.c:receive()) | 69 | local status = self.try(self.c:receive()) |
| 68 | local code = socket.skip(2, string.find(status, "HTTP/%d*%.%d* (%d%d%d)")) | 70 | local code = socket.skip(2, string.find(status, "HTTP/%d*%.%d* (%d%d%d)")) |
| 69 | return self.try(tonumber(code), status) | 71 | return self.try(base.tonumber(code), status) |
| 70 | end | 72 | end |
| 71 | 73 | ||
| 72 | function metat.__index:receiveheaders() | 74 | function metat.__index:receiveheaders() |
| @@ -97,11 +99,11 @@ end | |||
| 97 | function metat.__index:receivebody(headers, sink, step) | 99 | function metat.__index:receivebody(headers, sink, step) |
| 98 | sink = sink or ltn12.sink.null() | 100 | sink = sink or ltn12.sink.null() |
| 99 | step = step or ltn12.pump.step | 101 | step = step or ltn12.pump.step |
| 100 | local length = tonumber(headers["content-length"]) | 102 | local length = base.tonumber(headers["content-length"]) |
| 101 | local TE = headers["transfer-encoding"] | 103 | local TE = headers["transfer-encoding"] |
| 102 | local mode = "default" -- connection close | 104 | local mode = "default" -- connection close |
| 103 | if TE and TE ~= "identity" then mode = "http-chunked" | 105 | if TE and TE ~= "identity" then mode = "http-chunked" |
| 104 | elseif tonumber(headers["content-length"]) then mode = "by-length" end | 106 | elseif base.tonumber(headers["content-length"]) then mode = "by-length" end |
| 105 | return self.try(ltn12.pump.all(socket.source(mode, self.c, length), | 107 | return self.try(ltn12.pump.all(socket.source(mode, self.c, length), |
| 106 | sink, step)) | 108 | sink, step)) |
| 107 | end | 109 | end |
| @@ -159,9 +161,10 @@ local default = { | |||
| 159 | local function adjustrequest(reqt) | 161 | local function adjustrequest(reqt) |
| 160 | -- parse url if provided | 162 | -- parse url if provided |
| 161 | local nreqt = reqt.url and url.parse(reqt.url, default) or {} | 163 | local nreqt = reqt.url and url.parse(reqt.url, default) or {} |
| 164 | local t = url.parse(reqt.url, default) | ||
| 162 | -- explicit components override url | 165 | -- explicit components override url |
| 163 | for i,v in reqt do nreqt[i] = reqt[i] end | 166 | for i,v in reqt do nreqt[i] = reqt[i] end |
| 164 | socket.try(nreqt.host, "invalid host '" .. tostring(nreqt.host) .. "'") | 167 | socket.try(nreqt.host, "invalid host '" .. base.tostring(nreqt.host) .. "'") |
| 165 | -- compute uri if user hasn't overriden | 168 | -- compute uri if user hasn't overriden |
| 166 | nreqt.uri = reqt.uri or adjusturi(nreqt) | 169 | nreqt.uri = reqt.uri or adjusturi(nreqt) |
| 167 | -- ajust host and port if there is a proxy | 170 | -- ajust host and port if there is a proxy |
| @@ -253,6 +256,8 @@ local function srequest(u, body) | |||
| 253 | end | 256 | end |
| 254 | 257 | ||
| 255 | request = socket.protect(function(reqt, body) | 258 | request = socket.protect(function(reqt, body) |
| 256 | if type(reqt) == "string" then return srequest(reqt, body) | 259 | if base.type(reqt) == "string" then return srequest(reqt, body) |
| 257 | else return trequest(reqt) end | 260 | else return trequest(reqt) end |
| 258 | end) | 261 | end) |
| 262 | |||
| 263 | base.setmetatable(http, nil) | ||
