diff options
author | Diego Nehab <diego@tecgraf.puc-rio.br> | 2005-08-12 05:56:32 +0000 |
---|---|---|
committer | Diego Nehab <diego@tecgraf.puc-rio.br> | 2005-08-12 05:56:32 +0000 |
commit | 0c3cdd5ef2485a79d6fec9261f2850c41577d5b3 (patch) | |
tree | d69164c9f815e2d0308ba3f0d15b18e67163d879 /src/http.lua | |
parent | 37f7af4b9f1250e3c3439df03d43cf291a4d6f37 (diff) | |
download | luasocket-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.lua | 45 |
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") | |||
16 | local base = _G | 16 | local base = _G |
17 | local table = require("table") | 17 | local table = require("table") |
18 | module("socket.http") | 18 | module("socket.http") |
19 | getmetatable(_M).__index = nil | ||
19 | 20 | ||
20 | ----------------------------------------------------------------------------- | 21 | ----------------------------------------------------------------------------- |
21 | -- Program constants | 22 | -- Program constants |
@@ -105,26 +106,16 @@ end | |||
105 | ----------------------------------------------------------------------------- | 106 | ----------------------------------------------------------------------------- |
106 | local metat = { __index = {} } | 107 | local metat = { __index = {} } |
107 | 108 | ||
108 | -- default connect function, respecting the timeout | ||
109 | local 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 | ||
119 | end | ||
120 | |||
121 | function open(host, port, create) | 109 | function 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 |
129 | end | 120 | end |
130 | 121 | ||
@@ -134,11 +125,11 @@ function metat.__index:sendrequestline(method, uri) | |||
134 | end | 125 | end |
135 | 126 | ||
136 | function metat.__index:sendheaders(headers) | 127 | function 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 |
143 | end | 134 | end |
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) |
277 | end | 268 | end |
278 | 269 | ||
279 | function tredirect(reqt, headers) | 270 | function 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 | ||
291 | end | 285 | end |
292 | 286 | ||
293 | function trequest(reqt) | 287 | function 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 |
333 | end) | 327 | end) |
334 | 328 | ||
335 | --getmetatable(_M).__index = nil | ||