diff options
author | Diego Nehab <diego@tecgraf.puc-rio.br> | 2004-03-22 07:45:07 +0000 |
---|---|---|
committer | Diego Nehab <diego@tecgraf.puc-rio.br> | 2004-03-22 07:45:07 +0000 |
commit | 5b279bac9eea05d764e9cd14c89a699da8898126 (patch) | |
tree | 99878c46751c74e601d1de54c02e506dc5115b6c | |
parent | 1fa65d89ca5dc64756f7933d7cc3f524e4627dce (diff) | |
download | luasocket-5b279bac9eea05d764e9cd14c89a699da8898126.tar.gz luasocket-5b279bac9eea05d764e9cd14c89a699da8898126.tar.bz2 luasocket-5b279bac9eea05d764e9cd14c89a699da8898126.zip |
Settimeout wasn't returning 1...
http.lua is even cleaner. No trash in respt table.
-rw-r--r-- | TODO | 2 | ||||
-rw-r--r-- | src/http.lua | 122 | ||||
-rw-r--r-- | src/timeout.c | 3 |
3 files changed, 64 insertions, 63 deletions
@@ -19,6 +19,8 @@ | |||
19 | * Separar as classes em arquivos | 19 | * Separar as classes em arquivos |
20 | * Retorno de sendto em datagram sockets pode ser refused | 20 | * Retorno de sendto em datagram sockets pode ser refused |
21 | 21 | ||
22 | break smtp.send into c = smtp.open, c:send() c:close() | ||
23 | |||
22 | falar sobre encodet/wrapt/decodet no manual sobre mime | 24 | falar sobre encodet/wrapt/decodet no manual sobre mime |
23 | 25 | ||
24 | 26 | ||
diff --git a/src/http.lua b/src/http.lua index ab166e3..8b06184 100644 --- a/src/http.lua +++ b/src/http.lua | |||
@@ -39,10 +39,10 @@ local function third(a, b, c) | |||
39 | return c | 39 | return c |
40 | end | 40 | end |
41 | 41 | ||
42 | local function receive_headers(reqt, respt) | 42 | local function receive_headers(reqt, respt, tmp) |
43 | local headers = {} | 43 | local sock = tmp.sock |
44 | local sock = respt.tmp.sock | ||
45 | local line, name, value, _ | 44 | local line, name, value, _ |
45 | local headers = {} | ||
46 | -- store results | 46 | -- store results |
47 | respt.headers = headers | 47 | respt.headers = headers |
48 | -- get first line | 48 | -- get first line |
@@ -132,10 +132,10 @@ local function receive_body_untilclosed(sock, sink) | |||
132 | hand(sink, nil) | 132 | hand(sink, nil) |
133 | end | 133 | end |
134 | 134 | ||
135 | local function receive_body(reqt, respt) | 135 | local function receive_body(reqt, respt, tmp) |
136 | local sink = reqt.sink or ltn12.sink.null() | 136 | local sink = reqt.sink or ltn12.sink.null() |
137 | local headers = respt.headers | 137 | local headers = respt.headers |
138 | local sock = respt.tmp.sock | 138 | local sock = tmp.sock |
139 | local te = headers["transfer-encoding"] | 139 | local te = headers["transfer-encoding"] |
140 | if te and te ~= "identity" then | 140 | if te and te ~= "identity" then |
141 | -- get by chunked transfer-coding of message body | 141 | -- get by chunked transfer-coding of message body |
@@ -174,47 +174,50 @@ local function send_headers(sock, headers) | |||
174 | -- send request headers | 174 | -- send request headers |
175 | for i, v in pairs(headers) do | 175 | for i, v in pairs(headers) do |
176 | socket.try(sock:send(i .. ": " .. v .. "\r\n")) | 176 | socket.try(sock:send(i .. ": " .. v .. "\r\n")) |
177 | --io.write(i .. ": " .. v .. "\r\n") | ||
177 | end | 178 | end |
178 | -- mark end of request headers | 179 | -- mark end of request headers |
179 | socket.try(sock:send("\r\n")) | 180 | socket.try(sock:send("\r\n")) |
181 | --io.write("\r\n") | ||
180 | end | 182 | end |
181 | 183 | ||
182 | local function should_receive_body(reqt, respt) | 184 | local function should_receive_body(reqt, respt, tmp) |
183 | if reqt.method == "HEAD" then return nil end | 185 | if reqt.method == "HEAD" then return nil end |
184 | if respt.code == 204 or respt.code == 304 then return nil end | 186 | if respt.code == 204 or respt.code == 304 then return nil end |
185 | if respt.code >= 100 and respt.code < 200 then return nil end | 187 | if respt.code >= 100 and respt.code < 200 then return nil end |
186 | return 1 | 188 | return 1 |
187 | end | 189 | end |
188 | 190 | ||
189 | local function receive_status(reqt, respt) | 191 | local function receive_status(reqt, respt, tmp) |
190 | local sock = respt.tmp.sock | 192 | local status = socket.try(tmp.sock:receive()) |
191 | local status = socket.try(sock:receive()) | ||
192 | local code = third(string.find(status, "HTTP/%d*%.%d* (%d%d%d)")) | 193 | local code = third(string.find(status, "HTTP/%d*%.%d* (%d%d%d)")) |
193 | -- store results | 194 | -- store results |
194 | respt.code, respt.status = tonumber(code), status | 195 | respt.code, respt.status = tonumber(code), status |
195 | end | 196 | end |
196 | 197 | ||
197 | local function request_uri(reqt, respt) | 198 | local function request_uri(reqt, respt, tmp) |
198 | local url | 199 | local url = tmp.parsed |
199 | local parsed = respt.tmp.parsed | ||
200 | if not reqt.proxy then | 200 | if not reqt.proxy then |
201 | local parsed = tmp.parsed | ||
201 | url = { | 202 | url = { |
202 | path = parsed.path, | 203 | path = parsed.path, |
203 | params = parsed.params, | 204 | params = parsed.params, |
204 | query = parsed.query, | 205 | query = parsed.query, |
205 | fragment = parsed.fragment | 206 | fragment = parsed.fragment |
206 | } | 207 | } |
207 | else url = respt.tmp.parsed end | 208 | end |
208 | return socket.url.build(url) | 209 | return socket.url.build(url) |
209 | end | 210 | end |
210 | 211 | ||
211 | local function send_request(reqt, respt) | 212 | local function send_request(reqt, respt, tmp) |
212 | local uri = request_uri(reqt, respt) | 213 | local uri = request_uri(reqt, respt, tmp) |
213 | local sock = respt.tmp.sock | 214 | local sock = tmp.sock |
214 | local headers = respt.tmp.headers | 215 | local headers = tmp.headers |
215 | -- send request line | 216 | -- send request line |
216 | socket.try(sock:send((reqt.method or "GET") | 217 | socket.try(sock:send((reqt.method or "GET") |
217 | .. " " .. uri .. " HTTP/1.1\r\n")) | 218 | .. " " .. uri .. " HTTP/1.1\r\n")) |
219 | --io.write((reqt.method or "GET") | ||
220 | --.. " " .. uri .. " HTTP/1.1\r\n") | ||
218 | -- send request headers headeres | 221 | -- send request headers headeres |
219 | if reqt.source and not headers["content-length"] then | 222 | if reqt.source and not headers["content-length"] then |
220 | headers["transfer-encoding"] = "chunked" | 223 | headers["transfer-encoding"] = "chunked" |
@@ -227,8 +230,7 @@ local function send_request(reqt, respt) | |||
227 | end | 230 | end |
228 | end | 231 | end |
229 | 232 | ||
230 | local function open(reqt, respt) | 233 | local function open(reqt, respt, tmp) |
231 | local parsed = respt.tmp.parsed | ||
232 | local proxy = reqt.proxy or PROXY | 234 | local proxy = reqt.proxy or PROXY |
233 | local host, port | 235 | local host, port |
234 | if proxy then | 236 | if proxy then |
@@ -236,16 +238,15 @@ local function open(reqt, respt) | |||
236 | assert(pproxy.port and pproxy.host, "invalid proxy") | 238 | assert(pproxy.port and pproxy.host, "invalid proxy") |
237 | host, port = pproxy.host, pproxy.port | 239 | host, port = pproxy.host, pproxy.port |
238 | else | 240 | else |
239 | host, port = parsed.host, parsed.port | 241 | host, port = tmp.parsed.host, tmp.parsed.port |
240 | end | 242 | end |
241 | local sock = socket.try(socket.tcp()) | ||
242 | -- store results | 243 | -- store results |
243 | respt.tmp.sock = sock | 244 | tmp.sock = socket.try(socket.tcp()) |
244 | sock:settimeout(reqt.timeout or TIMEOUT) | 245 | socket.try(tmp.sock:settimeout(reqt.timeout or TIMEOUT)) |
245 | socket.try(sock:connect(host, port)) | 246 | socket.try(tmp.sock:connect(host, port)) |
246 | end | 247 | end |
247 | 248 | ||
248 | local function adjust_headers(reqt, respt) | 249 | local function adjust_headers(reqt, respt, tmp) |
249 | local lower = {} | 250 | local lower = {} |
250 | local headers = reqt.headers or {} | 251 | local headers = reqt.headers or {} |
251 | -- set default headers | 252 | -- set default headers |
@@ -254,14 +255,14 @@ local function adjust_headers(reqt, respt) | |||
254 | for i,v in headers do | 255 | for i,v in headers do |
255 | lower[string.lower(i)] = v | 256 | lower[string.lower(i)] = v |
256 | end | 257 | end |
257 | lower["host"] = respt.tmp.parsed.host | 258 | lower["host"] = tmp.parsed.host |
258 | -- this cannot be overriden | 259 | -- this cannot be overriden |
259 | lower["connection"] = "close" | 260 | lower["connection"] = "close" |
260 | -- store results | 261 | -- store results |
261 | respt.tmp.headers = lower | 262 | tmp.headers = lower |
262 | end | 263 | end |
263 | 264 | ||
264 | local function parse_url(reqt, respt) | 265 | local function parse_url(reqt, respt, tmp) |
265 | -- parse url with default fields | 266 | -- parse url with default fields |
266 | local parsed = socket.url.parse(reqt.url, { | 267 | local parsed = socket.url.parse(reqt.url, { |
267 | host = "", | 268 | host = "", |
@@ -277,19 +278,18 @@ local function parse_url(reqt, respt) | |||
277 | parsed.user = reqt.user or parsed.user | 278 | parsed.user = reqt.user or parsed.user |
278 | parsed.password = reqt.password or parsed.password | 279 | parsed.password = reqt.password or parsed.password |
279 | -- store results | 280 | -- store results |
280 | respt.tmp.parsed = parsed | 281 | tmp.parsed = parsed |
281 | end | 282 | end |
282 | 283 | ||
283 | -- forward declaration | 284 | -- forward declaration |
284 | local request_p | 285 | local request_p |
285 | 286 | ||
286 | local function should_authorize(reqt, respt) | 287 | local function should_authorize(reqt, respt, tmp) |
287 | -- if there has been an authorization attempt, it must have failed | 288 | -- if there has been an authorization attempt, it must have failed |
288 | if reqt.headers and reqt.headers["authorization"] then return nil end | 289 | if reqt.headers and reqt.headers["authorization"] then return nil end |
289 | -- if last attempt didn't fail due to lack of authentication, | 290 | -- if last attempt didn't fail due to lack of authentication, |
290 | -- or we don't have authorization information, we can't retry | 291 | -- or we don't have authorization information, we can't retry |
291 | return respt.code == 401 and | 292 | return respt.code == 401 and tmp.parsed.user and tmp.parsed.password |
292 | respt.tmp.parsed.user and respt.tmp.parsed.password | ||
293 | end | 293 | end |
294 | 294 | ||
295 | local function clone(headers) | 295 | local function clone(headers) |
@@ -301,11 +301,10 @@ local function clone(headers) | |||
301 | return copy | 301 | return copy |
302 | end | 302 | end |
303 | 303 | ||
304 | local function authorize(reqt, respt) | 304 | local function authorize(reqt, respt, tmp) |
305 | local headers = clone(reqt.headers) or {} | 305 | local headers = clone(reqt.headers) or {} |
306 | local parsed = respt.tmp.parsed | ||
307 | headers["authorization"] = "Basic " .. | 306 | headers["authorization"] = "Basic " .. |
308 | (mime.b64(parsed.user .. ":" .. parsed.password)) | 307 | (mime.b64(tmp.parsed.user .. ":" .. tmp.parsed.password)) |
309 | local autht = { | 308 | local autht = { |
310 | method = reqt.method, | 309 | method = reqt.method, |
311 | url = reqt.url, | 310 | url = reqt.url, |
@@ -315,18 +314,18 @@ local function authorize(reqt, respt) | |||
315 | timeout = reqt.timeout, | 314 | timeout = reqt.timeout, |
316 | proxy = reqt.proxy, | 315 | proxy = reqt.proxy, |
317 | } | 316 | } |
318 | request_p(autht, respt) | 317 | request_p(autht, respt, tmp) |
319 | end | 318 | end |
320 | 319 | ||
321 | local function should_redirect(reqt, respt) | 320 | local function should_redirect(reqt, respt, tmp) |
322 | return (reqt.redirect ~= false) and | 321 | return (reqt.redirect ~= false) and |
323 | (respt.code == 301 or respt.code == 302) and | 322 | (respt.code == 301 or respt.code == 302) and |
324 | (not reqt.method or reqt.method == "GET" or reqt.method == "HEAD") | 323 | (not reqt.method or reqt.method == "GET" or reqt.method == "HEAD") |
325 | and (not respt.tmp.nredirects or respt.tmp.nredirects < 5) | 324 | and (not tmp.nredirects or tmp.nredirects < 5) |
326 | end | 325 | end |
327 | 326 | ||
328 | local function redirect(reqt, respt) | 327 | local function redirect(reqt, respt, tmp) |
329 | respt.tmp.nredirects = (respt.tmp.nredirects or 0) + 1 | 328 | tmp.nredirects = (tmp.nredirects or 0) + 1 |
330 | local redirt = { | 329 | local redirt = { |
331 | method = reqt.method, | 330 | method = reqt.method, |
332 | -- the RFC says the redirect URL has to be absolute, but some | 331 | -- the RFC says the redirect URL has to be absolute, but some |
@@ -338,36 +337,35 @@ local function redirect(reqt, respt) | |||
338 | timeout = reqt.timeout, | 337 | timeout = reqt.timeout, |
339 | proxy = reqt.proxy | 338 | proxy = reqt.proxy |
340 | } | 339 | } |
341 | request_p(redirt, respt) | 340 | request_p(redirt, respt, tmp) |
342 | -- we pass the location header as a clue we redirected | 341 | -- we pass the location header as a clue we redirected |
343 | if respt.headers then respt.headers.location = redirt.url end | 342 | if respt.headers then respt.headers.location = redirt.url end |
344 | end | 343 | end |
345 | 344 | ||
346 | -- execute a request of through an exception | 345 | -- execute a request of through an exception |
347 | function request_p(reqt, respt) | 346 | function request_p(reqt, respt, tmp) |
348 | parse_url(reqt, respt) | 347 | parse_url(reqt, respt, tmp) |
349 | adjust_headers(reqt, respt) | 348 | adjust_headers(reqt, respt, tmp) |
350 | open(reqt, respt) | 349 | open(reqt, respt, tmp) |
351 | send_request(reqt, respt) | 350 | send_request(reqt, respt, tmp) |
352 | receive_status(reqt, respt) | 351 | receive_status(reqt, respt, tmp) |
353 | receive_headers(reqt, respt) | 352 | receive_headers(reqt, respt, tmp) |
354 | if should_redirect(reqt, respt) then | 353 | if should_redirect(reqt, respt, tmp) then |
355 | respt.tmp.sock:close() | 354 | tmp.sock:close() |
356 | redirect(reqt, respt) | 355 | redirect(reqt, respt, tmp) |
357 | elseif should_authorize(reqt, respt) then | 356 | elseif should_authorize(reqt, respt, tmp) then |
358 | respt.tmp.sock:close() | 357 | tmp.sock:close() |
359 | authorize(reqt, respt) | 358 | authorize(reqt, respt, tmp) |
360 | elseif should_receive_body(reqt, respt) then | 359 | elseif should_receive_body(reqt, respt, tmp) then |
361 | receive_body(reqt, respt) | 360 | receive_body(reqt, respt, tmp) |
362 | end | 361 | end |
363 | end | 362 | end |
364 | 363 | ||
365 | function request(reqt) | 364 | function request(reqt) |
366 | local respt = { tmp = {} } | 365 | local respt, tmp = {}, {} |
367 | local s, e = pcall(request_p, reqt, respt) | 366 | local s, e = pcall(request_p, reqt, respt, tmp) |
368 | if not s then respt.error = e end | 367 | if not s then respt.error = e end |
369 | if respt.tmp.sock then respt.tmp.sock:close() end | 368 | if tmp.sock then tmp.sock:close() end |
370 | respt.tmp = nil | ||
371 | return respt | 369 | return respt |
372 | end | 370 | end |
373 | 371 | ||
@@ -377,7 +375,7 @@ function get(url) | |||
377 | url = url, | 375 | url = url, |
378 | sink = ltn12.sink.table(t) | 376 | sink = ltn12.sink.table(t) |
379 | } | 377 | } |
380 | return table.getn(t) > 0 and table.concat(t), respt.headers, | 378 | return (table.getn(t) > 0 or nil) and table.concat(t), respt.headers, |
381 | respt.code, respt.error | 379 | respt.code, respt.error |
382 | end | 380 | end |
383 | 381 | ||
@@ -390,6 +388,6 @@ function post(url, body) | |||
390 | sink = ltn12.sink.table(t), | 388 | sink = ltn12.sink.table(t), |
391 | headers = { ["content-length"] = string.len(body) } | 389 | headers = { ["content-length"] = string.len(body) } |
392 | } | 390 | } |
393 | return table.getn(t) > 0 and table.concat(t), | 391 | return (table.getn(t) > 0 or nil) and table.concat(t), |
394 | respt.headers, respt.code, respt.error | 392 | respt.headers, respt.code, respt.error |
395 | end | 393 | end |
diff --git a/src/timeout.c b/src/timeout.c index 73a1146..1d710dc 100644 --- a/src/timeout.c +++ b/src/timeout.c | |||
@@ -169,7 +169,8 @@ int tm_meth_settimeout(lua_State *L, p_tm tm) | |||
169 | luaL_argcheck(L, 0, 3, "invalid timeout mode"); | 169 | luaL_argcheck(L, 0, 3, "invalid timeout mode"); |
170 | break; | 170 | break; |
171 | } | 171 | } |
172 | return 0; | 172 | lua_pushnumber(L, 1); |
173 | return 1; | ||
173 | } | 174 | } |
174 | 175 | ||
175 | /*=========================================================================*\ | 176 | /*=========================================================================*\ |