From dfb550308d111322919ae73bcc7393236dba9c2f Mon Sep 17 00:00:00 2001 From: DanyLE Date: Fri, 19 Aug 2022 19:59:10 +0200 Subject: [PATCH] update API to support binary POST data --- lib/asl/antd.c | 25 +++++++++++++++++++++++-- lib/asl/handle.c | 10 +++++++++- lib/lualib.h | 2 +- 3 files changed, 33 insertions(+), 4 deletions(-) diff --git a/lib/asl/antd.c b/lib/asl/antd.c index 848900b..503a821 100644 --- a/lib/asl/antd.c +++ b/lib/asl/antd.c @@ -4,17 +4,37 @@ // add a length field, and void lua_new_byte_array(lua_State*L, int n) { - size_t nbytes = sizeof(byte_array_t) + (n-1)*sizeof(unsigned char); - byte_array_t *a = (byte_array_t *)lua_newuserdata(L, nbytes); + size_t nbytes = sizeof(byte_array_t) + n*sizeof(unsigned char); + byte_array_t *a = (byte_array_t *)lua_newuserdata(L, nbytes); + a->data = &((char*)a)[sizeof(byte_array_t)]; luaL_getmetatable(L, BYTEARRAY); lua_setmetatable(L, -2); a->size = n; } + +void lua_new_light_byte_array(lua_State*L, int n, char* ptr) +{ + size_t nbytes = sizeof(byte_array_t); + byte_array_t *a = (byte_array_t *)lua_newuserdata(L, nbytes); + a->size = n; + a->data = ptr; + luaL_getmetatable(L, BYTEARRAY); + lua_setmetatable(L, -2); +} + static int l_new_barray (lua_State *L) { int n = luaL_checknumber(L, 1); lua_new_byte_array(L,n); return 1; /* new userdatum is already on the stack */ } + +static int l_new_lightbarray (lua_State *L) { + unsigned char * ptr = lua_touserdata(L,1); + int n = luaL_checknumber(L, 2); + lua_new_light_byte_array(L,n, ptr); + return 1; /* new userdatum is already on the stack */ +} + byte_array_t *l_check_barray (lua_State *L,int idx) { void *ud = luaL_checkudata(L, idx, BYTEARRAY); luaL_argcheck(L, ud != NULL, idx, "`byte array' expected"); @@ -78,6 +98,7 @@ static int l_barray_write(lua_State* L) } static const struct luaL_Reg barraylib[] = { + {"unew", l_new_lightbarray}, {"new", l_new_barray}, {"set", l_set_barray}, {"get", l_get_barray}, diff --git a/lib/asl/handle.c b/lib/asl/handle.c index 948d798..6a88c6a 100644 --- a/lib/asl/handle.c +++ b/lib/asl/handle.c @@ -23,7 +23,14 @@ static void push_dict_to_lua(lua_State* L, dictionary_t d) push_dict_to_lua(L, (dictionary_t)as->value); else { - lua_pushstring(L,as->value); + if(strncmp(as->key,"octet-stream",12) == 0) + { + lua_pushlightuserdata(L, as->value); + } + else + { + lua_pushstring(L,as->value); + } //printf("VALUE : %s\n",as->value ); } lua_settable(L, -3); @@ -76,6 +83,7 @@ void* lua_handle(void* data, void* meta) lua_pushlightuserdata(L, rq->client); //lua_pushnumber(L,client); lua_settable(L, -3); + lua_pushstring(L,"request"); push_dict_to_lua(L,rq->request); lua_settable(L, -3); diff --git a/lib/lualib.h b/lib/lualib.h index fdf4280..8e55a54 100644 --- a/lib/lualib.h +++ b/lib/lualib.h @@ -20,7 +20,7 @@ // add byte array support typedef struct{ int size; - unsigned char data[1]; + unsigned char * data; } byte_array_t; typedef struct{