Annotation of parser3/src/targets/apache13/modules/extra/mod_parser3.C, revision 1.4
1.2 paf 1: /** @file
2: Parser: apache module.
3:
4: Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com)
5:
6: Author: Alexander Petrosyan <paf@design.ru> (http://design.ru/paf)
7:
1.4 ! paf 8: $Id: mod_parser3.C,v 1.3 2001/03/21 14:24:54 paf Exp $
1.2 paf 9: */
10:
11: #include "httpd.h"
12: #include "http_config.h"
13: #include "http_core.h"
14: #include "http_log.h"
15: #include "http_main.h"
16: #include "http_protocol.h"
17: #include "util_script.h"
18:
19: #include <stdio.h>
20:
21:
22: #include "pa_common.h"
23: #include "pa_globals.h"
24: #include "pa_request.h"
25:
26: /*--------------------------------------------------------------------------*/
27: /* */
28: /* Data declarations. */
29: /* */
30: /* Here are the static cells and structure declarations private to our */
31: /* module. */
32: /* */
33: /*--------------------------------------------------------------------------*/
34:
35: /*
36: * Sample configuration record. Used for both per-directory and per-server
37: * configuration data.
38: *
39: * It's perfectly reasonable to have two different structures for the two
40: * different environments. The same command handlers will be called for
41: * both, though, so the handlers need to be able to tell them apart. One
42: * possibility is for both structures to start with an int which is zero for
43: * one and 1 for the other.
44: *
45: * Note that while the per-directory and per-server configuration records are
46: * available to most of the module handlers, they should be treated as
47: * READ-ONLY by all except the command and merge handlers. Sometimes handlers
48: * are handed a record that applies to the current location by implication or
49: * inheritance, and modifying it will change the rules for other locations.
50: */
51: typedef struct excfg {
52: int cmode; /* Environment to which record applies (directory,
53: * server, or combination).
54: */
55: #define CONFIG_MODE_SERVER 1
56: #define CONFIG_MODE_DIRECTORY 2
57: #define CONFIG_MODE_COMBO 3 /* Shouldn't ever happen. */
58: int local; /* Boolean: "Example" directive declared here? */
59: int congenital; /* Boolean: did we inherit an "Example"? */
60: char *trace; /* Pointer to trace string. */
61: char *loc; /* Location to which this record applies. */
62: } excfg;
63:
64: /*
65: * Let's set up a module-local static cell to point to the accreting callback
66: * trace. As each API callback is made to us, we'll tack on the particulars
67: * to whatever we've already recorded. To avoid massive memory bloat as
68: * directories are walked again and again, we record the routine/environment
69: * the first time (non-request context only), and ignore subsequent calls for
70: * the same routine/environment.
71: */
72: static const char *trace = NULL;
73: static table *static_calls_made = NULL;
74:
75: /*
76: * To avoid leaking memory from pools other than the per-request one, we
77: * allocate a module-private pool, and then use a sub-pool of that which gets
78: * freed each time we modify the trace. That way previous layers of trace
79: * data don't get lost.
80: */
81: static pool *example_pool = NULL;
82: static pool *example_subpool = NULL;
83:
84: /*
85: * Declare ourselves so the configuration routines can find and know us.
86: * We'll fill it in at the end of the module.
87: */
88: extern "C" module MODULE_VAR_EXPORT parser3_module;
89:
90: /*--------------------------------------------------------------------------*/
91: /* */
92: /* The following pseudo-prototype declarations illustrate the parameters */
93: /* passed to command handlers for the different types of directive */
94: /* syntax. If an argument was specified in the directive definition */
95: /* (look for "command_rec" below), it's available to the command handler */
96: /* via the (void *) info field in the cmd_parms argument passed to the */
97: /* handler (cmd->info for the examples below). */
98: /* */
99: /*--------------------------------------------------------------------------*/
100:
101: /*
102: * Command handler for a NO_ARGS directive.
103: *
104: * static const char *handle_NO_ARGS(cmd_parms *cmd, void *mconfig);
105: */
106:
107: /*
108: * Command handler for a RAW_ARGS directive. The "args" argument is the text
109: * of the commandline following the directive itself.
110: *
111: * static const char *handle_RAW_ARGS(cmd_parms *cmd, void *mconfig,
112: * const char *args);
113: */
114:
115: /*
116: * Command handler for a FLAG directive. The single parameter is passed in
117: * "bool", which is either zero or not for Off or On respectively.
118: *
119: * static const char *handle_FLAG(cmd_parms *cmd, void *mconfig, int bool);
120: */
121:
122: /*
123: * Command handler for a TAKE1 directive. The single parameter is passed in
124: * "word1".
125: *
126: * static const char *handle_TAKE1(cmd_parms *cmd, void *mconfig,
127: * char *word1);
128: */
129:
130: /*
131: * Command handler for a TAKE2 directive. TAKE2 commands must always have
132: * exactly two arguments.
133: *
134: * static const char *handle_TAKE2(cmd_parms *cmd, void *mconfig,
135: * char *word1, char *word2);
136: */
137:
138: /*
139: * Command handler for a TAKE3 directive. Like TAKE2, these must have exactly
140: * three arguments, or the parser complains and doesn't bother calling us.
141: *
142: * static const char *handle_TAKE3(cmd_parms *cmd, void *mconfig,
143: * char *word1, char *word2, char *word3);
144: */
145:
146: /*
147: * Command handler for a TAKE12 directive. These can take either one or two
148: * arguments.
149: * - word2 is a NULL pointer if no second argument was specified.
150: *
151: * static const char *handle_TAKE12(cmd_parms *cmd, void *mconfig,
152: * char *word1, char *word2);
153: */
154:
155: /*
156: * Command handler for a TAKE123 directive. A TAKE123 directive can be given,
157: * as might be expected, one, two, or three arguments.
158: * - word2 is a NULL pointer if no second argument was specified.
159: * - word3 is a NULL pointer if no third argument was specified.
160: *
161: * static const char *handle_TAKE123(cmd_parms *cmd, void *mconfig,
162: * char *word1, char *word2, char *word3);
163: */
164:
165: /*
166: * Command handler for a TAKE13 directive. Either one or three arguments are
167: * permitted - no two-parameters-only syntax is allowed.
168: * - word2 and word3 are NULL pointers if only one argument was specified.
169: *
170: * static const char *handle_TAKE13(cmd_parms *cmd, void *mconfig,
171: * char *word1, char *word2, char *word3);
172: */
173:
174: /*
175: * Command handler for a TAKE23 directive. At least two and as many as three
176: * arguments must be specified.
177: * - word3 is a NULL pointer if no third argument was specified.
178: *
179: * static const char *handle_TAKE23(cmd_parms *cmd, void *mconfig,
180: * char *word1, char *word2, char *word3);
181: */
182:
183: /*
184: * Command handler for a ITERATE directive.
185: * - Handler is called once for each of n arguments given to the directive.
186: * - word1 points to each argument in turn.
187: *
188: * static const char *handle_ITERATE(cmd_parms *cmd, void *mconfig,
189: * char *word1);
190: */
191:
192: /*
193: * Command handler for a ITERATE2 directive.
194: * - Handler is called once for each of the second and subsequent arguments
195: * given to the directive.
196: * - word1 is the same for each call for a particular directive instance (the
197: * first argument).
198: * - word2 points to each of the second and subsequent arguments in turn.
199: *
200: * static const char *handle_ITERATE2(cmd_parms *cmd, void *mconfig,
201: * char *word1, char *word2);
202: */
203:
204: /*--------------------------------------------------------------------------*/
205: /* */
206: /* These routines are strictly internal to this module, and support its */
207: /* operation. They are not referenced by any external portion of the */
208: /* server. */
209: /* */
210: /*--------------------------------------------------------------------------*/
211:
212: /*
213: * Locate our directory configuration record for the current request.
214: */
215: static excfg *our_dconfig(request_rec *r)
216: {
217:
218: return (excfg *) ap_get_module_config(r->per_dir_config, &parser3_module);
219: }
220:
221: #if 0
222: /*
223: * Locate our server configuration record for the specified server.
224: */
225: static excfg *our_sconfig(server_rec *s)
226: {
227:
228: return (excfg *) ap_get_module_config(s->module_config, &parser3_module);
229: }
230:
231: /*
232: * Likewise for our configuration record for the specified request.
233: */
234: static excfg *our_rconfig(request_rec *r)
235: {
236:
237: return (excfg *) ap_get_module_config(r->request_config, &parser3_module);
238: }
239: #endif
240:
241: /*
242: * This routine sets up some module-wide cells if they haven't been already.
243: */
244: static void setup_module_cells()
245: {
246: /*
247: * If we haven't already allocated our module-private pool, do so now.
248: */
249: if (example_pool == NULL) {
250: example_pool = ap_make_sub_pool(NULL);
251: };
252: /*
253: * Likewise for the table of routine/environment pairs we visit outside of
254: * request context.
255: */
256: if (static_calls_made == NULL) {
257: static_calls_made = ap_make_table(example_pool, 16);
258: };
259: }
260:
261: /*
262: * This routine is used to add a trace of a callback to the list. We're
263: * passed the server record (if available), the request record (if available),
264: * a pointer to our private configuration record (if available) for the
265: * environment to which the callback is supposed to apply, and some text. We
266: * turn this into a textual representation and add it to the tail of the list.
267: * The list can be displayed by the parser3_handler() routine.
268: *
269: * If the call occurs within a request context (i.e., we're passed a request
270: * record), we put the trace into the request pool and attach it to the
271: * request via the notes mechanism. Otherwise, the trace gets added
272: * to the static (non-request-specific) list.
273: *
274: * Note that the r->notes table is only for storing strings; if you need to
275: * maintain per-request data of any other type, you need to use another
276: * mechanism.
277: */
278:
279: #define TRACE_NOTE "example-trace"
280:
281: static void trace_add(server_rec *s, request_rec *r, excfg *mconfig,
282: const char *note)
283: {
284:
285: const char *sofar;
286: char *addon;
287: char *where;
288: pool *p;
289: const char *trace_copy;
290:
291: /*
292: * Make sure our pools and tables are set up - we need 'em.
293: */
294: setup_module_cells();
295: /*
296: * Now, if we're in request-context, we use the request pool.
297: */
298: if (r != NULL) {
299: p = r->pool;
300: if ((trace_copy = ap_table_get(r->notes, TRACE_NOTE)) == NULL) {
301: trace_copy = "";
302: }
303: }
304: else {
305: /*
306: * We're not in request context, so the trace gets attached to our
307: * module-wide pool. We do the create/destroy every time we're called
308: * in non-request context; this avoids leaking memory in some of
309: * the subsequent calls that allocate memory only once (such as the
310: * key formation below).
311: *
312: * Make a new sub-pool and copy any existing trace to it. Point the
313: * trace cell at the copied value.
314: */
315: p = ap_make_sub_pool(example_pool);
316: if (trace != NULL) {
317: trace = ap_pstrdup(p, trace);
318: }
319: /*
320: * Now, if we have a sub-pool from before, nuke it and replace with
321: * the one we just allocated.
322: */
323: if (example_subpool != NULL) {
324: ap_destroy_pool(example_subpool);
325: }
326: example_subpool = p;
327: trace_copy = trace;
328: }
329: /*
330: * If we weren't passed a configuration record, we can't figure out to
331: * what location this call applies. This only happens for co-routines
332: * that don't operate in a particular directory or server context. If we
333: * got a valid record, extract the location (directory or server) to which
334: * it applies.
335: */
336: where = (mconfig != NULL) ? mconfig->loc : "nowhere";
337: where = (where != NULL) ? where : "";
338: /*
339: * Now, if we're not in request context, see if we've been called with
340: * this particular combination before. The table is allocated in the
341: * module's private pool, which doesn't get destroyed.
342: */
343: if (r == NULL) {
344: char *key;
345:
346: key = ap_pstrcat(p, note, ":", where, NULL);
347: if (ap_table_get(static_calls_made, key) != NULL) {
348: /*
349: * Been here, done this.
350: */
351: return;
352: }
353: else {
354: /*
355: * First time for this combination of routine and environment -
356: * log it so we don't do it again.
357: */
358: ap_table_set(static_calls_made, key, "been here");
359: }
360: }
361: addon = ap_pstrcat(p, " <LI>\n", " <DL>\n", " <DT><SAMP>",
362: note, "</SAMP>\n", " </DT>\n", " <DD><SAMP>[",
363: where, "]</SAMP>\n", " </DD>\n", " </DL>\n",
364: " </LI>\n", NULL);
365: sofar = (trace_copy == NULL) ? "" : trace_copy;
366: trace_copy = ap_pstrcat(p, sofar, addon, NULL);
367: if (r != NULL) {
368: ap_table_set(r->notes, TRACE_NOTE, trace_copy);
369: }
370: else {
371: trace = trace_copy;
372: }
373: /*
374: * You *could* change the following if you wanted to see the calling
375: * sequence reported in the server's error_log, but beware - almost all of
376: * these co-routines are called for every single request, and the impact
377: * on the size (and readability) of the error_log is considerable.
378: */
379: #define EXAMPLE_LOG_EACH 0
380: #if EXAMPLE_LOG_EACH
381: if (s != NULL) {
382: ap_log_error(APLOG_MARK, APLOG_DEBUG, s, "mod_example: %s", note);
383: }
384: #endif
385: }
386:
387: /*--------------------------------------------------------------------------*/
388: /* We prototyped the various syntax for command handlers (routines that */
389: /* are called when the configuration parser detects a directive declared */
390: /* by our module) earlier. Now we actually declare a "real" routine that */
391: /* will be invoked by the parser when our "real" directive is */
392: /* encountered. */
393: /* */
394: /* If a command handler encounters a problem processing the directive, it */
395: /* signals this fact by returning a non-NULL pointer to a string */
396: /* describing the problem. */
397: /* */
398: /* The magic return value DECLINE_CMD is used to deal with directives */
399: /* that might be declared by multiple modules. If the command handler */
400: /* returns NULL, the directive was processed; if it returns DECLINE_CMD, */
401: /* the next module (if any) that declares the directive is given a chance */
402: /* at it. If it returns any other value, it's treated as the text of an */
403: /* error message. */
404: /*--------------------------------------------------------------------------*/
405: /*
406: * Command handler for the NO_ARGS "Example" directive. All we do is mark the
407: * call in the trace log, and flag the applicability of the directive to the
408: * current location in that location's configuration record.
409: */
410: static const char *cmd_example(cmd_parms *cmd, void *mconfig)
411: {
412:
413: excfg *cfg = (excfg *) mconfig;
414:
415: /*
416: * "Example Wuz Here"
417: */
418: cfg->local = 1;
419: trace_add(cmd->server, NULL, cfg, "cmd_example()");
420: return NULL;
421: }
422:
423: /*--------------------------------------------------------------------------*/
424: /* */
425: /* Now we declare our content handlers, which are invoked when the server */
426: /* encounters a document which our module is supposed to have a chance to */
427: /* see. (See mod_mime's SetHandler and AddHandler directives, and the */
428: /* mod_info and mod_status examples, for more details.) */
429: /* */
430: /* Since content handlers are dumping data directly into the connexion */
431: /* (using the r*() routines, such as rputs() and rprintf()) without */
432: /* intervention by other parts of the server, they need to make */
433: /* sure any accumulated HTTP headers are sent first. This is done by */
434: /* calling send_http_header(). Otherwise, no header will be sent at all, */
435: /* and the output sent to the client will actually be HTTP-uncompliant. */
436: /*--------------------------------------------------------------------------*/
437: /*
438: * Sample content handler. All this does is display the call list that has
439: * been built up so far.
440: *
441: * The return value instructs the caller concerning what happened and what to
442: * do next:
443: * OK ("we did our thing")
444: * DECLINED ("this isn't something with which we want to get involved")
445: * HTTP_mumble ("an error status should be reported")
446: */
447:
448:
449: // service funcs
450:
1.4 ! paf 451: const char *get_env(Pool& pool, const char *name) {
! 452: request_rec *r=static_cast<request_rec *>(pool.info());
! 453: return (const char *)ap_table_get(r->subprocess_env, name);
! 454: }
! 455:
1.2 paf 456: int read_post(Pool& pool, char *buf, int max_bytes) {
457: return 0;/* @todo
458: int read_size=0;
459: do {
460: int chunk_size=read
461: (fileno(stdin), buf+read_size, min(0x400*0x400, max_bytes-read_size));
462: if(chunk_size<0)
463: break;
464: read_size+=chunk_size;
465: } while(read_size<max_bytes);
466:
467: return read_size;*/
468: }
469:
470: void add_header_attribute(Pool& pool, const char *key, const char *value) {
471: request_rec *r=static_cast<request_rec *>(pool.info());
472:
1.3 paf 473: if(strcasecmp(key, "content-type")==0) {
1.2 paf 474: /* r->content_type, *not* r->headers_out("Content-type"). If you don't
475: * set it, it will be filled in with the server's default type (typically
476: * "text/plain"). You *must* also ensure that r->content_type is lower
477: * case.
478: */
479: r->content_type = value;
480: } else
481: ap_table_merge(r->headers_out, key, value);
482: }
483:
484: void send_header(Pool& pool) {
485: request_rec *r=static_cast<request_rec *>(pool.info());
486: ap_send_http_header(r);
487: }
488:
489: void send_body(Pool& pool, const char *buf, size_t size) {
490: request_rec *r=static_cast<request_rec *>(pool.info());
491: ap_rwrite(buf, size, r);
492: }
493:
494: ///@todo initSocks();
495: static int parser3_handler(request_rec *r)
496: {
497: Pool pool;
498: pool.set_storage(r->pool);
499: pool.set_info(r);
500:
501: excfg *dcfg;
502:
503: dcfg = our_dconfig(r);
504:
505:
506: /* A flag which modules can set, to indicate that the data being
507: * returned is volatile, and clients should be told not to cache it.
508: */
509: r->no_cache=1;
510:
511: ap_soft_timeout("send example call trace", r);
512: PTRY { // global try
513: const char *filespec_to_process=r->filename;
514:
1.4 ! paf 515: ap_add_common_vars(r);
! 516: ap_add_cgi_vars(r);
! 517:
1.2 paf 518: // Request info
519: Request::Info request_info;
520: const char *document_root=
521: (const char *)ap_table_get(r->subprocess_env, "DOCUMENT_ROOT");
522: if(!document_root) {
523: static char fake_document_root[MAX_STRING];
524: strncpy(fake_document_root, filespec_to_process, MAX_STRING);
525: rsplit(fake_document_root, '/'); rsplit(fake_document_root, '\\');// strip filename
526: document_root=fake_document_root;
527: }
528: request_info.document_root=document_root;
529: request_info.path_translated=filespec_to_process;
530: request_info.method=r->method;
531: request_info.query_string=r->args;
1.4 ! paf 532: request_info.uri=
! 533: (const char *)ap_table_get(r->subprocess_env, "REQUEST_URI");
1.2 paf 534: request_info.content_type=r->content_type;
535: const char *content_length =
536: (const char *)ap_table_get(r->subprocess_env, "CONTENT_LENGTH");
537: request_info.content_length=(content_length?atoi(content_length):0);
538: request_info.cookie=(const char *)ap_table_get(r->subprocess_env, "HTTP_COOKIE");
539:
540: // prepare to process request
541: Request request(pool,
542: request_info,
543: String::UL_HTML_TYPO
544: );
545:
546: /* move this to httpd.conf | .htaccess
547:
548: // some root-controlled location
549: char *sys_auto_path1;
550: #ifdef WIN32
551: // c:\windows
552: sys_auto_path1=(char *)pool.malloc(MAX_STRING);
553: GetWindowsDirectory(sys_auto_path1, MAX_STRING);
554: strcat(sys_auto_path1, PATH_DELIMITER_STRING);
555: #else
556: // ~nobody
1.4 ! paf 557: sys_auto_path1=zgetenv("HOME");
1.2 paf 558: #endif
559:
560: // beside by binary
561: char *sys_auto_path2=(char *)pool.malloc(MAX_STRING);
562: strncpy(sys_auto_path2, argv[0], MAX_STRING); // filespec of my binary
563: rsplit(sys_auto_path2, '/'); rsplit(sys_auto_path2, '\\');// strip filename
564: strcat(sys_auto_path2, PATH_DELIMITER_STRING);
565:
566: */
567:
568: // process the request
569: request.core(
570: 0/*sys_auto_path1*/,
571: 0/*sys_auto_path2*/,
572: r->header_only!=0);
573: // no actions with request' data past this point
574: // request.exception not not handled here, but all
575: // request' data are associated with it's pool=exception
576:
577: // successful finish
578: } PCATCH(e) { // global problem
579: const char *body=e.comment();
580: int content_length=strlen(body);
581:
582: // prepare header
583: (*service_funcs.add_header_attribute)(pool, "content-type", "text/plain");
584: char content_length_cstr[MAX_NUMBER];
585: snprintf(content_length_cstr, MAX_NUMBER, "%d", content_length);
586: (*service_funcs.add_header_attribute)(pool, "content-length",
587: content_length_cstr);
588:
589: // send header
590: (*service_funcs.send_header)(pool);
591:
592: // send body
593: if(!r->header_only)
594: (*service_funcs.send_body)(pool, body, content_length);
595:
596: // unsuccessful finish
597: }
598: PEND_CATCH
599: ap_kill_timeout(r);
600:
601: /*
602: * We did what we wanted to do, so tell the rest of the server we
603: * succeeded.
604: */
605: return OK;
606: }
607:
608: /*--------------------------------------------------------------------------*/
609: /* */
610: /* Now let's declare routines for each of the callback phase in order. */
611: /* (That's the order in which they're listed in the callback list, *not */
612: /* the order in which the server calls them! See the command_rec */
613: /* declaration near the bottom of this file.) Note that these may be */
614: /* called for situations that don't relate primarily to our function - in */
615: /* other words, the fixup handler shouldn't assume that the request has */
616: /* to do with "example" stuff. */
617: /* */
618: /* With the exception of the content handler, all of our routines will be */
619: /* called for each request, unless an earlier handler from another module */
620: /* aborted the sequence. */
621: /* */
622: /* Handlers that are declared as "int" can return the following: */
623: /* */
624: /* OK Handler accepted the request and did its thing with it. */
625: /* DECLINED Handler took no action. */
626: /* HTTP_mumble Handler looked at request and found it wanting. */
627: /* */
628: /* What the server does after calling a module handler depends upon the */
629: /* handler's return value. In all cases, if the handler returns */
630: /* DECLINED, the server will continue to the next module with an handler */
631: /* for the current phase. However, if the handler return a non-OK, */
632: /* non-DECLINED status, the server aborts the request right there. If */
633: /* the handler returns OK, the server's next action is phase-specific; */
634: /* see the individual handler comments below for details. */
635: /* */
636: /*--------------------------------------------------------------------------*/
637: /*
638: * This function is called during server initialisation. Any information
639: * that needs to be recorded must be in static cells, since there's no
640: * configuration record.
641: *
642: * There is no return value.
643: */
644:
645: /*
646: * All our module-initialiser does is add its trace to the log.
647: */
648: static void parser3_init(server_rec *s, pool *p)
649: {
650:
651: char *sname = s->server_hostname;
652:
653: static bool globals_inited=false;
654: if(globals_inited)
655: return;
656: globals_inited=true;
657:
658: static Pool pool; ///< global pool
659: pool.set_storage(p);
660: PTRY {
661: // init global variables
662: globals_init(pool);
663:
664: // Service funcs
1.4 ! paf 665: service_funcs.get_env=get_env;
1.2 paf 666: service_funcs.read_post=read_post;
667: service_funcs.add_header_attribute=add_header_attribute;
668: service_funcs.send_header=send_header;
669: service_funcs.send_body=send_body;
670:
671: //...
672: } PCATCH(e) { // global problem
673: const char *body=e.comment();
674: // somehow report that error
675: }
676: PEND_CATCH
677: }
678:
679: /*
680: * This function is called during server initialisation when an heavy-weight
681: * process (such as a child) is being initialised. As with the
682: * module-initialisation function, any information that needs to be recorded
683: * must be in static cells, since there's no configuration record.
684: *
685: * There is no return value.
686: */
687:
688: /*
689: * All our process-initialiser does is add its trace to the log.
690: */
691: static void example_child_init(server_rec *s, pool *p)
692: {
693:
694: char *note;
695: char *sname = s->server_hostname;
696:
697: /*
698: * Set up any module cells that ought to be initialised.
699: */
700: setup_module_cells();
701: /*
702: * The arbitrary text we add to our trace entry indicates for which server
703: * we're being called.
704: */
705: sname = (sname != NULL) ? sname : "";
706: note = ap_pstrcat(p, "example_child_init(", sname, ")", NULL);
707: trace_add(s, NULL, NULL, note);
708: }
709:
710: /*
711: * This function is called when an heavy-weight process (such as a child) is
712: * being run down or destroyed. As with the child-initialisation function,
713: * any information that needs to be recorded must be in static cells, since
714: * there's no configuration record.
715: *
716: * There is no return value.
717: */
718:
719: /*
720: * All our process-death routine does is add its trace to the log.
721: */
722: static void example_child_exit(server_rec *s, pool *p)
723: {
724:
725: char *note;
726: char *sname = s->server_hostname;
727:
728: /*
729: * The arbitrary text we add to our trace entry indicates for which server
730: * we're being called.
731: */
732: sname = (sname != NULL) ? sname : "";
733: note = ap_pstrcat(p, "example_child_exit(", sname, ")", NULL);
734: trace_add(s, NULL, NULL, note);
735: }
736:
737: /*
738: * This function gets called to create a per-directory configuration
739: * record. This will be called for the "default" server environment, and for
740: * each directory for which the parser finds any of our directives applicable.
741: * If a directory doesn't have any of our directives involved (i.e., they
742: * aren't in the .htaccess file, or a <Location>, <Directory>, or related
743: * block), this routine will *not* be called - the configuration for the
744: * closest ancestor is used.
745: *
746: * The return value is a pointer to the created module-specific
747: * structure.
748: */
749: static void *example_create_dir_config(pool *p, char *dirspec)
750: {
751:
752: excfg *cfg;
753: char *dname = dirspec;
754:
755: /*
756: * Allocate the space for our record from the pool supplied.
757: */
758: cfg = (excfg *) ap_pcalloc(p, sizeof(excfg));
759: /*
760: * Now fill in the defaults. If there are any `parent' configuration
761: * records, they'll get merged as part of a separate callback.
762: */
763: cfg->local = 0;
764: cfg->congenital = 0;
765: cfg->cmode = CONFIG_MODE_DIRECTORY;
766: /*
767: * Finally, add our trace to the callback list.
768: */
769: dname = (dname != NULL) ? dname : "";
770: cfg->loc = ap_pstrcat(p, "DIR(", dname, ")", NULL);
771: trace_add(NULL, NULL, cfg, "example_create_dir_config()");
772: return (void *) cfg;
773: }
774:
775: /*
776: * This function gets called to merge two per-directory configuration
777: * records. This is typically done to cope with things like .htaccess files
778: * or <Location> directives for directories that are beneath one for which a
779: * configuration record was already created. The routine has the
780: * responsibility of creating a new record and merging the contents of the
781: * other two into it appropriately. If the module doesn't declare a merge
782: * routine, the record for the closest ancestor location (that has one) is
783: * used exclusively.
784: *
785: * The routine MUST NOT modify any of its arguments!
786: *
787: * The return value is a pointer to the created module-specific structure
788: * containing the merged values.
789: */
790: static void *example_merge_dir_config(pool *p, void *parent_conf,
791: void *newloc_conf)
792: {
793:
794: excfg *merged_config = (excfg *) ap_pcalloc(p, sizeof(excfg));
795: excfg *pconf = (excfg *) parent_conf;
796: excfg *nconf = (excfg *) newloc_conf;
797: char *note;
798:
799: /*
800: * Some things get copied directly from the more-specific record, rather
801: * than getting merged.
802: */
803: merged_config->local = nconf->local;
804: merged_config->loc = ap_pstrdup(p, nconf->loc);
805: /*
806: * Others, like the setting of the `congenital' flag, get ORed in. The
807: * setting of that particular flag, for instance, is TRUE if it was ever
808: * true anywhere in the upstream configuration.
809: */
810: merged_config->congenital = (pconf->congenital | pconf->local);
811: /*
812: * If we're merging records for two different types of environment (server
813: * and directory), mark the new record appropriately. Otherwise, inherit
814: * the current value.
815: */
816: merged_config->cmode =
817: (pconf->cmode == nconf->cmode) ? pconf->cmode : CONFIG_MODE_COMBO;
818: /*
819: * Now just record our being called in the trace list. Include the
820: * locations we were asked to merge.
821: */
822: note = ap_pstrcat(p, "example_merge_dir_config(\"", pconf->loc, "\",\"",
823: nconf->loc, "\")", NULL);
824: trace_add(NULL, NULL, merged_config, note);
825: return (void *) merged_config;
826: }
827:
828: /*
829: * This function gets called to create a per-server configuration
830: * record. It will always be called for the "default" server.
831: *
832: * The return value is a pointer to the created module-specific
833: * structure.
834: */
835: static void *example_create_server_config(pool *p, server_rec *s)
836: {
837:
838: excfg *cfg;
839: char *sname = s->server_hostname;
840:
841: /*
842: * As with the example_create_dir_config() reoutine, we allocate and fill
843: * in an empty record.
844: */
845: cfg = (excfg *) ap_pcalloc(p, sizeof(excfg));
846: cfg->local = 0;
847: cfg->congenital = 0;
848: cfg->cmode = CONFIG_MODE_SERVER;
849: /*
850: * Note that we were called in the trace list.
851: */
852: sname = (sname != NULL) ? sname : "";
853: cfg->loc = ap_pstrcat(p, "SVR(", sname, ")", NULL);
854: trace_add(s, NULL, cfg, "example_create_server_config()");
855: return (void *) cfg;
856: }
857:
858: /*
859: * This function gets called to merge two per-server configuration
860: * records. This is typically done to cope with things like virtual hosts and
861: * the default server configuration The routine has the responsibility of
862: * creating a new record and merging the contents of the other two into it
863: * appropriately. If the module doesn't declare a merge routine, the more
864: * specific existing record is used exclusively.
865: *
866: * The routine MUST NOT modify any of its arguments!
867: *
868: * The return value is a pointer to the created module-specific structure
869: * containing the merged values.
870: */
871: static void *example_merge_server_config(pool *p, void *server1_conf,
872: void *server2_conf)
873: {
874:
875: excfg *merged_config = (excfg *) ap_pcalloc(p, sizeof(excfg));
876: excfg *s1conf = (excfg *) server1_conf;
877: excfg *s2conf = (excfg *) server2_conf;
878: char *note;
879:
880: /*
881: * Our inheritance rules are our own, and part of our module's semantics.
882: * Basically, just note whence we came.
883: */
884: merged_config->cmode =
885: (s1conf->cmode == s2conf->cmode) ? s1conf->cmode : CONFIG_MODE_COMBO;
886: merged_config->local = s2conf->local;
887: merged_config->congenital = (s1conf->congenital | s1conf->local);
888: merged_config->loc = ap_pstrdup(p, s2conf->loc);
889: /*
890: * Trace our call, including what we were asked to merge.
891: */
892: note = ap_pstrcat(p, "example_merge_server_config(\"", s1conf->loc, "\",\"",
893: s2conf->loc, "\")", NULL);
894: trace_add(NULL, NULL, merged_config, note);
895: return (void *) merged_config;
896: }
897:
898: /*
899: * This routine is called after the request has been read but before any other
900: * phases have been processed. This allows us to make decisions based upon
901: * the input header fields.
902: *
903: * The return value is OK, DECLINED, or HTTP_mumble. If we return OK, no
904: * further modules are called for this phase.
905: */
906: static int example_post_read_request(request_rec *r)
907: {
908:
909: excfg *cfg;
910:
911: cfg = our_dconfig(r);
912: /*
913: * We don't actually *do* anything here, except note the fact that we were
914: * called.
915: */
916: trace_add(r->server, r, cfg, "example_post_read_request()");
917: return DECLINED;
918: }
919:
920: /*
921: * This routine gives our module an opportunity to translate the URI into an
922: * actual filename. If we don't do anything special, the server's default
923: * rules (Alias directives and the like) will continue to be followed.
924: *
925: * The return value is OK, DECLINED, or HTTP_mumble. If we return OK, no
926: * further modules are called for this phase.
927: */
928: static int example_translate_handler(request_rec *r)
929: {
930:
931: excfg *cfg;
932:
933: cfg = our_dconfig(r);
934: /*
935: * We don't actually *do* anything here, except note the fact that we were
936: * called.
937: */
938: trace_add(r->server, r, cfg, "example_translate_handler()");
939: return DECLINED;
940: }
941:
942: /*
943: * This routine is called to check the authentication information sent with
944: * the request (such as looking up the user in a database and verifying that
945: * the [encrypted] password sent matches the one in the database).
946: *
947: * The return value is OK, DECLINED, or some HTTP_mumble error (typically
948: * HTTP_UNAUTHORIZED). If we return OK, no other modules are given a chance
949: * at the request during this phase.
950: */
951: static int example_check_user_id(request_rec *r)
952: {
953:
954: excfg *cfg;
955:
956: cfg = our_dconfig(r);
957: /*
958: * Don't do anything except log the call.
959: */
960: trace_add(r->server, r, cfg, "example_check_user_id()");
961: return DECLINED;
962: }
963:
964: /*
965: * This routine is called to check to see if the resource being requested
966: * requires authorisation.
967: *
968: * The return value is OK, DECLINED, or HTTP_mumble. If we return OK, no
969: * other modules are called during this phase.
970: *
971: * If *all* modules return DECLINED, the request is aborted with a server
972: * error.
973: */
974: static int example_auth_checker(request_rec *r)
975: {
976:
977: excfg *cfg;
978:
979: cfg = our_dconfig(r);
980: /*
981: * Log the call and return OK, or access will be denied (even though we
982: * didn't actually do anything).
983: */
984: trace_add(r->server, r, cfg, "example_auth_checker()");
985: return DECLINED;
986: }
987:
988: /*
989: * This routine is called to check for any module-specific restrictions placed
990: * upon the requested resource. (See the mod_access module for an example.)
991: *
992: * The return value is OK, DECLINED, or HTTP_mumble. All modules with an
993: * handler for this phase are called regardless of whether their predecessors
994: * return OK or DECLINED. The first one to return any other status, however,
995: * will abort the sequence (and the request) as usual.
996: */
997: static int example_access_checker(request_rec *r)
998: {
999:
1000: excfg *cfg;
1001:
1002: cfg = our_dconfig(r);
1003: trace_add(r->server, r, cfg, "example_access_checker()");
1004: return DECLINED;
1005: }
1006:
1007: /*
1008: * This routine is called to determine and/or set the various document type
1009: * information bits, like Content-type (via r->content_type), language, et
1010: * cetera.
1011: *
1012: * The return value is OK, DECLINED, or HTTP_mumble. If we return OK, no
1013: * further modules are given a chance at the request for this phase.
1014: */
1015: static int example_type_checker(request_rec *r)
1016: {
1017:
1018: excfg *cfg;
1019:
1020: cfg = our_dconfig(r);
1021: /*
1022: * Log the call, but don't do anything else - and report truthfully that
1023: * we didn't do anything.
1024: */
1025: trace_add(r->server, r, cfg, "example_type_checker()");
1026: return DECLINED;
1027: }
1028:
1029: /*
1030: * This routine is called to perform any module-specific fixing of header
1031: * fields, et cetera. It is invoked just before any content-handler.
1032: *
1033: * The return value is OK, DECLINED, or HTTP_mumble. If we return OK, the
1034: * server will still call any remaining modules with an handler for this
1035: * phase.
1036: */
1037: static int example_fixer_upper(request_rec *r)
1038: {
1039:
1040: excfg *cfg;
1041:
1042: cfg = our_dconfig(r);
1043: /*
1044: * Log the call and exit.
1045: */
1046: trace_add(r->server, r, cfg, "example_fixer_upper()");
1047: return OK;
1048: }
1049:
1050: /*
1051: * This routine is called to perform any module-specific logging activities
1052: * over and above the normal server things.
1053: *
1054: * The return value is OK, DECLINED, or HTTP_mumble. If we return OK, any
1055: * remaining modules with an handler for this phase will still be called.
1056: */
1057: static int example_logger(request_rec *r)
1058: {
1059:
1060: excfg *cfg;
1061:
1062: cfg = our_dconfig(r);
1063: trace_add(r->server, r, cfg, "example_logger()");
1064: return DECLINED;
1065: }
1066:
1067: /*
1068: * This routine is called to give the module a chance to look at the request
1069: * headers and take any appropriate specific actions early in the processing
1070: * sequence.
1071: *
1072: * The return value is OK, DECLINED, or HTTP_mumble. If we return OK, any
1073: * remaining modules with handlers for this phase will still be called.
1074: */
1075: static int example_header_parser(request_rec *r)
1076: {
1077:
1078: excfg *cfg;
1079:
1080: cfg = our_dconfig(r);
1081: trace_add(r->server, r, cfg, "example_header_parser()");
1082: return DECLINED;
1083: }
1084:
1085: /*--------------------------------------------------------------------------*/
1086: /* */
1087: /* All of the routines have been declared now. Here's the list of */
1088: /* directives specific to our module, and information about where they */
1089: /* may appear and how the command parser should pass them to us for */
1090: /* processing. Note that care must be taken to ensure that there are NO */
1091: /* collisions of directive names between modules. */
1092: /* */
1093: /*--------------------------------------------------------------------------*/
1094: /*
1095: * List of directives specific to our module.
1096: */
1097: static const command_rec example_cmds[] =
1098: {
1099: {
1100: "Example", /* directive name */
1101: (const char *(*)(void))((void *)cmd_example), /* config action routine */
1102: NULL, /* argument to include in call */
1103: (int)OR_OPTIONS, /* where available */
1104: NO_ARGS, /* arguments */
1105: "Example directive - no arguments"
1106: /* directive description */
1107: },
1108: {NULL}
1109: };
1110:
1111: /*--------------------------------------------------------------------------*/
1112: /* */
1113: /* Now the list of content handlers available from this module. */
1114: /* */
1115: /*--------------------------------------------------------------------------*/
1116: /*
1117: * List of content handlers our module supplies. Each handler is defined by
1118: * two parts: a name by which it can be referenced (such as by
1119: * {Add,Set}Handler), and the actual routine name. The list is terminated by
1120: * a NULL block, since it can be of variable length.
1121: *
1122: * Note that content-handlers are invoked on a most-specific to least-specific
1123: * basis; that is, a handler that is declared for "text/plain" will be
1124: * invoked before one that was declared for "text / *". Note also that
1125: * if a content-handler returns anything except DECLINED, no other
1126: * content-handlers will be called.
1127: */
1128: static const handler_rec parser3_handlers[] =
1129: {
1130: {"parser3-handler", parser3_handler},
1131: {NULL}
1132: };
1133:
1134: /*--------------------------------------------------------------------------*/
1135: /* */
1136: /* Finally, the list of callback routines and data structures that */
1137: /* provide the hooks into our module from the other parts of the server. */
1138: /* */
1139: /*--------------------------------------------------------------------------*/
1140: /*
1141: * Module definition for configuration. If a particular callback is not
1142: * needed, replace its routine name below with the word NULL.
1143: *
1144: * The number in brackets indicates the order in which the routine is called
1145: * during request processing. Note that not all routines are necessarily
1146: * called (such as if a resource doesn't have access restrictions).
1147: */
1148: module MODULE_VAR_EXPORT parser3_module =
1149: {
1150: STANDARD_MODULE_STUFF,
1151: parser3_init, /* module initializer */
1152: example_create_dir_config, /* per-directory config creator */
1153: example_merge_dir_config, /* dir config merger */
1154: example_create_server_config, /* server config creator */
1155: example_merge_server_config, /* server config merger */
1156: example_cmds, /* command table */
1157: parser3_handlers, /* [9] list of handlers */
1158: example_translate_handler, /* [2] filename-to-URI translation */
1159: example_check_user_id, /* [5] check/validate user_id */
1160: example_auth_checker, /* [6] check user_id is valid *here* */
1161: example_access_checker, /* [4] check access by host address */
1162: example_type_checker, /* [7] MIME type checker/setter */
1163: example_fixer_upper, /* [8] fixups */
1164: example_logger, /* [10] logger */
1165: #if MODULE_MAGIC_NUMBER >= 19970103
1166: example_header_parser, /* [3] header parser */
1167: #endif
1168: #if MODULE_MAGIC_NUMBER >= 19970719
1169: example_child_init, /* process initializer */
1170: #endif
1171: #if MODULE_MAGIC_NUMBER >= 19970728
1172: example_child_exit, /* process exit/cleanup */
1173: #endif
1174: #if MODULE_MAGIC_NUMBER >= 19970902
1175: example_post_read_request /* [1] post read_request handling */
1176: #endif
1177: };
E-mail: