Changed prefix of function names to match module names.

Removed some warnings and useless code.
master
Diego Nehab 2005-11-20 07:20:26 +00:00
parent 087b4f865d
commit f20f4889bf
11 changed files with 110 additions and 89 deletions

20
config
View File

@ -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

View File

@ -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"/>
<Tool

View File

@ -156,6 +156,12 @@
</File>
<File
RelativePath="src\wsocket.c">
<FileConfiguration
Name="Release|Win32">
<Tool
Name="VCCLCompilerTool"
GeneratePreprocessedFile="0"/>
</FileConfiguration>
</File>
</Filter>
<Filter

View File

@ -152,8 +152,8 @@ static int mime_global_wrp(lua_State *L)
static void b64setup(UC *b64unbase)
{
int i;
for (i = 0; i < 255; i++) b64unbase[i] = 255;
for (i = 0; i < 64; i++) b64unbase[b64base[i]] = i;
for (i = 0; i <= 255; i++) b64unbase[i] = (UC) 255;
for (i = 0; i < 64; i++) b64unbase[b64base[i]] = (UC) i;
b64unbase['='] = 0;
}
@ -191,7 +191,7 @@ static size_t b64pad(const UC *input, size_t size,
luaL_Buffer *buffer)
{
unsigned long value = 0;
UC code[4] = "====";
UC code[4] = {'=', '=', '=', '='};
switch (size) {
case 1:
value = input[0] << 4;
@ -480,38 +480,31 @@ static int mime_global_qp(lua_State *L)
* Accumulate characters until we are sure about how to deal with them.
* Once we are sure, output the to the buffer, in the correct form.
\*-------------------------------------------------------------------------*/
static size_t qpdecode(UC c, UC *input, size_t size,
luaL_Buffer *buffer)
{
static size_t qpdecode(UC c, UC *input, size_t size, luaL_Buffer *buffer) {
int d;
input[size++] = c;
/* deal with all characters we can deal */
while (size > 0) {
int c, d;
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;
}
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;
}
/*-------------------------------------------------------------------------*\

View File

@ -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));
}

View File

@ -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);

View File

@ -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);

View File

@ -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

View File

@ -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;

View File

@ -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;

View File

@ -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"