mirror of
https://github.com/lunarmodules/luasocket.git
synced 2025-07-16 05:49:53 +02:00
Final push for release...
This commit is contained in:
12
src/ftp.lua
12
src/ftp.lua
@ -17,6 +17,7 @@ local url = require("socket.url")
|
||||
local tp = require("socket.tp")
|
||||
local ltn12 = require("ltn12")
|
||||
module("socket.ftp")
|
||||
getmetatable(_M).__index = nil
|
||||
|
||||
-----------------------------------------------------------------------------
|
||||
-- Program constants
|
||||
@ -35,8 +36,8 @@ PASSWORD = "anonymous@anonymous.org"
|
||||
-----------------------------------------------------------------------------
|
||||
local metat = { __index = {} }
|
||||
|
||||
function open(server, port)
|
||||
local tp = socket.try(tp.connect(server, port or PORT, TIMEOUT))
|
||||
function open(server, port, create)
|
||||
local tp = socket.try(tp.connect(server, port or PORT, create, TIMEOUT))
|
||||
local f = base.setmetatable({ tp = tp }, metat)
|
||||
-- make sure everything gets closed in an exception
|
||||
f.try = socket.newtry(function() f:close() end)
|
||||
@ -199,7 +200,7 @@ end
|
||||
local function tput(putt)
|
||||
putt = override(putt)
|
||||
socket.try(putt.host, "missing hostname")
|
||||
local f = open(putt.host, putt.port)
|
||||
local f = open(putt.host, putt.port, putt.create)
|
||||
f:greet()
|
||||
f:login(putt.user, putt.password)
|
||||
if putt.type then f:type(putt.type) end
|
||||
@ -242,7 +243,7 @@ end)
|
||||
local function tget(gett)
|
||||
gett = override(gett)
|
||||
socket.try(gett.host, "missing hostname")
|
||||
local f = open(gett.host, gett.port)
|
||||
local f = open(gett.host, gett.port, gett.create)
|
||||
f:greet()
|
||||
f:login(gett.user, gett.password)
|
||||
if gett.type then f:type(gett.type) end
|
||||
@ -264,7 +265,7 @@ command = socket.protect(function(cmdt)
|
||||
cmdt = override(cmdt)
|
||||
socket.try(cmdt.host, "missing hostname")
|
||||
socket.try(cmdt.command, "missing command")
|
||||
local f = open(cmdt.host, cmdt.port)
|
||||
local f = open(cmdt.host, cmdt.port, cmdt.create)
|
||||
f:greet()
|
||||
f:login(cmdt.user, cmdt.password)
|
||||
f.try(f.tp:command(cmdt.command, cmdt.argument))
|
||||
@ -278,4 +279,3 @@ get = socket.protect(function(gett)
|
||||
else return tget(gett) end
|
||||
end)
|
||||
|
||||
--getmetatable(_M).__index = nil
|
||||
|
45
src/http.lua
45
src/http.lua
@ -16,6 +16,7 @@ local string = require("string")
|
||||
local base = _G
|
||||
local table = require("table")
|
||||
module("socket.http")
|
||||
getmetatable(_M).__index = nil
|
||||
|
||||
-----------------------------------------------------------------------------
|
||||
-- Program constants
|
||||
@ -105,26 +106,16 @@ end
|
||||
-----------------------------------------------------------------------------
|
||||
local metat = { __index = {} }
|
||||
|
||||
-- default connect function, respecting the timeout
|
||||
local function connect(host, port, create)
|
||||
local c, e = (create or socket.tcp)()
|
||||
if not c then return nil, e end
|
||||
c:settimeout(TIMEOUT)
|
||||
local r, e = c:connect(host, port or PORT)
|
||||
if not r then
|
||||
c:close()
|
||||
return nil, e
|
||||
end
|
||||
return c
|
||||
end
|
||||
|
||||
function open(host, port, create)
|
||||
-- create socket with user connect function, or with default
|
||||
local c = socket.try(connect(host, port, create))
|
||||
-- create our http request object, pointing to the socket
|
||||
local c = socket.try(create or socket.tcp)()
|
||||
local h = base.setmetatable({ c = c }, metat)
|
||||
-- make sure the object close gets called on exception
|
||||
-- create finalized try
|
||||
h.try = socket.newtry(function() h:close() end)
|
||||
-- set timeout before connecting
|
||||
h.try(c:settimeout(TIMEOUT))
|
||||
h.try(c:connect(host, port or PORT))
|
||||
-- here everything worked
|
||||
return h
|
||||
end
|
||||
|
||||
@ -134,11 +125,11 @@ function metat.__index:sendrequestline(method, uri)
|
||||
end
|
||||
|
||||
function metat.__index:sendheaders(headers)
|
||||
local h = "\r\n"
|
||||
for i, v in base.pairs(headers) do
|
||||
self.try(self.c:send(i .. ": " .. v .. "\r\n"))
|
||||
h = i .. ": " .. v .. "\r\n" .. h
|
||||
end
|
||||
-- mark end of request headers
|
||||
self.try(self.c:send("\r\n"))
|
||||
self.try(self.c:send(h))
|
||||
return 1
|
||||
end
|
||||
|
||||
@ -213,7 +204,7 @@ local function adjustheaders(headers, host)
|
||||
["te"] = "trailers"
|
||||
}
|
||||
-- override with user headers
|
||||
for i,v in pairs(headers or lower) do
|
||||
for i,v in base.pairs(headers or lower) do
|
||||
lower[string.lower(i)] = v
|
||||
end
|
||||
return lower
|
||||
@ -232,7 +223,7 @@ local function adjustrequest(reqt)
|
||||
local nreqt = reqt.url and url.parse(reqt.url, default) or {}
|
||||
local t = url.parse(reqt.url, default)
|
||||
-- explicit components override url
|
||||
for i,v in pairs(reqt) do nreqt[i] = v end
|
||||
for i,v in base.pairs(reqt) do nreqt[i] = v end
|
||||
socket.try(nreqt.host, "invalid host '" .. base.tostring(nreqt.host) .. "'")
|
||||
-- compute uri if user hasn't overriden
|
||||
nreqt.uri = reqt.uri or adjusturi(nreqt)
|
||||
@ -276,11 +267,11 @@ function tauthorize(reqt)
|
||||
return trequest(reqt)
|
||||
end
|
||||
|
||||
function tredirect(reqt, headers)
|
||||
return trequest {
|
||||
function tredirect(reqt, location)
|
||||
local result, code, headers, status = trequest {
|
||||
-- the RFC says the redirect URL has to be absolute, but some
|
||||
-- servers do not respect that
|
||||
url = url.absolute(reqt, headers["location"]),
|
||||
url = url.absolute(reqt, location),
|
||||
source = reqt.source,
|
||||
sink = reqt.sink,
|
||||
headers = reqt.headers,
|
||||
@ -288,6 +279,9 @@ function tredirect(reqt, headers)
|
||||
nredirects = (reqt.nredirects or 0) + 1,
|
||||
connect = reqt.connect
|
||||
}
|
||||
-- pass location header back as a hint we redirected
|
||||
headers.location = headers.location or location
|
||||
return result, code, headers, status
|
||||
end
|
||||
|
||||
function trequest(reqt)
|
||||
@ -301,7 +295,7 @@ function trequest(reqt)
|
||||
headers = h:receiveheaders()
|
||||
if shouldredirect(reqt, code, headers) then
|
||||
h:close()
|
||||
return tredirect(reqt, headers)
|
||||
return tredirect(reqt, headers.location)
|
||||
elseif shouldauthorize(reqt, code) then
|
||||
h:close()
|
||||
return tauthorize(reqt)
|
||||
@ -332,4 +326,3 @@ request = socket.protect(function(reqt, body)
|
||||
else return trequest(reqt) end
|
||||
end)
|
||||
|
||||
--getmetatable(_M).__index = nil
|
||||
|
@ -12,6 +12,7 @@ local string = require("string")
|
||||
local table = require("table")
|
||||
local base = _G
|
||||
module("ltn12")
|
||||
getmetatable(_M).__index = nil
|
||||
|
||||
filter = {}
|
||||
source = {}
|
||||
@ -134,8 +135,6 @@ function source.rewind(src)
|
||||
end
|
||||
end
|
||||
|
||||
local print = print
|
||||
|
||||
-- chains a source with a filter
|
||||
function source.chain(src, f)
|
||||
base.assert(src and f)
|
||||
@ -258,7 +257,8 @@ end
|
||||
function pump.step(src, snk)
|
||||
local chunk, src_err = src()
|
||||
local ret, snk_err = snk(chunk, src_err)
|
||||
return chunk and ret and not src_err and not snk_err, src_err or snk_err
|
||||
if chunk and ret then return 1
|
||||
else return nil, src_err or snk_err end
|
||||
end
|
||||
|
||||
-- pumps all data from a source to a sink, using a step function
|
||||
@ -267,8 +267,10 @@ function pump.all(src, snk, step)
|
||||
step = step or pump.step
|
||||
while true do
|
||||
local ret, err = step(src, snk)
|
||||
if not ret then return not err, err end
|
||||
if not ret then
|
||||
if err then return nil, err
|
||||
else return 1 end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
--getmetatable(_M).__index = nil
|
||||
|
@ -108,7 +108,7 @@ static int base_open(lua_State *L) {
|
||||
/*-------------------------------------------------------------------------*\
|
||||
* Initializes all library modules.
|
||||
\*-------------------------------------------------------------------------*/
|
||||
LUASOCKET_API int luaopen_socketcore(lua_State *L) {
|
||||
LUASOCKET_API int luaopen_socket_core(lua_State *L) {
|
||||
int i;
|
||||
base_open(L);
|
||||
for (i = 0; mod[i].name; i++) mod[i].func(L);
|
||||
|
87
src/makefile
Normal file
87
src/makefile
Normal file
@ -0,0 +1,87 @@
|
||||
#------
|
||||
# Load configuration
|
||||
#
|
||||
include ../config
|
||||
|
||||
#------
|
||||
# Hopefully no need to change anything below this line
|
||||
#
|
||||
|
||||
#------
|
||||
# Modules belonging to socket-core
|
||||
#
|
||||
SOCKET_OBJS:= \
|
||||
luasocket.o \
|
||||
timeout.o \
|
||||
buffer.o \
|
||||
io.o \
|
||||
auxiliar.o \
|
||||
options.o \
|
||||
inet.o \
|
||||
tcp.o \
|
||||
udp.o \
|
||||
except.o \
|
||||
select.o \
|
||||
$(COMPAT)/compat-5.1.o \
|
||||
usocket.o
|
||||
|
||||
#------
|
||||
# Modules belonging mime-core
|
||||
#
|
||||
MIME_OBJS:=\
|
||||
mime.o \
|
||||
$(COMPAT)/compat-5.1.o
|
||||
|
||||
#------
|
||||
# Modules belonging unix (local domain sockets)
|
||||
#
|
||||
UNIX_OBJS:=\
|
||||
buffer.o \
|
||||
auxiliar.o \
|
||||
options.o \
|
||||
timeout.o \
|
||||
io.o \
|
||||
usocket.o \
|
||||
unix.o
|
||||
|
||||
all: $(SOCKET_SO) $(MIME_SO)
|
||||
|
||||
$(SOCKET_SO): $(SOCKET_OBJS)
|
||||
$(LD) $(LDFLAGS) -o $@ $^
|
||||
|
||||
$(MIME_SO): $(MIME_OBJS)
|
||||
$(LD) $(LDFLAGS) -o $@ $^
|
||||
|
||||
$(UNIX_SO): $(UNIX_OBJS)
|
||||
$(LD) $(LDFLAGS) -o $@ $^
|
||||
|
||||
#------
|
||||
# List of dependencies
|
||||
#
|
||||
auxiliar.o: auxiliar.c auxiliar.h
|
||||
buffer.o: buffer.c buffer.h io.h timeout.h
|
||||
except.o: except.c except.h
|
||||
inet.o: inet.c inet.h socket.h io.h timeout.h usocket.h
|
||||
io.o: io.c io.h timeout.h
|
||||
luasocket.o: luasocket.c luasocket.h auxiliar.h except.h timeout.h \
|
||||
buffer.h io.h inet.h socket.h usocket.h tcp.h udp.h select.h
|
||||
mime.o: mime.c mime.h
|
||||
options.o: options.c auxiliar.h options.h socket.h io.h timeout.h \
|
||||
usocket.h inet.h
|
||||
select.o: select.c socket.h io.h timeout.h usocket.h select.h
|
||||
tcp.o: tcp.c auxiliar.h socket.h io.h timeout.h usocket.h inet.h \
|
||||
options.h tcp.h buffer.h
|
||||
timeout.o: timeout.c auxiliar.h timeout.h
|
||||
udp.o: udp.c auxiliar.h socket.h io.h timeout.h usocket.h inet.h \
|
||||
options.h udp.h
|
||||
unix.o: unix.c auxiliar.h socket.h io.h timeout.h usocket.h options.h \
|
||||
unix.h buffer.h
|
||||
usocket.o: usocket.c socket.h io.h timeout.h usocket.h
|
||||
|
||||
clean:
|
||||
rm -f $(SOCKET_SO) $(SOCKET_OBJS)
|
||||
rm -f $(MIME_SO) $(UNIX_SO) $(MIME_OBJS) $(UNIX_OBJS)
|
||||
|
||||
#------
|
||||
# End of makefile configuration
|
||||
#
|
@ -14,6 +14,7 @@ local mime = require("mime.core")
|
||||
local io = require("io")
|
||||
local string = require("string")
|
||||
module("mime")
|
||||
getmetatable(_M).__index = nil
|
||||
|
||||
-- encode, decode and wrap algorithm tables
|
||||
encodet = {}
|
||||
@ -84,5 +85,3 @@ end
|
||||
function stuff()
|
||||
return ltn12.filter.cycle(dot, 2)
|
||||
end
|
||||
|
||||
--getmetatable(_M).__index = nil
|
||||
|
29
src/smtp.lua
29
src/smtp.lua
@ -18,6 +18,7 @@ local tp = require("socket.tp")
|
||||
local ltn12 = require("ltn12")
|
||||
local mime = require("mime")
|
||||
module("socket.smtp")
|
||||
getmetatable(_M).__index = nil
|
||||
|
||||
-----------------------------------------------------------------------------
|
||||
-- Program constants
|
||||
@ -111,12 +112,12 @@ function metat.__index:send(mailt)
|
||||
self:data(ltn12.source.chain(mailt.source, mime.stuff()), mailt.step)
|
||||
end
|
||||
|
||||
function open(server, port)
|
||||
local tp = socket.try(tp.connect(server or SERVER, port or PORT, TIMEOUT))
|
||||
function open(server, port, create)
|
||||
local tp = socket.try(tp.connect(server or SERVER, port or PORT,
|
||||
create, TIMEOUT))
|
||||
local s = base.setmetatable({tp = tp}, metat)
|
||||
-- make sure tp is closed if we get an exception
|
||||
s.try = socket.newtry(function()
|
||||
if s.tp:command("QUIT") then s.tp:check("2..") end
|
||||
s:close()
|
||||
end)
|
||||
return s
|
||||
@ -165,10 +166,9 @@ end
|
||||
local function send_source(mesgt)
|
||||
-- set content-type if user didn't override
|
||||
if not mesgt.headers or not mesgt.headers["content-type"] then
|
||||
coroutine.yield('content-type: text/plain; charset="iso-8859-1"\r\n')
|
||||
end
|
||||
coroutine.yield('content-type: text/plain; charset="iso-8859-1"\r\n\r\n')
|
||||
else coroutine.yield("\r\n") end
|
||||
-- finish headers
|
||||
coroutine.yield("\r\n")
|
||||
-- send body from source
|
||||
while true do
|
||||
local chunk, err = mesgt.body()
|
||||
@ -182,21 +182,20 @@ end
|
||||
local function send_string(mesgt)
|
||||
-- set content-type if user didn't override
|
||||
if not mesgt.headers or not mesgt.headers["content-type"] then
|
||||
coroutine.yield('content-type: text/plain; charset="iso-8859-1"\r\n')
|
||||
end
|
||||
-- finish headers
|
||||
coroutine.yield("\r\n")
|
||||
coroutine.yield('content-type: text/plain; charset="iso-8859-1"\r\n\r\n')
|
||||
else coroutine.yield("\r\n") end
|
||||
-- send body from string
|
||||
coroutine.yield(mesgt.body)
|
||||
|
||||
end
|
||||
|
||||
-- yield the headers one by one
|
||||
-- yield the headers all at once
|
||||
local function send_headers(mesgt)
|
||||
if mesgt.headers then
|
||||
local h = ""
|
||||
for i,v in base.pairs(mesgt.headers) do
|
||||
coroutine.yield(i .. ':' .. v .. "\r\n")
|
||||
h = i .. ': ' .. v .. "\r\n" .. h
|
||||
end
|
||||
coroutine.yield(h)
|
||||
end
|
||||
end
|
||||
|
||||
@ -237,12 +236,10 @@ end
|
||||
-- High level SMTP API
|
||||
-----------------------------------------------------------------------------
|
||||
send = socket.protect(function(mailt)
|
||||
local s = open(mailt.server, mailt.port)
|
||||
local s = open(mailt.server, mailt.port, mailt.create)
|
||||
local ext = s:greet(mailt.domain)
|
||||
s:auth(mailt.user, mailt.password, ext)
|
||||
s:send(mailt)
|
||||
s:quit()
|
||||
return s:close()
|
||||
end)
|
||||
|
||||
--getmetatable(_M).__index = nil
|
||||
|
@ -12,6 +12,7 @@ local string = require("string")
|
||||
local math = require("math")
|
||||
local socket = require("socket.core")
|
||||
module("socket")
|
||||
getmetatable(_M).__index = nil
|
||||
|
||||
-----------------------------------------------------------------------------
|
||||
-- Exported auxiliar functions
|
||||
@ -131,5 +132,3 @@ sourcet["default"] = sourcet["until-closed"]
|
||||
|
||||
source = choose(sourcet)
|
||||
|
||||
-- clear globals from namespace
|
||||
getmetatable(_M).__index = nil
|
||||
|
70
src/ssl.c
70
src/ssl.c
@ -1,70 +0,0 @@
|
||||
/*=========================================================================*\
|
||||
* Simple client SSL support
|
||||
* LuaSocket toolkit
|
||||
*
|
||||
* RCS ID: $Id$
|
||||
\*=========================================================================*/
|
||||
#include <lua.h>
|
||||
#include <lauxlib.h>
|
||||
|
||||
#include "ssl.h"
|
||||
|
||||
/*=========================================================================*\
|
||||
* Internal function prototypes
|
||||
\*=========================================================================*/
|
||||
static int global_wrap(lua_State *L);
|
||||
|
||||
/* functions in library namespace */
|
||||
static luaL_reg func[] = {
|
||||
{"wrap", global_create},
|
||||
{NULL, NULL}
|
||||
};
|
||||
|
||||
static luaL_reg wrap[] = {
|
||||
{"__tostring", aux_tostring},
|
||||
{"__gc", meth_close},
|
||||
{"close", meth_close},
|
||||
{"receive", meth_receive},
|
||||
{"send", meth_send},
|
||||
{NULL, NULL}
|
||||
};
|
||||
|
||||
static luaL_reg owned[] = {
|
||||
{"__tostring", aux_tostring},
|
||||
{NULL, NULL}
|
||||
};
|
||||
|
||||
/*-------------------------------------------------------------------------*\
|
||||
* Initializes module
|
||||
\*-------------------------------------------------------------------------*/
|
||||
int ssl_open(lua_State *L)
|
||||
{
|
||||
aux_newclass(L, "ssl{wraper}", wrap);
|
||||
aux_newclass(L, "ssl{owned}", owned);
|
||||
lua_pushstring(L, "ssl")
|
||||
lua_newtable(L);
|
||||
luaL_openlib(L, NULL, func, 0);
|
||||
lua_settable(L, -3);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*=========================================================================*\
|
||||
* Library functions
|
||||
\*=========================================================================*/
|
||||
/*-------------------------------------------------------------------------*\
|
||||
* Wraps a tcp object into an SSL object
|
||||
\*-------------------------------------------------------------------------*/
|
||||
static int global_wrap(lua_State *L) {
|
||||
p_tcp tcp = (p_tcp) aux_checkclass(L, "tcp{client}", 1);
|
||||
/* change class of tcp object */
|
||||
aux_setclass(L, "ssl{owned}", 1);
|
||||
/* create wrapper */
|
||||
p_wrap wrap = (p_wrap) lua_newuserdata(L, sizeof(t_wrap));
|
||||
/* lock reference */
|
||||
lua_pushvalue(L, 1);
|
||||
wrap->ref = lua_ref(L, 1);
|
||||
/* initialize wrapper */
|
||||
wrap->tcp = tcp;
|
||||
io_init(&tcp->io, wrap_send, wrap_recv, wrap);
|
||||
return 1;
|
||||
}
|
29
src/ssl.h
29
src/ssl.h
@ -1,29 +0,0 @@
|
||||
#ifndef SSL_H
|
||||
#define SSL_H
|
||||
/*=========================================================================*\
|
||||
* Simple client SSL support
|
||||
* LuaSocket toolkit
|
||||
*
|
||||
* This is just a simple example to show how to extend LuaSocket
|
||||
*
|
||||
* RCS ID: $Id$
|
||||
\*=========================================================================*/
|
||||
#include <lua.h>
|
||||
#include <openssl/ssl.h>
|
||||
|
||||
#include "buffer.h"
|
||||
#include "timeout.h"
|
||||
#include "socket.h"
|
||||
#include "tcp.h"
|
||||
|
||||
typedef struct t_wrap_ {
|
||||
p_tcp tcp;
|
||||
SSL* ssl;
|
||||
int ref;
|
||||
} t_wrap;
|
||||
|
||||
typedef t_wrap *p_wrap;
|
||||
|
||||
int ssl_open(lua_State *L);
|
||||
|
||||
#endif /* SSL_H */
|
@ -13,6 +13,7 @@ local string = require("string")
|
||||
local socket = require("socket")
|
||||
local ltn12 = require("ltn12")
|
||||
module("socket.tp")
|
||||
getmetatable(_M).__index = nil
|
||||
|
||||
-----------------------------------------------------------------------------
|
||||
-- Program constants
|
||||
@ -98,7 +99,8 @@ end
|
||||
|
||||
function metat.__index:source(source, step)
|
||||
local sink = socket.sink("keep-open", self.c)
|
||||
return ltn12.pump.all(source, sink, step or ltn12.pump.step)
|
||||
local ret, err = ltn12.pump.all(source, sink, step or ltn12.pump.step)
|
||||
return ret, err
|
||||
end
|
||||
|
||||
-- closes the underlying c
|
||||
@ -108,8 +110,8 @@ function metat.__index:close()
|
||||
end
|
||||
|
||||
-- connect with server and return c object
|
||||
function connect(host, port, timeout)
|
||||
local c, e = socket.tcp()
|
||||
function connect(host, port, create, timeout)
|
||||
local c, e = (create or socket.tcp())
|
||||
if not c then return nil, e end
|
||||
c:settimeout(timeout or TIMEOUT)
|
||||
local r, e = c:connect(host, port)
|
||||
@ -120,4 +122,3 @@ function connect(host, port, timeout)
|
||||
return base.setmetatable({c = c}, metat)
|
||||
end
|
||||
|
||||
--getmetatable(_M).__index = nil
|
||||
|
@ -12,6 +12,7 @@ local string = require("string")
|
||||
local base = _G
|
||||
local table = require("table")
|
||||
module("socket.url")
|
||||
getmetatable(_M).__index = nil
|
||||
|
||||
-----------------------------------------------------------------------------
|
||||
-- Encodes a string into its escaped hexadecimal representation
|
||||
@ -279,4 +280,3 @@ function build_path(parsed, unsafe)
|
||||
return path
|
||||
end
|
||||
|
||||
--getmetatable(_M).__index = nil
|
||||
|
@ -74,7 +74,10 @@ int sock_select(int n, fd_set *rfds, fd_set *wfds, fd_set *efds, p_tm tm) {
|
||||
double t = tm_get(tm);
|
||||
tv.tv_sec = (int) t;
|
||||
tv.tv_usec = (int) ((t - tv.tv_sec) * 1.0e6);
|
||||
return select(0, rfds, wfds, efds, t >= 0.0? &tv: NULL);
|
||||
if (n <= 0) {
|
||||
Sleep(1000*t);
|
||||
return 0;
|
||||
} else return select(0, rfds, wfds, efds, t >= 0.0? &tv: NULL);
|
||||
}
|
||||
|
||||
/*-------------------------------------------------------------------------*\
|
||||
|
Reference in New Issue
Block a user