mirror of
https://github.com/lunarmodules/luasocket.git
synced 2024-12-25 12:08:21 +01:00
Almost ready to release.
This commit is contained in:
parent
773e35ced3
commit
a32c6d9140
12
TODO
12
TODO
@ -1,3 +1,7 @@
|
||||
|
||||
get rid of setmetatable(, nil) since packages don't need this anymore in
|
||||
5.1
|
||||
|
||||
new instalation scheme???
|
||||
|
||||
test empty socket.select no windows.
|
||||
@ -15,14 +19,7 @@ change ltn13 to make sure drawbacks are obvious
|
||||
use mike's "don't set to blocking before closing unless needed" patch?
|
||||
take a look at DB's smtp patch (add "extra argument" table)
|
||||
|
||||
optmize aux_getgroupudata (Mike idea)
|
||||
make aux_newclass receive upvalues
|
||||
use one upvalue per string name of class/group
|
||||
make aux_checkgroup by upvalue (faster)
|
||||
|
||||
add error message stuff to the manual
|
||||
|
||||
make sure all modules that can use it actually use socket.newtry
|
||||
adicionar exemplos de expansão: pipe, local, named pipe
|
||||
testar os options!
|
||||
- Thread-safe
|
||||
@ -30,6 +27,7 @@ testar os options!
|
||||
- proteger ou atomizar o conjunto (timedout, receive), (timedout, send)
|
||||
- inet_ntoa também é uma merda.
|
||||
|
||||
* protect doesn't catch errors by error and assert
|
||||
* BUG NO SET DO TINYIRC!!! SINISTRO.
|
||||
* _VERSION, _DEBUG, etc.
|
||||
* talk about new create field in HTTP, FTP and SMTP
|
||||
|
2
config
2
config
@ -27,8 +27,6 @@ COMPAT=compat-5.1r4
|
||||
# Top of your Lua installation
|
||||
# Relative paths will be inside the src tree
|
||||
#
|
||||
#INSTALL_TOP_LUA=/usr/local/share/lua/5.0
|
||||
#INSTALL_TOP_LIB=/usr/local/lib/lua/5.0
|
||||
INSTALL_TOP_LUA=share
|
||||
INSTALL_TOP_LIB=lib
|
||||
|
||||
|
@ -63,24 +63,28 @@ the package scheme will likely already have been answered.
|
||||
|
||||
<h3>Directory structure</h3>
|
||||
|
||||
<p> The standard distribution reserves a directory to be the root of
|
||||
the libraries installed
|
||||
on a given system. Let's call this directory <tt><ROOT></tt>.
|
||||
On my system, this is the <tt>/usr/local/share/lua/5.0</tt> directory.
|
||||
Here is the standard LuaSocket distribution directory structure:</p>
|
||||
<p> On Unix systems, the standard distribution uses two base
|
||||
directories, one for system dependent files, and another for system
|
||||
independent files. Let's call these directories <tt><LIB></tt>
|
||||
and <tt><SHARE></tt>, respectively.
|
||||
For instance, in my laptop, I use '<tt>/usr/local/lib/lua/5.0</tt>' for
|
||||
<tt><LIB></tt> and '<tt>/usr/local/share/lua/5.0</tt>' for
|
||||
<tt><SHARE></tt>. On Windows, only one directory is used, say
|
||||
'<tt>c:\program files\lua\5.0</tt>'. Here is the standard LuaSocket
|
||||
distribution directory structure:</p>
|
||||
|
||||
<pre class=example>
|
||||
<ROOT>/compat-5.1.lua
|
||||
<ROOT>/ltn12.lua
|
||||
<ROOT>/mime/init.lua
|
||||
<ROOT>/mime/core.dll
|
||||
<ROOT>/socket/init.lua
|
||||
<ROOT>/socket/core.dll
|
||||
<ROOT>/socket/http.lua
|
||||
<ROOT>/socket/tp.lua
|
||||
<ROOT>/socket/ftp.lua
|
||||
<ROOT>/socket/smtp.lua
|
||||
<ROOT>/socket/url.lua
|
||||
<SHARE>/compat-5.1.lua
|
||||
<SHARE>/ltn12.lua
|
||||
<SHARE>/mime/init.lua
|
||||
<LIB>/mime/core.dll
|
||||
<SHARE>/socket/init.lua
|
||||
<LIB>/socket/core.dll
|
||||
<SHARE>/socket/http.lua
|
||||
<SHARE>/socket/tp.lua
|
||||
<SHARE>/socket/ftp.lua
|
||||
<SHARE>/socket/smtp.lua
|
||||
<SHARE>/socket/url.lua
|
||||
</pre>
|
||||
|
||||
<p> Naturally, on Unix systems, <tt>core.dll</tt>
|
||||
@ -91,7 +95,7 @@ environment variables need to be set. The first environment variable tells
|
||||
the interpreter to load the <tt>compat-5.1.lua</tt> module at startup: </p>
|
||||
|
||||
<pre class=example>
|
||||
LUA_INIT=@<ROOT>/compat-5.1.lua
|
||||
LUA_INIT=@<SHARE>/compat-5.1.lua
|
||||
</pre>
|
||||
|
||||
<p>
|
||||
@ -101,13 +105,12 @@ directories and with the appropriate filename extensions.
|
||||
</p>
|
||||
|
||||
<pre class=example>
|
||||
LUA_PATH=<ROOT>/?.lua;?.lua
|
||||
LUA_CPATH=<ROOT>/?.dll;?.dll
|
||||
LUA_PATH=<SHARE>/?.lua;?.lua
|
||||
LUA_CPATH=<LIB>/?.dll;?.dll
|
||||
</pre>
|
||||
|
||||
<p> Again, naturally, in Unix the shared library extension would be
|
||||
<tt>.so</tt> instead of <tt>.dll</tt> and on Mac OS X it would be
|
||||
<tt>.dylib</tt></p>
|
||||
<p> Again, naturally, on Unix systmems the shared library extension would be
|
||||
<tt>.so</tt> instead of <tt>.dll</tt></p>
|
||||
|
||||
<h3>Using LuaSocket</h3>
|
||||
|
||||
@ -118,8 +121,8 @@ it should be easy to use LuaSocket. Just fire the interpreter and use the
|
||||
<pre class=example>
|
||||
Lua 5.0.2 Copyright (C) 1994-2004 Tecgraf, PUC-Rio
|
||||
> socket = require("socket")
|
||||
> print(socket.VERSION)
|
||||
--> LuaSocket 2.0 (beta3)
|
||||
> print(socket._VERSION)
|
||||
--> LuaSocket 2.0
|
||||
</pre>
|
||||
|
||||
<p> Each module loads their dependencies automatically, so you only need to
|
||||
@ -128,7 +131,8 @@ load the modues you directly depend upon: <p>
|
||||
<pre class=example>
|
||||
Lua 5.0.2 Copyright (C) 1994-2004 Tecgraf, PUC-Rio
|
||||
> http = require("socket.http")
|
||||
> print(http.request("http://www.tecgraf.puc-rio.br/luasocket"))
|
||||
>
|
||||
print(http.request("http://www.cs.princeton.edu/~diego/professional/luasocket"))
|
||||
--> homepage gets dumped to terminal
|
||||
</pre>
|
||||
|
||||
|
@ -1,277 +0,0 @@
|
||||
-----------------------------------------------------------------------------
|
||||
-- Little program that checks links in HTML files, using coroutines and
|
||||
-- non-blocking I/O. Thus, faster than simpler version of same program
|
||||
-- LuaSocket sample files
|
||||
-- Author: Diego Nehab
|
||||
-- RCS ID: $$
|
||||
-----------------------------------------------------------------------------
|
||||
local socket = require("socket")
|
||||
|
||||
TIMEOUT = 10
|
||||
|
||||
-- we need to yield across calls to protect, so we can't use pcall
|
||||
-- we borrow and simplify code from coxpcall to reimplement socket.protect
|
||||
-- before loading http
|
||||
function socket.protect(f)
|
||||
return function(...)
|
||||
local co = coroutine.create(f)
|
||||
while true do
|
||||
local results = {coroutine.resume(co, unpack(arg))}
|
||||
local status = results[1]
|
||||
table.remove(results, 1)
|
||||
if not status then
|
||||
return nil, results[1][1]
|
||||
end
|
||||
if coroutine.status(co) == "suspended" then
|
||||
arg = {coroutine.yield(unpack(results))}
|
||||
else
|
||||
return unpack(results)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
local http = require("socket.http")
|
||||
local url = require("socket.url")
|
||||
|
||||
-- creates a new set data structure
|
||||
function newset()
|
||||
local reverse = {}
|
||||
local set = {}
|
||||
return setmetatable(set, {__index = {
|
||||
insert = function(set, value)
|
||||
if not reverse[value] then
|
||||
table.insert(set, value)
|
||||
reverse[value] = table.getn(set)
|
||||
end
|
||||
end,
|
||||
remove = function(set, value)
|
||||
local index = reverse[value]
|
||||
if index then
|
||||
reverse[value] = nil
|
||||
local top = table.remove(set)
|
||||
if top ~= value then
|
||||
reverse[top] = index
|
||||
set[index] = top
|
||||
end
|
||||
end
|
||||
end
|
||||
}})
|
||||
end
|
||||
|
||||
local context = {}
|
||||
local sending = newset()
|
||||
local receiving = newset()
|
||||
local nthreads = 0
|
||||
|
||||
-- socket.tcp() replacement for non-blocking I/O
|
||||
-- implements enough functionality to be used with http.request
|
||||
-- in Lua 5.1, we have coroutine.running to simplify things...
|
||||
function newcreate(thread)
|
||||
return function()
|
||||
-- try to create underlying socket
|
||||
local tcp, error = socket.tcp()
|
||||
if not tcp then return nil, error end
|
||||
-- put it in non-blocking mode right away
|
||||
tcp:settimeout(0)
|
||||
local trap = {
|
||||
-- we ignore settimeout to preserve our 0 timeout
|
||||
settimeout = function(self, mode, value)
|
||||
return 1
|
||||
end,
|
||||
-- send in non-blocking mode and yield on timeout
|
||||
send = function(self, data, first, last)
|
||||
first = (first or 1) - 1
|
||||
local result, error
|
||||
while true do
|
||||
-- tell dispatcher we want to keep sending before we
|
||||
-- yield control
|
||||
sending:insert(tcp)
|
||||
-- return control to dispatcher
|
||||
-- if upon return the dispatcher tells us we timed out,
|
||||
-- return an error to whoever called us
|
||||
if coroutine.yield() == "timeout" then
|
||||
return nil, "timeout"
|
||||
end
|
||||
-- mark time we started waiting
|
||||
context[tcp].last = socket.gettime()
|
||||
-- try sending
|
||||
result, error, first = tcp:send(data, first+1, last)
|
||||
-- if we are done, or there was an unexpected error,
|
||||
-- break away from loop
|
||||
if error ~= "timeout" then return result, error, first end
|
||||
end
|
||||
end,
|
||||
-- receive in non-blocking mode and yield on timeout
|
||||
receive = function(self, pattern)
|
||||
local error, partial = "timeout", ""
|
||||
local value
|
||||
while true do
|
||||
-- tell dispatcher we want to keep receiving before we
|
||||
-- yield control
|
||||
receiving:insert(tcp)
|
||||
-- return control to dispatcher
|
||||
-- if upon return the dispatcher tells us we timed out,
|
||||
-- return an error to whoever called us
|
||||
if coroutine.yield() == "timeout" then
|
||||
return nil, "timeout"
|
||||
end
|
||||
-- mark time we started waiting
|
||||
context[tcp].last = socket.gettime()
|
||||
-- try receiving
|
||||
value, error, partial = tcp:receive(pattern, partial)
|
||||
-- if we are done, or there was an unexpected error,
|
||||
-- break away from loop
|
||||
if error ~= "timeout" then return value, error, partial end
|
||||
end
|
||||
end,
|
||||
-- connect in non-blocking mode and yield on timeout
|
||||
connect = function(self, host, port)
|
||||
local result, error = tcp:connect(host, port)
|
||||
-- mark time we started waiting
|
||||
context[tcp].last = socket.gettime()
|
||||
if error == "timeout" then
|
||||
-- tell dispatcher we will be able to write uppon connection
|
||||
sending:insert(tcp)
|
||||
-- return control to dispatcher
|
||||
-- if upon return the dispatcher tells us we have a
|
||||
-- timeout, just abort
|
||||
if coroutine.yield() == "timeout" then
|
||||
return nil, "timeout"
|
||||
end
|
||||
-- when we come back, check if connection was successful
|
||||
result, error = tcp:connect(host, port)
|
||||
if result or error == "already connected" then return 1
|
||||
else return nil, "non-blocking connect failed" end
|
||||
else return result, error end
|
||||
end,
|
||||
close = function(self)
|
||||
context[tcp] = nil
|
||||
return tcp:close()
|
||||
end
|
||||
}
|
||||
-- add newly created socket to context
|
||||
context[tcp] = {
|
||||
thread = thread,
|
||||
trap = trap
|
||||
}
|
||||
return trap
|
||||
end
|
||||
end
|
||||
|
||||
-- get the status of a URL, non-blocking
|
||||
function getstatus(link)
|
||||
local parsed = url.parse(link, {scheme = "file"})
|
||||
if parsed.scheme == "http" then
|
||||
local thread = coroutine.create(function(thread, link)
|
||||
local r, c, h, s = http.request{
|
||||
method = "HEAD",
|
||||
url = link,
|
||||
create = newcreate(thread)
|
||||
}
|
||||
if c == 200 then io.write('\t', link, '\n')
|
||||
else io.write('\t', link, ': ', c, '\n') end
|
||||
nthreads = nthreads - 1
|
||||
end)
|
||||
nthreads = nthreads + 1
|
||||
assert(coroutine.resume(thread, thread, link))
|
||||
end
|
||||
end
|
||||
|
||||
-- dispatch all threads until we are done
|
||||
function dispatch()
|
||||
while nthreads > 0 do
|
||||
-- check which sockets are interesting and act on them
|
||||
local readable, writable = socket.select(receiving, sending, 1)
|
||||
-- for all readable connections, resume their threads
|
||||
for _, who in ipairs(readable) do
|
||||
if context[who] then
|
||||
receiving:remove(who)
|
||||
assert(coroutine.resume(context[who].thread))
|
||||
end
|
||||
end
|
||||
-- for all writable connections, do the same
|
||||
for _, who in ipairs(writable) do
|
||||
if context[who] then
|
||||
sending:remove(who)
|
||||
assert(coroutine.resume(context[who].thread))
|
||||
end
|
||||
end
|
||||
-- politely ask replacement I/O functions in idle threads to
|
||||
-- return reporting a timeout
|
||||
local now = socket.gettime()
|
||||
for who, data in pairs(context) do
|
||||
if data.last and now - data.last > TIMEOUT then
|
||||
sending:remove(who)
|
||||
receiving:remove(who)
|
||||
assert(coroutine.resume(context[who].thread, "timeout"))
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function readfile(path)
|
||||
path = url.unescape(path)
|
||||
local file, error = io.open(path, "r")
|
||||
if file then
|
||||
local body = file:read("*a")
|
||||
file:close()
|
||||
return body
|
||||
else return nil, error end
|
||||
end
|
||||
|
||||
function load(u)
|
||||
local parsed = url.parse(u, { scheme = "file" })
|
||||
local body, headers, code, error
|
||||
local base = u
|
||||
if parsed.scheme == "http" then
|
||||
body, code, headers = http.request(u)
|
||||
if code == 200 then
|
||||
-- if there was a redirect, update base to reflect it
|
||||
base = headers.location or base
|
||||
end
|
||||
if not body then
|
||||
error = code
|
||||
end
|
||||
elseif parsed.scheme == "file" then
|
||||
body, error = readfile(parsed.path)
|
||||
else error = string.format("unhandled scheme '%s'", parsed.scheme) end
|
||||
return base, body, error
|
||||
end
|
||||
|
||||
function getlinks(body, base)
|
||||
-- get rid of comments
|
||||
body = string.gsub(body, "%<%!%-%-.-%-%-%>", "")
|
||||
local links = {}
|
||||
-- extract links
|
||||
body = string.gsub(body, '[Hh][Rr][Ee][Ff]%s*=%s*"([^"]*)"', function(href)
|
||||
table.insert(links, url.absolute(base, href))
|
||||
end)
|
||||
body = string.gsub(body, "[Hh][Rr][Ee][Ff]%s*=%s*'([^']*)'", function(href)
|
||||
table.insert(links, url.absolute(base, href))
|
||||
end)
|
||||
string.gsub(body, "[Hh][Rr][Ee][Ff]%s*=%s*(.-)>", function(href)
|
||||
table.insert(links, url.absolute(base, href))
|
||||
end)
|
||||
return links
|
||||
end
|
||||
|
||||
function checklinks(address)
|
||||
local base, body, error = load(address)
|
||||
if not body then print(error) return end
|
||||
print("Checking ", base)
|
||||
local links = getlinks(body, base)
|
||||
for _, link in ipairs(links) do
|
||||
getstatus(link)
|
||||
end
|
||||
end
|
||||
|
||||
arg = arg or {}
|
||||
if table.getn(arg) < 1 then
|
||||
print("Usage:\n luasocket check-links.lua {<url>}")
|
||||
exit()
|
||||
end
|
||||
for _, address in ipairs(arg) do
|
||||
checklinks(url.absolute("file:", address))
|
||||
end
|
||||
dispatch()
|
@ -5,7 +5,7 @@
|
||||
-- Author: Diego Nehab
|
||||
-- RCS ID: $$
|
||||
-----------------------------------------------------------------------------
|
||||
local url = require("socket.url")
|
||||
local url = require("url")
|
||||
local dispatch = require("dispatch")
|
||||
local http = require("socket.http")
|
||||
dispatch.TIMEOUT = 10
|
||||
|
@ -7,9 +7,9 @@ function load(s)
|
||||
print(s .. ":\t " .. (b-a) .. "k")
|
||||
end
|
||||
|
||||
load("socket")
|
||||
load("socket.url")
|
||||
load("url")
|
||||
load("ltn12")
|
||||
load("socket")
|
||||
load("mime")
|
||||
load("socket.tp")
|
||||
load("socket.smtp")
|
||||
|
@ -7,7 +7,7 @@
|
||||
local socket = require("socket")
|
||||
local http = require("socket.http")
|
||||
local ftp = require("socket.ftp")
|
||||
local url = require("socket.url")
|
||||
local url = require("url")
|
||||
local ltn12 = require("ltn12")
|
||||
|
||||
-- formats a number of seconds into human readable form
|
||||
|
@ -31,8 +31,8 @@
|
||||
* RCS ID: $Id$
|
||||
\*=========================================================================*/
|
||||
|
||||
#include <lua.h>
|
||||
#include <lauxlib.h>
|
||||
#include "lua.h"
|
||||
#include "lauxlib.h"
|
||||
|
||||
int aux_open(lua_State *L);
|
||||
void aux_newclass(lua_State *L, const char *classname, luaL_reg *func);
|
||||
|
@ -4,8 +4,8 @@
|
||||
*
|
||||
* RCS ID: $Id$
|
||||
\*=========================================================================*/
|
||||
#include <lua.h>
|
||||
#include <lauxlib.h>
|
||||
#include "lua.h"
|
||||
#include "lauxlib.h"
|
||||
|
||||
#include "buffer.h"
|
||||
|
||||
|
@ -17,7 +17,7 @@
|
||||
*
|
||||
* RCS ID: $Id$
|
||||
\*=========================================================================*/
|
||||
#include <lua.h>
|
||||
#include "lua.h"
|
||||
|
||||
#include "io.h"
|
||||
#include "timeout.h"
|
||||
|
@ -5,8 +5,9 @@
|
||||
* RCS ID: $Id$
|
||||
\*=========================================================================*/
|
||||
#include <stdio.h>
|
||||
#include <lua.h>
|
||||
#include <lauxlib.h>
|
||||
|
||||
#include "lua.h"
|
||||
#include "lauxlib.h"
|
||||
|
||||
#include "except.h"
|
||||
|
||||
|
@ -28,7 +28,7 @@
|
||||
* RCS ID: $Id$
|
||||
\*=========================================================================*/
|
||||
|
||||
#include <lua.h>
|
||||
#include "lua.h"
|
||||
|
||||
int except_open(lua_State *L);
|
||||
|
||||
|
@ -7,8 +7,8 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <lua.h>
|
||||
#include <lauxlib.h>
|
||||
#include "lua.h"
|
||||
#include "lauxlib.h"
|
||||
|
||||
#include "inet.h"
|
||||
|
||||
|
@ -16,7 +16,7 @@
|
||||
*
|
||||
* RCS ID: $Id$
|
||||
\*=========================================================================*/
|
||||
#include <lua.h>
|
||||
#include "lua.h"
|
||||
#include "socket.h"
|
||||
#include "timeout.h"
|
||||
|
||||
|
1
src/io.c
1
src/io.c
@ -27,7 +27,6 @@ const char *io_strerror(int err) {
|
||||
case IO_DONE: return NULL;
|
||||
case IO_CLOSED: return "closed";
|
||||
case IO_TIMEOUT: return "timeout";
|
||||
case IO_CLIPPED: return "clipped";
|
||||
default: return "unknown error";
|
||||
}
|
||||
}
|
||||
|
5
src/io.h
5
src/io.h
@ -15,7 +15,7 @@
|
||||
* RCS ID: $Id$
|
||||
\*=========================================================================*/
|
||||
#include <stdio.h>
|
||||
#include <lua.h>
|
||||
#include "lua.h"
|
||||
|
||||
#include "timeout.h"
|
||||
|
||||
@ -24,8 +24,7 @@ enum {
|
||||
IO_DONE = 0, /* operation completed successfully */
|
||||
IO_TIMEOUT = -1, /* operation timed out */
|
||||
IO_CLOSED = -2, /* the connection has been closed */
|
||||
IO_CLIPPED = -3, /* maxium bytes count reached */
|
||||
IO_UNKNOWN = -4
|
||||
IO_UNKNOWN = -3
|
||||
};
|
||||
|
||||
/* interface to error message function */
|
||||
|
@ -17,15 +17,17 @@
|
||||
/*=========================================================================*\
|
||||
* Standard include files
|
||||
\*=========================================================================*/
|
||||
#include <lua.h>
|
||||
#include <lauxlib.h>
|
||||
#include "lua.h"
|
||||
#include "lauxlib.h"
|
||||
|
||||
#if !defined(LUA_VERSION_NUM) || (LUA_VERSION_NUM < 501)
|
||||
#include "compat-5.1.h"
|
||||
#include "luasocket.h"
|
||||
#endif
|
||||
|
||||
/*=========================================================================*\
|
||||
* LuaSocket includes
|
||||
\*=========================================================================*/
|
||||
|
||||
#include "luasocket.h"
|
||||
#include "auxiliar.h"
|
||||
#include "except.h"
|
||||
#include "timeout.h"
|
||||
|
@ -8,10 +8,10 @@
|
||||
*
|
||||
* RCS ID: $Id$
|
||||
\*=========================================================================*/
|
||||
#include <lua.h>
|
||||
#include "lua.h"
|
||||
|
||||
/*-------------------------------------------------------------------------*\
|
||||
* Current luasocket version
|
||||
* Current socket library version
|
||||
\*-------------------------------------------------------------------------*/
|
||||
#define LUASOCKET_VERSION "LuaSocket 2.0"
|
||||
#define LUASOCKET_COPYRIGHT "Copyright (C) 2004-2005 Diego Nehab"
|
||||
|
@ -11,6 +11,7 @@ include ../config
|
||||
# Modules belonging to socket-core
|
||||
#
|
||||
SOCKET_OBJS:= \
|
||||
$(COMPAT)/compat-5.1.o \
|
||||
luasocket.o \
|
||||
timeout.o \
|
||||
buffer.o \
|
||||
@ -22,15 +23,15 @@ SOCKET_OBJS:= \
|
||||
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
|
||||
$(COMPAT)/compat-5.1.o \
|
||||
mime.o
|
||||
|
||||
|
||||
#------
|
||||
# Modules belonging unix (local domain sockets)
|
||||
|
11
src/mime.c
11
src/mime.c
@ -6,10 +6,13 @@
|
||||
\*=========================================================================*/
|
||||
#include <string.h>
|
||||
|
||||
#include <lua.h>
|
||||
#include <lauxlib.h>
|
||||
#include "lua.h"
|
||||
#include "lauxlib.h"
|
||||
|
||||
#if !defined(LUA_VERSION_NUM) || (LUA_VERSION_NUM < 501)
|
||||
#include "compat-5.1.h"
|
||||
#endif
|
||||
|
||||
#include "mime.h"
|
||||
|
||||
/*=========================================================================*\
|
||||
@ -81,6 +84,10 @@ static UC b64unbase[256];
|
||||
MIME_API int luaopen_mime_core(lua_State *L)
|
||||
{
|
||||
luaL_openlib(L, "mime", func, 0);
|
||||
/* make version string available to scripts */
|
||||
lua_pushstring(L, "_VERSION");
|
||||
lua_pushstring(L, MIME_VERSION);
|
||||
lua_rawset(L, -3);
|
||||
/* initialize lookup tables */
|
||||
qpsetup(qpclass, qpunbase);
|
||||
b64setup(b64unbase);
|
||||
|
11
src/mime.h
11
src/mime.h
@ -1,7 +1,7 @@
|
||||
#ifndef MIME_H
|
||||
#define MIME_H
|
||||
/*=========================================================================*\
|
||||
* MIME support functions
|
||||
* Core MIME support
|
||||
* LuaSocket toolkit
|
||||
*
|
||||
* This module provides functions to implement transfer content encodings
|
||||
@ -10,7 +10,14 @@
|
||||
*
|
||||
* RCS ID: $Id$
|
||||
\*=========================================================================*/
|
||||
#include <lua.h>
|
||||
#include "lua.h"
|
||||
|
||||
/*-------------------------------------------------------------------------*\
|
||||
* Current MIME library version
|
||||
\*-------------------------------------------------------------------------*/
|
||||
#define MIME_VERSION "MIME 1.0"
|
||||
#define MIME_COPYRIGHT "Copyright (C) 2004-2005 Diego Nehab"
|
||||
#define MIME_AUTHORS "Diego Nehab"
|
||||
|
||||
/*-------------------------------------------------------------------------*\
|
||||
* This macro prefixes all exported API functions
|
||||
|
@ -4,9 +4,10 @@
|
||||
*
|
||||
* RCS ID: $Id$
|
||||
\*=========================================================================*/
|
||||
#include <lauxlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "lauxlib.h"
|
||||
|
||||
#include "auxiliar.h"
|
||||
#include "options.h"
|
||||
#include "inet.h"
|
||||
|
@ -10,7 +10,7 @@
|
||||
* RCS ID: $Id$
|
||||
\*=========================================================================*/
|
||||
|
||||
#include <lua.h>
|
||||
#include "lua.h"
|
||||
#include "socket.h"
|
||||
|
||||
/* option registry */
|
||||
|
@ -6,8 +6,8 @@
|
||||
\*=========================================================================*/
|
||||
#include <string.h>
|
||||
|
||||
#include <lua.h>
|
||||
#include <lauxlib.h>
|
||||
#include "lua.h"
|
||||
#include "lauxlib.h"
|
||||
|
||||
#include "socket.h"
|
||||
#include "timeout.h"
|
||||
|
@ -53,7 +53,6 @@ int sock_waitfd(p_sock ps, int sw, p_tm tm);
|
||||
int sock_select(int n, fd_set *rfds, fd_set *wfds, fd_set *efds, p_tm tm);
|
||||
|
||||
int sock_connect(p_sock ps, SA *addr, socklen_t addr_len, p_tm tm);
|
||||
int sock_connected(p_sock ps, p_tm tm);
|
||||
int sock_create(p_sock ps, int domain, int type, int protocol);
|
||||
int sock_bind(p_sock ps, SA *addr, socklen_t addr_len);
|
||||
int sock_listen(p_sock ps, int backlog);
|
||||
|
@ -6,8 +6,8 @@
|
||||
\*=========================================================================*/
|
||||
#include <string.h>
|
||||
|
||||
#include <lua.h>
|
||||
#include <lauxlib.h>
|
||||
#include "lua.h"
|
||||
#include "lauxlib.h"
|
||||
|
||||
#include "auxiliar.h"
|
||||
#include "socket.h"
|
||||
|
@ -16,7 +16,7 @@
|
||||
*
|
||||
* RCS ID: $Id$
|
||||
\*=========================================================================*/
|
||||
#include <lua.h>
|
||||
#include "lua.h"
|
||||
|
||||
#include "buffer.h"
|
||||
#include "timeout.h"
|
||||
|
@ -6,8 +6,8 @@
|
||||
\*=========================================================================*/
|
||||
#include <stdio.h>
|
||||
|
||||
#include <lua.h>
|
||||
#include <lauxlib.h>
|
||||
#include "lua.h"
|
||||
#include "lauxlib.h"
|
||||
|
||||
#include "auxiliar.h"
|
||||
#include "timeout.h"
|
||||
|
@ -6,7 +6,7 @@
|
||||
*
|
||||
* RCS ID: $Id$
|
||||
\*=========================================================================*/
|
||||
#include <lua.h>
|
||||
#include "lua.h"
|
||||
|
||||
/* timeout control structure */
|
||||
typedef struct t_tm_ {
|
||||
|
@ -6,8 +6,8 @@
|
||||
\*=========================================================================*/
|
||||
#include <string.h>
|
||||
|
||||
#include <lua.h>
|
||||
#include <lauxlib.h>
|
||||
#include "lua.h"
|
||||
#include "lauxlib.h"
|
||||
|
||||
#include "auxiliar.h"
|
||||
#include "socket.h"
|
||||
|
@ -14,7 +14,7 @@
|
||||
*
|
||||
* RCS ID: $Id$
|
||||
\*=========================================================================*/
|
||||
#include <lua.h>
|
||||
#include "lua.h"
|
||||
|
||||
#include "timeout.h"
|
||||
#include "socket.h"
|
||||
|
@ -6,8 +6,8 @@
|
||||
\*=========================================================================*/
|
||||
#include <string.h>
|
||||
|
||||
#include <lua.h>
|
||||
#include <lauxlib.h>
|
||||
#include "lua.h"
|
||||
#include "lauxlib.h"
|
||||
|
||||
#include "auxiliar.h"
|
||||
#include "socket.h"
|
||||
|
@ -9,7 +9,7 @@
|
||||
*
|
||||
* RCS ID: $Id$
|
||||
\*=========================================================================*/
|
||||
#include <lua.h>
|
||||
#include "lua.h"
|
||||
|
||||
#include "buffer.h"
|
||||
#include "timeout.h"
|
||||
|
@ -14,6 +14,11 @@ local table = require("table")
|
||||
module("socket.url")
|
||||
getmetatable(_M).__index = nil
|
||||
|
||||
-----------------------------------------------------------------------------
|
||||
-- Module version
|
||||
-----------------------------------------------------------------------------
|
||||
_VERSION = "URL 1.0"
|
||||
|
||||
-----------------------------------------------------------------------------
|
||||
-- Encodes a string into its escaped hexadecimal representation
|
||||
-- Input
|
||||
|
@ -169,15 +169,8 @@ int sock_connect(p_sock ps, SA *addr, socklen_t len, p_tm tm) {
|
||||
/* zero timeout case optimization */
|
||||
if (tm_iszero(tm)) return IO_TIMEOUT;
|
||||
/* wait until we have the result of the connection attempt or timeout */
|
||||
return sock_connected(ps, tm);
|
||||
}
|
||||
|
||||
/*-------------------------------------------------------------------------*\
|
||||
* Checks if socket is connected, or return reason for failure
|
||||
\*-------------------------------------------------------------------------*/
|
||||
int sock_connected(p_sock ps, p_tm tm) {
|
||||
int err;
|
||||
if ((err = sock_waitfd(ps, WAITFD_C, tm) == IO_CLOSED)) {
|
||||
err = sock_waitfd(ps, WAITFD_C, tm);
|
||||
if (err == IO_CLOSED) {
|
||||
if (recv(*ps, (char *) &err, 0, 0) == 0) return IO_DONE;
|
||||
else return errno;
|
||||
} else return err;
|
||||
|
@ -22,7 +22,8 @@ int sock_open(void) {
|
||||
WORD wVersionRequested = MAKEWORD(2, 0);
|
||||
int err = WSAStartup(wVersionRequested, &wsaData );
|
||||
if (err != 0) return 0;
|
||||
if (LOBYTE(wsaData.wVersion) != 2 || HIBYTE(wsaData.wVersion) != 0) {
|
||||
if ((LOBYTE(wsaData.wVersion) != 2 || HIBYTE(wsaData.wVersion) != 0) &&
|
||||
(LOBYTE(wsaData.wVersion) != 1 || HIBYTE(wsaData.wVersion) != 1)) {
|
||||
WSACleanup();
|
||||
return 0;
|
||||
}
|
||||
@ -124,16 +125,8 @@ int sock_connect(p_sock ps, SA *addr, socklen_t len, p_tm tm) {
|
||||
/* zero timeout case optimization */
|
||||
if (tm_iszero(tm)) return IO_TIMEOUT;
|
||||
/* we wait until something happens */
|
||||
return sock_connected(ps, tm);
|
||||
}
|
||||
|
||||
/*-------------------------------------------------------------------------*\
|
||||
* Check if socket is connected
|
||||
\*-------------------------------------------------------------------------*/
|
||||
int sock_connected(p_sock ps, p_tm tm) {
|
||||
int err;
|
||||
/* give windows time to find out what is up (yes, disgusting) */
|
||||
if ((err = sock_waitfd(ps, WAITFD_C, tm)) == IO_CLOSED) {
|
||||
err = sock_waitfd(ps, WAITFD_C, tm);
|
||||
if (err == IO_CLOSED) {
|
||||
int len = sizeof(err);
|
||||
/* give windows time to set the error (yes, disgusting) */
|
||||
Sleep(10);
|
||||
@ -143,6 +136,7 @@ int sock_connected(p_sock ps, p_tm tm) {
|
||||
* "unknown error", but it's not really our fault */
|
||||
return err > 0? err: IO_UNKNOWN;
|
||||
} else return err;
|
||||
|
||||
}
|
||||
|
||||
/*-------------------------------------------------------------------------*\
|
||||
|
@ -382,7 +382,8 @@ function connect_timeout()
|
||||
assert(c, e)
|
||||
c:settimeout(0.1)
|
||||
local t = socket.gettime()
|
||||
local r, e = c:connect("127.0.0.2", 80)
|
||||
local r, e = c:connect("10.0.0.1", 81)
|
||||
print(r, e)
|
||||
assert(not r, "should not connect")
|
||||
assert(socket.gettime() - t < 2, "took too long to give up.")
|
||||
c:close()
|
||||
|
@ -71,7 +71,7 @@ local check_parse_url = function(gaba)
|
||||
local url = gaba.url
|
||||
gaba.url = nil
|
||||
local parsed = socket.url.parse(url)
|
||||
for i, v in gaba do
|
||||
for i, v in pairs(gaba) do
|
||||
if v ~= parsed[i] then
|
||||
io.write("parse: In test for '", url, "' expected ", i, " = '",
|
||||
v, "' but got '", tostring(parsed[i]), "'\n")
|
||||
@ -79,7 +79,7 @@ local check_parse_url = function(gaba)
|
||||
exit()
|
||||
end
|
||||
end
|
||||
for i, v in parsed do
|
||||
for i, v in pairs(parsed) do
|
||||
if v ~= gaba[i] then
|
||||
io.write("parse: In test for '", url, "' expected ", i, " = '",
|
||||
tostring(gaba[i]), "' but got '", v, "'\n")
|
||||
|
Loading…
Reference in New Issue
Block a user