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