Almost ready to release.

This commit is contained in:
Diego Nehab 2005-09-29 06:11:42 +00:00
parent 773e35ced3
commit a32c6d9140
38 changed files with 115 additions and 383 deletions

12
TODO
View File

@ -1,3 +1,7 @@
get rid of setmetatable(, nil) since packages don't need this anymore in
5.1
new instalation scheme??? new instalation scheme???
test empty socket.select no windows. 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? 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) 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 adicionar exemplos de expansão: pipe, local, named pipe
testar os options! testar os options!
- Thread-safe - Thread-safe
@ -30,6 +27,7 @@ testar os options!
- proteger ou atomizar o conjunto (timedout, receive), (timedout, send) - proteger ou atomizar o conjunto (timedout, receive), (timedout, send)
- inet_ntoa também é uma merda. - inet_ntoa também é uma merda.
* protect doesn't catch errors by error and assert
* BUG NO SET DO TINYIRC!!! SINISTRO. * BUG NO SET DO TINYIRC!!! SINISTRO.
* _VERSION, _DEBUG, etc. * _VERSION, _DEBUG, etc.
* talk about new create field in HTTP, FTP and SMTP * talk about new create field in HTTP, FTP and SMTP

2
config
View File

@ -27,8 +27,6 @@ COMPAT=compat-5.1r4
# Top of your Lua installation # Top of your Lua installation
# Relative paths will be inside the src tree # 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_LUA=share
INSTALL_TOP_LIB=lib INSTALL_TOP_LIB=lib

View File

@ -63,24 +63,28 @@ the package scheme will likely already have been answered.
<h3>Directory structure</h3> <h3>Directory structure</h3>
<p> The standard distribution reserves a directory to be the root of <p> On Unix systems, the standard distribution uses two base
the libraries installed directories, one for system dependent files, and another for system
on a given system. Let's call this directory <tt>&lt;ROOT&gt;</tt>. independent files. Let's call these directories <tt>&lt;LIB&gt;</tt>
On my system, this is the <tt>/usr/local/share/lua/5.0</tt> directory. and <tt>&lt;SHARE&gt;</tt>, respectively.
Here is the standard LuaSocket distribution directory structure:</p> For instance, in my laptop, I use '<tt>/usr/local/lib/lua/5.0</tt>' for
<tt>&lt;LIB&gt;</tt> and '<tt>/usr/local/share/lua/5.0</tt>' for
<tt>&lt;SHARE&gt;</tt>. On Windows, only one directory is used, say
'<tt>c:\program files\lua\5.0</tt>'. Here is the standard LuaSocket
distribution directory structure:</p>
<pre class=example> <pre class=example>
&lt;ROOT&gt;/compat-5.1.lua &lt;SHARE&gt;/compat-5.1.lua
&lt;ROOT&gt;/ltn12.lua &lt;SHARE&gt;/ltn12.lua
&lt;ROOT&gt;/mime/init.lua &lt;SHARE&gt;/mime/init.lua
&lt;ROOT&gt;/mime/core.dll &lt;LIB&gt;/mime/core.dll
&lt;ROOT&gt;/socket/init.lua &lt;SHARE&gt;/socket/init.lua
&lt;ROOT&gt;/socket/core.dll &lt;LIB&gt;/socket/core.dll
&lt;ROOT&gt;/socket/http.lua &lt;SHARE&gt;/socket/http.lua
&lt;ROOT&gt;/socket/tp.lua &lt;SHARE&gt;/socket/tp.lua
&lt;ROOT&gt;/socket/ftp.lua &lt;SHARE&gt;/socket/ftp.lua
&lt;ROOT&gt;/socket/smtp.lua &lt;SHARE&gt;/socket/smtp.lua
&lt;ROOT&gt;/socket/url.lua &lt;SHARE&gt;/socket/url.lua
</pre> </pre>
<p> Naturally, on Unix systems, <tt>core.dll</tt> <p> Naturally, on Unix systems, <tt>core.dll</tt>
@ -91,7 +95,7 @@ environment variables need to be set. The first environment variable tells
the interpreter to load the <tt>compat-5.1.lua</tt> module at startup: </p> the interpreter to load the <tt>compat-5.1.lua</tt> module at startup: </p>
<pre class=example> <pre class=example>
LUA_INIT=@&lt;ROOT&gt;/compat-5.1.lua LUA_INIT=@&lt;SHARE&gt;/compat-5.1.lua
</pre> </pre>
<p> <p>
@ -101,13 +105,12 @@ directories and with the appropriate filename extensions.
</p> </p>
<pre class=example> <pre class=example>
LUA_PATH=&lt;ROOT&gt;/?.lua;?.lua LUA_PATH=&lt;SHARE&gt;/?.lua;?.lua
LUA_CPATH=&lt;ROOT&gt;/?.dll;?.dll LUA_CPATH=&lt;LIB&gt;/?.dll;?.dll
</pre> </pre>
<p> Again, naturally, in Unix the shared library extension would be <p> Again, naturally, on Unix systmems the shared library extension would be
<tt>.so</tt> instead of <tt>.dll</tt> and on Mac OS X it would be <tt>.so</tt> instead of <tt>.dll</tt></p>
<tt>.dylib</tt></p>
<h3>Using LuaSocket</h3> <h3>Using LuaSocket</h3>
@ -118,8 +121,8 @@ it should be easy to use LuaSocket. Just fire the interpreter and use the
<pre class=example> <pre class=example>
Lua 5.0.2 Copyright (C) 1994-2004 Tecgraf, PUC-Rio Lua 5.0.2 Copyright (C) 1994-2004 Tecgraf, PUC-Rio
&gt; socket = require("socket") &gt; socket = require("socket")
&gt; print(socket.VERSION) &gt; print(socket._VERSION)
--&gt; LuaSocket 2.0 (beta3) --&gt; LuaSocket 2.0
</pre> </pre>
<p> Each module loads their dependencies automatically, so you only need to <p> Each module loads their dependencies automatically, so you only need to
@ -128,7 +131,8 @@ load the modues you directly depend upon: <p>
<pre class=example> <pre class=example>
Lua 5.0.2 Copyright (C) 1994-2004 Tecgraf, PUC-Rio Lua 5.0.2 Copyright (C) 1994-2004 Tecgraf, PUC-Rio
&gt; http = require("socket.http") &gt; http = require("socket.http")
&gt; print(http.request("http://www.tecgraf.puc-rio.br/luasocket")) &gt;
print(http.request("http://www.cs.princeton.edu/~diego/professional/luasocket"))
--&gt; homepage gets dumped to terminal --&gt; homepage gets dumped to terminal
</pre> </pre>

View File

@ -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 {<url>}")
exit()
end
for _, address in ipairs(arg) do
checklinks(url.absolute("file:", address))
end
dispatch()

View File

@ -5,7 +5,7 @@
-- Author: Diego Nehab -- Author: Diego Nehab
-- RCS ID: $$ -- RCS ID: $$
----------------------------------------------------------------------------- -----------------------------------------------------------------------------
local url = require("socket.url") local url = require("url")
local dispatch = require("dispatch") local dispatch = require("dispatch")
local http = require("socket.http") local http = require("socket.http")
dispatch.TIMEOUT = 10 dispatch.TIMEOUT = 10

View File

@ -7,9 +7,9 @@ function load(s)
print(s .. ":\t " .. (b-a) .. "k") print(s .. ":\t " .. (b-a) .. "k")
end end
load("socket") load("url")
load("socket.url")
load("ltn12") load("ltn12")
load("socket")
load("mime") load("mime")
load("socket.tp") load("socket.tp")
load("socket.smtp") load("socket.smtp")

View File

@ -7,7 +7,7 @@
local socket = require("socket") local socket = require("socket")
local http = require("socket.http") local http = require("socket.http")
local ftp = require("socket.ftp") local ftp = require("socket.ftp")
local url = require("socket.url") local url = require("url")
local ltn12 = require("ltn12") local ltn12 = require("ltn12")
-- formats a number of seconds into human readable form -- formats a number of seconds into human readable form

View File

@ -31,8 +31,8 @@
* RCS ID: $Id$ * RCS ID: $Id$
\*=========================================================================*/ \*=========================================================================*/
#include <lua.h> #include "lua.h"
#include <lauxlib.h> #include "lauxlib.h"
int aux_open(lua_State *L); int aux_open(lua_State *L);
void aux_newclass(lua_State *L, const char *classname, luaL_reg *func); void aux_newclass(lua_State *L, const char *classname, luaL_reg *func);

View File

@ -4,8 +4,8 @@
* *
* RCS ID: $Id$ * RCS ID: $Id$
\*=========================================================================*/ \*=========================================================================*/
#include <lua.h> #include "lua.h"
#include <lauxlib.h> #include "lauxlib.h"
#include "buffer.h" #include "buffer.h"

View File

@ -17,7 +17,7 @@
* *
* RCS ID: $Id$ * RCS ID: $Id$
\*=========================================================================*/ \*=========================================================================*/
#include <lua.h> #include "lua.h"
#include "io.h" #include "io.h"
#include "timeout.h" #include "timeout.h"

View File

@ -5,8 +5,9 @@
* RCS ID: $Id$ * RCS ID: $Id$
\*=========================================================================*/ \*=========================================================================*/
#include <stdio.h> #include <stdio.h>
#include <lua.h>
#include <lauxlib.h> #include "lua.h"
#include "lauxlib.h"
#include "except.h" #include "except.h"

View File

@ -28,7 +28,7 @@
* RCS ID: $Id$ * RCS ID: $Id$
\*=========================================================================*/ \*=========================================================================*/
#include <lua.h> #include "lua.h"
int except_open(lua_State *L); int except_open(lua_State *L);

View File

@ -7,8 +7,8 @@
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <lua.h> #include "lua.h"
#include <lauxlib.h> #include "lauxlib.h"
#include "inet.h" #include "inet.h"

View File

@ -16,7 +16,7 @@
* *
* RCS ID: $Id$ * RCS ID: $Id$
\*=========================================================================*/ \*=========================================================================*/
#include <lua.h> #include "lua.h"
#include "socket.h" #include "socket.h"
#include "timeout.h" #include "timeout.h"

View File

@ -27,7 +27,6 @@ const char *io_strerror(int err) {
case IO_DONE: return NULL; case IO_DONE: return NULL;
case IO_CLOSED: return "closed"; case IO_CLOSED: return "closed";
case IO_TIMEOUT: return "timeout"; case IO_TIMEOUT: return "timeout";
case IO_CLIPPED: return "clipped";
default: return "unknown error"; default: return "unknown error";
} }
} }

View File

@ -15,7 +15,7 @@
* RCS ID: $Id$ * RCS ID: $Id$
\*=========================================================================*/ \*=========================================================================*/
#include <stdio.h> #include <stdio.h>
#include <lua.h> #include "lua.h"
#include "timeout.h" #include "timeout.h"
@ -24,8 +24,7 @@ enum {
IO_DONE = 0, /* operation completed successfully */ IO_DONE = 0, /* operation completed successfully */
IO_TIMEOUT = -1, /* operation timed out */ IO_TIMEOUT = -1, /* operation timed out */
IO_CLOSED = -2, /* the connection has been closed */ IO_CLOSED = -2, /* the connection has been closed */
IO_CLIPPED = -3, /* maxium bytes count reached */ IO_UNKNOWN = -3
IO_UNKNOWN = -4
}; };
/* interface to error message function */ /* interface to error message function */

View File

@ -17,15 +17,17 @@
/*=========================================================================*\ /*=========================================================================*\
* Standard include files * Standard include files
\*=========================================================================*/ \*=========================================================================*/
#include <lua.h> #include "lua.h"
#include <lauxlib.h> #include "lauxlib.h"
#if !defined(LUA_VERSION_NUM) || (LUA_VERSION_NUM < 501)
#include "compat-5.1.h" #include "compat-5.1.h"
#include "luasocket.h" #endif
/*=========================================================================*\ /*=========================================================================*\
* LuaSocket includes * LuaSocket includes
\*=========================================================================*/ \*=========================================================================*/
#include "luasocket.h"
#include "auxiliar.h" #include "auxiliar.h"
#include "except.h" #include "except.h"
#include "timeout.h" #include "timeout.h"

View File

@ -8,10 +8,10 @@
* *
* RCS ID: $Id$ * RCS ID: $Id$
\*=========================================================================*/ \*=========================================================================*/
#include <lua.h> #include "lua.h"
/*-------------------------------------------------------------------------*\ /*-------------------------------------------------------------------------*\
* Current luasocket version * Current socket library version
\*-------------------------------------------------------------------------*/ \*-------------------------------------------------------------------------*/
#define LUASOCKET_VERSION "LuaSocket 2.0" #define LUASOCKET_VERSION "LuaSocket 2.0"
#define LUASOCKET_COPYRIGHT "Copyright (C) 2004-2005 Diego Nehab" #define LUASOCKET_COPYRIGHT "Copyright (C) 2004-2005 Diego Nehab"

View File

@ -11,6 +11,7 @@ include ../config
# Modules belonging to socket-core # Modules belonging to socket-core
# #
SOCKET_OBJS:= \ SOCKET_OBJS:= \
$(COMPAT)/compat-5.1.o \
luasocket.o \ luasocket.o \
timeout.o \ timeout.o \
buffer.o \ buffer.o \
@ -22,15 +23,15 @@ SOCKET_OBJS:= \
udp.o \ udp.o \
except.o \ except.o \
select.o \ select.o \
$(COMPAT)/compat-5.1.o \
usocket.o usocket.o
#------ #------
# Modules belonging mime-core # Modules belonging mime-core
# #
MIME_OBJS:=\ MIME_OBJS:=\
mime.o \ $(COMPAT)/compat-5.1.o \
$(COMPAT)/compat-5.1.o mime.o
#------ #------
# Modules belonging unix (local domain sockets) # Modules belonging unix (local domain sockets)

View File

@ -6,10 +6,13 @@
\*=========================================================================*/ \*=========================================================================*/
#include <string.h> #include <string.h>
#include <lua.h> #include "lua.h"
#include <lauxlib.h> #include "lauxlib.h"
#if !defined(LUA_VERSION_NUM) || (LUA_VERSION_NUM < 501)
#include "compat-5.1.h" #include "compat-5.1.h"
#endif
#include "mime.h" #include "mime.h"
/*=========================================================================*\ /*=========================================================================*\
@ -81,6 +84,10 @@ static UC b64unbase[256];
MIME_API int luaopen_mime_core(lua_State *L) MIME_API int luaopen_mime_core(lua_State *L)
{ {
luaL_openlib(L, "mime", func, 0); 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 */ /* initialize lookup tables */
qpsetup(qpclass, qpunbase); qpsetup(qpclass, qpunbase);
b64setup(b64unbase); b64setup(b64unbase);

View File

@ -1,7 +1,7 @@
#ifndef MIME_H #ifndef MIME_H
#define MIME_H #define MIME_H
/*=========================================================================*\ /*=========================================================================*\
* MIME support functions * Core MIME support
* LuaSocket toolkit * LuaSocket toolkit
* *
* This module provides functions to implement transfer content encodings * This module provides functions to implement transfer content encodings
@ -10,7 +10,14 @@
* *
* RCS ID: $Id$ * RCS ID: $Id$
\*=========================================================================*/ \*=========================================================================*/
#include <lua.h> #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 * This macro prefixes all exported API functions

View File

@ -4,9 +4,10 @@
* *
* RCS ID: $Id$ * RCS ID: $Id$
\*=========================================================================*/ \*=========================================================================*/
#include <lauxlib.h>
#include <string.h> #include <string.h>
#include "lauxlib.h"
#include "auxiliar.h" #include "auxiliar.h"
#include "options.h" #include "options.h"
#include "inet.h" #include "inet.h"

View File

@ -10,7 +10,7 @@
* RCS ID: $Id$ * RCS ID: $Id$
\*=========================================================================*/ \*=========================================================================*/
#include <lua.h> #include "lua.h"
#include "socket.h" #include "socket.h"
/* option registry */ /* option registry */

View File

@ -6,8 +6,8 @@
\*=========================================================================*/ \*=========================================================================*/
#include <string.h> #include <string.h>
#include <lua.h> #include "lua.h"
#include <lauxlib.h> #include "lauxlib.h"
#include "socket.h" #include "socket.h"
#include "timeout.h" #include "timeout.h"

View File

@ -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_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_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_create(p_sock ps, int domain, int type, int protocol);
int sock_bind(p_sock ps, SA *addr, socklen_t addr_len); int sock_bind(p_sock ps, SA *addr, socklen_t addr_len);
int sock_listen(p_sock ps, int backlog); int sock_listen(p_sock ps, int backlog);

View File

@ -6,8 +6,8 @@
\*=========================================================================*/ \*=========================================================================*/
#include <string.h> #include <string.h>
#include <lua.h> #include "lua.h"
#include <lauxlib.h> #include "lauxlib.h"
#include "auxiliar.h" #include "auxiliar.h"
#include "socket.h" #include "socket.h"

View File

@ -16,7 +16,7 @@
* *
* RCS ID: $Id$ * RCS ID: $Id$
\*=========================================================================*/ \*=========================================================================*/
#include <lua.h> #include "lua.h"
#include "buffer.h" #include "buffer.h"
#include "timeout.h" #include "timeout.h"

View File

@ -6,8 +6,8 @@
\*=========================================================================*/ \*=========================================================================*/
#include <stdio.h> #include <stdio.h>
#include <lua.h> #include "lua.h"
#include <lauxlib.h> #include "lauxlib.h"
#include "auxiliar.h" #include "auxiliar.h"
#include "timeout.h" #include "timeout.h"

View File

@ -6,7 +6,7 @@
* *
* RCS ID: $Id$ * RCS ID: $Id$
\*=========================================================================*/ \*=========================================================================*/
#include <lua.h> #include "lua.h"
/* timeout control structure */ /* timeout control structure */
typedef struct t_tm_ { typedef struct t_tm_ {

View File

@ -6,8 +6,8 @@
\*=========================================================================*/ \*=========================================================================*/
#include <string.h> #include <string.h>
#include <lua.h> #include "lua.h"
#include <lauxlib.h> #include "lauxlib.h"
#include "auxiliar.h" #include "auxiliar.h"
#include "socket.h" #include "socket.h"

View File

@ -14,7 +14,7 @@
* *
* RCS ID: $Id$ * RCS ID: $Id$
\*=========================================================================*/ \*=========================================================================*/
#include <lua.h> #include "lua.h"
#include "timeout.h" #include "timeout.h"
#include "socket.h" #include "socket.h"

View File

@ -6,8 +6,8 @@
\*=========================================================================*/ \*=========================================================================*/
#include <string.h> #include <string.h>
#include <lua.h> #include "lua.h"
#include <lauxlib.h> #include "lauxlib.h"
#include "auxiliar.h" #include "auxiliar.h"
#include "socket.h" #include "socket.h"

View File

@ -9,7 +9,7 @@
* *
* RCS ID: $Id$ * RCS ID: $Id$
\*=========================================================================*/ \*=========================================================================*/
#include <lua.h> #include "lua.h"
#include "buffer.h" #include "buffer.h"
#include "timeout.h" #include "timeout.h"

View File

@ -14,6 +14,11 @@ local table = require("table")
module("socket.url") module("socket.url")
getmetatable(_M).__index = nil getmetatable(_M).__index = nil
-----------------------------------------------------------------------------
-- Module version
-----------------------------------------------------------------------------
_VERSION = "URL 1.0"
----------------------------------------------------------------------------- -----------------------------------------------------------------------------
-- Encodes a string into its escaped hexadecimal representation -- Encodes a string into its escaped hexadecimal representation
-- Input -- Input

View File

@ -169,15 +169,8 @@ int sock_connect(p_sock ps, SA *addr, socklen_t len, p_tm tm) {
/* zero timeout case optimization */ /* zero timeout case optimization */
if (tm_iszero(tm)) return IO_TIMEOUT; if (tm_iszero(tm)) return IO_TIMEOUT;
/* wait until we have the result of the connection attempt or timeout */ /* wait until we have the result of the connection attempt or timeout */
return sock_connected(ps, tm); err = sock_waitfd(ps, WAITFD_C, tm);
} if (err == IO_CLOSED) {
/*-------------------------------------------------------------------------*\
* 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)) {
if (recv(*ps, (char *) &err, 0, 0) == 0) return IO_DONE; if (recv(*ps, (char *) &err, 0, 0) == 0) return IO_DONE;
else return errno; else return errno;
} else return err; } else return err;

View File

@ -22,7 +22,8 @@ int sock_open(void) {
WORD wVersionRequested = MAKEWORD(2, 0); WORD wVersionRequested = MAKEWORD(2, 0);
int err = WSAStartup(wVersionRequested, &wsaData ); int err = WSAStartup(wVersionRequested, &wsaData );
if (err != 0) return 0; 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(); WSACleanup();
return 0; return 0;
} }
@ -124,16 +125,8 @@ int sock_connect(p_sock ps, SA *addr, socklen_t len, p_tm tm) {
/* zero timeout case optimization */ /* zero timeout case optimization */
if (tm_iszero(tm)) return IO_TIMEOUT; if (tm_iszero(tm)) return IO_TIMEOUT;
/* we wait until something happens */ /* we wait until something happens */
return sock_connected(ps, tm); err = sock_waitfd(ps, WAITFD_C, tm);
} if (err == IO_CLOSED) {
/*-------------------------------------------------------------------------*\
* 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) {
int len = sizeof(err); int len = sizeof(err);
/* give windows time to set the error (yes, disgusting) */ /* give windows time to set the error (yes, disgusting) */
Sleep(10); Sleep(10);
@ -143,6 +136,7 @@ int sock_connected(p_sock ps, p_tm tm) {
* "unknown error", but it's not really our fault */ * "unknown error", but it's not really our fault */
return err > 0? err: IO_UNKNOWN; return err > 0? err: IO_UNKNOWN;
} else return err; } else return err;
} }
/*-------------------------------------------------------------------------*\ /*-------------------------------------------------------------------------*\

View File

@ -382,7 +382,8 @@ function connect_timeout()
assert(c, e) assert(c, e)
c:settimeout(0.1) c:settimeout(0.1)
local t = socket.gettime() 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(not r, "should not connect")
assert(socket.gettime() - t < 2, "took too long to give up.") assert(socket.gettime() - t < 2, "took too long to give up.")
c:close() c:close()

View File

@ -71,7 +71,7 @@ local check_parse_url = function(gaba)
local url = gaba.url local url = gaba.url
gaba.url = nil gaba.url = nil
local parsed = socket.url.parse(url) local parsed = socket.url.parse(url)
for i, v in gaba do for i, v in pairs(gaba) do
if v ~= parsed[i] then if v ~= parsed[i] then
io.write("parse: In test for '", url, "' expected ", i, " = '", io.write("parse: In test for '", url, "' expected ", i, " = '",
v, "' but got '", tostring(parsed[i]), "'\n") v, "' but got '", tostring(parsed[i]), "'\n")
@ -79,7 +79,7 @@ local check_parse_url = function(gaba)
exit() exit()
end end
end end
for i, v in parsed do for i, v in pairs(parsed) do
if v ~= gaba[i] then if v ~= gaba[i] then
io.write("parse: In test for '", url, "' expected ", i, " = '", io.write("parse: In test for '", url, "' expected ", i, " = '",
tostring(gaba[i]), "' but got '", v, "'\n") tostring(gaba[i]), "' but got '", v, "'\n")