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

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.12    ! paf         8:        $Id: mod_parser3.C,v 1.11 2001/03/22 17:13: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"
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.10      paf       432:        request_rec *r=static_cast<request_rec *>(pool.context());
1.4       paf       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.10      paf       437:        request_rec *r=static_cast<request_rec *>(pool.context());
1.5       paf       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.10      paf       464:        request_rec *r=static_cast<request_rec *>(pool.context());
1.2       paf       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.10      paf       478:        request_rec *r=static_cast<request_rec *>(pool.context());
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.10      paf       486:        request_rec *r=static_cast<request_rec *>(pool.context());
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.11      paf       503: /**
                    504:        main workhorse
                    505:        
                    506:        @todo intelligent cache-control
                    507: */
1.8       paf       508: static int parser_handler(request_rec *r)
1.2       paf       509: {
                    510:        Pool pool;
                    511:        pool.set_storage(r->pool);
1.10      paf       512:        pool.set_context(r);
1.2       paf       513: 
1.8       paf       514:     Parser_module_config *dcfg=our_dconfig(r);
1.2       paf       515: 
                    516: 
                    517:        /* A flag which modules can set, to indicate that the data being
                    518:         * returned is volatile, and clients should be told not to cache it.
                    519:         */
                    520:        r->no_cache=1;
                    521: 
                    522:        PTRY { // global try
                    523:                const char *filespec_to_process=r->filename;
                    524: 
1.4       paf       525:                ap_add_common_vars(r);
                    526:                ap_add_cgi_vars(r);
                    527: 
1.2       paf       528:                // Request info
                    529:                Request::Info request_info;
                    530:                const char *document_root=
                    531:                        (const char *)ap_table_get(r->subprocess_env, "DOCUMENT_ROOT");
                    532:                if(!document_root) {
                    533:                        static char fake_document_root[MAX_STRING];
                    534:                        strncpy(fake_document_root, filespec_to_process, MAX_STRING);
                    535:                        rsplit(fake_document_root, '/');  rsplit(fake_document_root, '\\');// strip filename
                    536:                        document_root=fake_document_root;
                    537:                }
                    538:                request_info.document_root=document_root;
                    539:                request_info.path_translated=filespec_to_process;
                    540:                request_info.method=r->method;
                    541:                request_info.query_string=r->args;
1.4       paf       542:                request_info.uri=
                    543:                        (const char *)ap_table_get(r->subprocess_env, "REQUEST_URI");
1.5       paf       544:                request_info.content_type=
                    545:                        (const char *)ap_table_get(r->subprocess_env, "CONTENT_TYPE");
1.2       paf       546:                const char *content_length = 
                    547:                        (const char *)ap_table_get(r->subprocess_env, "CONTENT_LENGTH");
                    548:                request_info.content_length=(content_length?atoi(content_length):0);
1.7       paf       549:                request_info.cookie=
                    550:                        (const char *)ap_table_get(r->subprocess_env, "HTTP_COOKIE");
1.2       paf       551: 
                    552:                // prepare to process request
                    553:                Request request(pool,
                    554:                        request_info,
                    555:                        String::UL_HTML_TYPO
                    556:                        );
                    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];
1.12    ! paf       575:                snprintf(content_length_cstr, MAX_NUMBER, "%lu", content_length);
1.2       paf       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:  */
1.9       paf       633: /// @todo answer this: "why when it's called second time all globals are cleared?"
1.8       paf       634: static void parser_init(server_rec *s, pool *p)
1.2       paf       635: {
1.8       paf       636: #if MODULE_MAGIC_NUMBER >= 19980527
                    637:        ap_add_version_component("Parser/" PARSER_VERSION);
                    638: #endif 
                    639:        
1.2       paf       640:        static bool globals_inited=false;
                    641:        if(globals_inited)
                    642:                return;
                    643:        globals_inited=true;
                    644: 
1.6       paf       645:        static Pool pool; // global pool
1.2       paf       646:        pool.set_storage(p);
                    647:        PTRY {
                    648:                // init global variables
                    649:                globals_init(pool);
                    650:                
                    651:                //...
                    652:        } PCATCH(e) { // global problem 
                    653:                const char *body=e.comment();
1.10      paf       654:                // TODO: somehow report that error
1.2       paf       655:        }
                    656:        PEND_CATCH
                    657: }
                    658: 
                    659: /* 
                    660:  * This function is called when an heavy-weight process (such as a child) is
                    661:  * being run down or destroyed.  As with the child-initialisation function,
                    662:  * any information that needs to be recorded must be in static cells, since
                    663:  * there's no configuration record.
                    664:  *
                    665:  * There is no return value.
                    666:  */
                    667: 
                    668: /*
                    669:  * All our process-death routine does is add its trace to the log.
                    670:  */
1.8       paf       671: static void parser_child_exit(server_rec *s, pool *p)
1.2       paf       672: {
                    673: 
                    674:     char *note;
                    675:     char *sname = s->server_hostname;
                    676: 
                    677:     /*
                    678:      * The arbitrary text we add to our trace entry indicates for which server
                    679:      * we're being called.
                    680:      */
                    681:     sname = (sname != NULL) ? sname : "";
1.8       paf       682:     note = ap_pstrcat(p, "parser_child_exit(", sname, ")", NULL);
1.2       paf       683:     trace_add(s, NULL, NULL, note);
                    684: }
                    685: 
                    686: /*
                    687:  * This function gets called to create a per-directory configuration
                    688:  * record.  This will be called for the "default" server environment, and for
                    689:  * each directory for which the parser finds any of our directives applicable.
                    690:  * If a directory doesn't have any of our directives involved (i.e., they
                    691:  * aren't in the .htaccess file, or a <Location>, <Directory>, or related
                    692:  * block), this routine will *not* be called - the configuration for the
                    693:  * closest ancestor is used.
                    694:  *
                    695:  * The return value is a pointer to the created module-specific
                    696:  * structure.
                    697:  */
1.8       paf       698: static void *parser_create_dir_config(pool *p, char *dirspec)
1.2       paf       699: {
                    700: 
1.8       paf       701:     Parser_module_config *cfg;
1.2       paf       702:     char *dname = dirspec;
                    703: 
                    704:     /*
                    705:      * Allocate the space for our record from the pool supplied.
                    706:      */
1.8       paf       707:     cfg = (Parser_module_config *) ap_pcalloc(p, sizeof(Parser_module_config));
1.2       paf       708:     /*
                    709:      * Now fill in the defaults.  If there are any `parent' configuration
                    710:      * records, they'll get merged as part of a separate callback.
                    711:      */
1.8       paf       712:     cfg->parser_root_auto_path = 0;
                    713:        cfg->parser_site_auto_path = 0;
1.2       paf       714:     cfg->cmode = CONFIG_MODE_DIRECTORY;
                    715:     /*
                    716:      * Finally, add our trace to the callback list.
                    717:      */
                    718:     dname = (dname != NULL) ? dname : "";
                    719:     cfg->loc = ap_pstrcat(p, "DIR(", dname, ")", NULL);
1.8       paf       720:     trace_add(NULL, NULL, cfg, "parser_create_dir_config()");
1.2       paf       721:     return (void *) cfg;
                    722: }
                    723: 
                    724: /*
                    725:  * This function gets called to merge two per-directory configuration
                    726:  * records.  This is typically done to cope with things like .htaccess files
                    727:  * or <Location> directives for directories that are beneath one for which a
                    728:  * configuration record was already created.  The routine has the
                    729:  * responsibility of creating a new record and merging the contents of the
                    730:  * other two into it appropriately.  If the module doesn't declare a merge
                    731:  * routine, the record for the closest ancestor location (that has one) is
                    732:  * used exclusively.
                    733:  *
                    734:  * The routine MUST NOT modify any of its arguments!
                    735:  *
                    736:  * The return value is a pointer to the created module-specific structure
                    737:  * containing the merged values.
                    738:  */
1.8       paf       739: static void *parser_merge_dir_config(pool *p, void *parent_conf,
1.2       paf       740:                                       void *newloc_conf)
                    741: {
                    742: 
1.8       paf       743:     Parser_module_config *merged_config = (Parser_module_config *) ap_pcalloc(p, sizeof(Parser_module_config));
                    744:     Parser_module_config *pconf = (Parser_module_config *) parent_conf;
                    745:     Parser_module_config *nconf = (Parser_module_config *) newloc_conf;
1.2       paf       746:     char *note;
                    747: 
1.8       paf       748: 
                    749:        // always from parent
                    750:     merged_config->parser_root_auto_path = ap_pstrdup(p, pconf->parser_root_auto_path);
1.2       paf       751:     /*
                    752:      * Some things get copied directly from the more-specific record, rather
                    753:      * than getting merged.
                    754:      */
1.8       paf       755:     merged_config->parser_site_auto_path = ap_pstrdup(p, nconf->parser_site_auto_path?
                    756:                nconf->parser_site_auto_path:pconf->parser_site_auto_path);
                    757: 
1.2       paf       758:     merged_config->loc = ap_pstrdup(p, nconf->loc);
                    759:     /*
                    760:      * If we're merging records for two different types of environment (server
                    761:      * and directory), mark the new record appropriately.  Otherwise, inherit
                    762:      * the current value.
                    763:      */
                    764:     merged_config->cmode =
                    765:         (pconf->cmode == nconf->cmode) ? pconf->cmode : CONFIG_MODE_COMBO;
                    766:     /*
                    767:      * Now just record our being called in the trace list.  Include the
                    768:      * locations we were asked to merge.
                    769:      */
1.8       paf       770:     note = ap_pstrcat(p, "parser_merge_dir_config(\"", pconf->loc, "\",\"",
1.2       paf       771:                    nconf->loc, "\")", NULL);
                    772:     trace_add(NULL, NULL, merged_config, note);
                    773:     return (void *) merged_config;
                    774: }
                    775: 
                    776: /*
                    777:  * This function gets called to create a per-server configuration
                    778:  * record.  It will always be called for the "default" server.
                    779:  *
                    780:  * The return value is a pointer to the created module-specific
                    781:  * structure.
                    782:  */
1.8       paf       783: static void *parser_create_server_config(pool *p, server_rec *s)
1.2       paf       784: {
                    785: 
1.8       paf       786:     Parser_module_config *cfg;
1.2       paf       787:     char *sname = s->server_hostname;
                    788: 
                    789:     /*
1.8       paf       790:      * As with the parser_create_dir_config() reoutine, we allocate and fill
1.2       paf       791:      * in an empty record.
                    792:      */
1.8       paf       793:     cfg = (Parser_module_config *) ap_pcalloc(p, sizeof(Parser_module_config));
                    794:     cfg->parser_root_auto_path = 0;
                    795:        cfg->parser_site_auto_path = 0;
1.2       paf       796:     cfg->cmode = CONFIG_MODE_SERVER;
                    797:     /*
                    798:      * Note that we were called in the trace list.
                    799:      */
                    800:     sname = (sname != NULL) ? sname : "";
                    801:     cfg->loc = ap_pstrcat(p, "SVR(", sname, ")", NULL);
1.8       paf       802:     trace_add(s, NULL, cfg, "parser_create_server_config()");
1.2       paf       803:     return (void *) cfg;
                    804: }
                    805: 
                    806: /*
                    807:  * This function gets called to merge two per-server configuration
                    808:  * records.  This is typically done to cope with things like virtual hosts and
                    809:  * the default server configuration  The routine has the responsibility of
                    810:  * creating a new record and merging the contents of the other two into it
                    811:  * appropriately.  If the module doesn't declare a merge routine, the more
                    812:  * specific existing record is used exclusively.
                    813:  *
                    814:  * The routine MUST NOT modify any of its arguments!
                    815:  *
                    816:  * The return value is a pointer to the created module-specific structure
                    817:  * containing the merged values.
                    818:  */
1.8       paf       819: static void *parser_merge_server_config(pool *p, void *server1_conf,
1.2       paf       820:                                          void *server2_conf)
                    821: {
                    822: 
1.8       paf       823:     Parser_module_config *merged_config = (Parser_module_config *) ap_pcalloc(p, sizeof(Parser_module_config));
                    824:     Parser_module_config *s1conf = (Parser_module_config *) server1_conf;
                    825:     Parser_module_config *s2conf = (Parser_module_config *) server2_conf;
1.2       paf       826: 
                    827:     /*
                    828:      * Our inheritance rules are our own, and part of our module's semantics.
                    829:      * Basically, just note whence we came.
                    830:      */
                    831:     merged_config->cmode =
                    832:         (s1conf->cmode == s2conf->cmode) ? s1conf->cmode : CONFIG_MODE_COMBO;
1.8       paf       833:     merged_config->parser_root_auto_path = ap_pstrdup(p, s2conf->parser_root_auto_path?
                    834:                s2conf->parser_root_auto_path:s1conf->parser_root_auto_path);
                    835:     merged_config->parser_site_auto_path = ap_pstrdup(p, s2conf->parser_site_auto_path?
                    836:                s2conf->parser_site_auto_path:s1conf->parser_site_auto_path);
1.2       paf       837:     merged_config->loc = ap_pstrdup(p, s2conf->loc);
1.8       paf       838:  
                    839:        return (void *) merged_config;
1.2       paf       840: }
                    841: 
                    842: /*
                    843:  * This routine gives our module an opportunity to translate the URI into an
                    844:  * actual filename.  If we don't do anything special, the server's default
                    845:  * rules (Alias directives and the like) will continue to be followed.
                    846:  *
                    847:  * The return value is OK, DECLINED, or HTTP_mumble.  If we return OK, no
                    848:  * further modules are called for this phase.
                    849:  */
1.8       paf       850: static int parser_translate_handler(request_rec *r)
1.2       paf       851: {
                    852: 
1.8       paf       853:     Parser_module_config *cfg;
1.2       paf       854: 
                    855:     cfg = our_dconfig(r);
                    856:     /*
                    857:      * We don't actually *do* anything here, except note the fact that we were
                    858:      * called.
                    859:      */
1.8       paf       860:     trace_add(r->server, r, cfg, "parser_translate_handler()");
1.2       paf       861:     return DECLINED;
                    862: }
                    863: 
                    864: /*
                    865:  * This routine is called to check the authentication information sent with
                    866:  * the request (such as looking up the user in a database and verifying that
                    867:  * the [encrypted] password sent matches the one in the database).
                    868:  *
                    869:  * The return value is OK, DECLINED, or some HTTP_mumble error (typically
                    870:  * HTTP_UNAUTHORIZED).  If we return OK, no other modules are given a chance
                    871:  * at the request during this phase.
                    872:  */
1.8       paf       873: static int parser_check_user_id(request_rec *r)
1.2       paf       874: {
                    875: 
1.8       paf       876:     Parser_module_config *cfg;
1.2       paf       877: 
                    878:     cfg = our_dconfig(r);
                    879:     /*
                    880:      * Don't do anything except log the call.
                    881:      */
1.8       paf       882:     trace_add(r->server, r, cfg, "parser_check_user_id()");
1.2       paf       883:     return DECLINED;
                    884: }
                    885: 
                    886: /*
                    887:  * This routine is called to check to see if the resource being requested
                    888:  * requires authorisation.
                    889:  *
                    890:  * The return value is OK, DECLINED, or HTTP_mumble.  If we return OK, no
                    891:  * other modules are called during this phase.
                    892:  *
                    893:  * If *all* modules return DECLINED, the request is aborted with a server
                    894:  * error.
                    895:  */
1.8       paf       896: static int parser_auth_checker(request_rec *r)
1.2       paf       897: {
                    898: 
1.8       paf       899:     Parser_module_config *cfg;
1.2       paf       900: 
                    901:     cfg = our_dconfig(r);
                    902:     /*
                    903:      * Log the call and return OK, or access will be denied (even though we
                    904:      * didn't actually do anything).
                    905:      */
1.8       paf       906:     trace_add(r->server, r, cfg, "parser_auth_checker()");
1.2       paf       907:     return DECLINED;
                    908: }
                    909: 
                    910: /*
                    911:  * This routine is called to check for any module-specific restrictions placed
                    912:  * upon the requested resource.  (See the mod_access module for an example.)
                    913:  *
                    914:  * The return value is OK, DECLINED, or HTTP_mumble.  All modules with an
                    915:  * handler for this phase are called regardless of whether their predecessors
                    916:  * return OK or DECLINED.  The first one to return any other status, however,
                    917:  * will abort the sequence (and the request) as usual.
                    918:  */
1.8       paf       919: static int parser_access_checker(request_rec *r)
1.2       paf       920: {
                    921: 
1.8       paf       922:     Parser_module_config *cfg;
1.2       paf       923: 
                    924:     cfg = our_dconfig(r);
1.8       paf       925:     trace_add(r->server, r, cfg, "parser_access_checker()");
1.2       paf       926:     return DECLINED;
                    927: }
                    928: 
                    929: /*--------------------------------------------------------------------------*/
                    930: /*                                                                          */
                    931: /* All of the routines have been declared now.  Here's the list of          */
                    932: /* directives specific to our module, and information about where they      */
                    933: /* may appear and how the command parser should pass them to us for         */
                    934: /* processing.  Note that care must be taken to ensure that there are NO    */
                    935: /* collisions of directive names between modules.                           */
                    936: /*                                                                          */
                    937: /*--------------------------------------------------------------------------*/
                    938: /* 
                    939:  * List of directives specific to our module.
                    940:  */
1.8       paf       941: static const command_rec parser_cmds[] =
1.2       paf       942: {
                    943:     {
1.8       paf       944:         "parser_root_auto_path",              /* directive name */
                    945:         (const char *(*)(void))((void *)cmd_parser_auto_path), // config action routine
                    946:         (void*)true,                   /* argument to include in call */
                    947:         (int)(ACCESS_CONF|RSRC_CONF),             /* where available */
                    948:         TAKE1,                /* arguments */
                    949:         "Parser root auto.p filespec (Admin)" // directive description
                    950:     },
                    951:     {
                    952:         "parser_site_auto_path",              /* directive name */
                    953:         (const char *(*)(void))((void *)cmd_parser_auto_path), // config action routine
                    954:         (void*)false,                   /* argument to include in call */
                    955:         (int)(OR_OPTIONS),             /* where available */
                    956:         TAKE1,                /* arguments */
                    957:         "Parser site auto.p filespec" // directive description
1.2       paf       958:     },
                    959:     {NULL}
                    960: };
                    961: 
                    962: /*--------------------------------------------------------------------------*/
                    963: /*                                                                          */
                    964: /* Now the list of content handlers available from this module.             */
                    965: /*                                                                          */
                    966: /*--------------------------------------------------------------------------*/
                    967: /* 
                    968:  * List of content handlers our module supplies.  Each handler is defined by
                    969:  * two parts: a name by which it can be referenced (such as by
                    970:  * {Add,Set}Handler), and the actual routine name.  The list is terminated by
                    971:  * a NULL block, since it can be of variable length.
                    972:  *
                    973:  * Note that content-handlers are invoked on a most-specific to least-specific
                    974:  * basis; that is, a handler that is declared for "text/plain" will be
                    975:  * invoked before one that was declared for "text / *".  Note also that
                    976:  * if a content-handler returns anything except DECLINED, no other
                    977:  * content-handlers will be called.
                    978:  */
1.8       paf       979: static const handler_rec parser_handlers[] =
1.2       paf       980: {
1.8       paf       981:     {"parser3-handler", parser_handler},
1.2       paf       982:     {NULL}
                    983: };
                    984: 
                    985: /*--------------------------------------------------------------------------*/
                    986: /*                                                                          */
                    987: /* Finally, the list of callback routines and data structures that          */
                    988: /* provide the hooks into our module from the other parts of the server.    */
                    989: /*                                                                          */
                    990: /*--------------------------------------------------------------------------*/
                    991: /* 
                    992:  * Module definition for configuration.  If a particular callback is not
                    993:  * needed, replace its routine name below with the word NULL.
                    994:  *
                    995:  * The number in brackets indicates the order in which the routine is called
                    996:  * during request processing.  Note that not all routines are necessarily
                    997:  * called (such as if a resource doesn't have access restrictions).
                    998:  */
                    999: module MODULE_VAR_EXPORT parser3_module =
                   1000: {
                   1001:     STANDARD_MODULE_STUFF,
1.8       paf      1002:     parser_init,               /* module initializer */
                   1003:     parser_create_dir_config,  /* per-directory config creator */
                   1004:     parser_merge_dir_config,   /* dir config merger */
                   1005:     parser_create_server_config,       /* server config creator */
                   1006:     parser_merge_server_config,        /* server config merger */
                   1007:     parser_cmds,               /* command table */
                   1008:     parser_handlers,           /* [9] list of handlers */
                   1009:     parser_translate_handler,  /* [2] filename-to-URI translation */
                   1010:     parser_check_user_id,      /* [5] check/validate user_id */
                   1011:     parser_auth_checker,       /* [6] check user_id is valid *here* */
                   1012:     parser_access_checker,     /* [4] check access by host address */
1.5       paf      1013:     0,                          /* [7] MIME type checker/setter */
                   1014:     0,                          /* [8] fixups */
1.8       paf      1015:     0                          /* [10] logger */
1.2       paf      1016: #if MODULE_MAGIC_NUMBER >= 19970103
1.5       paf      1017:     ,0                          /* [3] header parser */
1.2       paf      1018: #endif
                   1019: #if MODULE_MAGIC_NUMBER >= 19970719
1.7       paf      1020:     ,0                          /* process initializer */
1.2       paf      1021: #endif
                   1022: #if MODULE_MAGIC_NUMBER >= 19970728
1.8       paf      1023:     ,parser_child_exit         /* process exit/cleanup */
1.2       paf      1024: #endif
                   1025: #if MODULE_MAGIC_NUMBER >= 19970902
1.5       paf      1026:     ,0   /* [1] post read_request handling */
1.2       paf      1027: #endif
                   1028: };

E-mail: