]> granicus.if.org Git - apache/blob - modules/lua/lua_request.c
cce3a9ce4f631823e080a59123b1b18787c98e45
[apache] / modules / lua / lua_request.c
1 /**
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
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  */
17
18 #include "mod_lua.h"
19 #include "util_script.h"
20 #include "lua_apr.h"
21
22 APLOG_USE_MODULE(lua);
23
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);
27
28 void ap_lua_rstack_dump(lua_State *L, request_rec *r, const char *msg)
29 {
30     int i;
31     int top = lua_gettop(L);
32
33     ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, APLOGNO(01484) "Lua Stack Dump: [%s]", msg);
34
35     for (i = 1; i <= top; i++) {
36         int t = lua_type(L, i);
37         switch (t) {
38         case LUA_TSTRING:{
39                 ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r,
40                               "%d:  '%s'", i, lua_tostring(L, i));
41                 break;
42             }
43         case LUA_TUSERDATA:{
44                 ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, "%d:  userdata",
45                               i);
46                 break;
47             }
48         case LUA_TLIGHTUSERDATA:{
49                 ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r,
50                               "%d:  lightuserdata", i);
51                 break;
52             }
53         case LUA_TNIL:{
54                 ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, "%d:  NIL", i);
55                 break;
56             }
57         case LUA_TNONE:{
58                 ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, "%d:  None", i);
59                 break;
60             }
61         case LUA_TBOOLEAN:{
62                 ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r,
63                               "%d:  %s", i, lua_toboolean(L,
64                                                           i) ? "true" :
65                               "false");
66                 break;
67             }
68         case LUA_TNUMBER:{
69                 ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r,
70                               "%d:  %g", i, lua_tonumber(L, i));
71                 break;
72             }
73         case LUA_TTABLE:{
74                 ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r,
75                               "%d:  <table>", i);
76                 break;
77             }
78         case LUA_TFUNCTION:{
79                 ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r,
80                               "%d:  <function>", i);
81                 break;
82             }
83         default:{
84                 ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r,
85                               "%d:  unkown: -[%s]-", i, lua_typename(L, i));
86                 break;
87             }
88         }
89     }
90 }
91
92 /**
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.
96  */
97 static request_rec *ap_lua_check_request_rec(lua_State *L, int index)
98 {
99     request_rec *r;
100     luaL_checkudata(L, index, "Apache2.Request");
101     r = (request_rec *) lua_unboxpointer(L, index);
102     return r;
103 }
104
105 /* ------------------ request methods -------------------- */
106 /* helper callback for req_parseargs */
107 static int req_aprtable2luatable_cb(void *l, const char *key,
108                                     const char *value)
109 {
110     int t;
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>] */
114     /* build complex */
115
116     lua_getfield(L, -1, key);   /* [VALUE, table<s,t>, table<s,s>] */
117     /* rstack_dump(L, RRR, "after getfield"); */
118     t = lua_type(L, -1);
119     switch (t) {
120     case LUA_TNIL:
121     case LUA_TNONE:{
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>] */
128             break;
129         }
130     case LUA_TTABLE:{
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>] */
137             break;
138         }
139     }
140
141     /* L is [table<s,t>, table<s,s>] */
142     /* build simple */
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>]  */
148     }
149     else {
150         lua_pop(L, 1);
151     }
152     return 1;
153 }
154
155 /* r:parseargs() returning a lua table */
156 static int req_parseargs(lua_State *L)
157 {
158     apr_table_t *form_table;
159     request_rec *r = ap_lua_check_request_rec(L, 1);
160     lua_newtable(L);
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>>] */
165 }
166
167 /* wrap ap_rputs as r:puts(String) */
168 static int req_puts(lua_State *L)
169 {
170     request_rec *r = ap_lua_check_request_rec(L, 1);
171
172     int argc = lua_gettop(L);
173     int i;
174
175     for (i = 2; i <= argc; i++) {
176         ap_rputs(luaL_checkstring(L, i), r);
177     }
178     return 0;
179 }
180
181 /* wrap ap_rwrite as r:write(String) */
182 static int req_write(lua_State *L)
183 {
184     request_rec *r = ap_lua_check_request_rec(L, 1);
185     size_t n;
186     const char *buf = luaL_checklstring(L, 2, &n);
187
188     ap_rwrite((void *) buf, n, r);
189     return 0;
190 }
191
192 /* r:addoutputfilter(name|function) */
193 static int req_add_output_filter(lua_State *L)
194 {
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, APLOGNO(01485) "adding output filter %s",
198                   name);
199     ap_add_output_filter(name, L, r, r->connection);
200     return 0;
201 }
202
203 /* wrap ap_construct_url as r:construct_url(String) */
204 static int req_construct_url(lua_State *L)
205 {
206     request_rec *r = ap_lua_check_request_rec(L, 1);
207     const char *name = luaL_checkstring(L, 2);
208     lua_pushstring(L, ap_construct_url(r->pool, name, r));
209     return 1;
210 }
211
212 /* wrap ap_escape_html r:escape_html(String) */
213 static int req_escape_html(lua_State *L)
214 {
215     request_rec *r = ap_lua_check_request_rec(L, 1);
216     const char *s = luaL_checkstring(L, 2);
217     lua_pushstring(L, ap_escape_html(r->pool, s));
218     return 1;
219 }
220 /* wrap optional ssl_var_lookup as  r:ssl_var_lookup(String) */
221 static int req_ssl_var_lookup(lua_State *L)
222 {
223     request_rec *r = ap_lua_check_request_rec(L, 1);
224     const char *s = luaL_checkstring(L, 2);
225     const char *res = ap_lua_ssl_val(r->pool, r->server, r->connection, r, 
226                                      (char *)s);
227     lua_pushstring(L, res);
228     return 1;
229 }
230 /* BEGIN dispatch mathods for request_rec fields */
231
232 /* not really a field, but we treat it like one */
233 static const char *req_document_root(request_rec *r)
234 {
235     return ap_document_root(r);
236 }
237
238 static char *req_uri_field(request_rec *r)
239 {
240     return r->uri;
241 }
242
243 static const char *req_method_field(request_rec *r)
244 {
245     return r->method;
246 }
247 static const char *req_handler_field(request_rec *r)
248 {
249     return r->handler;
250 }
251 static const char *req_proxyreq_field(request_rec *r)
252 {
253     switch (r->proxyreq) {
254         case PROXYREQ_NONE:     return "PROXYREQ_NONE";
255         case PROXYREQ_PROXY:    return "PROXYREQ_PROXY";
256         case PROXYREQ_REVERSE:  return "PROXYREQ_REVERSE";
257         case PROXYREQ_RESPONSE: return "PROXYREQ_RESPONSE";
258         default: return NULL;
259     }
260 }
261 static const char *req_hostname_field(request_rec *r)
262 {
263     return r->hostname;
264 }
265
266 static const char *req_args_field(request_rec *r)
267 {
268     return r->args;
269 }
270
271 static const char *req_path_info_field(request_rec *r)
272 {
273     return r->path_info;
274 }
275
276 static const char *req_canonical_filename_field(request_rec *r)
277 {
278     return r->canonical_filename;
279 }
280
281 static const char *req_filename_field(request_rec *r)
282 {
283     return r->filename;
284 }
285
286 static const char *req_user_field(request_rec *r)
287 {
288     return r->user;
289 }
290
291 static const char *req_unparsed_uri_field(request_rec *r)
292 {
293     return r->unparsed_uri;
294 }
295
296 static const char *req_ap_auth_type_field(request_rec *r)
297 {
298     return r->ap_auth_type;
299 }
300
301 static const char *req_content_encoding_field(request_rec *r)
302 {
303     return r->content_encoding;
304 }
305
306 static const char *req_content_type_field(request_rec *r)
307 {
308     return r->content_type;
309 }
310
311 static const char *req_range_field(request_rec *r)
312 {
313     return r->range;
314 }
315
316 static const char *req_protocol_field(request_rec *r)
317 {
318     return r->protocol;
319 }
320
321 static const char *req_the_request_field(request_rec *r)
322 {
323     return r->the_request;
324 }
325
326 static int req_status_field(request_rec *r)
327 {
328     return r->status;
329 }
330
331 static int req_assbackwards_field(request_rec *r)
332 {
333     return r->assbackwards;
334 }
335
336 static apr_table_t* req_headers_in(request_rec *r)
337 {
338     return r->headers_in;
339 }
340
341 static apr_table_t* req_headers_out(request_rec *r)
342 {
343     return r->headers_out;
344 }
345
346 static apr_table_t* req_err_headers_out(request_rec *r)
347 {
348   return r->err_headers_out;
349 }
350
351 static apr_table_t* req_subprocess_env(request_rec *r)
352 {
353   return r->subprocess_env;
354 }
355
356 static apr_table_t* req_notes(request_rec *r)
357 {
358   return r->notes;
359 }
360
361 static int req_ssl_is_https_field(request_rec *r)
362 {
363     return ap_lua_ssl_is_https(r->connection);
364 }
365
366 /* END dispatch mathods for request_rec fields */
367
368 static int req_dispatch(lua_State *L)
369 {
370     apr_hash_t *dispatch;
371     req_fun_t *rft;
372     request_rec *r = ap_lua_check_request_rec(L, 1);
373     const char *name = luaL_checkstring(L, 2);
374     lua_pop(L, 2);
375
376     lua_getfield(L, LUA_REGISTRYINDEX, "Apache2.Request.dispatch");
377     dispatch = lua_touserdata(L, 1);
378     lua_pop(L, 1);
379
380     rft = apr_hash_get(dispatch, name, APR_HASH_KEY_STRING);
381     if (rft) {
382         switch (rft->type) {
383         case APL_REQ_FUNTYPE_TABLE:{
384                 apr_table_t *rs;
385                 req_field_apr_table_f func = (req_field_apr_table_f)rft->fun;
386                 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(01486)
387                               "request_rec->dispatching %s -> apr table",
388                               name);
389                 rs = (*func)(r);
390                 ap_lua_push_apr_table(L, rs);
391                 return 1;
392             }
393
394         case APL_REQ_FUNTYPE_LUACFUN:{
395                 lua_CFunction func = (lua_CFunction)rft->fun;
396                 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(01487)
397                               "request_rec->dispatching %s -> lua_CFunction",
398                               name);
399                 lua_pushcfunction(L, func);
400                 return 1;
401             }
402         case APL_REQ_FUNTYPE_STRING:{
403                 req_field_string_f func = (req_field_string_f)rft->fun;
404                 char *rs;
405                 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(01488)
406                               "request_rec->dispatching %s -> string", name);
407                 rs = (*func) (r);
408                 lua_pushstring(L, rs);
409                 return 1;
410             }
411         case APL_REQ_FUNTYPE_INT:{
412                 req_field_int_f func = (req_field_int_f)rft->fun;
413                 int rs;
414                 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(01489)
415                               "request_rec->dispatching %s -> int", name);
416                 rs = (*func) (r);
417                 lua_pushnumber(L, rs);
418                 return 1;
419             }
420         case APL_REQ_FUNTYPE_BOOLEAN:{
421                 req_field_int_f func = (req_field_int_f)rft->fun;
422                 int rs;
423                 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(01490)
424                               "request_rec->dispatching %s -> boolean", name);
425                 rs = (*func) (r);
426                 lua_pushboolean(L, rs);
427                 return 1;
428             }
429         }
430     }
431
432     ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(01491) "nothing for %s", name);
433     return 0;
434 }
435
436 /* helper function for the logging functions below */
437 static int req_log_at(lua_State *L, int level)
438 {
439     const char *msg;
440     request_rec *r = ap_lua_check_request_rec(L, 1);
441     lua_Debug dbg;
442
443     lua_getstack(L, 1, &dbg);
444     lua_getinfo(L, "Sl", &dbg);
445
446     msg = luaL_checkstring(L, 2);
447     ap_log_rerror(dbg.source, dbg.currentline, APLOG_MODULE_INDEX, level, 0,
448                   r, "%s", msg);
449     return 0;
450 }
451
452 /* r:debug(String) and friends which use apache logging */
453 static int req_emerg(lua_State *L)
454 {
455     req_log_at(L, APLOG_EMERG);
456     return 0;
457 }
458 static int req_alert(lua_State *L)
459 {
460     req_log_at(L, APLOG_ALERT);
461     return 0;
462 }
463 static int req_crit(lua_State *L)
464 {
465     req_log_at(L, APLOG_CRIT);
466     return 0;
467 }
468 static int req_err(lua_State *L)
469 {
470     req_log_at(L, APLOG_ERR);
471     return 0;
472 }
473 static int req_warn(lua_State *L)
474 {
475     req_log_at(L, APLOG_WARNING);
476     return 0;
477 }
478 static int req_notice(lua_State *L)
479 {
480     req_log_at(L, APLOG_NOTICE);
481     return 0;
482 }
483 static int req_info(lua_State *L)
484 {
485     req_log_at(L, APLOG_INFO);
486     return 0;
487 }
488 static int req_debug(lua_State *L)
489 {
490     req_log_at(L, APLOG_DEBUG);
491     return 0;
492 }
493
494 #define APLUA_REQ_TRACE(lev) static int req_trace##lev(lua_State *L)  \
495 {                                                               \
496     req_log_at(L, APLOG_TRACE##lev);                            \
497     return 0;                                                   \
498 }
499
500 APLUA_REQ_TRACE(1)
501 APLUA_REQ_TRACE(2)
502 APLUA_REQ_TRACE(3)
503 APLUA_REQ_TRACE(4)
504 APLUA_REQ_TRACE(5)
505 APLUA_REQ_TRACE(6)
506 APLUA_REQ_TRACE(7)
507 APLUA_REQ_TRACE(8)
508
509 /* handle r.status = 201 */
510 static int req_newindex(lua_State *L)
511 {
512     const char *key;
513     /* request_rec* r = lua_touserdata(L, lua_upvalueindex(1)); */
514     /* const char* key = luaL_checkstring(L, -2); */
515     request_rec *r = ap_lua_check_request_rec(L, 1);
516     key = luaL_checkstring(L, 2);
517
518     if (0 == strcmp("args", key)) {
519         const char *value = luaL_checkstring(L, 3);
520         r->args = apr_pstrdup(r->pool, value);
521         return 0;
522     }
523
524     if (0 == strcmp("content_type", key)) {
525         const char *value = luaL_checkstring(L, 3);
526         ap_set_content_type(r, apr_pstrdup(r->pool, value));
527         return 0;
528     }
529
530     if (0 == strcmp("filename", key)) {
531         const char *value = luaL_checkstring(L, 3);
532         r->filename = apr_pstrdup(r->pool, value);
533         return 0;
534     }
535
536     if (0 == strcmp("handler", key)) {
537         const char *value = luaL_checkstring(L, 3);
538         r->handler = apr_pstrdup(r->pool, value);
539         return 0;
540     }
541
542     if (0 == strcmp("proxyreq", key)) {
543         int value = luaL_checkinteger(L, 3);
544         r->proxyreq = value;
545         return 0;
546     }
547
548     if (0 == strcmp("status", key)) {
549         int code = luaL_checkinteger(L, 3);
550         r->status = code;
551         return 0;
552     }
553
554     if (0 == strcmp("uri", key)) {
555         const char *value = luaL_checkstring(L, 3);
556         r->uri = apr_pstrdup(r->pool, value);
557         return 0;
558     }
559
560     if (0 == strcmp("user", key)) {
561         const char *value = luaL_checkstring(L, 3);
562         r->user = apr_pstrdup(r->pool, value);
563         return 0;
564     }
565
566     lua_pushstring(L,
567                    apr_psprintf(r->pool,
568                                 "Property [%s] may not be set on a request_rec",
569                                 key));
570     lua_error(L);
571     return 0;
572 }
573
574 static const struct luaL_Reg request_methods[] = {
575     {"__index", req_dispatch},
576     {"__newindex", req_newindex},
577     /*   {"__newindex", req_set_field}, */
578     {NULL, NULL}
579 };
580
581
582 static const struct luaL_Reg connection_methods[] = {
583     {NULL, NULL}
584 };
585
586
587 static const struct luaL_Reg server_methods[] = {
588     {NULL, NULL}
589 };
590
591
592 static req_fun_t *makefun(const void *fun, int type, apr_pool_t *pool)
593 {
594     req_fun_t *rft = apr_palloc(pool, sizeof(req_fun_t));
595     rft->fun = fun;
596     rft->type = type;
597     return rft;
598 }
599
600 AP_LUA_DECLARE(void) ap_lua_load_request_lmodule(lua_State *L, apr_pool_t *p)
601 {
602
603     apr_hash_t *dispatch = apr_hash_make(p);
604
605     apr_hash_set(dispatch, "puts", APR_HASH_KEY_STRING,
606                  makefun(&req_puts, APL_REQ_FUNTYPE_LUACFUN, p));
607     apr_hash_set(dispatch, "write", APR_HASH_KEY_STRING,
608                  makefun(&req_write, APL_REQ_FUNTYPE_LUACFUN, p));
609     apr_hash_set(dispatch, "document_root", APR_HASH_KEY_STRING,
610                  makefun(&req_document_root, APL_REQ_FUNTYPE_STRING, p));
611     apr_hash_set(dispatch, "parseargs", APR_HASH_KEY_STRING,
612                  makefun(&req_parseargs, APL_REQ_FUNTYPE_LUACFUN, p));
613     apr_hash_set(dispatch, "debug", APR_HASH_KEY_STRING,
614                  makefun(&req_debug, APL_REQ_FUNTYPE_LUACFUN, p));
615     apr_hash_set(dispatch, "info", APR_HASH_KEY_STRING,
616                  makefun(&req_info, APL_REQ_FUNTYPE_LUACFUN, p));
617     apr_hash_set(dispatch, "notice", APR_HASH_KEY_STRING,
618                  makefun(&req_notice, APL_REQ_FUNTYPE_LUACFUN, p));
619     apr_hash_set(dispatch, "warn", APR_HASH_KEY_STRING,
620                  makefun(&req_warn, APL_REQ_FUNTYPE_LUACFUN, p));
621     apr_hash_set(dispatch, "err", APR_HASH_KEY_STRING,
622                  makefun(&req_err, APL_REQ_FUNTYPE_LUACFUN, p));
623     apr_hash_set(dispatch, "crit", APR_HASH_KEY_STRING,
624                  makefun(&req_crit, APL_REQ_FUNTYPE_LUACFUN, p));
625     apr_hash_set(dispatch, "alert", APR_HASH_KEY_STRING,
626                  makefun(&req_alert, APL_REQ_FUNTYPE_LUACFUN, p));
627     apr_hash_set(dispatch, "emerg", APR_HASH_KEY_STRING,
628                  makefun(&req_emerg, APL_REQ_FUNTYPE_LUACFUN, p));
629     apr_hash_set(dispatch, "trace1", APR_HASH_KEY_STRING,
630                  makefun(&req_trace1, APL_REQ_FUNTYPE_LUACFUN, p));
631     apr_hash_set(dispatch, "trace2", APR_HASH_KEY_STRING,
632                  makefun(&req_trace2, APL_REQ_FUNTYPE_LUACFUN, p));
633     apr_hash_set(dispatch, "trace3", APR_HASH_KEY_STRING,
634                  makefun(&req_trace3, APL_REQ_FUNTYPE_LUACFUN, p));
635     apr_hash_set(dispatch, "trace4", APR_HASH_KEY_STRING,
636                  makefun(&req_trace4, APL_REQ_FUNTYPE_LUACFUN, p));
637     apr_hash_set(dispatch, "trace5", APR_HASH_KEY_STRING,
638                  makefun(&req_trace5, APL_REQ_FUNTYPE_LUACFUN, p));
639     apr_hash_set(dispatch, "trace6", APR_HASH_KEY_STRING,
640                  makefun(&req_trace6, APL_REQ_FUNTYPE_LUACFUN, p));
641     apr_hash_set(dispatch, "trace7", APR_HASH_KEY_STRING,
642                  makefun(&req_trace7, APL_REQ_FUNTYPE_LUACFUN, p));
643     apr_hash_set(dispatch, "trace8", APR_HASH_KEY_STRING,
644                  makefun(&req_trace8, APL_REQ_FUNTYPE_LUACFUN, p));
645     apr_hash_set(dispatch, "add_output_filter", APR_HASH_KEY_STRING,
646                  makefun(&req_add_output_filter, APL_REQ_FUNTYPE_LUACFUN, p));
647     apr_hash_set(dispatch, "construct_url", APR_HASH_KEY_STRING,
648                  makefun(&req_construct_url, APL_REQ_FUNTYPE_LUACFUN, p));
649     apr_hash_set(dispatch, "escape_html", APR_HASH_KEY_STRING,
650                  makefun(&req_escape_html, APL_REQ_FUNTYPE_LUACFUN, p));
651     apr_hash_set(dispatch, "ssl_var_lookup", APR_HASH_KEY_STRING,
652                  makefun(&req_ssl_var_lookup, APL_REQ_FUNTYPE_LUACFUN, p));
653     apr_hash_set(dispatch, "is_https", APR_HASH_KEY_STRING,
654                  makefun(&req_ssl_is_https_field, APL_REQ_FUNTYPE_BOOLEAN, p));
655     apr_hash_set(dispatch, "assbackwards", APR_HASH_KEY_STRING,
656                  makefun(&req_assbackwards_field, APL_REQ_FUNTYPE_BOOLEAN, p));
657     apr_hash_set(dispatch, "status", APR_HASH_KEY_STRING,
658                  makefun(&req_status_field, APL_REQ_FUNTYPE_INT, p));
659     apr_hash_set(dispatch, "protocol", APR_HASH_KEY_STRING,
660                  makefun(&req_protocol_field, APL_REQ_FUNTYPE_STRING, p));
661     apr_hash_set(dispatch, "range", APR_HASH_KEY_STRING,
662                  makefun(&req_range_field, APL_REQ_FUNTYPE_STRING, p));
663     apr_hash_set(dispatch, "content_type", APR_HASH_KEY_STRING,
664                  makefun(&req_content_type_field, APL_REQ_FUNTYPE_STRING, p));
665     apr_hash_set(dispatch, "content_encoding", APR_HASH_KEY_STRING,
666                  makefun(&req_content_encoding_field, APL_REQ_FUNTYPE_STRING,
667                          p));
668     apr_hash_set(dispatch, "ap_auth_type", APR_HASH_KEY_STRING,
669                  makefun(&req_ap_auth_type_field, APL_REQ_FUNTYPE_STRING, p));
670     apr_hash_set(dispatch, "unparsed_uri", APR_HASH_KEY_STRING,
671                  makefun(&req_unparsed_uri_field, APL_REQ_FUNTYPE_STRING, p));
672     apr_hash_set(dispatch, "user", APR_HASH_KEY_STRING,
673                  makefun(&req_user_field, APL_REQ_FUNTYPE_STRING, p));
674     apr_hash_set(dispatch, "filename", APR_HASH_KEY_STRING,
675                  makefun(&req_filename_field, APL_REQ_FUNTYPE_STRING, p));
676     apr_hash_set(dispatch, "canonical_filename", APR_HASH_KEY_STRING,
677                  makefun(&req_canonical_filename_field,
678                          APL_REQ_FUNTYPE_STRING, p));
679     apr_hash_set(dispatch, "path_info", APR_HASH_KEY_STRING,
680                  makefun(&req_path_info_field, APL_REQ_FUNTYPE_STRING, p));
681     apr_hash_set(dispatch, "args", APR_HASH_KEY_STRING,
682                  makefun(&req_args_field, APL_REQ_FUNTYPE_STRING, p));
683     apr_hash_set(dispatch, "handler", APR_HASH_KEY_STRING,
684                  makefun(&req_handler_field, APL_REQ_FUNTYPE_STRING, p));
685     apr_hash_set(dispatch, "hostname", APR_HASH_KEY_STRING,
686                  makefun(&req_hostname_field, APL_REQ_FUNTYPE_STRING, p));
687     apr_hash_set(dispatch, "uri", APR_HASH_KEY_STRING,
688                  makefun(&req_uri_field, APL_REQ_FUNTYPE_STRING, p));
689     apr_hash_set(dispatch, "the_request", APR_HASH_KEY_STRING,
690                  makefun(&req_the_request_field, APL_REQ_FUNTYPE_STRING, p));
691     apr_hash_set(dispatch, "method", APR_HASH_KEY_STRING,
692                  makefun(&req_method_field, APL_REQ_FUNTYPE_STRING, p));
693     apr_hash_set(dispatch, "proxyreq", APR_HASH_KEY_STRING,
694                  makefun(&req_proxyreq_field, APL_REQ_FUNTYPE_STRING, p));
695     apr_hash_set(dispatch, "headers_in", APR_HASH_KEY_STRING,
696                  makefun(&req_headers_in, APL_REQ_FUNTYPE_TABLE, p));
697     apr_hash_set(dispatch, "headers_out", APR_HASH_KEY_STRING,
698                  makefun(&req_headers_out, APL_REQ_FUNTYPE_TABLE, p));
699     apr_hash_set(dispatch, "err_headers_out", APR_HASH_KEY_STRING,
700                  makefun(&req_err_headers_out, APL_REQ_FUNTYPE_TABLE, p));
701     apr_hash_set(dispatch, "notes", APR_HASH_KEY_STRING,
702                  makefun(&req_notes, APL_REQ_FUNTYPE_TABLE, p));
703     apr_hash_set(dispatch, "subprocess_env", APR_HASH_KEY_STRING,
704                  makefun(&req_subprocess_env, APL_REQ_FUNTYPE_TABLE, p));
705
706
707     lua_pushlightuserdata(L, dispatch);
708     lua_setfield(L, LUA_REGISTRYINDEX, "Apache2.Request.dispatch");
709
710     luaL_newmetatable(L, "Apache2.Request");    /* [metatable] */
711     lua_pushvalue(L, -1);
712
713     lua_setfield(L, -2, "__index");
714     luaL_register(L, NULL, request_methods);    /* [metatable] */
715
716     lua_pop(L, 2);
717
718     luaL_newmetatable(L, "Apache2.Connection"); /* [metatable] */
719     lua_pushvalue(L, -1);
720
721     lua_setfield(L, -2, "__index");
722     luaL_register(L, NULL, connection_methods); /* [metatable] */
723
724     lua_pop(L, 2);
725
726     luaL_newmetatable(L, "Apache2.Server");     /* [metatable] */
727     lua_pushvalue(L, -1);
728
729     lua_setfield(L, -2, "__index");
730     luaL_register(L, NULL, server_methods);     /* [metatable] */
731
732     lua_pop(L, 2);
733
734 }
735
736 AP_LUA_DECLARE(void) ap_lua_push_connection(lua_State *L, conn_rec *c)
737 {
738     lua_boxpointer(L, c);
739     luaL_getmetatable(L, "Apache2.Connection");
740     lua_setmetatable(L, -2);
741     luaL_getmetatable(L, "Apache2.Connection");
742
743     ap_lua_push_apr_table(L, c->notes);
744     lua_setfield(L, -2, "notes");
745
746     lua_pushstring(L, c->peer_ip);
747     lua_setfield(L, -2, "remote_ip");
748
749     lua_pop(L, 1);
750 }
751
752
753 AP_LUA_DECLARE(void) ap_lua_push_server(lua_State *L, server_rec *s)
754 {
755     lua_boxpointer(L, s);
756     luaL_getmetatable(L, "Apache2.Server");
757     lua_setmetatable(L, -2);
758     luaL_getmetatable(L, "Apache2.Server");
759
760     lua_pushstring(L, s->server_hostname);
761     lua_setfield(L, -2, "server_hostname");
762
763     lua_pop(L, 1);
764 }
765
766 AP_LUA_DECLARE(void) ap_lua_push_request(lua_State *L, request_rec *r)
767 {
768     lua_boxpointer(L, r);
769     luaL_getmetatable(L, "Apache2.Request");
770     lua_setmetatable(L, -2);
771 }