request.c revision cb1e20df951447249fd1344ae04a790219a16be5
842ae4bd224140319ae7feec1872b93dfd491143fielding * Licensed to the Apache Software Foundation (ASF) under one or more
842ae4bd224140319ae7feec1872b93dfd491143fielding * contributor license agreements. See the NOTICE file distributed with
842ae4bd224140319ae7feec1872b93dfd491143fielding * this work for additional information regarding copyright ownership.
842ae4bd224140319ae7feec1872b93dfd491143fielding * The ASF licenses this file to You under the Apache License, Version 2.0
842ae4bd224140319ae7feec1872b93dfd491143fielding * (the "License"); you may not use this file except in compliance with
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * the License. You may obtain a copy of the License at
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * Unless required by applicable law or agreed to in writing, software
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * distributed under the License is distributed on an "AS IS" BASIS,
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * See the License for the specific language governing permissions and
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * limitations under the License.
1747d30b98aa1bdbc43994c02cd46ab4cb9319e4fieldingtypedef char* (*req_field_string_f) (request_rec* r);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholesvoid rstack_dump(lua_State* L, request_rec* r, const char* msg) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, "Lua Stack Dump: [%s]", msg);
11f2c481e1d57bedb3f758565307501e9a2730ddtrawick int t = lua_type(L, i);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, "%d: userdata", i);
d266c3777146d36a4c23c17aad6f153aebea1bf4jorton ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, "%d: lightuserdata", i);
22f8da8087791fcb95b836c8a81937c5a9bba202bnicholes "%d: NIL", i);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes "%d: None", i);
0f60998368b493f90120180a93fc2e1e74490872covener "%d: %s", i, lua_toboolean(L, i) ? "true" : "false");
fa123db15501821e36e513afa78e839775ad2800covener "%d: <table>", i);
0568280364eb026393be492ebc732795c4934643jorton "%d: <function>", i);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * Verify that the thing at index is a request_rec wrapping
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * userdata thingamajig and return it if it is. if it is not
796e4a7141265d8ed7036e4628161c6eafb2a789jorton * lua will enter its error handling routine.
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholesstatic request_rec* apw_check_request_rec(lua_State* L, int index) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes request_rec* r = (request_rec*)lua_unboxpointer(L, index);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes/* ------------------ request methods -------------------- */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes/* helper callback for req_parseargs */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholesstatic int req_aprtable2luatable_cb(void *l, const char *key, const char *value) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes lua_State* L = (lua_State*)l; /* [table<s,t>, table<s,s>] */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* rstack_dump(L, RRR, "start of cb"); */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* L is [table<s,t>, table<s,s>] */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* build complex */
8113dac419143273351446c3ad653f3fe5ba5cfdwrowe lua_getfield(L, -1, key); /* [VALUE, table<s,t>, table<s,s>] */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* rstack_dump(L, RRR, "after getfield"); */
95b6fe1346805e1731e6e97c15d569c73be22cf7minfrin lua_newtable(L); /* [array, table<s,t>, table<s,s>] */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes lua_pushnumber(L, 1); /* [1, array, table<s,t>, table<s,s>] */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes lua_pushstring(L, value); /* [string, 1, array, table<s,t>, table<s,s>] */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes lua_settable(L, -3); /* [array, table<s,t>, table<s,s>] */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes lua_setfield(L, -2, key); /* [table<s,t>, table<s,s>] */
95b6fe1346805e1731e6e97c15d569c73be22cf7minfrin /* [array, table<s,t>, table<s,s>] */
713a2b68bac4aeb1e9c48785006c0732451039depquerna lua_pushnumber(L, size + 1); /* [#, array, table<s,t>, table<s,s>] */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes lua_pushstring(L, value); /* [string, #, array, table<s,t>, table<s,s>] */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes lua_settable(L, -3); /* [array, table<s,t>, table<s,s>] */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes lua_setfield(L, -2, key); /* [table<s,t>, table<s,s>] */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* L is [table<s,t>, table<s,s>] */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* build simple */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes lua_getfield(L, -2, key); /* [VALUE, table<s,s>, table<s,t>] */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (lua_isnoneornil(L, -1)) { /* only set if not already set */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes lua_pushstring(L, value); /* [string, table<s,s>, table<s,t>] */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes lua_setfield(L, -3, key); /* [table<s,s>, table<s,t>] */
f43b67c5a9d29b572eac916f8335cedc80c908bebnicholes/* r:parseargs() returning a lua table */
fa123db15501821e36e513afa78e839775ad2800covener apr_table_do(req_aprtable2luatable_cb, L, form_table, NULL);
fa123db15501821e36e513afa78e839775ad2800covener return 2; /* [table<string, string>, table<string, array<string>>] */
fa123db15501821e36e513afa78e839775ad2800covener/* wrap ap_rputs as r:puts(String) */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes/* wrap ap_rwrite as r:write(String) */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes/* r:parsebody() */
cceddc0b6c0fdaed0c73abda39975bb1d388243acovener if (ap_body_to_table(r, &form_table) == APR_SUCCESS) {
fa123db15501821e36e513afa78e839775ad2800covener apr_table_do(req_aprtable2luatable_cb, L, form_table, NULL);
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener/* r:addoutputfilter(name|function) */
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, "adding output filter %s", name);
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener char* doc_root = apr_pstrdup(r->pool, ap_document_root(r));
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener/* BEGIN dispatch mathods for request_rec fields */
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovenerstatic const char* req_method_field(request_rec* r) {
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovenerstatic const char* req_hostname_field(request_rec* r) {
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovenerstatic const char* req_path_info_field(request_rec* r) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholesstatic const char* req_canonical_filename_field(request_rec* r) {
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovenerstatic const char* req_filename_field(request_rec* r) {
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovenerstatic const char* req_unparsed_uri_field(request_rec* r) {
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovenerstatic const char* req_ap_auth_type_field(request_rec* r) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholesstatic const char* req_content_encoding_field(request_rec* r) {
b08925593f214f621161742925dcf074a8047e0acovenerstatic const char* req_content_type_field(request_rec* r) {
3dfeb02cfb853d8717ca0cc259b59fea610173f5bnicholesstatic const char* req_protocol_field(request_rec* r) {
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholesstatic const char* req_the_request_field(request_rec* r) {
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholesstatic int req_assbackwards_field(request_rec* r) {
513b324e774c559b579896df131fd7c8471ed529rederpj/* END dispatch mathods for request_rec fields */
513b324e774c559b579896df131fd7c8471ed529rederpj lua_getfield(L, LUA_REGISTRYINDEX, "Apache2.Request.dispatch");
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes req_fun_t* rft = apr_hash_get(dispatch, name, APR_HASH_KEY_STRING);
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes "request_rec->dispatching %s -> apr table (NOT IMPLEMENTED YET)", name);
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes "request_rec->dispatching %s -> lua_CFunction", name);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes/* helper function for the logging functions below */
9ad7b260be233be7d7b5576979825cac72e15498rederpj ap_log_rerror(dbg.source, dbg.currentline, level, 0, r, msg);
707f6d077f73cc948deead8df5b40ea42c1eaa78covener/* r:debug(String) and friends which use apache logging */
707f6d077f73cc948deead8df5b40ea42c1eaa78covenerstatic int req_emerg(lua_State* L) { req_log_at(L, APLOG_EMERG); return 0; }
707f6d077f73cc948deead8df5b40ea42c1eaa78covenerstatic int req_alert(lua_State* L) { req_log_at(L, APLOG_ALERT); return 0; }
707f6d077f73cc948deead8df5b40ea42c1eaa78covenerstatic int req_crit(lua_State* L) { req_log_at(L, APLOG_CRIT); return 0; }
9ad7b260be233be7d7b5576979825cac72e15498rederpjstatic int req_err(lua_State* L) { req_log_at(L, APLOG_ERR); return 0; }
707f6d077f73cc948deead8df5b40ea42c1eaa78covenerstatic int req_warn(lua_State* L) { req_log_at(L, APLOG_WARNING); return 0; }
707f6d077f73cc948deead8df5b40ea42c1eaa78covenerstatic int req_notice(lua_State* L) { req_log_at(L, APLOG_NOTICE); return 0; }
707f6d077f73cc948deead8df5b40ea42c1eaa78covenerstatic int req_info(lua_State* L) { req_log_at(L, APLOG_INFO); return 0; }
707f6d077f73cc948deead8df5b40ea42c1eaa78covenerstatic int req_debug(lua_State* L) { req_log_at(L, APLOG_DEBUG); return 0; }
707f6d077f73cc948deead8df5b40ea42c1eaa78covener/* handle r.status = 201 */
707f6d077f73cc948deead8df5b40ea42c1eaa78covener /* request_rec* r = lua_touserdata(L, lua_upvalueindex(1)); */
707f6d077f73cc948deead8df5b40ea42c1eaa78covener /* const char* key = luaL_checkstring(L, -2); */
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes lua_pushstring(L, apr_psprintf(r->pool, "Property [%s] may not be set on a request_rec", key));
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes /* {"__newindex", req_set_field}, */
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholesstatic const struct luaL_Reg connection_methods[] = {
4be9c459920a7c1cfe62d654327dae5c4bb6b284sfstatic req_fun_t* makefun(void* fun, int type, apr_pool_t* pool) {
4be9c459920a7c1cfe62d654327dae5c4bb6b284sfvoid apw_load_request_lmodule(lua_State *L, apr_pool_t *p) {
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf apr_hash_set(dispatch, "document_root", APR_HASH_KEY_STRING,
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes makefun(&req_document_root, APW_REQ_FUNTYPE_LUACFUN, p));
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes apr_hash_set(dispatch, "parseargs", APR_HASH_KEY_STRING,
7dbf29be626018bc389ef94c1846aeac4b72633bsf apr_hash_set(dispatch, "add_output_filter", APR_HASH_KEY_STRING,
7dbf29be626018bc389ef94c1846aeac4b72633bsf makefun(&req_add_output_filter, APW_REQ_FUNTYPE_LUACFUN, p));
7dbf29be626018bc389ef94c1846aeac4b72633bsf apr_hash_set(dispatch, "assbackwards", APR_HASH_KEY_STRING,
7dbf29be626018bc389ef94c1846aeac4b72633bsf makefun(&req_assbackwards_field, APW_REQ_FUNTYPE_BOOLEAN, p));
7dbf29be626018bc389ef94c1846aeac4b72633bsf makefun(&req_protocol_field, APW_REQ_FUNTYPE_STRING, p));
7dbf29be626018bc389ef94c1846aeac4b72633bsf apr_hash_set(dispatch, "content_type", APR_HASH_KEY_STRING,
7dbf29be626018bc389ef94c1846aeac4b72633bsf makefun(&req_content_type_field, APW_REQ_FUNTYPE_STRING, p));
7dbf29be626018bc389ef94c1846aeac4b72633bsf apr_hash_set(dispatch, "content_encoding", APR_HASH_KEY_STRING,
7dbf29be626018bc389ef94c1846aeac4b72633bsf makefun(&req_content_encoding_field, APW_REQ_FUNTYPE_STRING, p));
7dbf29be626018bc389ef94c1846aeac4b72633bsf apr_hash_set(dispatch, "ap_auth_type", APR_HASH_KEY_STRING,
7dbf29be626018bc389ef94c1846aeac4b72633bsf makefun(&req_ap_auth_type_field, APW_REQ_FUNTYPE_STRING, p));
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes apr_hash_set(dispatch, "unparsed_uri", APR_HASH_KEY_STRING,
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes makefun(&req_unparsed_uri_field, APW_REQ_FUNTYPE_STRING, p));
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes apr_hash_set(dispatch, "user", APR_HASH_KEY_STRING,
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes makefun(&req_user_field, APW_REQ_FUNTYPE_STRING, p));
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes apr_hash_set(dispatch, "filename", APR_HASH_KEY_STRING,
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes makefun(&req_filename_field, APW_REQ_FUNTYPE_STRING, p));
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes apr_hash_set(dispatch, "canonical_filename", APR_HASH_KEY_STRING,
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes makefun(&req_canonical_filename_field, APW_REQ_FUNTYPE_STRING, p));
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes apr_hash_set(dispatch, "path_info", APR_HASH_KEY_STRING,
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes makefun(&req_path_info_field, APW_REQ_FUNTYPE_STRING, p));
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes apr_hash_set(dispatch, "args", APR_HASH_KEY_STRING,
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes makefun(&req_hostname_field, APW_REQ_FUNTYPE_STRING, p));
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes apr_hash_set(dispatch, "uri", APR_HASH_KEY_STRING,
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes makefun(&req_uri_field, APW_REQ_FUNTYPE_STRING, p));
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes apr_hash_set(dispatch, "the_request", APR_HASH_KEY_STRING,
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes makefun(&req_the_request_field, APW_REQ_FUNTYPE_STRING, p));
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes apr_hash_set(dispatch, "method", APR_HASH_KEY_STRING,
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes makefun(&req_method_field, APW_REQ_FUNTYPE_STRING, p));
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes lua_setfield(L, LUA_REGISTRYINDEX, "Apache2.Request.dispatch");
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes luaL_newmetatable(L, "Apache2.Request"); /* [metatable] */
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes luaL_register(L, NULL, request_methods); /* [metatable] */
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes luaL_newmetatable(L, "Apache2.Connection"); /* [metatable] */
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes luaL_register(L, NULL, connection_methods); /* [metatable] */
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf luaL_newmetatable(L, "Apache2.Server"); /* [metatable] */
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf luaL_register(L, NULL, server_methods); /* [metatable] */