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