aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorDiego Nehab <diego@tecgraf.puc-rio.br>2004-05-30 21:36:22 +0000
committerDiego Nehab <diego@tecgraf.puc-rio.br>2004-05-30 21:36:22 +0000
commit5ca1049ab47f3f9ff9157f71af9072f04a637500 (patch)
tree24fcb14f2890900a4a709312ab25bfc2c14a3939 /src
parentc23240726e3044e3eaa32a82a999b754c08bc183 (diff)
downloadluasocket-5ca1049ab47f3f9ff9157f71af9072f04a637500.tar.gz
luasocket-5ca1049ab47f3f9ff9157f71af9072f04a637500.tar.bz2
luasocket-5ca1049ab47f3f9ff9157f71af9072f04a637500.zip
Fine tuning the "require" business.
Diffstat (limited to 'src')
-rw-r--r--src/ftp.lua11
-rw-r--r--src/http.lua2
-rw-r--r--src/luasocket.c17
-rw-r--r--src/mime.c3
-rw-r--r--src/select.h8
-rw-r--r--src/smtp.lua5
-rw-r--r--src/socket.h1
-rw-r--r--src/tp.lua6
-rw-r--r--src/url.lua3
-rw-r--r--src/usocket.c8
-rw-r--r--src/wsocket.c9
11 files changed, 46 insertions, 27 deletions
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]
13-- require other modules 13-- require other modules
14require("ltn12") 14require("ltn12")
15require("url") 15require("url")
16require("tp")
16 17
17-- create namespace inside LuaSocket namespace 18-- create namespace inside LuaSocket namespace
18socket.ftp = socket.ftp or {} 19socket.ftp = socket.ftp or {}
@@ -101,7 +102,9 @@ function metat.__index:send(sendt)
101 local data 102 local data
102 socket.try(self.pasvt or self.portt, "need port or pasv first") 103 socket.try(self.pasvt or self.portt, "need port or pasv first")
103 if self.pasvt then data = socket.try(pasv(self.pasvt)) end 104 if self.pasvt then data = socket.try(pasv(self.pasvt)) end
104 socket.try(self.tp:command(sendt.command or "stor", sendt.argument)) 105 local argument = sendt.argument or string.gsub(sendt.path, "^/", "")
106 local command = sendt.command or "stor"
107 socket.try(self.tp:command(command, argument))
105 local code, reply = socket.try(self.tp:check{"2..", "1.."}) 108 local code, reply = socket.try(self.tp:check{"2..", "1.."})
106 if self.portt then data = socket.try(port(self.portt)) end 109 if self.portt then data = socket.try(port(self.portt)) end
107 local step = sendt.step or ltn12.pump.step 110 local step = sendt.step or ltn12.pump.step
@@ -128,7 +131,9 @@ function metat.__index:receive(recvt)
128 local data 131 local data
129 socket.try(self.pasvt or self.portt, "need port or pasv first") 132 socket.try(self.pasvt or self.portt, "need port or pasv first")
130 if self.pasvt then data = socket.try(pasv(self.pasvt)) end 133 if self.pasvt then data = socket.try(pasv(self.pasvt)) end
131 socket.try(self.tp:command(recvt.command or "retr", recvt.argument)) 134 local argument = recvt.argument or string.gsub(recvt.path, "^/", "")
135 local command = recvt.command or "retr"
136 socket.try(self.tp:command(command, argument))
132 local code = socket.try(self.tp:check{"1..", "2.."}) 137 local code = socket.try(self.tp:check{"1..", "2.."})
133 if self.portt then data = socket.try(port(self.portt)) end 138 if self.portt then data = socket.try(port(self.portt)) end
134 local source = socket.source("until-closed", data) 139 local source = socket.source("until-closed", data)
@@ -200,8 +205,6 @@ local function parse(url)
200 putt.type = socket.skip(2, string.find(putt.params, pat)) 205 putt.type = socket.skip(2, string.find(putt.params, pat))
201 socket.try(putt.type == "a" or putt.type == "i") 206 socket.try(putt.type == "a" or putt.type == "i")
202 end 207 end
203 -- skip first backslash in path
204 putt.argument = string.sub(putt.path, 2)
205 return putt 208 return putt
206end 209end
207 210
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]
13-- require other modules 13-- require other modules
14require("ltn12") 14require("ltn12")
15require("mime") 15require("mime")
16-- get MIME namespace
17local mime = _G[MIME_LIBNAME]
16require("url") 18require("url")
17 19
18-- create namespace inside LuaSocket namespace 20-- 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 @@
25\*=========================================================================*/ 25\*=========================================================================*/
26#include "luasocket.h" 26#include "luasocket.h"
27 27
28#include "base.h"
29#include "auxiliar.h" 28#include "auxiliar.h"
29#include "base.h"
30#include "timeout.h" 30#include "timeout.h"
31#include "buffer.h" 31#include "buffer.h"
32#include "socket.h"
33#include "inet.h" 32#include "inet.h"
34#include "tcp.h" 33#include "tcp.h"
35#include "udp.h" 34#include "udp.h"
@@ -40,10 +39,10 @@
40* Modules 39* Modules
41\*-------------------------------------------------------------------------*/ 40\*-------------------------------------------------------------------------*/
42static const luaL_reg mod[] = { 41static const luaL_reg mod[] = {
42 {"auxiliar", aux_open},
43 {"base", base_open}, 43 {"base", base_open},
44 {"aux", aux_open}, 44 {"timeout", tm_open},
45 {"tm", tm_open}, 45 {"buffer", buf_open},
46 {"buf", buf_open},
47 {"inet", inet_open}, 46 {"inet", inet_open},
48 {"tcp", tcp_open}, 47 {"tcp", tcp_open},
49 {"udp", udp_open}, 48 {"udp", udp_open},
@@ -55,14 +54,8 @@ static const luaL_reg mod[] = {
55/*-------------------------------------------------------------------------*\ 54/*-------------------------------------------------------------------------*\
56* Initializes all library modules. 55* Initializes all library modules.
57\*-------------------------------------------------------------------------*/ 56\*-------------------------------------------------------------------------*/
58LUASOCKET_API int luaopen_socket(lua_State *L) 57LUASOCKET_API int luaopen_socket(lua_State *L) {
59{
60 int i; 58 int i;
61 if (!sock_open()) {
62 lua_pushnil(L);
63 lua_pushstring(L, "unable to initialize library");
64 return 2;
65 }
66 for (i = 0; mod[i].name; i++) 59 for (i = 0; mod[i].name; i++)
67 mod[i].func(L); 60 mod[i].func(L);
68 return 1; 61 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)
82 /* initialize lookup tables */ 82 /* initialize lookup tables */
83 qpsetup(qpclass, qpunbase); 83 qpsetup(qpclass, qpunbase);
84 b64setup(b64unbase); 84 b64setup(b64unbase);
85 lua_pop(L, 1); 85 return 1;
86 return 0;
87} 86}
88 87
89/*=========================================================================*\ 88/*=========================================================================*\
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 @@
7* To make the code as simple as possible, the select function is 7* To make the code as simple as possible, the select function is
8* implemented int Lua, with a few helper functions written in C. 8* implemented int Lua, with a few helper functions written in C.
9* 9*
10* Each object that can be passed to the select function has to be in the 10* Each object that can be passed to the select function has to export two
11* group select{able} and export two methods: fd() and dirty(). Fd returns 11* methods: fd() and dirty(). Fd returns the descriptor to be passed to the
12* the descriptor to be passed to the select function. Dirty() should return 12* select function. Dirty() should return true if there is data ready for
13* true if there is data ready for reading (required for buffered input). 13* reading (required for buffered input).
14* 14*
15* RCS ID: $Id$ 15* RCS ID: $Id$
16\*=========================================================================*/ 16\*=========================================================================*/
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 @@
6-- RCS ID: $Id$ 6-- RCS ID: $Id$
7----------------------------------------------------------------------------- 7-----------------------------------------------------------------------------
8-- make sure LuaSocket is loaded 8-- make sure LuaSocket is loaded
9require"socket" 9require("socket")
10-- get LuaSocket namespace 10-- get LuaSocket namespace
11local socket = _G[LUASOCKET_LIBNAME] 11local socket = _G[LUASOCKET_LIBNAME]
12 12
13require"ltn12" 13require("ltn12")
14require("tp")
14 15
15-- create smtp namespace inside LuaSocket namespace 16-- create smtp namespace inside LuaSocket namespace
16local smtp = socket.smtp or {} 17local 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;
38* interface to sockets 38* interface to sockets
39\*=========================================================================*/ 39\*=========================================================================*/
40int sock_open(void); 40int sock_open(void);
41int sock_close(void);
41void sock_destroy(p_sock ps); 42void sock_destroy(p_sock ps);
42void sock_shutdown(p_sock ps, int how); 43void sock_shutdown(p_sock ps, int how);
43int sock_send(p_sock ps, const char *data, size_t count, 44int 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 @@
6-- RCS ID: $Id$ 6-- RCS ID: $Id$
7----------------------------------------------------------------------------- 7-----------------------------------------------------------------------------
8-- make sure LuaSocket is loaded 8-- make sure LuaSocket is loaded
9if not LUASOCKET_LIBNAME then error('module requires LuaSocket') end 9require("socket")
10-- get LuaSocket namespace 10-- get LuaSocket namespace
11local socket = _G[LUASOCKET_LIBNAME] 11local socket = _G[LUASOCKET_LIBNAME]
12if not socket then error('module requires LuaSocket') end 12
13-- create namespace inside LuaSocket namespace 13-- create namespace inside LuaSocket namespace
14socket.tp = socket.tp or {} 14socket.tp = socket.tp or {}
15-- make all module globals fall into namespace 15-- make all module globals fall into namespace
@@ -35,6 +35,7 @@ local function get_reply(control)
35 -- reply ends with same code 35 -- reply ends with same code
36 until code == current and sep == " " 36 until code == current and sep == " "
37 end 37 end
38print(reply)
38 return code, reply 39 return code, reply
39end 40end
40 41
@@ -58,6 +59,7 @@ function metat.__index:check(ok)
58end 59end
59 60
60function metat.__index:command(cmd, arg) 61function metat.__index:command(cmd, arg)
62print(cmd, arg)
61 if arg then return self.control:send(cmd .. " " .. arg.. "\r\n") 63 if arg then return self.control:send(cmd .. " " .. arg.. "\r\n")
62 else return self.control:send(cmd .. "\r\n") end 64 else return self.control:send(cmd .. "\r\n") end
63end 65end
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 @@
9require("socket") 9require("socket")
10-- get LuaSocket namespace 10-- get LuaSocket namespace
11local socket = _G[LUASOCKET_LIBNAME] 11local socket = _G[LUASOCKET_LIBNAME]
12
12-- create url namespace inside LuaSocket namespace 13-- create url namespace inside LuaSocket namespace
13local url = socket.url or {} 14local url = socket.url or {}
14socket.url = url 15socket.url = url
15-- make all module globals fall into smtp namespace 16-- make all module globals fall into url namespace
16setmetatable(url, { __index = _G }) 17setmetatable(url, { __index = _G })
17setfenv(1, url) 18setfenv(1, url)
18 19
diff --git a/src/usocket.c b/src/usocket.c
index 9e6efd3..6b4182b 100644
--- a/src/usocket.c
+++ b/src/usocket.c
@@ -43,6 +43,14 @@ int sock_open(void)
43} 43}
44 44
45/*-------------------------------------------------------------------------*\ 45/*-------------------------------------------------------------------------*\
46* Close module
47\*-------------------------------------------------------------------------*/
48int sock_close(void)
49{
50 return 1;
51}
52
53/*-------------------------------------------------------------------------*\
46* Close and inutilize socket 54* Close and inutilize socket
47\*-------------------------------------------------------------------------*/ 55\*-------------------------------------------------------------------------*/
48void sock_destroy(p_sock ps) 56void sock_destroy(p_sock ps)
diff --git a/src/wsocket.c b/src/wsocket.c
index 023f470..08c1046 100644
--- a/src/wsocket.c
+++ b/src/wsocket.c
@@ -38,6 +38,15 @@ int sock_open(void)
38} 38}
39 39
40/*-------------------------------------------------------------------------*\ 40/*-------------------------------------------------------------------------*\
41* Close module
42\*-------------------------------------------------------------------------*/
43int sock_close(void)
44{
45 WSACleanup();
46 return 1;
47}
48
49/*-------------------------------------------------------------------------*\
41* Select with int timeout in ms 50* Select with int timeout in ms
42\*-------------------------------------------------------------------------*/ 51\*-------------------------------------------------------------------------*/
43int sock_select(int n, fd_set *rfds, fd_set *wfds, fd_set *efds, int timeout) 52int sock_select(int n, fd_set *rfds, fd_set *wfds, fd_set *efds, int timeout)