diff options
| author | Diego Nehab <diego@tecgraf.puc-rio.br> | 2004-05-30 21:36:22 +0000 |
|---|---|---|
| committer | Diego Nehab <diego@tecgraf.puc-rio.br> | 2004-05-30 21:36:22 +0000 |
| commit | 5ca1049ab47f3f9ff9157f71af9072f04a637500 (patch) | |
| tree | 24fcb14f2890900a4a709312ab25bfc2c14a3939 /src | |
| parent | c23240726e3044e3eaa32a82a999b754c08bc183 (diff) | |
| download | luasocket-5ca1049ab47f3f9ff9157f71af9072f04a637500.tar.gz luasocket-5ca1049ab47f3f9ff9157f71af9072f04a637500.tar.bz2 luasocket-5ca1049ab47f3f9ff9157f71af9072f04a637500.zip | |
Fine tuning the "require" business.
Diffstat (limited to 'src')
| -rw-r--r-- | src/ftp.lua | 11 | ||||
| -rw-r--r-- | src/http.lua | 2 | ||||
| -rw-r--r-- | src/luasocket.c | 17 | ||||
| -rw-r--r-- | src/mime.c | 3 | ||||
| -rw-r--r-- | src/select.h | 8 | ||||
| -rw-r--r-- | src/smtp.lua | 5 | ||||
| -rw-r--r-- | src/socket.h | 1 | ||||
| -rw-r--r-- | src/tp.lua | 6 | ||||
| -rw-r--r-- | src/url.lua | 3 | ||||
| -rw-r--r-- | src/usocket.c | 8 | ||||
| -rw-r--r-- | src/wsocket.c | 9 |
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 |
| 14 | require("ltn12") | 14 | require("ltn12") |
| 15 | require("url") | 15 | require("url") |
| 16 | require("tp") | ||
| 16 | 17 | ||
| 17 | -- create namespace inside LuaSocket namespace | 18 | -- create namespace inside LuaSocket namespace |
| 18 | socket.ftp = socket.ftp or {} | 19 | socket.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 |
| 206 | end | 209 | end |
| 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 |
| 14 | require("ltn12") | 14 | require("ltn12") |
| 15 | require("mime") | 15 | require("mime") |
| 16 | -- get MIME namespace | ||
| 17 | local mime = _G[MIME_LIBNAME] | ||
| 16 | require("url") | 18 | require("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 | \*-------------------------------------------------------------------------*/ |
| 42 | static const luaL_reg mod[] = { | 41 | static 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 | \*-------------------------------------------------------------------------*/ |
| 58 | LUASOCKET_API int luaopen_socket(lua_State *L) | 57 | LUASOCKET_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; |
| @@ -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 |
| 9 | require"socket" | 9 | require("socket") |
| 10 | -- get LuaSocket namespace | 10 | -- get LuaSocket namespace |
| 11 | local socket = _G[LUASOCKET_LIBNAME] | 11 | local socket = _G[LUASOCKET_LIBNAME] |
| 12 | 12 | ||
| 13 | require"ltn12" | 13 | require("ltn12") |
| 14 | require("tp") | ||
| 14 | 15 | ||
| 15 | -- create smtp namespace inside LuaSocket namespace | 16 | -- create smtp namespace inside LuaSocket namespace |
| 16 | local smtp = socket.smtp or {} | 17 | 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; | |||
| 38 | * interface to sockets | 38 | * interface to sockets |
| 39 | \*=========================================================================*/ | 39 | \*=========================================================================*/ |
| 40 | int sock_open(void); | 40 | int sock_open(void); |
| 41 | int sock_close(void); | ||
| 41 | void sock_destroy(p_sock ps); | 42 | void sock_destroy(p_sock ps); |
| 42 | void sock_shutdown(p_sock ps, int how); | 43 | void sock_shutdown(p_sock ps, int how); |
| 43 | int sock_send(p_sock ps, const char *data, size_t count, | 44 | int sock_send(p_sock ps, const char *data, size_t count, |
| @@ -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 |
| 9 | if not LUASOCKET_LIBNAME then error('module requires LuaSocket') end | 9 | require("socket") |
| 10 | -- get LuaSocket namespace | 10 | -- get LuaSocket namespace |
| 11 | local socket = _G[LUASOCKET_LIBNAME] | 11 | local socket = _G[LUASOCKET_LIBNAME] |
| 12 | if not socket then error('module requires LuaSocket') end | 12 | |
| 13 | -- create namespace inside LuaSocket namespace | 13 | -- create namespace inside LuaSocket namespace |
| 14 | socket.tp = socket.tp or {} | 14 | socket.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 |
| 38 | print(reply) | ||
| 38 | return code, reply | 39 | return code, reply |
| 39 | end | 40 | end |
| 40 | 41 | ||
| @@ -58,6 +59,7 @@ function metat.__index:check(ok) | |||
| 58 | end | 59 | end |
| 59 | 60 | ||
| 60 | function metat.__index:command(cmd, arg) | 61 | function metat.__index:command(cmd, arg) |
| 62 | print(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 |
| 63 | end | 65 | 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 @@ | |||
| 9 | require("socket") | 9 | require("socket") |
| 10 | -- get LuaSocket namespace | 10 | -- get LuaSocket namespace |
| 11 | local socket = _G[LUASOCKET_LIBNAME] | 11 | local socket = _G[LUASOCKET_LIBNAME] |
| 12 | |||
| 12 | -- create url namespace inside LuaSocket namespace | 13 | -- create url namespace inside LuaSocket namespace |
| 13 | local url = socket.url or {} | 14 | local url = socket.url or {} |
| 14 | socket.url = url | 15 | socket.url = url |
| 15 | -- make all module globals fall into smtp namespace | 16 | -- make all module globals fall into url namespace |
| 16 | setmetatable(url, { __index = _G }) | 17 | setmetatable(url, { __index = _G }) |
| 17 | setfenv(1, url) | 18 | setfenv(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 | \*-------------------------------------------------------------------------*/ | ||
| 48 | int sock_close(void) | ||
| 49 | { | ||
| 50 | return 1; | ||
| 51 | } | ||
| 52 | |||
| 53 | /*-------------------------------------------------------------------------*\ | ||
| 46 | * Close and inutilize socket | 54 | * Close and inutilize socket |
| 47 | \*-------------------------------------------------------------------------*/ | 55 | \*-------------------------------------------------------------------------*/ |
| 48 | void sock_destroy(p_sock ps) | 56 | void 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 | \*-------------------------------------------------------------------------*/ | ||
| 43 | int 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 | \*-------------------------------------------------------------------------*/ |
| 43 | int sock_select(int n, fd_set *rfds, fd_set *wfds, fd_set *efds, int timeout) | 52 | int sock_select(int n, fd_set *rfds, fd_set *wfds, fd_set *efds, int timeout) |
