lua_request.c revision cc150a9f6921c716256f94eaaf81075606d90890
/**
* 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"
#include "scoreboard.h"
#define POST_MAX_VARS 500
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: unknown: -[%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;
}
/*
=======================================================================================================================
lua_read_body(request_rec *r, const char **rbuf, apr_off_t *size): Reads any additional form data sent in POST/PUT
requests. Used for multipart POST data.
=======================================================================================================================
*/
{
return (rc);
}
if (ap_should_client_block(r)) {
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
char argsbuffer[HUGE_STRING_LEN];
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
}
else {
}
}
}
return (rc);
}
/* 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>>] */
}
/* r:parsebody(): Parses regular (url-enocded) or multipart POST data and returns two tables*/
static int req_parsebody(lua_State *L)
{
int res;
char *multipart;
const char *contentType;
lua_newtable(L);
lua_newtable(L); /* [table, table] */
if (contentType != NULL && (sscanf(contentType, "multipart/form-data; boundary=%250c", multipart) == 1)) {
const char *data;
int i;
return 2;
}
i = 0;
for
(
) {
i++;
if (i == POST_MAX_VARS) break;
if (!crlf) break;
"Content-Disposition: form-data; name=\"%255[^\"]\"; filename=\"%255[^\"]\"",
}
}
}
else {
char *buffer;
}
}
}
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;
int rv;
lua_pushinteger(L, rv);
return 1;
}
/* r:addoutputfilter(name|function) */
static int req_add_output_filter(lua_State *L)
{
name);
return 0;
}
/* wrap ap_construct_url as r:construct_url(String) */
static int req_construct_url(lua_State *L)
{
return 1;
}
/* wrap ap_escape_html r:escape_html(String) */
static int req_escape_html(lua_State *L)
{
const char *s = luaL_checkstring(L, 2);
return 1;
}
/* wrap optional ssl_var_lookup as r:ssl_var_lookup(String) */
static int req_ssl_var_lookup(lua_State *L)
{
const char *s = luaL_checkstring(L, 2);
(char *)s);
lua_pushstring(L, res);
return 1;
}
/* 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 const char *req_context_prefix(request_rec *r)
{
return ap_context_prefix(r);
}
static const char *req_context_document_root(request_rec *r)
{
return ap_context_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_handler_field(request_rec *r)
{
return r->handler;
}
static const char *req_proxyreq_field(request_rec *r)
{
switch (r->proxyreq) {
case PROXYREQ_NONE: return "PROXYREQ_NONE";
case PROXYREQ_PROXY: return "PROXYREQ_PROXY";
case PROXYREQ_REVERSE: return "PROXYREQ_REVERSE";
case PROXYREQ_RESPONSE: return "PROXYREQ_RESPONSE";
default: return NULL;
}
}
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 const char *req_log_id_field(request_rec *r)
{
return r->log_id;
}
static const char *req_useragent_ip_field(request_rec *r)
{
return r->useragent_ip;
}
static int req_remaining_field(request_rec *r)
{
return r->remaining;
}
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;
}
static int req_ssl_is_https_field(request_rec *r)
{
return ap_lua_ssl_is_https(r->connection);
}
static int lua_ap_rflush (lua_State *L) {
int returnValue;
request_rec *r;
r = ap_lua_check_request_rec(L, 1);
returnValue = ap_rflush(r);
lua_pushboolean(L, (returnValue == 0));
return 1;
}
static int lua_ap_port(request_rec* r)
{
return (int) ap_get_server_port(r);
}
static const char* lua_ap_options(request_rec* r)
{
int opts;
opts = ap_allow_options(r);
return apr_psprintf(r->pool, "%s %s %s %s %s %s", (opts&OPT_INDEXES) ? "Indexes" : "", (opts&OPT_INCLUDES) ? "Includes" : "", (opts&OPT_SYM_LINKS) ? "FollowSymLinks" : "", (opts&OPT_EXECCGI) ? "ExecCGI" : "", (opts&OPT_MULTI) ? "MultiViews" : "", (opts&OPT_ALL) == OPT_ALL ? "All" : "" );
}
static const char* lua_ap_allowoverrides(request_rec* r)
{
int opts;
opts = ap_allow_overrides(r);
return apr_psprintf(r->pool, "%s %s %s %s %s %s", (opts&OR_NONE) ? "None" : "", (opts&OR_LIMIT) ? "Limit" : "", (opts&OR_OPTIONS) ? "Options" : "", (opts&OR_FILEINFO) ? "FileInfo" : "", (opts&OR_AUTHCFG) ? "AuthCfg" : "", (opts&OR_INDEXES) ? "Indexes" : "" );
}
static int lua_ap_started(request_rec* r)
{
}
static const char* lua_ap_basic_auth_pw(request_rec* r)
{
ap_get_basic_auth_pw(r, &pw);
}
static int lua_ap_limit_req_body(request_rec* r)
{
return (int) ap_get_limit_req_body(r);
}
static int lua_ap_is_initial_req(request_rec *r)
{
return ap_is_initial_req(r);
}
static int lua_ap_some_auth_required(request_rec *r)
{
return ap_some_auth_required(r);
}
static int lua_ap_sendfile(lua_State *L)
{
const char *filename;
request_rec *r;
r = ap_lua_check_request_rec(L, 1);
lua_pushboolean(L, 0);
}
else {
r->pool);
if (rc == APR_SUCCESS) {
lua_pushinteger(L, sent);
}
else {
lua_pushboolean(L, 0);
}
}
return (1);
}
/* END dispatch methods 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 */
{
return req_log_at(L, APLOG_EMERG);
}
{
return req_log_at(L, APLOG_ALERT);
}
{
return req_log_at(L, APLOG_CRIT);
}
{
return req_log_at(L, APLOG_ERR);
}
{
return req_log_at(L, APLOG_WARNING);
}
static int req_notice(lua_State *L)
{
return req_log_at(L, APLOG_NOTICE);
}
{
return req_log_at(L, APLOG_INFO);
}
{
return req_log_at(L, APLOG_DEBUG);
}
{ \
}
/* 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;
}
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->client_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);
}