1
0
mirror of https://github.com/apache/httpd.git synced 2025-08-08 15:02:10 +03:00

mod_lua: Redesign the table construction/access mechanism, so we pass on a struct with the request_rec, the table pointer and the table name instead of just the table pointer. This allows us to use the request_rec for logging/editing purposes, as well as inform the user which exact table in the request_rec was modified.

git-svn-id: https://svn.apache.org/repos/asf/httpd/httpd/trunk@1582858 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Daniel Gruno
2014-03-28 18:38:41 +00:00
parent 8ae663bc78
commit d38e1b6e83
5 changed files with 74 additions and 30 deletions

View File

@@ -17,17 +17,18 @@
#include "mod_lua.h"
#include "lua_apr.h"
APLOG_USE_MODULE(lua);
apr_table_t *ap_lua_check_apr_table(lua_State *L, int index)
req_table_t *ap_lua_check_apr_table(lua_State *L, int index)
{
apr_table_t *t;
req_table_t* t;
luaL_checkudata(L, index, "Apr.Table");
t = lua_unboxpointer(L, index);
return t;
}
void ap_lua_push_apr_table(lua_State *L, apr_table_t *t)
void ap_lua_push_apr_table(lua_State *L, req_table_t *t)
{
lua_boxpointer(L, t);
luaL_getmetatable(L, "Apr.Table");
@@ -36,26 +37,35 @@ void ap_lua_push_apr_table(lua_State *L, apr_table_t *t)
static int lua_table_set(lua_State *L)
{
apr_table_t *t = ap_lua_check_apr_table(L, 1);
req_table_t *t = ap_lua_check_apr_table(L, 1);
const char *key = luaL_checkstring(L, 2);
const char *val = luaL_checkstring(L, 3);
/* Prevent response/header splitting by not allowing newlines in tables.
* At this stage, we don't have the request_rec handy, and we can't change
* a const char*, so we'll redirect to a standard error value instead.
*/
if (ap_strchr_c(val, '\n')) {
val = "[ERROR: Value contains newline, ignored.]";
/* Unless it's the 'notes' table, check for newline chars */
if (strcmp(t->n, "notes") && ap_strchr_c(val, '\n')) {
char *badchar;
char *replacement = apr_pstrdup(t->r->pool, val);
badchar = replacement;
while ( (badchar = ap_strchr(badchar, '\n')) ) {
*badchar = ' ';
}
if (t->r != NULL) {
ap_log_rerror(APLOG_MARK, APLOG_WARNING, 0, t->r,
APLOGNO(02614) "mod_lua: Value for '%s' in table '%s' contains newline!",
key, t->n);
}
apr_table_set(t->t, key, replacement);
}
else {
apr_table_set(t->t, key, val);
}
apr_table_set(t, key, val);
return 0;
}
static int lua_table_get(lua_State *L)
{
apr_table_t *t = ap_lua_check_apr_table(L, 1);
req_table_t *t = ap_lua_check_apr_table(L, 1);
const char *key = luaL_checkstring(L, 2);
const char *val = apr_table_get(t, key);
const char *val = apr_table_get(t->t, key);
lua_pushstring(L, val);
return 1;
}