Out of the box support for Lua 5.3.

This commit is contained in:
Diego Nehab 2015-02-18 20:51:37 -02:00
parent 5edf093643
commit ddf4292824
7 changed files with 96 additions and 85 deletions

View File

@ -5,8 +5,8 @@
# Targets: # Targets:
# install install system independent support # install install system independent support
# install-unix also install unix-only support # install-unix also install unix-only support
# install-both install for both lua5.1 and lua5.2 # install-both install for lua51 lua52 lua53
# install-both-unix also install unix-only # install-both-unix also install unix-only
# print print the build settings # print print the build settings
PLAT?= linux PLAT?= linux
@ -24,20 +24,26 @@ test:
lua test/hello.lua lua test/hello.lua
install-both: install-both:
$(MAKE) clean $(MAKE) clean
@cd src; $(MAKE) $(PLAT) LUAV=5.1 @cd src; $(MAKE) $(PLAT) LUAV=5.1
@cd src; $(MAKE) install LUAV=5.1 @cd src; $(MAKE) install LUAV=5.1
$(MAKE) clean $(MAKE) clean
@cd src; $(MAKE) $(PLAT) LUAV=5.2 @cd src; $(MAKE) $(PLAT) LUAV=5.2
@cd src; $(MAKE) install LUAV=5.2 @cd src; $(MAKE) install LUAV=5.2
$(MAKE) clean
@cd src; $(MAKE) $(PLAT) LUAV=5.3
@cd src; $(MAKE) install LUAV=5.3
install-both-unix: install-both-unix:
$(MAKE) clean $(MAKE) clean
@cd src; $(MAKE) $(PLAT) LUAV=5.1 @cd src; $(MAKE) $(PLAT) LUAV=5.1
@cd src; $(MAKE) install-unix LUAV=5.1 @cd src; $(MAKE) install-unix LUAV=5.1
$(MAKE) clean $(MAKE) clean
@cd src; $(MAKE) $(PLAT) LUAV=5.2 @cd src; $(MAKE) $(PLAT) LUAV=5.2
@cd src; $(MAKE) install-unix LUAV=5.2 @cd src; $(MAKE) install-unix LUAV=5.2
$(MAKE) clean
@cd src; $(MAKE) $(PLAT) LUAV=5.3
@cd src; $(MAKE) install-unix LUAV=5.3
.PHONY: test .PHONY: test

View File

@ -18,7 +18,6 @@
#include "lua.h" #include "lua.h"
#include "lauxlib.h" #include "lauxlib.h"
/*=========================================================================*\ /*=========================================================================*\
* LuaSocket includes * LuaSocket includes
\*=========================================================================*/ \*=========================================================================*/
@ -32,6 +31,11 @@
#include "udp.h" #include "udp.h"
#include "select.h" #include "select.h"
#if LUA_VERSION_NUM > 502 && !defined(LUA_COMPAT_APIINTCASTS)
#define luaL_checkint(L,n) ((int)luaL_checkinteger(L, (n)))
#define luaL_optint(L,n,d) ((int)luaL_optinteger(L, (n), (d)))
#endif
/*-------------------------------------------------------------------------*\ /*-------------------------------------------------------------------------*\
* Internal function prototypes * Internal function prototypes
\*-------------------------------------------------------------------------*/ \*-------------------------------------------------------------------------*/
@ -78,14 +82,6 @@ static int global_unload(lua_State *L) {
return 0; return 0;
} }
#if LUA_VERSION_NUM > 501
int luaL_typerror (lua_State *L, int narg, const char *tname) {
const char *msg = lua_pushfstring(L, "%s expected, got %s",
tname, luaL_typename(L, narg));
return luaL_argerror(L, narg, msg);
}
#endif
/*-------------------------------------------------------------------------*\ /*-------------------------------------------------------------------------*\
* Setup basic stuff. * Setup basic stuff.
\*-------------------------------------------------------------------------*/ \*-------------------------------------------------------------------------*/

View File

@ -147,7 +147,7 @@ DEF_macosx= -DLUASOCKET_$(DEBUG) -DUNIX_HAS_SUN_LEN -DLUA_$(COMPAT)_MODULE \
-DLUASOCKET_API='__attribute__((visibility("default")))' \ -DLUASOCKET_API='__attribute__((visibility("default")))' \
-DUNIX_API='__attribute__((visibility("default")))' \ -DUNIX_API='__attribute__((visibility("default")))' \
-DMIME_API='__attribute__((visibility("default")))' -DMIME_API='__attribute__((visibility("default")))'
CFLAGS_macosx= -I$(LUAINC) $(DEF) -pedantic -Wall -O2 -fno-common \ CFLAGS_macosx= -I$(LUAINC) $(DEF) -Wall -O2 -fno-common \
-fvisibility=hidden -fvisibility=hidden
LDFLAGS_macosx= -bundle -undefined dynamic_lookup -o LDFLAGS_macosx= -bundle -undefined dynamic_lookup -o
LD_macosx= export MACOSX_DEPLOYMENT_TARGET="10.3"; gcc LD_macosx= export MACOSX_DEPLOYMENT_TARGET="10.3"; gcc
@ -163,7 +163,7 @@ DEF_linux=-DLUASOCKET_$(DEBUG) -DLUA_$(COMPAT)_MODULE \
-DLUASOCKET_API='__attribute__((visibility("default")))' \ -DLUASOCKET_API='__attribute__((visibility("default")))' \
-DUNIX_API='__attribute__((visibility("default")))' \ -DUNIX_API='__attribute__((visibility("default")))' \
-DMIME_API='__attribute__((visibility("default")))' -DMIME_API='__attribute__((visibility("default")))'
CFLAGS_linux= -I$(LUAINC) $(DEF) -pedantic -Wall -Wshadow -Wextra \ CFLAGS_linux= -I$(LUAINC) $(DEF) -Wall -Wshadow -Wextra \
-Wimplicit -O2 -ggdb3 -fpic -fvisibility=hidden -Wimplicit -O2 -ggdb3 -fpic -fvisibility=hidden
LDFLAGS_linux=-O -shared -fpic -o LDFLAGS_linux=-O -shared -fpic -o
LD_linux=gcc LD_linux=gcc
@ -179,7 +179,7 @@ DEF_freebsd=-DLUASOCKET_$(DEBUG) -DLUA_$(COMPAT)_MODULE \
-DLUASOCKET_API='__attribute__((visibility("default")))' \ -DLUASOCKET_API='__attribute__((visibility("default")))' \
-DUNIX_API='__attribute__((visibility("default")))' \ -DUNIX_API='__attribute__((visibility("default")))' \
-DMIME_API='__attribute__((visibility("default")))' -DMIME_API='__attribute__((visibility("default")))'
CFLAGS_freebsd= -I$(LUAINC) $(DEF) -pedantic -Wall -Wshadow -Wextra \ CFLAGS_freebsd= -I$(LUAINC) $(DEF) -Wall -Wshadow -Wextra \
-Wimplicit -O2 -ggdb3 -fpic -fvisibility=hidden -Wimplicit -O2 -ggdb3 -fpic -fvisibility=hidden
LDFLAGS_freebsd=-O -shared -fpic -o LDFLAGS_freebsd=-O -shared -fpic -o
LD_freebsd=gcc LD_freebsd=gcc
@ -194,7 +194,7 @@ CC_mingw=gcc
DEF_mingw= -DLUASOCKET_INET_PTON -DLUASOCKET_$(DEBUG) -DLUA_$(COMPAT)_MODULE \ DEF_mingw= -DLUASOCKET_INET_PTON -DLUASOCKET_$(DEBUG) -DLUA_$(COMPAT)_MODULE \
-DWINVER=0x0501 -DLUASOCKET_API='__declspec(dllexport)' \ -DWINVER=0x0501 -DLUASOCKET_API='__declspec(dllexport)' \
-DMIME_API='__declspec(dllexport)' -DMIME_API='__declspec(dllexport)'
CFLAGS_mingw= -I$(LUAINC) $(DEF) -pedantic -Wall -O2 -fno-common \ CFLAGS_mingw= -I$(LUAINC) $(DEF) -Wall -O2 -fno-common \
-fvisibility=hidden -fvisibility=hidden
LDFLAGS_mingw= $(LUALIB) -shared -Wl,-s -lws2_32 -o LDFLAGS_mingw= $(LUALIB) -shared -Wl,-s -lws2_32 -o
LD_mingw=gcc LD_mingw=gcc

View File

@ -13,6 +13,11 @@
#include "mime.h" #include "mime.h"
#if LUA_VERSION_NUM > 502 && !defined(LUA_COMPAT_APIINTCASTS)
#define luaL_checkint(L,n) ((int)luaL_checkinteger(L, (n)))
#define luaL_optint(L,n,d) ((int)luaL_optinteger(L, (n), (d)))
#endif
/*=========================================================================*\ /*=========================================================================*\
* Don't want to trust escape character constants * Don't want to trust escape character constants
\*=========================================================================*/ \*=========================================================================*/
@ -41,7 +46,7 @@ static size_t b64decode(UC c, UC *input, size_t size, luaL_Buffer *buffer);
static void qpsetup(UC *class, UC *unbase); static void qpsetup(UC *class, UC *unbase);
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 size_t qppad(UC *input, size_t size, luaL_Buffer *buffer); static size_t qppad(UC *input, size_t size, luaL_Buffer *buffer);
@ -103,9 +108,9 @@ MIME_API int luaopen_mime_core(lua_State *L)
/*-------------------------------------------------------------------------*\ /*-------------------------------------------------------------------------*\
* Incrementaly breaks a string into lines. The string can have CRLF breaks. * Incrementaly breaks a string into lines. The string can have CRLF breaks.
* A, n = wrp(l, B, length) * A, n = wrp(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.
* 'l' is how many bytes are left for the first line of B. * 'l' is how many bytes are left for the first line of B.
* 'n' is the number of bytes left in the last line of A. * 'n' is the number of bytes left in the last line of A.
\*-------------------------------------------------------------------------*/ \*-------------------------------------------------------------------------*/
static int mime_global_wrp(lua_State *L) static int mime_global_wrp(lua_State *L)
{ {
@ -123,7 +128,7 @@ static int mime_global_wrp(lua_State *L)
else lua_pushnil(L); else lua_pushnil(L);
lua_pushnumber(L, length); lua_pushnumber(L, length);
return 2; return 2;
} }
luaL_buffinit(L, &buffer); luaL_buffinit(L, &buffer);
while (input < last) { while (input < last) {
switch (*input) { switch (*input) {
@ -150,9 +155,9 @@ static int mime_global_wrp(lua_State *L)
} }
/*-------------------------------------------------------------------------*\ /*-------------------------------------------------------------------------*\
* Fill base64 decode map. * Fill base64 decode map.
\*-------------------------------------------------------------------------*/ \*-------------------------------------------------------------------------*/
static void b64setup(UC *unbase) static void b64setup(UC *unbase)
{ {
int i; int i;
for (i = 0; i <= 255; i++) unbase[i] = (UC) 255; for (i = 0; i <= 255; i++) unbase[i] = (UC) 255;
@ -161,11 +166,11 @@ static void b64setup(UC *unbase)
} }
/*-------------------------------------------------------------------------*\ /*-------------------------------------------------------------------------*\
* Acumulates bytes in input buffer until 3 bytes are available. * Acumulates bytes in input buffer until 3 bytes are available.
* Translate the 3 bytes into Base64 form and append to buffer. * Translate the 3 bytes into Base64 form and append to buffer.
* Returns new number of bytes in buffer. * Returns new number of bytes in buffer.
\*-------------------------------------------------------------------------*/ \*-------------------------------------------------------------------------*/
static size_t b64encode(UC c, UC *input, size_t size, static size_t b64encode(UC c, UC *input, size_t size,
luaL_Buffer *buffer) luaL_Buffer *buffer)
{ {
input[size++] = c; input[size++] = c;
@ -174,7 +179,7 @@ static size_t b64encode(UC c, UC *input, size_t size,
unsigned long value = 0; unsigned long value = 0;
value += input[0]; value <<= 8; value += input[0]; value <<= 8;
value += input[1]; value <<= 8; value += input[1]; value <<= 8;
value += input[2]; value += input[2];
code[3] = b64base[value & 0x3f]; value >>= 6; code[3] = b64base[value & 0x3f]; value >>= 6;
code[2] = b64base[value & 0x3f]; value >>= 6; code[2] = b64base[value & 0x3f]; value >>= 6;
code[1] = b64base[value & 0x3f]; value >>= 6; code[1] = b64base[value & 0x3f]; value >>= 6;
@ -186,11 +191,11 @@ static size_t b64encode(UC c, UC *input, size_t size,
} }
/*-------------------------------------------------------------------------*\ /*-------------------------------------------------------------------------*\
* Encodes the Base64 last 1 or 2 bytes and adds padding '=' * Encodes the Base64 last 1 or 2 bytes and adds padding '='
* Result, if any, is appended to buffer. * Result, if any, is appended to buffer.
* Returns 0. * Returns 0.
\*-------------------------------------------------------------------------*/ \*-------------------------------------------------------------------------*/
static size_t b64pad(const UC *input, size_t size, static size_t b64pad(const UC *input, size_t size,
luaL_Buffer *buffer) luaL_Buffer *buffer)
{ {
unsigned long value = 0; unsigned long value = 0;
@ -203,7 +208,7 @@ static size_t b64pad(const UC *input, size_t size,
luaL_addlstring(buffer, (char *) code, 4); luaL_addlstring(buffer, (char *) code, 4);
break; break;
case 2: case 2:
value = input[0]; value <<= 8; value = input[0]; value <<= 8;
value |= input[1]; value <<= 2; value |= input[1]; value <<= 2;
code[2] = b64base[value & 0x3f]; value >>= 6; code[2] = b64base[value & 0x3f]; value >>= 6;
code[1] = b64base[value & 0x3f]; value >>= 6; code[1] = b64base[value & 0x3f]; value >>= 6;
@ -217,11 +222,11 @@ static size_t b64pad(const UC *input, size_t size,
} }
/*-------------------------------------------------------------------------*\ /*-------------------------------------------------------------------------*\
* Acumulates bytes in input buffer until 4 bytes are available. * Acumulates bytes in input buffer until 4 bytes are available.
* Translate the 4 bytes from Base64 form and append to buffer. * Translate the 4 bytes from Base64 form and append to buffer.
* Returns new number of bytes in buffer. * Returns new number of bytes in buffer.
\*-------------------------------------------------------------------------*/ \*-------------------------------------------------------------------------*/
static size_t b64decode(UC c, UC *input, size_t size, static size_t b64decode(UC c, UC *input, size_t size,
luaL_Buffer *buffer) luaL_Buffer *buffer)
{ {
/* ignore invalid characters */ /* ignore invalid characters */
@ -239,7 +244,7 @@ static size_t b64decode(UC c, UC *input, size_t size,
decoded[1] = (UC) (value & 0xff); value >>= 8; decoded[1] = (UC) (value & 0xff); value >>= 8;
decoded[0] = (UC) value; decoded[0] = (UC) value;
/* take care of paddding */ /* take care of paddding */
valid = (input[2] == '=') ? 1 : (input[3] == '=') ? 2 : 3; valid = (input[2] == '=') ? 1 : (input[3] == '=') ? 2 : 3;
luaL_addlstring(buffer, (char *) decoded, valid); luaL_addlstring(buffer, (char *) decoded, valid);
return 0; return 0;
/* need more data */ /* need more data */
@ -251,7 +256,7 @@ static size_t b64decode(UC c, UC *input, size_t size,
* A, B = b64(C, D) * A, B = b64(C, D)
* A is the encoded version of the largest prefix of C .. D that is * A is the encoded version of the largest prefix of C .. D that is
* divisible by 3. B has the remaining bytes of C .. D, *without* encoding. * divisible by 3. B has the remaining bytes of C .. D, *without* encoding.
* The easiest thing would be to concatenate the two strings and * The easiest thing would be to concatenate the two strings and
* encode the result, but we can't afford that or Lua would dupplicate * encode the result, but we can't afford that or Lua would dupplicate
* every chunk we received. * every chunk we received.
\*-------------------------------------------------------------------------*/ \*-------------------------------------------------------------------------*/
@ -272,7 +277,7 @@ static int mime_global_b64(lua_State *L)
lua_settop(L, 2); lua_settop(L, 2);
/* process first part of the input */ /* process first part of the input */
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 *) luaL_optlstring(L, 2, NULL, &isize);
/* if second part is nil, we are done */ /* if second part is nil, we are done */
@ -288,7 +293,7 @@ static int mime_global_b64(lua_State *L)
} }
/* otherwise process the second part */ /* otherwise process the second part */
last = input + isize; last = input + isize;
while (input < last) while (input < last)
asize = b64encode(*input++, atom, asize, &buffer); asize = b64encode(*input++, atom, asize, &buffer);
luaL_pushresult(&buffer); luaL_pushresult(&buffer);
lua_pushlstring(L, (char *) atom, asize); lua_pushlstring(L, (char *) atom, asize);
@ -318,7 +323,7 @@ static int mime_global_unb64(lua_State *L)
lua_settop(L, 2); lua_settop(L, 2);
/* process first part of the input */ /* process first part of the input */
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 *) luaL_optlstring(L, 2, NULL, &isize);
/* if second is nil, we are done */ /* if second is nil, we are done */
@ -333,7 +338,7 @@ static int mime_global_unb64(lua_State *L)
} }
/* otherwise, process the rest of the input */ /* otherwise, process the rest of the input */
last = input + isize; last = input + isize;
while (input < last) while (input < last)
asize = b64decode(*input++, atom, asize, &buffer); asize = b64decode(*input++, atom, asize, &buffer);
luaL_pushresult(&buffer); luaL_pushresult(&buffer);
lua_pushlstring(L, (char *) atom, asize); lua_pushlstring(L, (char *) atom, asize);
@ -349,7 +354,7 @@ static int mime_global_unb64(lua_State *L)
* 9 and 32 can be plain, unless in the end of a line, where must be =XX * 9 and 32 can be plain, unless in the end of a line, where must be =XX
* encoded lines must be no longer than 76 not counting CRLF * encoded lines must be no longer than 76 not counting CRLF
* soft line-break are =CRLF * soft line-break are =CRLF
* To encode one byte, we need to see the next two. * To encode one byte, we need to see the next two.
* Worst case is when we see a space, and wonder if a CRLF is comming * Worst case is when we see a space, and wonder if a CRLF is comming
\*-------------------------------------------------------------------------*/ \*-------------------------------------------------------------------------*/
/*-------------------------------------------------------------------------*\ /*-------------------------------------------------------------------------*\
@ -362,7 +367,7 @@ static void qpsetup(UC *cl, UC *unbase)
for (i = 0; i < 256; i++) cl[i] = QP_QUOTED; for (i = 0; i < 256; i++) cl[i] = QP_QUOTED;
for (i = 33; i <= 60; i++) cl[i] = QP_PLAIN; for (i = 33; i <= 60; i++) cl[i] = QP_PLAIN;
for (i = 62; i <= 126; i++) cl[i] = QP_PLAIN; for (i = 62; i <= 126; i++) cl[i] = QP_PLAIN;
cl['\t'] = QP_IF_LAST; cl['\t'] = QP_IF_LAST;
cl[' '] = QP_IF_LAST; cl[' '] = QP_IF_LAST;
cl['\r'] = QP_CR; cl['\r'] = QP_CR;
for (i = 0; i < 256; i++) unbase[i] = 255; for (i = 0; i < 256; i++) unbase[i] = 255;
@ -388,9 +393,9 @@ static void qpquote(UC c, luaL_Buffer *buffer)
/*-------------------------------------------------------------------------*\ /*-------------------------------------------------------------------------*\
* Accumulate characters until we are sure about how to deal with them. * Accumulate characters until we are sure about how to deal with them.
* Once we are sure, output to the buffer, in the correct form. * Once we are sure, output to the buffer, in the correct form.
\*-------------------------------------------------------------------------*/ \*-------------------------------------------------------------------------*/
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)
{ {
input[size++] = c; input[size++] = c;
@ -431,7 +436,7 @@ static size_t qpencode(UC c, UC *input, size_t size,
} }
/*-------------------------------------------------------------------------*\ /*-------------------------------------------------------------------------*\
* Deal with the final characters * Deal with the final characters
\*-------------------------------------------------------------------------*/ \*-------------------------------------------------------------------------*/
static size_t qppad(UC *input, size_t size, luaL_Buffer *buffer) static size_t qppad(UC *input, size_t size, luaL_Buffer *buffer)
{ {
@ -448,8 +453,8 @@ static size_t qppad(UC *input, size_t size, luaL_Buffer *buffer)
* Incrementally converts a string to quoted-printable * Incrementally converts a string to quoted-printable
* A, B = qp(C, D, marker) * A, B = qp(C, D, marker)
* Marker is the text to be used to replace CRLF sequences found in A. * Marker is the text to be used to replace CRLF sequences found in A.
* A is the encoded version of the largest prefix of C .. D that * A is the encoded version of the largest prefix of C .. D that
* can be encoded without doubts. * can be encoded without doubts.
* B has the remaining bytes of C .. D, *without* encoding. * B has the remaining bytes of C .. D, *without* encoding.
\*-------------------------------------------------------------------------*/ \*-------------------------------------------------------------------------*/
static int mime_global_qp(lua_State *L) static int mime_global_qp(lua_State *L)
@ -493,7 +498,7 @@ static int mime_global_qp(lua_State *L)
/*-------------------------------------------------------------------------*\ /*-------------------------------------------------------------------------*\
* Accumulate characters until we are sure about how to deal with them. * Accumulate characters until we are sure about how to deal with them.
* Once we are sure, output the to the buffer, in the correct form. * Once we are sure, output the to the buffer, in the correct form.
\*-------------------------------------------------------------------------*/ \*-------------------------------------------------------------------------*/
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) {
int d; int d;
@ -501,8 +506,8 @@ static size_t qpdecode(UC c, UC *input, size_t size, luaL_Buffer *buffer) {
/* deal with all characters we can deal */ /* deal with all characters we can deal */
switch (input[0]) { switch (input[0]) {
/* if we have an escape character */ /* if we have an escape character */
case '=': case '=':
if (size < 3) return size; if (size < 3) return size;
/* eliminate soft line break */ /* eliminate soft line break */
if (input[1] == '\r' && input[2] == '\n') return 0; if (input[1] == '\r' && input[2] == '\n') return 0;
/* decode quoted representation */ /* decode quoted representation */
@ -512,7 +517,7 @@ static size_t qpdecode(UC c, UC *input, size_t size, luaL_Buffer *buffer) {
else luaL_addchar(buffer, (char) ((c << 4) + d)); else luaL_addchar(buffer, (char) ((c << 4) + d));
return 0; return 0;
case '\r': case '\r':
if (size < 2) return size; if (size < 2) return size;
if (input[1] == '\n') luaL_addlstring(buffer, (char *)input, 2); if (input[1] == '\n') luaL_addlstring(buffer, (char *)input, 2);
return 0; return 0;
default: default:
@ -525,8 +530,8 @@ static size_t qpdecode(UC c, UC *input, size_t size, luaL_Buffer *buffer) {
/*-------------------------------------------------------------------------*\ /*-------------------------------------------------------------------------*\
* Incrementally decodes a string in quoted-printable * Incrementally decodes a string in quoted-printable
* A, B = qp(C, D) * A, B = qp(C, D)
* A is the decoded version of the largest prefix of C .. D that * A is the decoded version of the largest prefix of C .. D that
* can be decoded without doubts. * can be decoded without doubts.
* B has the remaining bytes of C .. D, *without* decoding. * B has the remaining bytes of C .. D, *without* decoding.
\*-------------------------------------------------------------------------*/ \*-------------------------------------------------------------------------*/
static int mime_global_unqp(lua_State *L) static int mime_global_unqp(lua_State *L)
@ -555,7 +560,7 @@ static int mime_global_unqp(lua_State *L)
if (!(*lua_tostring(L, -1))) lua_pushnil(L); if (!(*lua_tostring(L, -1))) lua_pushnil(L);
lua_pushnil(L); lua_pushnil(L);
return 2; return 2;
} }
/* otherwise process rest of input */ /* otherwise process rest of input */
last = input + isize; last = input + isize;
while (input < last) while (input < last)
@ -568,9 +573,9 @@ 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 = qpwrp(l, B, length) * A, n = qpwrp(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.
* 'l' is how many bytes are left for the first line of B. * 'l' is how many bytes are left for the first line of B.
* 'n' is the number 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
\*-------------------------------------------------------------------------*/ \*-------------------------------------------------------------------------*/
@ -603,11 +608,11 @@ static int mime_global_qpwrp(lua_State *L)
if (left <= 3) { if (left <= 3) {
left = length; left = length;
luaL_addstring(&buffer, EQCRLF); luaL_addstring(&buffer, EQCRLF);
} }
luaL_addchar(&buffer, *input); luaL_addchar(&buffer, *input);
left--; left--;
break; break;
default: default:
if (left <= 1) { if (left <= 1) {
left = length; left = length;
luaL_addstring(&buffer, EQCRLF); luaL_addstring(&buffer, EQCRLF);
@ -635,7 +640,7 @@ static int mime_global_qpwrp(lua_State *L)
* last is the previous character * last is the previous character
\*-------------------------------------------------------------------------*/ \*-------------------------------------------------------------------------*/
#define eolcandidate(c) (c == '\r' || c == '\n') #define eolcandidate(c) (c == '\r' || c == '\n')
static int eolprocess(int c, int last, const char *marker, static int eolprocess(int c, int last, const char *marker,
luaL_Buffer *buffer) luaL_Buffer *buffer)
{ {
if (eolcandidate(c)) { if (eolcandidate(c)) {
@ -653,10 +658,10 @@ static int eolprocess(int c, int last, const char *marker,
} }
/*-------------------------------------------------------------------------*\ /*-------------------------------------------------------------------------*\
* Converts a string to uniform EOL convention. * Converts a string to uniform EOL convention.
* A, n = eol(o, B, marker) * A, n = eol(o, B, marker)
* A is the converted version of the largest prefix of B that can be * A is the converted version of the largest prefix of B that can be
* converted unambiguously. 'o' is the context returned by the previous * converted unambiguously. 'o' is the context returned by the previous
* call. 'n' is the new context. * call. 'n' is the new context.
\*-------------------------------------------------------------------------*/ \*-------------------------------------------------------------------------*/
static int mime_global_eol(lua_State *L) static int mime_global_eol(lua_State *L)
@ -683,18 +688,18 @@ static int mime_global_eol(lua_State *L)
} }
/*-------------------------------------------------------------------------*\ /*-------------------------------------------------------------------------*\
* Takes one byte and stuff it if needed. * Takes one byte and stuff it if needed.
\*-------------------------------------------------------------------------*/ \*-------------------------------------------------------------------------*/
static size_t dot(int c, size_t state, luaL_Buffer *buffer) static size_t dot(int c, size_t state, luaL_Buffer *buffer)
{ {
luaL_addchar(buffer, (char) c); luaL_addchar(buffer, (char) c);
switch (c) { switch (c) {
case '\r': case '\r':
return 1; return 1;
case '\n': case '\n':
return (state == 1)? 2: 0; return (state == 1)? 2: 0;
case '.': case '.':
if (state == 2) if (state == 2)
luaL_addchar(buffer, '.'); luaL_addchar(buffer, '.');
default: default:
return 0; return 0;
@ -719,7 +724,7 @@ static int mime_global_dot(lua_State *L)
} }
/* process all input */ /* process all input */
luaL_buffinit(L, &buffer); luaL_buffinit(L, &buffer);
while (input < last) while (input < last)
state = dot(*input++, state, &buffer); state = dot(*input++, state, &buffer);
luaL_pushresult(&buffer); luaL_pushresult(&buffer);
lua_pushnumber(L, (lua_Number) state); lua_pushnumber(L, (lua_Number) state);

4
test/auth/.htaccess Normal file
View File

@ -0,0 +1,4 @@
AuthName "test-auth"
AuthType Basic
AuthUserFile /Users/diego/impa/luasocket/test/auth/.htpasswd
Require valid-user

View File

@ -1 +1 @@
luasocket:l8n2npozPB.sQ luasocket:$apr1$47u2O.Me$.m/5BWAtt7GVoxsouIPBR1

View File

@ -1,4 +1,4 @@
-- needs Alias from /home/c/diego/tec/luasocket/test to -- needs Alias from /home/c/diego/tec/luasocket/test to
-- "/luasocket-test" and "/luasocket-test/" -- "/luasocket-test" and "/luasocket-test/"
-- needs ScriptAlias from /home/c/diego/tec/luasocket/test/cgi -- needs ScriptAlias from /home/c/diego/tec/luasocket/test/cgi
-- to "/luasocket-test-cgi" and "/luasocket-test-cgi/" -- to "/luasocket-test-cgi" and "/luasocket-test-cgi/"
@ -36,22 +36,22 @@ index = readfile(index_file)
local check_result = function(response, expect, ignore) local check_result = function(response, expect, ignore)
for i,v in pairs(response) do for i,v in pairs(response) do
if not ignore[i] then if not ignore[i] then
if v ~= expect[i] then if v ~= expect[i] then
local f = io.open("err", "w") local f = io.open("err", "w")
f:write(tostring(v), "\n\n versus\n\n", tostring(expect[i])) f:write(tostring(v), "\n\n versus\n\n", tostring(expect[i]))
f:close() f:close()
fail(i .. " differs!") fail(i .. " differs!")
end end
end end
end end
for i,v in pairs(expect) do for i,v in pairs(expect) do
if not ignore[i] then if not ignore[i] then
if v ~= response[i] then if v ~= response[i] then
local f = io.open("err", "w") local f = io.open("err", "w")
f:write(tostring(response[i]), "\n\n versus\n\n", tostring(v)) f:write(tostring(response[i]), "\n\n versus\n\n", tostring(v))
v = string.sub(type(v) == "string" and v or "", 1, 70) v = string.sub(type(v) == "string" and v or "", 1, 70)
f:close() f:close()
fail(i .. " differs!") fail(i .. " differs!")
end end
end end
end end
@ -61,10 +61,10 @@ end
local check_request = function(request, expect, ignore) local check_request = function(request, expect, ignore)
local t local t
if not request.sink then request.sink, t = ltn12.sink.table() end if not request.sink then request.sink, t = ltn12.sink.table() end
request.source = request.source or request.source = request.source or
(request.body and ltn12.source.string(request.body)) (request.body and ltn12.source.string(request.body))
local response = {} local response = {}
response.code, response.headers, response.status = response.code, response.headers, response.status =
socket.skip(1, http.request(request)) socket.skip(1, http.request(request))
if t and #t > 0 then response.body = table.concat(t) end if t and #t > 0 then response.body = table.concat(t) end
check_result(response, expect, ignore) check_result(response, expect, ignore)
@ -82,7 +82,7 @@ else fail(back.query) 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 = http.request("http://" .. host .. cgiprefix .. back = http.request("http://" .. host .. cgiprefix ..
"/query-string?" .. forth) "/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
@ -120,10 +120,10 @@ check_request(request, expect, ignore)
------------------------------------------------------------------------ ------------------------------------------------------------------------
io.write("testing invalid url: ") io.write("testing invalid url: ")
local r, e = http.request{url = host .. prefix} local r, e = http.request{url = host .. prefix}
assert(r == nil and e == "invalid host ''") assert(r == nil and e == "invalid host ''")
r, re = http.request(host .. prefix) r, re = http.request(host .. prefix)
assert(r == nil and e == re, tostring(r) ..", " .. tostring(re) .. assert(r == nil and e == re, tostring(r) ..", " .. tostring(re) ..
" vs " .. tostring(e)) " vs " .. tostring(e))
print("ok") print("ok")
io.write("testing invalid empty port: ") io.write("testing invalid empty port: ")
@ -212,7 +212,7 @@ os.remove(index_file .. "-back")
io.write("testing ltn12.(sink|source).chain and mime.(encode|decode): ") io.write("testing ltn12.(sink|source).chain and mime.(encode|decode): ")
local function b64length(len) local function b64length(len)
local a = math.ceil(len/3)*4 local a = math.ceil(len/3)*4
local l = math.ceil(a/76) local l = math.ceil(a/76)
return a + l*2 return a + l*2
end end
@ -313,7 +313,7 @@ ignore = {
headers = 1 headers = 1
} }
check_request(request, expect, ignore) check_request(request, expect, ignore)
------------------------------------------------------------------------ ------------------------------------------------------------------------
io.write("testing document not found: ") io.write("testing document not found: ")
request = { request = {
@ -429,9 +429,9 @@ print("ok")
io.write("testing host not found: ") io.write("testing host not found: ")
local c, e = socket.connect("example.invalid", 80) local c, e = socket.connect("example.invalid", 80)
local r, re = http.request{url = "http://example.invalid/does/not/exist"} local r, re = http.request{url = "http://example.invalid/does/not/exist"}
assert(r == nil and e == re, tostring(r) .. " " .. tostring(re)) assert(r == nil and e == re, tostring(r) .. " " .. tostring(re))
r, re = http.request("http://example.invalid/does/not/exist") r, re = http.request("http://example.invalid/does/not/exist")
assert(r == nil and e == re) assert(r == nil and e == re)
print("ok") print("ok")
------------------------------------------------------------------------ ------------------------------------------------------------------------