From: Daniel Gruno Date: Wed, 5 Feb 2014 12:13:11 +0000 (+0000) Subject: Backport r1564727: Fix support for uploading files by using pushlstring instead of... X-Git-Tag: 2.4.8~145 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=b508cd24d3d87bd504d2705490628b2e86a58a93;p=apache Backport r1564727: Fix support for uploading files by using pushlstring instead of pushstring when pushing binary data. git-svn-id: https://svn.apache.org/repos/asf/httpd/httpd/branches/2.4.x@1564729 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/CHANGES b/CHANGES index 2389435524..c2c9aa01da 100644 --- a/CHANGES +++ b/CHANGES @@ -45,6 +45,9 @@ Changes with Apache 2.4.8 *) mod_socache_shmcb.c: Remove arbitrary restriction on shared memory size previously limited to 64MB. [Jens Låås ] + *) mod_lua: Use binary copy when dealing with uploads through r:parsebody() + to prevent truncating files. [Daniel Gruno] + Changes with Apache 2.4.7 *) APR 1.5.0 or later is now required for the event MPM. diff --git a/modules/lua/lua_request.c b/modules/lua/lua_request.c index efd22d059a..252df42e84 100644 --- a/modules/lua/lua_request.c +++ b/modules/lua/lua_request.c @@ -171,6 +171,55 @@ static int req_aprtable2luatable_cb(void *l, const char *key, return 1; } +/* helper callback for req_parseargs */ +static int req_aprtable2luatable_cb_len(void *l, const char *key, + const char *value, size_t len) +{ + int t; + lua_State *L = (lua_State *) l; /* [table, table] */ + /* rstack_dump(L, RRR, "start of cb"); */ + /* L is [table, table] */ + /* build complex */ + + lua_getfield(L, -1, key); /* [VALUE, table, table] */ + /* rstack_dump(L, RRR, "after getfield"); */ + t = lua_type(L, -1); + switch (t) { + case LUA_TNIL: + case LUA_TNONE:{ + lua_pop(L, 1); /* [table, table] */ + lua_newtable(L); /* [array, table, table] */ + lua_pushnumber(L, 1); /* [1, array, table, table] */ + lua_pushlstring(L, value, len); /* [string, 1, array, table, table] */ + lua_settable(L, -3); /* [array, table, table] */ + lua_setfield(L, -2, key); /* [table, table] */ + break; + } + case LUA_TTABLE:{ + /* [array, table, table] */ + int size = lua_objlen(L, -1); + lua_pushnumber(L, size + 1); /* [#, array, table, table] */ + lua_pushlstring(L, value, len); /* [string, #, array, table, table] */ + lua_settable(L, -3); /* [array, table, table] */ + lua_setfield(L, -2, key); /* [table, table] */ + break; + } + } + + /* L is [table, table] */ + /* build simple */ + lua_getfield(L, -2, key); /* [VALUE, table, table] */ + if (lua_isnoneornil(L, -1)) { /* only set if not already set */ + lua_pop(L, 1); /* [table, table]] */ + lua_pushlstring(L, value, len); /* [string, table, table] */ + lua_setfield(L, -3, key); /* [table, table] */ + } + else { + lua_pop(L, 1); + } + return 1; +} + /* ======================================================================================================================= @@ -313,7 +362,7 @@ static int req_parsebody(lua_State *L) "Content-Disposition: form-data; name=\"%255[^\"]\"; filename=\"%255[^\"]\"", key, filename); if (strlen(key)) { - req_aprtable2luatable_cb(L, key, buffer); + req_aprtable2luatable_cb_len(L, key, buffer, vlen); } } }