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