2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
19 #include "util_script.h"
22 APLOG_USE_MODULE(lua);
24 typedef char *(*req_field_string_f) (request_rec * r);
25 typedef int (*req_field_int_f) (request_rec * r);
26 typedef apr_table_t *(*req_field_apr_table_f) (request_rec * r);
28 void ap_lua_rstack_dump(lua_State *L, request_rec *r, const char *msg)
31 int top = lua_gettop(L);
33 ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, "Lua Stack Dump: [%s]", msg);
35 for (i = 1; i <= top; i++) {
36 int t = lua_type(L, i);
39 ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r,
40 "%d: '%s'", i, lua_tostring(L, i));
44 ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, "%d: userdata",
48 case LUA_TLIGHTUSERDATA:{
49 ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r,
50 "%d: lightuserdata", i);
54 ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, "%d: NIL", i);
58 ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, "%d: None", i);
62 ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r,
63 "%d: %s", i, lua_toboolean(L,
69 ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r,
70 "%d: %g", i, lua_tonumber(L, i));
74 ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r,
79 ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r,
84 ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r,
85 "%d: unkown: -[%s]-", i, lua_typename(L, i));
93 * Verify that the thing at index is a request_rec wrapping
94 * userdata thingamajig and return it if it is. if it is not
95 * lua will enter its error handling routine.
97 static request_rec *ap_lua_check_request_rec(lua_State *L, int index)
100 luaL_checkudata(L, index, "Apache2.Request");
101 r = (request_rec *) lua_unboxpointer(L, index);
105 /* ------------------ request methods -------------------- */
106 /* helper callback for req_parseargs */
107 static int req_aprtable2luatable_cb(void *l, const char *key,
111 lua_State *L = (lua_State *) l; /* [table<s,t>, table<s,s>] */
112 /* rstack_dump(L, RRR, "start of cb"); */
113 /* L is [table<s,t>, table<s,s>] */
116 lua_getfield(L, -1, key); /* [VALUE, table<s,t>, table<s,s>] */
117 /* rstack_dump(L, RRR, "after getfield"); */
122 lua_pop(L, 1); /* [table<s,t>, table<s,s>] */
123 lua_newtable(L); /* [array, table<s,t>, table<s,s>] */
124 lua_pushnumber(L, 1); /* [1, array, table<s,t>, table<s,s>] */
125 lua_pushstring(L, value); /* [string, 1, array, table<s,t>, table<s,s>] */
126 lua_settable(L, -3); /* [array, table<s,t>, table<s,s>] */
127 lua_setfield(L, -2, key); /* [table<s,t>, table<s,s>] */
131 /* [array, table<s,t>, table<s,s>] */
132 int size = lua_objlen(L, -1);
133 lua_pushnumber(L, size + 1); /* [#, array, table<s,t>, table<s,s>] */
134 lua_pushstring(L, value); /* [string, #, array, table<s,t>, table<s,s>] */
135 lua_settable(L, -3); /* [array, table<s,t>, table<s,s>] */
136 lua_setfield(L, -2, key); /* [table<s,t>, table<s,s>] */
141 /* L is [table<s,t>, table<s,s>] */
143 lua_getfield(L, -2, key); /* [VALUE, table<s,s>, table<s,t>] */
144 if (lua_isnoneornil(L, -1)) { /* only set if not already set */
145 lua_pop(L, 1); /* [table<s,s>, table<s,t>]] */
146 lua_pushstring(L, value); /* [string, table<s,s>, table<s,t>] */
147 lua_setfield(L, -3, key); /* [table<s,s>, table<s,t>] */
155 /* r:parseargs() returning a lua table */
156 static int req_parseargs(lua_State *L)
158 apr_table_t *form_table;
159 request_rec *r = ap_lua_check_request_rec(L, 1);
161 lua_newtable(L); /* [table, table] */
162 ap_args_to_table(r, &form_table);
163 apr_table_do(req_aprtable2luatable_cb, L, form_table, NULL);
164 return 2; /* [table<string, string>, table<string, array<string>>] */
167 /* wrap ap_rputs as r:puts(String) */
168 static int req_puts(lua_State *L)
170 request_rec *r = ap_lua_check_request_rec(L, 1);
172 int argc = lua_gettop(L);
175 for (i = 2; i <= argc; i++) {
176 ap_rputs(luaL_checkstring(L, i), r);
181 /* wrap ap_rwrite as r:write(String) */
182 static int req_write(lua_State *L)
184 request_rec *r = ap_lua_check_request_rec(L, 1);
186 const char *buf = luaL_checklstring(L, 2, &n);
188 ap_rwrite((void *) buf, n, r);
192 /* r:addoutputfilter(name|function) */
193 static int req_add_output_filter(lua_State *L)
195 request_rec *r = ap_lua_check_request_rec(L, 1);
196 const char *name = luaL_checkstring(L, 2);
197 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, "adding output filter %s",
199 ap_add_output_filter(name, L, r, r->connection);
203 /* BEGIN dispatch mathods for request_rec fields */
205 /* not really a field, but we treat it like one */
206 static const char *req_document_root(request_rec *r)
208 return ap_document_root(r);
211 static char *req_uri_field(request_rec *r)
216 static const char *req_method_field(request_rec *r)
221 static const char *req_hostname_field(request_rec *r)
226 static const char *req_args_field(request_rec *r)
231 static const char *req_path_info_field(request_rec *r)
236 static const char *req_canonical_filename_field(request_rec *r)
238 return r->canonical_filename;
241 static const char *req_filename_field(request_rec *r)
246 static const char *req_user_field(request_rec *r)
251 static const char *req_unparsed_uri_field(request_rec *r)
253 return r->unparsed_uri;
256 static const char *req_ap_auth_type_field(request_rec *r)
258 return r->ap_auth_type;
261 static const char *req_content_encoding_field(request_rec *r)
263 return r->content_encoding;
266 static const char *req_content_type_field(request_rec *r)
268 return r->content_type;
271 static const char *req_range_field(request_rec *r)
276 static const char *req_protocol_field(request_rec *r)
281 static const char *req_the_request_field(request_rec *r)
283 return r->the_request;
286 static int req_status_field(request_rec *r)
291 static int req_assbackwards_field(request_rec *r)
293 return r->assbackwards;
296 static apr_table_t* req_headers_in(request_rec *r)
298 return r->headers_in;
301 static apr_table_t* req_headers_out(request_rec *r)
303 return r->headers_out;
306 static apr_table_t* req_err_headers_out(request_rec *r)
308 return r->err_headers_out;
311 static apr_table_t* req_subprocess_env(request_rec *r)
313 return r->subprocess_env;
316 static apr_table_t* req_notes(request_rec *r)
321 /* END dispatch mathods for request_rec fields */
323 static int req_dispatch(lua_State *L)
325 apr_hash_t *dispatch;
327 request_rec *r = ap_lua_check_request_rec(L, 1);
328 const char *name = luaL_checkstring(L, 2);
331 lua_getfield(L, LUA_REGISTRYINDEX, "Apache2.Request.dispatch");
332 dispatch = lua_touserdata(L, 1);
335 rft = apr_hash_get(dispatch, name, APR_HASH_KEY_STRING);
338 case APL_REQ_FUNTYPE_TABLE:{
340 req_field_apr_table_f func = (req_field_apr_table_f)rft->fun;
341 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
342 "request_rec->dispatching %s -> apr table",
345 ap_lua_push_apr_table(L, rs);
349 case APL_REQ_FUNTYPE_LUACFUN:{
350 lua_CFunction func = (lua_CFunction)rft->fun;
351 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
352 "request_rec->dispatching %s -> lua_CFunction",
354 lua_pushcfunction(L, func);
357 case APL_REQ_FUNTYPE_STRING:{
358 req_field_string_f func = (req_field_string_f)rft->fun;
360 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
361 "request_rec->dispatching %s -> string", name);
363 lua_pushstring(L, rs);
366 case APL_REQ_FUNTYPE_INT:{
367 req_field_int_f func = (req_field_int_f)rft->fun;
369 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
370 "request_rec->dispatching %s -> int", name);
372 lua_pushnumber(L, rs);
375 case APL_REQ_FUNTYPE_BOOLEAN:{
376 req_field_int_f func = (req_field_int_f)rft->fun;
378 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
379 "request_rec->dispatching %s -> boolean", name);
381 lua_pushboolean(L, rs);
387 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, "nothing for %s", name);
391 /* helper function for the logging functions below */
392 static int req_log_at(lua_State *L, int level)
395 request_rec *r = ap_lua_check_request_rec(L, 1);
398 lua_getstack(L, 1, &dbg);
399 lua_getinfo(L, "Sl", &dbg);
401 msg = luaL_checkstring(L, 2);
402 ap_log_rerror(dbg.source, dbg.currentline, APLOG_MODULE_INDEX, level, 0, r, msg);
406 /* r:debug(String) and friends which use apache logging */
407 static int req_emerg(lua_State *L)
409 req_log_at(L, APLOG_EMERG);
412 static int req_alert(lua_State *L)
414 req_log_at(L, APLOG_ALERT);
417 static int req_crit(lua_State *L)
419 req_log_at(L, APLOG_CRIT);
422 static int req_err(lua_State *L)
424 req_log_at(L, APLOG_ERR);
427 static int req_warn(lua_State *L)
429 req_log_at(L, APLOG_WARNING);
432 static int req_notice(lua_State *L)
434 req_log_at(L, APLOG_NOTICE);
437 static int req_info(lua_State *L)
439 req_log_at(L, APLOG_INFO);
442 static int req_debug(lua_State *L)
444 req_log_at(L, APLOG_DEBUG);
448 /* handle r.status = 201 */
449 static int req_newindex(lua_State *L)
452 /* request_rec* r = lua_touserdata(L, lua_upvalueindex(1)); */
453 /* const char* key = luaL_checkstring(L, -2); */
454 request_rec *r = ap_lua_check_request_rec(L, 1);
455 key = luaL_checkstring(L, 2);
456 if (0 == strcmp("status", key)) {
457 int code = luaL_checkinteger(L, 3);
462 if (0 == strcmp("content_type", key)) {
463 const char *value = luaL_checkstring(L, 3);
464 ap_set_content_type(r, apr_pstrdup(r->pool, value));
468 if (0 == strcmp("filename", key)) {
469 const char *value = luaL_checkstring(L, 3);
470 r->filename = apr_pstrdup(r->pool, value);
474 if (0 == strcmp("uri", key)) {
475 const char *value = luaL_checkstring(L, 3);
476 r->uri = apr_pstrdup(r->pool, value);
480 if (0 == apr_strnatcmp("user", key)) {
481 const char *value = luaL_checkstring(L, 3);
482 r->user = apr_pstrdup(r->pool, value);
487 apr_psprintf(r->pool,
488 "Property [%s] may not be set on a request_rec",
494 static const struct luaL_Reg request_methods[] = {
495 {"__index", req_dispatch},
496 {"__newindex", req_newindex},
497 /* {"__newindex", req_set_field}, */
502 static const struct luaL_Reg connection_methods[] = {
507 static const struct luaL_Reg server_methods[] = {
512 static req_fun_t *makefun(const void *fun, int type, apr_pool_t *pool)
514 req_fun_t *rft = apr_palloc(pool, sizeof(req_fun_t));
520 AP_LUA_DECLARE(void) ap_lua_load_request_lmodule(lua_State *L, apr_pool_t *p)
523 apr_hash_t *dispatch = apr_hash_make(p);
525 apr_hash_set(dispatch, "puts", APR_HASH_KEY_STRING,
526 makefun(&req_puts, APL_REQ_FUNTYPE_LUACFUN, p));
527 apr_hash_set(dispatch, "write", APR_HASH_KEY_STRING,
528 makefun(&req_write, APL_REQ_FUNTYPE_LUACFUN, p));
529 apr_hash_set(dispatch, "document_root", APR_HASH_KEY_STRING,
530 makefun(&req_document_root, APL_REQ_FUNTYPE_STRING, p));
531 apr_hash_set(dispatch, "parseargs", APR_HASH_KEY_STRING,
532 makefun(&req_parseargs, APL_REQ_FUNTYPE_LUACFUN, p));
533 apr_hash_set(dispatch, "debug", APR_HASH_KEY_STRING,
534 makefun(&req_debug, APL_REQ_FUNTYPE_LUACFUN, p));
535 apr_hash_set(dispatch, "info", APR_HASH_KEY_STRING,
536 makefun(&req_info, APL_REQ_FUNTYPE_LUACFUN, p));
537 apr_hash_set(dispatch, "notice", APR_HASH_KEY_STRING,
538 makefun(&req_notice, APL_REQ_FUNTYPE_LUACFUN, p));
539 apr_hash_set(dispatch, "warn", APR_HASH_KEY_STRING,
540 makefun(&req_warn, APL_REQ_FUNTYPE_LUACFUN, p));
541 apr_hash_set(dispatch, "err", APR_HASH_KEY_STRING,
542 makefun(&req_err, APL_REQ_FUNTYPE_LUACFUN, p));
543 apr_hash_set(dispatch, "crit", APR_HASH_KEY_STRING,
544 makefun(&req_crit, APL_REQ_FUNTYPE_LUACFUN, p));
545 apr_hash_set(dispatch, "alert", APR_HASH_KEY_STRING,
546 makefun(&req_alert, APL_REQ_FUNTYPE_LUACFUN, p));
547 apr_hash_set(dispatch, "emerg", APR_HASH_KEY_STRING,
548 makefun(&req_emerg, APL_REQ_FUNTYPE_LUACFUN, p));
549 apr_hash_set(dispatch, "add_output_filter", APR_HASH_KEY_STRING,
550 makefun(&req_add_output_filter, APL_REQ_FUNTYPE_LUACFUN, p));
551 apr_hash_set(dispatch, "assbackwards", APR_HASH_KEY_STRING,
552 makefun(&req_assbackwards_field, APL_REQ_FUNTYPE_BOOLEAN, p));
553 apr_hash_set(dispatch, "status", APR_HASH_KEY_STRING,
554 makefun(&req_status_field, APL_REQ_FUNTYPE_INT, p));
555 apr_hash_set(dispatch, "protocol", APR_HASH_KEY_STRING,
556 makefun(&req_protocol_field, APL_REQ_FUNTYPE_STRING, p));
557 apr_hash_set(dispatch, "range", APR_HASH_KEY_STRING,
558 makefun(&req_range_field, APL_REQ_FUNTYPE_STRING, p));
559 apr_hash_set(dispatch, "content_type", APR_HASH_KEY_STRING,
560 makefun(&req_content_type_field, APL_REQ_FUNTYPE_STRING, p));
561 apr_hash_set(dispatch, "content_encoding", APR_HASH_KEY_STRING,
562 makefun(&req_content_encoding_field, APL_REQ_FUNTYPE_STRING,
564 apr_hash_set(dispatch, "ap_auth_type", APR_HASH_KEY_STRING,
565 makefun(&req_ap_auth_type_field, APL_REQ_FUNTYPE_STRING, p));
566 apr_hash_set(dispatch, "unparsed_uri", APR_HASH_KEY_STRING,
567 makefun(&req_unparsed_uri_field, APL_REQ_FUNTYPE_STRING, p));
568 apr_hash_set(dispatch, "user", APR_HASH_KEY_STRING,
569 makefun(&req_user_field, APL_REQ_FUNTYPE_STRING, p));
570 apr_hash_set(dispatch, "filename", APR_HASH_KEY_STRING,
571 makefun(&req_filename_field, APL_REQ_FUNTYPE_STRING, p));
572 apr_hash_set(dispatch, "canonical_filename", APR_HASH_KEY_STRING,
573 makefun(&req_canonical_filename_field,
574 APL_REQ_FUNTYPE_STRING, p));
575 apr_hash_set(dispatch, "path_info", APR_HASH_KEY_STRING,
576 makefun(&req_path_info_field, APL_REQ_FUNTYPE_STRING, p));
577 apr_hash_set(dispatch, "args", APR_HASH_KEY_STRING,
578 makefun(&req_args_field, APL_REQ_FUNTYPE_STRING, p));
579 apr_hash_set(dispatch, "hostname", APR_HASH_KEY_STRING,
580 makefun(&req_hostname_field, APL_REQ_FUNTYPE_STRING, p));
581 apr_hash_set(dispatch, "uri", APR_HASH_KEY_STRING,
582 makefun(&req_uri_field, APL_REQ_FUNTYPE_STRING, p));
583 apr_hash_set(dispatch, "the_request", APR_HASH_KEY_STRING,
584 makefun(&req_the_request_field, APL_REQ_FUNTYPE_STRING, p));
585 apr_hash_set(dispatch, "method", APR_HASH_KEY_STRING,
586 makefun(&req_method_field, APL_REQ_FUNTYPE_STRING, p));
587 apr_hash_set(dispatch, "headers_in", APR_HASH_KEY_STRING,
588 makefun(&req_headers_in, APL_REQ_FUNTYPE_TABLE, p));
589 apr_hash_set(dispatch, "headers_out", APR_HASH_KEY_STRING,
590 makefun(&req_headers_out, APL_REQ_FUNTYPE_TABLE, p));
591 apr_hash_set(dispatch, "err_headers_out", APR_HASH_KEY_STRING,
592 makefun(&req_err_headers_out, APL_REQ_FUNTYPE_TABLE, p));
593 apr_hash_set(dispatch, "notes", APR_HASH_KEY_STRING,
594 makefun(&req_notes, APL_REQ_FUNTYPE_TABLE, p));
595 apr_hash_set(dispatch, "subprocess_env", APR_HASH_KEY_STRING,
596 makefun(&req_subprocess_env, APL_REQ_FUNTYPE_TABLE, p));
599 lua_pushlightuserdata(L, dispatch);
600 lua_setfield(L, LUA_REGISTRYINDEX, "Apache2.Request.dispatch");
602 luaL_newmetatable(L, "Apache2.Request"); /* [metatable] */
603 lua_pushvalue(L, -1);
605 lua_setfield(L, -2, "__index");
606 luaL_register(L, NULL, request_methods); /* [metatable] */
610 luaL_newmetatable(L, "Apache2.Connection"); /* [metatable] */
611 lua_pushvalue(L, -1);
613 lua_setfield(L, -2, "__index");
614 luaL_register(L, NULL, connection_methods); /* [metatable] */
618 luaL_newmetatable(L, "Apache2.Server"); /* [metatable] */
619 lua_pushvalue(L, -1);
621 lua_setfield(L, -2, "__index");
622 luaL_register(L, NULL, server_methods); /* [metatable] */
628 AP_LUA_DECLARE(void) ap_lua_push_connection(lua_State *L, conn_rec *c)
630 lua_boxpointer(L, c);
631 luaL_getmetatable(L, "Apache2.Connection");
632 lua_setmetatable(L, -2);
633 luaL_getmetatable(L, "Apache2.Connection");
635 ap_lua_push_apr_table(L, c->notes);
636 lua_setfield(L, -2, "notes");
638 lua_pushstring(L, c->remote_ip);
639 lua_setfield(L, -2, "remote_ip");
645 AP_LUA_DECLARE(void) ap_lua_push_server(lua_State *L, server_rec *s)
647 lua_boxpointer(L, s);
648 luaL_getmetatable(L, "Apache2.Server");
649 lua_setmetatable(L, -2);
650 luaL_getmetatable(L, "Apache2.Server");
652 lua_pushstring(L, s->server_hostname);
653 lua_setfield(L, -2, "server_hostname");
658 AP_LUA_DECLARE(void) ap_lua_push_request(lua_State *L, request_rec *r)
660 lua_boxpointer(L, r);
661 luaL_getmetatable(L, "Apache2.Request");
662 lua_setmetatable(L, -2);