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 /src | |
| parent | 63d60223da9de60f873ca08a25dbd9512c998929 (diff) | |
| download | luasocket-9ed7f955e5fc69af9bf1794fa2c8cd227981ba24.tar.gz luasocket-9ed7f955e5fc69af9bf1794fa2c8cd227981ba24.tar.bz2 luasocket-9ed7f955e5fc69af9bf1794fa2c8cd227981ba24.zip | |
Só pra não perder se der merda.
Diffstat (limited to 'src')
| -rw-r--r-- | src/ftp.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 |
17 files changed, 139 insertions, 145 deletions
diff --git a/src/ftp.lua b/src/ftp.lua index 842fdbb..79772f8 100644 --- a/src/ftp.lua +++ b/src/ftp.lua | |||
| @@ -5,21 +5,22 @@ | |||
| 5 | -- Conforming to: RFC 959, LTN7 | 5 | -- Conforming to: RFC 959, LTN7 |
| 6 | -- RCS ID: $Id$ | 6 | -- RCS ID: $Id$ |
| 7 | ----------------------------------------------------------------------------- | 7 | ----------------------------------------------------------------------------- |
| 8 | -- make sure LuaSocket is loaded | 8 | |
| 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 | ||
