mirror of
https://github.com/lunarmodules/luasocket.git
synced 2024-11-16 02:08:21 +01:00
Changed the naming convention of the mime module.
Looks beautiful.
This commit is contained in:
parent
0b61b577f5
commit
3a7ac1e043
3
TODO
3
TODO
@ -19,6 +19,9 @@ tests
|
|||||||
checar garbage collection
|
checar garbage collection
|
||||||
check for interrupts
|
check for interrupts
|
||||||
|
|
||||||
|
trust character constants in mime.c? noooooo.
|
||||||
|
smtp.lua needs stuff filter
|
||||||
|
|
||||||
new option.c module to put all options (TCP and UDP share...)?
|
new option.c module to put all options (TCP and UDP share...)?
|
||||||
testar os options!
|
testar os options!
|
||||||
add _tostring methods!
|
add _tostring methods!
|
||||||
|
20
etc/get.lua
20
etc/get.lua
@ -19,7 +19,7 @@ function nicetime(s)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
if l == "s" then return string.format("%2.0f%s", s, l)
|
if l == "s" then return string.format("%5.0f%s", s, l)
|
||||||
else return string.format("%5.2f%s", s, l) end
|
else return string.format("%5.2f%s", s, l) end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -42,20 +42,16 @@ function nicesize(b)
|
|||||||
end
|
end
|
||||||
|
|
||||||
-- returns a string with the current state of the download
|
-- returns a string with the current state of the download
|
||||||
|
local remaining_s = "%s received, %s/s throughput, %2.0f%% done, %s remaining"
|
||||||
|
local elapsed_s = "%s received, %s/s throughput, %s elapsed "
|
||||||
function gauge(got, delta, size)
|
function gauge(got, delta, size)
|
||||||
local rate = got / delta
|
local rate = got / delta
|
||||||
if size and size >= 1 then
|
if size and size >= 1 then
|
||||||
return string.format("%s received, %s/s throughput, " ..
|
return string.format(remaining_s, nicesize(got), nicesize(rate),
|
||||||
"%.0f%% done, %s remaining",
|
100*got/size, nicetime((size-got)/rate))
|
||||||
nicesize(got),
|
|
||||||
nicesize(rate),
|
|
||||||
100*got/size,
|
|
||||||
nicetime((size-got)/rate))
|
|
||||||
else
|
else
|
||||||
return string.format("%s received, %s/s throughput, %s elapsed",
|
return string.format(elapsed_s, nicesize(got),
|
||||||
nicesize(got),
|
nicesize(rate), nicetime(delta))
|
||||||
nicesize(rate),
|
|
||||||
nicetime(delta))
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -78,7 +74,7 @@ function stats(size)
|
|||||||
return chunk
|
return chunk
|
||||||
else
|
else
|
||||||
-- close up
|
-- close up
|
||||||
io.stderr:write("\n")
|
io.stderr:write("\r", gauge(got, delta), "\n")
|
||||||
return ""
|
return ""
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -70,7 +70,7 @@
|
|||||||
<Tool
|
<Tool
|
||||||
Name="VCCLCompilerTool"
|
Name="VCCLCompilerTool"
|
||||||
AdditionalIncludeDirectories="net/include"
|
AdditionalIncludeDirectories="net/include"
|
||||||
PreprocessorDefinitions="WIN32;NDEBUG;_CONSOLE, LUASOCKET_DEBUG"
|
PreprocessorDefinitions="WIN32;NDEBUG;_CONSOLE, LUASOCKET_DEBUG, LUASOCKET_COMPILED"
|
||||||
RuntimeLibrary="4"
|
RuntimeLibrary="4"
|
||||||
UsePrecompiledHeader="0"
|
UsePrecompiledHeader="0"
|
||||||
WarningLevel="3"
|
WarningLevel="3"
|
||||||
|
80
src/mime.c
80
src/mime.c
@ -35,17 +35,20 @@ static int mime_global_unqp(lua_State *L);
|
|||||||
static int mime_global_qpfmt(lua_State *L);
|
static int mime_global_qpfmt(lua_State *L);
|
||||||
static int mime_global_eol(lua_State *L);
|
static int mime_global_eol(lua_State *L);
|
||||||
|
|
||||||
static void b64fill(UC *b64unbase);
|
static void b64setup(UC *b64unbase);
|
||||||
static size_t b64encode(UC c, UC *input, size_t size, luaL_Buffer *buffer);
|
static size_t b64encode(UC c, UC *input, size_t size, luaL_Buffer *buffer);
|
||||||
static size_t b64pad(const UC *input, size_t size, luaL_Buffer *buffer);
|
static size_t b64pad(const UC *input, size_t size, luaL_Buffer *buffer);
|
||||||
static size_t b64decode(UC c, UC *input, size_t size, luaL_Buffer *buffer);
|
static size_t b64decode(UC c, UC *input, size_t size, luaL_Buffer *buffer);
|
||||||
|
|
||||||
static void qpfill(UC *qpclass, UC *qpunbase);
|
static void qpsetup(UC *qpclass, UC *qpunbase);
|
||||||
static void qpquote(UC c, luaL_Buffer *buffer);
|
static void qpquote(UC c, luaL_Buffer *buffer);
|
||||||
static size_t qpdecode(UC c, UC *input, size_t size, luaL_Buffer *buffer);
|
static size_t qpdecode(UC c, UC *input, size_t size, luaL_Buffer *buffer);
|
||||||
static size_t qpencode(UC c, UC *input, size_t size,
|
static size_t qpencode(UC c, UC *input, size_t size,
|
||||||
const char *marker, luaL_Buffer *buffer);
|
const char *marker, luaL_Buffer *buffer);
|
||||||
|
|
||||||
|
static const char *checklstring(lua_State *L, int n, size_t *l);
|
||||||
|
static const char *optlstring(lua_State *L, int n, const char *v, size_t *l);
|
||||||
|
|
||||||
/* code support functions */
|
/* code support functions */
|
||||||
static luaL_reg func[] = {
|
static luaL_reg func[] = {
|
||||||
{ "eol", mime_global_eol },
|
{ "eol", mime_global_eol },
|
||||||
@ -96,8 +99,27 @@ void mime_open(lua_State *L)
|
|||||||
lua_settable(L, -3);
|
lua_settable(L, -3);
|
||||||
lua_pop(L, 1);
|
lua_pop(L, 1);
|
||||||
/* initialize lookup tables */
|
/* initialize lookup tables */
|
||||||
qpfill(qpclass, qpunbase);
|
qpsetup(qpclass, qpunbase);
|
||||||
b64fill(b64unbase);
|
b64setup(b64unbase);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*-------------------------------------------------------------------------*\
|
||||||
|
* Check if a string was provided. We accept false also.
|
||||||
|
\*-------------------------------------------------------------------------*/
|
||||||
|
static const char *checklstring(lua_State *L, int n, size_t *l)
|
||||||
|
{
|
||||||
|
if (lua_isnil(L, n) || (lua_isboolean(L, n) && !lua_toboolean(L, n))) {
|
||||||
|
*l = 0;
|
||||||
|
return NULL;
|
||||||
|
} else return luaL_checklstring(L, n, l);
|
||||||
|
}
|
||||||
|
|
||||||
|
static const char *optlstring(lua_State *L, int n, const char *v, size_t *l)
|
||||||
|
{
|
||||||
|
if (lua_isnil(L, n) || (lua_isboolean(L, n) && !lua_toboolean(L, n))) {
|
||||||
|
*l = 0;
|
||||||
|
return NULL;
|
||||||
|
} else return luaL_optlstring(L, n, v, l);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*=========================================================================*\
|
/*=========================================================================*\
|
||||||
@ -105,19 +127,19 @@ void mime_open(lua_State *L)
|
|||||||
\*=========================================================================*/
|
\*=========================================================================*/
|
||||||
/*-------------------------------------------------------------------------*\
|
/*-------------------------------------------------------------------------*\
|
||||||
* Incrementaly breaks a string into lines
|
* Incrementaly breaks a string into lines
|
||||||
* A, n = fmt(B, length, left)
|
* A, n = fmt(l, B, length, marker)
|
||||||
* A is a copy of B, broken into lines of at most 'length' bytes.
|
* A is a copy of B, broken into lines of at most 'length' bytes.
|
||||||
* Left is how many bytes are left in the first line of B. 'n' is the number
|
* 'l' is how many bytes are left for the first line of B.
|
||||||
* of bytes left in the last line of A.
|
* 'n' is the number of bytes left in the last line of A.
|
||||||
|
* Marker is the end-of-line marker.
|
||||||
\*-------------------------------------------------------------------------*/
|
\*-------------------------------------------------------------------------*/
|
||||||
static int mime_global_fmt(lua_State *L)
|
static int mime_global_fmt(lua_State *L)
|
||||||
{
|
{
|
||||||
size_t size = 0;
|
size_t size = 0;
|
||||||
const UC *input = (UC *) (lua_isnil(L, 1)? NULL:
|
int left = (int) luaL_checknumber(L, 1);
|
||||||
luaL_checklstring(L, 1, &size));
|
const UC *input = (UC *) checklstring(L, 2, &size);
|
||||||
const UC *last = input + size;
|
const UC *last = input + size;
|
||||||
int length = (int) luaL_checknumber(L, 2);
|
int length = (int) luaL_optnumber(L, 3, 76);
|
||||||
int left = (int) luaL_optnumber(L, 3, length);
|
|
||||||
const char *marker = luaL_optstring(L, 4, CRLF);
|
const char *marker = luaL_optstring(L, 4, CRLF);
|
||||||
luaL_Buffer buffer;
|
luaL_Buffer buffer;
|
||||||
luaL_buffinit(L, &buffer);
|
luaL_buffinit(L, &buffer);
|
||||||
@ -140,7 +162,7 @@ static int mime_global_fmt(lua_State *L)
|
|||||||
/*-------------------------------------------------------------------------*\
|
/*-------------------------------------------------------------------------*\
|
||||||
* Fill base64 decode map.
|
* Fill base64 decode map.
|
||||||
\*-------------------------------------------------------------------------*/
|
\*-------------------------------------------------------------------------*/
|
||||||
static void b64fill(UC *b64unbase)
|
static void b64setup(UC *b64unbase)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
for (i = 0; i < 255; i++) b64unbase[i] = 255;
|
for (i = 0; i < 255; i++) b64unbase[i] = 255;
|
||||||
@ -255,7 +277,7 @@ static int mime_global_b64(lua_State *L)
|
|||||||
luaL_buffinit(L, &buffer);
|
luaL_buffinit(L, &buffer);
|
||||||
while (input < last)
|
while (input < last)
|
||||||
asize = b64encode(*input++, atom, asize, &buffer);
|
asize = b64encode(*input++, atom, asize, &buffer);
|
||||||
input = (UC *) luaL_optlstring(L, 2, NULL, &isize);
|
input = (UC *) optlstring(L, 2, NULL, &isize);
|
||||||
if (input) {
|
if (input) {
|
||||||
last = input + isize;
|
last = input + isize;
|
||||||
while (input < last)
|
while (input < last)
|
||||||
@ -283,7 +305,7 @@ static int mime_global_unb64(lua_State *L)
|
|||||||
luaL_buffinit(L, &buffer);
|
luaL_buffinit(L, &buffer);
|
||||||
while (input < last)
|
while (input < last)
|
||||||
asize = b64decode(*input++, atom, asize, &buffer);
|
asize = b64decode(*input++, atom, asize, &buffer);
|
||||||
input = (UC *) luaL_optlstring(L, 2, NULL, &isize);
|
input = (UC *) optlstring(L, 2, NULL, &isize);
|
||||||
if (input) {
|
if (input) {
|
||||||
last = input + isize;
|
last = input + isize;
|
||||||
while (input < last)
|
while (input < last)
|
||||||
@ -311,7 +333,7 @@ static int mime_global_unb64(lua_State *L)
|
|||||||
* Split quoted-printable characters into classes
|
* Split quoted-printable characters into classes
|
||||||
* Precompute reverse map for encoding
|
* Precompute reverse map for encoding
|
||||||
\*-------------------------------------------------------------------------*/
|
\*-------------------------------------------------------------------------*/
|
||||||
static void qpfill(UC *qpclass, UC *qpunbase)
|
static void qpsetup(UC *qpclass, UC *qpunbase)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
for (i = 0; i < 256; i++) qpclass[i] = QP_QUOTED;
|
for (i = 0; i < 256; i++) qpclass[i] = QP_QUOTED;
|
||||||
@ -417,15 +439,14 @@ static int mime_global_qp(lua_State *L)
|
|||||||
|
|
||||||
size_t asize = 0, isize = 0;
|
size_t asize = 0, isize = 0;
|
||||||
UC atom[3];
|
UC atom[3];
|
||||||
const UC *input = (UC *) (lua_isnil(L, 1) ? NULL:
|
const UC *input = (UC *) checklstring(L, 1, &isize);
|
||||||
luaL_checklstring(L, 1, &isize));
|
|
||||||
const UC *last = input + isize;
|
const UC *last = input + isize;
|
||||||
const char *marker = luaL_optstring(L, 3, CRLF);
|
const char *marker = luaL_optstring(L, 3, CRLF);
|
||||||
luaL_Buffer buffer;
|
luaL_Buffer buffer;
|
||||||
luaL_buffinit(L, &buffer);
|
luaL_buffinit(L, &buffer);
|
||||||
while (input < last)
|
while (input < last)
|
||||||
asize = qpencode(*input++, atom, asize, marker, &buffer);
|
asize = qpencode(*input++, atom, asize, marker, &buffer);
|
||||||
input = (UC *) luaL_optlstring(L, 2, NULL, &isize);
|
input = (UC *) optlstring(L, 2, NULL, &isize);
|
||||||
if (input) {
|
if (input) {
|
||||||
last = input + isize;
|
last = input + isize;
|
||||||
while (input < last)
|
while (input < last)
|
||||||
@ -486,14 +507,13 @@ static int mime_global_unqp(lua_State *L)
|
|||||||
|
|
||||||
size_t asize = 0, isize = 0;
|
size_t asize = 0, isize = 0;
|
||||||
UC atom[3];
|
UC atom[3];
|
||||||
const UC *input = (UC *) (lua_isnil(L, 1) ? NULL:
|
const UC *input = (UC *) checklstring(L, 1, &isize);
|
||||||
luaL_checklstring(L, 1, &isize));
|
|
||||||
const UC *last = input + isize;
|
const UC *last = input + isize;
|
||||||
luaL_Buffer buffer;
|
luaL_Buffer buffer;
|
||||||
luaL_buffinit(L, &buffer);
|
luaL_buffinit(L, &buffer);
|
||||||
while (input < last)
|
while (input < last)
|
||||||
asize = qpdecode(*input++, atom, asize, &buffer);
|
asize = qpdecode(*input++, atom, asize, &buffer);
|
||||||
input = (UC *) luaL_optlstring(L, 2, NULL, &isize);
|
input = (UC *) optlstring(L, 2, NULL, &isize);
|
||||||
if (input) {
|
if (input) {
|
||||||
last = input + isize;
|
last = input + isize;
|
||||||
while (input < last)
|
while (input < last)
|
||||||
@ -506,21 +526,20 @@ static int mime_global_unqp(lua_State *L)
|
|||||||
|
|
||||||
/*-------------------------------------------------------------------------*\
|
/*-------------------------------------------------------------------------*\
|
||||||
* Incrementally breaks a quoted-printed string into lines
|
* Incrementally breaks a quoted-printed string into lines
|
||||||
* A, n = qpfmt(B, length, left)
|
* A, n = qpfmt(l, B, length)
|
||||||
* A is a copy of B, broken into lines of at most 'length' bytes.
|
* A is a copy of B, broken into lines of at most 'length' bytes.
|
||||||
* Left is how many bytes are left in the first line of B. 'n' is the number
|
* 'l' is how many bytes are left for the first line of B.
|
||||||
* of bytes left in the last line of A.
|
* 'n' is the number of bytes left in the last line of A.
|
||||||
* There are two complications: lines can't be broken in the middle
|
* There are two complications: lines can't be broken in the middle
|
||||||
* of an encoded =XX, and there might be line breaks already
|
* of an encoded =XX, and there might be line breaks already
|
||||||
\*-------------------------------------------------------------------------*/
|
\*-------------------------------------------------------------------------*/
|
||||||
static int mime_global_qpfmt(lua_State *L)
|
static int mime_global_qpfmt(lua_State *L)
|
||||||
{
|
{
|
||||||
size_t size = 0;
|
size_t size = 0;
|
||||||
const UC *input = (UC *) (lua_isnil(L, 1)? NULL:
|
int left = (int) luaL_checknumber(L, 1);
|
||||||
luaL_checklstring(L, 1, &size));
|
const UC *input = (UC *) checklstring(L, 2, &size);
|
||||||
const UC *last = input + size;
|
const UC *last = input + size;
|
||||||
int length = (int) luaL_checknumber(L, 2);
|
int length = (int) luaL_optnumber(L, 3, 76);
|
||||||
int left = (int) luaL_optnumber(L, 3, length);
|
|
||||||
luaL_Buffer buffer;
|
luaL_Buffer buffer;
|
||||||
luaL_buffinit(L, &buffer);
|
luaL_buffinit(L, &buffer);
|
||||||
while (input < last) {
|
while (input < last) {
|
||||||
@ -597,15 +616,14 @@ static int mime_global_eol(lua_State *L)
|
|||||||
{
|
{
|
||||||
size_t asize = 0, isize = 0;
|
size_t asize = 0, isize = 0;
|
||||||
UC atom[2];
|
UC atom[2];
|
||||||
const UC *input = (UC *) (lua_isnil(L, 1)? NULL:
|
const UC *input = (UC *) checklstring(L, 1, &isize);
|
||||||
luaL_checklstring(L, 1, &isize));
|
|
||||||
const UC *last = input + isize;
|
const UC *last = input + isize;
|
||||||
const char *marker = luaL_optstring(L, 3, CRLF);
|
const char *marker = luaL_optstring(L, 3, CRLF);
|
||||||
luaL_Buffer buffer;
|
luaL_Buffer buffer;
|
||||||
luaL_buffinit(L, &buffer);
|
luaL_buffinit(L, &buffer);
|
||||||
while (input < last)
|
while (input < last)
|
||||||
asize = eolconvert(*input++, atom, asize, marker, &buffer);
|
asize = eolconvert(*input++, atom, asize, marker, &buffer);
|
||||||
input = (UC *) luaL_optlstring(L, 2, NULL, &isize);
|
input = (UC *) optlstring(L, 2, NULL, &isize);
|
||||||
if (input) {
|
if (input) {
|
||||||
last = input + isize;
|
last = input + isize;
|
||||||
while (input < last)
|
while (input < last)
|
||||||
|
94
src/mime.lua
94
src/mime.lua
@ -10,75 +10,71 @@ socket.mime = mime
|
|||||||
setmetatable(mime, { __index = _G })
|
setmetatable(mime, { __index = _G })
|
||||||
setfenv(1, mime)
|
setfenv(1, mime)
|
||||||
|
|
||||||
base64 = {}
|
-- encode, decode and wrap algorithm tables
|
||||||
qprint = {}
|
local et = {}
|
||||||
|
local dt = {}
|
||||||
|
local wt = {}
|
||||||
|
|
||||||
function base64.encode()
|
-- creates a function that chooses an algorithm from a given table
|
||||||
local unfinished = ""
|
local function choose(table)
|
||||||
|
return function(method, ...)
|
||||||
|
local f = table[method or "nil"]
|
||||||
|
if not f then return nil, "unknown method (" .. tostring(method) .. ")"
|
||||||
|
else return f(unpack(arg)) end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
-- creates a function that cicles a filter with a given initial
|
||||||
|
-- context and extra arguments
|
||||||
|
local function cicle(f, ctx, ...)
|
||||||
return function(chunk)
|
return function(chunk)
|
||||||
local done
|
local ret
|
||||||
done, unfinished = b64(unfinished, chunk)
|
ret, ctx = f(ctx, chunk, unpack(arg))
|
||||||
return done
|
return ret
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function base64.decode()
|
-- function that choose the encoding, decoding or wrap algorithm
|
||||||
local unfinished = ""
|
encode = choose(et)
|
||||||
return function(chunk)
|
decode = choose(dt)
|
||||||
local done
|
wrap = choose(wt)
|
||||||
done, unfinished = unb64(unfinished, chunk)
|
|
||||||
return done
|
-- define the encoding algorithms
|
||||||
end
|
et['base64'] = function()
|
||||||
|
return cicle(b64, "")
|
||||||
end
|
end
|
||||||
|
|
||||||
function qprint.encode(mode)
|
et['quoted-printable'] = function(mode)
|
||||||
mode = (mode == "binary") and "=0D=0A" or "\13\10"
|
return cicle(qp, "", (mode == "binary") and "=0D=0A" or "\13\10")
|
||||||
local unfinished = ""
|
|
||||||
return function(chunk)
|
|
||||||
local done
|
|
||||||
done, unfinished = qp(unfinished, chunk, mode)
|
|
||||||
return done
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
function qprint.decode()
|
-- define the decoding algorithms
|
||||||
local unfinished = ""
|
dt['base64'] = function()
|
||||||
return function(chunk)
|
return cicle(unb64, "")
|
||||||
local done
|
|
||||||
done, unfinished = unqp(unfinished, chunk)
|
|
||||||
return done
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
function split(length, marker)
|
dt['quoted-printable'] = function()
|
||||||
|
return cicle(unqp, "")
|
||||||
|
end
|
||||||
|
|
||||||
|
-- define the wrap algorithms
|
||||||
|
wt['character'] = function(length)
|
||||||
length = length or 76
|
length = length or 76
|
||||||
local left = length
|
return cicle(fmt, length, length)
|
||||||
return function(chunk)
|
|
||||||
local done
|
|
||||||
done, left = fmt(chunk, length, left, marker)
|
|
||||||
return done
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
wt['base64'] = wt['character']
|
||||||
|
|
||||||
function qprint.split(length)
|
wt['quoted-printable'] = function(length)
|
||||||
length = length or 76
|
length = length or 76
|
||||||
local left = length
|
return cicle(qpfmt, length, length)
|
||||||
return function(chunk)
|
|
||||||
local done
|
|
||||||
done, left = qpfmt(chunk, length, left)
|
|
||||||
return done
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- define the end-of-line translation function
|
||||||
function canonic(marker)
|
function canonic(marker)
|
||||||
local unfinished = ""
|
return cicle(eol, "", marker)
|
||||||
return function(chunk)
|
|
||||||
local done
|
|
||||||
done, unfinished = eol(unfinished, chunk, marker)
|
|
||||||
return done
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- chains several filters together
|
||||||
function chain(...)
|
function chain(...)
|
||||||
local layers = table.getn(arg)
|
local layers = table.getn(arg)
|
||||||
return function (chunk)
|
return function (chunk)
|
||||||
|
@ -5,8 +5,8 @@
|
|||||||
-- needs "AllowOverride AuthConfig" on /home/c/diego/tec/luasocket/test/auth
|
-- needs "AllowOverride AuthConfig" on /home/c/diego/tec/luasocket/test/auth
|
||||||
dofile("noglobals.lua")
|
dofile("noglobals.lua")
|
||||||
|
|
||||||
local host, proxy, request, response
|
local host, proxy, request, response, index_file
|
||||||
local ignore, expect, index, prefix, cgiprefix
|
local ignore, expect, index, prefix, cgiprefix, index_crlf
|
||||||
|
|
||||||
socket.http.TIMEOUT = 5
|
socket.http.TIMEOUT = 5
|
||||||
|
|
||||||
@ -16,6 +16,7 @@ host = host or "diego.student.dyn.cs.princeton.edu"
|
|||||||
proxy = proxy or "http://localhost:3128"
|
proxy = proxy or "http://localhost:3128"
|
||||||
prefix = prefix or "/luasocket-test"
|
prefix = prefix or "/luasocket-test"
|
||||||
cgiprefix = cgiprefix or "/luasocket-test-cgi"
|
cgiprefix = cgiprefix or "/luasocket-test-cgi"
|
||||||
|
index_file = "test/index.html"
|
||||||
|
|
||||||
local readfile = function(name)
|
local readfile = function(name)
|
||||||
local f = io.open(name, "r")
|
local f = io.open(name, "r")
|
||||||
@ -25,7 +26,8 @@ local readfile = function(name)
|
|||||||
return s
|
return s
|
||||||
end
|
end
|
||||||
|
|
||||||
index = readfile("test/index.html")
|
-- read index with CRLF convention
|
||||||
|
index = readfile(index_file)
|
||||||
|
|
||||||
local similar = function(s1, s2)
|
local similar = function(s1, s2)
|
||||||
return string.lower(string.gsub(s1 or "", "%s", "")) ==
|
return string.lower(string.gsub(s1 or "", "%s", "")) ==
|
||||||
@ -43,12 +45,12 @@ local check = function (v, e)
|
|||||||
else fail(e) end
|
else fail(e) end
|
||||||
end
|
end
|
||||||
|
|
||||||
local check_request = function(request, expect, ignore)
|
local check_result = function(response, expect, ignore)
|
||||||
local response = socket.http.request(request)
|
|
||||||
for i,v in response do
|
for i,v in response do
|
||||||
if not ignore[i] then
|
if not ignore[i] then
|
||||||
if v ~= expect[i] then
|
if v ~= expect[i] then
|
||||||
if string.len(v) < 80 then print(v) end
|
v = string.sub(type(v) == "string" and v or "", 1, 70)
|
||||||
|
print(v)
|
||||||
fail(i .. " differs!")
|
fail(i .. " differs!")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -56,7 +58,8 @@ local check_request = function(request, expect, ignore)
|
|||||||
for i,v in expect do
|
for i,v in expect do
|
||||||
if not ignore[i] then
|
if not ignore[i] then
|
||||||
if v ~= response[i] then
|
if v ~= response[i] then
|
||||||
if string.len(v) < 80 then print(v) end
|
v = string.sub(type(v) == "string" and v or "", 1, 70)
|
||||||
|
print(v)
|
||||||
fail(i .. " differs!")
|
fail(i .. " differs!")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -64,6 +67,17 @@ local check_request = function(request, expect, ignore)
|
|||||||
print("ok")
|
print("ok")
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local check_request = function(request, expect, ignore)
|
||||||
|
local response = socket.http.request(request)
|
||||||
|
check_result(response, expect, ignore)
|
||||||
|
end
|
||||||
|
|
||||||
|
local check_request_cb = function(request, response, expect, ignore)
|
||||||
|
local response = socket.http.request_cb(request, response)
|
||||||
|
check_result(response, expect, ignore)
|
||||||
|
end
|
||||||
|
|
||||||
|
------------------------------------------------------------------------
|
||||||
io.write("testing request uri correctness: ")
|
io.write("testing request uri correctness: ")
|
||||||
local forth = cgiprefix .. "/request-uri?" .. "this+is+the+query+string"
|
local forth = cgiprefix .. "/request-uri?" .. "this+is+the+query+string"
|
||||||
local back, h, c, e = socket.http.get("http://" .. host .. forth)
|
local back, h, c, e = socket.http.get("http://" .. host .. forth)
|
||||||
@ -72,12 +86,15 @@ back = socket.url.parse(back)
|
|||||||
if similar(back.query, "this+is+the+query+string") then print("ok")
|
if similar(back.query, "this+is+the+query+string") then print("ok")
|
||||||
else fail() end
|
else fail() end
|
||||||
|
|
||||||
|
------------------------------------------------------------------------
|
||||||
io.write("testing query string correctness: ")
|
io.write("testing query string correctness: ")
|
||||||
forth = "this+is+the+query+string"
|
forth = "this+is+the+query+string"
|
||||||
back = socket.http.get("http://" .. host .. cgiprefix .. "/query-string?" .. forth)
|
back = socket.http.get("http://" .. host .. cgiprefix ..
|
||||||
|
"/query-string?" .. forth)
|
||||||
if similar(back, forth) then print("ok")
|
if similar(back, forth) then print("ok")
|
||||||
else fail("failed!") end
|
else fail("failed!") end
|
||||||
|
|
||||||
|
------------------------------------------------------------------------
|
||||||
io.write("testing document retrieval: ")
|
io.write("testing document retrieval: ")
|
||||||
request = {
|
request = {
|
||||||
url = "http://" .. host .. prefix .. "/index.html"
|
url = "http://" .. host .. prefix .. "/index.html"
|
||||||
@ -92,6 +109,7 @@ ignore = {
|
|||||||
}
|
}
|
||||||
check_request(request, expect, ignore)
|
check_request(request, expect, ignore)
|
||||||
|
|
||||||
|
------------------------------------------------------------------------
|
||||||
io.write("testing redirect loop: ")
|
io.write("testing redirect loop: ")
|
||||||
request = {
|
request = {
|
||||||
url = "http://" .. host .. cgiprefix .. "/redirect-loop"
|
url = "http://" .. host .. cgiprefix .. "/redirect-loop"
|
||||||
@ -106,6 +124,7 @@ ignore = {
|
|||||||
}
|
}
|
||||||
check_request(request, expect, ignore)
|
check_request(request, expect, ignore)
|
||||||
|
|
||||||
|
------------------------------------------------------------------------
|
||||||
io.write("testing post method: ")
|
io.write("testing post method: ")
|
||||||
-- wanted to test chunked post, but apache doesn't support it...
|
-- wanted to test chunked post, but apache doesn't support it...
|
||||||
request = {
|
request = {
|
||||||
@ -125,6 +144,7 @@ ignore = {
|
|||||||
}
|
}
|
||||||
check_request(request, expect, ignore)
|
check_request(request, expect, ignore)
|
||||||
|
|
||||||
|
------------------------------------------------------------------------
|
||||||
io.write("testing proxy with post method: ")
|
io.write("testing proxy with post method: ")
|
||||||
request = {
|
request = {
|
||||||
url = "http://" .. host .. cgiprefix .. "/cat",
|
url = "http://" .. host .. cgiprefix .. "/cat",
|
||||||
@ -143,10 +163,78 @@ ignore = {
|
|||||||
}
|
}
|
||||||
check_request(request, expect, ignore)
|
check_request(request, expect, ignore)
|
||||||
|
|
||||||
|
------------------------------------------------------------------------
|
||||||
io.write("testing simple post function: ")
|
io.write("testing simple post function: ")
|
||||||
back = socket.http.post("http://" .. host .. cgiprefix .. "/cat", index)
|
back = socket.http.post("http://" .. host .. cgiprefix .. "/cat", index)
|
||||||
check(back == index)
|
check(back == index)
|
||||||
|
|
||||||
|
------------------------------------------------------------------------
|
||||||
|
io.write("testing send.file and receive.file callbacks: ")
|
||||||
|
request = {
|
||||||
|
url = "http://" .. host .. cgiprefix .. "/cat",
|
||||||
|
method = "POST",
|
||||||
|
body_cb = socket.callback.send.file(io.open(index_file, "r")),
|
||||||
|
headers = { ["content-length"] = string.len(index) }
|
||||||
|
}
|
||||||
|
response = {
|
||||||
|
body_cb = socket.callback.receive.file(io.open(index_file .. "-back", "w"))
|
||||||
|
}
|
||||||
|
expect = {
|
||||||
|
code = 200
|
||||||
|
}
|
||||||
|
ignore = {
|
||||||
|
body_cb = 1,
|
||||||
|
status = 1,
|
||||||
|
headers = 1
|
||||||
|
}
|
||||||
|
check_request_cb(request, response, expect, ignore)
|
||||||
|
back = readfile(index_file .. "-back")
|
||||||
|
check(back == index)
|
||||||
|
os.remove(index_file .. "-back")
|
||||||
|
|
||||||
|
------------------------------------------------------------------------
|
||||||
|
io.write("testing send.chain and receive.chain callbacks: ")
|
||||||
|
|
||||||
|
local function b64length(len)
|
||||||
|
local a = math.ceil(len/3)*4
|
||||||
|
local l = math.ceil(a/76)
|
||||||
|
return a + l*2
|
||||||
|
end
|
||||||
|
|
||||||
|
local req_cb = socket.callback.send.chain(
|
||||||
|
socket.callback.send.file(io.open(index_file, "r")),
|
||||||
|
socket.mime.chain(
|
||||||
|
socket.mime.encode("base64"),
|
||||||
|
socket.mime.wrap("base64")
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
local resp_cb = socket.callback.receive.chain(
|
||||||
|
socket.mime.decode("base64"),
|
||||||
|
socket.callback.receive.file(io.open(index_file .. "-back", "w"))
|
||||||
|
)
|
||||||
|
|
||||||
|
request = {
|
||||||
|
url = "http://" .. host .. cgiprefix .. "/cat",
|
||||||
|
method = "POST",
|
||||||
|
body_cb = req_cb,
|
||||||
|
headers = { ["content-length"] = b64length(string.len(index)) }
|
||||||
|
}
|
||||||
|
response = { body_cb = resp_cb }
|
||||||
|
expect = {
|
||||||
|
code = 200
|
||||||
|
}
|
||||||
|
ignore = {
|
||||||
|
body_cb = 1,
|
||||||
|
status = 1,
|
||||||
|
headers = 1
|
||||||
|
}
|
||||||
|
check_request_cb(request, response, expect, ignore)
|
||||||
|
back = readfile(index_file .. "-back")
|
||||||
|
check(back == index)
|
||||||
|
os.remove(index_file .. "-back")
|
||||||
|
|
||||||
|
------------------------------------------------------------------------
|
||||||
io.write("testing simple post function with table args: ")
|
io.write("testing simple post function with table args: ")
|
||||||
back = socket.http.post {
|
back = socket.http.post {
|
||||||
url = "http://" .. host .. cgiprefix .. "/cat",
|
url = "http://" .. host .. cgiprefix .. "/cat",
|
||||||
@ -154,6 +242,7 @@ back = socket.http.post {
|
|||||||
}
|
}
|
||||||
check(back == index)
|
check(back == index)
|
||||||
|
|
||||||
|
------------------------------------------------------------------------
|
||||||
io.write("testing http redirection: ")
|
io.write("testing http redirection: ")
|
||||||
request = {
|
request = {
|
||||||
url = "http://" .. host .. prefix
|
url = "http://" .. host .. prefix
|
||||||
@ -168,6 +257,7 @@ ignore = {
|
|||||||
}
|
}
|
||||||
check_request(request, expect, ignore)
|
check_request(request, expect, ignore)
|
||||||
|
|
||||||
|
------------------------------------------------------------------------
|
||||||
io.write("testing proxy with redirection: ")
|
io.write("testing proxy with redirection: ")
|
||||||
request = {
|
request = {
|
||||||
url = "http://" .. host .. prefix,
|
url = "http://" .. host .. prefix,
|
||||||
@ -183,7 +273,7 @@ ignore = {
|
|||||||
}
|
}
|
||||||
check_request(request, expect, ignore)
|
check_request(request, expect, ignore)
|
||||||
|
|
||||||
|
------------------------------------------------------------------------
|
||||||
io.write("testing automatic auth failure: ")
|
io.write("testing automatic auth failure: ")
|
||||||
request = {
|
request = {
|
||||||
url = "http://really:wrong@" .. host .. prefix .. "/auth/index.html"
|
url = "http://really:wrong@" .. host .. prefix .. "/auth/index.html"
|
||||||
@ -198,6 +288,7 @@ ignore = {
|
|||||||
}
|
}
|
||||||
check_request(request, expect, ignore)
|
check_request(request, expect, ignore)
|
||||||
|
|
||||||
|
------------------------------------------------------------------------
|
||||||
io.write("testing http redirection failure: ")
|
io.write("testing http redirection failure: ")
|
||||||
request = {
|
request = {
|
||||||
url = "http://" .. host .. prefix,
|
url = "http://" .. host .. prefix,
|
||||||
@ -213,6 +304,7 @@ ignore = {
|
|||||||
}
|
}
|
||||||
check_request(request, expect, ignore)
|
check_request(request, expect, ignore)
|
||||||
|
|
||||||
|
------------------------------------------------------------------------
|
||||||
io.write("testing host not found: ")
|
io.write("testing host not found: ")
|
||||||
request = {
|
request = {
|
||||||
url = "http://wronghost/does/not/exist"
|
url = "http://wronghost/does/not/exist"
|
||||||
@ -224,6 +316,7 @@ expect = {
|
|||||||
ignore = {}
|
ignore = {}
|
||||||
check_request(request, expect, ignore)
|
check_request(request, expect, ignore)
|
||||||
|
|
||||||
|
------------------------------------------------------------------------
|
||||||
io.write("testing invalid url: ")
|
io.write("testing invalid url: ")
|
||||||
request = {
|
request = {
|
||||||
url = host .. prefix
|
url = host .. prefix
|
||||||
@ -235,6 +328,7 @@ expect = {
|
|||||||
ignore = {}
|
ignore = {}
|
||||||
check_request(request, expect, ignore)
|
check_request(request, expect, ignore)
|
||||||
|
|
||||||
|
------------------------------------------------------------------------
|
||||||
io.write("testing document not found: ")
|
io.write("testing document not found: ")
|
||||||
request = {
|
request = {
|
||||||
url = "http://" .. host .. "/wrongdocument.html"
|
url = "http://" .. host .. "/wrongdocument.html"
|
||||||
@ -249,6 +343,7 @@ ignore = {
|
|||||||
}
|
}
|
||||||
check_request(request, expect, ignore)
|
check_request(request, expect, ignore)
|
||||||
|
|
||||||
|
------------------------------------------------------------------------
|
||||||
io.write("testing auth failure: ")
|
io.write("testing auth failure: ")
|
||||||
request = {
|
request = {
|
||||||
url = "http://" .. host .. prefix .. "/auth/index.html"
|
url = "http://" .. host .. prefix .. "/auth/index.html"
|
||||||
@ -263,6 +358,7 @@ ignore = {
|
|||||||
}
|
}
|
||||||
check_request(request, expect, ignore)
|
check_request(request, expect, ignore)
|
||||||
|
|
||||||
|
------------------------------------------------------------------------
|
||||||
io.write("testing manual basic auth: ")
|
io.write("testing manual basic auth: ")
|
||||||
request = {
|
request = {
|
||||||
url = "http://" .. host .. prefix .. "/auth/index.html",
|
url = "http://" .. host .. prefix .. "/auth/index.html",
|
||||||
@ -280,6 +376,7 @@ ignore = {
|
|||||||
}
|
}
|
||||||
check_request(request, expect, ignore)
|
check_request(request, expect, ignore)
|
||||||
|
|
||||||
|
------------------------------------------------------------------------
|
||||||
io.write("testing automatic basic auth: ")
|
io.write("testing automatic basic auth: ")
|
||||||
request = {
|
request = {
|
||||||
url = "http://luasocket:password@" .. host .. prefix .. "/auth/index.html"
|
url = "http://luasocket:password@" .. host .. prefix .. "/auth/index.html"
|
||||||
@ -294,6 +391,7 @@ ignore = {
|
|||||||
}
|
}
|
||||||
check_request(request, expect, ignore)
|
check_request(request, expect, ignore)
|
||||||
|
|
||||||
|
------------------------------------------------------------------------
|
||||||
io.write("testing auth info overriding: ")
|
io.write("testing auth info overriding: ")
|
||||||
request = {
|
request = {
|
||||||
url = "http://really:wrong@" .. host .. prefix .. "/auth/index.html",
|
url = "http://really:wrong@" .. host .. prefix .. "/auth/index.html",
|
||||||
@ -310,6 +408,7 @@ ignore = {
|
|||||||
}
|
}
|
||||||
check_request(request, expect, ignore)
|
check_request(request, expect, ignore)
|
||||||
|
|
||||||
|
------------------------------------------------------------------------
|
||||||
io.write("testing cgi output retrieval (probably chunked...): ")
|
io.write("testing cgi output retrieval (probably chunked...): ")
|
||||||
request = {
|
request = {
|
||||||
url = "http://" .. host .. cgiprefix .. "/cat-index-html"
|
url = "http://" .. host .. cgiprefix .. "/cat-index-html"
|
||||||
@ -324,6 +423,7 @@ ignore = {
|
|||||||
}
|
}
|
||||||
check_request(request, expect, ignore)
|
check_request(request, expect, ignore)
|
||||||
|
|
||||||
|
------------------------------------------------------------------------
|
||||||
io.write("testing wrong scheme: ")
|
io.write("testing wrong scheme: ")
|
||||||
request = {
|
request = {
|
||||||
url = "wrong://" .. host .. cgiprefix .. "/cat",
|
url = "wrong://" .. host .. cgiprefix .. "/cat",
|
||||||
@ -336,11 +436,13 @@ ignore = {
|
|||||||
}
|
}
|
||||||
check_request(request, expect, ignore)
|
check_request(request, expect, ignore)
|
||||||
|
|
||||||
|
------------------------------------------------------------------------
|
||||||
local body
|
local body
|
||||||
io.write("testing simple get function: ")
|
io.write("testing simple get function: ")
|
||||||
body = socket.http.get("http://" .. host .. prefix .. "/index.html")
|
body = socket.http.get("http://" .. host .. prefix .. "/index.html")
|
||||||
check(body == index)
|
check(body == index)
|
||||||
|
|
||||||
|
------------------------------------------------------------------------
|
||||||
io.write("testing simple get function with table args: ")
|
io.write("testing simple get function with table args: ")
|
||||||
body = socket.http.get {
|
body = socket.http.get {
|
||||||
url = "http://really:wrong@" .. host .. prefix .. "/auth/index.html",
|
url = "http://really:wrong@" .. host .. prefix .. "/auth/index.html",
|
||||||
@ -349,6 +451,7 @@ body = socket.http.get {
|
|||||||
}
|
}
|
||||||
check(body == index)
|
check(body == index)
|
||||||
|
|
||||||
|
------------------------------------------------------------------------
|
||||||
io.write("testing HEAD method: ")
|
io.write("testing HEAD method: ")
|
||||||
socket.http.TIMEOUT = 1
|
socket.http.TIMEOUT = 1
|
||||||
response = socket.http.request {
|
response = socket.http.request {
|
||||||
@ -357,6 +460,7 @@ response = socket.http.request {
|
|||||||
}
|
}
|
||||||
check(response and response.headers)
|
check(response and response.headers)
|
||||||
|
|
||||||
|
------------------------------------------------------------------------
|
||||||
print("passed all tests")
|
print("passed all tests")
|
||||||
|
|
||||||
print(string.format("done in %.2fs", socket.time() - t))
|
print(string.format("done in %.2fs", socket.time() - t))
|
||||||
|
@ -66,8 +66,8 @@ local function compare(input, output)
|
|||||||
end
|
end
|
||||||
|
|
||||||
local function encode_qptest(mode)
|
local function encode_qptest(mode)
|
||||||
local encode = socket.mime.qprint.encode(mode)
|
local encode = socket.mime.encode("quoted-printable", mode)
|
||||||
local split = socket.mime.qprint.split()
|
local split = socket.mime.wrap("quoted-printable")
|
||||||
local chain = socket.mime.chain(encode, split)
|
local chain = socket.mime.chain(encode, split)
|
||||||
transform(qptest, eqptest, chain)
|
transform(qptest, eqptest, chain)
|
||||||
end
|
end
|
||||||
@ -77,7 +77,7 @@ local function compare_qptest()
|
|||||||
end
|
end
|
||||||
|
|
||||||
local function decode_qptest()
|
local function decode_qptest()
|
||||||
local decode = socket.mime.qprint.decode()
|
local decode = socket.mime.decode("quoted-printable")
|
||||||
transform(eqptest, dqptest, decode)
|
transform(eqptest, dqptest, decode)
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -151,23 +151,23 @@ local function cleanup_qptest()
|
|||||||
end
|
end
|
||||||
|
|
||||||
local function encode_b64test()
|
local function encode_b64test()
|
||||||
local e1 = socket.mime.base64.encode()
|
local e1 = socket.mime.encode("base64")
|
||||||
local e2 = socket.mime.base64.encode()
|
local e2 = socket.mime.encode("base64")
|
||||||
local e3 = socket.mime.base64.encode()
|
local e3 = socket.mime.encode("base64")
|
||||||
local e4 = socket.mime.base64.encode()
|
local e4 = socket.mime.encode("base64")
|
||||||
local sp4 = socket.mime.split()
|
local sp4 = socket.mime.wrap("character")
|
||||||
local sp3 = socket.mime.split(59)
|
local sp3 = socket.mime.wrap("character", 59)
|
||||||
local sp2 = socket.mime.split(30)
|
local sp2 = socket.mime.wrap("character", 30)
|
||||||
local sp1 = socket.mime.split(27)
|
local sp1 = socket.mime.wrap("character", 27)
|
||||||
local chain = socket.mime.chain(e1, sp1, e2, sp2, e3, sp3, e4, sp4)
|
local chain = socket.mime.chain(e1, sp1, e2, sp2, e3, sp3, e4, sp4)
|
||||||
transform(b64test, eb64test, chain)
|
transform(b64test, eb64test, chain)
|
||||||
end
|
end
|
||||||
|
|
||||||
local function decode_b64test()
|
local function decode_b64test()
|
||||||
local d1 = socket.mime.base64.decode()
|
local d1 = socket.mime.decode("base64")
|
||||||
local d2 = socket.mime.base64.decode()
|
local d2 = socket.mime.decode("base64")
|
||||||
local d3 = socket.mime.base64.decode()
|
local d3 = socket.mime.decode("base64")
|
||||||
local d4 = socket.mime.base64.decode()
|
local d4 = socket.mime.decode("base64")
|
||||||
local chain = socket.mime.chain(d1, d2, d3, d4)
|
local chain = socket.mime.chain(d1, d2, d3, d4)
|
||||||
transform(eb64test, db64test, chain)
|
transform(eb64test, db64test, chain)
|
||||||
end
|
end
|
||||||
|
Loading…
Reference in New Issue
Block a user