More adjustments/bugfixes.
This commit is contained in:
parent
f7579db9e8
commit
bce1cb30d8
33 changed files with 135 additions and 96 deletions
|
@ -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
|
||||
|
|
|
@ -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 */
|
||||
|
|
|
@ -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 */
|
||||
|
|
32
src/mime.lua
32
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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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 */
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue