]> granicus.if.org Git - apache/blob - modules/lua/lua_request.c
Cleanup effort in prep for GA push:
[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, "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, "adding output filter %s",
198                   name);
199     ap_add_output_filter(name, L, r, r->connection);
200     return 0;
201 }
202
203 /* BEGIN dispatch mathods for request_rec fields */
204
205 /* not really a field, but we treat it like one */
206 static const char *req_document_root(request_rec *r)
207 {
208     return ap_document_root(r);
209 }
210
211 static char *req_uri_field(request_rec *r)
212 {
213     return r->uri;
214 }
215
216 static const char *req_method_field(request_rec *r)
217 {
218     return r->method;
219 }
220
221 static const char *req_hostname_field(request_rec *r)
222 {
223     return r->hostname;
224 }
225
226 static const char *req_args_field(request_rec *r)
227 {
228     return r->args;
229 }
230
231 static const char *req_path_info_field(request_rec *r)
232 {
233     return r->path_info;
234 }
235
236 static const char *req_canonical_filename_field(request_rec *r)
237 {
238     return r->canonical_filename;
239 }
240
241 static const char *req_filename_field(request_rec *r)
242 {
243     return r->filename;
244 }
245
246 static const char *req_user_field(request_rec *r)
247 {
248     return r->user;
249 }
250
251 static const char *req_unparsed_uri_field(request_rec *r)
252 {
253     return r->unparsed_uri;
254 }
255
256 static const char *req_ap_auth_type_field(request_rec *r)
257 {
258     return r->ap_auth_type;
259 }
260
261 static const char *req_content_encoding_field(request_rec *r)
262 {
263     return r->content_encoding;
264 }
265
266 static const char *req_content_type_field(request_rec *r)
267 {
268     return r->content_type;
269 }
270
271 static const char *req_range_field(request_rec *r)
272 {
273     return r->range;
274 }
275
276 static const char *req_protocol_field(request_rec *r)
277 {
278     return r->protocol;
279 }
280
281 static const char *req_the_request_field(request_rec *r)
282 {
283     return r->the_request;
284 }
285
286 static int req_status_field(request_rec *r)
287 {
288     return r->status;
289 }
290
291 static int req_assbackwards_field(request_rec *r)
292 {
293     return r->assbackwards;
294 }
295
296 static apr_table_t* req_headers_in(request_rec *r)
297 {
298     return r->headers_in;
299 }
300
301 static apr_table_t* req_headers_out(request_rec *r)
302 {
303     return r->headers_out;
304 }
305
306 static apr_table_t* req_err_headers_out(request_rec *r)
307 {
308   return r->err_headers_out;
309 }
310
311 static apr_table_t* req_subprocess_env(request_rec *r)
312 {
313   return r->subprocess_env;
314 }
315
316 static apr_table_t* req_notes(request_rec *r)
317 {
318   return r->notes;
319 }
320
321 /* END dispatch mathods for request_rec fields */
322
323 static int req_dispatch(lua_State *L)
324 {
325     apr_hash_t *dispatch;
326     req_fun_t *rft;
327     request_rec *r = ap_lua_check_request_rec(L, 1);
328     const char *name = luaL_checkstring(L, 2);
329     lua_pop(L, 2);
330
331     lua_getfield(L, LUA_REGISTRYINDEX, "Apache2.Request.dispatch");
332     dispatch = lua_touserdata(L, 1);
333     lua_pop(L, 1);
334
335     rft = apr_hash_get(dispatch, name, APR_HASH_KEY_STRING);
336     if (rft) {
337         switch (rft->type) {
338         case APL_REQ_FUNTYPE_TABLE:{
339                 apr_table_t *rs;
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",
343                               name);
344                 rs = (*func)(r);
345                 ap_lua_push_apr_table(L, rs);
346                 return 1;
347             }
348
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",
353                               name);
354                 lua_pushcfunction(L, func);
355                 return 1;
356             }
357         case APL_REQ_FUNTYPE_STRING:{
358                 req_field_string_f func = (req_field_string_f)rft->fun;
359                 char *rs;
360                 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
361                               "request_rec->dispatching %s -> string", name);
362                 rs = (*func) (r);
363                 lua_pushstring(L, rs);
364                 return 1;
365             }
366         case APL_REQ_FUNTYPE_INT:{
367                 req_field_int_f func = (req_field_int_f)rft->fun;
368                 int rs;
369                 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
370                               "request_rec->dispatching %s -> int", name);
371                 rs = (*func) (r);
372                 lua_pushnumber(L, rs);
373                 return 1;
374             }
375         case APL_REQ_FUNTYPE_BOOLEAN:{
376                 req_field_int_f func = (req_field_int_f)rft->fun;
377                 int rs;
378                 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
379                               "request_rec->dispatching %s -> boolean", name);
380                 rs = (*func) (r);
381                 lua_pushboolean(L, rs);
382                 return 1;
383             }
384         }
385     }
386
387     ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, "nothing for %s", name);
388     return 0;
389 }
390
391 /* helper function for the logging functions below */
392 static int req_log_at(lua_State *L, int level)
393 {
394     const char *msg;
395     request_rec *r = ap_lua_check_request_rec(L, 1);
396     lua_Debug dbg;
397
398     lua_getstack(L, 1, &dbg);
399     lua_getinfo(L, "Sl", &dbg);
400
401     msg = luaL_checkstring(L, 2);
402     ap_log_rerror(dbg.source, dbg.currentline, APLOG_MODULE_INDEX, level, 0, r, msg);
403     return 0;
404 }
405
406 /* r:debug(String) and friends which use apache logging */
407 static int req_emerg(lua_State *L)
408 {
409     req_log_at(L, APLOG_EMERG);
410     return 0;
411 }
412 static int req_alert(lua_State *L)
413 {
414     req_log_at(L, APLOG_ALERT);
415     return 0;
416 }
417 static int req_crit(lua_State *L)
418 {
419     req_log_at(L, APLOG_CRIT);
420     return 0;
421 }
422 static int req_err(lua_State *L)
423 {
424     req_log_at(L, APLOG_ERR);
425     return 0;
426 }
427 static int req_warn(lua_State *L)
428 {
429     req_log_at(L, APLOG_WARNING);
430     return 0;
431 }
432 static int req_notice(lua_State *L)
433 {
434     req_log_at(L, APLOG_NOTICE);
435     return 0;
436 }
437 static int req_info(lua_State *L)
438 {
439     req_log_at(L, APLOG_INFO);
440     return 0;
441 }
442 static int req_debug(lua_State *L)
443 {
444     req_log_at(L, APLOG_DEBUG);
445     return 0;
446 }
447
448 /* handle r.status = 201 */
449 static int req_newindex(lua_State *L)
450 {
451     const char *key;
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);
458         r->status = code;
459         return 0;
460     }
461
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));
465         return 0;
466     }
467
468     if (0 == strcmp("filename", key)) {
469         const char *value = luaL_checkstring(L, 3);
470         r->filename = apr_pstrdup(r->pool, value);
471         return 0;
472     }
473
474     if (0 == strcmp("uri", key)) {
475         const char *value = luaL_checkstring(L, 3);
476         r->uri = apr_pstrdup(r->pool, value);
477         return 0;
478     }
479
480     if (0 == apr_strnatcmp("user", key)) {
481         const char *value = luaL_checkstring(L, 3);
482         r->user = apr_pstrdup(r->pool, value);
483         return 0;
484     }
485
486     lua_pushstring(L,
487                    apr_psprintf(r->pool,
488                                 "Property [%s] may not be set on a request_rec",
489                                 key));
490     lua_error(L);
491     return 0;
492 }
493
494 static const struct luaL_Reg request_methods[] = {
495     {"__index", req_dispatch},
496     {"__newindex", req_newindex},
497     /*   {"__newindex", req_set_field}, */
498     {NULL, NULL}
499 };
500
501
502 static const struct luaL_Reg connection_methods[] = {
503     {NULL, NULL}
504 };
505
506
507 static const struct luaL_Reg server_methods[] = {
508     {NULL, NULL}
509 };
510
511
512 static req_fun_t *makefun(const void *fun, int type, apr_pool_t *pool)
513 {
514     req_fun_t *rft = apr_palloc(pool, sizeof(req_fun_t));
515     rft->fun = fun;
516     rft->type = type;
517     return rft;
518 }
519
520 AP_LUA_DECLARE(void) ap_lua_load_request_lmodule(lua_State *L, apr_pool_t *p)
521 {
522
523     apr_hash_t *dispatch = apr_hash_make(p);
524
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,
563                          p));
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));
597
598
599     lua_pushlightuserdata(L, dispatch);
600     lua_setfield(L, LUA_REGISTRYINDEX, "Apache2.Request.dispatch");
601
602     luaL_newmetatable(L, "Apache2.Request");    /* [metatable] */
603     lua_pushvalue(L, -1);
604
605     lua_setfield(L, -2, "__index");
606     luaL_register(L, NULL, request_methods);    /* [metatable] */
607
608     lua_pop(L, 2);
609
610     luaL_newmetatable(L, "Apache2.Connection"); /* [metatable] */
611     lua_pushvalue(L, -1);
612
613     lua_setfield(L, -2, "__index");
614     luaL_register(L, NULL, connection_methods); /* [metatable] */
615
616     lua_pop(L, 2);
617
618     luaL_newmetatable(L, "Apache2.Server");     /* [metatable] */
619     lua_pushvalue(L, -1);
620
621     lua_setfield(L, -2, "__index");
622     luaL_register(L, NULL, server_methods);     /* [metatable] */
623
624     lua_pop(L, 2);
625
626 }
627
628 AP_LUA_DECLARE(void) ap_lua_push_connection(lua_State *L, conn_rec *c)
629 {
630     lua_boxpointer(L, c);
631     luaL_getmetatable(L, "Apache2.Connection");
632     lua_setmetatable(L, -2);
633     luaL_getmetatable(L, "Apache2.Connection");
634
635     ap_lua_push_apr_table(L, c->notes);
636     lua_setfield(L, -2, "notes");
637
638     lua_pushstring(L, c->remote_ip);
639     lua_setfield(L, -2, "remote_ip");
640
641     lua_pop(L, 1);
642 }
643
644
645 AP_LUA_DECLARE(void) ap_lua_push_server(lua_State *L, server_rec *s)
646 {
647     lua_boxpointer(L, s);
648     luaL_getmetatable(L, "Apache2.Server");
649     lua_setmetatable(L, -2);
650     luaL_getmetatable(L, "Apache2.Server");
651
652     lua_pushstring(L, s->server_hostname);
653     lua_setfield(L, -2, "server_hostname");
654
655     lua_pop(L, 1);
656 }
657
658 AP_LUA_DECLARE(void) ap_lua_push_request(lua_State *L, request_rec *r)
659 {
660     lua_boxpointer(L, r);
661     luaL_getmetatable(L, "Apache2.Request");
662     lua_setmetatable(L, -2);
663 }