From bce1cb30d856d167e167c4c2997f9bebe03a612c Mon Sep 17 00:00:00 2001 From: Diego Nehab Date: Mon, 21 Jun 2004 06:07:58 +0000 Subject: More adjustments/bugfixes. --- src/http.lua | 2 -- src/luasocket.h | 3 +++ src/mime.h | 3 +++ src/mime.lua | 32 ++++++++++++++++---------------- src/socket.lua | 3 +-- src/tcp.c | 6 ++++-- src/udp.c | 6 +++--- src/usocket.c | 6 +----- src/wsocket.c | 6 +----- 9 files changed, 32 insertions(+), 35 deletions(-) (limited to 'src') diff --git a/src/http.lua b/src/http.lua index 9c568bc..cdb435d 100644 --- a/src/http.lua +++ b/src/http.lua @@ -22,8 +22,6 @@ TIMEOUT = 60 PORT = 80 -- user agent field sent in request USERAGENT = socket.VERSION --- block size used in transfers -BLOCKSIZE = 2048 ----------------------------------------------------------------------------- -- Low level HTTP API diff --git a/src/luasocket.h b/src/luasocket.h index 6d30605..35dc3d9 100644 --- a/src/luasocket.h +++ b/src/luasocket.h @@ -25,7 +25,10 @@ /*-------------------------------------------------------------------------*\ * Initializes the library. \*-------------------------------------------------------------------------*/ +#ifndef LUASOCKET_LIBNAME #define LUASOCKET_LIBNAME "socket" +#endif + LUASOCKET_API int luaopen_socket(lua_State *L); #endif /* LUASOCKET_H */ diff --git a/src/mime.h b/src/mime.h index be16920..ad8a573 100644 --- a/src/mime.h +++ b/src/mime.h @@ -19,7 +19,10 @@ #define MIME_API extern #endif +#ifndef MIME_LIBNAME #define MIME_LIBNAME "mime" +#endif + MIME_API int luaopen_mime(lua_State *L); #endif /* MIME_H */ diff --git a/src/mime.lua b/src/mime.lua index 345bd25..fcdc358 100644 --- a/src/mime.lua +++ b/src/mime.lua @@ -12,9 +12,9 @@ local mime = requirelib("mime", "luaopen_mime", getfenv(1)) local ltn12 = require("ltn12") -- encode, decode and wrap algorithm tables -encodet = {} -decodet = {} -wrapt = {} +mime.encodet = {} +mime.decodet = {} +mime.wrapt = {} -- creates a function that chooses a filter by name from a given table local function choose(table) @@ -29,47 +29,47 @@ local function choose(table) end -- define the encoding filters -encodet['base64'] = function() +mime.encodet['base64'] = function() return ltn12.filter.cycle(b64, "") end -encodet['quoted-printable'] = function(mode) +mime.encodet['quoted-printable'] = function(mode) return ltn12.filter.cycle(qp, "", (mode == "binary") and "=0D=0A" or "\r\n") end -- define the decoding filters -decodet['base64'] = function() +mime.decodet['base64'] = function() return ltn12.filter.cycle(unb64, "") end -decodet['quoted-printable'] = function() +mime.decodet['quoted-printable'] = function() return ltn12.filter.cycle(unqp, "") end -- define the line-wrap filters -wrapt['text'] = function(length) +mime.wrapt['text'] = function(length) length = length or 76 return ltn12.filter.cycle(wrp, length, length) end -wrapt['base64'] = wrapt['text'] -wrapt['default'] = wrapt['text'] +mime.wrapt['base64'] = wrapt['text'] +mime.wrapt['default'] = wrapt['text'] -wrapt['quoted-printable'] = function() +mime.wrapt['quoted-printable'] = function() return ltn12.filter.cycle(qpwrp, 76, 76) end -- function that choose the encoding, decoding or wrap algorithm -encode = choose(encodet) -decode = choose(decodet) -wrap = choose(wrapt) +mime.encode = choose(encodet) +mime.decode = choose(decodet) +mime.wrap = choose(wrapt) -- define the end-of-line normalization filter -function normalize(marker) +function mime.normalize(marker) return ltn12.filter.cycle(eol, 0, marker) end -- high level stuffing filter -function stuff() +function mime.stuff() return ltn12.filter.cycle(dot, 2) end diff --git a/src/socket.lua b/src/socket.lua index 4d64651..b3889d7 100644 --- a/src/socket.lua +++ b/src/socket.lua @@ -8,7 +8,6 @@ -- Load LuaSocket from dynamic library ----------------------------------------------------------------------------- local socket = requirelib("luasocket", "luaopen_socket", getfenv(1)) -_LOADED["socket"] = socket ----------------------------------------------------------------------------- -- Auxiliar functions @@ -31,7 +30,7 @@ function socket.bind(host, port, backlog) sock:setoption("reuseaddr", true) local res, err = sock:bind(host, port) if not res then return nil, err end - backlog = backlog or 1 + backlog = backlog or 32 res, err = sock:listen(backlog) if not res then return nil, err end return sock diff --git a/src/tcp.c b/src/tcp.c index adc2585..cbfebdb 100644 --- a/src/tcp.c +++ b/src/tcp.c @@ -130,7 +130,7 @@ static int meth_setoption(lua_State *L) static int meth_getfd(lua_State *L) { p_tcp tcp = (p_tcp) aux_checkgroup(L, "tcp{any}", 1); - lua_pushnumber(L, tcp->sock); + lua_pushnumber(L, (int) tcp->sock); return 1; } @@ -164,6 +164,7 @@ static int meth_accept(lua_State *L) p_tcp clnt = lua_newuserdata(L, sizeof(t_tcp)); aux_setclass(L, "tcp{client}", -1); /* initialize structure fields */ + sock_setnonblocking(&sock); clnt->sock = sock; io_init(&clnt->io, (p_send)sock_send, (p_recv)sock_recv, &clnt->sock); tm_init(&clnt->tm, -1, -1); @@ -310,10 +311,11 @@ static int global_create(lua_State *L) if (!err) { /* allocate tcp object */ p_tcp tcp = (p_tcp) lua_newuserdata(L, sizeof(t_tcp)); - tcp->sock = sock; /* set its type as master object */ aux_setclass(L, "tcp{master}", -1); /* initialize remaining structure fields */ + sock_setnonblocking(&sock); + tcp->sock = sock; io_init(&tcp->io, (p_send) sock_send, (p_recv) sock_recv, &tcp->sock); tm_init(&tcp->tm, -1, -1); buf_init(&tcp->buf, &tcp->io, &tcp->tm); diff --git a/src/udp.c b/src/udp.c index 512cc0b..d20d61b 100644 --- a/src/udp.c +++ b/src/udp.c @@ -208,7 +208,7 @@ static int meth_receivefrom(lua_State *L) static int meth_getfd(lua_State *L) { p_udp udp = (p_udp) aux_checkgroup(L, "udp{any}", 1); - lua_pushnumber(L, udp->sock); + lua_pushnumber(L, (int) udp->sock); return 1; } @@ -328,10 +328,10 @@ static int global_create(lua_State *L) if (!err) { /* allocate tcp object */ p_udp udp = (p_udp) lua_newuserdata(L, sizeof(t_udp)); - udp->sock = sock; - /* set its type as master object */ aux_setclass(L, "udp{unconnected}", -1); /* initialize remaining structure fields */ + sock_setnonblocking(&sock); + udp->sock = sock; tm_init(&udp->tm, -1, -1); return 1; } else { diff --git a/src/usocket.c b/src/usocket.c index ea0f172..617b1ea 100644 --- a/src/usocket.c +++ b/src/usocket.c @@ -81,7 +81,6 @@ const char *sock_create(p_sock ps, int domain, int type, int protocol) t_sock sock = socket(domain, type, protocol); if (sock == SOCK_INVALID) return sock_createstrerror(errno); *ps = sock; - sock_setnonblocking(ps); return NULL; } @@ -178,10 +177,7 @@ const char *sock_accept(p_sock ps, p_sock pa, SA *addr, do *pa = accept(sock, addr, addr_len); while (*pa < 0 && errno == EINTR); /* if result is valid, we are done */ - if (*pa != SOCK_INVALID) { - sock_setnonblocking(pa); - return NULL; - } + if (*pa != SOCK_INVALID) return NULL; /* find out if we failed for a fatal reason */ if (errno != EWOULDBLOCK && errno != ECONNABORTED) return sock_acceptstrerror(errno); diff --git a/src/wsocket.c b/src/wsocket.c index 84a49dc..e276fe0 100644 --- a/src/wsocket.c +++ b/src/wsocket.c @@ -88,7 +88,6 @@ const char *sock_create(p_sock ps, int domain, int type, int protocol) if (sock == SOCK_INVALID) return sock_createstrerror(WSAGetLastError()); *ps = sock; - sock_setnonblocking(ps); return NULL; } @@ -177,10 +176,7 @@ const char *sock_accept(p_sock ps, p_sock pa, SA *addr, /* try to get client socket */ *pa = accept(sock, addr, addr_len); /* if return is valid, we are done */ - if (*pa != SOCK_INVALID) { - sock_setnonblocking(pa); - return NULL; - } + if (*pa != SOCK_INVALID) return NULL; /* optimization */ if (timeout == 0) return io_strerror(IO_TIMEOUT); /* otherwise find out why we failed */ -- cgit v1.2.3-55-g6feb