From f20f4889bfe5a02cd9b77868b90cc8042352176a Mon Sep 17 00:00:00 2001 From: Diego Nehab Date: Sun, 20 Nov 2005 07:20:26 +0000 Subject: [PATCH] Changed prefix of function names to match module names. Removed some warnings and useless code. --- config | 20 ++++++++--------- mime.vcproj | 2 +- socket.vcproj | 6 +++++ src/mime.c | 57 +++++++++++++++++++++-------------------------- src/options.c | 4 ++-- src/select.c | 37 ++++++++++++++++-------------- src/socket.h | 3 ++- src/url.lua | 29 ++++++++++++++++-------- src/usocket.c | 3 ++- src/wsocket.c | 34 ++++++++++++++++------------ test/httptest.lua | 4 ++-- 11 files changed, 110 insertions(+), 89 deletions(-) diff --git a/config b/config index 255477b..175a885 100644 --- a/config +++ b/config @@ -37,20 +37,20 @@ INSTALL_EXEC=cp # Compiler and linker settings # for Mac OS X # -CC=gcc -DEF=-DLUASOCKET_DEBUG -DUNIX_HAS_SUN_LEN -CFLAGS= $(LUAINC) -I$(COMPAT) $(DEF) -pedantic -Wall -O2 -fno-common -LDFLAGS=-bundle -undefined dynamic_lookup -LD=export MACOSX_DEPLOYMENT_TARGET="10.3"; gcc +#CC=gcc +#DEF=-DLUASOCKET_DEBUG -DUNIX_HAS_SUN_LEN +#CFLAGS= $(LUAINC) -I$(COMPAT) $(DEF) -pedantic -Wall -O2 -fno-common +#LDFLAGS=-bundle -undefined dynamic_lookup +#LD=export MACOSX_DEPLOYMENT_TARGET="10.3"; gcc #------ # Compiler and linker settings # for Linux -#CC=gcc -#DEF=-DLUASOCKET_DEBUG -#CFLAGS= $(LUAINC) -I$(COMPAT) $(DEF) -pedantic -Wall -O2 -fpic -#LDFLAGS=-O -shared -#LD=gcc +CC=gcc +DEF=-DLUASOCKET_DEBUG +CFLAGS= $(LUAINC) -I$(COMPAT) $(DEF) -pedantic -Wall -O2 -fpic +LDFLAGS=-O -shared +LD=gcc #------ # End of makefile configuration diff --git a/mime.vcproj b/mime.vcproj index f5c57c3..e5c4393 100644 --- a/mime.vcproj +++ b/mime.vcproj @@ -72,7 +72,7 @@ PreprocessorDefinitions="WIN32;NDEBUG;_WINDOWS;_USRDLL;MIME_EXPORTS; MIME_API=__declspec(dllexport)" RuntimeLibrary="2" UsePrecompiledHeader="0" - WarningLevel="3" + WarningLevel="4" Detect64BitPortabilityProblems="TRUE" DebugInformationFormat="0"/> + + + 15 || d > 15) luaL_addlstring(buffer, (char *)input, 3); - else luaL_putchar(buffer, (c << 4) + d); - return 0; - case '\r': - if (size < 2) return size; - if (input[1] == '\n') luaL_addlstring(buffer, (char *)input, 2); - return 0; - default: - if (input[0] == '\t' || (input[0] > 31 && input[0] < 127)) - luaL_putchar(buffer, input[0]); - return 0; - } - input[0] = input[1]; input[1] = input[2]; - size--; + switch (input[0]) { + /* if we have an escape character */ + case '=': + if (size < 3) return size; + /* eliminate soft line break */ + if (input[1] == '\r' && input[2] == '\n') return 0; + /* decode quoted representation */ + c = qpunbase[input[1]]; d = qpunbase[input[2]]; + /* if it is an invalid, do not decode */ + if (c > 15 || d > 15) luaL_addlstring(buffer, (char *)input, 3); + else luaL_putchar(buffer, (c << 4) + d); + return 0; + case '\r': + if (size < 2) return size; + if (input[1] == '\n') luaL_addlstring(buffer, (char *)input, 2); + return 0; + default: + if (input[0] == '\t' || (input[0] > 31 && input[0] < 127)) + luaL_putchar(buffer, input[0]); + return 0; } - return 0; } /*-------------------------------------------------------------------------*\ diff --git a/src/options.c b/src/options.c index 0f1ca2c..a464a4b 100644 --- a/src/options.c +++ b/src/options.c @@ -80,12 +80,12 @@ int opt_linger(lua_State *L, p_socket ps) lua_gettable(L, 3); if (!lua_isboolean(L, -1)) luaL_argerror(L, 3, "boolean 'on' field expected"); - li.l_onoff = lua_toboolean(L, -1); + li.l_onoff = (u_short) lua_toboolean(L, -1); lua_pushstring(L, "timeout"); lua_gettable(L, 3); if (!lua_isnumber(L, -1)) luaL_argerror(L, 3, "number 'timeout' field expected"); - li.l_linger = (int) lua_tonumber(L, -1); + li.l_linger = (u_short) lua_tonumber(L, -1); return opt_set(L, ps, SOL_SOCKET, SO_LINGER, (char *) &li, sizeof(li)); } diff --git a/src/select.c b/src/select.c index 5a3d502..99b59f5 100644 --- a/src/select.c +++ b/src/select.c @@ -16,11 +16,12 @@ /*=========================================================================*\ * Internal function prototypes. \*=========================================================================*/ -static int getfd(lua_State *L); +static t_socket getfd(lua_State *L); static int dirty(lua_State *L); -static int collect_fd(lua_State *L, int tab, int max_fd, int itab, fd_set *set); +static t_socket collect_fd(lua_State *L, int tab, t_socket max_fd, + int itab, fd_set *set); static int check_dirty(lua_State *L, int tab, int dtab, fd_set *set); -static void return_fd(lua_State *L, fd_set *set, int max_fd, +static void return_fd(lua_State *L, fd_set *set, t_socket max_fd, int itab, int tab, int start); static void make_assoc(lua_State *L, int tab); static int global_select(lua_State *L); @@ -49,7 +50,8 @@ int select_open(lua_State *L) { * Waits for a set of sockets until a condition is met or timeout. \*-------------------------------------------------------------------------*/ static int global_select(lua_State *L) { - int rtab, wtab, itab, max_fd, ret, ndirty; + int rtab, wtab, itab, ret, ndirty; + t_socket max_fd; fd_set rset, wset; t_timeout tm; double t = luaL_optnumber(L, 3, -1); @@ -58,7 +60,7 @@ static int global_select(lua_State *L) { lua_newtable(L); itab = lua_gettop(L); lua_newtable(L); rtab = lua_gettop(L); lua_newtable(L); wtab = lua_gettop(L); - max_fd = collect_fd(L, 1, -1, itab, &rset); + max_fd = collect_fd(L, 1, SOCKET_INVALID, itab, &rset); ndirty = check_dirty(L, 1, rtab, &rset); t = ndirty > 0? 0.0: t; timeout_init(&tm, t, -1); @@ -83,15 +85,15 @@ static int global_select(lua_State *L) { /*=========================================================================*\ * Internal functions \*=========================================================================*/ -static int getfd(lua_State *L) { - int fd = -1; +static t_socket getfd(lua_State *L) { + t_socket fd = SOCKET_INVALID; lua_pushstring(L, "getfd"); lua_gettable(L, -2); if (!lua_isnil(L, -1)) { lua_pushvalue(L, -2); lua_call(L, 1, 1); if (lua_isnumber(L, -1)) - fd = (int) lua_tonumber(L, -1); + fd = (t_socket) lua_tonumber(L, -1); } lua_pop(L, 1); return fd; @@ -110,13 +112,13 @@ static int dirty(lua_State *L) { return is; } -static int collect_fd(lua_State *L, int tab, int max_fd, +static t_socket collect_fd(lua_State *L, int tab, t_socket max_fd, int itab, fd_set *set) { int i = 1; if (lua_isnil(L, tab)) return max_fd; while (1) { - int fd; + t_socket fd; lua_pushnumber(L, i); lua_gettable(L, tab); if (lua_isnil(L, -1)) { @@ -124,9 +126,10 @@ static int collect_fd(lua_State *L, int tab, int max_fd, break; } fd = getfd(L); - if (fd >= 0) { + if (fd != SOCKET_INVALID) { FD_SET(fd, set); - if (max_fd < fd) max_fd = fd; + if (max_fd == SOCKET_INVALID || max_fd < fd) + max_fd = fd; lua_pushnumber(L, fd); lua_pushvalue(L, -2); lua_settable(L, itab); @@ -141,8 +144,8 @@ static int check_dirty(lua_State *L, int tab, int dtab, fd_set *set) { int ndirty = 0, i = 1; if (lua_isnil(L, tab)) return 0; - while (1) { - int fd; + while (1) { + t_socket fd; lua_pushnumber(L, i); lua_gettable(L, tab); if (lua_isnil(L, -1)) { @@ -150,7 +153,7 @@ static int check_dirty(lua_State *L, int tab, int dtab, fd_set *set) { break; } fd = getfd(L); - if (fd >= 0 && dirty(L)) { + if (fd != SOCKET_INVALID && dirty(L)) { lua_pushnumber(L, ++ndirty); lua_pushvalue(L, -2); lua_settable(L, dtab); @@ -162,9 +165,9 @@ static int check_dirty(lua_State *L, int tab, int dtab, fd_set *set) { return ndirty; } -static void return_fd(lua_State *L, fd_set *set, int max_fd, +static void return_fd(lua_State *L, fd_set *set, t_socket max_fd, int itab, int tab, int start) { - int fd; + t_socket fd; for (fd = 0; fd < max_fd; fd++) { if (FD_ISSET(fd, set)) { lua_pushnumber(L, ++start); diff --git a/src/socket.h b/src/socket.h index a599d8a..de5d79f 100644 --- a/src/socket.h +++ b/src/socket.h @@ -50,7 +50,8 @@ void socket_setnonblocking(p_socket ps); void socket_setblocking(p_socket ps); int socket_waitfd(p_socket ps, int sw, p_timeout tm); -int socket_select(int n, fd_set *rfds, fd_set *wfds, fd_set *efds, p_timeout tm); +int socket_select(t_socket n, fd_set *rfds, fd_set *wfds, fd_set *efds, + p_timeout tm); int socket_connect(p_socket ps, SA *addr, socklen_t addr_len, p_timeout tm); int socket_create(p_socket ps, int domain, int type, int protocol); diff --git a/src/url.lua b/src/url.lua index ec62ad6..52e88da 100644 --- a/src/url.lua +++ b/src/url.lua @@ -130,29 +130,41 @@ function parse(url, default) -- remove whitespace -- url = string.gsub(url, "%s", "") -- get fragment - url = string.gsub(url, "#(.*)$", function(f) parsed.fragment = f end) + url = string.gsub(url, "#(.*)$", function(f) + parsed.fragment = f + return "" + end) -- get scheme url = string.gsub(url, "^([%w][%w%+%-%.]*)%:", - function(s) parsed.scheme = s end) + function(s) parsed.scheme = s; return "" end) -- get authority - url = string.gsub(url, "^//([^/]*)", function(n) parsed.authority = n end) + url = string.gsub(url, "^//([^/]*)", function(n) + parsed.authority = n + return "" + end) -- get query stringing - url = string.gsub(url, "%?(.*)", function(q) parsed.query = q end) + url = string.gsub(url, "%?(.*)", function(q) + parsed.query = q + return "" + end) -- get params - url = string.gsub(url, "%;(.*)", function(p) parsed.params = p end) + url = string.gsub(url, "%;(.*)", function(p) + parsed.params = p + return "" + end) -- path is whatever was left if url ~= "" then parsed.path = url end local authority = parsed.authority if not authority then return parsed end authority = string.gsub(authority,"^([^@]*)@", - function(u) parsed.userinfo = u end) + function(u) parsed.userinfo = u; return "" end) authority = string.gsub(authority, ":([^:]*)$", - function(p) parsed.port = p end) + function(p) parsed.port = p; return "" end) if authority ~= "" then parsed.host = authority end local userinfo = parsed.userinfo if not userinfo then return parsed end userinfo = string.gsub(userinfo, ":([^:]*)$", - function(p) parsed.password = p end) + function(p) parsed.password = p; return "" end) parsed.user = userinfo return parsed end @@ -283,4 +295,3 @@ function build_path(parsed, unsafe) if parsed.is_absolute then path = "/" .. path end return path end - diff --git a/src/usocket.c b/src/usocket.c index e9db6dd..6a0b178 100644 --- a/src/usocket.c +++ b/src/usocket.c @@ -102,7 +102,8 @@ void socket_destroy(p_socket ps) { /*-------------------------------------------------------------------------*\ * Select with timeout control \*-------------------------------------------------------------------------*/ -int socket_select(int n, fd_set *rfds, fd_set *wfds, fd_set *efds, p_timeout tm) { +int socket_select(t_socket n, fd_set *rfds, fd_set *wfds, fd_set *efds, + p_timeout tm) { int ret; do { struct timeval tv; diff --git a/src/wsocket.c b/src/wsocket.c index a690dad..dce333b 100644 --- a/src/wsocket.c +++ b/src/wsocket.c @@ -46,16 +46,20 @@ int socket_close(void) { #define WAITFD_E 4 #define WAITFD_C (WAITFD_E|WAITFD_W) -int socket_waitfd(p_socket ps, int sw, p_tm tm) { +int socket_waitfd(p_socket ps, int sw, p_timeout tm) { int ret; fd_set rfds, wfds, efds, *rp = NULL, *wp = NULL, *ep = NULL; struct timeval tv, *tp = NULL; double t; - if (tm_iszero(tm)) return IO_TIMEOUT; /* optimize timeout == 0 case */ - if (sw & WAITFD_R) { FD_ZERO(&rfds); FD_SET(*ps, &rfds); rp = &rfds; } + if (timeout_iszero(tm)) return IO_TIMEOUT; /* optimize timeout == 0 case */ + if (sw & WAITFD_R) { + FD_ZERO(&rfds); + FD_SET(*ps, &rfds); + rp = &rfds; + } if (sw & WAITFD_W) { FD_ZERO(&wfds); FD_SET(*ps, &wfds); wp = &wfds; } if (sw & WAITFD_C) { FD_ZERO(&efds); FD_SET(*ps, &efds); ep = &efds; } - if ((t = tm_get(tm)) >= 0.0) { + if ((t = timeout_get(tm)) >= 0.0) { tv.tv_sec = (int) t; tv.tv_usec = (int) ((t-tv.tv_sec)*1.0e6); tp = &tv; @@ -70,9 +74,10 @@ int socket_waitfd(p_socket ps, int sw, p_tm tm) { /*-------------------------------------------------------------------------*\ * Select with int timeout in ms \*-------------------------------------------------------------------------*/ -int socket_select(int n, fd_set *rfds, fd_set *wfds, fd_set *efds, p_tm tm) { +int socket_select(t_socket n, fd_set *rfds, fd_set *wfds, fd_set *efds, + p_timeout tm) { struct timeval tv; - double t = tm_get(tm); + double t = timeout_get(tm); tv.tv_sec = (int) t; tv.tv_usec = (int) ((t - tv.tv_sec) * 1.0e6); if (n <= 0) { @@ -113,7 +118,7 @@ int socket_create(p_socket ps, int domain, int type, int protocol) { /*-------------------------------------------------------------------------*\ * Connects or returns error message \*-------------------------------------------------------------------------*/ -int socket_connect(p_socket ps, SA *addr, socklen_t len, p_tm tm) { +int socket_connect(p_socket ps, SA *addr, socklen_t len, p_timeout tm) { int err; /* don't call on closed socket */ if (*ps == SOCKET_INVALID) return IO_CLOSED; @@ -123,7 +128,7 @@ int socket_connect(p_socket ps, SA *addr, socklen_t len, p_tm tm) { err = WSAGetLastError(); if (err != WSAEWOULDBLOCK && err != WSAEINPROGRESS) return err; /* zero timeout case optimization */ - if (tm_iszero(tm)) return IO_TIMEOUT; + if (timeout_iszero(tm)) return IO_TIMEOUT; /* we wait until something happens */ err = socket_waitfd(ps, WAITFD_C, tm); if (err == IO_CLOSED) { @@ -131,7 +136,7 @@ int socket_connect(p_socket ps, SA *addr, socklen_t len, p_tm tm) { /* give windows time to set the error (yes, disgusting) */ Sleep(10); /* find out why we failed */ - getsockopt(*ps, SOL_SOCKETET, SO_ERROR, (char *)&err, &len); + getsockopt(*ps, SOL_SOCKET, SO_ERROR, (char *)&err, &len); /* we KNOW there was an error. if 'why' is 0, we will return * "unknown error", but it's not really our fault */ return err > 0? err: IO_UNKNOWN; @@ -164,7 +169,8 @@ int socket_listen(p_socket ps, int backlog) { /*-------------------------------------------------------------------------*\ * Accept with timeout \*-------------------------------------------------------------------------*/ -int socket_accept(p_socket ps, p_socket pa, SA *addr, socklen_t *len, p_tm tm) { +int socket_accept(p_socket ps, p_socket pa, SA *addr, socklen_t *len, + p_timeout tm) { SA daddr; socklen_t dlen = sizeof(daddr); if (*ps == SOCKET_INVALID) return IO_CLOSED; @@ -192,7 +198,7 @@ int socket_accept(p_socket ps, p_socket pa, SA *addr, socklen_t *len, p_tm tm) { * Therefore, whoever calls this function should not pass a huge buffer. \*-------------------------------------------------------------------------*/ int socket_send(p_socket ps, const char *data, size_t count, - size_t *sent, p_tm tm) + size_t *sent, p_timeout tm) { int err; /* avoid making system calls on closed sockets */ @@ -222,7 +228,7 @@ int socket_send(p_socket ps, const char *data, size_t count, * Sendto with timeout \*-------------------------------------------------------------------------*/ int socket_sendto(p_socket ps, const char *data, size_t count, size_t *sent, - SA *addr, socklen_t len, p_tm tm) + SA *addr, socklen_t len, p_timeout tm) { int err; if (*ps == SOCKET_INVALID) return IO_CLOSED; @@ -243,7 +249,7 @@ int socket_sendto(p_socket ps, const char *data, size_t count, size_t *sent, /*-------------------------------------------------------------------------*\ * Receive with timeout \*-------------------------------------------------------------------------*/ -int socket_recv(p_socket ps, char *data, size_t count, size_t *got, p_tm tm) { +int socket_recv(p_socket ps, char *data, size_t count, size_t *got, p_timeout tm) { int err; if (*ps == SOCKET_INVALID) return IO_CLOSED; *got = 0; @@ -265,7 +271,7 @@ int socket_recv(p_socket ps, char *data, size_t count, size_t *got, p_tm tm) { * Recvfrom with timeout \*-------------------------------------------------------------------------*/ int socket_recvfrom(p_socket ps, char *data, size_t count, size_t *got, - SA *addr, socklen_t *len, p_tm tm) { + SA *addr, socklen_t *len, p_timeout tm) { int err; if (*ps == SOCKET_INVALID) return IO_CLOSED; *got = 0; diff --git a/test/httptest.lua b/test/httptest.lua index 3816b54..0951389 100644 --- a/test/httptest.lua +++ b/test/httptest.lua @@ -22,8 +22,8 @@ http.TIMEOUT = 10 local t = socket.gettime() -host = host or "localhost" -- "diego.student.princeton.edu" -proxy = proxy or "http://localhost:3128" +host = host or "dell-diego" -- "diego.student.princeton.edu" +proxy = proxy or "http://dell-diego:3128" prefix = prefix or "/luasocket-test" cgiprefix = cgiprefix or "/luasocket-test-cgi" index_file = "index.html"