|
Packit |
90a5c9 |
/**
|
|
Packit |
90a5c9 |
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
Packit |
90a5c9 |
* contributor license agreements. See the NOTICE file distributed with
|
|
Packit |
90a5c9 |
* this work for additional information regarding copyright ownership.
|
|
Packit |
90a5c9 |
* The ASF licenses this file to You under the Apache License, Version 2.0
|
|
Packit |
90a5c9 |
* (the "License"); you may not use this file except in compliance with
|
|
Packit |
90a5c9 |
* the License. You may obtain a copy of the License at
|
|
Packit |
90a5c9 |
*
|
|
Packit |
90a5c9 |
* http://www.apache.org/licenses/LICENSE-2.0
|
|
Packit |
90a5c9 |
*
|
|
Packit |
90a5c9 |
* Unless required by applicable law or agreed to in writing, software
|
|
Packit |
90a5c9 |
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
Packit |
90a5c9 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
Packit |
90a5c9 |
* See the License for the specific language governing permissions and
|
|
Packit |
90a5c9 |
* limitations under the License.
|
|
Packit |
90a5c9 |
*/
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
#include "mod_lua.h"
|
|
Packit |
90a5c9 |
#include "lua_apr.h"
|
|
Packit |
90a5c9 |
APLOG_USE_MODULE(lua);
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
req_table_t *ap_lua_check_apr_table(lua_State *L, int index)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
req_table_t* t;
|
|
Packit |
90a5c9 |
luaL_checkudata(L, index, "Apr.Table");
|
|
Packit |
90a5c9 |
t = lua_unboxpointer(L, index);
|
|
Packit |
90a5c9 |
return t;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
void ap_lua_push_apr_table(lua_State *L, req_table_t *t)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
lua_boxpointer(L, t);
|
|
Packit |
90a5c9 |
luaL_getmetatable(L, "Apr.Table");
|
|
Packit |
90a5c9 |
lua_setmetatable(L, -2);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
static int lua_table_set(lua_State *L)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
req_table_t *t = ap_lua_check_apr_table(L, 1);
|
|
Packit |
90a5c9 |
const char *key = luaL_checkstring(L, 2);
|
|
Packit |
90a5c9 |
const char *val = luaL_checkstring(L, 3);
|
|
Packit |
90a5c9 |
/* Unless it's the 'notes' table, check for newline chars */
|
|
Packit |
90a5c9 |
/* t->r will be NULL in case of the connection notes, but since
|
|
Packit |
90a5c9 |
we aren't going to check anything called 'notes', we can safely
|
|
Packit |
90a5c9 |
disregard checking whether t->r is defined.
|
|
Packit |
90a5c9 |
*/
|
|
Packit |
90a5c9 |
if (strcmp(t->n, "notes") && ap_strchr_c(val, '\n')) {
|
|
Packit |
90a5c9 |
char *badchar;
|
|
Packit |
90a5c9 |
char *replacement = apr_pstrdup(t->r->pool, val);
|
|
Packit |
90a5c9 |
badchar = replacement;
|
|
Packit |
90a5c9 |
while ( (badchar = ap_strchr(badchar, '\n')) ) {
|
|
Packit |
90a5c9 |
*badchar = ' ';
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
ap_log_rerror(APLOG_MARK, APLOG_WARNING, 0, t->r, APLOGNO(02614)
|
|
Packit |
90a5c9 |
"mod_lua: Value for '%s' in table '%s' contains newline!",
|
|
Packit |
90a5c9 |
key, t->n);
|
|
Packit |
90a5c9 |
apr_table_set(t->t, key, replacement);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
else {
|
|
Packit |
90a5c9 |
apr_table_set(t->t, key, val);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
return 0;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
static int lua_table_get(lua_State *L)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
req_table_t *t = ap_lua_check_apr_table(L, 1);
|
|
Packit |
90a5c9 |
const char *key = luaL_checkstring(L, 2);
|
|
Packit |
90a5c9 |
const char *val = apr_table_get(t->t, key);
|
|
Packit |
90a5c9 |
lua_pushstring(L, val);
|
|
Packit |
90a5c9 |
return 1;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
static const luaL_Reg lua_table_methods[] = {
|
|
Packit |
90a5c9 |
{"set", lua_table_set},
|
|
Packit |
90a5c9 |
{"get", lua_table_get},
|
|
Packit |
90a5c9 |
{0, 0}
|
|
Packit |
90a5c9 |
};
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
int ap_lua_init(lua_State *L, apr_pool_t *p)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
luaL_newmetatable(L, "Apr.Table");
|
|
Packit |
90a5c9 |
#if LUA_VERSION_NUM < 502
|
|
Packit |
90a5c9 |
luaL_register(L, "apr_table", lua_table_methods);
|
|
Packit |
90a5c9 |
#else
|
|
Packit |
90a5c9 |
luaL_newlib(L, lua_table_methods);
|
|
Packit |
90a5c9 |
#endif
|
|
Packit |
90a5c9 |
lua_pushstring(L, "__index");
|
|
Packit |
90a5c9 |
lua_pushstring(L, "get");
|
|
Packit |
90a5c9 |
lua_gettable(L, 2);
|
|
Packit |
90a5c9 |
lua_settable(L, 1);
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
lua_pushstring(L, "__newindex");
|
|
Packit |
90a5c9 |
lua_pushstring(L, "set");
|
|
Packit |
90a5c9 |
lua_gettable(L, 2);
|
|
Packit |
90a5c9 |
lua_settable(L, 1);
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
return 0;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
|