From bce1cb30d856d167e167c4c2997f9bebe03a612c Mon Sep 17 00:00:00 2001 From: Diego Nehab Date: Mon, 21 Jun 2004 06:07:58 +0000 Subject: [PATCH] More adjustments/bugfixes. --- doc/dns.html | 6 ++++-- doc/ftp.html | 10 +++++++--- doc/http.html | 6 ++++-- doc/index.html | 6 ++++-- doc/introduction.html | 8 +++++--- doc/ltn12.html | 7 +++++-- doc/mime.html | 8 +++++--- doc/reference.html | 6 ++++-- doc/smtp.html | 12 +++++++----- doc/socket.html | 20 ++++++++++++++++---- doc/tcp.html | 17 ++++++++++------- doc/udp.html | 12 ++++++++---- doc/url.html | 12 +++++++----- samples/cddb.lua | 4 ++-- samples/daytimeclnt.lua | 2 +- samples/echosrvr.lua | 2 +- samples/talker.lua | 2 +- samples/tinyirc.lua | 2 +- 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 +----- test/httptest.lua | 10 +++++----- test/mimetest.lua | 2 +- test/stufftest.lua | 2 +- test/testclnt.lua | 4 ++-- test/testsrvr.lua | 2 +- test/urltest.lua | 2 +- 33 files changed, 135 insertions(+), 96 deletions(-) diff --git a/doc/dns.html b/doc/dns.html index 47134d8..09a9bdb 100644 --- a/doc/dns.html +++ b/doc/dns.html @@ -3,7 +3,9 @@ -LuaSocket: Network support for the Lua language + + +LuaSocket: DNS support @@ -16,7 +18,7 @@
diff --git a/doc/ftp.html b/doc/ftp.html index fe514e9..158b402 100644 --- a/doc/ftp.html +++ b/doc/ftp.html @@ -1,7 +1,11 @@ - + + -LuaSocket: Network support for the Lua language + + +LuaSocket: FTP support @@ -14,7 +18,7 @@
-LuaSocket +LuaSocket
Network support for the Lua language
diff --git a/doc/http.html b/doc/http.html index 4780436..13acd1c 100644 --- a/doc/http.html +++ b/doc/http.html @@ -3,7 +3,9 @@ -LuaSocket: Network support for the Lua language + + +LuaSocket: HTTP support @@ -16,7 +18,7 @@
-LuaSocket +LuaSocket
Network support for the Lua language
diff --git a/doc/index.html b/doc/index.html index 0e9eb1d..abba703 100644 --- a/doc/index.html +++ b/doc/index.html @@ -3,6 +3,8 @@ + +LuaSocket: Network support for the Lua language @@ -16,7 +18,7 @@
-LuaSocket +LuaSocket
Network support for the Lua language
@@ -276,7 +278,7 @@ return convention WILL break old code; does not call listen anymore. It is the new listen method that turns a TCP object into a server object; -
  • socket.time became socket.gettime for uniformity; +
  • socket.time changed to socket.gettime for uniformity;
  • Interface to options has changed. diff --git a/doc/introduction.html b/doc/introduction.html index 2c738d6..6468148 100644 --- a/doc/introduction.html +++ b/doc/introduction.html @@ -3,7 +3,9 @@ -LuaSocket: Network support for the Lua language + + +LuaSocket: Introduction to the core @@ -16,7 +18,7 @@
  • -LuaSocket +LuaSocket
    Network support for the Lua language
    @@ -209,7 +211,7 @@ print("Please telnet to localhost on port " .. port) print("After connecting, you have 10s to enter a line to be echoed") -- loop forever waiting for clients while 1 do - -- wait for a conection from any client + -- wait for a connection from any client local client = server:accept() -- make sure we don't block waiting for this client's line client:settimeout(10) diff --git a/doc/ltn12.html b/doc/ltn12.html index bfb9b51..40dcf96 100644 --- a/doc/ltn12.html +++ b/doc/ltn12.html @@ -3,7 +3,10 @@ -LuaSocket: Network support for the Lua language + + +LuaSocket: LTN12 module @@ -16,7 +19,7 @@
    -LuaSocket +LuaSocket
    Network support for the Lua language
    diff --git a/doc/mime.html b/doc/mime.html index 0f4ffa8..6758a4b 100644 --- a/doc/mime.html +++ b/doc/mime.html @@ -3,7 +3,9 @@ -LuaSocket: Network support for the Lua language + + +LuaSocket: MIME module @@ -16,7 +18,7 @@
    -LuaSocket +LuaSocket
    Network support for the Lua language
    @@ -92,7 +94,7 @@ The function returns a filter that performs the conversion. Note: There is no perfect solution to this problem. Different end-of-line markers are an evil that will probably plague developers forever. This function, however, will work perfectly for text created with any of -the most common end-of-line markers, i.e. the MacOS (CR), the Unix (LF), +the most common end-of-line markers, i.e. the Mac OS (CR), the Unix (LF), or the DOS (CRLF) conventions. Even if the data has mixed end-of-line markers, the function will still work well, although it doesn't guarantee that the number of empty lines will be correct. diff --git a/doc/reference.html b/doc/reference.html index 5c07c4d..8d129e3 100644 --- a/doc/reference.html +++ b/doc/reference.html @@ -3,7 +3,9 @@ -LuaSocket: Network support for the Lua language + + +LuaSocket: Index to reference manual @@ -16,7 +18,7 @@
    -LuaSocket +LuaSocket
    Network support for the Lua language
    diff --git a/doc/smtp.html b/doc/smtp.html index 224dde3..b975045 100644 --- a/doc/smtp.html +++ b/doc/smtp.html @@ -3,7 +3,9 @@ -LuaSocket: Network support for the Lua language + + +LuaSocket: SMTP support @@ -16,7 +18,7 @@
    -LuaSocket +LuaSocket
    Network support for the Lua language
    @@ -48,7 +50,7 @@ control (if you bother to read the code). Another RFC of interest is RFC 2822, which governs the Internet Message Format. -Multipart messages (those that contain attatchments) are part +Multipart messages (those that contain attachments) are part of the MIME standard, but described mainly in RFC 2046 @@ -98,7 +100,7 @@ in representing them in a Lua table.

    -The following constants can be set to control the default behaviour of +The following constants can be set to control the default behavior of the SMTP module:

    @@ -256,7 +258,7 @@ mesgt = { to = "Fulano da Silva <fulano@example.com>", cc = '"Beltrano F. Nunes" <beltrano@example.com>', subject = "My first message" - } + }, body = "I hope this works. If it does, I can send you another 1000 copies." } diff --git a/doc/socket.html b/doc/socket.html index 1bbeb7f..129b134 100644 --- a/doc/socket.html +++ b/doc/socket.html @@ -3,7 +3,9 @@ -LuaSocket: Network support for the Lua language + + +LuaSocket: The socket namespace @@ -16,7 +18,7 @@
    -LuaSocket +LuaSocket
    Network support for the Lua language
    @@ -55,6 +57,16 @@ socket.bind(address, port [, backlog])

    +This function is a shortcut that creates and returns a TCP server object +bound to a local address and port, ready to +accept client connections. Optionally, +user can also specify the backlog argument to the +listen method (defaults to 32). +

    + +

    +Note: The server object returned will have the option "reuseaddr" +set to true.

    @@ -64,9 +76,9 @@ socket.connect(address, port [, locaddr, locport])

    -This function is a shortcut that creates and returns a TCP socket object +This function is a shortcut that creates and returns a TCP client object connected to a remote host at a given port. Optionally, -the use can also specify the local address and port to bind +the user can also specify the local address and port to bind (locaddr and locport).

    diff --git a/doc/tcp.html b/doc/tcp.html index 37b0b56..5c0da75 100644 --- a/doc/tcp.html +++ b/doc/tcp.html @@ -3,7 +3,9 @@ -LuaSocket: Network support for the Lua language + + +LuaSocket: TCP/IP support @@ -16,7 +18,7 @@
    -LuaSocket +LuaSocket
    Network support for the Lua language
    @@ -74,7 +76,7 @@ reported by nil followed by a message describing the error.

    -Note: calling socket.select +Note: calling socket.select with a server object in the receive parameter before a call to accept does not guarantee accept will return immediately. Use the nil followed by an error message.

    -Note: The function socket.bind +Note: The function socket.bind is available and is a shortcut for the creation server sockets.

    @@ -163,15 +165,15 @@ describing the error. In case of success, the method returns 1.

    -Note: The function socket.connect +Note: The function socket.connect is available and is a shortcut for the creation of client sockets.

    Note: Starting with LuaSocket 2.0, the settimeout -function affects the behavior of connect, causing it to return in case of -a timeout error. +method affects the behavior of connect, causing it to return in case of +a timeout.

    @@ -197,6 +199,7 @@ Note: It makes no sense to call this method on server objects.

    +master:getsockname()
    client:getsockname()
    server:getsockname()

    diff --git a/doc/udp.html b/doc/udp.html index 69895ce..75d1d9e 100644 --- a/doc/udp.html +++ b/doc/udp.html @@ -1,10 +1,14 @@ + "http://www.w3.org/TR/html4/strict.dtd"> + -LuaSocket: Network support for the Lua language + + +LuaSocket: UDP support + @@ -14,7 +18,7 @@
    -LuaSocket +LuaSocket
    Network support for the Lua language
    @@ -240,7 +244,7 @@ unconnected:setpeername(address, port)

    Changes the peer of a UDP object. This method turns an unconnected UDP object into a connected UDP -object or vice-versa. +object or vice versa.

    diff --git a/doc/url.html b/doc/url.html index 207995c..92acf36 100644 --- a/doc/url.html +++ b/doc/url.html @@ -3,7 +3,9 @@ -LuaSocket: Network support for the Lua language + + +LuaSocket: URL support @@ -16,7 +18,7 @@

    -LuaSocket +LuaSocket
    Network support for the Lua language
    @@ -135,7 +137,7 @@ Rebuilds an URL from its parts. Parsed_url is a table with same components returned by parse. Lower level components, if specified, -take precedence over hight level components of the URL grammar. +take precedence over high level components of the URL grammar.

    @@ -279,8 +281,8 @@ Breaks a <path> URL component into all its

    Since some characters are reserved in URLs, they must be escaped whenever present in a <path> component. Therefore, before -returning a list with all the parsed segments, the function unescapes all -of them. +returning a list with all the parsed segments, the function removes +escaping from all of them.

    diff --git a/samples/cddb.lua b/samples/cddb.lua index aefa04e..daa5df1 100644 --- a/samples/cddb.lua +++ b/samples/cddb.lua @@ -1,5 +1,5 @@ -socket = require("socket") -http = require("http") +local socket = require("socket") +local http = require("http") if not arg or not arg[1] or not arg[2] then print("luasocket cddb.lua []") diff --git a/samples/daytimeclnt.lua b/samples/daytimeclnt.lua index b4075e9..6a26ffc 100644 --- a/samples/daytimeclnt.lua +++ b/samples/daytimeclnt.lua @@ -4,7 +4,7 @@ -- Author: Diego Nehab -- RCS ID: $Id$ ----------------------------------------------------------------------------- -require"socket" +local socket = require"socket" host = host or "127.0.0.1" port = port or 13 if arg then diff --git a/samples/echosrvr.lua b/samples/echosrvr.lua index 73bf374..3ebbe85 100644 --- a/samples/echosrvr.lua +++ b/samples/echosrvr.lua @@ -4,7 +4,7 @@ -- Author: Diego Nehab -- RCS ID: $Id$ ----------------------------------------------------------------------------- -socket = require("socket") +local socket = require("socket") host = host or "127.0.0.1" port = port or 7 if arg then diff --git a/samples/talker.lua b/samples/talker.lua index bafd021..3f6e69c 100644 --- a/samples/talker.lua +++ b/samples/talker.lua @@ -4,7 +4,7 @@ -- Author: Diego Nehab -- RCS ID: $Id$ ----------------------------------------------------------------------------- -socket = require("socket") +local socket = require("socket") host = host or "localhost" port = port or 8080 if arg then diff --git a/samples/tinyirc.lua b/samples/tinyirc.lua index 85ebe29..13f42ec 100644 --- a/samples/tinyirc.lua +++ b/samples/tinyirc.lua @@ -4,7 +4,7 @@ -- Author: Diego Nehab -- RCS ID: $Id$ ----------------------------------------------------------------------------- -socket = require("socket") +local socket = require("socket") host = host or "*" port1 = port1 or 8080 port2 = port2 or 8181 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 */ diff --git a/test/httptest.lua b/test/httptest.lua index ea8465d..3a4d461 100644 --- a/test/httptest.lua +++ b/test/httptest.lua @@ -4,11 +4,11 @@ -- to "/luasocket-test-cgi" and "/luasocket-test-cgi/" -- needs "AllowOverride AuthConfig" on /home/c/diego/tec/luasocket/test/auth -socket = require("socket") -http = require("http") -mime = require("mime") -url = require("url") -ltn12 = require("ltn12") +local socket = require("socket") +local http = require("http") +local mime = require("mime") +local url = require("url") +local ltn12 = require("ltn12") dofile("testsupport.lua") diff --git a/test/mimetest.lua b/test/mimetest.lua index f51c3b8..685c610 100644 --- a/test/mimetest.lua +++ b/test/mimetest.lua @@ -8,7 +8,7 @@ local qptest = "qptest.bin" local eqptest = "qptest.bin2" local dqptest = "qptest.bin3" -local b64test = "luasocket.dll" +local b64test = "luasocket.dylib" local eb64test = "b64test.bin" local db64test = "b64test.bin2" diff --git a/test/stufftest.lua b/test/stufftest.lua index eddf636..490053f 100644 --- a/test/stufftest.lua +++ b/test/stufftest.lua @@ -1,4 +1,4 @@ -mime = require("mime") +local mime = require("mime") function test_dot(original, right) local result, n = mime.dot(2, original) diff --git a/test/testclnt.lua b/test/testclnt.lua index 1b20ad1..9aa07fe 100644 --- a/test/testclnt.lua +++ b/test/testclnt.lua @@ -1,4 +1,4 @@ -socket = require"socket" +local socket = require"socket" host = host or "localhost" port = port or "8080" @@ -418,7 +418,7 @@ function connect_errors() assert(not c and e == "connection refused", e) print("ok") io.stderr:write("host not found: ") - local c, e = socket.connect("not.exist.com", 1); + local c, e = socket.connect("host.is.invalid", 1); assert(not c and e == "host not found", e) print("ok") end diff --git a/test/testsrvr.lua b/test/testsrvr.lua index 5b842f1..71281cf 100644 --- a/test/testsrvr.lua +++ b/test/testsrvr.lua @@ -1,4 +1,4 @@ -socket = require"socket" +local socket = require"socket" host = host or "localhost" port = port or "8080" diff --git a/test/urltest.lua b/test/urltest.lua index 92f2fae..eb15bcf 100644 --- a/test/urltest.lua +++ b/test/urltest.lua @@ -1,4 +1,4 @@ -socket = require("socket") +local socket = require("socket") socket.url = require("url") dofile("testsupport.lua")
    -LuaSocket +LuaSocket
    Network support for the Lua language