mirror of
https://github.com/lunarmodules/luasocket.git
synced 2024-12-26 04:28:20 +01:00
Só pra não perder se der merda.
This commit is contained in:
parent
63d60223da
commit
9ed7f955e5
@ -1,7 +1,7 @@
|
||||
This directory contains code that is more useful than the examples. This code
|
||||
*is* supported.
|
||||
|
||||
lua.lua and luasocket.lua
|
||||
lua.lua
|
||||
|
||||
These are modules to suport dynamic loading of LuaSocket by the stand alone
|
||||
Lua Interpreter with the use of the "require" function. For my Mac OS X
|
||||
|
@ -1,5 +1,5 @@
|
||||
require("ltn12")
|
||||
require("mime")
|
||||
local ltn12 = require("ltn12")
|
||||
local mime = require("mime")
|
||||
local source = ltn12.source.file(io.stdin)
|
||||
local sink = ltn12.sink.file(io.stdout)
|
||||
local convert
|
||||
|
@ -4,15 +4,14 @@
|
||||
-- Author: Diego Nehab
|
||||
-- RCS ID: $Id$
|
||||
-----------------------------------------------------------------------------
|
||||
|
||||
require"http"
|
||||
|
||||
socket.http.TIMEOUT = 10
|
||||
local http = require("http")
|
||||
local url = require("url")
|
||||
http.TIMEOUT = 10
|
||||
|
||||
cache = {}
|
||||
|
||||
function readfile(path)
|
||||
path = socket.url.unescape(path)
|
||||
path = url.unescape(path)
|
||||
local file, error = io.open(path, "r")
|
||||
if file then
|
||||
local body = file:read("*a")
|
||||
@ -21,32 +20,32 @@ function readfile(path)
|
||||
else return nil, error end
|
||||
end
|
||||
|
||||
function getstatus(url)
|
||||
local parsed = socket.url.parse(url, { scheme = "file" })
|
||||
if cache[url] then return cache[url] end
|
||||
function getstatus(u)
|
||||
local parsed = url.parse(u, {scheme = "file"})
|
||||
if cache[u] then return cache[u] end
|
||||
local res
|
||||
if parsed.scheme == "http" then
|
||||
local request = { url = url, method = "HEAD" }
|
||||
local response = socket.http.request(request)
|
||||
local request = {url = u, method = "HEAD"}
|
||||
local response = http.request(request)
|
||||
if response.code == 200 then res = nil
|
||||
else res = response.status or response.error end
|
||||
elseif parsed.scheme == "file" then
|
||||
local file, error = io.open(socket.url.unescape(parsed.path), "r")
|
||||
local file, error = io.open(url.unescape(parsed.path), "r")
|
||||
if file then
|
||||
file:close()
|
||||
res = nil
|
||||
else res = error end
|
||||
else res = string.format("unhandled scheme '%s'", parsed.scheme) end
|
||||
cache[url] = res
|
||||
cache[u] = res
|
||||
return res
|
||||
end
|
||||
|
||||
function retrieve(url)
|
||||
local parsed = socket.url.parse(url, { scheme = "file" })
|
||||
function retrieve(u)
|
||||
local parsed = url.parse(u, { scheme = "file" })
|
||||
local body, headers, code, error
|
||||
local base = url
|
||||
local base = u
|
||||
if parsed.scheme == "http" then
|
||||
body, headers, code, error = socket.http.get(url)
|
||||
body, headers, code, error = http.get(u)
|
||||
if code == 200 then
|
||||
base = base or headers.location
|
||||
end
|
||||
@ -62,19 +61,19 @@ function getlinks(body, base)
|
||||
local links = {}
|
||||
-- extract links
|
||||
body = string.gsub(body, '[Hh][Rr][Ee][Ff]%s*=%s*"([^"]*)"', function(href)
|
||||
table.insert(links, socket.url.absolute(base, href))
|
||||
table.insert(links, url.absolute(base, href))
|
||||
end)
|
||||
body = string.gsub(body, "[Hh][Rr][Ee][Ff]%s*=%s*'([^']*)'", function(href)
|
||||
table.insert(links, socket.url.absolute(base, href))
|
||||
table.insert(links, url.absolute(base, href))
|
||||
end)
|
||||
string.gsub(body, "[Hh][Rr][Ee][Ff]%s*=%s*(.-)>", function(href)
|
||||
table.insert(links, socket.url.absolute(base, href))
|
||||
table.insert(links, url.absolute(base, href))
|
||||
end)
|
||||
return links
|
||||
end
|
||||
|
||||
function checklinks(url)
|
||||
local base, body, error = retrieve(url)
|
||||
function checklinks(u)
|
||||
local base, body, error = retrieve(u)
|
||||
if not body then print(error) return end
|
||||
local links = getlinks(body, base)
|
||||
for _, l in ipairs(links) do
|
||||
@ -91,5 +90,5 @@ if table.getn(arg) < 1 then
|
||||
end
|
||||
for _, a in ipairs(arg) do
|
||||
print("Checking ", a)
|
||||
checklinks(socket.url.absolute("file:", a))
|
||||
checklinks(url.absolute("file:", a))
|
||||
end
|
||||
|
@ -4,7 +4,7 @@
|
||||
-- Author: Diego Nehab
|
||||
-- RCS ID: $Id$
|
||||
-----------------------------------------------------------------------------
|
||||
require"socket"
|
||||
local socket = require("socket")
|
||||
|
||||
function get_status(sock, valid)
|
||||
local line, err = sock:receive()
|
||||
@ -77,5 +77,5 @@ if arg and arg[1] then
|
||||
defs, err = dict_get(arg[1], arg[2])
|
||||
print(defs or err)
|
||||
else
|
||||
io.write("Usage:\n luasocket dict.lua <word> [<dictionary>]\n")
|
||||
io.write("Usage:\n lua dict.lua <word> [<dictionary>]\n")
|
||||
end
|
||||
|
@ -1,6 +1,5 @@
|
||||
require"mime.lua"
|
||||
require"ltn12.lua"
|
||||
|
||||
local mime = require("mime")
|
||||
local ltn12 = require("ltn12")
|
||||
local marker = '\n'
|
||||
if arg and arg[1] == '-d' then marker = '\r\n' end
|
||||
local filter = mime.normalize(marker)
|
||||
|
39
etc/get.lua
39
etc/get.lua
@ -4,9 +4,10 @@
|
||||
-- Author: Diego Nehab
|
||||
-- RCS ID: $Id$
|
||||
-----------------------------------------------------------------------------
|
||||
require"http"
|
||||
require"ftp"
|
||||
require"url"
|
||||
socket = require("socket")
|
||||
http = require("http")
|
||||
ftp = require("ftp")
|
||||
url = require("url")
|
||||
|
||||
-- formats a number of seconds into human readable form
|
||||
function nicetime(s)
|
||||
@ -84,55 +85,55 @@ function stats(size)
|
||||
end
|
||||
|
||||
-- determines the size of a http file
|
||||
function gethttpsize(url)
|
||||
local respt = socket.http.request {method = "HEAD", url = url}
|
||||
function gethttpsize(u)
|
||||
local respt = http.request {method = "HEAD", url = u}
|
||||
if respt.code == 200 then
|
||||
return tonumber(respt.headers["content-length"])
|
||||
end
|
||||
end
|
||||
|
||||
-- downloads a file using the http protocol
|
||||
function getbyhttp(url, file)
|
||||
function getbyhttp(u, file)
|
||||
local save = ltn12.sink.file(file or io.stdout)
|
||||
-- only print feedback if output is not stdout
|
||||
if file then save = ltn12.sink.chain(stats(gethttpsize(url)), save) end
|
||||
local respt = socket.http.request {url = url, sink = save }
|
||||
if file then save = ltn12.sink.chain(stats(gethttpsize(u)), save) end
|
||||
local respt = http.request {url = u, sink = save }
|
||||
if respt.code ~= 200 then print(respt.status or respt.error) end
|
||||
end
|
||||
|
||||
-- downloads a file using the ftp protocol
|
||||
function getbyftp(url, file)
|
||||
function getbyftp(u, file)
|
||||
local save = ltn12.sink.file(file or io.stdout)
|
||||
-- only print feedback if output is not stdout
|
||||
-- and we don't know how big the file is
|
||||
if file then save = ltn12.sink.chain(stats(), save) end
|
||||
local gett = socket.url.parse(url)
|
||||
local gett = url.parse(u)
|
||||
gett.sink = save
|
||||
gett.type = "i"
|
||||
local ret, err = socket.ftp.get(gett)
|
||||
local ret, err = ftp.get(gett)
|
||||
if err then print(err) end
|
||||
end
|
||||
|
||||
-- determines the scheme
|
||||
function getscheme(url)
|
||||
function getscheme(u)
|
||||
-- this is an heuristic to solve a common invalid url poblem
|
||||
if not string.find(url, "//") then url = "//" .. url end
|
||||
local parsed = socket.url.parse(url, {scheme = "http"})
|
||||
if not string.find(u, "//") then u = "//" .. u end
|
||||
local parsed = url.parse(u, {scheme = "http"})
|
||||
return parsed.scheme
|
||||
end
|
||||
|
||||
-- gets a file either by http or ftp, saving as <name>
|
||||
function get(url, name)
|
||||
function get(u, name)
|
||||
local fout = name and io.open(name, "wb")
|
||||
local scheme = getscheme(url)
|
||||
if scheme == "ftp" then getbyftp(url, fout)
|
||||
elseif scheme == "http" then getbyhttp(url, fout)
|
||||
local scheme = getscheme(u)
|
||||
if scheme == "ftp" then getbyftp(u, fout)
|
||||
elseif scheme == "http" then getbyhttp(u, fout)
|
||||
else print("unknown scheme" .. scheme) end
|
||||
end
|
||||
|
||||
-- main program
|
||||
arg = arg or {}
|
||||
if table.getn(arg) < 1 then
|
||||
io.write("Usage:\n luasocket get.lua <remote-url> [<local-file>]\n")
|
||||
io.write("Usage:\n lua get.lua <remote-url> [<local-file>]\n")
|
||||
os.exit(1)
|
||||
else get(arg[1], arg[2]) end
|
||||
|
@ -1,4 +1,5 @@
|
||||
require"http"
|
||||
socket = require("socket")
|
||||
http = require("http")
|
||||
|
||||
if not arg or not arg[1] or not arg[2] then
|
||||
print("luasocket cddb.lua <category> <disc-id> [<server>]")
|
||||
@ -31,7 +32,7 @@ end
|
||||
local host = socket.dns.gethostname()
|
||||
local query = "%s?cmd=cddb+read+%s+%s&hello=LuaSocket+%s+LuaSocket+2.0&proto=6"
|
||||
local url = string.format(query, server, arg[1], arg[2], host)
|
||||
local body, headers, code, error = socket.http.get(url)
|
||||
local body, headers, code, error = http.get(url)
|
||||
|
||||
if code == 200 then
|
||||
local data, code, error = parse(body)
|
||||
|
@ -4,7 +4,7 @@
|
||||
-- Author: Diego Nehab
|
||||
-- RCS ID: $Id$
|
||||
-----------------------------------------------------------------------------
|
||||
require"socket"
|
||||
local socket = require("socket")
|
||||
host = host or "localhost"
|
||||
port = port or 7
|
||||
if arg then
|
||||
|
@ -4,7 +4,7 @@
|
||||
-- Author: Diego Nehab
|
||||
-- RCS ID: $Id$
|
||||
-----------------------------------------------------------------------------
|
||||
require"socket"
|
||||
socket = require("socket")
|
||||
host = host or "127.0.0.1"
|
||||
port = port or 7
|
||||
if arg then
|
||||
|
@ -4,7 +4,7 @@
|
||||
-- Author: Diego Nehab
|
||||
-- RCS ID: $Id$
|
||||
-----------------------------------------------------------------------------
|
||||
require("socket")
|
||||
local socket = require("socket")
|
||||
host = host or "*"
|
||||
port = port or 8080
|
||||
if arg then
|
||||
|
37
src/ftp.lua
37
src/ftp.lua
@ -5,21 +5,22 @@
|
||||
-- Conforming to: RFC 959, LTN7
|
||||
-- RCS ID: $Id$
|
||||
-----------------------------------------------------------------------------
|
||||
-- make sure LuaSocket is loaded
|
||||
require("socket")
|
||||
-- get LuaSocket namespace
|
||||
local socket = _G[LUASOCKET_LIBNAME]
|
||||
|
||||
-- require other modules
|
||||
require("ltn12")
|
||||
require("url")
|
||||
require("tp")
|
||||
-----------------------------------------------------------------------------
|
||||
-- Load other required modules
|
||||
-----------------------------------------------------------------------------
|
||||
local socket = require("socket")
|
||||
local ltn12 = require("ltn12")
|
||||
local url = require("url")
|
||||
local tp = require("tp")
|
||||
|
||||
-- create namespace inside LuaSocket namespace
|
||||
socket.ftp = socket.ftp or {}
|
||||
-----------------------------------------------------------------------------
|
||||
-- Setup namespace
|
||||
-----------------------------------------------------------------------------
|
||||
local ftp = {}
|
||||
-- make all module globals fall into namespace
|
||||
setmetatable(socket.ftp, { __index = _G })
|
||||
setfenv(1, socket.ftp)
|
||||
setmetatable(ftp, { __index = _G })
|
||||
setfenv(1, ftp)
|
||||
|
||||
-----------------------------------------------------------------------------
|
||||
-- Program constants
|
||||
@ -196,8 +197,8 @@ local default = {
|
||||
scheme = "ftp"
|
||||
}
|
||||
|
||||
local function parse(url)
|
||||
local putt = socket.try(socket.url.parse(url, default))
|
||||
local function parse(u)
|
||||
local putt = socket.try(url.parse(u, default))
|
||||
socket.try(putt.scheme == "ftp", "invalid scheme '" .. putt.scheme .. "'")
|
||||
socket.try(putt.host, "invalid host")
|
||||
local pat = "^type=(.)$"
|
||||
@ -208,8 +209,8 @@ local function parse(url)
|
||||
return putt
|
||||
end
|
||||
|
||||
local function sput(url, body)
|
||||
local putt = parse(url)
|
||||
local function sput(u, body)
|
||||
local putt = parse(u)
|
||||
putt.source = ltn12.source.string(body)
|
||||
return tput(putt)
|
||||
end
|
||||
@ -230,8 +231,8 @@ local function tget(gett)
|
||||
return ftp:close()
|
||||
end
|
||||
|
||||
local function sget(url, body)
|
||||
local gett = parse(url)
|
||||
local function sget(u, body)
|
||||
local gett = parse(u)
|
||||
local t = {}
|
||||
gett.sink = ltn12.sink.table(t)
|
||||
tget(gett)
|
||||
|
57
src/http.lua
57
src/http.lua
@ -5,23 +5,22 @@
|
||||
-- Conforming to RFC 2616
|
||||
-- RCS ID: $Id$
|
||||
-----------------------------------------------------------------------------
|
||||
-- make sure LuaSocket is loaded
|
||||
require("socket")
|
||||
-- get LuaSocket namespace
|
||||
local socket = _G[LUASOCKET_LIBNAME]
|
||||
|
||||
-- require other modules
|
||||
require("ltn12")
|
||||
require("mime")
|
||||
-- get MIME namespace
|
||||
local mime = _G[MIME_LIBNAME]
|
||||
require("url")
|
||||
-----------------------------------------------------------------------------
|
||||
-- Load other required modules
|
||||
-------------------------------------------------------------------------------
|
||||
local socket = require("socket")
|
||||
local ltn12 = require("ltn12")
|
||||
local mime = require("mime")
|
||||
local url = require("url")
|
||||
|
||||
-- create namespace inside LuaSocket namespace
|
||||
socket.http = socket.http or {}
|
||||
-----------------------------------------------------------------------------
|
||||
-- Setup namespace
|
||||
-------------------------------------------------------------------------------
|
||||
http = {}
|
||||
-- make all module globals fall into namespace
|
||||
setmetatable(socket.http, { __index = _G })
|
||||
setfenv(1, socket.http)
|
||||
setmetatable(http, { __index = _G })
|
||||
setfenv(1, http)
|
||||
|
||||
-----------------------------------------------------------------------------
|
||||
-- Program constants
|
||||
@ -116,17 +115,17 @@ local function receive_status(reqt, respt, tmp)
|
||||
end
|
||||
|
||||
local function request_uri(reqt, respt, tmp)
|
||||
local url = tmp.parsed
|
||||
local u = tmp.parsed
|
||||
if not reqt.proxy then
|
||||
local parsed = tmp.parsed
|
||||
url = {
|
||||
u = {
|
||||
path = parsed.path,
|
||||
params = parsed.params,
|
||||
query = parsed.query,
|
||||
fragment = parsed.fragment
|
||||
}
|
||||
end
|
||||
return socket.url.build(url)
|
||||
return url.build(u)
|
||||
end
|
||||
|
||||
local function send_request(reqt, respt, tmp)
|
||||
@ -155,7 +154,7 @@ local function open(reqt, respt, tmp)
|
||||
local proxy = reqt.proxy or PROXY
|
||||
local host, port
|
||||
if proxy then
|
||||
local pproxy = socket.url.parse(proxy)
|
||||
local pproxy = url.parse(proxy)
|
||||
socket.try(pproxy.port and pproxy.host, "invalid proxy")
|
||||
host, port = pproxy.host, pproxy.port
|
||||
else
|
||||
@ -169,15 +168,13 @@ end
|
||||
|
||||
local function adjust_headers(reqt, respt, tmp)
|
||||
local lower = {}
|
||||
local headers = reqt.headers or {}
|
||||
-- set default headers
|
||||
lower["user-agent"] = USERAGENT
|
||||
-- override with user values
|
||||
for i,v in headers do
|
||||
for i,v in (reqt.headers or lower) do
|
||||
lower[string.lower(i)] = v
|
||||
end
|
||||
lower["user-agent"] = lower["user-agent"] or USERAGENT
|
||||
-- these cannot be overriden
|
||||
lower["host"] = tmp.parsed.host
|
||||
-- this cannot be overriden
|
||||
lower["connection"] = "close"
|
||||
-- store results
|
||||
tmp.headers = lower
|
||||
@ -185,7 +182,7 @@ end
|
||||
|
||||
local function parse_url(reqt, respt, tmp)
|
||||
-- parse url with default fields
|
||||
local parsed = socket.url.parse(reqt.url, {
|
||||
local parsed = url.parse(reqt.url, {
|
||||
host = "",
|
||||
port = PORT,
|
||||
path ="/",
|
||||
@ -250,7 +247,7 @@ local function redirect(reqt, respt, tmp)
|
||||
method = reqt.method,
|
||||
-- the RFC says the redirect URL has to be absolute, but some
|
||||
-- servers do not respect that
|
||||
url = socket.url.absolute(reqt.url, respt.headers["location"]),
|
||||
url = url.absolute(reqt.url, respt.headers["location"]),
|
||||
source = reqt.source,
|
||||
sink = reqt.sink,
|
||||
headers = reqt.headers,
|
||||
@ -296,20 +293,20 @@ function request(reqt)
|
||||
return respt
|
||||
end
|
||||
|
||||
function get(url)
|
||||
function get(u)
|
||||
local t = {}
|
||||
respt = request {
|
||||
url = url,
|
||||
url = u,
|
||||
sink = ltn12.sink.table(t)
|
||||
}
|
||||
return (table.getn(t) > 0 or nil) and table.concat(t), respt.headers,
|
||||
respt.code, respt.error
|
||||
end
|
||||
|
||||
function post(url, body)
|
||||
function post(u, body)
|
||||
local t = {}
|
||||
respt = request {
|
||||
url = url,
|
||||
url = u,
|
||||
method = "POST",
|
||||
source = ltn12.source.string(body),
|
||||
sink = ltn12.sink.table(t),
|
||||
@ -318,3 +315,5 @@ function post(url, body)
|
||||
return (table.getn(t) > 0 or nil) and table.concat(t),
|
||||
respt.headers, respt.code, respt.error
|
||||
end
|
||||
|
||||
return http
|
||||
|
@ -37,13 +37,10 @@ static luaL_reg func[] = {
|
||||
\*-------------------------------------------------------------------------*/
|
||||
int inet_open(lua_State *L)
|
||||
{
|
||||
lua_pushstring(L, LUASOCKET_LIBNAME);
|
||||
lua_gettable(L, LUA_GLOBALSINDEX);
|
||||
lua_pushstring(L, "dns");
|
||||
lua_newtable(L);
|
||||
luaL_openlib(L, NULL, func, 0);
|
||||
lua_settable(L, -3);
|
||||
lua_pop(L, 1);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -1,10 +1,16 @@
|
||||
-- create module namespace
|
||||
ltn12 = ltn12 or {}
|
||||
-- make all globals fall into ltn12 namespace
|
||||
-----------------------------------------------------------------------------
|
||||
-- LTN12 - Filters, sources, sinks and pumps.
|
||||
-- LuaSocket toolkit.
|
||||
-- Author: Diego Nehab
|
||||
-- RCS ID: $Id$
|
||||
-----------------------------------------------------------------------------
|
||||
|
||||
-----------------------------------------------------------------------------
|
||||
-- Setup namespace
|
||||
-----------------------------------------------------------------------------
|
||||
local ltn12 = {}
|
||||
setmetatable(ltn12, { __index = _G })
|
||||
setfenv(1, ltn12)
|
||||
|
||||
-- sub namespaces
|
||||
filter = {}
|
||||
source = {}
|
||||
sink = {}
|
||||
@ -13,15 +19,14 @@ pump = {}
|
||||
-- 2048 seems to be better in windows...
|
||||
BLOCKSIZE = 2048
|
||||
|
||||
local function second(a, b)
|
||||
return b
|
||||
end
|
||||
|
||||
local function shift(a, b, c)
|
||||
return b, c
|
||||
end
|
||||
|
||||
-- returns a high level filter that cycles a cycles a low-level filter
|
||||
-----------------------------------------------------------------------------
|
||||
-- Filter stuff
|
||||
-----------------------------------------------------------------------------
|
||||
-- returns a high level filter that cycles a low-level filter
|
||||
function filter.cycle(low, ctx, extra)
|
||||
return function(chunk)
|
||||
local ret
|
||||
@ -61,6 +66,9 @@ function filter.chain(...)
|
||||
return f
|
||||
end
|
||||
|
||||
-----------------------------------------------------------------------------
|
||||
-- Source stuff
|
||||
-----------------------------------------------------------------------------
|
||||
-- create an empty source
|
||||
local function empty()
|
||||
return nil
|
||||
@ -162,6 +170,9 @@ function source.cat(...)
|
||||
end
|
||||
end
|
||||
|
||||
-----------------------------------------------------------------------------
|
||||
-- Sink stuff
|
||||
-----------------------------------------------------------------------------
|
||||
-- creates a sink that stores into a table
|
||||
function sink.table(t)
|
||||
t = t or {}
|
||||
@ -224,6 +235,9 @@ function sink.chain(f, snk)
|
||||
end
|
||||
end
|
||||
|
||||
-----------------------------------------------------------------------------
|
||||
-- Pump stuff
|
||||
-----------------------------------------------------------------------------
|
||||
-- pumps one chunk from the source to the sink
|
||||
function pump.step(src, snk)
|
||||
local chunk, src_err = src()
|
||||
@ -239,3 +253,5 @@ function pump.all(src, snk, step)
|
||||
if not ret then return not err, err end
|
||||
end
|
||||
end
|
||||
|
||||
return ltn12
|
||||
|
@ -33,7 +33,6 @@
|
||||
#include "tcp.h"
|
||||
#include "udp.h"
|
||||
#include "select.h"
|
||||
#include "smtp.h"
|
||||
|
||||
/*-------------------------------------------------------------------------*\
|
||||
* Modules
|
||||
@ -47,7 +46,6 @@ static const luaL_reg mod[] = {
|
||||
{"tcp", tcp_open},
|
||||
{"udp", udp_open},
|
||||
{"select", select_open},
|
||||
{"smtp", smtp_open},
|
||||
{NULL, NULL}
|
||||
};
|
||||
|
||||
@ -56,7 +54,6 @@ static const luaL_reg mod[] = {
|
||||
\*-------------------------------------------------------------------------*/
|
||||
LUASOCKET_API int luaopen_socket(lua_State *L) {
|
||||
int i;
|
||||
for (i = 0; mod[i].name; i++)
|
||||
mod[i].func(L);
|
||||
for (i = 0; mod[i].name; i++) mod[i].func(L);
|
||||
return 1;
|
||||
}
|
||||
|
@ -15,13 +15,6 @@
|
||||
\*-------------------------------------------------------------------------*/
|
||||
#define LUASOCKET_VERSION "LuaSocket 2.0 (beta)"
|
||||
|
||||
/*-------------------------------------------------------------------------*\
|
||||
* Library's namespace
|
||||
\*-------------------------------------------------------------------------*/
|
||||
#ifndef LUASOCKET_LIBNAME
|
||||
#define LUASOCKET_LIBNAME "socket"
|
||||
#endif
|
||||
|
||||
/*-------------------------------------------------------------------------*\
|
||||
* This macro prefixes all exported API functions
|
||||
\*-------------------------------------------------------------------------*/
|
||||
|
@ -76,9 +76,8 @@ static UC b64unbase[256];
|
||||
\*-------------------------------------------------------------------------*/
|
||||
MIME_API int luaopen_mime(lua_State *L)
|
||||
{
|
||||
lua_pushstring(L, MIME_LIBNAME);
|
||||
lua_setglobal(L, "MIME_LIBNAME");
|
||||
luaL_openlib(L, MIME_LIBNAME, func, 0);
|
||||
lua_newtable(L);
|
||||
luaL_openlib(L, NULL, func, 0);
|
||||
/* initialize lookup tables */
|
||||
qpsetup(qpclass, qpunbase);
|
||||
b64setup(b64unbase);
|
||||
@ -626,7 +625,6 @@ static int eolprocess(int c, int last, const char *marker,
|
||||
luaL_putchar(buffer, c);
|
||||
return 0;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/*-------------------------------------------------------------------------*\
|
||||
|
@ -21,11 +21,4 @@
|
||||
|
||||
MIME_API int luaopen_mime(lua_State *L);
|
||||
|
||||
/*-------------------------------------------------------------------------*\
|
||||
* Library's namespace
|
||||
\*-------------------------------------------------------------------------*/
|
||||
#ifndef MIME_LIBNAME
|
||||
#define MIME_LIBNAME "mime"
|
||||
#endif
|
||||
|
||||
#endif /* MIME_H */
|
||||
|
16
src/mime.lua
16
src/mime.lua
@ -9,19 +9,17 @@
|
||||
-- Load MIME from dynamic library
|
||||
-- Comment these lines if you are loading static
|
||||
-----------------------------------------------------------------------------
|
||||
open, err1, err2 = loadlib("mime", "luaopen_mime")
|
||||
if not open then error(err1) end
|
||||
open()
|
||||
if not MIME_LIBNAME then error("MIME init failed") end
|
||||
local open = assert(loadlib("mime", "luaopen_mime"))
|
||||
local mime = assert(open())
|
||||
|
||||
-----------------------------------------------------------------------------
|
||||
-- Namespace independence
|
||||
-- Load other required modules
|
||||
-----------------------------------------------------------------------------
|
||||
local mime = _G[MIME_LIBNAME]
|
||||
if not mime then error('MIME init FAILED') end
|
||||
|
||||
require("ltn12")
|
||||
local ltn12 = require("ltn12")
|
||||
|
||||
-----------------------------------------------------------------------------
|
||||
-- Setup namespace
|
||||
-----------------------------------------------------------------------------
|
||||
-- make all module globals fall into mime namespace
|
||||
setmetatable(mime, { __index = _G })
|
||||
setfenv(1, mime)
|
||||
|
@ -50,8 +50,7 @@ int select_open(lua_State *L)
|
||||
#else
|
||||
lua_dofile(L, "select.lua");
|
||||
#endif
|
||||
luaL_openlib(L, LUASOCKET_LIBNAME, func, 1);
|
||||
lua_pop(L, 1);
|
||||
luaL_openlib(L, NULL, func, 1);
|
||||
aux_newclass(L, "select{fd_set}", set);
|
||||
return 0;
|
||||
}
|
||||
|
41
src/smtp.lua
41
src/smtp.lua
@ -5,22 +5,28 @@
|
||||
-- Conforming to RFC 2821
|
||||
-- RCS ID: $Id$
|
||||
-----------------------------------------------------------------------------
|
||||
-- make sure LuaSocket is loaded
|
||||
require("socket")
|
||||
-- get LuaSocket namespace
|
||||
local socket = _G[LUASOCKET_LIBNAME]
|
||||
|
||||
require("ltn12")
|
||||
require("tp")
|
||||
-----------------------------------------------------------------------------
|
||||
-- Load SMTP from dynamic library
|
||||
-- Comment these lines if you are loading static
|
||||
-----------------------------------------------------------------------------
|
||||
local open = assert(loadlib("smtp", "luaopen_smtp"))
|
||||
local smtp = assert(open())
|
||||
|
||||
-- create smtp namespace inside LuaSocket namespace
|
||||
local smtp = socket.smtp or {}
|
||||
socket.smtp = smtp
|
||||
-----------------------------------------------------------------------------
|
||||
-- Load other required modules
|
||||
-----------------------------------------------------------------------------
|
||||
local socket = require("socket")
|
||||
local ltn12 = require("ltn12")
|
||||
local tp = require("tp")
|
||||
|
||||
-----------------------------------------------------------------------------
|
||||
-- Setup namespace
|
||||
-----------------------------------------------------------------------------
|
||||
-- make all module globals fall into smtp namespace
|
||||
setmetatable(smtp, { __index = _G })
|
||||
setfenv(1, smtp)
|
||||
|
||||
|
||||
-- default server used to send e-mails
|
||||
SERVER = "localhost"
|
||||
-- default port
|
||||
@ -89,7 +95,7 @@ end
|
||||
|
||||
function open(server, port)
|
||||
print(server or SERVER, port or PORT)
|
||||
local tp, error = socket.tp.connect(server or SERVER, port or PORT)
|
||||
local tp, error = tp.connect(server or SERVER, port or PORT)
|
||||
if not tp then return nil, error end
|
||||
return setmetatable({tp = tp}, metat)
|
||||
end
|
||||
@ -176,11 +182,16 @@ end
|
||||
|
||||
-- set defaul headers
|
||||
local function adjust_headers(mesgt)
|
||||
mesgt.headers = mesgt.headers or {}
|
||||
mesgt.headers["mime-version"] = "1.0"
|
||||
mesgt.headers["date"] = mesgt.headers["date"] or
|
||||
local lower = {}
|
||||
for i,v in (mesgt or lower) do
|
||||
lower[string.lower(i)] = v
|
||||
end
|
||||
lower["date"] = lower["date"] or
|
||||
os.date("!%a, %d %b %Y %H:%M:%S ") .. (mesgt.zone or ZONE)
|
||||
mesgt.headers["x-mailer"] = mesgt.headers["x-mailer"] or socket.version
|
||||
lower["x-mailer"] = lower["x-mailer"] or socket.version
|
||||
-- this can't be overriden
|
||||
lower["mime-version"] = "1.0"
|
||||
mesgt.headers = lower
|
||||
end
|
||||
|
||||
function message(mesgt)
|
||||
|
@ -6,18 +6,9 @@
|
||||
|
||||
-----------------------------------------------------------------------------
|
||||
-- Load LuaSocket from dynamic library
|
||||
-- Comment these lines if you are loading static
|
||||
-----------------------------------------------------------------------------
|
||||
open, err1, err2 = loadlib("luasocket", "luaopen_socket")
|
||||
if not open then error(err1) end
|
||||
open()
|
||||
if not LUASOCKET_LIBNAME then error("LuaSocket init failed") end
|
||||
|
||||
-----------------------------------------------------------------------------
|
||||
-- Namespace independence
|
||||
-----------------------------------------------------------------------------
|
||||
local socket = _G[LUASOCKET_LIBNAME]
|
||||
if not socket then error('LuaSocket init failed') end
|
||||
local open = assert(loadlib("luasocket", "luaopen_socket"))
|
||||
local socket = assert(open())
|
||||
|
||||
-----------------------------------------------------------------------------
|
||||
-- Auxiliar functions
|
||||
@ -172,3 +163,5 @@ end
|
||||
socket.sourcet["default"] = socket.sourcet["until-closed"]
|
||||
|
||||
socket.source = socket.choose(socket.sourcet)
|
||||
|
||||
return socket
|
||||
|
@ -96,8 +96,7 @@ int tcp_open(lua_State *L)
|
||||
aux_add2group(L, "tcp{client}", "select{able}");
|
||||
aux_add2group(L, "tcp{server}", "select{able}");
|
||||
/* define library functions */
|
||||
luaL_openlib(L, LUASOCKET_LIBNAME, func, 0);
|
||||
lua_pop(L, 1);
|
||||
luaL_openlib(L, NULL, func, 0);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -143,8 +143,7 @@ int tm_gettime(void)
|
||||
\*-------------------------------------------------------------------------*/
|
||||
int tm_open(lua_State *L)
|
||||
{
|
||||
luaL_openlib(L, LUASOCKET_LIBNAME, func, 0);
|
||||
lua_pop(L, 1);
|
||||
luaL_openlib(L, NULL, func, 0);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
22
src/tp.lua
22
src/tp.lua
@ -5,16 +5,18 @@
|
||||
-- Conforming to: RFC 2616, LTN7
|
||||
-- RCS ID: $Id$
|
||||
-----------------------------------------------------------------------------
|
||||
-- make sure LuaSocket is loaded
|
||||
require("socket")
|
||||
-- get LuaSocket namespace
|
||||
local socket = _G[LUASOCKET_LIBNAME]
|
||||
|
||||
-- create namespace inside LuaSocket namespace
|
||||
socket.tp = socket.tp or {}
|
||||
-- make all module globals fall into namespace
|
||||
setmetatable(socket.tp, { __index = _G })
|
||||
setfenv(1, socket.tp)
|
||||
-----------------------------------------------------------------------------
|
||||
-- Load other required modules
|
||||
-----------------------------------------------------------------------------
|
||||
local socket = require("socket")
|
||||
|
||||
-----------------------------------------------------------------------------
|
||||
-- Setup namespace
|
||||
-----------------------------------------------------------------------------
|
||||
tp = {}
|
||||
setmetatable(tp, { __index = _G })
|
||||
setfenv(1, tp)
|
||||
|
||||
TIMEOUT = 60
|
||||
|
||||
@ -107,3 +109,5 @@ function connect(host, port)
|
||||
control:settimeout(TIMEOUT)
|
||||
return setmetatable({control = control}, metat)
|
||||
end
|
||||
|
||||
return tp
|
||||
|
@ -90,8 +90,7 @@ int udp_open(lua_State *L)
|
||||
aux_add2group(L, "udp{connected}", "select{able}");
|
||||
aux_add2group(L, "udp{unconnected}", "select{able}");
|
||||
/* define library functions */
|
||||
luaL_openlib(L, LUASOCKET_LIBNAME, func, 0);
|
||||
lua_pop(L, 1);
|
||||
luaL_openlib(L, NULL, func, 0);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
16
src/url.lua
16
src/url.lua
@ -4,16 +4,12 @@
|
||||
-- Author: Diego Nehab
|
||||
-- Conforming to: RFC 2396, LTN7
|
||||
-- RCS ID: $Id$
|
||||
----------------------------------------------------------------------------
|
||||
-- make sure LuaSocket is loaded
|
||||
require("socket")
|
||||
-- get LuaSocket namespace
|
||||
local socket = _G[LUASOCKET_LIBNAME]
|
||||
-----------------------------------------------------------------------------
|
||||
|
||||
-- create url namespace inside LuaSocket namespace
|
||||
local url = socket.url or {}
|
||||
socket.url = url
|
||||
-- make all module globals fall into url namespace
|
||||
-----------------------------------------------------------------------------
|
||||
-- Setup namespace
|
||||
-----------------------------------------------------------------------------
|
||||
local url = {}
|
||||
setmetatable(url, { __index = _G })
|
||||
setfenv(1, url)
|
||||
|
||||
@ -275,3 +271,5 @@ function build_path(parsed, unsafe)
|
||||
if parsed.is_absolute then path = "/" .. path end
|
||||
return path
|
||||
end
|
||||
|
||||
return url
|
||||
|
@ -1,6 +1,6 @@
|
||||
require("socket")
|
||||
require("ltn12")
|
||||
require("mime")
|
||||
local socket = require("socket")
|
||||
local ltn12 = require("ltn12")
|
||||
local mime = require("mime")
|
||||
|
||||
dofile("testsupport.lua")
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user