Closer to release...

This commit is contained in:
Diego Nehab 2003-03-28 21:08:50 +00:00
parent 307603b24d
commit f18d1b7cd0
31 changed files with 163 additions and 77 deletions

1
TODO
View File

@ -1,4 +1,5 @@
- Inicializaccao das classes pode falhar?
- Ajeitar melhor a hierarquia de classes. Ajeitar o file...
* Como mostrar um erro em lua_socketlibopen()...
* O location do "redirect" pode ser relativo ao servidor atual (não pode,

View File

@ -1,3 +1,7 @@
-----------------------------------------------------------------------------
-- Little program that checks links in HTML files
-- LuaSocket 1.5 sample files.
-----------------------------------------------------------------------------
socket.http.TIMEOUT = 10
cache = {}
@ -14,7 +18,7 @@ end
function getstatus(url)
local parsed = socket.url.parse(url, { scheme = "file" })
if cache[url] then return cache[url].res end
if cache[url] then return cache[url] end
local res
if parsed.scheme == "http" then
local request = { url = url }
@ -34,7 +38,7 @@ function getstatus(url)
res = nil
else res = error end
else res = string.format("unhandled scheme '%s'", parsed.scheme) end
cache[url] = { res = res }
cache[url] = res
return res
end

View File

@ -1,12 +1,16 @@
-----------------------------------------------------------------------------
-- Little program to download DICT word definitions
-- LuaSocket 1.5 sample files
-----------------------------------------------------------------------------
function get_status(sock, valid)
local line, err = sock:receive()
local code, par
if not line then sock:close() return err end
_, _, code = strfind(line, "^(%d%d%d)")
_, _, code = string.find(line, "^(%d%d%d)")
code = tonumber(code)
if code ~= valid then return code end
if code == 150 then
_,_,_, par = strfind(line, "^(%d%d%d) (%d*)")
_,_,_, par = string.find(line, "^(%d%d%d) (%d*)")
par = tonumber(par)
end
return nil, par
@ -24,7 +28,7 @@ function get_def(sock)
end
function dict_open()
local sock, err = connect("dict.org", 2628)
local sock, err = socket.connect("dict.org", 2628)
if not sock then return nil, err end
sock:timeout(10)
local code, par = get_status(sock, 220)
@ -48,7 +52,7 @@ function dict_define(sock, word, dict)
end
code, par = get_status(sock, 250)
if code then return nil, code end
return gsub(defs, "%s%s$", "")
return string.gsub(defs, "%s%s$", "")
end
function dict_close(sock)
@ -65,3 +69,10 @@ function dict_get(word, dict)
dict_close(sock)
return defs, err
end
if arg and arg[1] then
defs, err = dict_get(arg[1], arg[2])
print(defs or err)
else
io.write("Usage:\n luasocket dict.lua <word> [<dictionary>]\n")
end

View File

@ -1,3 +1,7 @@
-----------------------------------------------------------------------------
-- Little program to download files from URLs
-- LuaSocket 1.5 sample files
-----------------------------------------------------------------------------
-- formats a number of seconds into human readable form
function nicetime(s)
local l = "s"
@ -63,15 +67,15 @@ function receive2disk(file, size)
size = size
}
local receive_cb = function(chunk, err)
local dt = socket._time() - %aux.start -- elapsed time since start
local dt = socket._time() - aux.start -- elapsed time since start
if not chunk or chunk == "" then
io.write("\n")
aux.file:close()
return
end
aux.file:write(chunk)
aux.got = aux.got + string.len(chunk) -- total bytes received
if dt < 0.1 then return 1 end -- not enough time for estimate
aux.got = aux.got + string.len(chunk) -- total bytes received
if dt < 0.1 then return 1 end -- not enough time for estimate
io.write("\r", gauge(aux.got, dt, aux.size))
return 1
end
@ -122,7 +126,7 @@ function getschemeandname(url, name)
return parsed.scheme, name
end
-- gets a file either by http or url, saving as name
-- gets a file either by http or ftp, saving as <name>
function get(url, name)
local scheme
scheme, name = getschemeandname(url, name)

View File

@ -4,11 +4,11 @@ if arg then
host = arg[1] or host
port = arg[2] or port
end
host = toip(host)
udp = udpsocket()
host = socket.toip(host)
udp = socket.udp()
print("Using host '" ..host.. "' and port " ..port.. "...")
err = udp:sendto("anything", host, port)
if err then print(err) exit() end
dgram, err = udp:receive()
if not dgram then print(err) exit() end
write(dgram)
io.write(dgram)

View File

@ -4,18 +4,18 @@ if arg then
host = arg[1] or host
port = arg[2] or port
end
host = toip(host)
udp, err = udpsocket()
host = socket.toip(host)
udp, err = socket.udp()
if not udp then print(err) exit() end
err = udp:setpeername(host, port)
if err then print(err) exit() end
print("Using host '" ..host.. "' and port " .. port .. "...")
while 1 do
line = read()
if not line then exit() end
line = io.read()
if not line then os.exit() end
err = udp:send(line)
if err then print(err) exit() end
if err then print(err) os.exit() end
dgram, err = udp:receive()
if not dgram then print(err) exit() end
if not dgram then print(err) os.exit() end
print(dgram)
end

View File

@ -5,10 +5,10 @@ if arg then
port = arg[2] or port
end
print("Binding to host '" ..host.. "' and port " ..port.. "...")
udp, err = udpsocket()
if not udp then print(err) exit() end
udp, err = socket.udp()
if not udp then print(err) os.exit() end
err = udp:setsockname(host, port)
if err then print(err) exit() end
if err then print(err) os.exit() end
udp:timeout(5)
ip, port = udp:getsockname()
print("Waiting packets on " .. ip .. ":" .. port .. "...")

View File

@ -1,3 +1,7 @@
-----------------------------------------------------------------------------
-- Little program to dump lines received at a given port
-- LuaSocket 1.5 sample files
-----------------------------------------------------------------------------
host = host or "*"
port = port or 8080
if arg then
@ -5,7 +9,7 @@ if arg then
port = arg[2] or port
end
print("Binding to host '" ..host.. "' and port " ..port.. "...")
s, e = bind(host, port)
s, e = socket.bind(host, port)
if not s then
print(e)
exit()

View File

@ -3,6 +3,8 @@
* Lua methods:
* send: unbuffered send using C base_send
* receive: buffered read using C base_receive
*
* RCS ID: $Id$
\*=========================================================================*/
#include <lua.h>
#include <lauxlib.h>

View File

@ -1,5 +1,6 @@
/*=========================================================================*\
* Buffered input/output routines
*
* RCS ID: $Id$
\*=========================================================================*/
#ifndef BUF_H_
@ -16,7 +17,7 @@
\*-------------------------------------------------------------------------*/
typedef struct t_buf_tag {
size_t buf_first, buf_last;
uchar buf_data[BUF_SIZE];
char buf_data[BUF_SIZE];
p_base buf_base;
} t_buf;
typedef t_buf *p_buf;

View File

@ -2,7 +2,6 @@
-- FTP support for the Lua language
-- LuaSocket 1.5 toolkit.
-- Author: Diego Nehab
-- Date: 26/12/2000
-- Conforming to: RFC 959, LTN7
-- RCS ID: $Id$
-----------------------------------------------------------------------------

View File

@ -2,7 +2,6 @@
-- HTTP/1.1 client support for the Lua language.
-- LuaSocket 1.5 toolkit.
-- Author: Diego Nehab
-- Date: 26/12/2000
-- Conforming to: RFC 2616, LTN7
-- RCS ID: $Id$
-----------------------------------------------------------------------------

View File

@ -1,11 +1,14 @@
/*=========================================================================*\
* Internet domain class
* Internet domain class: inherits from the Socket class, and implement
* a few methods shared by all internet related objects
* Lua methods:
* getpeername: gets socket peer ip address and port
* getsockname: gets local socket ip address and port
* Global Lua fuctions:
* toip: gets resolver info on host name
* tohostname: gets resolver info on dotted-quad
*
* RCS ID: $Id$
\*=========================================================================*/
#include <string.h>
@ -145,7 +148,7 @@ static int inet_lua_getpeername(lua_State *L)
{
p_sock sock = (p_sock) lua_touserdata(L, 1);
struct sockaddr_in peer;
int peer_len = sizeof(peer);
size_t peer_len = sizeof(peer);
if (getpeername(sock->fd, (SA *) &peer, &peer_len) < 0) {
lua_pushnil(L);
return 1;
@ -167,7 +170,7 @@ static int inet_lua_getsockname(lua_State *L)
{
p_sock sock = (p_sock) lua_touserdata(L, 1);
struct sockaddr_in local;
int local_len = sizeof(local);
size_t local_len = sizeof(local);
if (getsockname(sock->fd, (SA *) &local, &local_len) < 0) {
lua_pushnil(L);
return 1;

View File

@ -1,5 +1,7 @@
/*=========================================================================*\
* Internet domain class
* Internet domain class: inherits from the Socket class, and implement
* a few methods shared by all internet related objects
*
* RCS ID: $Id$
\*=========================================================================*/
#ifndef INET_H_

View File

@ -63,6 +63,13 @@ LUASOCKET_API int lua_socketlibopen(lua_State *L)
lua_dofile(L, "http.lua");
lua_dofile(L, "smtp.lua");
lua_dofile(L, "ftp.lua");
#else
#include "concat.loh"
#include "code.loh"
#include "url.loh"
#include "http.loh"
#include "smtp.loh"
#include "ftp.loh"
#endif
return 0;
}

View File

@ -1,6 +1,13 @@
/*=========================================================================*\
* Select implementation
* Global Lua fuctions:
* select: waits until socket ready
* RCS ID: $Id$
\*=========================================================================*/
#include <lua.h>
#include <lauxlib.h>
#include "luasocket.h"
#include "lspriv.h"
#include "lsselect.h"
#include "lsfd.h"
@ -33,10 +40,17 @@ void select_open(lua_State *L)
{
/* push select auxiliar lua function and register
* select_lua_select with it as an upvalue */
luaL_loadfile(L, "lsselect.lua");
lua_call(L, 0, 1);
#ifdef LUASOCKET_DEBUG
lua_dofile(L, "lsselect.lua");
#else
#include "lsselect.loh"
#endif
lua_getglobal(L, LUASOCKET_LIBNAME);
lua_pushstring(L, "_select");
lua_gettable(L, -2);
lua_pushcclosure(L, select_lua_select, 1);
priv_newglobal(L, "select");
lua_pop(L, 1);
/* create luasocket(select) table */
lua_pushstring(L, "luasocket(select)");
lua_newtable(L);

View File

@ -1,3 +1,7 @@
/*=========================================================================*\
* Select implementation
* RCS ID: $Id$
\*=========================================================================*/
#ifndef SLCT_H_
#define SLCT_H_

View File

@ -2,7 +2,6 @@
-- SMTP support for the Lua language.
-- LuaSocket 1.5 toolkit
-- Author: Diego Nehab
-- Date: 26/12/2000
-- Conforming to: RFC 821, LTN7
-- RCS ID: $Id$
-----------------------------------------------------------------------------

View File

@ -1,3 +1,9 @@
/*=========================================================================*\
* Socket class: inherits from the File Descriptor class and is here just
* for extensibility in the future
*
* RCS ID: $id$
\*=========================================================================*/
#ifndef SOCK_H_
#define SOCK_H_

View File

@ -1,5 +1,10 @@
/*=========================================================================*\
* Timeout management functions
* Global Lua functions:
* _sleep: (debug mode only)
* _time: (debug mode only)
*
* RCS ID: $Id$
\*=========================================================================*/
#include <lua.h>
#include <lauxlib.h>
@ -20,10 +25,8 @@
/*=========================================================================*\
* Internal function prototypes
\*=========================================================================*/
#ifdef LUASOCKET_DEBUG
static int tm_lua_time(lua_State *L);
static int tm_lua_sleep(lua_State *L);
#endif
/*=========================================================================*\
* Exported functions.
@ -123,12 +126,10 @@ int tm_gettime(void)
void tm_open(lua_State *L)
{
(void) L;
#ifdef LUASOCKET_DEBUG
lua_pushcfunction(L, tm_lua_time);
priv_newglobal(L, "_time");
lua_pushcfunction(L, tm_lua_sleep);
priv_newglobal(L, "_sleep");
#endif
}
/*=========================================================================*\
@ -137,7 +138,6 @@ void tm_open(lua_State *L)
/*-------------------------------------------------------------------------*\
* Returns the time the system has been up, in secconds.
\*-------------------------------------------------------------------------*/
#ifdef LUASOCKET_DEBUG
static int tm_lua_time(lua_State *L)
{
lua_pushnumber(L, tm_gettime()/1000.0);
@ -157,4 +157,3 @@ int tm_lua_sleep(lua_State *L)
#endif
return 0;
}
#endif

View File

@ -1,3 +1,8 @@
/*=========================================================================*\
* Timeout management functions
*
* RCS ID: $Id$
\*=========================================================================*/
#ifndef _TM_H
#define _TM_H

View File

@ -1,5 +1,17 @@
/*=========================================================================*\
* UDP socket object implementation (inherits from sock and inet)
* UDP class: inherits from Socked and Internet domain classes and provides
* all the functionality for UDP objects.
* Lua methods:
* send: using compat module
* sendto: using compat module
* receive: using compat module
* receivefrom: using compat module
* setpeername: using internet module
* setsockname: using internet module
* Global Lua functions:
* udp: creates the udp object
*
* RCS ID: $Id$
\*=========================================================================*/
#include <string.h>
@ -21,7 +33,7 @@ static int udp_lua_receivefrom(lua_State *L);
static int udp_lua_setpeername(lua_State *L);
static int udp_lua_setsockname(lua_State *L);
static int udp_global_udpsocket(lua_State *L);
static int udp_global_udp(lua_State *L);
static struct luaL_reg funcs[] = {
{"send", udp_lua_send},
@ -44,7 +56,7 @@ void udp_open(lua_State *L)
priv_newclass(L, UDP_CLASS);
udp_inherit(L, UDP_CLASS);
/* declare global functions */
lua_pushcfunction(L, udp_global_udpsocket);
lua_pushcfunction(L, udp_global_udp);
priv_newglobal(L, "udp");
for (i = 0; i < sizeof(funcs)/sizeof(funcs[0]); i++)
priv_newglobalmethod(L, funcs[i].name);
@ -99,7 +111,7 @@ p_udp udp_push(lua_State *L)
* On success: udp socket
* On error: nil, followed by an error message
\*-------------------------------------------------------------------------*/
static int udp_global_udpsocket(lua_State *L)
static int udp_global_udp(lua_State *L)
{
int oldtop = lua_gettop(L);
p_udp udp = udp_push(L);
@ -134,7 +146,7 @@ static int udp_global_udpsocket(lua_State *L)
static int udp_lua_receive(lua_State *L)
{
p_udp udp = (p_udp) lua_touserdata(L, 1);
unsigned char buffer[UDP_DATAGRAMSIZE];
char buffer[UDP_DATAGRAMSIZE];
size_t got, wanted = (size_t) luaL_optnumber(L, 2, sizeof(buffer));
int err;
p_tm tm = &udp->base_tm;
@ -162,8 +174,8 @@ static int udp_lua_receivefrom(lua_State *L)
p_udp udp = (p_udp) lua_touserdata(L, 1);
p_tm tm = &udp->base_tm;
struct sockaddr_in peer;
int peer_len = sizeof(peer);
unsigned char buffer[UDP_DATAGRAMSIZE];
size_t peer_len = sizeof(peer);
char buffer[UDP_DATAGRAMSIZE];
size_t wanted = (size_t) luaL_optnumber(L, 2, sizeof(buffer));
size_t got;
int err;

View File

@ -1,3 +1,9 @@
/*=========================================================================*\
* UDP class: inherits from Socked and Internet domain classes and provides
* all the functionality for UDP objects.
*
* RCS ID: $Id$
\*=========================================================================*/
#ifndef UDP_H_
#define UDP_H_

View File

@ -1,8 +1,11 @@
/*=========================================================================*\
* Network compatibilization module
* Network compatibilization module: Unix version
*
* RCS ID: $Id$
\*=========================================================================*/
#include <lua.h>
#include <lauxlib.h>
#include <string.h>
#include "lscompat.h"
@ -26,7 +29,7 @@ int compat_open(lua_State *L)
}
COMPAT_FD compat_accept(COMPAT_FD s, struct sockaddr *addr,
int *len, int deadline)
size_t *len, int deadline)
{
struct timeval tv;
fd_set fds;
@ -72,7 +75,7 @@ int compat_send(COMPAT_FD c, cchar *data, size_t count, size_t *sent,
}
int compat_sendto(COMPAT_FD c, cchar *data, size_t count, size_t *sent,
int deadline, SA *addr, int len)
int deadline, SA *addr, size_t len)
{
struct timeval tv;
fd_set fds;
@ -104,7 +107,7 @@ int compat_sendto(COMPAT_FD c, cchar *data, size_t count, size_t *sent,
}
}
int compat_recv(COMPAT_FD c, uchar *data, size_t count, size_t *got,
int compat_recv(COMPAT_FD c, char *data, size_t count, size_t *got,
int deadline)
{
struct timeval tv;
@ -131,8 +134,8 @@ int compat_recv(COMPAT_FD c, uchar *data, size_t count, size_t *got,
}
}
int compat_recvfrom(COMPAT_FD c, uchar *data, size_t count, size_t *got,
int deadline, SA *addr, int *len)
int compat_recvfrom(COMPAT_FD c, char *data, size_t count, size_t *got,
int deadline, SA *addr, size_t *len)
{
struct timeval tv;
fd_set fds;

View File

@ -1,3 +1,8 @@
/*=========================================================================*\
* Network compatibilization module: Unix version
*
* RCS ID: $Id$
\*=========================================================================*/
#ifndef UNIX_H_
#define UNIX_H_

View File

@ -2,7 +2,6 @@
-- URI parsing, composition and relative URL resolution
-- LuaSocket 1.5 toolkit.
-- Author: Diego Nehab
-- Date: 20/7/2001
-- Conforming to: RFC 2396, LTN7
-- RCS ID: $Id$
----------------------------------------------------------------------------

View File

@ -60,7 +60,7 @@ local empty = function()
end
local get = function()
s = ""
local s = ""
for i,v in ipairs(files) do
s = s .. "\n" .. readfile(v)
end

View File

@ -1,5 +1,5 @@
HOST = HOST or "localhost"
PORT = PORT or "8080"
host = host or "localhost"
port = port or "8080"
function pass(...)
local s = string.format(unpack(arg))
@ -83,14 +83,14 @@ function tcpreconnect()
if data then data:close() data = nil end
data = server:accept()
]]
data, err = socket.connect(HOST, PORT)
data, err = socket.connect(host, port)
if not data then fail(err)
else pass("connected!") end
end
reconnect = tcpreconnect
pass("attempting control connection...")
control, err = socket.connect(HOST, PORT)
control, err = socket.connect(host, port)
if err then fail(err)
else pass("connected!") end
@ -104,10 +104,10 @@ function empty_connect()
if data then data:close() data = nil end
data = server:accept()
]]
data, err = socket.connect("", PORT)
data, err = socket.connect("", port)
if not data then
pass("ok")
data = socket.connect(HOST, PORT)
data = socket.connect(host, port)
else fail("should not have connected!") end
end

View File

@ -1,7 +1,7 @@
HOST = HOST or "localhost"
PORT = PORT or "8080"
host = host or "localhost"
port = port or "8080"
server, error = socket.bind(HOST, PORT)
server, error = socket.bind(host, port)
if not server then print("server: " .. tostring(error)) os.exit() end
while 1 do
print("server: waiting for client connection...");

View File

@ -1,25 +1,23 @@
-- load tftpclng.lua
assert(dofile("../examples/tftpclnt.lua"))
-- load tftpclnt.lua
dofile("tftpclnt.lua")
-- needs tftp server running on localhost, with root pointing to
-- /home/i/diego/public/html/luasocket/test
-- a directory with index.html in it
function readfile(file)
local f = openfile("file", "rb")
local a
if f then
a = read(f, "*a")
closefile(f)
end
return a
local f = io.open(file, "r")
if not f then return nil end
local a = f:read("*a")
f:close()
return a
end
host = host or "localhost"
print("downloading")
err = tftp_get(host, 69, "index.html", "index.got")
assert(not err, err)
original = readfile("index.index")
original = readfile("test/index.html")
retrieved = readfile("index.got")
remove("index.got")
os.remove("index.got")
assert(original == retrieved, "files differ!")
print("passed")

View File

@ -1,5 +1,4 @@
dofile("noglobals.lua")
local check_build_url = function(parsed)
local built = socket.url.build(parsed)