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

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

E-mail: