lua_request.c revision b3c110de5a0e4248a2c98a92b651f7032adc7cf8
1633838b8255282d10af15c5c84cee5a51466712Bob Halley/**
ca41b452ede6feaa9d8739ec3cae19389a7b0d03Bob Halley * Licensed to the Apache Software Foundation (ASF) under one or more
1633838b8255282d10af15c5c84cee5a51466712Bob Halley * contributor license agreements. See the NOTICE file distributed with
1633838b8255282d10af15c5c84cee5a51466712Bob Halley * this work for additional information regarding copyright ownership.
1633838b8255282d10af15c5c84cee5a51466712Bob Halley * The ASF licenses this file to You under the Apache License, Version 2.0
1633838b8255282d10af15c5c84cee5a51466712Bob Halley * (the "License"); you may not use this file except in compliance with
1633838b8255282d10af15c5c84cee5a51466712Bob Halley * the License. You may obtain a copy of the License at
1633838b8255282d10af15c5c84cee5a51466712Bob Halley *
1633838b8255282d10af15c5c84cee5a51466712Bob Halley * http://www.apache.org/licenses/LICENSE-2.0
1633838b8255282d10af15c5c84cee5a51466712Bob Halley *
1633838b8255282d10af15c5c84cee5a51466712Bob Halley * Unless required by applicable law or agreed to in writing, software
1633838b8255282d10af15c5c84cee5a51466712Bob Halley * distributed under the License is distributed on an "AS IS" BASIS,
1633838b8255282d10af15c5c84cee5a51466712Bob Halley * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1633838b8255282d10af15c5c84cee5a51466712Bob Halley * See the License for the specific language governing permissions and
1633838b8255282d10af15c5c84cee5a51466712Bob Halley * limitations under the License.
1633838b8255282d10af15c5c84cee5a51466712Bob Halley */
7b5e3afd21d4f1792812ab371ee687807235dd87Bob Halley
d25afd60ee2286cb171c4960a790f3d7041b6f85Bob Halley#include "mod_lua.h"
d25afd60ee2286cb171c4960a790f3d7041b6f85Bob Halley#include "util_script.h"
7b5e3afd21d4f1792812ab371ee687807235dd87Bob Halley#include "lua_apr.h"
7b5e3afd21d4f1792812ab371ee687807235dd87Bob Halley
7b5e3afd21d4f1792812ab371ee687807235dd87Bob HalleyAPLOG_USE_MODULE(lua);
7b5e3afd21d4f1792812ab371ee687807235dd87Bob Halley
7b5e3afd21d4f1792812ab371ee687807235dd87Bob Halleytypedef char *(*req_field_string_f) (request_rec * r);
7b5e3afd21d4f1792812ab371ee687807235dd87Bob Halleytypedef int (*req_field_int_f) (request_rec * r);
08dbab20efc74209b611bf543e0e9a2743aca84aBob Halleytypedef apr_table_t *(*req_field_apr_table_f) (request_rec * r);
4556681e191b7c1654639895ce719d98f2822ee2Michael Graff
4556681e191b7c1654639895ce719d98f2822ee2Michael Graffvoid ap_lua_rstack_dump(lua_State *L, request_rec *r, const char *msg)
7b5e3afd21d4f1792812ab371ee687807235dd87Bob Halley{
7b5e3afd21d4f1792812ab371ee687807235dd87Bob Halley int i;
7b5e3afd21d4f1792812ab371ee687807235dd87Bob Halley int top = lua_gettop(L);
7b5e3afd21d4f1792812ab371ee687807235dd87Bob Halley
005df5aba5181dc57b0e84eae72929f009b6b8fdBob Halley ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, "Lua Stack Dump: [%s]", msg);
7b5e3afd21d4f1792812ab371ee687807235dd87Bob Halley
3df43dad430973e273bc55b304c6d5965390db06Michael Graff for (i = 1; i <= top; i++) {
005df5aba5181dc57b0e84eae72929f009b6b8fdBob Halley int t = lua_type(L, i);
7b5e3afd21d4f1792812ab371ee687807235dd87Bob Halley switch (t) {
7b5e3afd21d4f1792812ab371ee687807235dd87Bob Halley case LUA_TSTRING:{
7b5e3afd21d4f1792812ab371ee687807235dd87Bob Halley ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r,
005df5aba5181dc57b0e84eae72929f009b6b8fdBob Halley "%d: '%s'", i, lua_tostring(L, i));
c98d185d0cd20b745b4e3d62aa6fcc22ac0e7c51Bob Halley break;
4556681e191b7c1654639895ce719d98f2822ee2Michael Graff }
4556681e191b7c1654639895ce719d98f2822ee2Michael Graff case LUA_TUSERDATA:{
005df5aba5181dc57b0e84eae72929f009b6b8fdBob Halley ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, "%d: userdata",
005df5aba5181dc57b0e84eae72929f009b6b8fdBob Halley i);
005df5aba5181dc57b0e84eae72929f009b6b8fdBob Halley break;
005df5aba5181dc57b0e84eae72929f009b6b8fdBob Halley }
005df5aba5181dc57b0e84eae72929f009b6b8fdBob Halley case LUA_TLIGHTUSERDATA:{
005df5aba5181dc57b0e84eae72929f009b6b8fdBob Halley ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r,
005df5aba5181dc57b0e84eae72929f009b6b8fdBob Halley "%d: lightuserdata", i);
005df5aba5181dc57b0e84eae72929f009b6b8fdBob Halley break;
3df43dad430973e273bc55b304c6d5965390db06Michael Graff }
4556681e191b7c1654639895ce719d98f2822ee2Michael Graff case LUA_TNIL:{
4556681e191b7c1654639895ce719d98f2822ee2Michael Graff ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, "%d: NIL", i);
005df5aba5181dc57b0e84eae72929f009b6b8fdBob Halley break;
005df5aba5181dc57b0e84eae72929f009b6b8fdBob Halley }
005df5aba5181dc57b0e84eae72929f009b6b8fdBob Halley case LUA_TNONE:{
005df5aba5181dc57b0e84eae72929f009b6b8fdBob Halley ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, "%d: None", i);
005df5aba5181dc57b0e84eae72929f009b6b8fdBob Halley break;
005df5aba5181dc57b0e84eae72929f009b6b8fdBob Halley }
005df5aba5181dc57b0e84eae72929f009b6b8fdBob Halley case LUA_TBOOLEAN:{
c98d185d0cd20b745b4e3d62aa6fcc22ac0e7c51Bob Halley ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r,
005df5aba5181dc57b0e84eae72929f009b6b8fdBob Halley "%d: %s", i, lua_toboolean(L,
005df5aba5181dc57b0e84eae72929f009b6b8fdBob Halley i) ? "true" :
005df5aba5181dc57b0e84eae72929f009b6b8fdBob Halley "false");
005df5aba5181dc57b0e84eae72929f009b6b8fdBob Halley break;
005df5aba5181dc57b0e84eae72929f009b6b8fdBob Halley }
005df5aba5181dc57b0e84eae72929f009b6b8fdBob Halley case LUA_TNUMBER:{
005df5aba5181dc57b0e84eae72929f009b6b8fdBob Halley ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r,
005df5aba5181dc57b0e84eae72929f009b6b8fdBob Halley "%d: %g", i, lua_tonumber(L, i));
3df43dad430973e273bc55b304c6d5965390db06Michael Graff break;
005df5aba5181dc57b0e84eae72929f009b6b8fdBob Halley }
005df5aba5181dc57b0e84eae72929f009b6b8fdBob Halley case LUA_TTABLE:{
7b5e3afd21d4f1792812ab371ee687807235dd87Bob Halley ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r,
7b5e3afd21d4f1792812ab371ee687807235dd87Bob Halley "%d: <table>", i);
7b5e3afd21d4f1792812ab371ee687807235dd87Bob Halley break;
3740b569ae76295b941d57a724a43beb75b533baBob Halley }
7b5e3afd21d4f1792812ab371ee687807235dd87Bob Halley case LUA_TFUNCTION:{
7b5e3afd21d4f1792812ab371ee687807235dd87Bob Halley ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r,
7b5e3afd21d4f1792812ab371ee687807235dd87Bob Halley "%d: <function>", i);
7b5e3afd21d4f1792812ab371ee687807235dd87Bob Halley break;
3df43dad430973e273bc55b304c6d5965390db06Michael Graff }
7b5e3afd21d4f1792812ab371ee687807235dd87Bob Halley default:{
7b5e3afd21d4f1792812ab371ee687807235dd87Bob Halley ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r,
7b5e3afd21d4f1792812ab371ee687807235dd87Bob Halley "%d: unkown: -[%s]-", i, lua_typename(L, i));
7b5e3afd21d4f1792812ab371ee687807235dd87Bob Halley break;
7b5e3afd21d4f1792812ab371ee687807235dd87Bob Halley }
7b5e3afd21d4f1792812ab371ee687807235dd87Bob Halley }
7b5e3afd21d4f1792812ab371ee687807235dd87Bob Halley }
3740b569ae76295b941d57a724a43beb75b533baBob Halley}
7b5e3afd21d4f1792812ab371ee687807235dd87Bob Halley
7b5e3afd21d4f1792812ab371ee687807235dd87Bob Halley/**
7b5e3afd21d4f1792812ab371ee687807235dd87Bob Halley * Verify that the thing at index is a request_rec wrapping
7b5e3afd21d4f1792812ab371ee687807235dd87Bob Halley * userdata thingamajig and return it if it is. if it is not
3df43dad430973e273bc55b304c6d5965390db06Michael Graff * lua will enter its error handling routine.
7b5e3afd21d4f1792812ab371ee687807235dd87Bob Halley */
7b5e3afd21d4f1792812ab371ee687807235dd87Bob Halleystatic request_rec *ap_lua_check_request_rec(lua_State *L, int index)
7b5e3afd21d4f1792812ab371ee687807235dd87Bob Halley{
7b5e3afd21d4f1792812ab371ee687807235dd87Bob Halley request_rec *r;
7b5e3afd21d4f1792812ab371ee687807235dd87Bob Halley luaL_checkudata(L, index, "Apache2.Request");
7b5e3afd21d4f1792812ab371ee687807235dd87Bob Halley r = (request_rec *) lua_unboxpointer(L, index);
7b5e3afd21d4f1792812ab371ee687807235dd87Bob Halley return r;
3740b569ae76295b941d57a724a43beb75b533baBob Halley}
7b5e3afd21d4f1792812ab371ee687807235dd87Bob Halley
7b5e3afd21d4f1792812ab371ee687807235dd87Bob Halley/* ------------------ request methods -------------------- */
7b5e3afd21d4f1792812ab371ee687807235dd87Bob Halley/* helper callback for req_parseargs */
7b5e3afd21d4f1792812ab371ee687807235dd87Bob Halleystatic int req_aprtable2luatable_cb(void *l, const char *key,
3df43dad430973e273bc55b304c6d5965390db06Michael Graff const char *value)
7b5e3afd21d4f1792812ab371ee687807235dd87Bob Halley{
7b5e3afd21d4f1792812ab371ee687807235dd87Bob Halley int t;
08dbab20efc74209b611bf543e0e9a2743aca84aBob Halley lua_State *L = (lua_State *) l; /* [table<s,t>, table<s,s>] */
7b5e3afd21d4f1792812ab371ee687807235dd87Bob Halley /* rstack_dump(L, RRR, "start of cb"); */
7b5e3afd21d4f1792812ab371ee687807235dd87Bob Halley /* L is [table<s,t>, table<s,s>] */
7b5e3afd21d4f1792812ab371ee687807235dd87Bob Halley /* build complex */
7b5e3afd21d4f1792812ab371ee687807235dd87Bob Halley
3740b569ae76295b941d57a724a43beb75b533baBob Halley lua_getfield(L, -1, key); /* [VALUE, table<s,t>, table<s,s>] */
7b5e3afd21d4f1792812ab371ee687807235dd87Bob Halley /* rstack_dump(L, RRR, "after getfield"); */
7b5e3afd21d4f1792812ab371ee687807235dd87Bob Halley t = lua_type(L, -1);
7b5e3afd21d4f1792812ab371ee687807235dd87Bob Halley switch (t) {
7b5e3afd21d4f1792812ab371ee687807235dd87Bob Halley case LUA_TNIL:
3df43dad430973e273bc55b304c6d5965390db06Michael Graff case LUA_TNONE:{
7b5e3afd21d4f1792812ab371ee687807235dd87Bob Halley lua_pop(L, 1); /* [table<s,t>, table<s,s>] */
7b5e3afd21d4f1792812ab371ee687807235dd87Bob Halley lua_newtable(L); /* [array, table<s,t>, table<s,s>] */
7b5e3afd21d4f1792812ab371ee687807235dd87Bob Halley lua_pushnumber(L, 1); /* [1, array, table<s,t>, table<s,s>] */
7b5e3afd21d4f1792812ab371ee687807235dd87Bob Halley lua_pushstring(L, value); /* [string, 1, array, table<s,t>, table<s,s>] */
7b5e3afd21d4f1792812ab371ee687807235dd87Bob Halley lua_settable(L, -3); /* [array, table<s,t>, table<s,s>] */
7b5e3afd21d4f1792812ab371ee687807235dd87Bob Halley lua_setfield(L, -2, key); /* [table<s,t>, table<s,s>] */
3740b569ae76295b941d57a724a43beb75b533baBob Halley break;
7b5e3afd21d4f1792812ab371ee687807235dd87Bob Halley }
7b5e3afd21d4f1792812ab371ee687807235dd87Bob Halley case LUA_TTABLE:{
7b5e3afd21d4f1792812ab371ee687807235dd87Bob Halley /* [array, table<s,t>, table<s,s>] */
7b5e3afd21d4f1792812ab371ee687807235dd87Bob Halley int size = lua_objlen(L, -1);
3df43dad430973e273bc55b304c6d5965390db06Michael Graff lua_pushnumber(L, size + 1); /* [#, array, table<s,t>, table<s,s>] */
7b5e3afd21d4f1792812ab371ee687807235dd87Bob Halley lua_pushstring(L, value); /* [string, #, array, table<s,t>, table<s,s>] */
7b5e3afd21d4f1792812ab371ee687807235dd87Bob Halley lua_settable(L, -3); /* [array, table<s,t>, table<s,s>] */
7b5e3afd21d4f1792812ab371ee687807235dd87Bob Halley lua_setfield(L, -2, key); /* [table<s,t>, table<s,s>] */
c98d185d0cd20b745b4e3d62aa6fcc22ac0e7c51Bob Halley break;
c98d185d0cd20b745b4e3d62aa6fcc22ac0e7c51Bob Halley }
c98d185d0cd20b745b4e3d62aa6fcc22ac0e7c51Bob Halley }
c98d185d0cd20b745b4e3d62aa6fcc22ac0e7c51Bob Halley
7b5e3afd21d4f1792812ab371ee687807235dd87Bob Halley /* L is [table<s,t>, table<s,s>] */
7b5e3afd21d4f1792812ab371ee687807235dd87Bob Halley /* build simple */
7b5e3afd21d4f1792812ab371ee687807235dd87Bob Halley lua_getfield(L, -2, key); /* [VALUE, table<s,s>, table<s,t>] */
3740b569ae76295b941d57a724a43beb75b533baBob Halley if (lua_isnoneornil(L, -1)) { /* only set if not already set */
7b5e3afd21d4f1792812ab371ee687807235dd87Bob Halley lua_pop(L, 1); /* [table<s,s>, table<s,t>]] */
7b5e3afd21d4f1792812ab371ee687807235dd87Bob Halley lua_pushstring(L, value); /* [string, table<s,s>, table<s,t>] */
7b5e3afd21d4f1792812ab371ee687807235dd87Bob Halley lua_setfield(L, -3, key); /* [table<s,s>, table<s,t>] */
7b5e3afd21d4f1792812ab371ee687807235dd87Bob Halley }
3df43dad430973e273bc55b304c6d5965390db06Michael Graff else {
005df5aba5181dc57b0e84eae72929f009b6b8fdBob Halley lua_pop(L, 1);
7b5e3afd21d4f1792812ab371ee687807235dd87Bob Halley }
c98d185d0cd20b745b4e3d62aa6fcc22ac0e7c51Bob Halley return 1;
c98d185d0cd20b745b4e3d62aa6fcc22ac0e7c51Bob Halley}
7b5e3afd21d4f1792812ab371ee687807235dd87Bob Halley
7b5e3afd21d4f1792812ab371ee687807235dd87Bob Halley/* r:parseargs() returning a lua table */
7b5e3afd21d4f1792812ab371ee687807235dd87Bob Halleystatic int req_parseargs(lua_State *L)
3740b569ae76295b941d57a724a43beb75b533baBob Halley{
7b5e3afd21d4f1792812ab371ee687807235dd87Bob Halley apr_table_t *form_table;
7b5e3afd21d4f1792812ab371ee687807235dd87Bob Halley request_rec *r = ap_lua_check_request_rec(L, 1);
7b5e3afd21d4f1792812ab371ee687807235dd87Bob Halley lua_newtable(L);
7b5e3afd21d4f1792812ab371ee687807235dd87Bob Halley lua_newtable(L); /* [table, table] */
3df43dad430973e273bc55b304c6d5965390db06Michael Graff ap_args_to_table(r, &form_table);
7b5e3afd21d4f1792812ab371ee687807235dd87Bob Halley apr_table_do(req_aprtable2luatable_cb, L, form_table, NULL);
7b5e3afd21d4f1792812ab371ee687807235dd87Bob Halley return 2; /* [table<string, string>, table<string, array<string>>] */
7b5e3afd21d4f1792812ab371ee687807235dd87Bob Halley}
005df5aba5181dc57b0e84eae72929f009b6b8fdBob Halley
7b5e3afd21d4f1792812ab371ee687807235dd87Bob Halley/* wrap ap_rputs as r:puts(String) */
7b5e3afd21d4f1792812ab371ee687807235dd87Bob Halleystatic int req_puts(lua_State *L)
7b5e3afd21d4f1792812ab371ee687807235dd87Bob Halley{
3740b569ae76295b941d57a724a43beb75b533baBob Halley request_rec *r = ap_lua_check_request_rec(L, 1);
7b5e3afd21d4f1792812ab371ee687807235dd87Bob Halley
7b5e3afd21d4f1792812ab371ee687807235dd87Bob Halley int argc = lua_gettop(L);
7b5e3afd21d4f1792812ab371ee687807235dd87Bob Halley int i;
7b5e3afd21d4f1792812ab371ee687807235dd87Bob Halley
3df43dad430973e273bc55b304c6d5965390db06Michael Graff for (i = 2; i <= argc; i++) {
7b5e3afd21d4f1792812ab371ee687807235dd87Bob Halley ap_rputs(luaL_checkstring(L, i), r);
7b5e3afd21d4f1792812ab371ee687807235dd87Bob Halley }
005df5aba5181dc57b0e84eae72929f009b6b8fdBob Halley return 0;
005df5aba5181dc57b0e84eae72929f009b6b8fdBob Halley}
7b5e3afd21d4f1792812ab371ee687807235dd87Bob Halley
7b5e3afd21d4f1792812ab371ee687807235dd87Bob Halley/* wrap ap_rwrite as r:write(String) */
c98d185d0cd20b745b4e3d62aa6fcc22ac0e7c51Bob Halleystatic int req_write(lua_State *L)
c98d185d0cd20b745b4e3d62aa6fcc22ac0e7c51Bob Halley{
c98d185d0cd20b745b4e3d62aa6fcc22ac0e7c51Bob Halley request_rec *r = ap_lua_check_request_rec(L, 1);
c98d185d0cd20b745b4e3d62aa6fcc22ac0e7c51Bob Halley size_t n;
c98d185d0cd20b745b4e3d62aa6fcc22ac0e7c51Bob Halley const char *buf = luaL_checklstring(L, 2, &n);
c98d185d0cd20b745b4e3d62aa6fcc22ac0e7c51Bob Halley
3df43dad430973e273bc55b304c6d5965390db06Michael Graff ap_rwrite((void *) buf, n, r);
c98d185d0cd20b745b4e3d62aa6fcc22ac0e7c51Bob Halley return 0;
c98d185d0cd20b745b4e3d62aa6fcc22ac0e7c51Bob Halley}
c98d185d0cd20b745b4e3d62aa6fcc22ac0e7c51Bob Halley
c98d185d0cd20b745b4e3d62aa6fcc22ac0e7c51Bob Halley/* r:addoutputfilter(name|function) */
c98d185d0cd20b745b4e3d62aa6fcc22ac0e7c51Bob Halleystatic int req_add_output_filter(lua_State *L)
c98d185d0cd20b745b4e3d62aa6fcc22ac0e7c51Bob Halley{
c98d185d0cd20b745b4e3d62aa6fcc22ac0e7c51Bob Halley request_rec *r = ap_lua_check_request_rec(L, 1);
c98d185d0cd20b745b4e3d62aa6fcc22ac0e7c51Bob Halley const char *name = luaL_checkstring(L, 2);
c98d185d0cd20b745b4e3d62aa6fcc22ac0e7c51Bob Halley ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, "adding output filter %s",
c98d185d0cd20b745b4e3d62aa6fcc22ac0e7c51Bob Halley name);
c98d185d0cd20b745b4e3d62aa6fcc22ac0e7c51Bob Halley ap_add_output_filter(name, L, r, r->connection);
c98d185d0cd20b745b4e3d62aa6fcc22ac0e7c51Bob Halley return 0;
c98d185d0cd20b745b4e3d62aa6fcc22ac0e7c51Bob Halley}
c98d185d0cd20b745b4e3d62aa6fcc22ac0e7c51Bob Halley
c98d185d0cd20b745b4e3d62aa6fcc22ac0e7c51Bob Halley/* wrap ap_construct_url as r:construct_url(String) */
c98d185d0cd20b745b4e3d62aa6fcc22ac0e7c51Bob Halleystatic int req_construct_url(lua_State *L)
c98d185d0cd20b745b4e3d62aa6fcc22ac0e7c51Bob Halley{
c98d185d0cd20b745b4e3d62aa6fcc22ac0e7c51Bob Halley request_rec *r = ap_lua_check_request_rec(L, 1);
c98d185d0cd20b745b4e3d62aa6fcc22ac0e7c51Bob Halley const char *name = luaL_checkstring(L, 2);
3df43dad430973e273bc55b304c6d5965390db06Michael Graff lua_pushstring(L, ap_construct_url(r->pool, name, r));
c98d185d0cd20b745b4e3d62aa6fcc22ac0e7c51Bob Halley return 1;
c98d185d0cd20b745b4e3d62aa6fcc22ac0e7c51Bob Halley}
c98d185d0cd20b745b4e3d62aa6fcc22ac0e7c51Bob Halley/* BEGIN dispatch mathods for request_rec fields */
c98d185d0cd20b745b4e3d62aa6fcc22ac0e7c51Bob Halley
c98d185d0cd20b745b4e3d62aa6fcc22ac0e7c51Bob Halley/* not really a field, but we treat it like one */
c98d185d0cd20b745b4e3d62aa6fcc22ac0e7c51Bob Halleystatic const char *req_document_root(request_rec *r)
7b5e3afd21d4f1792812ab371ee687807235dd87Bob Halley{
3740b569ae76295b941d57a724a43beb75b533baBob Halley return ap_document_root(r);
7b5e3afd21d4f1792812ab371ee687807235dd87Bob Halley}
7b5e3afd21d4f1792812ab371ee687807235dd87Bob Halley
7b5e3afd21d4f1792812ab371ee687807235dd87Bob Halleystatic char *req_uri_field(request_rec *r)
7b5e3afd21d4f1792812ab371ee687807235dd87Bob Halley{
3df43dad430973e273bc55b304c6d5965390db06Michael Graff return r->uri;
7b5e3afd21d4f1792812ab371ee687807235dd87Bob Halley}
005df5aba5181dc57b0e84eae72929f009b6b8fdBob Halley
7b5e3afd21d4f1792812ab371ee687807235dd87Bob Halleystatic const char *req_method_field(request_rec *r)
7b5e3afd21d4f1792812ab371ee687807235dd87Bob Halley{
7b5e3afd21d4f1792812ab371ee687807235dd87Bob Halley return r->method;
3740b569ae76295b941d57a724a43beb75b533baBob Halley}
7b5e3afd21d4f1792812ab371ee687807235dd87Bob Halley
005df5aba5181dc57b0e84eae72929f009b6b8fdBob Halleystatic const char *req_hostname_field(request_rec *r)
7b5e3afd21d4f1792812ab371ee687807235dd87Bob Halley{
7b5e3afd21d4f1792812ab371ee687807235dd87Bob Halley return r->hostname;
3df43dad430973e273bc55b304c6d5965390db06Michael Graff}
005df5aba5181dc57b0e84eae72929f009b6b8fdBob Halley
7b5e3afd21d4f1792812ab371ee687807235dd87Bob Halleystatic const char *req_args_field(request_rec *r)
7b5e3afd21d4f1792812ab371ee687807235dd87Bob Halley{
7b5e3afd21d4f1792812ab371ee687807235dd87Bob Halley return r->args;
7b5e3afd21d4f1792812ab371ee687807235dd87Bob Halley}
7b5e3afd21d4f1792812ab371ee687807235dd87Bob Halley
3740b569ae76295b941d57a724a43beb75b533baBob Halleystatic const char *req_path_info_field(request_rec *r)
7b5e3afd21d4f1792812ab371ee687807235dd87Bob Halley{
7b5e3afd21d4f1792812ab371ee687807235dd87Bob Halley return r->path_info;
7b5e3afd21d4f1792812ab371ee687807235dd87Bob Halley}
7b5e3afd21d4f1792812ab371ee687807235dd87Bob Halley
3df43dad430973e273bc55b304c6d5965390db06Michael Graffstatic const char *req_canonical_filename_field(request_rec *r)
005df5aba5181dc57b0e84eae72929f009b6b8fdBob Halley{
7b5e3afd21d4f1792812ab371ee687807235dd87Bob Halley return r->canonical_filename;
7b5e3afd21d4f1792812ab371ee687807235dd87Bob Halley}
7b5e3afd21d4f1792812ab371ee687807235dd87Bob Halley
7b5e3afd21d4f1792812ab371ee687807235dd87Bob Halleystatic const char *req_filename_field(request_rec *r)
7b5e3afd21d4f1792812ab371ee687807235dd87Bob Halley{
3740b569ae76295b941d57a724a43beb75b533baBob Halley return r->filename;
7b5e3afd21d4f1792812ab371ee687807235dd87Bob Halley}
005df5aba5181dc57b0e84eae72929f009b6b8fdBob Halley
7b5e3afd21d4f1792812ab371ee687807235dd87Bob Halleystatic const char *req_user_field(request_rec *r)
7b5e3afd21d4f1792812ab371ee687807235dd87Bob Halley{
7b5e3afd21d4f1792812ab371ee687807235dd87Bob Halley return r->user;
7b5e3afd21d4f1792812ab371ee687807235dd87Bob Halley}
7b5e3afd21d4f1792812ab371ee687807235dd87Bob Halley
7b5e3afd21d4f1792812ab371ee687807235dd87Bob Halleystatic const char *req_unparsed_uri_field(request_rec *r)
7b5e3afd21d4f1792812ab371ee687807235dd87Bob Halley{
3df43dad430973e273bc55b304c6d5965390db06Michael Graff return r->unparsed_uri;
7b5e3afd21d4f1792812ab371ee687807235dd87Bob Halley}
005df5aba5181dc57b0e84eae72929f009b6b8fdBob Halley
005df5aba5181dc57b0e84eae72929f009b6b8fdBob Halleystatic const char *req_ap_auth_type_field(request_rec *r)
005df5aba5181dc57b0e84eae72929f009b6b8fdBob Halley{
7b5e3afd21d4f1792812ab371ee687807235dd87Bob Halley return r->ap_auth_type;
c98d185d0cd20b745b4e3d62aa6fcc22ac0e7c51Bob Halley}
c98d185d0cd20b745b4e3d62aa6fcc22ac0e7c51Bob Halley
c98d185d0cd20b745b4e3d62aa6fcc22ac0e7c51Bob Halleystatic const char *req_content_encoding_field(request_rec *r)
c98d185d0cd20b745b4e3d62aa6fcc22ac0e7c51Bob Halley{
005df5aba5181dc57b0e84eae72929f009b6b8fdBob Halley return r->content_encoding;
7b5e3afd21d4f1792812ab371ee687807235dd87Bob Halley}
7b5e3afd21d4f1792812ab371ee687807235dd87Bob Halley
63bd57250ebc55355cac6560aa0efdc887b67140Bob Halleystatic const char *req_content_type_field(request_rec *r)
71b306bf33eed22731216404921f397e6440c671Brian Wellington{
71b306bf33eed22731216404921f397e6440c671Brian Wellington return r->content_type;
71b306bf33eed22731216404921f397e6440c671Brian Wellington}
71b306bf33eed22731216404921f397e6440c671Brian Wellington
71b306bf33eed22731216404921f397e6440c671Brian Wellingtonstatic const char *req_range_field(request_rec *r)
71b306bf33eed22731216404921f397e6440c671Brian Wellington{
71b306bf33eed22731216404921f397e6440c671Brian Wellington return r->range;
71b306bf33eed22731216404921f397e6440c671Brian Wellington}
71b306bf33eed22731216404921f397e6440c671Brian Wellington
3df43dad430973e273bc55b304c6d5965390db06Michael Graffstatic const char *req_protocol_field(request_rec *r)
71b306bf33eed22731216404921f397e6440c671Brian Wellington{
71b306bf33eed22731216404921f397e6440c671Brian Wellington return r->protocol;
71b306bf33eed22731216404921f397e6440c671Brian Wellington}
71b306bf33eed22731216404921f397e6440c671Brian Wellington
71b306bf33eed22731216404921f397e6440c671Brian Wellingtonstatic const char *req_the_request_field(request_rec *r)
71b306bf33eed22731216404921f397e6440c671Brian Wellington{
71b306bf33eed22731216404921f397e6440c671Brian Wellington return r->the_request;
71b306bf33eed22731216404921f397e6440c671Brian Wellington}
71b306bf33eed22731216404921f397e6440c671Brian Wellington
71b306bf33eed22731216404921f397e6440c671Brian Wellingtonstatic int req_status_field(request_rec *r)
71b306bf33eed22731216404921f397e6440c671Brian Wellington{
71b306bf33eed22731216404921f397e6440c671Brian Wellington return r->status;
71b306bf33eed22731216404921f397e6440c671Brian Wellington}
71b306bf33eed22731216404921f397e6440c671Brian Wellington
71b306bf33eed22731216404921f397e6440c671Brian Wellingtonstatic int req_assbackwards_field(request_rec *r)
3df43dad430973e273bc55b304c6d5965390db06Michael Graff{
71b306bf33eed22731216404921f397e6440c671Brian Wellington return r->assbackwards;
71b306bf33eed22731216404921f397e6440c671Brian Wellington}
71b306bf33eed22731216404921f397e6440c671Brian Wellington
71b306bf33eed22731216404921f397e6440c671Brian Wellingtonstatic apr_table_t* req_headers_in(request_rec *r)
71b306bf33eed22731216404921f397e6440c671Brian Wellington{
71b306bf33eed22731216404921f397e6440c671Brian Wellington return r->headers_in;
71b306bf33eed22731216404921f397e6440c671Brian Wellington}
71b306bf33eed22731216404921f397e6440c671Brian Wellington
63bd57250ebc55355cac6560aa0efdc887b67140Bob Halleystatic apr_table_t* req_headers_out(request_rec *r)
63bd57250ebc55355cac6560aa0efdc887b67140Bob Halley{
63bd57250ebc55355cac6560aa0efdc887b67140Bob Halley return r->headers_out;
63bd57250ebc55355cac6560aa0efdc887b67140Bob Halley}
63bd57250ebc55355cac6560aa0efdc887b67140Bob Halley
63bd57250ebc55355cac6560aa0efdc887b67140Bob Halleystatic apr_table_t* req_err_headers_out(request_rec *r)
63bd57250ebc55355cac6560aa0efdc887b67140Bob Halley{
63bd57250ebc55355cac6560aa0efdc887b67140Bob Halley return r->err_headers_out;
63bd57250ebc55355cac6560aa0efdc887b67140Bob Halley}
63bd57250ebc55355cac6560aa0efdc887b67140Bob Halley
3df43dad430973e273bc55b304c6d5965390db06Michael Graffstatic apr_table_t* req_subprocess_env(request_rec *r)
63bd57250ebc55355cac6560aa0efdc887b67140Bob Halley{
63bd57250ebc55355cac6560aa0efdc887b67140Bob Halley return r->subprocess_env;
63bd57250ebc55355cac6560aa0efdc887b67140Bob Halley}
63bd57250ebc55355cac6560aa0efdc887b67140Bob Halley
63bd57250ebc55355cac6560aa0efdc887b67140Bob Halleystatic apr_table_t* req_notes(request_rec *r)
63bd57250ebc55355cac6560aa0efdc887b67140Bob Halley{
63bd57250ebc55355cac6560aa0efdc887b67140Bob Halley return r->notes;
63bd57250ebc55355cac6560aa0efdc887b67140Bob Halley}
63bd57250ebc55355cac6560aa0efdc887b67140Bob Halley
63bd57250ebc55355cac6560aa0efdc887b67140Bob Halley/* END dispatch mathods for request_rec fields */
63bd57250ebc55355cac6560aa0efdc887b67140Bob Halley
22f735acbce7ffe95af20bb58bb8929b6f1d674fMichael Graffstatic int req_dispatch(lua_State *L)
22f735acbce7ffe95af20bb58bb8929b6f1d674fMichael Graff{
22f735acbce7ffe95af20bb58bb8929b6f1d674fMichael Graff apr_hash_t *dispatch;
22f735acbce7ffe95af20bb58bb8929b6f1d674fMichael Graff req_fun_t *rft;
22f735acbce7ffe95af20bb58bb8929b6f1d674fMichael Graff request_rec *r = ap_lua_check_request_rec(L, 1);
3df43dad430973e273bc55b304c6d5965390db06Michael Graff const char *name = luaL_checkstring(L, 2);
22f735acbce7ffe95af20bb58bb8929b6f1d674fMichael Graff lua_pop(L, 2);
22f735acbce7ffe95af20bb58bb8929b6f1d674fMichael Graff
22f735acbce7ffe95af20bb58bb8929b6f1d674fMichael Graff lua_getfield(L, LUA_REGISTRYINDEX, "Apache2.Request.dispatch");
22f735acbce7ffe95af20bb58bb8929b6f1d674fMichael Graff dispatch = lua_touserdata(L, 1);
22f735acbce7ffe95af20bb58bb8929b6f1d674fMichael Graff lua_pop(L, 1);
22f735acbce7ffe95af20bb58bb8929b6f1d674fMichael Graff
22f735acbce7ffe95af20bb58bb8929b6f1d674fMichael Graff rft = apr_hash_get(dispatch, name, APR_HASH_KEY_STRING);
22f735acbce7ffe95af20bb58bb8929b6f1d674fMichael Graff if (rft) {
22f735acbce7ffe95af20bb58bb8929b6f1d674fMichael Graff switch (rft->type) {
63bd57250ebc55355cac6560aa0efdc887b67140Bob Halley case APL_REQ_FUNTYPE_TABLE:{
63bd57250ebc55355cac6560aa0efdc887b67140Bob Halley apr_table_t *rs;
63bd57250ebc55355cac6560aa0efdc887b67140Bob Halley req_field_apr_table_f func = (req_field_apr_table_f)rft->fun;
63bd57250ebc55355cac6560aa0efdc887b67140Bob Halley ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
63bd57250ebc55355cac6560aa0efdc887b67140Bob Halley "request_rec->dispatching %s -> apr table",
63bd57250ebc55355cac6560aa0efdc887b67140Bob Halley name);
63bd57250ebc55355cac6560aa0efdc887b67140Bob Halley rs = (*func)(r);
63bd57250ebc55355cac6560aa0efdc887b67140Bob Halley ap_lua_push_apr_table(L, rs);
63bd57250ebc55355cac6560aa0efdc887b67140Bob Halley return 1;
63bd57250ebc55355cac6560aa0efdc887b67140Bob Halley }
3df43dad430973e273bc55b304c6d5965390db06Michael Graff
63bd57250ebc55355cac6560aa0efdc887b67140Bob Halley case APL_REQ_FUNTYPE_LUACFUN:{
63bd57250ebc55355cac6560aa0efdc887b67140Bob Halley lua_CFunction func = (lua_CFunction)rft->fun;
63bd57250ebc55355cac6560aa0efdc887b67140Bob Halley ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
63bd57250ebc55355cac6560aa0efdc887b67140Bob Halley "request_rec->dispatching %s -> lua_CFunction",
63bd57250ebc55355cac6560aa0efdc887b67140Bob Halley name);
63bd57250ebc55355cac6560aa0efdc887b67140Bob Halley lua_pushcfunction(L, func);
63bd57250ebc55355cac6560aa0efdc887b67140Bob Halley return 1;
63bd57250ebc55355cac6560aa0efdc887b67140Bob Halley }
63bd57250ebc55355cac6560aa0efdc887b67140Bob Halley case APL_REQ_FUNTYPE_STRING:{
63bd57250ebc55355cac6560aa0efdc887b67140Bob Halley req_field_string_f func = (req_field_string_f)rft->fun;
63bd57250ebc55355cac6560aa0efdc887b67140Bob Halley char *rs;
63bd57250ebc55355cac6560aa0efdc887b67140Bob Halley ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
22f735acbce7ffe95af20bb58bb8929b6f1d674fMichael Graff "request_rec->dispatching %s -> string", name);
22f735acbce7ffe95af20bb58bb8929b6f1d674fMichael Graff rs = (*func) (r);
22f735acbce7ffe95af20bb58bb8929b6f1d674fMichael Graff lua_pushstring(L, rs);
22f735acbce7ffe95af20bb58bb8929b6f1d674fMichael Graff return 1;
22f735acbce7ffe95af20bb58bb8929b6f1d674fMichael Graff }
22f735acbce7ffe95af20bb58bb8929b6f1d674fMichael Graff case APL_REQ_FUNTYPE_INT:{
3df43dad430973e273bc55b304c6d5965390db06Michael Graff req_field_int_f func = (req_field_int_f)rft->fun;
22f735acbce7ffe95af20bb58bb8929b6f1d674fMichael Graff int rs;
22f735acbce7ffe95af20bb58bb8929b6f1d674fMichael Graff ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
22f735acbce7ffe95af20bb58bb8929b6f1d674fMichael Graff "request_rec->dispatching %s -> int", name);
22f735acbce7ffe95af20bb58bb8929b6f1d674fMichael Graff rs = (*func) (r);
22f735acbce7ffe95af20bb58bb8929b6f1d674fMichael Graff lua_pushnumber(L, rs);
c910282c40424a4c9a1f8533d186bed73099aedeDavid Lawrence return 1;
c910282c40424a4c9a1f8533d186bed73099aedeDavid Lawrence }
c910282c40424a4c9a1f8533d186bed73099aedeDavid Lawrence case APL_REQ_FUNTYPE_BOOLEAN:{
c910282c40424a4c9a1f8533d186bed73099aedeDavid Lawrence req_field_int_f func = (req_field_int_f)rft->fun;
22f735acbce7ffe95af20bb58bb8929b6f1d674fMichael Graff int rs;
441a57f959ae5e08b3008e067898d11d5ab71e0bMichael Graff ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
e5b0c46fbb30acbc7a12c2fdf8687c56246a7b3aAndreas Gustafsson "request_rec->dispatching %s -> boolean", name);
e5b0c46fbb30acbc7a12c2fdf8687c56246a7b3aAndreas Gustafsson rs = (*func) (r);
e5b0c46fbb30acbc7a12c2fdf8687c56246a7b3aAndreas Gustafsson lua_pushboolean(L, rs);
e5b0c46fbb30acbc7a12c2fdf8687c56246a7b3aAndreas Gustafsson return 1;
e5b0c46fbb30acbc7a12c2fdf8687c56246a7b3aAndreas Gustafsson }
e5b0c46fbb30acbc7a12c2fdf8687c56246a7b3aAndreas Gustafsson }
e5b0c46fbb30acbc7a12c2fdf8687c56246a7b3aAndreas Gustafsson }
e5b0c46fbb30acbc7a12c2fdf8687c56246a7b3aAndreas Gustafsson
74da83876c49f1c05d6f84c5257d88573a4e637bAndreas Gustafsson ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, "nothing for %s", name);
e5b0c46fbb30acbc7a12c2fdf8687c56246a7b3aAndreas Gustafsson return 0;
e5b0c46fbb30acbc7a12c2fdf8687c56246a7b3aAndreas Gustafsson}
e5b0c46fbb30acbc7a12c2fdf8687c56246a7b3aAndreas Gustafsson
e5b0c46fbb30acbc7a12c2fdf8687c56246a7b3aAndreas Gustafsson/* helper function for the logging functions below */
8a21ac0f095f6379d00805a1efa156bbd6790ab3Mark Andrewsstatic int req_log_at(lua_State *L, int level)
8a21ac0f095f6379d00805a1efa156bbd6790ab3Mark Andrews{
8a21ac0f095f6379d00805a1efa156bbd6790ab3Mark Andrews const char *msg;
f8d63d12dd6fe15de6a1339722c61b030d1e5badBob Halley request_rec *r = ap_lua_check_request_rec(L, 1);
8a21ac0f095f6379d00805a1efa156bbd6790ab3Mark Andrews lua_Debug dbg;
8a21ac0f095f6379d00805a1efa156bbd6790ab3Mark Andrews
8a21ac0f095f6379d00805a1efa156bbd6790ab3Mark Andrews lua_getstack(L, 1, &dbg);
8a21ac0f095f6379d00805a1efa156bbd6790ab3Mark Andrews lua_getinfo(L, "Sl", &dbg);
8a21ac0f095f6379d00805a1efa156bbd6790ab3Mark Andrews
8a21ac0f095f6379d00805a1efa156bbd6790ab3Mark Andrews msg = luaL_checkstring(L, 2);
8a21ac0f095f6379d00805a1efa156bbd6790ab3Mark Andrews ap_log_rerror(dbg.source, dbg.currentline, APLOG_MODULE_INDEX, level, 0,
8a21ac0f095f6379d00805a1efa156bbd6790ab3Mark Andrews r, "%s", msg);
f8d63d12dd6fe15de6a1339722c61b030d1e5badBob Halley return 0;
f8d63d12dd6fe15de6a1339722c61b030d1e5badBob Halley}
8a21ac0f095f6379d00805a1efa156bbd6790ab3Mark Andrews
f8d63d12dd6fe15de6a1339722c61b030d1e5badBob Halley/* r:debug(String) and friends which use apache logging */
8a21ac0f095f6379d00805a1efa156bbd6790ab3Mark Andrewsstatic int req_emerg(lua_State *L)
8a21ac0f095f6379d00805a1efa156bbd6790ab3Mark Andrews{
8a21ac0f095f6379d00805a1efa156bbd6790ab3Mark Andrews req_log_at(L, APLOG_EMERG);
cae6ddd340bc40475544d020565199b7a357411aBob Halley return 0;
cae6ddd340bc40475544d020565199b7a357411aBob Halley}
cae6ddd340bc40475544d020565199b7a357411aBob Halleystatic int req_alert(lua_State *L)
cae6ddd340bc40475544d020565199b7a357411aBob Halley{
cae6ddd340bc40475544d020565199b7a357411aBob Halley req_log_at(L, APLOG_ALERT);
cae6ddd340bc40475544d020565199b7a357411aBob Halley return 0;
cae6ddd340bc40475544d020565199b7a357411aBob Halley}
cae6ddd340bc40475544d020565199b7a357411aBob Halleystatic int req_crit(lua_State *L)
cae6ddd340bc40475544d020565199b7a357411aBob Halley{
cae6ddd340bc40475544d020565199b7a357411aBob Halley req_log_at(L, APLOG_CRIT);
cae6ddd340bc40475544d020565199b7a357411aBob Halley return 0;
cae6ddd340bc40475544d020565199b7a357411aBob Halley}
cae6ddd340bc40475544d020565199b7a357411aBob Halleystatic int req_err(lua_State *L)
cae6ddd340bc40475544d020565199b7a357411aBob Halley{
cae6ddd340bc40475544d020565199b7a357411aBob Halley req_log_at(L, APLOG_ERR);
cae6ddd340bc40475544d020565199b7a357411aBob Halley return 0;
cae6ddd340bc40475544d020565199b7a357411aBob Halley}
cae6ddd340bc40475544d020565199b7a357411aBob Halleystatic int req_warn(lua_State *L)
441a57f959ae5e08b3008e067898d11d5ab71e0bMichael Graff{
4556681e191b7c1654639895ce719d98f2822ee2Michael Graff req_log_at(L, APLOG_WARNING);
4556681e191b7c1654639895ce719d98f2822ee2Michael Graff return 0;
441a57f959ae5e08b3008e067898d11d5ab71e0bMichael Graff}
4556681e191b7c1654639895ce719d98f2822ee2Michael Graffstatic int req_notice(lua_State *L)
441a57f959ae5e08b3008e067898d11d5ab71e0bMichael Graff{
441a57f959ae5e08b3008e067898d11d5ab71e0bMichael Graff req_log_at(L, APLOG_NOTICE);
441a57f959ae5e08b3008e067898d11d5ab71e0bMichael Graff return 0;
441a57f959ae5e08b3008e067898d11d5ab71e0bMichael Graff}
4556681e191b7c1654639895ce719d98f2822ee2Michael Graffstatic int req_info(lua_State *L)
441a57f959ae5e08b3008e067898d11d5ab71e0bMichael Graff{
441a57f959ae5e08b3008e067898d11d5ab71e0bMichael Graff req_log_at(L, APLOG_INFO);
441a57f959ae5e08b3008e067898d11d5ab71e0bMichael Graff return 0;
4556681e191b7c1654639895ce719d98f2822ee2Michael Graff}
441a57f959ae5e08b3008e067898d11d5ab71e0bMichael Graffstatic int req_debug(lua_State *L)
4556681e191b7c1654639895ce719d98f2822ee2Michael Graff{
441a57f959ae5e08b3008e067898d11d5ab71e0bMichael Graff req_log_at(L, APLOG_DEBUG);
441a57f959ae5e08b3008e067898d11d5ab71e0bMichael Graff return 0;
441a57f959ae5e08b3008e067898d11d5ab71e0bMichael Graff}
441a57f959ae5e08b3008e067898d11d5ab71e0bMichael Graff
441a57f959ae5e08b3008e067898d11d5ab71e0bMichael Graff/* handle r.status = 201 */
441a57f959ae5e08b3008e067898d11d5ab71e0bMichael Graffstatic int req_newindex(lua_State *L)
84d79ecd505cc880d01a73a2bf47541eaa53fcbcMichael Graff{
4556681e191b7c1654639895ce719d98f2822ee2Michael Graff const char *key;
441a57f959ae5e08b3008e067898d11d5ab71e0bMichael Graff /* request_rec* r = lua_touserdata(L, lua_upvalueindex(1)); */
441a57f959ae5e08b3008e067898d11d5ab71e0bMichael Graff /* const char* key = luaL_checkstring(L, -2); */
4556681e191b7c1654639895ce719d98f2822ee2Michael Graff request_rec *r = ap_lua_check_request_rec(L, 1);
4556681e191b7c1654639895ce719d98f2822ee2Michael Graff key = luaL_checkstring(L, 2);
441a57f959ae5e08b3008e067898d11d5ab71e0bMichael Graff
441a57f959ae5e08b3008e067898d11d5ab71e0bMichael Graff if (0 == strcmp("args", key)) {
3df43dad430973e273bc55b304c6d5965390db06Michael Graff const char *value = luaL_checkstring(L, 3);
4556681e191b7c1654639895ce719d98f2822ee2Michael Graff r->args = apr_pstrdup(r->pool, value);
441a57f959ae5e08b3008e067898d11d5ab71e0bMichael Graff return 0;
441a57f959ae5e08b3008e067898d11d5ab71e0bMichael Graff }
441a57f959ae5e08b3008e067898d11d5ab71e0bMichael Graff
441a57f959ae5e08b3008e067898d11d5ab71e0bMichael Graff if (0 == strcmp("content_type", key)) {
4556681e191b7c1654639895ce719d98f2822ee2Michael Graff const char *value = luaL_checkstring(L, 3);
4556681e191b7c1654639895ce719d98f2822ee2Michael Graff ap_set_content_type(r, apr_pstrdup(r->pool, value));
4556681e191b7c1654639895ce719d98f2822ee2Michael Graff return 0;
4556681e191b7c1654639895ce719d98f2822ee2Michael Graff }
441a57f959ae5e08b3008e067898d11d5ab71e0bMichael Graff
441a57f959ae5e08b3008e067898d11d5ab71e0bMichael Graff if (0 == strcmp("filename", key)) {
441a57f959ae5e08b3008e067898d11d5ab71e0bMichael Graff const char *value = luaL_checkstring(L, 3);
r->filename = apr_pstrdup(r->pool, value);
return 0;
}
if (0 == strcmp("status", key)) {
int code = luaL_checkinteger(L, 3);
r->status = code;
return 0;
}
if (0 == strcmp("uri", key)) {
const char *value = luaL_checkstring(L, 3);
r->uri = apr_pstrdup(r->pool, value);
return 0;
}
if (0 == strcmp("user", key)) {
const char *value = luaL_checkstring(L, 3);
r->user = apr_pstrdup(r->pool, value);
return 0;
}
lua_pushstring(L,
apr_psprintf(r->pool,
"Property [%s] may not be set on a request_rec",
key));
lua_error(L);
return 0;
}
static const struct luaL_Reg request_methods[] = {
{"__index", req_dispatch},
{"__newindex", req_newindex},
/* {"__newindex", req_set_field}, */
{NULL, NULL}
};
static const struct luaL_Reg connection_methods[] = {
{NULL, NULL}
};
static const struct luaL_Reg server_methods[] = {
{NULL, NULL}
};
static req_fun_t *makefun(const void *fun, int type, apr_pool_t *pool)
{
req_fun_t *rft = apr_palloc(pool, sizeof(req_fun_t));
rft->fun = fun;
rft->type = type;
return rft;
}
AP_LUA_DECLARE(void) ap_lua_load_request_lmodule(lua_State *L, apr_pool_t *p)
{
apr_hash_t *dispatch = apr_hash_make(p);
apr_hash_set(dispatch, "puts", APR_HASH_KEY_STRING,
makefun(&req_puts, APL_REQ_FUNTYPE_LUACFUN, p));
apr_hash_set(dispatch, "write", APR_HASH_KEY_STRING,
makefun(&req_write, APL_REQ_FUNTYPE_LUACFUN, p));
apr_hash_set(dispatch, "document_root", APR_HASH_KEY_STRING,
makefun(&req_document_root, APL_REQ_FUNTYPE_STRING, p));
apr_hash_set(dispatch, "parseargs", APR_HASH_KEY_STRING,
makefun(&req_parseargs, APL_REQ_FUNTYPE_LUACFUN, p));
apr_hash_set(dispatch, "debug", APR_HASH_KEY_STRING,
makefun(&req_debug, APL_REQ_FUNTYPE_LUACFUN, p));
apr_hash_set(dispatch, "info", APR_HASH_KEY_STRING,
makefun(&req_info, APL_REQ_FUNTYPE_LUACFUN, p));
apr_hash_set(dispatch, "notice", APR_HASH_KEY_STRING,
makefun(&req_notice, APL_REQ_FUNTYPE_LUACFUN, p));
apr_hash_set(dispatch, "warn", APR_HASH_KEY_STRING,
makefun(&req_warn, APL_REQ_FUNTYPE_LUACFUN, p));
apr_hash_set(dispatch, "err", APR_HASH_KEY_STRING,
makefun(&req_err, APL_REQ_FUNTYPE_LUACFUN, p));
apr_hash_set(dispatch, "crit", APR_HASH_KEY_STRING,
makefun(&req_crit, APL_REQ_FUNTYPE_LUACFUN, p));
apr_hash_set(dispatch, "alert", APR_HASH_KEY_STRING,
makefun(&req_alert, APL_REQ_FUNTYPE_LUACFUN, p));
apr_hash_set(dispatch, "emerg", APR_HASH_KEY_STRING,
makefun(&req_emerg, APL_REQ_FUNTYPE_LUACFUN, p));
apr_hash_set(dispatch, "add_output_filter", APR_HASH_KEY_STRING,
makefun(&req_add_output_filter, APL_REQ_FUNTYPE_LUACFUN, p));
apr_hash_set(dispatch, "construct_url", APR_HASH_KEY_STRING,
makefun(&req_construct_url, APL_REQ_FUNTYPE_LUACFUN, p));
apr_hash_set(dispatch, "assbackwards", APR_HASH_KEY_STRING,
makefun(&req_assbackwards_field, APL_REQ_FUNTYPE_BOOLEAN, p));
apr_hash_set(dispatch, "status", APR_HASH_KEY_STRING,
makefun(&req_status_field, APL_REQ_FUNTYPE_INT, p));
apr_hash_set(dispatch, "protocol", APR_HASH_KEY_STRING,
makefun(&req_protocol_field, APL_REQ_FUNTYPE_STRING, p));
apr_hash_set(dispatch, "range", APR_HASH_KEY_STRING,
makefun(&req_range_field, APL_REQ_FUNTYPE_STRING, p));
apr_hash_set(dispatch, "content_type", APR_HASH_KEY_STRING,
makefun(&req_content_type_field, APL_REQ_FUNTYPE_STRING, p));
apr_hash_set(dispatch, "content_encoding", APR_HASH_KEY_STRING,
makefun(&req_content_encoding_field, APL_REQ_FUNTYPE_STRING,
p));
apr_hash_set(dispatch, "ap_auth_type", APR_HASH_KEY_STRING,
makefun(&req_ap_auth_type_field, APL_REQ_FUNTYPE_STRING, p));
apr_hash_set(dispatch, "unparsed_uri", APR_HASH_KEY_STRING,
makefun(&req_unparsed_uri_field, APL_REQ_FUNTYPE_STRING, p));
apr_hash_set(dispatch, "user", APR_HASH_KEY_STRING,
makefun(&req_user_field, APL_REQ_FUNTYPE_STRING, p));
apr_hash_set(dispatch, "filename", APR_HASH_KEY_STRING,
makefun(&req_filename_field, APL_REQ_FUNTYPE_STRING, p));
apr_hash_set(dispatch, "canonical_filename", APR_HASH_KEY_STRING,
makefun(&req_canonical_filename_field,
APL_REQ_FUNTYPE_STRING, p));
apr_hash_set(dispatch, "path_info", APR_HASH_KEY_STRING,
makefun(&req_path_info_field, APL_REQ_FUNTYPE_STRING, p));
apr_hash_set(dispatch, "args", APR_HASH_KEY_STRING,
makefun(&req_args_field, APL_REQ_FUNTYPE_STRING, p));
apr_hash_set(dispatch, "hostname", APR_HASH_KEY_STRING,
makefun(&req_hostname_field, APL_REQ_FUNTYPE_STRING, p));
apr_hash_set(dispatch, "uri", APR_HASH_KEY_STRING,
makefun(&req_uri_field, APL_REQ_FUNTYPE_STRING, p));
apr_hash_set(dispatch, "the_request", APR_HASH_KEY_STRING,
makefun(&req_the_request_field, APL_REQ_FUNTYPE_STRING, p));
apr_hash_set(dispatch, "method", APR_HASH_KEY_STRING,
makefun(&req_method_field, APL_REQ_FUNTYPE_STRING, p));
apr_hash_set(dispatch, "headers_in", APR_HASH_KEY_STRING,
makefun(&req_headers_in, APL_REQ_FUNTYPE_TABLE, p));
apr_hash_set(dispatch, "headers_out", APR_HASH_KEY_STRING,
makefun(&req_headers_out, APL_REQ_FUNTYPE_TABLE, p));
apr_hash_set(dispatch, "err_headers_out", APR_HASH_KEY_STRING,
makefun(&req_err_headers_out, APL_REQ_FUNTYPE_TABLE, p));
apr_hash_set(dispatch, "notes", APR_HASH_KEY_STRING,
makefun(&req_notes, APL_REQ_FUNTYPE_TABLE, p));
apr_hash_set(dispatch, "subprocess_env", APR_HASH_KEY_STRING,
makefun(&req_subprocess_env, APL_REQ_FUNTYPE_TABLE, p));
lua_pushlightuserdata(L, dispatch);
lua_setfield(L, LUA_REGISTRYINDEX, "Apache2.Request.dispatch");
luaL_newmetatable(L, "Apache2.Request"); /* [metatable] */
lua_pushvalue(L, -1);
lua_setfield(L, -2, "__index");
luaL_register(L, NULL, request_methods); /* [metatable] */
lua_pop(L, 2);
luaL_newmetatable(L, "Apache2.Connection"); /* [metatable] */
lua_pushvalue(L, -1);
lua_setfield(L, -2, "__index");
luaL_register(L, NULL, connection_methods); /* [metatable] */
lua_pop(L, 2);
luaL_newmetatable(L, "Apache2.Server"); /* [metatable] */
lua_pushvalue(L, -1);
lua_setfield(L, -2, "__index");
luaL_register(L, NULL, server_methods); /* [metatable] */
lua_pop(L, 2);
}
AP_LUA_DECLARE(void) ap_lua_push_connection(lua_State *L, conn_rec *c)
{
lua_boxpointer(L, c);
luaL_getmetatable(L, "Apache2.Connection");
lua_setmetatable(L, -2);
luaL_getmetatable(L, "Apache2.Connection");
ap_lua_push_apr_table(L, c->notes);
lua_setfield(L, -2, "notes");
lua_pushstring(L, c->remote_ip);
lua_setfield(L, -2, "remote_ip");
lua_pop(L, 1);
}
AP_LUA_DECLARE(void) ap_lua_push_server(lua_State *L, server_rec *s)
{
lua_boxpointer(L, s);
luaL_getmetatable(L, "Apache2.Server");
lua_setmetatable(L, -2);
luaL_getmetatable(L, "Apache2.Server");
lua_pushstring(L, s->server_hostname);
lua_setfield(L, -2, "server_hostname");
lua_pop(L, 1);
}
AP_LUA_DECLARE(void) ap_lua_push_request(lua_State *L, request_rec *r)
{
lua_boxpointer(L, r);
luaL_getmetatable(L, "Apache2.Request");
lua_setmetatable(L, -2);
}