aboutsummaryrefslogtreecommitdiff
path: root/src/http.lua
diff options
context:
space:
mode:
authorDiego Nehab <diego@tecgraf.puc-rio.br>2004-01-19 18:22:51 +0000
committerDiego Nehab <diego@tecgraf.puc-rio.br>2004-01-19 18:22:51 +0000
commit3f1712ed48f9deab4fab67d7d342dc20c2b5b0ce (patch)
tree4843296b98ab3524b9cbed1c6428b101e8107492 /src/http.lua
parentfbb42b80cb0d299f38e0a4df9b0fa01228b39225 (diff)
downloadluasocket-3f1712ed48f9deab4fab67d7d342dc20c2b5b0ce.tar.gz
luasocket-3f1712ed48f9deab4fab67d7d342dc20c2b5b0ce.tar.bz2
luasocket-3f1712ed48f9deab4fab67d7d342dc20c2b5b0ce.zip
Added gethostname.
Cleaned up TODO. Moved luasocket specific stuff from auxiliar.c to luasocket.c
Diffstat (limited to 'src/http.lua')
-rw-r--r--src/http.lua55
1 files changed, 38 insertions, 17 deletions
diff --git a/src/http.lua b/src/http.lua
index 72bde0a..4d6e426 100644
--- a/src/http.lua
+++ b/src/http.lua
@@ -25,7 +25,7 @@ TIMEOUT = 60
25-- default port for document retrieval 25-- default port for document retrieval
26PORT = 80 26PORT = 80
27-- user agent field sent in request 27-- user agent field sent in request
28USERAGENT = "LuaSocket 2.0" 28USERAGENT = socket.version
29-- block size used in transfers 29-- block size used in transfers
30BLOCKSIZE = 8192 30BLOCKSIZE = 8192
31 31
@@ -429,8 +429,7 @@ local function authorize(reqt, parsed, respt)
429 body_cb = reqt.body_cb, 429 body_cb = reqt.body_cb,
430 headers = reqt.headers, 430 headers = reqt.headers,
431 timeout = reqt.timeout, 431 timeout = reqt.timeout,
432 proxyhost = reqt.proxyhost, 432 proxy = reqt.proxy,
433 proxyport = reqt.proxyport
434 } 433 }
435 return request_cb(autht, respt) 434 return request_cb(autht, respt)
436end 435end
@@ -471,8 +470,7 @@ local function redirect(reqt, respt)
471 body_cb = reqt.body_cb, 470 body_cb = reqt.body_cb,
472 headers = reqt.headers, 471 headers = reqt.headers,
473 timeout = reqt.timeout, 472 timeout = reqt.timeout,
474 proxyhost = reqt.proxyhost, 473 proxy = reqt.proxy
475 proxyport = reqt.proxyport
476 } 474 }
477 respt = request_cb(redirt, respt) 475 respt = request_cb(redirt, respt)
478 -- we pass the location header as a clue we tried to redirect 476 -- we pass the location header as a clue we tried to redirect
@@ -491,7 +489,7 @@ end
491----------------------------------------------------------------------------- 489-----------------------------------------------------------------------------
492local function request_uri(reqt, parsed) 490local function request_uri(reqt, parsed)
493 local url 491 local url
494 if not reqt.proxyhost and not reqt.proxyport then 492 if not reqt.proxy then
495 url = { 493 url = {
496 path = parsed.path, 494 path = parsed.path,
497 params = parsed.params, 495 params = parsed.params,
@@ -523,6 +521,39 @@ local function build_request(data)
523end 521end
524 522
525----------------------------------------------------------------------------- 523-----------------------------------------------------------------------------
524-- Connects to a server, be it a proxy or not
525-- Input
526-- reqt: the request table
527-- parsed: the parsed request url
528-- Returns
529-- sock: connection socket, or nil in case of error
530-- err: error message
531-----------------------------------------------------------------------------
532local function try_connect(reqt, parsed)
533 reqt.proxy = reqt.proxy or PROXY
534 local host, port
535 if reqt.proxy then
536 local pproxy = socket.url.parse(reqt.proxy)
537 if not pproxy.port or not pproxy.host then
538 return nil, "invalid proxy"
539 end
540 host, port = pproxy.host, pproxy.port
541 else
542 host, port = parsed.host, parsed.port
543 end
544 local sock, ret, err
545 sock, err = socket.tcp()
546 if not sock then return nil, err end
547 sock:settimeout(reqt.timeout or TIMEOUT)
548 ret, err = sock:connect(host, port)
549 if not ret then
550 sock:close()
551 return nil, err
552 end
553 return sock
554end
555
556-----------------------------------------------------------------------------
526-- Sends a HTTP request and retrieves the server reply using callbacks to 557-- Sends a HTTP request and retrieves the server reply using callbacks to
527-- send the request body and receive the response body 558-- send the request body and receive the response body
528-- Input 559-- Input
@@ -562,18 +593,8 @@ function request_cb(reqt, respt)
562 -- fill default headers 593 -- fill default headers
563 reqt.headers = fill_headers(reqt.headers, parsed) 594 reqt.headers = fill_headers(reqt.headers, parsed)
564 -- try to connect to server 595 -- try to connect to server
565 sock, respt.error = socket.tcp() 596 sock, respt.error = try_connect(reqt, parsed)
566 if not sock then return respt end 597 if not sock then return respt end
567 -- set connection timeout so that we do not hang forever
568 sock:settimeout(reqt.timeout or TIMEOUT)
569 ret, respt.error = sock:connect(
570 reqt.proxyhost or PROXYHOST or parsed.host,
571 reqt.proxyport or PROXYPORT or parsed.port
572 )
573 if not ret then
574 sock:close()
575 return respt
576 end
577 -- send request message 598 -- send request message
578 respt.error = send_request(sock, reqt.method, 599 respt.error = send_request(sock, reqt.method,
579 request_uri(reqt, parsed), reqt.headers, reqt.body_cb) 600 request_uri(reqt, parsed), reqt.headers, reqt.body_cb)