From a32c6d91402329adb769c4fe8bbb24f0d4e99051 Mon Sep 17 00:00:00 2001 From: Diego Nehab Date: Thu, 29 Sep 2005 06:11:42 +0000 Subject: [PATCH] Almost ready to release. --- TODO | 12 +- config | 2 - doc/installation.html | 54 ++++---- etc/check-links-nb.lua | 277 ----------------------------------------- etc/check-links.lua | 2 +- etc/check-memory.lua | 4 +- etc/get.lua | 2 +- src/auxiliar.h | 4 +- src/buffer.c | 4 +- src/buffer.h | 2 +- src/except.c | 5 +- src/except.h | 2 +- src/inet.c | 4 +- src/inet.h | 2 +- src/io.c | 1 - src/io.h | 5 +- src/luasocket.c | 10 +- src/luasocket.h | 4 +- src/makefile | 7 +- src/mime.c | 11 +- src/mime.h | 11 +- src/options.c | 3 +- src/options.h | 2 +- src/select.c | 4 +- src/socket.h | 1 - src/tcp.c | 4 +- src/tcp.h | 2 +- src/timeout.c | 4 +- src/timeout.h | 2 +- src/udp.c | 4 +- src/udp.h | 2 +- src/unix.c | 4 +- src/unix.h | 2 +- src/url.lua | 5 + src/usocket.c | 11 +- src/wsocket.c | 16 +-- test/testclnt.lua | 3 +- test/urltest.lua | 4 +- 38 files changed, 115 insertions(+), 383 deletions(-) delete mode 100644 etc/check-links-nb.lua diff --git a/TODO b/TODO index bd60aaa..1d7a1db 100644 --- a/TODO +++ b/TODO @@ -1,3 +1,7 @@ + +get rid of setmetatable(, nil) since packages don't need this anymore in +5.1 + new instalation scheme??? test empty socket.select no windows. @@ -15,14 +19,7 @@ change ltn13 to make sure drawbacks are obvious use mike's "don't set to blocking before closing unless needed" patch? take a look at DB's smtp patch (add "extra argument" table) -optmize aux_getgroupudata (Mike idea) - make aux_newclass receive upvalues - use one upvalue per string name of class/group - make aux_checkgroup by upvalue (faster) -add error message stuff to the manual - -make sure all modules that can use it actually use socket.newtry adicionar exemplos de expansão: pipe, local, named pipe testar os options! - Thread-safe @@ -30,6 +27,7 @@ testar os options! - proteger ou atomizar o conjunto (timedout, receive), (timedout, send) - inet_ntoa também é uma merda. +* protect doesn't catch errors by error and assert * BUG NO SET DO TINYIRC!!! SINISTRO. * _VERSION, _DEBUG, etc. * talk about new create field in HTTP, FTP and SMTP diff --git a/config b/config index a85e8e6..55a9a63 100644 --- a/config +++ b/config @@ -27,8 +27,6 @@ COMPAT=compat-5.1r4 # Top of your Lua installation # Relative paths will be inside the src tree # -#INSTALL_TOP_LUA=/usr/local/share/lua/5.0 -#INSTALL_TOP_LIB=/usr/local/lib/lua/5.0 INSTALL_TOP_LUA=share INSTALL_TOP_LIB=lib diff --git a/doc/installation.html b/doc/installation.html index 62604ed..d30e919 100644 --- a/doc/installation.html +++ b/doc/installation.html @@ -63,24 +63,28 @@ the package scheme will likely already have been answered.

Directory structure

-

The standard distribution reserves a directory to be the root of -the libraries installed -on a given system. Let's call this directory <ROOT>. -On my system, this is the /usr/local/share/lua/5.0 directory. -Here is the standard LuaSocket distribution directory structure:

+

On Unix systems, the standard distribution uses two base +directories, one for system dependent files, and another for system +independent files. Let's call these directories <LIB> +and <SHARE>, respectively. +For instance, in my laptop, I use '/usr/local/lib/lua/5.0' for +<LIB> and '/usr/local/share/lua/5.0' for +<SHARE>. On Windows, only one directory is used, say +'c:\program files\lua\5.0'. Here is the standard LuaSocket +distribution directory structure:

-<ROOT>/compat-5.1.lua
-<ROOT>/ltn12.lua
-<ROOT>/mime/init.lua
-<ROOT>/mime/core.dll
-<ROOT>/socket/init.lua
-<ROOT>/socket/core.dll
-<ROOT>/socket/http.lua
-<ROOT>/socket/tp.lua
-<ROOT>/socket/ftp.lua
-<ROOT>/socket/smtp.lua
-<ROOT>/socket/url.lua
+<SHARE>/compat-5.1.lua
+<SHARE>/ltn12.lua
+<SHARE>/mime/init.lua
+<LIB>/mime/core.dll
+<SHARE>/socket/init.lua
+<LIB>/socket/core.dll
+<SHARE>/socket/http.lua
+<SHARE>/socket/tp.lua
+<SHARE>/socket/ftp.lua
+<SHARE>/socket/smtp.lua
+<SHARE>/socket/url.lua
 

Naturally, on Unix systems, core.dll @@ -91,7 +95,7 @@ environment variables need to be set. The first environment variable tells the interpreter to load the compat-5.1.lua module at startup:

-LUA_INIT=@<ROOT>/compat-5.1.lua
+LUA_INIT=@<SHARE>/compat-5.1.lua
 

@@ -101,13 +105,12 @@ directories and with the appropriate filename extensions.

-LUA_PATH=<ROOT>/?.lua;?.lua
-LUA_CPATH=<ROOT>/?.dll;?.dll
+LUA_PATH=<SHARE>/?.lua;?.lua
+LUA_CPATH=<LIB>/?.dll;?.dll
 
-

Again, naturally, in Unix the shared library extension would be -.so instead of .dll and on Mac OS X it would be -.dylib

+

Again, naturally, on Unix systmems the shared library extension would be +.so instead of .dll

Using LuaSocket

@@ -118,8 +121,8 @@ it should be easy to use LuaSocket. Just fire the interpreter and use the
 Lua 5.0.2  Copyright (C) 1994-2004 Tecgraf, PUC-Rio
 > socket = require("socket")
-> print(socket.VERSION)
---> LuaSocket 2.0 (beta3)
+> print(socket._VERSION)
+--> LuaSocket 2.0
 

Each module loads their dependencies automatically, so you only need to @@ -128,7 +131,8 @@ load the modues you directly depend upon:

 Lua 5.0.2  Copyright (C) 1994-2004 Tecgraf, PUC-Rio
 > http = require("socket.http")
-> print(http.request("http://www.tecgraf.puc-rio.br/luasocket"))
+>
+print(http.request("http://www.cs.princeton.edu/~diego/professional/luasocket"))
 --> homepage gets dumped to terminal
 
diff --git a/etc/check-links-nb.lua b/etc/check-links-nb.lua deleted file mode 100644 index c379e9a..0000000 --- a/etc/check-links-nb.lua +++ /dev/null @@ -1,277 +0,0 @@ ------------------------------------------------------------------------------ --- Little program that checks links in HTML files, using coroutines and --- non-blocking I/O. Thus, faster than simpler version of same program --- LuaSocket sample files --- Author: Diego Nehab --- RCS ID: $$ ------------------------------------------------------------------------------ -local socket = require("socket") - -TIMEOUT = 10 - --- we need to yield across calls to protect, so we can't use pcall --- we borrow and simplify code from coxpcall to reimplement socket.protect --- before loading http -function socket.protect(f) - return function(...) - local co = coroutine.create(f) - while true do - local results = {coroutine.resume(co, unpack(arg))} - local status = results[1] - table.remove(results, 1) - if not status then - return nil, results[1][1] - end - if coroutine.status(co) == "suspended" then - arg = {coroutine.yield(unpack(results))} - else - return unpack(results) - end - end - end -end - -local http = require("socket.http") -local url = require("socket.url") - --- creates a new set data structure -function newset() - local reverse = {} - local set = {} - return setmetatable(set, {__index = { - insert = function(set, value) - if not reverse[value] then - table.insert(set, value) - reverse[value] = table.getn(set) - end - end, - remove = function(set, value) - local index = reverse[value] - if index then - reverse[value] = nil - local top = table.remove(set) - if top ~= value then - reverse[top] = index - set[index] = top - end - end - end - }}) -end - -local context = {} -local sending = newset() -local receiving = newset() -local nthreads = 0 - --- socket.tcp() replacement for non-blocking I/O --- implements enough functionality to be used with http.request --- in Lua 5.1, we have coroutine.running to simplify things... -function newcreate(thread) - return function() - -- try to create underlying socket - local tcp, error = socket.tcp() - if not tcp then return nil, error end - -- put it in non-blocking mode right away - tcp:settimeout(0) - local trap = { - -- we ignore settimeout to preserve our 0 timeout - settimeout = function(self, mode, value) - return 1 - end, - -- send in non-blocking mode and yield on timeout - send = function(self, data, first, last) - first = (first or 1) - 1 - local result, error - while true do - -- tell dispatcher we want to keep sending before we - -- yield control - sending:insert(tcp) - -- return control to dispatcher - -- if upon return the dispatcher tells us we timed out, - -- return an error to whoever called us - if coroutine.yield() == "timeout" then - return nil, "timeout" - end - -- mark time we started waiting - context[tcp].last = socket.gettime() - -- try sending - result, error, first = tcp:send(data, first+1, last) - -- if we are done, or there was an unexpected error, - -- break away from loop - if error ~= "timeout" then return result, error, first end - end - end, - -- receive in non-blocking mode and yield on timeout - receive = function(self, pattern) - local error, partial = "timeout", "" - local value - while true do - -- tell dispatcher we want to keep receiving before we - -- yield control - receiving:insert(tcp) - -- return control to dispatcher - -- if upon return the dispatcher tells us we timed out, - -- return an error to whoever called us - if coroutine.yield() == "timeout" then - return nil, "timeout" - end - -- mark time we started waiting - context[tcp].last = socket.gettime() - -- try receiving - value, error, partial = tcp:receive(pattern, partial) - -- if we are done, or there was an unexpected error, - -- break away from loop - if error ~= "timeout" then return value, error, partial end - end - end, - -- connect in non-blocking mode and yield on timeout - connect = function(self, host, port) - local result, error = tcp:connect(host, port) - -- mark time we started waiting - context[tcp].last = socket.gettime() - if error == "timeout" then - -- tell dispatcher we will be able to write uppon connection - sending:insert(tcp) - -- return control to dispatcher - -- if upon return the dispatcher tells us we have a - -- timeout, just abort - if coroutine.yield() == "timeout" then - return nil, "timeout" - end - -- when we come back, check if connection was successful - result, error = tcp:connect(host, port) - if result or error == "already connected" then return 1 - else return nil, "non-blocking connect failed" end - else return result, error end - end, - close = function(self) - context[tcp] = nil - return tcp:close() - end - } - -- add newly created socket to context - context[tcp] = { - thread = thread, - trap = trap - } - return trap - end -end - --- get the status of a URL, non-blocking -function getstatus(link) - local parsed = url.parse(link, {scheme = "file"}) - if parsed.scheme == "http" then - local thread = coroutine.create(function(thread, link) - local r, c, h, s = http.request{ - method = "HEAD", - url = link, - create = newcreate(thread) - } - if c == 200 then io.write('\t', link, '\n') - else io.write('\t', link, ': ', c, '\n') end - nthreads = nthreads - 1 - end) - nthreads = nthreads + 1 - assert(coroutine.resume(thread, thread, link)) - end -end - --- dispatch all threads until we are done -function dispatch() - while nthreads > 0 do - -- check which sockets are interesting and act on them - local readable, writable = socket.select(receiving, sending, 1) - -- for all readable connections, resume their threads - for _, who in ipairs(readable) do - if context[who] then - receiving:remove(who) - assert(coroutine.resume(context[who].thread)) - end - end - -- for all writable connections, do the same - for _, who in ipairs(writable) do - if context[who] then - sending:remove(who) - assert(coroutine.resume(context[who].thread)) - end - end - -- politely ask replacement I/O functions in idle threads to - -- return reporting a timeout - local now = socket.gettime() - for who, data in pairs(context) do - if data.last and now - data.last > TIMEOUT then - sending:remove(who) - receiving:remove(who) - assert(coroutine.resume(context[who].thread, "timeout")) - end - end - end -end - -function readfile(path) - path = url.unescape(path) - local file, error = io.open(path, "r") - if file then - local body = file:read("*a") - file:close() - return body - else return nil, error end -end - -function load(u) - local parsed = url.parse(u, { scheme = "file" }) - local body, headers, code, error - local base = u - if parsed.scheme == "http" then - body, code, headers = http.request(u) - if code == 200 then - -- if there was a redirect, update base to reflect it - base = headers.location or base - end - if not body then - error = code - end - elseif parsed.scheme == "file" then - body, error = readfile(parsed.path) - else error = string.format("unhandled scheme '%s'", parsed.scheme) end - return base, body, error -end - -function getlinks(body, base) - -- get rid of comments - body = string.gsub(body, "%<%!%-%-.-%-%-%>", "") - local links = {} - -- extract links - body = string.gsub(body, '[Hh][Rr][Ee][Ff]%s*=%s*"([^"]*)"', function(href) - table.insert(links, url.absolute(base, href)) - end) - body = string.gsub(body, "[Hh][Rr][Ee][Ff]%s*=%s*'([^']*)'", function(href) - table.insert(links, url.absolute(base, href)) - end) - string.gsub(body, "[Hh][Rr][Ee][Ff]%s*=%s*(.-)>", function(href) - table.insert(links, url.absolute(base, href)) - end) - return links -end - -function checklinks(address) - local base, body, error = load(address) - if not body then print(error) return end - print("Checking ", base) - local links = getlinks(body, base) - for _, link in ipairs(links) do - getstatus(link) - end -end - -arg = arg or {} -if table.getn(arg) < 1 then - print("Usage:\n luasocket check-links.lua {}") - exit() -end -for _, address in ipairs(arg) do - checklinks(url.absolute("file:", address)) -end -dispatch() diff --git a/etc/check-links.lua b/etc/check-links.lua index 725cd2a..5c124a8 100644 --- a/etc/check-links.lua +++ b/etc/check-links.lua @@ -5,7 +5,7 @@ -- Author: Diego Nehab -- RCS ID: $$ ----------------------------------------------------------------------------- -local url = require("socket.url") +local url = require("url") local dispatch = require("dispatch") local http = require("socket.http") dispatch.TIMEOUT = 10 diff --git a/etc/check-memory.lua b/etc/check-memory.lua index 7892ba0..7bcdf67 100644 --- a/etc/check-memory.lua +++ b/etc/check-memory.lua @@ -7,9 +7,9 @@ function load(s) print(s .. ":\t " .. (b-a) .. "k") end -load("socket") -load("socket.url") +load("url") load("ltn12") +load("socket") load("mime") load("socket.tp") load("socket.smtp") diff --git a/etc/get.lua b/etc/get.lua index 0c95d54..bd5af28 100644 --- a/etc/get.lua +++ b/etc/get.lua @@ -7,7 +7,7 @@ local socket = require("socket") local http = require("socket.http") local ftp = require("socket.ftp") -local url = require("socket.url") +local url = require("url") local ltn12 = require("ltn12") -- formats a number of seconds into human readable form diff --git a/src/auxiliar.h b/src/auxiliar.h index 70f4704..ff20b50 100644 --- a/src/auxiliar.h +++ b/src/auxiliar.h @@ -31,8 +31,8 @@ * RCS ID: $Id$ \*=========================================================================*/ -#include -#include +#include "lua.h" +#include "lauxlib.h" int aux_open(lua_State *L); void aux_newclass(lua_State *L, const char *classname, luaL_reg *func); diff --git a/src/buffer.c b/src/buffer.c index 1188fda..b69a9b8 100644 --- a/src/buffer.c +++ b/src/buffer.c @@ -4,8 +4,8 @@ * * RCS ID: $Id$ \*=========================================================================*/ -#include -#include +#include "lua.h" +#include "lauxlib.h" #include "buffer.h" diff --git a/src/buffer.h b/src/buffer.h index 8e5fb6c..f43e676 100644 --- a/src/buffer.h +++ b/src/buffer.h @@ -17,7 +17,7 @@ * * RCS ID: $Id$ \*=========================================================================*/ -#include +#include "lua.h" #include "io.h" #include "timeout.h" diff --git a/src/except.c b/src/except.c index dabaf19..fc4c038 100644 --- a/src/except.c +++ b/src/except.c @@ -5,8 +5,9 @@ * RCS ID: $Id$ \*=========================================================================*/ #include -#include -#include + +#include "lua.h" +#include "lauxlib.h" #include "except.h" diff --git a/src/except.h b/src/except.h index 2c57b27..03e417d 100644 --- a/src/except.h +++ b/src/except.h @@ -28,7 +28,7 @@ * RCS ID: $Id$ \*=========================================================================*/ -#include +#include "lua.h" int except_open(lua_State *L); diff --git a/src/inet.c b/src/inet.c index d102060..81ecd22 100644 --- a/src/inet.c +++ b/src/inet.c @@ -7,8 +7,8 @@ #include #include -#include -#include +#include "lua.h" +#include "lauxlib.h" #include "inet.h" diff --git a/src/inet.h b/src/inet.h index 4b3639a..da95e7e 100644 --- a/src/inet.h +++ b/src/inet.h @@ -16,7 +16,7 @@ * * RCS ID: $Id$ \*=========================================================================*/ -#include +#include "lua.h" #include "socket.h" #include "timeout.h" diff --git a/src/io.c b/src/io.c index fe0af76..317cfd4 100644 --- a/src/io.c +++ b/src/io.c @@ -27,7 +27,6 @@ const char *io_strerror(int err) { case IO_DONE: return NULL; case IO_CLOSED: return "closed"; case IO_TIMEOUT: return "timeout"; - case IO_CLIPPED: return "clipped"; default: return "unknown error"; } } diff --git a/src/io.h b/src/io.h index 4f9de57..bcaf416 100644 --- a/src/io.h +++ b/src/io.h @@ -15,7 +15,7 @@ * RCS ID: $Id$ \*=========================================================================*/ #include -#include +#include "lua.h" #include "timeout.h" @@ -24,8 +24,7 @@ enum { IO_DONE = 0, /* operation completed successfully */ IO_TIMEOUT = -1, /* operation timed out */ IO_CLOSED = -2, /* the connection has been closed */ - IO_CLIPPED = -3, /* maxium bytes count reached */ - IO_UNKNOWN = -4 + IO_UNKNOWN = -3 }; /* interface to error message function */ diff --git a/src/luasocket.c b/src/luasocket.c index 94ea05b..434b5b7 100644 --- a/src/luasocket.c +++ b/src/luasocket.c @@ -17,15 +17,17 @@ /*=========================================================================*\ * Standard include files \*=========================================================================*/ -#include -#include +#include "lua.h" +#include "lauxlib.h" + +#if !defined(LUA_VERSION_NUM) || (LUA_VERSION_NUM < 501) #include "compat-5.1.h" -#include "luasocket.h" +#endif /*=========================================================================*\ * LuaSocket includes \*=========================================================================*/ - +#include "luasocket.h" #include "auxiliar.h" #include "except.h" #include "timeout.h" diff --git a/src/luasocket.h b/src/luasocket.h index c7d09d8..0143fa7 100644 --- a/src/luasocket.h +++ b/src/luasocket.h @@ -8,10 +8,10 @@ * * RCS ID: $Id$ \*=========================================================================*/ -#include +#include "lua.h" /*-------------------------------------------------------------------------*\ -* Current luasocket version +* Current socket library version \*-------------------------------------------------------------------------*/ #define LUASOCKET_VERSION "LuaSocket 2.0" #define LUASOCKET_COPYRIGHT "Copyright (C) 2004-2005 Diego Nehab" diff --git a/src/makefile b/src/makefile index 7ef18bf..9a97dd3 100644 --- a/src/makefile +++ b/src/makefile @@ -11,6 +11,7 @@ include ../config # Modules belonging to socket-core # SOCKET_OBJS:= \ + $(COMPAT)/compat-5.1.o \ luasocket.o \ timeout.o \ buffer.o \ @@ -22,15 +23,15 @@ SOCKET_OBJS:= \ udp.o \ except.o \ select.o \ - $(COMPAT)/compat-5.1.o \ usocket.o #------ # Modules belonging mime-core # MIME_OBJS:=\ - mime.o \ - $(COMPAT)/compat-5.1.o + $(COMPAT)/compat-5.1.o \ + mime.o + #------ # Modules belonging unix (local domain sockets) diff --git a/src/mime.c b/src/mime.c index 4539e2c..4dfcae5 100644 --- a/src/mime.c +++ b/src/mime.c @@ -6,10 +6,13 @@ \*=========================================================================*/ #include -#include -#include +#include "lua.h" +#include "lauxlib.h" +#if !defined(LUA_VERSION_NUM) || (LUA_VERSION_NUM < 501) #include "compat-5.1.h" +#endif + #include "mime.h" /*=========================================================================*\ @@ -81,6 +84,10 @@ static UC b64unbase[256]; MIME_API int luaopen_mime_core(lua_State *L) { luaL_openlib(L, "mime", func, 0); + /* make version string available to scripts */ + lua_pushstring(L, "_VERSION"); + lua_pushstring(L, MIME_VERSION); + lua_rawset(L, -3); /* initialize lookup tables */ qpsetup(qpclass, qpunbase); b64setup(b64unbase); diff --git a/src/mime.h b/src/mime.h index eda0898..a56751c 100644 --- a/src/mime.h +++ b/src/mime.h @@ -1,7 +1,7 @@ #ifndef MIME_H #define MIME_H /*=========================================================================*\ -* MIME support functions +* Core MIME support * LuaSocket toolkit * * This module provides functions to implement transfer content encodings @@ -10,7 +10,14 @@ * * RCS ID: $Id$ \*=========================================================================*/ -#include +#include "lua.h" + +/*-------------------------------------------------------------------------*\ +* Current MIME library version +\*-------------------------------------------------------------------------*/ +#define MIME_VERSION "MIME 1.0" +#define MIME_COPYRIGHT "Copyright (C) 2004-2005 Diego Nehab" +#define MIME_AUTHORS "Diego Nehab" /*-------------------------------------------------------------------------*\ * This macro prefixes all exported API functions diff --git a/src/options.c b/src/options.c index c9e69f0..5236a3f 100644 --- a/src/options.c +++ b/src/options.c @@ -4,9 +4,10 @@ * * RCS ID: $Id$ \*=========================================================================*/ -#include #include +#include "lauxlib.h" + #include "auxiliar.h" #include "options.h" #include "inet.h" diff --git a/src/options.h b/src/options.h index d57bfaa..6ebf1f6 100644 --- a/src/options.h +++ b/src/options.h @@ -10,7 +10,7 @@ * RCS ID: $Id$ \*=========================================================================*/ -#include +#include "lua.h" #include "socket.h" /* option registry */ diff --git a/src/select.c b/src/select.c index e2cd91d..d28ade1 100644 --- a/src/select.c +++ b/src/select.c @@ -6,8 +6,8 @@ \*=========================================================================*/ #include -#include -#include +#include "lua.h" +#include "lauxlib.h" #include "socket.h" #include "timeout.h" diff --git a/src/socket.h b/src/socket.h index 4443bcc..ad12b1b 100644 --- a/src/socket.h +++ b/src/socket.h @@ -53,7 +53,6 @@ int sock_waitfd(p_sock ps, int sw, p_tm tm); int sock_select(int n, fd_set *rfds, fd_set *wfds, fd_set *efds, p_tm tm); int sock_connect(p_sock ps, SA *addr, socklen_t addr_len, p_tm tm); -int sock_connected(p_sock ps, p_tm tm); int sock_create(p_sock ps, int domain, int type, int protocol); int sock_bind(p_sock ps, SA *addr, socklen_t addr_len); int sock_listen(p_sock ps, int backlog); diff --git a/src/tcp.c b/src/tcp.c index c79ddd1..8b4c7e3 100644 --- a/src/tcp.c +++ b/src/tcp.c @@ -6,8 +6,8 @@ \*=========================================================================*/ #include -#include -#include +#include "lua.h" +#include "lauxlib.h" #include "auxiliar.h" #include "socket.h" diff --git a/src/tcp.h b/src/tcp.h index 708023e..fe4fd8c 100644 --- a/src/tcp.h +++ b/src/tcp.h @@ -16,7 +16,7 @@ * * RCS ID: $Id$ \*=========================================================================*/ -#include +#include "lua.h" #include "buffer.h" #include "timeout.h" diff --git a/src/timeout.c b/src/timeout.c index be68228..4f1d345 100644 --- a/src/timeout.c +++ b/src/timeout.c @@ -6,8 +6,8 @@ \*=========================================================================*/ #include -#include -#include +#include "lua.h" +#include "lauxlib.h" #include "auxiliar.h" #include "timeout.h" diff --git a/src/timeout.h b/src/timeout.h index d2f9be0..27e0a8a 100644 --- a/src/timeout.h +++ b/src/timeout.h @@ -6,7 +6,7 @@ * * RCS ID: $Id$ \*=========================================================================*/ -#include +#include "lua.h" /* timeout control structure */ typedef struct t_tm_ { diff --git a/src/udp.c b/src/udp.c index 094e137..4de7248 100644 --- a/src/udp.c +++ b/src/udp.c @@ -6,8 +6,8 @@ \*=========================================================================*/ #include -#include -#include +#include "lua.h" +#include "lauxlib.h" #include "auxiliar.h" #include "socket.h" diff --git a/src/udp.h b/src/udp.h index 520573d..3591998 100644 --- a/src/udp.h +++ b/src/udp.h @@ -14,7 +14,7 @@ * * RCS ID: $Id$ \*=========================================================================*/ -#include +#include "lua.h" #include "timeout.h" #include "socket.h" diff --git a/src/unix.c b/src/unix.c index c169268..217a600 100644 --- a/src/unix.c +++ b/src/unix.c @@ -6,8 +6,8 @@ \*=========================================================================*/ #include -#include -#include +#include "lua.h" +#include "lauxlib.h" #include "auxiliar.h" #include "socket.h" diff --git a/src/unix.h b/src/unix.h index aaaef3d..60b00fe 100644 --- a/src/unix.h +++ b/src/unix.h @@ -9,7 +9,7 @@ * * RCS ID: $Id$ \*=========================================================================*/ -#include +#include "lua.h" #include "buffer.h" #include "timeout.h" diff --git a/src/url.lua b/src/url.lua index bd39d98..5427cbc 100644 --- a/src/url.lua +++ b/src/url.lua @@ -14,6 +14,11 @@ local table = require("table") module("socket.url") getmetatable(_M).__index = nil +----------------------------------------------------------------------------- +-- Module version +----------------------------------------------------------------------------- +_VERSION = "URL 1.0" + ----------------------------------------------------------------------------- -- Encodes a string into its escaped hexadecimal representation -- Input diff --git a/src/usocket.c b/src/usocket.c index dcb40e6..f067fa1 100644 --- a/src/usocket.c +++ b/src/usocket.c @@ -169,15 +169,8 @@ int sock_connect(p_sock ps, SA *addr, socklen_t len, p_tm tm) { /* zero timeout case optimization */ if (tm_iszero(tm)) return IO_TIMEOUT; /* wait until we have the result of the connection attempt or timeout */ - return sock_connected(ps, tm); -} - -/*-------------------------------------------------------------------------*\ -* Checks if socket is connected, or return reason for failure -\*-------------------------------------------------------------------------*/ -int sock_connected(p_sock ps, p_tm tm) { - int err; - if ((err = sock_waitfd(ps, WAITFD_C, tm) == IO_CLOSED)) { + err = sock_waitfd(ps, WAITFD_C, tm); + if (err == IO_CLOSED) { if (recv(*ps, (char *) &err, 0, 0) == 0) return IO_DONE; else return errno; } else return err; diff --git a/src/wsocket.c b/src/wsocket.c index 0f6005f..b533eae 100644 --- a/src/wsocket.c +++ b/src/wsocket.c @@ -22,7 +22,8 @@ int sock_open(void) { WORD wVersionRequested = MAKEWORD(2, 0); int err = WSAStartup(wVersionRequested, &wsaData ); if (err != 0) return 0; - if (LOBYTE(wsaData.wVersion) != 2 || HIBYTE(wsaData.wVersion) != 0) { + if ((LOBYTE(wsaData.wVersion) != 2 || HIBYTE(wsaData.wVersion) != 0) && + (LOBYTE(wsaData.wVersion) != 1 || HIBYTE(wsaData.wVersion) != 1)) { WSACleanup(); return 0; } @@ -124,16 +125,8 @@ int sock_connect(p_sock ps, SA *addr, socklen_t len, p_tm tm) { /* zero timeout case optimization */ if (tm_iszero(tm)) return IO_TIMEOUT; /* we wait until something happens */ - return sock_connected(ps, tm); -} - -/*-------------------------------------------------------------------------*\ -* Check if socket is connected -\*-------------------------------------------------------------------------*/ -int sock_connected(p_sock ps, p_tm tm) { - int err; - /* give windows time to find out what is up (yes, disgusting) */ - if ((err = sock_waitfd(ps, WAITFD_C, tm)) == IO_CLOSED) { + err = sock_waitfd(ps, WAITFD_C, tm); + if (err == IO_CLOSED) { int len = sizeof(err); /* give windows time to set the error (yes, disgusting) */ Sleep(10); @@ -143,6 +136,7 @@ int sock_connected(p_sock ps, p_tm tm) { * "unknown error", but it's not really our fault */ return err > 0? err: IO_UNKNOWN; } else return err; + } /*-------------------------------------------------------------------------*\ diff --git a/test/testclnt.lua b/test/testclnt.lua index 83f925b..cf71c9f 100644 --- a/test/testclnt.lua +++ b/test/testclnt.lua @@ -382,7 +382,8 @@ function connect_timeout() assert(c, e) c:settimeout(0.1) local t = socket.gettime() - local r, e = c:connect("127.0.0.2", 80) + local r, e = c:connect("10.0.0.1", 81) +print(r, e) assert(not r, "should not connect") assert(socket.gettime() - t < 2, "took too long to give up.") c:close() diff --git a/test/urltest.lua b/test/urltest.lua index ae54db9..8dc0c14 100644 --- a/test/urltest.lua +++ b/test/urltest.lua @@ -71,7 +71,7 @@ local check_parse_url = function(gaba) local url = gaba.url gaba.url = nil local parsed = socket.url.parse(url) - for i, v in gaba do + for i, v in pairs(gaba) do if v ~= parsed[i] then io.write("parse: In test for '", url, "' expected ", i, " = '", v, "' but got '", tostring(parsed[i]), "'\n") @@ -79,7 +79,7 @@ local check_parse_url = function(gaba) exit() end end - for i, v in parsed do + for i, v in pairs(parsed) do if v ~= gaba[i] then io.write("parse: In test for '", url, "' expected ", i, " = '", tostring(gaba[i]), "' but got '", v, "'\n")