aboutsummaryrefslogtreecommitdiff
path: root/src/http.lua
diff options
context:
space:
mode:
authorDiego Nehab <diego@tecgraf.puc-rio.br>2004-11-27 07:58:04 +0000
committerDiego Nehab <diego@tecgraf.puc-rio.br>2004-11-27 07:58:04 +0000
commit7c97e8e40aaa665226fb54449773dc3134e755b2 (patch)
tree47888d4c924fc24bf3b355bf58120ea3cdc74bc4 /src/http.lua
parenteb0fc857ddea6f084d338589e2a33d3e7d4eade6 (diff)
downloadluasocket-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.lua23
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")
12local url = require("socket.url") 12local url = require("socket.url")
13local ltn12 = require("ltn12") 13local ltn12 = require("ltn12")
14local mime = require("mime") 14local mime = require("mime")
15 15local string = require("string")
16module("socket.http") 16local base = require("base")
17local table = require("table")
18local http = module("socket.http")
17 19
18----------------------------------------------------------------------------- 20-----------------------------------------------------------------------------
19-- Program constants 21-- Program constants
@@ -32,7 +34,7 @@ local metat = { __index = {} }
32 34
33function open(host, port) 35function 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)
46end 48end
47 49
48function metat.__index:sendheaders(headers) 50function 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
66function metat.__index:receivestatusline() 68function 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)
70end 72end
71 73
72function metat.__index:receiveheaders() 74function metat.__index:receiveheaders()
@@ -97,11 +99,11 @@ end
97function metat.__index:receivebody(headers, sink, step) 99function 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))
107end 109end
@@ -159,9 +161,10 @@ local default = {
159local function adjustrequest(reqt) 161local 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)
253end 256end
254 257
255request = socket.protect(function(reqt, body) 258request = 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
258end) 261end)
262
263base.setmetatable(http, nil)