From 3f1712ed48f9deab4fab67d7d342dc20c2b5b0ce Mon Sep 17 00:00:00 2001 From: Diego Nehab Date: Mon, 19 Jan 2004 18:22:51 +0000 Subject: Added gethostname. Cleaned up TODO. Moved luasocket specific stuff from auxiliar.c to luasocket.c --- src/auxiliar.c | 19 +------------------ src/http.lua | 55 +++++++++++++++++++++++++++++++++++++----------------- src/luasocket.c | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++- src/luasocket.h | 2 +- src/tcp.c | 4 ++-- src/udp.c | 2 +- 6 files changed, 100 insertions(+), 40 deletions(-) (limited to 'src') 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 @@ \*-------------------------------------------------------------------------*/ void aux_open(lua_State *L) { - /* create namespace table */ - lua_pushstring(L, LUASOCKET_LIBNAME); - lua_newtable(L); -#ifdef LUASOCKET_DEBUG - lua_pushstring(L, "debug"); - lua_pushnumber(L, 1); - lua_rawset(L, -3); -#endif - /* make version string available so scripts */ - lua_pushstring(L, "version"); - lua_pushstring(L, LUASOCKET_VERSION); - lua_rawset(L, -3); - /* store namespace as global */ - lua_settable(L, LUA_GLOBALSINDEX); - /* make sure modules know what is our namespace */ - lua_pushstring(L, "LUASOCKET_LIBNAME"); - lua_pushstring(L, LUASOCKET_LIBNAME); - lua_settable(L, LUA_GLOBALSINDEX); + ; } /*-------------------------------------------------------------------------*\ 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 -- default port for document retrieval PORT = 80 -- user agent field sent in request -USERAGENT = "LuaSocket 2.0" +USERAGENT = socket.version -- block size used in transfers BLOCKSIZE = 8192 @@ -429,8 +429,7 @@ local function authorize(reqt, parsed, respt) body_cb = reqt.body_cb, headers = reqt.headers, timeout = reqt.timeout, - proxyhost = reqt.proxyhost, - proxyport = reqt.proxyport + proxy = reqt.proxy, } return request_cb(autht, respt) end @@ -471,8 +470,7 @@ local function redirect(reqt, respt) body_cb = reqt.body_cb, headers = reqt.headers, timeout = reqt.timeout, - proxyhost = reqt.proxyhost, - proxyport = reqt.proxyport + proxy = reqt.proxy } respt = request_cb(redirt, respt) -- we pass the location header as a clue we tried to redirect @@ -491,7 +489,7 @@ end ----------------------------------------------------------------------------- local function request_uri(reqt, parsed) local url - if not reqt.proxyhost and not reqt.proxyport then + if not reqt.proxy then url = { path = parsed.path, params = parsed.params, @@ -522,6 +520,39 @@ local function build_request(data) return reqt end +----------------------------------------------------------------------------- +-- Connects to a server, be it a proxy or not +-- Input +-- reqt: the request table +-- parsed: the parsed request url +-- Returns +-- sock: connection socket, or nil in case of error +-- err: error message +----------------------------------------------------------------------------- +local function try_connect(reqt, parsed) + reqt.proxy = reqt.proxy or PROXY + local host, port + if reqt.proxy then + local pproxy = socket.url.parse(reqt.proxy) + if not pproxy.port or not pproxy.host then + return nil, "invalid proxy" + end + host, port = pproxy.host, pproxy.port + else + host, port = parsed.host, parsed.port + end + local sock, ret, err + sock, err = socket.tcp() + if not sock then return nil, err end + sock:settimeout(reqt.timeout or TIMEOUT) + ret, err = sock:connect(host, port) + if not ret then + sock:close() + return nil, err + end + return sock +end + ----------------------------------------------------------------------------- -- Sends a HTTP request and retrieves the server reply using callbacks to -- send the request body and receive the response body @@ -562,18 +593,8 @@ function request_cb(reqt, respt) -- fill default headers reqt.headers = fill_headers(reqt.headers, parsed) -- try to connect to server - sock, respt.error = socket.tcp() + sock, respt.error = try_connect(reqt, parsed) if not sock then return respt end - -- set connection timeout so that we do not hang forever - sock:settimeout(reqt.timeout or TIMEOUT) - ret, respt.error = sock:connect( - reqt.proxyhost or PROXYHOST or parsed.host, - reqt.proxyport or PROXYPORT or parsed.port - ) - if not ret then - sock:close() - return respt - end -- send request message respt.error = send_request(sock, reqt.method, 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 @@ #include "mime.h" /*=========================================================================*\ -* Exported functions +* Declarations \*=========================================================================*/ +static int global_gethostname(lua_State *L); +static int base_open(lua_State *L); + +/* functions in library namespace */ +static luaL_reg func[] = { + {"gethostname", global_gethostname}, + {NULL, NULL} +}; + +/*-------------------------------------------------------------------------*\ +* Setup basic stuff. +\*-------------------------------------------------------------------------*/ +static int base_open(lua_State *L) +{ + /* create namespace table */ + lua_pushstring(L, LUASOCKET_LIBNAME); + lua_newtable(L); +#ifdef LUASOCKET_DEBUG + lua_pushstring(L, "debug"); + lua_pushnumber(L, 1); + lua_rawset(L, -3); +#endif + /* make version string available so scripts */ + lua_pushstring(L, "version"); + lua_pushstring(L, LUASOCKET_VERSION); + lua_rawset(L, -3); + /* store namespace as global */ + lua_settable(L, LUA_GLOBALSINDEX); + /* make sure modules know what is our namespace */ + lua_pushstring(L, "LUASOCKET_LIBNAME"); + lua_pushstring(L, LUASOCKET_LIBNAME); + lua_settable(L, LUA_GLOBALSINDEX); + /* define library functions */ + luaL_openlib(L, LUASOCKET_LIBNAME, func, 0); + lua_pop(L, 1); + return 0; +} + +/*-------------------------------------------------------------------------*\ +* Gets the host name +\*-------------------------------------------------------------------------*/ +static int global_gethostname(lua_State *L) +{ + char name[257]; + name[256] = '\0'; + if (gethostname(name, 256) < 0) { + lua_pushnil(L); + lua_pushstring(L, "gethostname failed"); + return 2; + } else { + lua_pushstring(L, name); + return 1; + } +} + /*-------------------------------------------------------------------------*\ * Initializes all library modules. \*-------------------------------------------------------------------------*/ @@ -45,6 +100,7 @@ LUASOCKET_API int luaopen_socket(lua_State *L) { if (!sock_open()) return 0; /* initialize all modules */ + base_open(L); aux_open(L); tm_open(L); 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 @@ /*-------------------------------------------------------------------------*\ * Current luasocket version \*-------------------------------------------------------------------------*/ -#define LUASOCKET_VERSION "LuaSocket 2.0 (alpha)" +#define LUASOCKET_VERSION "LuaSocket 2.0 (beta)" /*-------------------------------------------------------------------------*\ * Library's namespace diff --git a/src/tcp.c b/src/tcp.c index 21aa7f5..5505848 100644 --- a/src/tcp.c +++ b/src/tcp.c @@ -153,7 +153,7 @@ static int opt_keepalive(lua_State *L) return opt_boolean(L, SOL_SOCKET, SO_KEEPALIVE); } -int opt_linger(lua_State *L) +static int opt_linger(lua_State *L) { p_tcp tcp = (p_tcp) aux_checkclass(L, "tcp{client}", 1); struct linger li; @@ -334,7 +334,7 @@ static int meth_settimeout(lua_State *L) /*-------------------------------------------------------------------------*\ * Creates a master tcp object \*-------------------------------------------------------------------------*/ -int global_create(lua_State *L) +static int global_create(lua_State *L) { t_sock sock; const char *err = inet_trycreate(&sock, SOCK_STREAM); diff --git a/src/udp.c b/src/udp.c index c730206..c5e14d1 100644 --- a/src/udp.c +++ b/src/udp.c @@ -417,7 +417,7 @@ static int meth_setsockname(lua_State *L) /*-------------------------------------------------------------------------*\ * Creates a master udp object \*-------------------------------------------------------------------------*/ -int global_create(lua_State *L) +static int global_create(lua_State *L) { t_sock sock; const char *err = inet_trycreate(&sock, SOCK_DGRAM); -- cgit v1.2.3-55-g6feb