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