Annotation of parser3/src/targets/apache13/modules/extra/mod_parser3.C, revision 1.3

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: 
1.3     ! paf         8:        $Id: mod_parser3.C,v 1.2 2001/03/21 14:06:49 paf Exp $
1.2       paf         9: */
                     10: 
                     11: #include "httpd.h"
                     12: #include "http_config.h"
                     13: #include "http_core.h"
                     14: #include "http_log.h"
                     15: #include "http_main.h"
                     16: #include "http_protocol.h"
                     17: #include "util_script.h"
                     18: 
                     19: #include <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: 
1.3     ! paf       468:        if(strcasecmp(key, "content-type")==0) {
1.2       paf       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: 
                    510:                // Request info
                    511:                Request::Info request_info;
                    512:                const char *document_root=
                    513:                        (const char *)ap_table_get(r->subprocess_env, "DOCUMENT_ROOT");
                    514:                if(!document_root) {
                    515:                        static char fake_document_root[MAX_STRING];
                    516:                        strncpy(fake_document_root, filespec_to_process, MAX_STRING);
                    517:                        rsplit(fake_document_root, '/');  rsplit(fake_document_root, '\\');// strip filename
                    518:                        document_root=fake_document_root;
                    519:                }
                    520:                request_info.document_root=document_root;
                    521:                request_info.path_translated=filespec_to_process;
                    522:                request_info.method=r->method;
                    523:                request_info.query_string=r->args;
                    524:                request_info.uri=r->uri;
                    525:                request_info.content_type=r->content_type;
                    526:                const char *content_length = 
                    527:                        (const char *)ap_table_get(r->subprocess_env, "CONTENT_LENGTH");
                    528:                request_info.content_length=(content_length?atoi(content_length):0);
                    529:                request_info.cookie=(const char *)ap_table_get(r->subprocess_env, "HTTP_COOKIE");
                    530: 
                    531:                // prepare to process request
                    532:                Request request(pool,
                    533:                        request_info,
                    534:                        String::UL_HTML_TYPO
                    535:                        );
                    536:                
                    537:                /* move this to httpd.conf | .htaccess
                    538: 
                    539:                // some root-controlled location
                    540:                char *sys_auto_path1;
                    541: #ifdef WIN32
                    542:                // c:\windows
                    543:                sys_auto_path1=(char *)pool.malloc(MAX_STRING);
                    544:                GetWindowsDirectory(sys_auto_path1, MAX_STRING);
                    545:                strcat(sys_auto_path1, PATH_DELIMITER_STRING);
                    546: #else
                    547:                // ~nobody
                    548:                sys_auto_path1=getenv("HOME");
                    549: #endif
                    550:                
                    551:                // beside by binary
                    552:                char *sys_auto_path2=(char *)pool.malloc(MAX_STRING);
                    553:                strncpy(sys_auto_path2, argv[0], MAX_STRING);  // filespec of my binary
                    554:                rsplit(sys_auto_path2, '/');  rsplit(sys_auto_path2, '\\');// strip filename
                    555:                strcat(sys_auto_path2, PATH_DELIMITER_STRING);
                    556:                
                    557:                */
                    558: 
                    559:                // process the request
                    560:                request.core(
                    561:                        0/*sys_auto_path1*/, 
                    562:                        0/*sys_auto_path2*/,
                    563:                        r->header_only!=0);
                    564:                // no actions with request' data past this point
                    565:                // request.exception not not handled here, but all
                    566:                // request' data are associated with it's pool=exception
                    567: 
                    568:                // successful finish
                    569:        } PCATCH(e) { // global problem 
                    570:                const char *body=e.comment();
                    571:                int content_length=strlen(body);
                    572: 
                    573:                // prepare header
                    574:                (*service_funcs.add_header_attribute)(pool, "content-type", "text/plain");
                    575:                char content_length_cstr[MAX_NUMBER];
                    576:                snprintf(content_length_cstr, MAX_NUMBER, "%d", content_length);
                    577:                (*service_funcs.add_header_attribute)(pool, "content-length", 
                    578:                        content_length_cstr);
                    579: 
                    580:                // send header
                    581:                (*service_funcs.send_header)(pool);
                    582: 
                    583:                // send body
                    584:                if(!r->header_only)
                    585:                        (*service_funcs.send_body)(pool, body, content_length);
                    586: 
                    587:                // unsuccessful finish
                    588:        }
                    589:        PEND_CATCH
                    590:        ap_kill_timeout(r);
                    591: 
                    592:     /*
                    593:      * We did what we wanted to do, so tell the rest of the server we
                    594:      * succeeded.
                    595:      */
                    596:     return OK;
                    597: }
                    598: 
                    599: /*--------------------------------------------------------------------------*/
                    600: /*                                                                          */
                    601: /* Now let's declare routines for each of the callback phase in order.      */
                    602: /* (That's the order in which they're listed in the callback list, *not     */
                    603: /* the order in which the server calls them!  See the command_rec           */
                    604: /* declaration near the bottom of this file.)  Note that these may be       */
                    605: /* called for situations that don't relate primarily to our function - in   */
                    606: /* other words, the fixup handler shouldn't assume that the request has     */
                    607: /* to do with "example" stuff.                                              */
                    608: /*                                                                          */
                    609: /* With the exception of the content handler, all of our routines will be   */
                    610: /* called for each request, unless an earlier handler from another module   */
                    611: /* aborted the sequence.                                                    */
                    612: /*                                                                          */
                    613: /* Handlers that are declared as "int" can return the following:            */
                    614: /*                                                                          */
                    615: /*  OK          Handler accepted the request and did its thing with it.     */
                    616: /*  DECLINED    Handler took no action.                                     */
                    617: /*  HTTP_mumble Handler looked at request and found it wanting.             */
                    618: /*                                                                          */
                    619: /* What the server does after calling a module handler depends upon the     */
                    620: /* handler's return value.  In all cases, if the handler returns            */
                    621: /* DECLINED, the server will continue to the next module with an handler    */
                    622: /* for the current phase.  However, if the handler return a non-OK,         */
                    623: /* non-DECLINED status, the server aborts the request right there.  If      */
                    624: /* the handler returns OK, the server's next action is phase-specific;      */
                    625: /* see the individual handler comments below for details.                   */
                    626: /*                                                                          */
                    627: /*--------------------------------------------------------------------------*/
                    628: /* 
                    629:  * This function is called during server initialisation.  Any information
                    630:  * that needs to be recorded must be in static cells, since there's no
                    631:  * configuration record.
                    632:  *
                    633:  * There is no return value.
                    634:  */
                    635: 
                    636: /*
                    637:  * All our module-initialiser does is add its trace to the log.
                    638:  */
                    639: static void parser3_init(server_rec *s, pool *p)
                    640: {
                    641: 
                    642:     char *sname = s->server_hostname;
                    643: 
                    644:        static bool globals_inited=false;
                    645:        if(globals_inited)
                    646:                return;
                    647:        globals_inited=true;
                    648: 
                    649:        static Pool pool; ///< global pool
                    650:        pool.set_storage(p);
                    651:        PTRY {
                    652:                // init global variables
                    653:                globals_init(pool);
                    654: 
                    655:                // Service funcs 
                    656:                service_funcs.read_post=read_post;
                    657:                service_funcs.add_header_attribute=add_header_attribute;
                    658:                service_funcs.send_header=send_header;
                    659:                service_funcs.send_body=send_body;
                    660:                
                    661:                //...
                    662:        } PCATCH(e) { // global problem 
                    663:                const char *body=e.comment();
                    664:                // somehow report that error
                    665:        }
                    666:        PEND_CATCH
                    667: }
                    668: 
                    669: /* 
                    670:  * This function is called during server initialisation when an heavy-weight
                    671:  * process (such as a child) is being initialised.  As with the
                    672:  * module-initialisation function, any information that needs to be recorded
                    673:  * must be in static cells, since there's no configuration record.
                    674:  *
                    675:  * There is no return value.
                    676:  */
                    677: 
                    678: /*
                    679:  * All our process-initialiser does is add its trace to the log.
                    680:  */
                    681: static void example_child_init(server_rec *s, pool *p)
                    682: {
                    683: 
                    684:     char *note;
                    685:     char *sname = s->server_hostname;
                    686: 
                    687:     /*
                    688:      * Set up any module cells that ought to be initialised.
                    689:      */
                    690:     setup_module_cells();
                    691:     /*
                    692:      * The arbitrary text we add to our trace entry indicates for which server
                    693:      * we're being called.
                    694:      */
                    695:     sname = (sname != NULL) ? sname : "";
                    696:     note = ap_pstrcat(p, "example_child_init(", sname, ")", NULL);
                    697:     trace_add(s, NULL, NULL, note);
                    698: }
                    699: 
                    700: /* 
                    701:  * This function is called when an heavy-weight process (such as a child) is
                    702:  * being run down or destroyed.  As with the child-initialisation function,
                    703:  * any information that needs to be recorded must be in static cells, since
                    704:  * there's no configuration record.
                    705:  *
                    706:  * There is no return value.
                    707:  */
                    708: 
                    709: /*
                    710:  * All our process-death routine does is add its trace to the log.
                    711:  */
                    712: static void example_child_exit(server_rec *s, pool *p)
                    713: {
                    714: 
                    715:     char *note;
                    716:     char *sname = s->server_hostname;
                    717: 
                    718:     /*
                    719:      * The arbitrary text we add to our trace entry indicates for which server
                    720:      * we're being called.
                    721:      */
                    722:     sname = (sname != NULL) ? sname : "";
                    723:     note = ap_pstrcat(p, "example_child_exit(", sname, ")", NULL);
                    724:     trace_add(s, NULL, NULL, note);
                    725: }
                    726: 
                    727: /*
                    728:  * This function gets called to create a per-directory configuration
                    729:  * record.  This will be called for the "default" server environment, and for
                    730:  * each directory for which the parser finds any of our directives applicable.
                    731:  * If a directory doesn't have any of our directives involved (i.e., they
                    732:  * aren't in the .htaccess file, or a <Location>, <Directory>, or related
                    733:  * block), this routine will *not* be called - the configuration for the
                    734:  * closest ancestor is used.
                    735:  *
                    736:  * The return value is a pointer to the created module-specific
                    737:  * structure.
                    738:  */
                    739: static void *example_create_dir_config(pool *p, char *dirspec)
                    740: {
                    741: 
                    742:     excfg *cfg;
                    743:     char *dname = dirspec;
                    744: 
                    745:     /*
                    746:      * Allocate the space for our record from the pool supplied.
                    747:      */
                    748:     cfg = (excfg *) ap_pcalloc(p, sizeof(excfg));
                    749:     /*
                    750:      * Now fill in the defaults.  If there are any `parent' configuration
                    751:      * records, they'll get merged as part of a separate callback.
                    752:      */
                    753:     cfg->local = 0;
                    754:     cfg->congenital = 0;
                    755:     cfg->cmode = CONFIG_MODE_DIRECTORY;
                    756:     /*
                    757:      * Finally, add our trace to the callback list.
                    758:      */
                    759:     dname = (dname != NULL) ? dname : "";
                    760:     cfg->loc = ap_pstrcat(p, "DIR(", dname, ")", NULL);
                    761:     trace_add(NULL, NULL, cfg, "example_create_dir_config()");
                    762:     return (void *) cfg;
                    763: }
                    764: 
                    765: /*
                    766:  * This function gets called to merge two per-directory configuration
                    767:  * records.  This is typically done to cope with things like .htaccess files
                    768:  * or <Location> directives for directories that are beneath one for which a
                    769:  * configuration record was already created.  The routine has the
                    770:  * responsibility of creating a new record and merging the contents of the
                    771:  * other two into it appropriately.  If the module doesn't declare a merge
                    772:  * routine, the record for the closest ancestor location (that has one) is
                    773:  * used exclusively.
                    774:  *
                    775:  * The routine MUST NOT modify any of its arguments!
                    776:  *
                    777:  * The return value is a pointer to the created module-specific structure
                    778:  * containing the merged values.
                    779:  */
                    780: static void *example_merge_dir_config(pool *p, void *parent_conf,
                    781:                                       void *newloc_conf)
                    782: {
                    783: 
                    784:     excfg *merged_config = (excfg *) ap_pcalloc(p, sizeof(excfg));
                    785:     excfg *pconf = (excfg *) parent_conf;
                    786:     excfg *nconf = (excfg *) newloc_conf;
                    787:     char *note;
                    788: 
                    789:     /*
                    790:      * Some things get copied directly from the more-specific record, rather
                    791:      * than getting merged.
                    792:      */
                    793:     merged_config->local = nconf->local;
                    794:     merged_config->loc = ap_pstrdup(p, nconf->loc);
                    795:     /*
                    796:      * Others, like the setting of the `congenital' flag, get ORed in.  The
                    797:      * setting of that particular flag, for instance, is TRUE if it was ever
                    798:      * true anywhere in the upstream configuration.
                    799:      */
                    800:     merged_config->congenital = (pconf->congenital | pconf->local);
                    801:     /*
                    802:      * If we're merging records for two different types of environment (server
                    803:      * and directory), mark the new record appropriately.  Otherwise, inherit
                    804:      * the current value.
                    805:      */
                    806:     merged_config->cmode =
                    807:         (pconf->cmode == nconf->cmode) ? pconf->cmode : CONFIG_MODE_COMBO;
                    808:     /*
                    809:      * Now just record our being called in the trace list.  Include the
                    810:      * locations we were asked to merge.
                    811:      */
                    812:     note = ap_pstrcat(p, "example_merge_dir_config(\"", pconf->loc, "\",\"",
                    813:                    nconf->loc, "\")", NULL);
                    814:     trace_add(NULL, NULL, merged_config, note);
                    815:     return (void *) merged_config;
                    816: }
                    817: 
                    818: /*
                    819:  * This function gets called to create a per-server configuration
                    820:  * record.  It will always be called for the "default" server.
                    821:  *
                    822:  * The return value is a pointer to the created module-specific
                    823:  * structure.
                    824:  */
                    825: static void *example_create_server_config(pool *p, server_rec *s)
                    826: {
                    827: 
                    828:     excfg *cfg;
                    829:     char *sname = s->server_hostname;
                    830: 
                    831:     /*
                    832:      * As with the example_create_dir_config() reoutine, we allocate and fill
                    833:      * in an empty record.
                    834:      */
                    835:     cfg = (excfg *) ap_pcalloc(p, sizeof(excfg));
                    836:     cfg->local = 0;
                    837:     cfg->congenital = 0;
                    838:     cfg->cmode = CONFIG_MODE_SERVER;
                    839:     /*
                    840:      * Note that we were called in the trace list.
                    841:      */
                    842:     sname = (sname != NULL) ? sname : "";
                    843:     cfg->loc = ap_pstrcat(p, "SVR(", sname, ")", NULL);
                    844:     trace_add(s, NULL, cfg, "example_create_server_config()");
                    845:     return (void *) cfg;
                    846: }
                    847: 
                    848: /*
                    849:  * This function gets called to merge two per-server configuration
                    850:  * records.  This is typically done to cope with things like virtual hosts and
                    851:  * the default server configuration  The routine has the responsibility of
                    852:  * creating a new record and merging the contents of the other two into it
                    853:  * appropriately.  If the module doesn't declare a merge routine, the more
                    854:  * specific existing record is used exclusively.
                    855:  *
                    856:  * The routine MUST NOT modify any of its arguments!
                    857:  *
                    858:  * The return value is a pointer to the created module-specific structure
                    859:  * containing the merged values.
                    860:  */
                    861: static void *example_merge_server_config(pool *p, void *server1_conf,
                    862:                                          void *server2_conf)
                    863: {
                    864: 
                    865:     excfg *merged_config = (excfg *) ap_pcalloc(p, sizeof(excfg));
                    866:     excfg *s1conf = (excfg *) server1_conf;
                    867:     excfg *s2conf = (excfg *) server2_conf;
                    868:     char *note;
                    869: 
                    870:     /*
                    871:      * Our inheritance rules are our own, and part of our module's semantics.
                    872:      * Basically, just note whence we came.
                    873:      */
                    874:     merged_config->cmode =
                    875:         (s1conf->cmode == s2conf->cmode) ? s1conf->cmode : CONFIG_MODE_COMBO;
                    876:     merged_config->local = s2conf->local;
                    877:     merged_config->congenital = (s1conf->congenital | s1conf->local);
                    878:     merged_config->loc = ap_pstrdup(p, s2conf->loc);
                    879:     /*
                    880:      * Trace our call, including what we were asked to merge.
                    881:      */
                    882:     note = ap_pstrcat(p, "example_merge_server_config(\"", s1conf->loc, "\",\"",
                    883:                    s2conf->loc, "\")", NULL);
                    884:     trace_add(NULL, NULL, merged_config, note);
                    885:     return (void *) merged_config;
                    886: }
                    887: 
                    888: /*
                    889:  * This routine is called after the request has been read but before any other
                    890:  * phases have been processed.  This allows us to make decisions based upon
                    891:  * the input header fields.
                    892:  *
                    893:  * The return value is OK, DECLINED, or HTTP_mumble.  If we return OK, no
                    894:  * further modules are called for this phase.
                    895:  */
                    896: static int example_post_read_request(request_rec *r)
                    897: {
                    898: 
                    899:     excfg *cfg;
                    900: 
                    901:     cfg = our_dconfig(r);
                    902:     /*
                    903:      * We don't actually *do* anything here, except note the fact that we were
                    904:      * called.
                    905:      */
                    906:     trace_add(r->server, r, cfg, "example_post_read_request()");
                    907:     return DECLINED;
                    908: }
                    909: 
                    910: /*
                    911:  * This routine gives our module an opportunity to translate the URI into an
                    912:  * actual filename.  If we don't do anything special, the server's default
                    913:  * rules (Alias directives and the like) will continue to be followed.
                    914:  *
                    915:  * The return value is OK, DECLINED, or HTTP_mumble.  If we return OK, no
                    916:  * further modules are called for this phase.
                    917:  */
                    918: static int example_translate_handler(request_rec *r)
                    919: {
                    920: 
                    921:     excfg *cfg;
                    922: 
                    923:     cfg = our_dconfig(r);
                    924:     /*
                    925:      * We don't actually *do* anything here, except note the fact that we were
                    926:      * called.
                    927:      */
                    928:     trace_add(r->server, r, cfg, "example_translate_handler()");
                    929:     return DECLINED;
                    930: }
                    931: 
                    932: /*
                    933:  * This routine is called to check the authentication information sent with
                    934:  * the request (such as looking up the user in a database and verifying that
                    935:  * the [encrypted] password sent matches the one in the database).
                    936:  *
                    937:  * The return value is OK, DECLINED, or some HTTP_mumble error (typically
                    938:  * HTTP_UNAUTHORIZED).  If we return OK, no other modules are given a chance
                    939:  * at the request during this phase.
                    940:  */
                    941: static int example_check_user_id(request_rec *r)
                    942: {
                    943: 
                    944:     excfg *cfg;
                    945: 
                    946:     cfg = our_dconfig(r);
                    947:     /*
                    948:      * Don't do anything except log the call.
                    949:      */
                    950:     trace_add(r->server, r, cfg, "example_check_user_id()");
                    951:     return DECLINED;
                    952: }
                    953: 
                    954: /*
                    955:  * This routine is called to check to see if the resource being requested
                    956:  * requires authorisation.
                    957:  *
                    958:  * The return value is OK, DECLINED, or HTTP_mumble.  If we return OK, no
                    959:  * other modules are called during this phase.
                    960:  *
                    961:  * If *all* modules return DECLINED, the request is aborted with a server
                    962:  * error.
                    963:  */
                    964: static int example_auth_checker(request_rec *r)
                    965: {
                    966: 
                    967:     excfg *cfg;
                    968: 
                    969:     cfg = our_dconfig(r);
                    970:     /*
                    971:      * Log the call and return OK, or access will be denied (even though we
                    972:      * didn't actually do anything).
                    973:      */
                    974:     trace_add(r->server, r, cfg, "example_auth_checker()");
                    975:     return DECLINED;
                    976: }
                    977: 
                    978: /*
                    979:  * This routine is called to check for any module-specific restrictions placed
                    980:  * upon the requested resource.  (See the mod_access module for an example.)
                    981:  *
                    982:  * The return value is OK, DECLINED, or HTTP_mumble.  All modules with an
                    983:  * handler for this phase are called regardless of whether their predecessors
                    984:  * return OK or DECLINED.  The first one to return any other status, however,
                    985:  * will abort the sequence (and the request) as usual.
                    986:  */
                    987: static int example_access_checker(request_rec *r)
                    988: {
                    989: 
                    990:     excfg *cfg;
                    991: 
                    992:     cfg = our_dconfig(r);
                    993:     trace_add(r->server, r, cfg, "example_access_checker()");
                    994:     return DECLINED;
                    995: }
                    996: 
                    997: /*
                    998:  * This routine is called to determine and/or set the various document type
                    999:  * information bits, like Content-type (via r->content_type), language, et
                   1000:  * cetera.
                   1001:  *
                   1002:  * The return value is OK, DECLINED, or HTTP_mumble.  If we return OK, no
                   1003:  * further modules are given a chance at the request for this phase.
                   1004:  */
                   1005: static int example_type_checker(request_rec *r)
                   1006: {
                   1007: 
                   1008:     excfg *cfg;
                   1009: 
                   1010:     cfg = our_dconfig(r);
                   1011:     /*
                   1012:      * Log the call, but don't do anything else - and report truthfully that
                   1013:      * we didn't do anything.
                   1014:      */
                   1015:     trace_add(r->server, r, cfg, "example_type_checker()");
                   1016:     return DECLINED;
                   1017: }
                   1018: 
                   1019: /*
                   1020:  * This routine is called to perform any module-specific fixing of header
                   1021:  * fields, et cetera.  It is invoked just before any content-handler.
                   1022:  *
                   1023:  * The return value is OK, DECLINED, or HTTP_mumble.  If we return OK, the
                   1024:  * server will still call any remaining modules with an handler for this
                   1025:  * phase.
                   1026:  */
                   1027: static int example_fixer_upper(request_rec *r)
                   1028: {
                   1029: 
                   1030:     excfg *cfg;
                   1031: 
                   1032:     cfg = our_dconfig(r);
                   1033:     /*
                   1034:      * Log the call and exit.
                   1035:      */
                   1036:     trace_add(r->server, r, cfg, "example_fixer_upper()");
                   1037:     return OK;
                   1038: }
                   1039: 
                   1040: /*
                   1041:  * This routine is called to perform any module-specific logging activities
                   1042:  * over and above the normal server things.
                   1043:  *
                   1044:  * The return value is OK, DECLINED, or HTTP_mumble.  If we return OK, any
                   1045:  * remaining modules with an handler for this phase will still be called.
                   1046:  */
                   1047: static int example_logger(request_rec *r)
                   1048: {
                   1049: 
                   1050:     excfg *cfg;
                   1051: 
                   1052:     cfg = our_dconfig(r);
                   1053:     trace_add(r->server, r, cfg, "example_logger()");
                   1054:     return DECLINED;
                   1055: }
                   1056: 
                   1057: /*
                   1058:  * This routine is called to give the module a chance to look at the request
                   1059:  * headers and take any appropriate specific actions early in the processing
                   1060:  * sequence.
                   1061:  *
                   1062:  * The return value is OK, DECLINED, or HTTP_mumble.  If we return OK, any
                   1063:  * remaining modules with handlers for this phase will still be called.
                   1064:  */
                   1065: static int example_header_parser(request_rec *r)
                   1066: {
                   1067: 
                   1068:     excfg *cfg;
                   1069: 
                   1070:     cfg = our_dconfig(r);
                   1071:     trace_add(r->server, r, cfg, "example_header_parser()");
                   1072:     return DECLINED;
                   1073: }
                   1074: 
                   1075: /*--------------------------------------------------------------------------*/
                   1076: /*                                                                          */
                   1077: /* All of the routines have been declared now.  Here's the list of          */
                   1078: /* directives specific to our module, and information about where they      */
                   1079: /* may appear and how the command parser should pass them to us for         */
                   1080: /* processing.  Note that care must be taken to ensure that there are NO    */
                   1081: /* collisions of directive names between modules.                           */
                   1082: /*                                                                          */
                   1083: /*--------------------------------------------------------------------------*/
                   1084: /* 
                   1085:  * List of directives specific to our module.
                   1086:  */
                   1087: static const command_rec example_cmds[] =
                   1088: {
                   1089:     {
                   1090:         "Example",              /* directive name */
                   1091:         (const char *(*)(void))((void *)cmd_example),            /* config action routine */
                   1092:         NULL,                   /* argument to include in call */
                   1093:         (int)OR_OPTIONS,             /* where available */
                   1094:         NO_ARGS,                /* arguments */
                   1095:         "Example directive - no arguments"
                   1096:                                 /* directive description */
                   1097:     },
                   1098:     {NULL}
                   1099: };
                   1100: 
                   1101: /*--------------------------------------------------------------------------*/
                   1102: /*                                                                          */
                   1103: /* Now the list of content handlers available from this module.             */
                   1104: /*                                                                          */
                   1105: /*--------------------------------------------------------------------------*/
                   1106: /* 
                   1107:  * List of content handlers our module supplies.  Each handler is defined by
                   1108:  * two parts: a name by which it can be referenced (such as by
                   1109:  * {Add,Set}Handler), and the actual routine name.  The list is terminated by
                   1110:  * a NULL block, since it can be of variable length.
                   1111:  *
                   1112:  * Note that content-handlers are invoked on a most-specific to least-specific
                   1113:  * basis; that is, a handler that is declared for "text/plain" will be
                   1114:  * invoked before one that was declared for "text / *".  Note also that
                   1115:  * if a content-handler returns anything except DECLINED, no other
                   1116:  * content-handlers will be called.
                   1117:  */
                   1118: static const handler_rec parser3_handlers[] =
                   1119: {
                   1120:     {"parser3-handler", parser3_handler},
                   1121:     {NULL}
                   1122: };
                   1123: 
                   1124: /*--------------------------------------------------------------------------*/
                   1125: /*                                                                          */
                   1126: /* Finally, the list of callback routines and data structures that          */
                   1127: /* provide the hooks into our module from the other parts of the server.    */
                   1128: /*                                                                          */
                   1129: /*--------------------------------------------------------------------------*/
                   1130: /* 
                   1131:  * Module definition for configuration.  If a particular callback is not
                   1132:  * needed, replace its routine name below with the word NULL.
                   1133:  *
                   1134:  * The number in brackets indicates the order in which the routine is called
                   1135:  * during request processing.  Note that not all routines are necessarily
                   1136:  * called (such as if a resource doesn't have access restrictions).
                   1137:  */
                   1138: module MODULE_VAR_EXPORT parser3_module =
                   1139: {
                   1140:     STANDARD_MODULE_STUFF,
                   1141:     parser3_init,               /* module initializer */
                   1142:     example_create_dir_config,  /* per-directory config creator */
                   1143:     example_merge_dir_config,   /* dir config merger */
                   1144:     example_create_server_config,       /* server config creator */
                   1145:     example_merge_server_config,        /* server config merger */
                   1146:     example_cmds,               /* command table */
                   1147:     parser3_handlers,           /* [9] list of handlers */
                   1148:     example_translate_handler,  /* [2] filename-to-URI translation */
                   1149:     example_check_user_id,      /* [5] check/validate user_id */
                   1150:     example_auth_checker,       /* [6] check user_id is valid *here* */
                   1151:     example_access_checker,     /* [4] check access by host address */
                   1152:     example_type_checker,       /* [7] MIME type checker/setter */
                   1153:     example_fixer_upper,        /* [8] fixups */
                   1154:     example_logger,             /* [10] logger */
                   1155: #if MODULE_MAGIC_NUMBER >= 19970103
                   1156:     example_header_parser,      /* [3] header parser */
                   1157: #endif
                   1158: #if MODULE_MAGIC_NUMBER >= 19970719
                   1159:     example_child_init,         /* process initializer */
                   1160: #endif
                   1161: #if MODULE_MAGIC_NUMBER >= 19970728
                   1162:     example_child_exit,         /* process exit/cleanup */
                   1163: #endif
                   1164: #if MODULE_MAGIC_NUMBER >= 19970902
                   1165:     example_post_read_request   /* [1] post read_request handling */
                   1166: #endif
                   1167: };

E-mail: