From 5ca1049ab47f3f9ff9157f71af9072f04a637500 Mon Sep 17 00:00:00 2001 From: Diego Nehab Date: Sun, 30 May 2004 21:36:22 +0000 Subject: Fine tuning the "require" business. --- src/ftp.lua | 11 +++++++---- src/http.lua | 2 ++ src/luasocket.c | 17 +++++------------ src/mime.c | 3 +-- src/select.h | 8 ++++---- src/smtp.lua | 5 +++-- src/socket.h | 1 + src/tp.lua | 6 ++++-- src/url.lua | 3 ++- src/usocket.c | 8 ++++++++ src/wsocket.c | 9 +++++++++ 11 files changed, 46 insertions(+), 27 deletions(-) (limited to 'src') diff --git a/src/ftp.lua b/src/ftp.lua index 72be695..842fdbb 100644 --- a/src/ftp.lua +++ b/src/ftp.lua @@ -13,6 +13,7 @@ local socket = _G[LUASOCKET_LIBNAME] -- require other modules require("ltn12") require("url") +require("tp") -- create namespace inside LuaSocket namespace socket.ftp = socket.ftp or {} @@ -101,7 +102,9 @@ function metat.__index:send(sendt) local data socket.try(self.pasvt or self.portt, "need port or pasv first") if self.pasvt then data = socket.try(pasv(self.pasvt)) end - socket.try(self.tp:command(sendt.command or "stor", sendt.argument)) + local argument = sendt.argument or string.gsub(sendt.path, "^/", "") + local command = sendt.command or "stor" + socket.try(self.tp:command(command, argument)) local code, reply = socket.try(self.tp:check{"2..", "1.."}) if self.portt then data = socket.try(port(self.portt)) end local step = sendt.step or ltn12.pump.step @@ -128,7 +131,9 @@ function metat.__index:receive(recvt) local data socket.try(self.pasvt or self.portt, "need port or pasv first") if self.pasvt then data = socket.try(pasv(self.pasvt)) end - socket.try(self.tp:command(recvt.command or "retr", recvt.argument)) + local argument = recvt.argument or string.gsub(recvt.path, "^/", "") + local command = recvt.command or "retr" + socket.try(self.tp:command(command, argument)) local code = socket.try(self.tp:check{"1..", "2.."}) if self.portt then data = socket.try(port(self.portt)) end local source = socket.source("until-closed", data) @@ -200,8 +205,6 @@ local function parse(url) putt.type = socket.skip(2, string.find(putt.params, pat)) socket.try(putt.type == "a" or putt.type == "i") end - -- skip first backslash in path - putt.argument = string.sub(putt.path, 2) return putt end diff --git a/src/http.lua b/src/http.lua index b372a2e..66a236d 100644 --- a/src/http.lua +++ b/src/http.lua @@ -13,6 +13,8 @@ local socket = _G[LUASOCKET_LIBNAME] -- require other modules require("ltn12") require("mime") +-- get MIME namespace +local mime = _G[MIME_LIBNAME] require("url") -- create namespace inside LuaSocket namespace diff --git a/src/luasocket.c b/src/luasocket.c index 8d49be5..a5b6cb0 100644 --- a/src/luasocket.c +++ b/src/luasocket.c @@ -25,11 +25,10 @@ \*=========================================================================*/ #include "luasocket.h" -#include "base.h" #include "auxiliar.h" +#include "base.h" #include "timeout.h" #include "buffer.h" -#include "socket.h" #include "inet.h" #include "tcp.h" #include "udp.h" @@ -40,10 +39,10 @@ * Modules \*-------------------------------------------------------------------------*/ static const luaL_reg mod[] = { + {"auxiliar", aux_open}, {"base", base_open}, - {"aux", aux_open}, - {"tm", tm_open}, - {"buf", buf_open}, + {"timeout", tm_open}, + {"buffer", buf_open}, {"inet", inet_open}, {"tcp", tcp_open}, {"udp", udp_open}, @@ -55,14 +54,8 @@ static const luaL_reg mod[] = { /*-------------------------------------------------------------------------*\ * Initializes all library modules. \*-------------------------------------------------------------------------*/ -LUASOCKET_API int luaopen_socket(lua_State *L) -{ +LUASOCKET_API int luaopen_socket(lua_State *L) { int i; - if (!sock_open()) { - lua_pushnil(L); - lua_pushstring(L, "unable to initialize library"); - return 2; - } for (i = 0; mod[i].name; i++) mod[i].func(L); return 1; diff --git a/src/mime.c b/src/mime.c index 7a2baae..8cfcd26 100644 --- a/src/mime.c +++ b/src/mime.c @@ -82,8 +82,7 @@ int luaopen_mime(lua_State *L) /* initialize lookup tables */ qpsetup(qpclass, qpunbase); b64setup(b64unbase); - lua_pop(L, 1); - return 0; + return 1; } /*=========================================================================*\ diff --git a/src/select.h b/src/select.h index de10ea4..b58f082 100644 --- a/src/select.h +++ b/src/select.h @@ -7,10 +7,10 @@ * To make the code as simple as possible, the select function is * implemented int Lua, with a few helper functions written in C. * -* Each object that can be passed to the select function has to be in the -* group select{able} and export two methods: fd() and dirty(). Fd returns -* the descriptor to be passed to the select function. Dirty() should return -* true if there is data ready for reading (required for buffered input). +* Each object that can be passed to the select function has to export two +* methods: fd() and dirty(). Fd returns the descriptor to be passed to the +* select function. Dirty() should return true if there is data ready for +* reading (required for buffered input). * * RCS ID: $Id$ \*=========================================================================*/ diff --git a/src/smtp.lua b/src/smtp.lua index 01babbe..3108395 100644 --- a/src/smtp.lua +++ b/src/smtp.lua @@ -6,11 +6,12 @@ -- RCS ID: $Id$ ----------------------------------------------------------------------------- -- make sure LuaSocket is loaded -require"socket" +require("socket") -- get LuaSocket namespace local socket = _G[LUASOCKET_LIBNAME] -require"ltn12" +require("ltn12") +require("tp") -- create smtp namespace inside LuaSocket namespace local smtp = socket.smtp or {} diff --git a/src/socket.h b/src/socket.h index 2e7b6f9..85e8848 100644 --- a/src/socket.h +++ b/src/socket.h @@ -38,6 +38,7 @@ typedef struct sockaddr SA; * interface to sockets \*=========================================================================*/ int sock_open(void); +int sock_close(void); void sock_destroy(p_sock ps); void sock_shutdown(p_sock ps, int how); int sock_send(p_sock ps, const char *data, size_t count, diff --git a/src/tp.lua b/src/tp.lua index b671e58..f510226 100644 --- a/src/tp.lua +++ b/src/tp.lua @@ -6,10 +6,10 @@ -- RCS ID: $Id$ ----------------------------------------------------------------------------- -- make sure LuaSocket is loaded -if not LUASOCKET_LIBNAME then error('module requires LuaSocket') end +require("socket") -- get LuaSocket namespace local socket = _G[LUASOCKET_LIBNAME] -if not socket then error('module requires LuaSocket') end + -- create namespace inside LuaSocket namespace socket.tp = socket.tp or {} -- make all module globals fall into namespace @@ -35,6 +35,7 @@ local function get_reply(control) -- reply ends with same code until code == current and sep == " " end +print(reply) return code, reply end @@ -58,6 +59,7 @@ function metat.__index:check(ok) end function metat.__index:command(cmd, arg) +print(cmd, arg) if arg then return self.control:send(cmd .. " " .. arg.. "\r\n") else return self.control:send(cmd .. "\r\n") end end diff --git a/src/url.lua b/src/url.lua index 16b19e0..8c591c0 100644 --- a/src/url.lua +++ b/src/url.lua @@ -9,10 +9,11 @@ require("socket") -- get LuaSocket namespace local socket = _G[LUASOCKET_LIBNAME] + -- create url namespace inside LuaSocket namespace local url = socket.url or {} socket.url = url --- make all module globals fall into smtp namespace +-- make all module globals fall into url namespace setmetatable(url, { __index = _G }) setfenv(1, url) diff --git a/src/usocket.c b/src/usocket.c index 9e6efd3..6b4182b 100644 --- a/src/usocket.c +++ b/src/usocket.c @@ -42,6 +42,14 @@ int sock_open(void) return 1; } +/*-------------------------------------------------------------------------*\ +* Close module +\*-------------------------------------------------------------------------*/ +int sock_close(void) +{ + return 1; +} + /*-------------------------------------------------------------------------*\ * Close and inutilize socket \*-------------------------------------------------------------------------*/ diff --git a/src/wsocket.c b/src/wsocket.c index 023f470..08c1046 100644 --- a/src/wsocket.c +++ b/src/wsocket.c @@ -37,6 +37,15 @@ int sock_open(void) return 1; } +/*-------------------------------------------------------------------------*\ +* Close module +\*-------------------------------------------------------------------------*/ +int sock_close(void) +{ + WSACleanup(); + return 1; +} + /*-------------------------------------------------------------------------*\ * Select with int timeout in ms \*-------------------------------------------------------------------------*/ -- cgit v1.2.3-55-g6feb