Annotation of parser3/src/targets/apache13/modules/extra/mod_parser3.C, revision 1.14
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.14 ! paf 8: $Id: mod_parser3.C,v 1.13 2001/03/23 08:47:48 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 "pa_common.h"
20: #include "pa_globals.h"
21: #include "pa_request.h"
1.8 paf 22: #include "pa_version.h"
1.2 paf 23:
1.8 paf 24: struct Parser_module_config {
25: const char* parser_root_auto_path; /// filespec of admin's auto.p file
26: const char* parser_site_auto_path; /// filespec of site's auto.p file
27: };
1.2 paf 28:
29: /*
30: * Declare ourselves so the configuration routines can find and know us.
31: * We'll fill it in at the end of the module.
32: */
33: extern "C" module MODULE_VAR_EXPORT parser3_module;
34:
35: /*
36: * Locate our directory configuration record for the current request.
37: */
1.14 ! paf 38: static Parser_module_config *our_dconfig(request_rec *r) {
! 39: return (Parser_module_config *)
! 40: ap_get_module_config(r->per_dir_config, &parser3_module);
1.2 paf 41: }
42:
1.14 ! paf 43: static const char *cmd_parser_auto_path(cmd_parms *cmd, void *mconfig, char *file_spec) {
1.8 paf 44: Parser_module_config *cfg = (Parser_module_config *) mconfig;
1.2 paf 45:
1.8 paf 46: // remember assigned filespec into cfg
47: (cmd->info?cfg->parser_root_auto_path:cfg->parser_site_auto_path)=file_spec;
1.2 paf 48:
49: return NULL;
50: }
51:
1.8 paf 52:
1.2 paf 53: /*--------------------------------------------------------------------------*/
54: /* */
55: /* Now we declare our content handlers, which are invoked when the server */
56: /* encounters a document which our module is supposed to have a chance to */
57: /* see. (See mod_mime's SetHandler and AddHandler directives, and the */
58: /* mod_info and mod_status examples, for more details.) */
59: /* */
60: /* Since content handlers are dumping data directly into the connexion */
61: /* (using the r*() routines, such as rputs() and rprintf()) without */
62: /* intervention by other parts of the server, they need to make */
63: /* sure any accumulated HTTP headers are sent first. This is done by */
64: /* calling send_http_header(). Otherwise, no header will be sent at all, */
65: /* and the output sent to the client will actually be HTTP-uncompliant. */
66: /*--------------------------------------------------------------------------*/
67: /*
68: * Sample content handler. All this does is display the call list that has
69: * been built up so far.
70: *
71: * The return value instructs the caller concerning what happened and what to
72: * do next:
73: * OK ("we did our thing")
74: * DECLINED ("this isn't something with which we want to get involved")
75: * HTTP_mumble ("an error status should be reported")
76: */
77:
78:
1.8 paf 79: //@{
80: /// service func decl
1.14 ! paf 81: static const char *sapi_get_env(Pool& pool, const char *name) {
1.10 paf 82: request_rec *r=static_cast<request_rec *>(pool.context());
1.4 paf 83: return (const char *)ap_table_get(r->subprocess_env, name);
84: }
85:
1.14 ! paf 86: static uint sapi_read_post(Pool& pool, char *buf, uint max_bytes) {
1.10 paf 87: request_rec *r=static_cast<request_rec *>(pool.context());
1.5 paf 88:
89: /* ap_log_error(APLOG_MARK, APLOG_DEBUG, r->server,
1.14 ! paf 90: "mod_parser3: sapi_read_post(max=%u)", max_bytes);
1.5 paf 91: */
92: int retval;
93: if((retval = ap_setup_client_block(r, REQUEST_CHUNKED_ERROR)))
94: return 0;
95: if(!ap_should_client_block(r))
96: return 0;
97:
98: uint total_read_bytes=0;
99: void (*handler)(int)=signal(SIGPIPE, SIG_IGN);
100: while (total_read_bytes<max_bytes) {
101: ap_hard_timeout("Read POST information", r); /* start timeout timer */
102: uint read_bytes=
103: ap_get_client_block(r, buf+total_read_bytes, max_bytes-total_read_bytes);
104: ap_reset_timeout(r);
105: if (read_bytes<=0)
1.2 paf 106: break;
1.5 paf 107: total_read_bytes+=read_bytes;
108: }
109: signal(SIGPIPE, handler);
110: return total_read_bytes;
1.2 paf 111: }
112:
1.14 ! paf 113: static void sapi_add_header_attribute(Pool& pool, const char *key, const char *value) {
1.10 paf 114: request_rec *r=static_cast<request_rec *>(pool.context());
1.2 paf 115:
1.3 paf 116: if(strcasecmp(key, "content-type")==0) {
1.2 paf 117: /* r->content_type, *not* r->headers_out("Content-type"). If you don't
118: * set it, it will be filled in with the server's default type (typically
119: * "text/plain"). You *must* also ensure that r->content_type is lower
120: * case.
121: */
122: r->content_type = value;
123: } else
124: ap_table_merge(r->headers_out, key, value);
125: }
126:
1.14 ! paf 127: static void sapi_send_header(Pool& pool) {
1.10 paf 128: request_rec *r=static_cast<request_rec *>(pool.context());
1.5 paf 129:
1.8 paf 130: ap_hard_timeout("Send header", r);
1.2 paf 131: ap_send_http_header(r);
1.5 paf 132: ap_kill_timeout(r);
1.2 paf 133: }
134:
1.14 ! paf 135: static void sapi_send_body(Pool& pool, const char *buf, size_t size) {
1.10 paf 136: request_rec *r=static_cast<request_rec *>(pool.context());
1.5 paf 137:
1.8 paf 138: ap_hard_timeout("Send body", r);
1.2 paf 139: ap_rwrite(buf, size, r);
1.5 paf 140: ap_kill_timeout(r);
1.2 paf 141: }
1.8 paf 142: //@}
143:
1.14 ! paf 144: /// SAPI
! 145: SAPI sapi={
! 146: sapi_get_env,
! 147: sapi_read_post,
! 148: sapi_add_header_attribute,
! 149: sapi_send_header,
! 150: sapi_send_body
1.8 paf 151: };
1.2 paf 152:
1.11 paf 153: /**
154: main workhorse
155:
156: @todo intelligent cache-control
157: */
1.8 paf 158: static int parser_handler(request_rec *r)
1.2 paf 159: {
160: Pool pool;
161: pool.set_storage(r->pool);
1.10 paf 162: pool.set_context(r);
1.2 paf 163:
1.8 paf 164: Parser_module_config *dcfg=our_dconfig(r);
1.2 paf 165:
166:
167: /* A flag which modules can set, to indicate that the data being
168: * returned is volatile, and clients should be told not to cache it.
169: */
170: r->no_cache=1;
171:
172: PTRY { // global try
1.4 paf 173: ap_add_common_vars(r);
174: ap_add_cgi_vars(r);
175:
1.2 paf 176: // Request info
177: Request::Info request_info;
1.14 ! paf 178: request_info.document_root=(const char *)
! 179: ap_table_get(r->subprocess_env, "DOCUMENT_ROOT");
! 180: request_info.path_translated=r->filename;
1.2 paf 181: request_info.method=r->method;
182: request_info.query_string=r->args;
1.14 ! paf 183: request_info.uri=(const char *)
! 184: ap_table_get(r->subprocess_env, "REQUEST_URI");
! 185: request_info.content_type=(const char *)
! 186: ap_table_get(r->subprocess_env, "CONTENT_TYPE");
! 187: const char *content_length=(const char *)
! 188: ap_table_get(r->subprocess_env, "CONTENT_LENGTH");
1.2 paf 189: request_info.content_length=(content_length?atoi(content_length):0);
1.14 ! paf 190: request_info.cookie=(const char *)
! 191: ap_table_get(r->subprocess_env, "HTTP_COOKIE");
1.2 paf 192:
193: // prepare to process request
194: Request request(pool,
195: request_info,
196: String::UL_HTML_TYPO
197: );
198:
199: // process the request
200: request.core(
1.8 paf 201: dcfg->parser_root_auto_path, true, // /path/to/admin/auto.p
202: dcfg->parser_site_auto_path, true, // /path/to/site/auto.p
1.2 paf 203: r->header_only!=0);
204: // no actions with request' data past this point
205: // request.exception not not handled here, but all
206: // request' data are associated with it's pool=exception
207:
208: // successful finish
209: } PCATCH(e) { // global problem
210: const char *body=e.comment();
211: int content_length=strlen(body);
212:
213: // prepare header
1.14 ! paf 214: sapi_add_header_attribute(pool, "content-type", "text/plain");
1.2 paf 215: char content_length_cstr[MAX_NUMBER];
1.12 paf 216: snprintf(content_length_cstr, MAX_NUMBER, "%lu", content_length);
1.14 ! paf 217: sapi_add_header_attribute(pool, "content-length", content_length_cstr);
1.2 paf 218:
219: // send header
1.14 ! paf 220: sapi_send_header(pool);
1.2 paf 221:
222: // send body
223: if(!r->header_only)
1.14 ! paf 224: sapi_send_body(pool, body, content_length);
1.2 paf 225:
226: // unsuccessful finish
227: }
228: PEND_CATCH
229:
230: /*
231: * We did what we wanted to do, so tell the rest of the server we
232: * succeeded.
233: */
234: return OK;
235: }
236:
237: /*--------------------------------------------------------------------------*/
238: /* */
239: /* Now let's declare routines for each of the callback phase in order. */
240: /* (That's the order in which they're listed in the callback list, *not */
241: /* the order in which the server calls them! See the command_rec */
242: /* declaration near the bottom of this file.) Note that these may be */
243: /* called for situations that don't relate primarily to our function - in */
244: /* other words, the fixup handler shouldn't assume that the request has */
245: /* to do with "example" stuff. */
246: /* */
247: /* With the exception of the content handler, all of our routines will be */
248: /* called for each request, unless an earlier handler from another module */
249: /* aborted the sequence. */
250: /* */
251: /* Handlers that are declared as "int" can return the following: */
252: /* */
253: /* OK Handler accepted the request and did its thing with it. */
254: /* DECLINED Handler took no action. */
255: /* HTTP_mumble Handler looked at request and found it wanting. */
256: /* */
257: /* What the server does after calling a module handler depends upon the */
258: /* handler's return value. In all cases, if the handler returns */
259: /* DECLINED, the server will continue to the next module with an handler */
260: /* for the current phase. However, if the handler return a non-OK, */
261: /* non-DECLINED status, the server aborts the request right there. If */
262: /* the handler returns OK, the server's next action is phase-specific; */
263: /* see the individual handler comments below for details. */
264: /* */
265: /*--------------------------------------------------------------------------*/
266: /*
267: * This function is called during server initialisation. Any information
268: * that needs to be recorded must be in static cells, since there's no
269: * configuration record.
270: *
271: * There is no return value.
272: */
1.14 ! paf 273:
! 274: static void setup_module_cells() {
1.2 paf 275: static bool globals_inited=false;
276: if(globals_inited)
277: return;
278: globals_inited=true;
279:
1.14 ! paf 280: /*
! 281: * allocate our module-private pool.
! 282: */
! 283: pool *module_pool=ap_make_sub_pool(NULL);
! 284:
1.6 paf 285: static Pool pool; // global pool
1.14 ! paf 286: pool.set_storage(module_pool);
1.2 paf 287: PTRY {
288: // init global variables
289: globals_init(pool);
290:
291: //...
292: } PCATCH(e) { // global problem
293: const char *body=e.comment();
1.10 paf 294: // TODO: somehow report that error
1.2 paf 295: }
296: PEND_CATCH
297: }
298:
1.14 ! paf 299: static void parser_server_init(server_rec *s, pool *p) {
! 300: #if MODULE_MAGIC_NUMBER >= 19980527
! 301: ap_add_version_component("Parser/" PARSER_VERSION);
! 302: #endif
1.2 paf 303:
304: /*
1.14 ! paf 305: * Set up any module cells that ought to be initialised.
1.2 paf 306: */
1.14 ! paf 307: setup_module_cells();
1.2 paf 308: }
309:
310: /*
311: * This function gets called to create a per-directory configuration
312: * record. This will be called for the "default" server environment, and for
313: * each directory for which the parser finds any of our directives applicable.
314: * If a directory doesn't have any of our directives involved (i.e., they
315: * aren't in the .htaccess file, or a <Location>, <Directory>, or related
316: * block), this routine will *not* be called - the configuration for the
317: * closest ancestor is used.
318: *
319: * The return value is a pointer to the created module-specific
320: * structure.
321: */
1.14 ! paf 322: static void *parser_create_dir_config(pool *p, char *dirspec) {
! 323: /*
1.2 paf 324: * Allocate the space for our record from the pool supplied.
325: */
1.14 ! paf 326: Parser_module_config *cfg=
! 327: (Parser_module_config *) ap_pcalloc(p, sizeof(Parser_module_config));
1.2 paf 328: /*
329: * Now fill in the defaults. If there are any `parent' configuration
330: * records, they'll get merged as part of a separate callback.
331: */
1.8 paf 332: cfg->parser_root_auto_path = 0;
333: cfg->parser_site_auto_path = 0;
1.2 paf 334: return (void *) cfg;
335: }
336:
337: /*
338: * This function gets called to merge two per-directory configuration
339: * records. This is typically done to cope with things like .htaccess files
340: * or <Location> directives for directories that are beneath one for which a
341: * configuration record was already created. The routine has the
342: * responsibility of creating a new record and merging the contents of the
343: * other two into it appropriately. If the module doesn't declare a merge
344: * routine, the record for the closest ancestor location (that has one) is
345: * used exclusively.
346: *
347: * The routine MUST NOT modify any of its arguments!
348: *
349: * The return value is a pointer to the created module-specific structure
350: * containing the merged values.
351: */
1.8 paf 352: static void *parser_merge_dir_config(pool *p, void *parent_conf,
1.14 ! paf 353: void *newloc_conf) {
! 354: Parser_module_config *merged_config =
! 355: (Parser_module_config *) ap_pcalloc(p, sizeof(Parser_module_config));
1.8 paf 356: Parser_module_config *pconf = (Parser_module_config *) parent_conf;
357: Parser_module_config *nconf = (Parser_module_config *) newloc_conf;
358:
359: // always from parent
360: merged_config->parser_root_auto_path = ap_pstrdup(p, pconf->parser_root_auto_path);
1.2 paf 361: /*
362: * Some things get copied directly from the more-specific record, rather
363: * than getting merged.
364: */
1.8 paf 365: merged_config->parser_site_auto_path = ap_pstrdup(p, nconf->parser_site_auto_path?
366: nconf->parser_site_auto_path:pconf->parser_site_auto_path);
367:
1.2 paf 368: return (void *) merged_config;
369: }
370:
371: /*
372: * This function gets called to create a per-server configuration
373: * record. It will always be called for the "default" server.
374: *
375: * The return value is a pointer to the created module-specific
376: * structure.
377: */
1.14 ! paf 378: static void *parser_create_server_config(pool *p, server_rec *s) {
1.2 paf 379: /*
1.8 paf 380: * As with the parser_create_dir_config() reoutine, we allocate and fill
1.2 paf 381: * in an empty record.
382: */
1.14 ! paf 383: Parser_module_config *cfg=
! 384: (Parser_module_config *) ap_pcalloc(p, sizeof(Parser_module_config));
! 385:
1.8 paf 386: cfg->parser_root_auto_path = 0;
387: cfg->parser_site_auto_path = 0;
1.14 ! paf 388:
1.2 paf 389: return (void *) cfg;
390: }
391:
392: /*
393: * This function gets called to merge two per-server configuration
394: * records. This is typically done to cope with things like virtual hosts and
395: * the default server configuration The routine has the responsibility of
396: * creating a new record and merging the contents of the other two into it
397: * appropriately. If the module doesn't declare a merge routine, the more
398: * specific existing record is used exclusively.
399: *
400: * The routine MUST NOT modify any of its arguments!
401: *
402: * The return value is a pointer to the created module-specific structure
403: * containing the merged values.
404: */
1.8 paf 405: static void *parser_merge_server_config(pool *p, void *server1_conf,
1.2 paf 406: void *server2_conf)
407: {
408:
1.14 ! paf 409: Parser_module_config *merged_config =
! 410: (Parser_module_config *) ap_pcalloc(p, sizeof(Parser_module_config));
1.8 paf 411: Parser_module_config *s1conf = (Parser_module_config *) server1_conf;
412: Parser_module_config *s2conf = (Parser_module_config *) server2_conf;
1.2 paf 413:
414: /*
415: * Our inheritance rules are our own, and part of our module's semantics.
416: * Basically, just note whence we came.
417: */
1.8 paf 418: merged_config->parser_root_auto_path = ap_pstrdup(p, s2conf->parser_root_auto_path?
419: s2conf->parser_root_auto_path:s1conf->parser_root_auto_path);
420: merged_config->parser_site_auto_path = ap_pstrdup(p, s2conf->parser_site_auto_path?
421: s2conf->parser_site_auto_path:s1conf->parser_site_auto_path);
422:
423: return (void *) merged_config;
1.2 paf 424: }
425:
426: /*
427: * This routine gives our module an opportunity to translate the URI into an
428: * actual filename. If we don't do anything special, the server's default
429: * rules (Alias directives and the like) will continue to be followed.
430: *
431: * The return value is OK, DECLINED, or HTTP_mumble. If we return OK, no
432: * further modules are called for this phase.
433: */
1.14 ! paf 434: static int parser_translate_handler(request_rec *r) {
! 435: Parser_module_config *cfg=our_dconfig(r);
1.2 paf 436: return DECLINED;
437: }
438:
439: /*
440: * This routine is called to check the authentication information sent with
441: * the request (such as looking up the user in a database and verifying that
442: * the [encrypted] password sent matches the one in the database).
443: *
444: * The return value is OK, DECLINED, or some HTTP_mumble error (typically
445: * HTTP_UNAUTHORIZED). If we return OK, no other modules are given a chance
446: * at the request during this phase.
447: */
1.14 ! paf 448: static int parser_check_user_id(request_rec *r) {
! 449: Parser_module_config *cfg=our_dconfig(r);
1.2 paf 450: return DECLINED;
451: }
452:
453: /*
454: * This routine is called to check to see if the resource being requested
455: * requires authorisation.
456: *
457: * The return value is OK, DECLINED, or HTTP_mumble. If we return OK, no
458: * other modules are called during this phase.
459: *
460: * If *all* modules return DECLINED, the request is aborted with a server
461: * error.
462: */
1.14 ! paf 463: static int parser_auth_checker(request_rec *r) {
! 464: Parser_module_config *cfg=our_dconfig(r);
1.2 paf 465: return DECLINED;
466: }
467:
468: /*
469: * This routine is called to check for any module-specific restrictions placed
470: * upon the requested resource. (See the mod_access module for an example.)
471: *
472: * The return value is OK, DECLINED, or HTTP_mumble. All modules with an
473: * handler for this phase are called regardless of whether their predecessors
474: * return OK or DECLINED. The first one to return any other status, however,
475: * will abort the sequence (and the request) as usual.
476: */
1.14 ! paf 477: static int parser_access_checker(request_rec *r) {
1.2 paf 478:
1.14 ! paf 479: Parser_module_config *cfg=our_dconfig(r);
1.2 paf 480: return DECLINED;
481: }
482:
483: /*--------------------------------------------------------------------------*/
484: /* */
485: /* All of the routines have been declared now. Here's the list of */
486: /* directives specific to our module, and information about where they */
487: /* may appear and how the command parser should pass them to us for */
488: /* processing. Note that care must be taken to ensure that there are NO */
489: /* collisions of directive names between modules. */
490: /* */
491: /*--------------------------------------------------------------------------*/
492: /*
493: * List of directives specific to our module.
494: */
1.8 paf 495: static const command_rec parser_cmds[] =
1.2 paf 496: {
497: {
1.8 paf 498: "parser_root_auto_path", /* directive name */
499: (const char *(*)(void))((void *)cmd_parser_auto_path), // config action routine
500: (void*)true, /* argument to include in call */
501: (int)(ACCESS_CONF|RSRC_CONF), /* where available */
502: TAKE1, /* arguments */
503: "Parser root auto.p filespec (Admin)" // directive description
504: },
505: {
506: "parser_site_auto_path", /* directive name */
507: (const char *(*)(void))((void *)cmd_parser_auto_path), // config action routine
508: (void*)false, /* argument to include in call */
509: (int)(OR_OPTIONS), /* where available */
510: TAKE1, /* arguments */
511: "Parser site auto.p filespec" // directive description
1.2 paf 512: },
513: {NULL}
514: };
515:
516: /*--------------------------------------------------------------------------*/
517: /* */
518: /* Now the list of content handlers available from this module. */
519: /* */
520: /*--------------------------------------------------------------------------*/
521: /*
522: * List of content handlers our module supplies. Each handler is defined by
523: * two parts: a name by which it can be referenced (such as by
524: * {Add,Set}Handler), and the actual routine name. The list is terminated by
525: * a NULL block, since it can be of variable length.
526: *
527: * Note that content-handlers are invoked on a most-specific to least-specific
528: * basis; that is, a handler that is declared for "text/plain" will be
529: * invoked before one that was declared for "text / *". Note also that
530: * if a content-handler returns anything except DECLINED, no other
531: * content-handlers will be called.
532: */
1.8 paf 533: static const handler_rec parser_handlers[] =
1.2 paf 534: {
1.8 paf 535: {"parser3-handler", parser_handler},
1.2 paf 536: {NULL}
537: };
538:
539: /*--------------------------------------------------------------------------*/
540: /* */
541: /* Finally, the list of callback routines and data structures that */
542: /* provide the hooks into our module from the other parts of the server. */
543: /* */
544: /*--------------------------------------------------------------------------*/
545: /*
546: * Module definition for configuration. If a particular callback is not
547: * needed, replace its routine name below with the word NULL.
548: *
549: * The number in brackets indicates the order in which the routine is called
550: * during request processing. Note that not all routines are necessarily
551: * called (such as if a resource doesn't have access restrictions).
552: */
553: module MODULE_VAR_EXPORT parser3_module =
554: {
555: STANDARD_MODULE_STUFF,
1.14 ! paf 556: parser_server_init, /* module initializer */
! 557: parser_create_dir_config, /* per-directory config creator */
! 558: parser_merge_dir_config, /* dir config merger */
! 559: parser_create_server_config, /* server config creator */
! 560: parser_merge_server_config, /* server config merger */
! 561: parser_cmds, /* command table */
! 562: parser_handlers, /* [9] list of handlers */
! 563: parser_translate_handler, /* [2] filename-to-URI translation */
! 564: parser_check_user_id, /* [5] check/validate user_id */
! 565: parser_auth_checker, /* [6] check user_id is valid *here* */
! 566: parser_access_checker, /* [4] check access by host address */
! 567: 0, /* [7] MIME type checker/setter */
! 568: 0, /* [8] fixups */
! 569: 0 /* [10] logger */
1.2 paf 570: };
E-mail: