aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorDiego Nehab <diego@tecgraf.puc-rio.br>2004-06-04 15:15:45 +0000
committerDiego Nehab <diego@tecgraf.puc-rio.br>2004-06-04 15:15:45 +0000
commit9ed7f955e5fc69af9bf1794fa2c8cd227981ba24 (patch)
tree8c3521366ef84f534bbec278437be7ea24e2ac1c /src
parent63d60223da9de60f873ca08a25dbd9512c998929 (diff)
downloadluasocket-9ed7f955e5fc69af9bf1794fa2c8cd227981ba24.tar.gz
luasocket-9ed7f955e5fc69af9bf1794fa2c8cd227981ba24.tar.bz2
luasocket-9ed7f955e5fc69af9bf1794fa2c8cd227981ba24.zip
Só pra não perder se der merda.
Diffstat (limited to 'src')
-rw-r--r--src/ftp.lua41
-rw-r--r--src/http.lua57
-rw-r--r--src/inet.c3
-rw-r--r--src/ltn12.lua36
-rw-r--r--src/luasocket.c5
-rw-r--r--src/luasocket.h7
-rw-r--r--src/mime.c6
-rw-r--r--src/mime.h7
-rw-r--r--src/mime.lua16
-rw-r--r--src/select.c3
-rw-r--r--src/smtp.lua41
-rw-r--r--src/socket.lua15
-rw-r--r--src/tcp.c3
-rw-r--r--src/timeout.c3
-rw-r--r--src/tp.lua22
-rw-r--r--src/udp.c3
-rw-r--r--src/url.lua16
17 files changed, 139 insertions, 145 deletions
diff --git a/src/ftp.lua b/src/ftp.lua
index 842fdbb..79772f8 100644
--- a/src/ftp.lua
+++ b/src/ftp.lua
@@ -5,21 +5,22 @@
5-- Conforming to: RFC 959, LTN7 5-- Conforming to: RFC 959, LTN7
6-- RCS ID: $Id$ 6-- RCS ID: $Id$
7----------------------------------------------------------------------------- 7-----------------------------------------------------------------------------
8-- make sure LuaSocket is loaded 8
9require("socket") 9-----------------------------------------------------------------------------
10-- get LuaSocket namespace 10-- Load other required modules
11local socket = _G[LUASOCKET_LIBNAME] 11-----------------------------------------------------------------------------
12 12local socket = require("socket")
13-- require other modules 13local ltn12 = require("ltn12")
14require("ltn12") 14local url = require("url")
15require("url") 15local tp = require("tp")
16require("tp") 16
17 17-----------------------------------------------------------------------------
18-- create namespace inside LuaSocket namespace 18-- Setup namespace
19socket.ftp = socket.ftp or {} 19-----------------------------------------------------------------------------
20local ftp = {}
20-- make all module globals fall into namespace 21-- make all module globals fall into namespace
21setmetatable(socket.ftp, { __index = _G }) 22setmetatable(ftp, { __index = _G })
22setfenv(1, socket.ftp) 23setfenv(1, ftp)
23 24
24----------------------------------------------------------------------------- 25-----------------------------------------------------------------------------
25-- Program constants 26-- Program constants
@@ -196,8 +197,8 @@ local default = {
196 scheme = "ftp" 197 scheme = "ftp"
197} 198}
198 199
199local function parse(url) 200local function parse(u)
200 local putt = socket.try(socket.url.parse(url, default)) 201 local putt = socket.try(url.parse(u, default))
201 socket.try(putt.scheme == "ftp", "invalid scheme '" .. putt.scheme .. "'") 202 socket.try(putt.scheme == "ftp", "invalid scheme '" .. putt.scheme .. "'")
202 socket.try(putt.host, "invalid host") 203 socket.try(putt.host, "invalid host")
203 local pat = "^type=(.)$" 204 local pat = "^type=(.)$"
@@ -208,8 +209,8 @@ local function parse(url)
208 return putt 209 return putt
209end 210end
210 211
211local function sput(url, body) 212local function sput(u, body)
212 local putt = parse(url) 213 local putt = parse(u)
213 putt.source = ltn12.source.string(body) 214 putt.source = ltn12.source.string(body)
214 return tput(putt) 215 return tput(putt)
215end 216end
@@ -230,8 +231,8 @@ local function tget(gett)
230 return ftp:close() 231 return ftp:close()
231end 232end
232 233
233local function sget(url, body) 234local function sget(u, body)
234 local gett = parse(url) 235 local gett = parse(u)
235 local t = {} 236 local t = {}
236 gett.sink = ltn12.sink.table(t) 237 gett.sink = ltn12.sink.table(t)
237 tget(gett) 238 tget(gett)
diff --git a/src/http.lua b/src/http.lua
index 66a236d..ebe6b54 100644
--- a/src/http.lua
+++ b/src/http.lua
@@ -5,23 +5,22 @@
5-- Conforming to RFC 2616 5-- Conforming to RFC 2616
6-- RCS ID: $Id$ 6-- RCS ID: $Id$
7----------------------------------------------------------------------------- 7-----------------------------------------------------------------------------
8-- make sure LuaSocket is loaded
9require("socket")
10-- get LuaSocket namespace
11local socket = _G[LUASOCKET_LIBNAME]
12 8
13-- require other modules 9-----------------------------------------------------------------------------
14require("ltn12") 10-- Load other required modules
15require("mime") 11-------------------------------------------------------------------------------
16-- get MIME namespace 12local socket = require("socket")
17local mime = _G[MIME_LIBNAME] 13local ltn12 = require("ltn12")
18require("url") 14local mime = require("mime")
15local url = require("url")
19 16
20-- create namespace inside LuaSocket namespace 17-----------------------------------------------------------------------------
21socket.http = socket.http or {} 18-- Setup namespace
19-------------------------------------------------------------------------------
20http = {}
22-- make all module globals fall into namespace 21-- make all module globals fall into namespace
23setmetatable(socket.http, { __index = _G }) 22setmetatable(http, { __index = _G })
24setfenv(1, socket.http) 23setfenv(1, http)
25 24
26----------------------------------------------------------------------------- 25-----------------------------------------------------------------------------
27-- Program constants 26-- Program constants
@@ -116,17 +115,17 @@ local function receive_status(reqt, respt, tmp)
116end 115end
117 116
118local function request_uri(reqt, respt, tmp) 117local function request_uri(reqt, respt, tmp)
119 local url = tmp.parsed 118 local u = tmp.parsed
120 if not reqt.proxy then 119 if not reqt.proxy then
121 local parsed = tmp.parsed 120 local parsed = tmp.parsed
122 url = { 121 u = {
123 path = parsed.path, 122 path = parsed.path,
124 params = parsed.params, 123 params = parsed.params,
125 query = parsed.query, 124 query = parsed.query,
126 fragment = parsed.fragment 125 fragment = parsed.fragment
127 } 126 }
128 end 127 end
129 return socket.url.build(url) 128 return url.build(u)
130end 129end
131 130
132local function send_request(reqt, respt, tmp) 131local function send_request(reqt, respt, tmp)
@@ -155,7 +154,7 @@ local function open(reqt, respt, tmp)
155 local proxy = reqt.proxy or PROXY 154 local proxy = reqt.proxy or PROXY
156 local host, port 155 local host, port
157 if proxy then 156 if proxy then
158 local pproxy = socket.url.parse(proxy) 157 local pproxy = url.parse(proxy)
159 socket.try(pproxy.port and pproxy.host, "invalid proxy") 158 socket.try(pproxy.port and pproxy.host, "invalid proxy")
160 host, port = pproxy.host, pproxy.port 159 host, port = pproxy.host, pproxy.port
161 else 160 else
@@ -169,15 +168,13 @@ end
169 168
170local function adjust_headers(reqt, respt, tmp) 169local function adjust_headers(reqt, respt, tmp)
171 local lower = {} 170 local lower = {}
172 local headers = reqt.headers or {}
173 -- set default headers
174 lower["user-agent"] = USERAGENT
175 -- override with user values 171 -- override with user values
176 for i,v in headers do 172 for i,v in (reqt.headers or lower) do
177 lower[string.lower(i)] = v 173 lower[string.lower(i)] = v
178 end 174 end
175 lower["user-agent"] = lower["user-agent"] or USERAGENT
176 -- these cannot be overriden
179 lower["host"] = tmp.parsed.host 177 lower["host"] = tmp.parsed.host
180 -- this cannot be overriden
181 lower["connection"] = "close" 178 lower["connection"] = "close"
182 -- store results 179 -- store results
183 tmp.headers = lower 180 tmp.headers = lower
@@ -185,7 +182,7 @@ end
185 182
186local function parse_url(reqt, respt, tmp) 183local function parse_url(reqt, respt, tmp)
187 -- parse url with default fields 184 -- parse url with default fields
188 local parsed = socket.url.parse(reqt.url, { 185 local parsed = url.parse(reqt.url, {
189 host = "", 186 host = "",
190 port = PORT, 187 port = PORT,
191 path ="/", 188 path ="/",
@@ -250,7 +247,7 @@ local function redirect(reqt, respt, tmp)
250 method = reqt.method, 247 method = reqt.method,
251 -- the RFC says the redirect URL has to be absolute, but some 248 -- the RFC says the redirect URL has to be absolute, but some
252 -- servers do not respect that 249 -- servers do not respect that
253 url = socket.url.absolute(reqt.url, respt.headers["location"]), 250 url = url.absolute(reqt.url, respt.headers["location"]),
254 source = reqt.source, 251 source = reqt.source,
255 sink = reqt.sink, 252 sink = reqt.sink,
256 headers = reqt.headers, 253 headers = reqt.headers,
@@ -296,20 +293,20 @@ function request(reqt)
296 return respt 293 return respt
297end 294end
298 295
299function get(url) 296function get(u)
300 local t = {} 297 local t = {}
301 respt = request { 298 respt = request {
302 url = url, 299 url = u,
303 sink = ltn12.sink.table(t) 300 sink = ltn12.sink.table(t)
304 } 301 }
305 return (table.getn(t) > 0 or nil) and table.concat(t), respt.headers, 302 return (table.getn(t) > 0 or nil) and table.concat(t), respt.headers,
306 respt.code, respt.error 303 respt.code, respt.error
307end 304end
308 305
309function post(url, body) 306function post(u, body)
310 local t = {} 307 local t = {}
311 respt = request { 308 respt = request {
312 url = url, 309 url = u,
313 method = "POST", 310 method = "POST",
314 source = ltn12.source.string(body), 311 source = ltn12.source.string(body),
315 sink = ltn12.sink.table(t), 312 sink = ltn12.sink.table(t),
@@ -318,3 +315,5 @@ function post(url, body)
318 return (table.getn(t) > 0 or nil) and table.concat(t), 315 return (table.getn(t) > 0 or nil) and table.concat(t),
319 respt.headers, respt.code, respt.error 316 respt.headers, respt.code, respt.error
320end 317end
318
319return http
diff --git a/src/inet.c b/src/inet.c
index 8941575..3a57441 100644
--- a/src/inet.c
+++ b/src/inet.c
@@ -37,13 +37,10 @@ static luaL_reg func[] = {
37\*-------------------------------------------------------------------------*/ 37\*-------------------------------------------------------------------------*/
38int inet_open(lua_State *L) 38int inet_open(lua_State *L)
39{ 39{
40 lua_pushstring(L, LUASOCKET_LIBNAME);
41 lua_gettable(L, LUA_GLOBALSINDEX);
42 lua_pushstring(L, "dns"); 40 lua_pushstring(L, "dns");
43 lua_newtable(L); 41 lua_newtable(L);
44 luaL_openlib(L, NULL, func, 0); 42 luaL_openlib(L, NULL, func, 0);
45 lua_settable(L, -3); 43 lua_settable(L, -3);
46 lua_pop(L, 1);
47 return 0; 44 return 0;
48} 45}
49 46
diff --git a/src/ltn12.lua b/src/ltn12.lua
index 56e6043..41855f0 100644
--- a/src/ltn12.lua
+++ b/src/ltn12.lua
@@ -1,10 +1,16 @@
1-- create module namespace 1-----------------------------------------------------------------------------
2ltn12 = ltn12 or {} 2-- LTN12 - Filters, sources, sinks and pumps.
3-- make all globals fall into ltn12 namespace 3-- LuaSocket toolkit.
4-- Author: Diego Nehab
5-- RCS ID: $Id$
6-----------------------------------------------------------------------------
7
8-----------------------------------------------------------------------------
9-- Setup namespace
10-----------------------------------------------------------------------------
11local ltn12 = {}
4setmetatable(ltn12, { __index = _G }) 12setmetatable(ltn12, { __index = _G })
5setfenv(1, ltn12) 13setfenv(1, ltn12)
6
7-- sub namespaces
8filter = {} 14filter = {}
9source = {} 15source = {}
10sink = {} 16sink = {}
@@ -13,15 +19,14 @@ pump = {}
13-- 2048 seems to be better in windows... 19-- 2048 seems to be better in windows...
14BLOCKSIZE = 2048 20BLOCKSIZE = 2048
15 21
16local function second(a, b)
17 return b
18end
19
20local function shift(a, b, c) 22local function shift(a, b, c)
21 return b, c 23 return b, c
22end 24end
23 25
24-- returns a high level filter that cycles a cycles a low-level filter 26-----------------------------------------------------------------------------
27-- Filter stuff
28-----------------------------------------------------------------------------
29-- returns a high level filter that cycles a low-level filter
25function filter.cycle(low, ctx, extra) 30function filter.cycle(low, ctx, extra)
26 return function(chunk) 31 return function(chunk)
27 local ret 32 local ret
@@ -61,6 +66,9 @@ function filter.chain(...)
61 return f 66 return f
62end 67end
63 68
69-----------------------------------------------------------------------------
70-- Source stuff
71-----------------------------------------------------------------------------
64-- create an empty source 72-- create an empty source
65local function empty() 73local function empty()
66 return nil 74 return nil
@@ -162,6 +170,9 @@ function source.cat(...)
162 end 170 end
163end 171end
164 172
173-----------------------------------------------------------------------------
174-- Sink stuff
175-----------------------------------------------------------------------------
165-- creates a sink that stores into a table 176-- creates a sink that stores into a table
166function sink.table(t) 177function sink.table(t)
167 t = t or {} 178 t = t or {}
@@ -224,6 +235,9 @@ function sink.chain(f, snk)
224 end 235 end
225end 236end
226 237
238-----------------------------------------------------------------------------
239-- Pump stuff
240-----------------------------------------------------------------------------
227-- pumps one chunk from the source to the sink 241-- pumps one chunk from the source to the sink
228function pump.step(src, snk) 242function pump.step(src, snk)
229 local chunk, src_err = src() 243 local chunk, src_err = src()
@@ -239,3 +253,5 @@ function pump.all(src, snk, step)
239 if not ret then return not err, err end 253 if not ret then return not err, err end
240 end 254 end
241end 255end
256
257return ltn12
diff --git a/src/luasocket.c b/src/luasocket.c
index a5b6cb0..ca3a52c 100644
--- a/src/luasocket.c
+++ b/src/luasocket.c
@@ -33,7 +33,6 @@
33#include "tcp.h" 33#include "tcp.h"
34#include "udp.h" 34#include "udp.h"
35#include "select.h" 35#include "select.h"
36#include "smtp.h"
37 36
38/*-------------------------------------------------------------------------*\ 37/*-------------------------------------------------------------------------*\
39* Modules 38* Modules
@@ -47,7 +46,6 @@ static const luaL_reg mod[] = {
47 {"tcp", tcp_open}, 46 {"tcp", tcp_open},
48 {"udp", udp_open}, 47 {"udp", udp_open},
49 {"select", select_open}, 48 {"select", select_open},
50 {"smtp", smtp_open},
51 {NULL, NULL} 49 {NULL, NULL}
52}; 50};
53 51
@@ -56,7 +54,6 @@ static const luaL_reg mod[] = {
56\*-------------------------------------------------------------------------*/ 54\*-------------------------------------------------------------------------*/
57LUASOCKET_API int luaopen_socket(lua_State *L) { 55LUASOCKET_API int luaopen_socket(lua_State *L) {
58 int i; 56 int i;
59 for (i = 0; mod[i].name; i++) 57 for (i = 0; mod[i].name; i++) mod[i].func(L);
60 mod[i].func(L);
61 return 1; 58 return 1;
62} 59}
diff --git a/src/luasocket.h b/src/luasocket.h
index 81d7b3f..716b7ff 100644
--- a/src/luasocket.h
+++ b/src/luasocket.h
@@ -16,13 +16,6 @@
16#define LUASOCKET_VERSION "LuaSocket 2.0 (beta)" 16#define LUASOCKET_VERSION "LuaSocket 2.0 (beta)"
17 17
18/*-------------------------------------------------------------------------*\ 18/*-------------------------------------------------------------------------*\
19* Library's namespace
20\*-------------------------------------------------------------------------*/
21#ifndef LUASOCKET_LIBNAME
22#define LUASOCKET_LIBNAME "socket"
23#endif
24
25/*-------------------------------------------------------------------------*\
26* This macro prefixes all exported API functions 19* This macro prefixes all exported API functions
27\*-------------------------------------------------------------------------*/ 20\*-------------------------------------------------------------------------*/
28#ifndef LUASOCKET_API 21#ifndef LUASOCKET_API
diff --git a/src/mime.c b/src/mime.c
index 966509b..f42528c 100644
--- a/src/mime.c
+++ b/src/mime.c
@@ -76,9 +76,8 @@ static UC b64unbase[256];
76\*-------------------------------------------------------------------------*/ 76\*-------------------------------------------------------------------------*/
77MIME_API int luaopen_mime(lua_State *L) 77MIME_API int luaopen_mime(lua_State *L)
78{ 78{
79 lua_pushstring(L, MIME_LIBNAME); 79 lua_newtable(L);
80 lua_setglobal(L, "MIME_LIBNAME"); 80 luaL_openlib(L, NULL, func, 0);
81 luaL_openlib(L, MIME_LIBNAME, func, 0);
82 /* initialize lookup tables */ 81 /* initialize lookup tables */
83 qpsetup(qpclass, qpunbase); 82 qpsetup(qpclass, qpunbase);
84 b64setup(b64unbase); 83 b64setup(b64unbase);
@@ -626,7 +625,6 @@ static int eolprocess(int c, int last, const char *marker,
626 luaL_putchar(buffer, c); 625 luaL_putchar(buffer, c);
627 return 0; 626 return 0;
628 } 627 }
629
630} 628}
631 629
632/*-------------------------------------------------------------------------*\ 630/*-------------------------------------------------------------------------*\
diff --git a/src/mime.h b/src/mime.h
index 35389f0..6febedf 100644
--- a/src/mime.h
+++ b/src/mime.h
@@ -21,11 +21,4 @@
21 21
22MIME_API int luaopen_mime(lua_State *L); 22MIME_API int luaopen_mime(lua_State *L);
23 23
24/*-------------------------------------------------------------------------*\
25* Library's namespace
26\*-------------------------------------------------------------------------*/
27#ifndef MIME_LIBNAME
28#define MIME_LIBNAME "mime"
29#endif
30
31#endif /* MIME_H */ 24#endif /* MIME_H */
diff --git a/src/mime.lua b/src/mime.lua
index 8f2cfff..ecf310d 100644
--- a/src/mime.lua
+++ b/src/mime.lua
@@ -9,19 +9,17 @@
9-- Load MIME from dynamic library 9-- Load MIME from dynamic library
10-- Comment these lines if you are loading static 10-- Comment these lines if you are loading static
11----------------------------------------------------------------------------- 11-----------------------------------------------------------------------------
12open, err1, err2 = loadlib("mime", "luaopen_mime") 12local open = assert(loadlib("mime", "luaopen_mime"))
13if not open then error(err1) end 13local mime = assert(open())
14open()
15if not MIME_LIBNAME then error("MIME init failed") end
16 14
17----------------------------------------------------------------------------- 15-----------------------------------------------------------------------------
18-- Namespace independence 16-- Load other required modules
19----------------------------------------------------------------------------- 17-----------------------------------------------------------------------------
20local mime = _G[MIME_LIBNAME] 18local ltn12 = require("ltn12")
21if not mime then error('MIME init FAILED') end
22
23require("ltn12")
24 19
20-----------------------------------------------------------------------------
21-- Setup namespace
22-----------------------------------------------------------------------------
25-- make all module globals fall into mime namespace 23-- make all module globals fall into mime namespace
26setmetatable(mime, { __index = _G }) 24setmetatable(mime, { __index = _G })
27setfenv(1, mime) 25setfenv(1, mime)
diff --git a/src/select.c b/src/select.c
index 41bdaa4..1ebd82c 100644
--- a/src/select.c
+++ b/src/select.c
@@ -50,8 +50,7 @@ int select_open(lua_State *L)
50#else 50#else
51 lua_dofile(L, "select.lua"); 51 lua_dofile(L, "select.lua");
52#endif 52#endif
53 luaL_openlib(L, LUASOCKET_LIBNAME, func, 1); 53 luaL_openlib(L, NULL, func, 1);
54 lua_pop(L, 1);
55 aux_newclass(L, "select{fd_set}", set); 54 aux_newclass(L, "select{fd_set}", set);
56 return 0; 55 return 0;
57} 56}
diff --git a/src/smtp.lua b/src/smtp.lua
index 3108395..7ae99a5 100644
--- a/src/smtp.lua
+++ b/src/smtp.lua
@@ -5,22 +5,28 @@
5-- Conforming to RFC 2821 5-- Conforming to RFC 2821
6-- RCS ID: $Id$ 6-- RCS ID: $Id$
7----------------------------------------------------------------------------- 7-----------------------------------------------------------------------------
8-- make sure LuaSocket is loaded
9require("socket")
10-- get LuaSocket namespace
11local socket = _G[LUASOCKET_LIBNAME]
12 8
13require("ltn12") 9-----------------------------------------------------------------------------
14require("tp") 10-- Load SMTP from dynamic library
11-- Comment these lines if you are loading static
12-----------------------------------------------------------------------------
13local open = assert(loadlib("smtp", "luaopen_smtp"))
14local smtp = assert(open())
15
16-----------------------------------------------------------------------------
17-- Load other required modules
18-----------------------------------------------------------------------------
19local socket = require("socket")
20local ltn12 = require("ltn12")
21local tp = require("tp")
15 22
16-- create smtp namespace inside LuaSocket namespace 23-----------------------------------------------------------------------------
17local smtp = socket.smtp or {} 24-- Setup namespace
18socket.smtp = smtp 25-----------------------------------------------------------------------------
19-- make all module globals fall into smtp namespace 26-- make all module globals fall into smtp namespace
20setmetatable(smtp, { __index = _G }) 27setmetatable(smtp, { __index = _G })
21setfenv(1, smtp) 28setfenv(1, smtp)
22 29
23
24-- default server used to send e-mails 30-- default server used to send e-mails
25SERVER = "localhost" 31SERVER = "localhost"
26-- default port 32-- default port
@@ -89,7 +95,7 @@ end
89 95
90function open(server, port) 96function open(server, port)
91 print(server or SERVER, port or PORT) 97 print(server or SERVER, port or PORT)
92 local tp, error = socket.tp.connect(server or SERVER, port or PORT) 98 local tp, error = tp.connect(server or SERVER, port or PORT)
93 if not tp then return nil, error end 99 if not tp then return nil, error end
94 return setmetatable({tp = tp}, metat) 100 return setmetatable({tp = tp}, metat)
95end 101end
@@ -176,11 +182,16 @@ end
176 182
177-- set defaul headers 183-- set defaul headers
178local function adjust_headers(mesgt) 184local function adjust_headers(mesgt)
179 mesgt.headers = mesgt.headers or {} 185 local lower = {}
180 mesgt.headers["mime-version"] = "1.0" 186 for i,v in (mesgt or lower) do
181 mesgt.headers["date"] = mesgt.headers["date"] or 187 lower[string.lower(i)] = v
188 end
189 lower["date"] = lower["date"] or
182 os.date("!%a, %d %b %Y %H:%M:%S ") .. (mesgt.zone or ZONE) 190 os.date("!%a, %d %b %Y %H:%M:%S ") .. (mesgt.zone or ZONE)
183 mesgt.headers["x-mailer"] = mesgt.headers["x-mailer"] or socket.version 191 lower["x-mailer"] = lower["x-mailer"] or socket.version
192 -- this can't be overriden
193 lower["mime-version"] = "1.0"
194 mesgt.headers = lower
184end 195end
185 196
186function message(mesgt) 197function message(mesgt)
diff --git a/src/socket.lua b/src/socket.lua
index e6e20f2..418cd1b 100644
--- a/src/socket.lua
+++ b/src/socket.lua
@@ -6,18 +6,9 @@
6 6
7----------------------------------------------------------------------------- 7-----------------------------------------------------------------------------
8-- Load LuaSocket from dynamic library 8-- Load LuaSocket from dynamic library
9-- Comment these lines if you are loading static
10----------------------------------------------------------------------------- 9-----------------------------------------------------------------------------
11open, err1, err2 = loadlib("luasocket", "luaopen_socket") 10local open = assert(loadlib("luasocket", "luaopen_socket"))
12if not open then error(err1) end 11local socket = assert(open())
13open()
14if not LUASOCKET_LIBNAME then error("LuaSocket init failed") end
15
16-----------------------------------------------------------------------------
17-- Namespace independence
18-----------------------------------------------------------------------------
19local socket = _G[LUASOCKET_LIBNAME]
20if not socket then error('LuaSocket init failed') end
21 12
22----------------------------------------------------------------------------- 13-----------------------------------------------------------------------------
23-- Auxiliar functions 14-- Auxiliar functions
@@ -172,3 +163,5 @@ end
172socket.sourcet["default"] = socket.sourcet["until-closed"] 163socket.sourcet["default"] = socket.sourcet["until-closed"]
173 164
174socket.source = socket.choose(socket.sourcet) 165socket.source = socket.choose(socket.sourcet)
166
167return socket
diff --git a/src/tcp.c b/src/tcp.c
index d0bc957..90cfcde 100644
--- a/src/tcp.c
+++ b/src/tcp.c
@@ -96,8 +96,7 @@ int tcp_open(lua_State *L)
96 aux_add2group(L, "tcp{client}", "select{able}"); 96 aux_add2group(L, "tcp{client}", "select{able}");
97 aux_add2group(L, "tcp{server}", "select{able}"); 97 aux_add2group(L, "tcp{server}", "select{able}");
98 /* define library functions */ 98 /* define library functions */
99 luaL_openlib(L, LUASOCKET_LIBNAME, func, 0); 99 luaL_openlib(L, NULL, func, 0);
100 lua_pop(L, 1);
101 return 0; 100 return 0;
102} 101}
103 102
diff --git a/src/timeout.c b/src/timeout.c
index 1d710dc..bd6c3b4 100644
--- a/src/timeout.c
+++ b/src/timeout.c
@@ -143,8 +143,7 @@ int tm_gettime(void)
143\*-------------------------------------------------------------------------*/ 143\*-------------------------------------------------------------------------*/
144int tm_open(lua_State *L) 144int tm_open(lua_State *L)
145{ 145{
146 luaL_openlib(L, LUASOCKET_LIBNAME, func, 0); 146 luaL_openlib(L, NULL, func, 0);
147 lua_pop(L, 1);
148 return 0; 147 return 0;
149} 148}
150 149
diff --git a/src/tp.lua b/src/tp.lua
index f510226..3e9dba6 100644
--- a/src/tp.lua
+++ b/src/tp.lua
@@ -5,16 +5,18 @@
5-- Conforming to: RFC 2616, LTN7 5-- Conforming to: RFC 2616, LTN7
6-- RCS ID: $Id$ 6-- RCS ID: $Id$
7----------------------------------------------------------------------------- 7-----------------------------------------------------------------------------
8-- make sure LuaSocket is loaded
9require("socket")
10-- get LuaSocket namespace
11local socket = _G[LUASOCKET_LIBNAME]
12 8
13-- create namespace inside LuaSocket namespace 9-----------------------------------------------------------------------------
14socket.tp = socket.tp or {} 10-- Load other required modules
15-- make all module globals fall into namespace 11-----------------------------------------------------------------------------
16setmetatable(socket.tp, { __index = _G }) 12local socket = require("socket")
17setfenv(1, socket.tp) 13
14-----------------------------------------------------------------------------
15-- Setup namespace
16-----------------------------------------------------------------------------
17tp = {}
18setmetatable(tp, { __index = _G })
19setfenv(1, tp)
18 20
19TIMEOUT = 60 21TIMEOUT = 60
20 22
@@ -107,3 +109,5 @@ function connect(host, port)
107 control:settimeout(TIMEOUT) 109 control:settimeout(TIMEOUT)
108 return setmetatable({control = control}, metat) 110 return setmetatable({control = control}, metat)
109end 111end
112
113return tp
diff --git a/src/udp.c b/src/udp.c
index 19cefe6..4770a2e 100644
--- a/src/udp.c
+++ b/src/udp.c
@@ -90,8 +90,7 @@ int udp_open(lua_State *L)
90 aux_add2group(L, "udp{connected}", "select{able}"); 90 aux_add2group(L, "udp{connected}", "select{able}");
91 aux_add2group(L, "udp{unconnected}", "select{able}"); 91 aux_add2group(L, "udp{unconnected}", "select{able}");
92 /* define library functions */ 92 /* define library functions */
93 luaL_openlib(L, LUASOCKET_LIBNAME, func, 0); 93 luaL_openlib(L, NULL, func, 0);
94 lua_pop(L, 1);
95 return 0; 94 return 0;
96} 95}
97 96
diff --git a/src/url.lua b/src/url.lua
index 8c591c0..2441268 100644
--- a/src/url.lua
+++ b/src/url.lua
@@ -4,16 +4,12 @@
4-- Author: Diego Nehab 4-- Author: Diego Nehab
5-- Conforming to: RFC 2396, LTN7 5-- Conforming to: RFC 2396, LTN7
6-- RCS ID: $Id$ 6-- RCS ID: $Id$
7---------------------------------------------------------------------------- 7-----------------------------------------------------------------------------
8-- make sure LuaSocket is loaded
9require("socket")
10-- get LuaSocket namespace
11local socket = _G[LUASOCKET_LIBNAME]
12 8
13-- create url namespace inside LuaSocket namespace 9-----------------------------------------------------------------------------
14local url = socket.url or {} 10-- Setup namespace
15socket.url = url 11-----------------------------------------------------------------------------
16-- make all module globals fall into url namespace 12local url = {}
17setmetatable(url, { __index = _G }) 13setmetatable(url, { __index = _G })
18setfenv(1, url) 14setfenv(1, url)
19 15
@@ -275,3 +271,5 @@ function build_path(parsed, unsafe)
275 if parsed.is_absolute then path = "/" .. path end 271 if parsed.is_absolute then path = "/" .. path end
276 return path 272 return path
277end 273end
274
275return url