diff options
author | Diego Nehab <diego@tecgraf.puc-rio.br> | 2001-09-12 18:16:09 +0000 |
---|---|---|
committer | Diego Nehab <diego@tecgraf.puc-rio.br> | 2001-09-12 18:16:09 +0000 |
commit | 504ecdc0aaeb89dd16cfc8f058de3b03d869270e (patch) | |
tree | dfe7e7fc81c11f10f2a4f270ca137103c3424b3a /src | |
parent | cb61077f7a8c71536c3ad331f041bc4ab97c719c (diff) | |
download | luasocket-504ecdc0aaeb89dd16cfc8f058de3b03d869270e.tar.gz luasocket-504ecdc0aaeb89dd16cfc8f058de3b03d869270e.tar.bz2 luasocket-504ecdc0aaeb89dd16cfc8f058de3b03d869270e.zip |
Simple HTTP functions can deal with table arguments also.
Diffstat (limited to 'src')
-rw-r--r-- | src/http.lua | 101 |
1 files changed, 71 insertions, 30 deletions
diff --git a/src/http.lua b/src/http.lua index 1e4c2cd..7eaaf9e 100644 --- a/src/http.lua +++ b/src/http.lua | |||
@@ -1,6 +1,6 @@ | |||
1 | ----------------------------------------------------------------------------- | 1 | ----------------------------------------------------------------------------- |
2 | -- HTTP/1.1 client support for the Lua language. | 2 | -- HTTP/1.1 client support for the Lua language. |
3 | -- LuaSocket 1.4a toolkit. | 3 | -- LuaSocket 1.4 toolkit. |
4 | -- Author: Diego Nehab | 4 | -- Author: Diego Nehab |
5 | -- Date: 26/12/2000 | 5 | -- Date: 26/12/2000 |
6 | -- Conforming to: RFC 2616, LTN7 | 6 | -- Conforming to: RFC 2616, LTN7 |
@@ -18,23 +18,23 @@ Public.TIMEOUT = 60 | |||
18 | -- default port for document retrieval | 18 | -- default port for document retrieval |
19 | Public.PORT = 80 | 19 | Public.PORT = 80 |
20 | -- user agent field sent in request | 20 | -- user agent field sent in request |
21 | Public.USERAGENT = "LuaSocket 1.4a" | 21 | Public.USERAGENT = "LuaSocket 1.4" |
22 | -- block size used in transfers | 22 | -- block size used in transfers |
23 | Public.BLOCKSIZE = 8192 | 23 | Public.BLOCKSIZE = 8192 |
24 | 24 | ||
25 | ----------------------------------------------------------------------------- | 25 | ----------------------------------------------------------------------------- |
26 | -- Required libraries | 26 | -- Required libraries |
27 | ----------------------------------------------------------------------------- | 27 | ----------------------------------------------------------------------------- |
28 | dofile "buffer.lua" | 28 | dofile "concat.lua" |
29 | dofile "url.lua" | 29 | dofile "url.lua" |
30 | dofile "code.lua" | 30 | dofile "code.lua" |
31 | 31 | ||
32 | ----------------------------------------------------------------------------- | 32 | ----------------------------------------------------------------------------- |
33 | -- Tries to get a pattern from the server and closes socket on error | 33 | -- Tries to get a pattern from the server and closes socket on error |
34 | -- sock: socket connected to the server | 34 | -- sock: socket connected to the server |
35 | -- ...: patterns to receive | 35 | -- ...: pattern to receive |
36 | -- Returns | 36 | -- Returns |
37 | -- ...: received patterns | 37 | -- ...: received pattern |
38 | -- err: error message if any | 38 | -- err: error message if any |
39 | ----------------------------------------------------------------------------- | 39 | ----------------------------------------------------------------------------- |
40 | function Private.try_receive(...) | 40 | function Private.try_receive(...) |
@@ -313,6 +313,26 @@ function Private.send_indirect(data, send_cb, chunk, size) | |||
313 | end | 313 | end |
314 | 314 | ||
315 | ----------------------------------------------------------------------------- | 315 | ----------------------------------------------------------------------------- |
316 | -- Sends mime headers | ||
317 | -- Input | ||
318 | -- sock: server socket | ||
319 | -- headers: table with mime headers to be sent | ||
320 | -- Returns | ||
321 | -- err: error message if any | ||
322 | ----------------------------------------------------------------------------- | ||
323 | function Private.send_headers(sock, headers) | ||
324 | local err | ||
325 | headers = headers or {} | ||
326 | -- send request headers | ||
327 | for i, v in headers do | ||
328 | err = %Private.try_send(sock, i .. ": " .. v .. "\r\n") | ||
329 | if err then return err end | ||
330 | end | ||
331 | -- mark end of request headers | ||
332 | return %Private.try_send(sock, "\r\n") | ||
333 | end | ||
334 | |||
335 | ----------------------------------------------------------------------------- | ||
316 | -- Sends a HTTP request message through socket | 336 | -- Sends a HTTP request message through socket |
317 | -- Input | 337 | -- Input |
318 | -- sock: socket connected to the server | 338 | -- sock: socket connected to the server |
@@ -340,12 +360,7 @@ function Private.send_request(sock, method, uri, headers, body_cb) | |||
340 | end | 360 | end |
341 | end | 361 | end |
342 | -- send request headers | 362 | -- send request headers |
343 | for i, v in headers do | 363 | err = %Private.send_headers(sock, headers) |
344 | err = %Private.try_send(sock, i .. ": " .. v .. "\r\n") | ||
345 | if err then return err end | ||
346 | end | ||
347 | -- mark end of request headers | ||
348 | err = %Private.try_send(sock, "\r\n") | ||
349 | if err then return err end | 364 | if err then return err end |
350 | -- send request message body, if any | 365 | -- send request message body, if any |
351 | if body_cb then | 366 | if body_cb then |
@@ -427,7 +442,7 @@ function Private.authorize(request, parsed, response) | |||
427 | body_cb = request.body_cb, | 442 | body_cb = request.body_cb, |
428 | headers = request.headers | 443 | headers = request.headers |
429 | } | 444 | } |
430 | return %Public.request_indirect(authorize, response) | 445 | return %Public.request_cb(authorize, response) |
431 | end | 446 | end |
432 | 447 | ||
433 | ----------------------------------------------------------------------------- | 448 | ----------------------------------------------------------------------------- |
@@ -467,7 +482,7 @@ function Private.redirect(request, response) | |||
467 | body_cb = request.body_cb, | 482 | body_cb = request.body_cb, |
468 | headers = request.headers | 483 | headers = request.headers |
469 | } | 484 | } |
470 | return %Public.request_indirect(redirect, response) | 485 | return %Public.request_cb(redirect, response) |
471 | end | 486 | end |
472 | 487 | ||
473 | ----------------------------------------------------------------------------- | 488 | ----------------------------------------------------------------------------- |
@@ -487,6 +502,23 @@ function Private.request_uri(parsed) | |||
487 | end | 502 | end |
488 | 503 | ||
489 | ----------------------------------------------------------------------------- | 504 | ----------------------------------------------------------------------------- |
505 | -- Builds a request table from a URL or request table | ||
506 | -- Input | ||
507 | -- url_or_request: target url or request table (a table with the fields: | ||
508 | -- url: the target URL | ||
509 | -- user: account user name | ||
510 | -- password: account password) | ||
511 | -- Returns | ||
512 | -- request: request table | ||
513 | ----------------------------------------------------------------------------- | ||
514 | function Private.build_request(data) | ||
515 | local request = {} | ||
516 | if type(data) == "table" then for i, v in data do request[i] = v end | ||
517 | else request.url = data end | ||
518 | return request | ||
519 | end | ||
520 | |||
521 | ----------------------------------------------------------------------------- | ||
490 | -- Sends a HTTP request and retrieves the server reply using callbacks to | 522 | -- Sends a HTTP request and retrieves the server reply using callbacks to |
491 | -- send the request body and receive the response body | 523 | -- send the request body and receive the response body |
492 | -- Input | 524 | -- Input |
@@ -506,8 +538,12 @@ end | |||
506 | -- code: server status code, or nil if failed | 538 | -- code: server status code, or nil if failed |
507 | -- error: error message, or nil if successfull | 539 | -- error: error message, or nil if successfull |
508 | ----------------------------------------------------------------------------- | 540 | ----------------------------------------------------------------------------- |
509 | function Public.request_indirect(request, response) | 541 | function Public.request_cb(request, response) |
510 | local parsed = URL.parse_url(request.url, {port = %Public.PORT, path ="/"}) | 542 | local parsed = URL.parse_url(request.url, { |
543 | host = "", | ||
544 | port = %Public.PORT, | ||
545 | path ="/" | ||
546 | }) | ||
511 | -- explicit authentication info overrides that given by the URL | 547 | -- explicit authentication info overrides that given by the URL |
512 | parsed.user = request.user or parsed.user | 548 | parsed.user = request.user or parsed.user |
513 | parsed.password = request.password or parsed.password | 549 | parsed.password = request.password or parsed.password |
@@ -578,10 +614,10 @@ function Public.request(request) | |||
578 | end | 614 | end |
579 | local cat = Concat.create() | 615 | local cat = Concat.create() |
580 | response.body_cb = function(chunk, err) | 616 | response.body_cb = function(chunk, err) |
581 | %cat:addstring(chunk) | 617 | if chunk then %cat:addstring(chunk) end |
582 | return 1 | 618 | return 1 |
583 | end | 619 | end |
584 | %Public.request_indirect(request, response) | 620 | response = %Public.request_cb(request, response) |
585 | response.body = cat:getresult() | 621 | response.body = cat:getresult() |
586 | response.body_cb = nil | 622 | response.body_cb = nil |
587 | return response | 623 | return response |
@@ -590,18 +626,20 @@ end | |||
590 | ----------------------------------------------------------------------------- | 626 | ----------------------------------------------------------------------------- |
591 | -- Retrieves a URL by the method "GET" | 627 | -- Retrieves a URL by the method "GET" |
592 | -- Input | 628 | -- Input |
593 | -- url: request URL, i.e. the document to be retrieved | 629 | -- url_or_request: target url or request table (a table with the fields: |
630 | -- url: the target URL | ||
631 | -- user: account user name | ||
632 | -- password: account password) | ||
594 | -- Returns | 633 | -- Returns |
595 | -- body: response message body, or nil if failed | 634 | -- body: response message body, or nil if failed |
596 | -- headers: response header fields received, or nil if failed | 635 | -- headers: response header fields received, or nil if failed |
597 | -- status: server response status line, or nil if failed | 636 | -- status: server response status line, or nil if failed |
598 | -- error: error message if any | 637 | -- error: error message if any |
599 | ----------------------------------------------------------------------------- | 638 | ----------------------------------------------------------------------------- |
600 | function Public.get(url) | 639 | function Public.get(url_or_request) |
601 | local response = %Public.request { | 640 | local request = %Private.build_request(url_or_request) |
602 | method = "GET", | 641 | request.method = "GET" |
603 | url = url | 642 | local response = %Public.request(request) |
604 | } | ||
605 | return response.body, response.headers, | 643 | return response.body, response.headers, |
606 | response.status, response.error | 644 | response.status, response.error |
607 | end | 645 | end |
@@ -609,7 +647,11 @@ end | |||
609 | ----------------------------------------------------------------------------- | 647 | ----------------------------------------------------------------------------- |
610 | -- Retrieves a URL by the method "POST" | 648 | -- Retrieves a URL by the method "POST" |
611 | -- Input | 649 | -- Input |
612 | -- url: request URL, i.e. the document to be retrieved | 650 | -- url_or_request: target url or request table (a table with the fields: |
651 | -- url: the target URL | ||
652 | -- body: request message body | ||
653 | -- user: account user name | ||
654 | -- password: account password) | ||
613 | -- body: request message body, or nil if none | 655 | -- body: request message body, or nil if none |
614 | -- Returns | 656 | -- Returns |
615 | -- body: response message body, or nil if failed | 657 | -- body: response message body, or nil if failed |
@@ -617,12 +659,11 @@ end | |||
617 | -- status: server response status line, or nil if failed | 659 | -- status: server response status line, or nil if failed |
618 | -- error: error message, or nil if successfull | 660 | -- error: error message, or nil if successfull |
619 | ----------------------------------------------------------------------------- | 661 | ----------------------------------------------------------------------------- |
620 | function Public.post(url, body) | 662 | function Public.post(url_or_request, body) |
621 | local response = %Public.request { | 663 | local request = %Private.build_request(url_or_request) |
622 | method = "POST", | 664 | request.method = "POST" |
623 | url = url, | 665 | request.body = request.body or body |
624 | body = body | 666 | local response = %Public.request(request) |
625 | } | ||
626 | return response.body, response.headers, | 667 | return response.body, response.headers, |
627 | response.status, response.error | 668 | response.status, response.error |
628 | end | 669 | end |