diff options
author | Diego Nehab <diego@tecgraf.puc-rio.br> | 2004-01-19 18:22:51 +0000 |
---|---|---|
committer | Diego Nehab <diego@tecgraf.puc-rio.br> | 2004-01-19 18:22:51 +0000 |
commit | 3f1712ed48f9deab4fab67d7d342dc20c2b5b0ce (patch) | |
tree | 4843296b98ab3524b9cbed1c6428b101e8107492 /src | |
parent | fbb42b80cb0d299f38e0a4df9b0fa01228b39225 (diff) | |
download | luasocket-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')
-rw-r--r-- | src/auxiliar.c | 19 | ||||
-rw-r--r-- | src/http.lua | 55 | ||||
-rw-r--r-- | src/luasocket.c | 58 | ||||
-rw-r--r-- | src/luasocket.h | 2 | ||||
-rw-r--r-- | src/tcp.c | 4 | ||||
-rw-r--r-- | src/udp.c | 2 |
6 files changed, 100 insertions, 40 deletions
diff --git a/src/auxiliar.c b/src/auxiliar.c index 65425c5..4b3a0f6 100644 --- a/src/auxiliar.c +++ b/src/auxiliar.c | |||
@@ -17,24 +17,7 @@ | |||
17 | \*-------------------------------------------------------------------------*/ | 17 | \*-------------------------------------------------------------------------*/ |
18 | void aux_open(lua_State *L) | 18 | void aux_open(lua_State *L) |
19 | { | 19 | { |
20 | /* create namespace table */ | 20 | ; |
21 | lua_pushstring(L, LUASOCKET_LIBNAME); | ||
22 | lua_newtable(L); | ||
23 | #ifdef LUASOCKET_DEBUG | ||
24 | lua_pushstring(L, "debug"); | ||
25 | lua_pushnumber(L, 1); | ||
26 | lua_rawset(L, -3); | ||
27 | #endif | ||
28 | /* make version string available so scripts */ | ||
29 | lua_pushstring(L, "version"); | ||
30 | lua_pushstring(L, LUASOCKET_VERSION); | ||
31 | lua_rawset(L, -3); | ||
32 | /* store namespace as global */ | ||
33 | lua_settable(L, LUA_GLOBALSINDEX); | ||
34 | /* make sure modules know what is our namespace */ | ||
35 | lua_pushstring(L, "LUASOCKET_LIBNAME"); | ||
36 | lua_pushstring(L, LUASOCKET_LIBNAME); | ||
37 | lua_settable(L, LUA_GLOBALSINDEX); | ||
38 | } | 21 | } |
39 | 22 | ||
40 | /*-------------------------------------------------------------------------*\ | 23 | /*-------------------------------------------------------------------------*\ |
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 |
26 | PORT = 80 | 26 | PORT = 80 |
27 | -- user agent field sent in request | 27 | -- user agent field sent in request |
28 | USERAGENT = "LuaSocket 2.0" | 28 | USERAGENT = socket.version |
29 | -- block size used in transfers | 29 | -- block size used in transfers |
30 | BLOCKSIZE = 8192 | 30 | BLOCKSIZE = 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) |
436 | end | 435 | end |
@@ -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 | ----------------------------------------------------------------------------- |
492 | local function request_uri(reqt, parsed) | 490 | local 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) | |||
523 | end | 521 | end |
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 | ----------------------------------------------------------------------------- | ||
532 | local 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 | ||
554 | end | ||
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) |
diff --git a/src/luasocket.c b/src/luasocket.c index 578d65c..73583a8 100644 --- a/src/luasocket.c +++ b/src/luasocket.c | |||
@@ -36,8 +36,63 @@ | |||
36 | #include "mime.h" | 36 | #include "mime.h" |
37 | 37 | ||
38 | /*=========================================================================*\ | 38 | /*=========================================================================*\ |
39 | * Exported functions | 39 | * Declarations |
40 | \*=========================================================================*/ | 40 | \*=========================================================================*/ |
41 | static int global_gethostname(lua_State *L); | ||
42 | static int base_open(lua_State *L); | ||
43 | |||
44 | /* functions in library namespace */ | ||
45 | static luaL_reg func[] = { | ||
46 | {"gethostname", global_gethostname}, | ||
47 | {NULL, NULL} | ||
48 | }; | ||
49 | |||
50 | /*-------------------------------------------------------------------------*\ | ||
51 | * Setup basic stuff. | ||
52 | \*-------------------------------------------------------------------------*/ | ||
53 | static int base_open(lua_State *L) | ||
54 | { | ||
55 | /* create namespace table */ | ||
56 | lua_pushstring(L, LUASOCKET_LIBNAME); | ||
57 | lua_newtable(L); | ||
58 | #ifdef LUASOCKET_DEBUG | ||
59 | lua_pushstring(L, "debug"); | ||
60 | lua_pushnumber(L, 1); | ||
61 | lua_rawset(L, -3); | ||
62 | #endif | ||
63 | /* make version string available so scripts */ | ||
64 | lua_pushstring(L, "version"); | ||
65 | lua_pushstring(L, LUASOCKET_VERSION); | ||
66 | lua_rawset(L, -3); | ||
67 | /* store namespace as global */ | ||
68 | lua_settable(L, LUA_GLOBALSINDEX); | ||
69 | /* make sure modules know what is our namespace */ | ||
70 | lua_pushstring(L, "LUASOCKET_LIBNAME"); | ||
71 | lua_pushstring(L, LUASOCKET_LIBNAME); | ||
72 | lua_settable(L, LUA_GLOBALSINDEX); | ||
73 | /* define library functions */ | ||
74 | luaL_openlib(L, LUASOCKET_LIBNAME, func, 0); | ||
75 | lua_pop(L, 1); | ||
76 | return 0; | ||
77 | } | ||
78 | |||
79 | /*-------------------------------------------------------------------------*\ | ||
80 | * Gets the host name | ||
81 | \*-------------------------------------------------------------------------*/ | ||
82 | static int global_gethostname(lua_State *L) | ||
83 | { | ||
84 | char name[257]; | ||
85 | name[256] = '\0'; | ||
86 | if (gethostname(name, 256) < 0) { | ||
87 | lua_pushnil(L); | ||
88 | lua_pushstring(L, "gethostname failed"); | ||
89 | return 2; | ||
90 | } else { | ||
91 | lua_pushstring(L, name); | ||
92 | return 1; | ||
93 | } | ||
94 | } | ||
95 | |||
41 | /*-------------------------------------------------------------------------*\ | 96 | /*-------------------------------------------------------------------------*\ |
42 | * Initializes all library modules. | 97 | * Initializes all library modules. |
43 | \*-------------------------------------------------------------------------*/ | 98 | \*-------------------------------------------------------------------------*/ |
@@ -45,6 +100,7 @@ LUASOCKET_API int luaopen_socket(lua_State *L) | |||
45 | { | 100 | { |
46 | if (!sock_open()) return 0; | 101 | if (!sock_open()) return 0; |
47 | /* initialize all modules */ | 102 | /* initialize all modules */ |
103 | base_open(L); | ||
48 | aux_open(L); | 104 | aux_open(L); |
49 | tm_open(L); | 105 | tm_open(L); |
50 | buf_open(L); | 106 | buf_open(L); |
diff --git a/src/luasocket.h b/src/luasocket.h index ac26824..81d7b3f 100644 --- a/src/luasocket.h +++ b/src/luasocket.h | |||
@@ -13,7 +13,7 @@ | |||
13 | /*-------------------------------------------------------------------------*\ | 13 | /*-------------------------------------------------------------------------*\ |
14 | * Current luasocket version | 14 | * Current luasocket version |
15 | \*-------------------------------------------------------------------------*/ | 15 | \*-------------------------------------------------------------------------*/ |
16 | #define LUASOCKET_VERSION "LuaSocket 2.0 (alpha)" | 16 | #define LUASOCKET_VERSION "LuaSocket 2.0 (beta)" |
17 | 17 | ||
18 | /*-------------------------------------------------------------------------*\ | 18 | /*-------------------------------------------------------------------------*\ |
19 | * Library's namespace | 19 | * Library's namespace |
@@ -153,7 +153,7 @@ static int opt_keepalive(lua_State *L) | |||
153 | return opt_boolean(L, SOL_SOCKET, SO_KEEPALIVE); | 153 | return opt_boolean(L, SOL_SOCKET, SO_KEEPALIVE); |
154 | } | 154 | } |
155 | 155 | ||
156 | int opt_linger(lua_State *L) | 156 | static int opt_linger(lua_State *L) |
157 | { | 157 | { |
158 | p_tcp tcp = (p_tcp) aux_checkclass(L, "tcp{client}", 1); | 158 | p_tcp tcp = (p_tcp) aux_checkclass(L, "tcp{client}", 1); |
159 | struct linger li; | 159 | struct linger li; |
@@ -334,7 +334,7 @@ static int meth_settimeout(lua_State *L) | |||
334 | /*-------------------------------------------------------------------------*\ | 334 | /*-------------------------------------------------------------------------*\ |
335 | * Creates a master tcp object | 335 | * Creates a master tcp object |
336 | \*-------------------------------------------------------------------------*/ | 336 | \*-------------------------------------------------------------------------*/ |
337 | int global_create(lua_State *L) | 337 | static int global_create(lua_State *L) |
338 | { | 338 | { |
339 | t_sock sock; | 339 | t_sock sock; |
340 | const char *err = inet_trycreate(&sock, SOCK_STREAM); | 340 | const char *err = inet_trycreate(&sock, SOCK_STREAM); |
@@ -417,7 +417,7 @@ static int meth_setsockname(lua_State *L) | |||
417 | /*-------------------------------------------------------------------------*\ | 417 | /*-------------------------------------------------------------------------*\ |
418 | * Creates a master udp object | 418 | * Creates a master udp object |
419 | \*-------------------------------------------------------------------------*/ | 419 | \*-------------------------------------------------------------------------*/ |
420 | int global_create(lua_State *L) | 420 | static int global_create(lua_State *L) |
421 | { | 421 | { |
422 | t_sock sock; | 422 | t_sock sock; |
423 | const char *err = inet_trycreate(&sock, SOCK_DGRAM); | 423 | const char *err = inet_trycreate(&sock, SOCK_DGRAM); |