aboutsummaryrefslogtreecommitdiff
path: root/src/http.lua
diff options
context:
space:
mode:
authorDiego Nehab <diego@tecgraf.puc-rio.br>2005-08-12 05:56:32 +0000
committerDiego Nehab <diego@tecgraf.puc-rio.br>2005-08-12 05:56:32 +0000
commit0c3cdd5ef2485a79d6fec9261f2850c41577d5b3 (patch)
treed69164c9f815e2d0308ba3f0d15b18e67163d879 /src/http.lua
parent37f7af4b9f1250e3c3439df03d43cf291a4d6f37 (diff)
downloadluasocket-0c3cdd5ef2485a79d6fec9261f2850c41577d5b3.tar.gz
luasocket-0c3cdd5ef2485a79d6fec9261f2850c41577d5b3.tar.bz2
luasocket-0c3cdd5ef2485a79d6fec9261f2850c41577d5b3.zip
Final push for release...
Diffstat (limited to 'src/http.lua')
-rw-r--r--src/http.lua45
1 files changed, 19 insertions, 26 deletions
diff --git a/src/http.lua b/src/http.lua
index 91c52da..9434d97 100644
--- a/src/http.lua
+++ b/src/http.lua
@@ -16,6 +16,7 @@ local string = require("string")
16local base = _G 16local base = _G
17local table = require("table") 17local table = require("table")
18module("socket.http") 18module("socket.http")
19getmetatable(_M).__index = nil
19 20
20----------------------------------------------------------------------------- 21-----------------------------------------------------------------------------
21-- Program constants 22-- Program constants
@@ -105,26 +106,16 @@ end
105----------------------------------------------------------------------------- 106-----------------------------------------------------------------------------
106local metat = { __index = {} } 107local metat = { __index = {} }
107 108
108-- default connect function, respecting the timeout
109local function connect(host, port, create)
110 local c, e = (create or socket.tcp)()
111 if not c then return nil, e end
112 c:settimeout(TIMEOUT)
113 local r, e = c:connect(host, port or PORT)
114 if not r then
115 c:close()
116 return nil, e
117 end
118 return c
119end
120
121function open(host, port, create) 109function open(host, port, create)
122 -- create socket with user connect function, or with default 110 -- create socket with user connect function, or with default
123 local c = socket.try(connect(host, port, create)) 111 local c = socket.try(create or socket.tcp)()
124 -- create our http request object, pointing to the socket
125 local h = base.setmetatable({ c = c }, metat) 112 local h = base.setmetatable({ c = c }, metat)
126 -- make sure the object close gets called on exception 113 -- create finalized try
127 h.try = socket.newtry(function() h:close() end) 114 h.try = socket.newtry(function() h:close() end)
115 -- set timeout before connecting
116 h.try(c:settimeout(TIMEOUT))
117 h.try(c:connect(host, port or PORT))
118 -- here everything worked
128 return h 119 return h
129end 120end
130 121
@@ -134,11 +125,11 @@ function metat.__index:sendrequestline(method, uri)
134end 125end
135 126
136function metat.__index:sendheaders(headers) 127function metat.__index:sendheaders(headers)
128 local h = "\r\n"
137 for i, v in base.pairs(headers) do 129 for i, v in base.pairs(headers) do
138 self.try(self.c:send(i .. ": " .. v .. "\r\n")) 130 h = i .. ": " .. v .. "\r\n" .. h
139 end 131 end
140 -- mark end of request headers 132 self.try(self.c:send(h))
141 self.try(self.c:send("\r\n"))
142 return 1 133 return 1
143end 134end
144 135
@@ -213,7 +204,7 @@ local function adjustheaders(headers, host)
213 ["te"] = "trailers" 204 ["te"] = "trailers"
214 } 205 }
215 -- override with user headers 206 -- override with user headers
216 for i,v in pairs(headers or lower) do 207 for i,v in base.pairs(headers or lower) do
217 lower[string.lower(i)] = v 208 lower[string.lower(i)] = v
218 end 209 end
219 return lower 210 return lower
@@ -232,7 +223,7 @@ local function adjustrequest(reqt)
232 local nreqt = reqt.url and url.parse(reqt.url, default) or {} 223 local nreqt = reqt.url and url.parse(reqt.url, default) or {}
233 local t = url.parse(reqt.url, default) 224 local t = url.parse(reqt.url, default)
234 -- explicit components override url 225 -- explicit components override url
235 for i,v in pairs(reqt) do nreqt[i] = v end 226 for i,v in base.pairs(reqt) do nreqt[i] = v end
236 socket.try(nreqt.host, "invalid host '" .. base.tostring(nreqt.host) .. "'") 227 socket.try(nreqt.host, "invalid host '" .. base.tostring(nreqt.host) .. "'")
237 -- compute uri if user hasn't overriden 228 -- compute uri if user hasn't overriden
238 nreqt.uri = reqt.uri or adjusturi(nreqt) 229 nreqt.uri = reqt.uri or adjusturi(nreqt)
@@ -276,11 +267,11 @@ function tauthorize(reqt)
276 return trequest(reqt) 267 return trequest(reqt)
277end 268end
278 269
279function tredirect(reqt, headers) 270function tredirect(reqt, location)
280 return trequest { 271 local result, code, headers, status = trequest {
281 -- the RFC says the redirect URL has to be absolute, but some 272 -- the RFC says the redirect URL has to be absolute, but some
282 -- servers do not respect that 273 -- servers do not respect that
283 url = url.absolute(reqt, headers["location"]), 274 url = url.absolute(reqt, location),
284 source = reqt.source, 275 source = reqt.source,
285 sink = reqt.sink, 276 sink = reqt.sink,
286 headers = reqt.headers, 277 headers = reqt.headers,
@@ -288,6 +279,9 @@ function tredirect(reqt, headers)
288 nredirects = (reqt.nredirects or 0) + 1, 279 nredirects = (reqt.nredirects or 0) + 1,
289 connect = reqt.connect 280 connect = reqt.connect
290 } 281 }
282 -- pass location header back as a hint we redirected
283 headers.location = headers.location or location
284 return result, code, headers, status
291end 285end
292 286
293function trequest(reqt) 287function trequest(reqt)
@@ -301,7 +295,7 @@ function trequest(reqt)
301 headers = h:receiveheaders() 295 headers = h:receiveheaders()
302 if shouldredirect(reqt, code, headers) then 296 if shouldredirect(reqt, code, headers) then
303 h:close() 297 h:close()
304 return tredirect(reqt, headers) 298 return tredirect(reqt, headers.location)
305 elseif shouldauthorize(reqt, code) then 299 elseif shouldauthorize(reqt, code) then
306 h:close() 300 h:close()
307 return tauthorize(reqt) 301 return tauthorize(reqt)
@@ -332,4 +326,3 @@ request = socket.protect(function(reqt, body)
332 else return trequest(reqt) end 326 else return trequest(reqt) end
333end) 327end)
334 328
335--getmetatable(_M).__index = nil