aboutsummaryrefslogtreecommitdiff
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
parent63d60223da9de60f873ca08a25dbd9512c998929 (diff)
downloadluasocket-9ed7f955e5fc69af9bf1794fa2c8cd227981ba24.tar.gz
luasocket-9ed7f955e5fc69af9bf1794fa2c8cd227981ba24.tar.bz2
luasocket-9ed7f955e5fc69af9bf1794fa2c8cd227981ba24.zip
Só pra não perder se der merda.
-rw-r--r--etc/README2
-rw-r--r--etc/b64.lua4
-rw-r--r--etc/check-links.lua43
-rw-r--r--etc/dict.lua4
-rw-r--r--etc/eol.lua5
-rw-r--r--etc/get.lua39
-rw-r--r--samples/cddb.lua5
-rw-r--r--samples/echoclnt.lua2
-rw-r--r--samples/echosrvr.lua2
-rw-r--r--samples/listener.lua2
-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
-rw-r--r--test/mimetest.lua6
28 files changed, 196 insertions, 202 deletions
diff --git a/etc/README b/etc/README
index 623adf7..eacb262 100644
--- a/etc/README
+++ b/etc/README
@@ -1,7 +1,7 @@
1This directory contains code that is more useful than the examples. This code 1This 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
6These are modules to suport dynamic loading of LuaSocket by the stand alone 6These are modules to suport dynamic loading of LuaSocket by the stand alone
7Lua Interpreter with the use of the "require" function. For my Mac OS X 7Lua 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 @@
1require("ltn12") 1local ltn12 = require("ltn12")
2require("mime") 2local mime = require("mime")
3local source = ltn12.source.file(io.stdin) 3local source = ltn12.source.file(io.stdin)
4local sink = ltn12.sink.file(io.stdout) 4local sink = ltn12.sink.file(io.stdout)
5local convert 5local 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 7local http = require("http")
8require"http" 8local url = require("url")
9 9http.TIMEOUT = 10
10socket.http.TIMEOUT = 10
11 10
12cache = {} 11cache = {}
13 12
14function readfile(path) 13function 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
22end 21end
23 22
24function getstatus(url) 23function 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
42end 41end
43 42
44function retrieve(url) 43function 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
74end 73end
75 74
76function checklinks(url) 75function 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
91end 90end
92for _, a in ipairs(arg) do 91for _, 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))
95end 94end
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-----------------------------------------------------------------------------
7require"socket" 7local socket = require("socket")
8 8
9function get_status(sock, valid) 9function 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)
79else 79else
80 io.write("Usage:\n luasocket dict.lua <word> [<dictionary>]\n") 80 io.write("Usage:\n lua dict.lua <word> [<dictionary>]\n")
81end 81end
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 @@
1require"mime.lua" 1local mime = require("mime")
2require"ltn12.lua" 2local ltn12 = require("ltn12")
3
4local marker = '\n' 3local marker = '\n'
5if arg and arg[1] == '-d' then marker = '\r\n' end 4if arg and arg[1] == '-d' then marker = '\r\n' end
6local filter = mime.normalize(marker) 5local 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-----------------------------------------------------------------------------
7require"http" 7socket = require("socket")
8require"ftp" 8http = require("http")
9require"url" 9ftp = require("ftp")
10url = require("url")
10 11
11-- formats a number of seconds into human readable form 12-- formats a number of seconds into human readable form
12function nicetime(s) 13function nicetime(s)
@@ -84,55 +85,55 @@ function stats(size)
84end 85end
85 86
86-- determines the size of a http file 87-- determines the size of a http file
87function gethttpsize(url) 88function 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
92end 93end
93 94
94-- downloads a file using the http protocol 95-- downloads a file using the http protocol
95function getbyhttp(url, file) 96function 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
101end 102end
102 103
103-- downloads a file using the ftp protocol 104-- downloads a file using the ftp protocol
104function getbyftp(url, file) 105function 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
114end 115end
115 116
116-- determines the scheme 117-- determines the scheme
117function getscheme(url) 118function 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
122end 123end
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>
125function get(url, name) 126function 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
131end 132end
132 133
133-- main program 134-- main program
134arg = arg or {} 135arg = arg or {}
135if table.getn(arg) < 1 then 136if 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)
138else get(arg[1], arg[2]) end 139else 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 @@
1require"http" 1socket = require("socket")
2http = require("http")
2 3
3if not arg or not arg[1] or not arg[2] then 4if 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
31local host = socket.dns.gethostname() 32local host = socket.dns.gethostname()
32local query = "%s?cmd=cddb+read+%s+%s&hello=LuaSocket+%s+LuaSocket+2.0&proto=6" 33local query = "%s?cmd=cddb+read+%s+%s&hello=LuaSocket+%s+LuaSocket+2.0&proto=6"
33local url = string.format(query, server, arg[1], arg[2], host) 34local url = string.format(query, server, arg[1], arg[2], host)
34local body, headers, code, error = socket.http.get(url) 35local body, headers, code, error = http.get(url)
35 36
36if code == 200 then 37if 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-----------------------------------------------------------------------------
7require"socket" 7local socket = require("socket")
8host = host or "localhost" 8host = host or "localhost"
9port = port or 7 9port = port or 7
10if arg then 10if 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-----------------------------------------------------------------------------
7require"socket" 7socket = require("socket")
8host = host or "127.0.0.1" 8host = host or "127.0.0.1"
9port = port or 7 9port = port or 7
10if arg then 10if 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-----------------------------------------------------------------------------
7require("socket") 7local socket = require("socket")
8host = host or "*" 8host = host or "*"
9port = port or 8080 9port = port or 8080
10if arg then 10if 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
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
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 @@
1require("socket") 1local socket = require("socket")
2require("ltn12") 2local ltn12 = require("ltn12")
3require("mime") 3local mime = require("mime")
4 4
5dofile("testsupport.lua") 5dofile("testsupport.lua")
6 6