diff options
author | Diego Nehab <diego@tecgraf.puc-rio.br> | 2004-06-04 15:15:45 +0000 |
---|---|---|
committer | Diego Nehab <diego@tecgraf.puc-rio.br> | 2004-06-04 15:15:45 +0000 |
commit | 9ed7f955e5fc69af9bf1794fa2c8cd227981ba24 (patch) | |
tree | 8c3521366ef84f534bbec278437be7ea24e2ac1c | |
parent | 63d60223da9de60f873ca08a25dbd9512c998929 (diff) | |
download | luasocket-9ed7f955e5fc69af9bf1794fa2c8cd227981ba24.tar.gz luasocket-9ed7f955e5fc69af9bf1794fa2c8cd227981ba24.tar.bz2 luasocket-9ed7f955e5fc69af9bf1794fa2c8cd227981ba24.zip |
Só pra não perder se der merda.
-rw-r--r-- | etc/README | 2 | ||||
-rw-r--r-- | etc/b64.lua | 4 | ||||
-rw-r--r-- | etc/check-links.lua | 43 | ||||
-rw-r--r-- | etc/dict.lua | 4 | ||||
-rw-r--r-- | etc/eol.lua | 5 | ||||
-rw-r--r-- | etc/get.lua | 39 | ||||
-rw-r--r-- | samples/cddb.lua | 5 | ||||
-rw-r--r-- | samples/echoclnt.lua | 2 | ||||
-rw-r--r-- | samples/echosrvr.lua | 2 | ||||
-rw-r--r-- | samples/listener.lua | 2 | ||||
-rw-r--r-- | src/ftp.lua | 41 | ||||
-rw-r--r-- | src/http.lua | 57 | ||||
-rw-r--r-- | src/inet.c | 3 | ||||
-rw-r--r-- | src/ltn12.lua | 36 | ||||
-rw-r--r-- | src/luasocket.c | 5 | ||||
-rw-r--r-- | src/luasocket.h | 7 | ||||
-rw-r--r-- | src/mime.c | 6 | ||||
-rw-r--r-- | src/mime.h | 7 | ||||
-rw-r--r-- | src/mime.lua | 16 | ||||
-rw-r--r-- | src/select.c | 3 | ||||
-rw-r--r-- | src/smtp.lua | 41 | ||||
-rw-r--r-- | src/socket.lua | 15 | ||||
-rw-r--r-- | src/tcp.c | 3 | ||||
-rw-r--r-- | src/timeout.c | 3 | ||||
-rw-r--r-- | src/tp.lua | 22 | ||||
-rw-r--r-- | src/udp.c | 3 | ||||
-rw-r--r-- | src/url.lua | 16 | ||||
-rw-r--r-- | test/mimetest.lua | 6 |
28 files changed, 196 insertions, 202 deletions
@@ -1,7 +1,7 @@ | |||
1 | This directory contains code that is more useful than the examples. This code | 1 | This directory contains code that is more useful than the examples. This code |
2 | *is* supported. | 2 | *is* supported. |
3 | 3 | ||
4 | lua.lua and luasocket.lua | 4 | lua.lua |
5 | 5 | ||
6 | These are modules to suport dynamic loading of LuaSocket by the stand alone | 6 | These are modules to suport dynamic loading of LuaSocket by the stand alone |
7 | Lua Interpreter with the use of the "require" function. For my Mac OS X | 7 | Lua Interpreter with the use of the "require" function. For my Mac OS X |
diff --git a/etc/b64.lua b/etc/b64.lua index b86b870..4d5f83e 100644 --- a/etc/b64.lua +++ b/etc/b64.lua | |||
@@ -1,5 +1,5 @@ | |||
1 | require("ltn12") | 1 | local ltn12 = require("ltn12") |
2 | require("mime") | 2 | local mime = require("mime") |
3 | local source = ltn12.source.file(io.stdin) | 3 | local source = ltn12.source.file(io.stdin) |
4 | local sink = ltn12.sink.file(io.stdout) | 4 | local sink = ltn12.sink.file(io.stdout) |
5 | local convert | 5 | local convert |
diff --git a/etc/check-links.lua b/etc/check-links.lua index 898ed4b..a4e9ef8 100644 --- a/etc/check-links.lua +++ b/etc/check-links.lua | |||
@@ -4,15 +4,14 @@ | |||
4 | -- Author: Diego Nehab | 4 | -- Author: Diego Nehab |
5 | -- RCS ID: $Id$ | 5 | -- RCS ID: $Id$ |
6 | ----------------------------------------------------------------------------- | 6 | ----------------------------------------------------------------------------- |
7 | 7 | local http = require("http") | |
8 | require"http" | 8 | local url = require("url") |
9 | 9 | http.TIMEOUT = 10 | |
10 | socket.http.TIMEOUT = 10 | ||
11 | 10 | ||
12 | cache = {} | 11 | cache = {} |
13 | 12 | ||
14 | function readfile(path) | 13 | function readfile(path) |
15 | path = socket.url.unescape(path) | 14 | path = url.unescape(path) |
16 | local file, error = io.open(path, "r") | 15 | local file, error = io.open(path, "r") |
17 | if file then | 16 | if file then |
18 | local body = file:read("*a") | 17 | local body = file:read("*a") |
@@ -21,32 +20,32 @@ function readfile(path) | |||
21 | else return nil, error end | 20 | else return nil, error end |
22 | end | 21 | end |
23 | 22 | ||
24 | function getstatus(url) | 23 | function getstatus(u) |
25 | local parsed = socket.url.parse(url, { scheme = "file" }) | 24 | local parsed = url.parse(u, {scheme = "file"}) |
26 | if cache[url] then return cache[url] end | 25 | if cache[u] then return cache[u] end |
27 | local res | 26 | local res |
28 | if parsed.scheme == "http" then | 27 | if parsed.scheme == "http" then |
29 | local request = { url = url, method = "HEAD" } | 28 | local request = {url = u, method = "HEAD"} |
30 | local response = socket.http.request(request) | 29 | local response = http.request(request) |
31 | if response.code == 200 then res = nil | 30 | if response.code == 200 then res = nil |
32 | else res = response.status or response.error end | 31 | else res = response.status or response.error end |
33 | elseif parsed.scheme == "file" then | 32 | elseif parsed.scheme == "file" then |
34 | local file, error = io.open(socket.url.unescape(parsed.path), "r") | 33 | local file, error = io.open(url.unescape(parsed.path), "r") |
35 | if file then | 34 | if file then |
36 | file:close() | 35 | file:close() |
37 | res = nil | 36 | res = nil |
38 | else res = error end | 37 | else res = error end |
39 | else res = string.format("unhandled scheme '%s'", parsed.scheme) end | 38 | else res = string.format("unhandled scheme '%s'", parsed.scheme) end |
40 | cache[url] = res | 39 | cache[u] = res |
41 | return res | 40 | return res |
42 | end | 41 | end |
43 | 42 | ||
44 | function retrieve(url) | 43 | function retrieve(u) |
45 | local parsed = socket.url.parse(url, { scheme = "file" }) | 44 | local parsed = url.parse(u, { scheme = "file" }) |
46 | local body, headers, code, error | 45 | local body, headers, code, error |
47 | local base = url | 46 | local base = u |
48 | if parsed.scheme == "http" then | 47 | if parsed.scheme == "http" then |
49 | body, headers, code, error = socket.http.get(url) | 48 | body, headers, code, error = http.get(u) |
50 | if code == 200 then | 49 | if code == 200 then |
51 | base = base or headers.location | 50 | base = base or headers.location |
52 | end | 51 | end |
@@ -62,19 +61,19 @@ function getlinks(body, base) | |||
62 | local links = {} | 61 | local links = {} |
63 | -- extract links | 62 | -- extract links |
64 | body = string.gsub(body, '[Hh][Rr][Ee][Ff]%s*=%s*"([^"]*)"', function(href) | 63 | body = string.gsub(body, '[Hh][Rr][Ee][Ff]%s*=%s*"([^"]*)"', function(href) |
65 | table.insert(links, socket.url.absolute(base, href)) | 64 | table.insert(links, url.absolute(base, href)) |
66 | end) | 65 | end) |
67 | body = string.gsub(body, "[Hh][Rr][Ee][Ff]%s*=%s*'([^']*)'", function(href) | 66 | body = string.gsub(body, "[Hh][Rr][Ee][Ff]%s*=%s*'([^']*)'", function(href) |
68 | table.insert(links, socket.url.absolute(base, href)) | 67 | table.insert(links, url.absolute(base, href)) |
69 | end) | 68 | end) |
70 | string.gsub(body, "[Hh][Rr][Ee][Ff]%s*=%s*(.-)>", function(href) | 69 | string.gsub(body, "[Hh][Rr][Ee][Ff]%s*=%s*(.-)>", function(href) |
71 | table.insert(links, socket.url.absolute(base, href)) | 70 | table.insert(links, url.absolute(base, href)) |
72 | end) | 71 | end) |
73 | return links | 72 | return links |
74 | end | 73 | end |
75 | 74 | ||
76 | function checklinks(url) | 75 | function checklinks(u) |
77 | local base, body, error = retrieve(url) | 76 | local base, body, error = retrieve(u) |
78 | if not body then print(error) return end | 77 | if not body then print(error) return end |
79 | local links = getlinks(body, base) | 78 | local links = getlinks(body, base) |
80 | for _, l in ipairs(links) do | 79 | for _, l in ipairs(links) do |
@@ -91,5 +90,5 @@ if table.getn(arg) < 1 then | |||
91 | end | 90 | end |
92 | for _, a in ipairs(arg) do | 91 | for _, a in ipairs(arg) do |
93 | print("Checking ", a) | 92 | print("Checking ", a) |
94 | checklinks(socket.url.absolute("file:", a)) | 93 | checklinks(url.absolute("file:", a)) |
95 | end | 94 | end |
diff --git a/etc/dict.lua b/etc/dict.lua index e5d4740..dd001cf 100644 --- a/etc/dict.lua +++ b/etc/dict.lua | |||
@@ -4,7 +4,7 @@ | |||
4 | -- Author: Diego Nehab | 4 | -- Author: Diego Nehab |
5 | -- RCS ID: $Id$ | 5 | -- RCS ID: $Id$ |
6 | ----------------------------------------------------------------------------- | 6 | ----------------------------------------------------------------------------- |
7 | require"socket" | 7 | local socket = require("socket") |
8 | 8 | ||
9 | function get_status(sock, valid) | 9 | function get_status(sock, valid) |
10 | local line, err = sock:receive() | 10 | local line, err = sock:receive() |
@@ -77,5 +77,5 @@ if arg and arg[1] then | |||
77 | defs, err = dict_get(arg[1], arg[2]) | 77 | defs, err = dict_get(arg[1], arg[2]) |
78 | print(defs or err) | 78 | print(defs or err) |
79 | else | 79 | else |
80 | io.write("Usage:\n luasocket dict.lua <word> [<dictionary>]\n") | 80 | io.write("Usage:\n lua dict.lua <word> [<dictionary>]\n") |
81 | end | 81 | end |
diff --git a/etc/eol.lua b/etc/eol.lua index b13684b..d3da776 100644 --- a/etc/eol.lua +++ b/etc/eol.lua | |||
@@ -1,6 +1,5 @@ | |||
1 | require"mime.lua" | 1 | local mime = require("mime") |
2 | require"ltn12.lua" | 2 | local ltn12 = require("ltn12") |
3 | |||
4 | local marker = '\n' | 3 | local marker = '\n' |
5 | if arg and arg[1] == '-d' then marker = '\r\n' end | 4 | if arg and arg[1] == '-d' then marker = '\r\n' end |
6 | local filter = mime.normalize(marker) | 5 | local filter = mime.normalize(marker) |
diff --git a/etc/get.lua b/etc/get.lua index 35de7d7..c1e0542 100644 --- a/etc/get.lua +++ b/etc/get.lua | |||
@@ -4,9 +4,10 @@ | |||
4 | -- Author: Diego Nehab | 4 | -- Author: Diego Nehab |
5 | -- RCS ID: $Id$ | 5 | -- RCS ID: $Id$ |
6 | ----------------------------------------------------------------------------- | 6 | ----------------------------------------------------------------------------- |
7 | require"http" | 7 | socket = require("socket") |
8 | require"ftp" | 8 | http = require("http") |
9 | require"url" | 9 | ftp = require("ftp") |
10 | url = require("url") | ||
10 | 11 | ||
11 | -- formats a number of seconds into human readable form | 12 | -- formats a number of seconds into human readable form |
12 | function nicetime(s) | 13 | function nicetime(s) |
@@ -84,55 +85,55 @@ function stats(size) | |||
84 | end | 85 | end |
85 | 86 | ||
86 | -- determines the size of a http file | 87 | -- determines the size of a http file |
87 | function gethttpsize(url) | 88 | function gethttpsize(u) |
88 | local respt = socket.http.request {method = "HEAD", url = url} | 89 | local respt = http.request {method = "HEAD", url = u} |
89 | if respt.code == 200 then | 90 | if respt.code == 200 then |
90 | return tonumber(respt.headers["content-length"]) | 91 | return tonumber(respt.headers["content-length"]) |
91 | end | 92 | end |
92 | end | 93 | end |
93 | 94 | ||
94 | -- downloads a file using the http protocol | 95 | -- downloads a file using the http protocol |
95 | function getbyhttp(url, file) | 96 | function getbyhttp(u, file) |
96 | local save = ltn12.sink.file(file or io.stdout) | 97 | local save = ltn12.sink.file(file or io.stdout) |
97 | -- only print feedback if output is not stdout | 98 | -- only print feedback if output is not stdout |
98 | if file then save = ltn12.sink.chain(stats(gethttpsize(url)), save) end | 99 | if file then save = ltn12.sink.chain(stats(gethttpsize(u)), save) end |
99 | local respt = socket.http.request {url = url, sink = save } | 100 | local respt = http.request {url = u, sink = save } |
100 | if respt.code ~= 200 then print(respt.status or respt.error) end | 101 | if respt.code ~= 200 then print(respt.status or respt.error) end |
101 | end | 102 | end |
102 | 103 | ||
103 | -- downloads a file using the ftp protocol | 104 | -- downloads a file using the ftp protocol |
104 | function getbyftp(url, file) | 105 | function getbyftp(u, file) |
105 | local save = ltn12.sink.file(file or io.stdout) | 106 | local save = ltn12.sink.file(file or io.stdout) |
106 | -- only print feedback if output is not stdout | 107 | -- only print feedback if output is not stdout |
107 | -- and we don't know how big the file is | 108 | -- and we don't know how big the file is |
108 | if file then save = ltn12.sink.chain(stats(), save) end | 109 | if file then save = ltn12.sink.chain(stats(), save) end |
109 | local gett = socket.url.parse(url) | 110 | local gett = url.parse(u) |
110 | gett.sink = save | 111 | gett.sink = save |
111 | gett.type = "i" | 112 | gett.type = "i" |
112 | local ret, err = socket.ftp.get(gett) | 113 | local ret, err = ftp.get(gett) |
113 | if err then print(err) end | 114 | if err then print(err) end |
114 | end | 115 | end |
115 | 116 | ||
116 | -- determines the scheme | 117 | -- determines the scheme |
117 | function getscheme(url) | 118 | function getscheme(u) |
118 | -- this is an heuristic to solve a common invalid url poblem | 119 | -- this is an heuristic to solve a common invalid url poblem |
119 | if not string.find(url, "//") then url = "//" .. url end | 120 | if not string.find(u, "//") then u = "//" .. u end |
120 | local parsed = socket.url.parse(url, {scheme = "http"}) | 121 | local parsed = url.parse(u, {scheme = "http"}) |
121 | return parsed.scheme | 122 | return parsed.scheme |
122 | end | 123 | end |
123 | 124 | ||
124 | -- gets a file either by http or ftp, saving as <name> | 125 | -- gets a file either by http or ftp, saving as <name> |
125 | function get(url, name) | 126 | function get(u, name) |
126 | local fout = name and io.open(name, "wb") | 127 | local fout = name and io.open(name, "wb") |
127 | local scheme = getscheme(url) | 128 | local scheme = getscheme(u) |
128 | if scheme == "ftp" then getbyftp(url, fout) | 129 | if scheme == "ftp" then getbyftp(u, fout) |
129 | elseif scheme == "http" then getbyhttp(url, fout) | 130 | elseif scheme == "http" then getbyhttp(u, fout) |
130 | else print("unknown scheme" .. scheme) end | 131 | else print("unknown scheme" .. scheme) end |
131 | end | 132 | end |
132 | 133 | ||
133 | -- main program | 134 | -- main program |
134 | arg = arg or {} | 135 | arg = arg or {} |
135 | if table.getn(arg) < 1 then | 136 | if table.getn(arg) < 1 then |
136 | io.write("Usage:\n luasocket get.lua <remote-url> [<local-file>]\n") | 137 | io.write("Usage:\n lua get.lua <remote-url> [<local-file>]\n") |
137 | os.exit(1) | 138 | os.exit(1) |
138 | else get(arg[1], arg[2]) end | 139 | else get(arg[1], arg[2]) end |
diff --git a/samples/cddb.lua b/samples/cddb.lua index d2e5b59..0ceba09 100644 --- a/samples/cddb.lua +++ b/samples/cddb.lua | |||
@@ -1,4 +1,5 @@ | |||
1 | require"http" | 1 | socket = require("socket") |
2 | http = require("http") | ||
2 | 3 | ||
3 | if not arg or not arg[1] or not arg[2] then | 4 | if not arg or not arg[1] or not arg[2] then |
4 | print("luasocket cddb.lua <category> <disc-id> [<server>]") | 5 | print("luasocket cddb.lua <category> <disc-id> [<server>]") |
@@ -31,7 +32,7 @@ end | |||
31 | local host = socket.dns.gethostname() | 32 | local host = socket.dns.gethostname() |
32 | local query = "%s?cmd=cddb+read+%s+%s&hello=LuaSocket+%s+LuaSocket+2.0&proto=6" | 33 | local query = "%s?cmd=cddb+read+%s+%s&hello=LuaSocket+%s+LuaSocket+2.0&proto=6" |
33 | local url = string.format(query, server, arg[1], arg[2], host) | 34 | local url = string.format(query, server, arg[1], arg[2], host) |
34 | local body, headers, code, error = socket.http.get(url) | 35 | local body, headers, code, error = http.get(url) |
35 | 36 | ||
36 | if code == 200 then | 37 | if code == 200 then |
37 | local data, code, error = parse(body) | 38 | local data, code, error = parse(body) |
diff --git a/samples/echoclnt.lua b/samples/echoclnt.lua index 877e99b..038061d 100644 --- a/samples/echoclnt.lua +++ b/samples/echoclnt.lua | |||
@@ -4,7 +4,7 @@ | |||
4 | -- Author: Diego Nehab | 4 | -- Author: Diego Nehab |
5 | -- RCS ID: $Id$ | 5 | -- RCS ID: $Id$ |
6 | ----------------------------------------------------------------------------- | 6 | ----------------------------------------------------------------------------- |
7 | require"socket" | 7 | local socket = require("socket") |
8 | host = host or "localhost" | 8 | host = host or "localhost" |
9 | port = port or 7 | 9 | port = port or 7 |
10 | if arg then | 10 | if arg then |
diff --git a/samples/echosrvr.lua b/samples/echosrvr.lua index 79bff8f..73bf374 100644 --- a/samples/echosrvr.lua +++ b/samples/echosrvr.lua | |||
@@ -4,7 +4,7 @@ | |||
4 | -- Author: Diego Nehab | 4 | -- Author: Diego Nehab |
5 | -- RCS ID: $Id$ | 5 | -- RCS ID: $Id$ |
6 | ----------------------------------------------------------------------------- | 6 | ----------------------------------------------------------------------------- |
7 | require"socket" | 7 | socket = require("socket") |
8 | host = host or "127.0.0.1" | 8 | host = host or "127.0.0.1" |
9 | port = port or 7 | 9 | port = port or 7 |
10 | if arg then | 10 | if arg then |
diff --git a/samples/listener.lua b/samples/listener.lua index bedbde5..b2f7a7e 100644 --- a/samples/listener.lua +++ b/samples/listener.lua | |||
@@ -4,7 +4,7 @@ | |||
4 | -- Author: Diego Nehab | 4 | -- Author: Diego Nehab |
5 | -- RCS ID: $Id$ | 5 | -- RCS ID: $Id$ |
6 | ----------------------------------------------------------------------------- | 6 | ----------------------------------------------------------------------------- |
7 | require("socket") | 7 | local socket = require("socket") |
8 | host = host or "*" | 8 | host = host or "*" |
9 | port = port or 8080 | 9 | port = port or 8080 |
10 | if arg then | 10 | if arg then |
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 | |
9 | require("socket") | 9 | ----------------------------------------------------------------------------- |
10 | -- get LuaSocket namespace | 10 | -- Load other required modules |
11 | local socket = _G[LUASOCKET_LIBNAME] | 11 | ----------------------------------------------------------------------------- |
12 | 12 | local socket = require("socket") | |
13 | -- require other modules | 13 | local ltn12 = require("ltn12") |
14 | require("ltn12") | 14 | local url = require("url") |
15 | require("url") | 15 | local tp = require("tp") |
16 | require("tp") | 16 | |
17 | 17 | ----------------------------------------------------------------------------- | |
18 | -- create namespace inside LuaSocket namespace | 18 | -- Setup namespace |
19 | socket.ftp = socket.ftp or {} | 19 | ----------------------------------------------------------------------------- |
20 | local ftp = {} | ||
20 | -- make all module globals fall into namespace | 21 | -- make all module globals fall into namespace |
21 | setmetatable(socket.ftp, { __index = _G }) | 22 | setmetatable(ftp, { __index = _G }) |
22 | setfenv(1, socket.ftp) | 23 | setfenv(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 | ||
199 | local function parse(url) | 200 | local 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 |
209 | end | 210 | end |
210 | 211 | ||
211 | local function sput(url, body) | 212 | local 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) |
215 | end | 216 | end |
@@ -230,8 +231,8 @@ local function tget(gett) | |||
230 | return ftp:close() | 231 | return ftp:close() |
231 | end | 232 | end |
232 | 233 | ||
233 | local function sget(url, body) | 234 | local 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 | ||
9 | require("socket") | ||
10 | -- get LuaSocket namespace | ||
11 | local socket = _G[LUASOCKET_LIBNAME] | ||
12 | 8 | ||
13 | -- require other modules | 9 | ----------------------------------------------------------------------------- |
14 | require("ltn12") | 10 | -- Load other required modules |
15 | require("mime") | 11 | ------------------------------------------------------------------------------- |
16 | -- get MIME namespace | 12 | local socket = require("socket") |
17 | local mime = _G[MIME_LIBNAME] | 13 | local ltn12 = require("ltn12") |
18 | require("url") | 14 | local mime = require("mime") |
15 | local url = require("url") | ||
19 | 16 | ||
20 | -- create namespace inside LuaSocket namespace | 17 | ----------------------------------------------------------------------------- |
21 | socket.http = socket.http or {} | 18 | -- Setup namespace |
19 | ------------------------------------------------------------------------------- | ||
20 | http = {} | ||
22 | -- make all module globals fall into namespace | 21 | -- make all module globals fall into namespace |
23 | setmetatable(socket.http, { __index = _G }) | 22 | setmetatable(http, { __index = _G }) |
24 | setfenv(1, socket.http) | 23 | setfenv(1, http) |
25 | 24 | ||
26 | ----------------------------------------------------------------------------- | 25 | ----------------------------------------------------------------------------- |
27 | -- Program constants | 26 | -- Program constants |
@@ -116,17 +115,17 @@ local function receive_status(reqt, respt, tmp) | |||
116 | end | 115 | end |
117 | 116 | ||
118 | local function request_uri(reqt, respt, tmp) | 117 | local 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) |
130 | end | 129 | end |
131 | 130 | ||
132 | local function send_request(reqt, respt, tmp) | 131 | local 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 | ||
170 | local function adjust_headers(reqt, respt, tmp) | 169 | local 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 | ||
186 | local function parse_url(reqt, respt, tmp) | 183 | local 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 |
297 | end | 294 | end |
298 | 295 | ||
299 | function get(url) | 296 | function 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 |
307 | end | 304 | end |
308 | 305 | ||
309 | function post(url, body) | 306 | function 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 |
320 | end | 317 | end |
318 | |||
319 | return http | ||
@@ -37,13 +37,10 @@ static luaL_reg func[] = { | |||
37 | \*-------------------------------------------------------------------------*/ | 37 | \*-------------------------------------------------------------------------*/ |
38 | int inet_open(lua_State *L) | 38 | int 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 | ----------------------------------------------------------------------------- |
2 | ltn12 = 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 | ----------------------------------------------------------------------------- | ||
11 | local ltn12 = {} | ||
4 | setmetatable(ltn12, { __index = _G }) | 12 | setmetatable(ltn12, { __index = _G }) |
5 | setfenv(1, ltn12) | 13 | setfenv(1, ltn12) |
6 | |||
7 | -- sub namespaces | ||
8 | filter = {} | 14 | filter = {} |
9 | source = {} | 15 | source = {} |
10 | sink = {} | 16 | sink = {} |
@@ -13,15 +19,14 @@ pump = {} | |||
13 | -- 2048 seems to be better in windows... | 19 | -- 2048 seems to be better in windows... |
14 | BLOCKSIZE = 2048 | 20 | BLOCKSIZE = 2048 |
15 | 21 | ||
16 | local function second(a, b) | ||
17 | return b | ||
18 | end | ||
19 | |||
20 | local function shift(a, b, c) | 22 | local function shift(a, b, c) |
21 | return b, c | 23 | return b, c |
22 | end | 24 | end |
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 | ||
25 | function filter.cycle(low, ctx, extra) | 30 | function 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 |
62 | end | 67 | end |
63 | 68 | ||
69 | ----------------------------------------------------------------------------- | ||
70 | -- Source stuff | ||
71 | ----------------------------------------------------------------------------- | ||
64 | -- create an empty source | 72 | -- create an empty source |
65 | local function empty() | 73 | local function empty() |
66 | return nil | 74 | return nil |
@@ -162,6 +170,9 @@ function source.cat(...) | |||
162 | end | 170 | end |
163 | end | 171 | end |
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 |
166 | function sink.table(t) | 177 | function sink.table(t) |
167 | t = t or {} | 178 | t = t or {} |
@@ -224,6 +235,9 @@ function sink.chain(f, snk) | |||
224 | end | 235 | end |
225 | end | 236 | end |
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 |
228 | function pump.step(src, snk) | 242 | function 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 |
241 | end | 255 | end |
256 | |||
257 | return 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 | \*-------------------------------------------------------------------------*/ |
57 | LUASOCKET_API int luaopen_socket(lua_State *L) { | 55 | LUASOCKET_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 |
@@ -76,9 +76,8 @@ static UC b64unbase[256]; | |||
76 | \*-------------------------------------------------------------------------*/ | 76 | \*-------------------------------------------------------------------------*/ |
77 | MIME_API int luaopen_mime(lua_State *L) | 77 | MIME_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 | /*-------------------------------------------------------------------------*\ |
@@ -21,11 +21,4 @@ | |||
21 | 21 | ||
22 | MIME_API int luaopen_mime(lua_State *L); | 22 | MIME_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 | ----------------------------------------------------------------------------- |
12 | open, err1, err2 = loadlib("mime", "luaopen_mime") | 12 | local open = assert(loadlib("mime", "luaopen_mime")) |
13 | if not open then error(err1) end | 13 | local mime = assert(open()) |
14 | open() | ||
15 | if not MIME_LIBNAME then error("MIME init failed") end | ||
16 | 14 | ||
17 | ----------------------------------------------------------------------------- | 15 | ----------------------------------------------------------------------------- |
18 | -- Namespace independence | 16 | -- Load other required modules |
19 | ----------------------------------------------------------------------------- | 17 | ----------------------------------------------------------------------------- |
20 | local mime = _G[MIME_LIBNAME] | 18 | local ltn12 = require("ltn12") |
21 | if not mime then error('MIME init FAILED') end | ||
22 | |||
23 | require("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 |
26 | setmetatable(mime, { __index = _G }) | 24 | setmetatable(mime, { __index = _G }) |
27 | setfenv(1, mime) | 25 | setfenv(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 | ||
9 | require("socket") | ||
10 | -- get LuaSocket namespace | ||
11 | local socket = _G[LUASOCKET_LIBNAME] | ||
12 | 8 | ||
13 | require("ltn12") | 9 | ----------------------------------------------------------------------------- |
14 | require("tp") | 10 | -- Load SMTP from dynamic library |
11 | -- Comment these lines if you are loading static | ||
12 | ----------------------------------------------------------------------------- | ||
13 | local open = assert(loadlib("smtp", "luaopen_smtp")) | ||
14 | local smtp = assert(open()) | ||
15 | |||
16 | ----------------------------------------------------------------------------- | ||
17 | -- Load other required modules | ||
18 | ----------------------------------------------------------------------------- | ||
19 | local socket = require("socket") | ||
20 | local ltn12 = require("ltn12") | ||
21 | local tp = require("tp") | ||
15 | 22 | ||
16 | -- create smtp namespace inside LuaSocket namespace | 23 | ----------------------------------------------------------------------------- |
17 | local smtp = socket.smtp or {} | 24 | -- Setup namespace |
18 | socket.smtp = smtp | 25 | ----------------------------------------------------------------------------- |
19 | -- make all module globals fall into smtp namespace | 26 | -- make all module globals fall into smtp namespace |
20 | setmetatable(smtp, { __index = _G }) | 27 | setmetatable(smtp, { __index = _G }) |
21 | setfenv(1, smtp) | 28 | setfenv(1, smtp) |
22 | 29 | ||
23 | |||
24 | -- default server used to send e-mails | 30 | -- default server used to send e-mails |
25 | SERVER = "localhost" | 31 | SERVER = "localhost" |
26 | -- default port | 32 | -- default port |
@@ -89,7 +95,7 @@ end | |||
89 | 95 | ||
90 | function open(server, port) | 96 | function 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) |
95 | end | 101 | end |
@@ -176,11 +182,16 @@ end | |||
176 | 182 | ||
177 | -- set defaul headers | 183 | -- set defaul headers |
178 | local function adjust_headers(mesgt) | 184 | local 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 | ||
184 | end | 195 | end |
185 | 196 | ||
186 | function message(mesgt) | 197 | function 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 | ----------------------------------------------------------------------------- |
11 | open, err1, err2 = loadlib("luasocket", "luaopen_socket") | 10 | local open = assert(loadlib("luasocket", "luaopen_socket")) |
12 | if not open then error(err1) end | 11 | local socket = assert(open()) |
13 | open() | ||
14 | if not LUASOCKET_LIBNAME then error("LuaSocket init failed") end | ||
15 | |||
16 | ----------------------------------------------------------------------------- | ||
17 | -- Namespace independence | ||
18 | ----------------------------------------------------------------------------- | ||
19 | local socket = _G[LUASOCKET_LIBNAME] | ||
20 | if not socket then error('LuaSocket init failed') end | ||
21 | 12 | ||
22 | ----------------------------------------------------------------------------- | 13 | ----------------------------------------------------------------------------- |
23 | -- Auxiliar functions | 14 | -- Auxiliar functions |
@@ -172,3 +163,5 @@ end | |||
172 | socket.sourcet["default"] = socket.sourcet["until-closed"] | 163 | socket.sourcet["default"] = socket.sourcet["until-closed"] |
173 | 164 | ||
174 | socket.source = socket.choose(socket.sourcet) | 165 | socket.source = socket.choose(socket.sourcet) |
166 | |||
167 | return socket | ||
@@ -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 | \*-------------------------------------------------------------------------*/ |
144 | int tm_open(lua_State *L) | 144 | int 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 | ||
@@ -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 | ||
9 | require("socket") | ||
10 | -- get LuaSocket namespace | ||
11 | local socket = _G[LUASOCKET_LIBNAME] | ||
12 | 8 | ||
13 | -- create namespace inside LuaSocket namespace | 9 | ----------------------------------------------------------------------------- |
14 | socket.tp = socket.tp or {} | 10 | -- Load other required modules |
15 | -- make all module globals fall into namespace | 11 | ----------------------------------------------------------------------------- |
16 | setmetatable(socket.tp, { __index = _G }) | 12 | local socket = require("socket") |
17 | setfenv(1, socket.tp) | 13 | |
14 | ----------------------------------------------------------------------------- | ||
15 | -- Setup namespace | ||
16 | ----------------------------------------------------------------------------- | ||
17 | tp = {} | ||
18 | setmetatable(tp, { __index = _G }) | ||
19 | setfenv(1, tp) | ||
18 | 20 | ||
19 | TIMEOUT = 60 | 21 | TIMEOUT = 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) |
109 | end | 111 | end |
112 | |||
113 | return tp | ||
@@ -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 | ||
9 | require("socket") | ||
10 | -- get LuaSocket namespace | ||
11 | local socket = _G[LUASOCKET_LIBNAME] | ||
12 | 8 | ||
13 | -- create url namespace inside LuaSocket namespace | 9 | ----------------------------------------------------------------------------- |
14 | local url = socket.url or {} | 10 | -- Setup namespace |
15 | socket.url = url | 11 | ----------------------------------------------------------------------------- |
16 | -- make all module globals fall into url namespace | 12 | local url = {} |
17 | setmetatable(url, { __index = _G }) | 13 | setmetatable(url, { __index = _G }) |
18 | setfenv(1, url) | 14 | setfenv(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 |
277 | end | 273 | end |
274 | |||
275 | return url | ||
diff --git a/test/mimetest.lua b/test/mimetest.lua index 29101a9..2d5bce6 100644 --- a/test/mimetest.lua +++ b/test/mimetest.lua | |||
@@ -1,6 +1,6 @@ | |||
1 | require("socket") | 1 | local socket = require("socket") |
2 | require("ltn12") | 2 | local ltn12 = require("ltn12") |
3 | require("mime") | 3 | local mime = require("mime") |
4 | 4 | ||
5 | dofile("testsupport.lua") | 5 | dofile("testsupport.lua") |
6 | 6 | ||