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