lua_request.c revision 3261b7df2cb7439d9171ea29cd415fced8dcd2f4
/**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "mod_lua.h"
#include "util_script.h"
#include "lua_apr.h"
typedef char *(*req_field_string_f) (request_rec * r);
typedef int (*req_field_int_f) (request_rec * r);
{
int i;
int top = lua_gettop(L);
for (i = 1; i <= top; i++) {
int t = lua_type(L, i);
switch (t) {
case LUA_TSTRING:{
"%d: '%s'", i, lua_tostring(L, i));
break;
}
case LUA_TUSERDATA:{
i);
break;
}
case LUA_TLIGHTUSERDATA:{
"%d: lightuserdata", i);
break;
}
case LUA_TNIL:{
break;
}
case LUA_TNONE:{
break;
}
case LUA_TBOOLEAN:{
"%d: %s", i, lua_toboolean(L,
i) ? "true" :
"false");
break;
}
case LUA_TNUMBER:{
"%d: %g", i, lua_tonumber(L, i));
break;
}
case LUA_TTABLE:{
"%d: <table>", i);
break;
}
case LUA_TFUNCTION:{
"%d: <function>", i);
break;
}
default:{
"%d: unkown: -[%s]-", i, lua_typename(L, i));
break;
}
}
}
}
/**
* Verify that the thing at index is a request_rec wrapping
* userdata thingamajig and return it if it is. if it is not
* lua will enter its error handling routine.
*/
{
request_rec *r;
return r;
}
/* ------------------ request methods -------------------- */
/* helper callback for req_parseargs */
static int req_aprtable2luatable_cb(void *l, const char *key,
const char *value)
{
int t;
/* rstack_dump(L, RRR, "start of cb"); */
/* L is [table<s,t>, table<s,s>] */
/* build complex */
/* rstack_dump(L, RRR, "after getfield"); */
t = lua_type(L, -1);
switch (t) {
case LUA_TNIL:
case LUA_TNONE:{
lua_newtable(L); /* [array, table<s,t>, table<s,s>] */
break;
}
case LUA_TTABLE:{
/* [array, table<s,t>, table<s,s>] */
break;
}
}
/* L is [table<s,t>, table<s,s>] */
/* build simple */
}
else {
lua_pop(L, 1);
}
return 1;
}
/* r:parseargs() returning a lua table */
static int req_parseargs(lua_State *L)
{
lua_newtable(L);
lua_newtable(L); /* [table, table] */
ap_args_to_table(r, &form_table);
return 2; /* [table<string, string>, table<string, array<string>>] */
}
/* wrap ap_rputs as r:puts(String) */
{
int argc = lua_gettop(L);
int i;
for (i = 2; i <= argc; i++) {
ap_rputs(luaL_checkstring(L, i), r);
}
return 0;
}
/* wrap ap_rwrite as r:write(String) */
{
size_t n;
return 0;
}
/* r:addoutputfilter(name|function) */
static int req_add_output_filter(lua_State *L)
{
name);
return 0;
}
/* BEGIN dispatch mathods for request_rec fields */
/* not really a field, but we treat it like one */
static const char *req_document_root(request_rec *r)
{
return ap_document_root(r);
}
static char *req_uri_field(request_rec *r)
{
return r->uri;
}
static const char *req_method_field(request_rec *r)
{
return r->method;
}
static const char *req_hostname_field(request_rec *r)
{
return r->hostname;
}
static const char *req_args_field(request_rec *r)
{
return r->args;
}
static const char *req_path_info_field(request_rec *r)
{
return r->path_info;
}
static const char *req_canonical_filename_field(request_rec *r)
{
return r->canonical_filename;
}
static const char *req_filename_field(request_rec *r)
{
return r->filename;
}
static const char *req_user_field(request_rec *r)
{
return r->user;
}
static const char *req_unparsed_uri_field(request_rec *r)
{
return r->unparsed_uri;
}
static const char *req_ap_auth_type_field(request_rec *r)
{
return r->ap_auth_type;
}
static const char *req_content_encoding_field(request_rec *r)
{
return r->content_encoding;
}
static const char *req_content_type_field(request_rec *r)
{
return r->content_type;
}
static const char *req_range_field(request_rec *r)
{
return r->range;
}
static const char *req_protocol_field(request_rec *r)
{
return r->protocol;
}
static const char *req_the_request_field(request_rec *r)
{
return r->the_request;
}
static int req_status_field(request_rec *r)
{
return r->status;
}
static int req_assbackwards_field(request_rec *r)
{
return r->assbackwards;
}
{
return r->headers_in;
}
{
return r->headers_out;
}
{
return r->err_headers_out;
}
{
return r->subprocess_env;
}
{
return r->notes;
}
/* END dispatch mathods for request_rec fields */
static int req_dispatch(lua_State *L)
{
lua_pop(L, 2);
lua_pop(L, 1);
if (rft) {
case APL_REQ_FUNTYPE_TABLE:{
"request_rec->dispatching %s -> apr table",
name);
ap_lua_push_apr_table(L, rs);
return 1;
}
case APL_REQ_FUNTYPE_LUACFUN:{
"request_rec->dispatching %s -> lua_CFunction",
name);
lua_pushcfunction(L, func);
return 1;
}
case APL_REQ_FUNTYPE_STRING:{
char *rs;
"request_rec->dispatching %s -> string", name);
lua_pushstring(L, rs);
return 1;
}
case APL_REQ_FUNTYPE_INT:{
int rs;
"request_rec->dispatching %s -> int", name);
lua_pushnumber(L, rs);
return 1;
}
case APL_REQ_FUNTYPE_BOOLEAN:{
int rs;
"request_rec->dispatching %s -> boolean", name);
lua_pushboolean(L, rs);
return 1;
}
}
}
return 0;
}
/* helper function for the logging functions below */
{
const char *msg;
r, "%s", msg);
return 0;
}
/* r:debug(String) and friends which use apache logging */
{
req_log_at(L, APLOG_EMERG);
return 0;
}
{
req_log_at(L, APLOG_ALERT);
return 0;
}
{
req_log_at(L, APLOG_CRIT);
return 0;
}
{
req_log_at(L, APLOG_ERR);
return 0;
}
{
req_log_at(L, APLOG_WARNING);
return 0;
}
static int req_notice(lua_State *L)
{
req_log_at(L, APLOG_NOTICE);
return 0;
}
{
req_log_at(L, APLOG_INFO);
return 0;
}
{
req_log_at(L, APLOG_DEBUG);
return 0;
}
/* handle r.status = 201 */
static int req_newindex(lua_State *L)
{
const char *key;
/* request_rec* r = lua_touserdata(L, lua_upvalueindex(1)); */
/* const char* key = luaL_checkstring(L, -2); */
return 0;
}
return 0;
}
return 0;
}
return 0;
}
return 0;
}
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}, */
};
static const struct luaL_Reg connection_methods[] = {
};
static const struct luaL_Reg server_methods[] = {
};
{
return rft;
}
{
p));
APL_REQ_FUNTYPE_STRING, p));
lua_pushvalue(L, -1);
lua_pop(L, 2);
lua_pushvalue(L, -1);
lua_pop(L, 2);
lua_pushvalue(L, -1);
lua_pop(L, 2);
}
{
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_pushstring(L, c->remote_ip);
lua_pop(L, 1);
}
{
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_pop(L, 1);
}
{
lua_boxpointer(L, r);
luaL_getmetatable(L, "Apache2.Request");
lua_setmetatable(L, -2);
}