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