request.c revision fa21815c2fce84c8a6f56bccd3ad4c0537c6f1fd
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz/**
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz * Licensed to the Apache Software Foundation (ASF) under one or more
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz * contributor license agreements. See the NOTICE file distributed with
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz * this work for additional information regarding copyright ownership.
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz * The ASF licenses this file to You under the Apache License, Version 2.0
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz * (the "License"); you may not use this file except in compliance with
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz * the License. You may obtain a copy of the License at
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz *
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz * http://www.apache.org/licenses/LICENSE-2.0
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz *
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz * Unless required by applicable law or agreed to in writing, software
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz * distributed under the License is distributed on an "AS IS" BASIS,
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz * See the License for the specific language governing permissions and
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz * limitations under the License.
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz */
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz#include "mod_lua.h"
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz#include "util_script.h"
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz#include "apr_lua.h"
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantztypedef char* (*req_field_string_f) (request_rec* r);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantztypedef int (*req_field_int_f) (request_rec* r);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantzvoid rstack_dump(lua_State* L, request_rec* r, const char* msg) {
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, "Lua Stack Dump: [%s]", msg);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz int i;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz int top = lua_gettop(L);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz for (i = 1; i<= top; i++) {
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz int t = lua_type(L, i);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz switch(t) {
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz case LUA_TSTRING: {
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r,
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz "%d: '%s'", i, lua_tostring(L, i));
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz break;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz }
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz case LUA_TUSERDATA: {
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, "%d: userdata", i);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz break;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz }
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz case LUA_TLIGHTUSERDATA: {
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, "%d: lightuserdata", i);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz break;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz }
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz case LUA_TNIL: {
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r,
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz "%d: NIL", i);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz break;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz }
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz case LUA_TNONE: {
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r,
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz "%d: None", i);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz break;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz }
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz case LUA_TBOOLEAN: {
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r,
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz "%d: %s", i, lua_toboolean(L, i) ? "true" : "false");
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz break;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz }
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz case LUA_TNUMBER: {
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r,
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz "%d: %g", i, lua_tonumber(L, i));
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz break;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz }
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz case LUA_TTABLE: {
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r,
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz "%d: <table>", i);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz break;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz }
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz case LUA_TFUNCTION: {
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r,
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz "%d: <function>", i);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz break;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz }
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz default: {
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r,
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz "%d: unkown: -[%s]-", i, lua_typename(L, i));
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz break;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz }
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz }
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz }
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz}
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz/**
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz * Verify that the thing at index is a request_rec wrapping
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz * userdata thingamajig and return it if it is. if it is not
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz * lua will enter its error handling routine.
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz */
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantzstatic request_rec* apl_check_request_rec(lua_State* L, int index) {
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz luaL_checkudata(L, index, "Apache2.Request");
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz request_rec* r = (request_rec*)lua_unboxpointer(L, index);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz return r;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz}
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz/* ------------------ request methods -------------------- */
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz/* helper callback for req_parseargs */
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantzstatic int req_aprtable2luatable_cb(void *l, const char *key, const char *value) {
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz lua_State* L = (lua_State*)l; /* [table<s,t>, table<s,s>] */
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz /* rstack_dump(L, RRR, "start of cb"); */
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz /* L is [table<s,t>, table<s,s>] */
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz /* build complex */
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz lua_getfield(L, -1, key); /* [VALUE, table<s,t>, table<s,s>] */
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz /* rstack_dump(L, RRR, "after getfield"); */
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz int t = lua_type(L, -1);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz switch(t) {
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz case LUA_TNIL:
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz case LUA_TNONE: {
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz lua_pop(L, 1); /* [table<s,t>, table<s,s>] */
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz lua_newtable(L); /* [array, table<s,t>, table<s,s>] */
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz lua_pushnumber(L, 1); /* [1, array, table<s,t>, table<s,s>] */
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz lua_pushstring(L, value); /* [string, 1, array, table<s,t>, table<s,s>] */
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz lua_settable(L, -3); /* [array, table<s,t>, table<s,s>] */
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz lua_setfield(L, -2, key); /* [table<s,t>, table<s,s>] */
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz break;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz }
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz case LUA_TTABLE: {
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz /* [array, table<s,t>, table<s,s>] */
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz int size = lua_objlen(L, -1);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz lua_pushnumber(L, size + 1); /* [#, array, table<s,t>, table<s,s>] */
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz lua_pushstring(L, value); /* [string, #, array, table<s,t>, table<s,s>] */
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz lua_settable(L, -3); /* [array, table<s,t>, table<s,s>] */
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz lua_setfield(L, -2, key); /* [table<s,t>, table<s,s>] */
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz break;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz }
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz }
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz /* L is [table<s,t>, table<s,s>] */
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz /* build simple */
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz lua_getfield(L, -2, key); /* [VALUE, table<s,s>, table<s,t>] */
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz if (lua_isnoneornil(L, -1)) { /* only set if not already set */
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz lua_pop(L, 1); /* [table<s,s>, table<s,t>]] */
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz lua_pushstring(L, value); /* [string, table<s,s>, table<s,t>] */
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz lua_setfield(L, -3, key); /* [table<s,s>, table<s,t>] */
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz } else { lua_pop(L, 1); }
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz return 1;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz}
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz/* r:parseargs() returning a lua table */
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantzstatic int req_parseargs(lua_State* L) {
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz request_rec* r = apl_check_request_rec(L, 1);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz lua_newtable(L);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz lua_newtable(L); /* [table, table] */
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz apr_table_t* form_table;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz ap_args_to_table(r, &form_table);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz apr_table_do(req_aprtable2luatable_cb, L, form_table, NULL);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz return 2; /* [table<string, string>, table<string, array<string>>] */
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz}
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz/* wrap ap_rputs as r:puts(String) */
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantzstatic int req_puts(lua_State* L) {
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz request_rec* r = apl_check_request_rec(L, 1);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz int argc = lua_gettop(L);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz int i;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz for (i=2;i<=argc;i++) {
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz ap_rputs(luaL_checkstring(L, i), r);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz }
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz return 0;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz}
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz/* wrap ap_rwrite as r:write(String) */
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantzstatic int req_write(lua_State* L) {
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz request_rec* r = apl_check_request_rec(L, 1);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz size_t n;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz const char* buf = luaL_checklstring(L, 2, &n);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz ap_rwrite((void *)buf, n, r);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz return 0;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz}
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz/* r:parsebody() */
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantzstatic int req_parsebody(lua_State* L) {
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz request_rec* r = apl_check_request_rec(L, 1);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz lua_newtable(L);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz lua_newtable(L);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz apr_table_t* form_table;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz if (ap_body_to_table(r, &form_table) == APR_SUCCESS) {
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz apr_table_do(req_aprtable2luatable_cb, L, form_table, NULL);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz }
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz return 2;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz}
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz/* r:addoutputfilter(name|function) */
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantzstatic int req_add_output_filter(lua_State *L) {
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz request_rec* r = apl_check_request_rec(L, 1);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz const char *name = luaL_checkstring(L, 2);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, "adding output filter %s", name);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz ap_add_output_filter(name, L, r, r->connection);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz return 0;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz}
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantzstatic int req_document_root(lua_State* L) {
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz request_rec* r = apl_check_request_rec(L, 1);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz char* doc_root = apr_pstrdup(r->pool, ap_document_root(r));
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz lua_pushstring(L, doc_root);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz return 1;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz}
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz/* BEGIN dispatch mathods for request_rec fields */
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantzstatic char* req_uri_field(request_rec* r) {
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz return r->uri;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz}
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantzstatic const char* req_method_field(request_rec* r) {
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz return r->method;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz}
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantzstatic const char* req_hostname_field(request_rec* r) {
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz return r->hostname;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz}
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantzstatic const char* req_args_field(request_rec* r) {
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz return r->args;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz}
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantzstatic const char* req_path_info_field(request_rec* r) {
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz return r->path_info;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz}
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantzstatic const char* req_canonical_filename_field(request_rec* r) {
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz return r->canonical_filename;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz}
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantzstatic const char* req_filename_field(request_rec* r) {
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz return r->filename;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz}
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantzstatic const char* req_user_field(request_rec* r) {
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz return r->user;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz}
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantzstatic const char* req_unparsed_uri_field(request_rec* r) {
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz return r->unparsed_uri;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz}
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantzstatic const char* req_ap_auth_type_field(request_rec* r) {
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz return r->ap_auth_type;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz}
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantzstatic const char* req_content_encoding_field(request_rec* r) {
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz return r->content_encoding;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz}
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantzstatic const char* req_content_type_field(request_rec* r) {
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz return r->content_type;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz}
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantzstatic const char* req_range_field(request_rec* r) {
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz return r->range;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz}
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantzstatic const char* req_protocol_field(request_rec* r) {
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz return r->protocol;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz}
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantzstatic const char* req_the_request_field(request_rec* r) {
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz return r->the_request;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz}
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantzstatic int req_status_field(request_rec* r) {
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz return r->status;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz}
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantzstatic int req_assbackwards_field(request_rec* r) {
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz return r->assbackwards;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz}
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz/* END dispatch mathods for request_rec fields */
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantzstatic int req_dispatch(lua_State* L) {
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz request_rec* r = apl_check_request_rec(L, 1);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz const char *name = luaL_checkstring(L, 2);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz lua_pop(L, 2);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz lua_getfield(L, LUA_REGISTRYINDEX, "Apache2.Request.dispatch");
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz apr_hash_t* dispatch = lua_touserdata(L, 1);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz lua_pop(L, 1);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz req_fun_t* rft = apr_hash_get(dispatch, name, APR_HASH_KEY_STRING);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz if (rft) {
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz switch(rft->type) {
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz case APL_REQ_FUNTYPE_TABLE: {
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz "request_rec->dispatching %s -> apr table (NOT IMPLEMENTED YET)", name);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz return 0;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz }
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz case APL_REQ_FUNTYPE_LUACFUN: {
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz "request_rec->dispatching %s -> lua_CFunction", name);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz lua_CFunction func = rft->fun;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz lua_pushcfunction(L, func);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz return 1;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz }
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz case APL_REQ_FUNTYPE_STRING: {
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz "request_rec->dispatching %s -> string", name);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz req_field_string_f func = rft->fun;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz char* rs = (*func)(r);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz lua_pushstring(L, rs);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz return 1;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz }
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz case APL_REQ_FUNTYPE_INT: {
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz "request_rec->dispatching %s -> int", name);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz req_field_int_f func = rft->fun;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz int rs = (*func)(r);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz lua_pushnumber(L, rs);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz return 1;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz }
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz case APL_REQ_FUNTYPE_BOOLEAN: {
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz "request_rec->dispatching %s -> boolean", name);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz req_field_int_f func = rft->fun;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz int rs = (*func)(r);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz lua_pushboolean(L, rs);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz return 1;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz }
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz }
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz }
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz "nothing for %s", name);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz return 0;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz}
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz/* helper function for the logging functions below */
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantzstatic int req_log_at(lua_State* L, int level) {
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz request_rec* r = apl_check_request_rec(L, 1);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz lua_Debug dbg;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz lua_getstack(L, 1, &dbg);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz lua_getinfo(L, "Sl", &dbg);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz const char* msg = luaL_checkstring(L, 2);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz ap_log_rerror(dbg.source, dbg.currentline, level, 0, r, msg);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz return 0;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz}
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz/* r:debug(String) and friends which use apache logging */
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantzstatic int req_emerg(lua_State* L) { req_log_at(L, APLOG_EMERG); return 0; }
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantzstatic int req_alert(lua_State* L) { req_log_at(L, APLOG_ALERT); return 0; }
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantzstatic int req_crit(lua_State* L) { req_log_at(L, APLOG_CRIT); return 0; }
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantzstatic int req_err(lua_State* L) { req_log_at(L, APLOG_ERR); return 0; }
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantzstatic int req_warn(lua_State* L) { req_log_at(L, APLOG_WARNING); return 0; }
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantzstatic int req_notice(lua_State* L) { req_log_at(L, APLOG_NOTICE); return 0; }
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantzstatic int req_info(lua_State* L) { req_log_at(L, APLOG_INFO); return 0; }
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantzstatic int req_debug(lua_State* L) { req_log_at(L, APLOG_DEBUG); return 0; }
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz/* handle r.status = 201 */
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantzstatic int req_newindex(lua_State* L) {
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz /* request_rec* r = lua_touserdata(L, lua_upvalueindex(1)); */
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz /* const char* key = luaL_checkstring(L, -2); */
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz request_rec* r = apl_check_request_rec(L, 1);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz rstack_dump(L, r, "req_newindex");
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz const char *key = luaL_checkstring(L, 2);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz rstack_dump(L, r, "req_newindex");
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz if (0 == apr_strnatcmp("status", key)) {
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz int code = luaL_checkinteger(L, 3);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz r->status = code;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz luaL_getmetatable(L, "Apache2.Request");
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz lua_pushinteger(L, code);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz lua_setfield(L, -2, "status");
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz lua_pop(L, 1);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz return 0;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz }
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz if (0 == apr_strnatcmp("content_type", key)) {
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz const char* value = luaL_checkstring(L, 3);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz r->content_type = apr_pstrdup(r->pool, value);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz luaL_getmetatable(L, "Apache2.Request");
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz lua_pushstring(L, value);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz lua_setfield(L, -2, "content_type");
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz lua_pop(L, 1);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz return 0;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz }
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz if (0 == apr_strnatcmp("filename", key)) {
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz const char* value = luaL_checkstring(L, 3);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz r->filename = apr_pstrdup(r->pool, value);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz luaL_getmetatable(L, "Apache2.Request");
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz lua_pushstring(L, value);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz lua_setfield(L, -2, "filename");
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz lua_pop(L, 1);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz return 0;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz }
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz if (0 == apr_strnatcmp("uri", key)) {
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz const char* value = luaL_checkstring(L, 3);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz r->uri = apr_pstrdup(r->pool, value);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz luaL_getmetatable(L, "Apache2.Request");
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz lua_pushstring(L, value);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz lua_setfield(L, -2, "uri");
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz lua_pop(L, 1);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz return 0;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz }
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz lua_pushstring(L, apr_psprintf(r->pool, "Property [%s] may not be set on a request_rec", key));
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz lua_error(L);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz return 0;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz}
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantzstatic const struct luaL_Reg request_methods[] = {
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz {"__index", req_dispatch},
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz {"__newindex", req_newindex},
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz /* {"__newindex", req_set_field}, */
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz {NULL, NULL}
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz};
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantzstatic const struct luaL_Reg connection_methods[] = {
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz {NULL, NULL}
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz};
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantzstatic const struct luaL_Reg server_methods[] = {
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz {NULL, NULL}
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz};
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantzstatic req_fun_t* makefun(void* fun, int type, apr_pool_t* pool) {
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz req_fun_t* rft = apr_palloc(pool, sizeof(req_fun_t));
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz rft->fun = fun;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz rft->type = type;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz return rft;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz}
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantzvoid apl_load_request_lmodule(lua_State *L, apr_pool_t *p) {
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz apr_hash_t* dispatch = apr_hash_make(p);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz apr_hash_set(dispatch, "puts", APR_HASH_KEY_STRING,
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz makefun(&req_puts, APL_REQ_FUNTYPE_LUACFUN, p));
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz apr_hash_set(dispatch, "write", APR_HASH_KEY_STRING,
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz makefun(&req_write, APL_REQ_FUNTYPE_LUACFUN, p));
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz apr_hash_set(dispatch, "document_root", APR_HASH_KEY_STRING,
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz makefun(&req_document_root, APL_REQ_FUNTYPE_LUACFUN, p));
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz apr_hash_set(dispatch, "parseargs", APR_HASH_KEY_STRING,
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz makefun(&req_parseargs, APL_REQ_FUNTYPE_LUACFUN, p));
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz apr_hash_set(dispatch, "parsebody", APR_HASH_KEY_STRING,
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz makefun(&req_parsebody, APL_REQ_FUNTYPE_LUACFUN, p));
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz apr_hash_set(dispatch, "debug", APR_HASH_KEY_STRING,
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz makefun(&req_debug, APL_REQ_FUNTYPE_LUACFUN, p));
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz apr_hash_set(dispatch, "info", APR_HASH_KEY_STRING,
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz makefun(&req_info, APL_REQ_FUNTYPE_LUACFUN, p));
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz apr_hash_set(dispatch, "notice", APR_HASH_KEY_STRING,
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz makefun(&req_notice, APL_REQ_FUNTYPE_LUACFUN, p));
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz apr_hash_set(dispatch, "warn", APR_HASH_KEY_STRING,
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz makefun(req_warn, APL_REQ_FUNTYPE_LUACFUN, p));
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz apr_hash_set(dispatch, "err", APR_HASH_KEY_STRING,
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz makefun(&req_err, APL_REQ_FUNTYPE_LUACFUN, p));
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz apr_hash_set(dispatch, "crit", APR_HASH_KEY_STRING,
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz makefun(&req_crit, APL_REQ_FUNTYPE_LUACFUN, p));
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz apr_hash_set(dispatch, "alert", APR_HASH_KEY_STRING,
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz makefun(&req_alert, APL_REQ_FUNTYPE_LUACFUN, p));
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz apr_hash_set(dispatch, "emerg", APR_HASH_KEY_STRING,
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz makefun(&req_emerg, APL_REQ_FUNTYPE_LUACFUN, p));
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz apr_hash_set(dispatch, "add_output_filter", APR_HASH_KEY_STRING,
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz makefun(&req_add_output_filter, APL_REQ_FUNTYPE_LUACFUN, p));
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz apr_hash_set(dispatch, "assbackwards", APR_HASH_KEY_STRING,
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz makefun(&req_assbackwards_field, APL_REQ_FUNTYPE_BOOLEAN, p));
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz apr_hash_set(dispatch, "status", APR_HASH_KEY_STRING,
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz makefun(&req_status_field, APL_REQ_FUNTYPE_INT, p));
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz apr_hash_set(dispatch, "protocol", APR_HASH_KEY_STRING,
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz makefun(&req_protocol_field, APL_REQ_FUNTYPE_STRING, p));
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz apr_hash_set(dispatch, "range", APR_HASH_KEY_STRING,
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz makefun(&req_range_field, APL_REQ_FUNTYPE_STRING, p));
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz apr_hash_set(dispatch, "content_type", APR_HASH_KEY_STRING,
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz makefun(&req_content_type_field, APL_REQ_FUNTYPE_STRING, p));
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz apr_hash_set(dispatch, "content_encoding", APR_HASH_KEY_STRING,
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz makefun(&req_content_encoding_field, APL_REQ_FUNTYPE_STRING, p));
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz apr_hash_set(dispatch, "ap_auth_type", APR_HASH_KEY_STRING,
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz makefun(&req_ap_auth_type_field, APL_REQ_FUNTYPE_STRING, p));
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz apr_hash_set(dispatch, "unparsed_uri", APR_HASH_KEY_STRING,
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz makefun(&req_unparsed_uri_field, APL_REQ_FUNTYPE_STRING, p));
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz apr_hash_set(dispatch, "user", APR_HASH_KEY_STRING,
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz makefun(&req_user_field, APL_REQ_FUNTYPE_STRING, p));
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz apr_hash_set(dispatch, "filename", APR_HASH_KEY_STRING,
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz makefun(&req_filename_field, APL_REQ_FUNTYPE_STRING, p));
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz apr_hash_set(dispatch, "canonical_filename", APR_HASH_KEY_STRING,
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz makefun(&req_canonical_filename_field, APL_REQ_FUNTYPE_STRING, p));
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz apr_hash_set(dispatch, "path_info", APR_HASH_KEY_STRING,
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz makefun(&req_path_info_field, APL_REQ_FUNTYPE_STRING, p));
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz apr_hash_set(dispatch, "args", APR_HASH_KEY_STRING,
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz makefun(&req_args_field, APL_REQ_FUNTYPE_STRING, p));
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz apr_hash_set(dispatch, "hostname", APR_HASH_KEY_STRING,
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz makefun(&req_hostname_field, APL_REQ_FUNTYPE_STRING, p));
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz apr_hash_set(dispatch, "uri", APR_HASH_KEY_STRING,
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz makefun(&req_uri_field, APL_REQ_FUNTYPE_STRING, p));
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz apr_hash_set(dispatch, "the_request", APR_HASH_KEY_STRING,
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz makefun(&req_the_request_field, APL_REQ_FUNTYPE_STRING, p));
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz apr_hash_set(dispatch, "method", APR_HASH_KEY_STRING,
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz makefun(&req_method_field, APL_REQ_FUNTYPE_STRING, p));
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz lua_pushlightuserdata(L, dispatch);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz lua_setfield(L, LUA_REGISTRYINDEX, "Apache2.Request.dispatch");
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz luaL_newmetatable(L, "Apache2.Request"); /* [metatable] */
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz lua_pushvalue(L, -1);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz lua_setfield(L, -2, "__index");
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz luaL_register(L, NULL, request_methods); /* [metatable] */
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz lua_pop(L, 2);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz luaL_newmetatable(L, "Apache2.Connection"); /* [metatable] */
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz lua_pushvalue(L, -1);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz lua_setfield(L, -2, "__index");
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz luaL_register(L, NULL, connection_methods); /* [metatable] */
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz lua_pop(L, 2);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz luaL_newmetatable(L, "Apache2.Server"); /* [metatable] */
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz lua_pushvalue(L, -1);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz lua_setfield(L, -2, "__index");
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz luaL_register(L, NULL, server_methods); /* [metatable] */
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz lua_pop(L, 2);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz}
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantzvoid apl_push_connection(lua_State* L, conn_rec* c) {
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz lua_boxpointer(L, c);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz luaL_getmetatable(L, "Apache2.Connection");
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz lua_setmetatable(L, -2);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz luaL_getmetatable(L, "Apache2.Connection");
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz apl_push_apr_table(L, "notes", c->notes);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz lua_pushstring(L, c->remote_ip);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz lua_setfield(L, -2, "remote_ip");
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz lua_pop(L, 1);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz}
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantzvoid apl_push_server(lua_State* L, server_rec* s) {
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz lua_boxpointer(L, s);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz luaL_getmetatable(L, "Apache2.Server");
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz lua_setmetatable(L, -2);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz luaL_getmetatable(L, "Apache2.Server");
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz lua_pushstring(L, s->server_hostname);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz lua_setfield(L, -2, "server_hostname");
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz lua_pop(L, 1);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz}
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantzvoid apl_push_request(lua_State* L, request_rec* r) {
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz lua_boxpointer(L, r);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz luaL_getmetatable(L, "Apache2.Request");
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz lua_setmetatable(L, -2);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz}
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz