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

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.10    ! paf         8:        $Id: mod_parser3.C,v 1.9 2001/03/22 12:10:01 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.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);
1.10    ! paf       508:        pool.set_context(r);
1.2       paf       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:                
                    554:                // process the request
                    555:                request.core(
1.8       paf       556:                        dcfg->parser_root_auto_path, true, // /path/to/admin/auto.p
                    557:                        dcfg->parser_site_auto_path, true, // /path/to/site/auto.p
1.2       paf       558:                        r->header_only!=0);
                    559:                // no actions with request' data past this point
                    560:                // request.exception not not handled here, but all
                    561:                // request' data are associated with it's pool=exception
                    562: 
                    563:                // successful finish
                    564:        } PCATCH(e) { // global problem 
                    565:                const char *body=e.comment();
                    566:                int content_length=strlen(body);
                    567: 
                    568:                // prepare header
                    569:                (*service_funcs.add_header_attribute)(pool, "content-type", "text/plain");
                    570:                char content_length_cstr[MAX_NUMBER];
                    571:                snprintf(content_length_cstr, MAX_NUMBER, "%d", content_length);
                    572:                (*service_funcs.add_header_attribute)(pool, "content-length", 
                    573:                        content_length_cstr);
                    574: 
                    575:                // send header
                    576:                (*service_funcs.send_header)(pool);
                    577: 
                    578:                // send body
                    579:                if(!r->header_only)
                    580:                        (*service_funcs.send_body)(pool, body, content_length);
                    581: 
                    582:                // unsuccessful finish
                    583:        }
                    584:        PEND_CATCH
                    585: 
                    586:     /*
                    587:      * We did what we wanted to do, so tell the rest of the server we
                    588:      * succeeded.
                    589:      */
                    590:     return OK;
                    591: }
                    592: 
                    593: /*--------------------------------------------------------------------------*/
                    594: /*                                                                          */
                    595: /* Now let's declare routines for each of the callback phase in order.      */
                    596: /* (That's the order in which they're listed in the callback list, *not     */
                    597: /* the order in which the server calls them!  See the command_rec           */
                    598: /* declaration near the bottom of this file.)  Note that these may be       */
                    599: /* called for situations that don't relate primarily to our function - in   */
                    600: /* other words, the fixup handler shouldn't assume that the request has     */
                    601: /* to do with "example" stuff.                                              */
                    602: /*                                                                          */
                    603: /* With the exception of the content handler, all of our routines will be   */
                    604: /* called for each request, unless an earlier handler from another module   */
                    605: /* aborted the sequence.                                                    */
                    606: /*                                                                          */
                    607: /* Handlers that are declared as "int" can return the following:            */
                    608: /*                                                                          */
                    609: /*  OK          Handler accepted the request and did its thing with it.     */
                    610: /*  DECLINED    Handler took no action.                                     */
                    611: /*  HTTP_mumble Handler looked at request and found it wanting.             */
                    612: /*                                                                          */
                    613: /* What the server does after calling a module handler depends upon the     */
                    614: /* handler's return value.  In all cases, if the handler returns            */
                    615: /* DECLINED, the server will continue to the next module with an handler    */
                    616: /* for the current phase.  However, if the handler return a non-OK,         */
                    617: /* non-DECLINED status, the server aborts the request right there.  If      */
                    618: /* the handler returns OK, the server's next action is phase-specific;      */
                    619: /* see the individual handler comments below for details.                   */
                    620: /*                                                                          */
                    621: /*--------------------------------------------------------------------------*/
                    622: /* 
                    623:  * This function is called during server initialisation.  Any information
                    624:  * that needs to be recorded must be in static cells, since there's no
                    625:  * configuration record.
                    626:  *
                    627:  * There is no return value.
                    628:  */
1.9       paf       629: /// @todo answer this: "why when it's called second time all globals are cleared?"
1.8       paf       630: static void parser_init(server_rec *s, pool *p)
1.2       paf       631: {
1.8       paf       632: #if MODULE_MAGIC_NUMBER >= 19980527
                    633:        ap_add_version_component("Parser/" PARSER_VERSION);
                    634: #endif 
                    635:        
1.2       paf       636:        static bool globals_inited=false;
                    637:        if(globals_inited)
                    638:                return;
                    639:        globals_inited=true;
                    640: 
1.6       paf       641:        static Pool pool; // global pool
1.2       paf       642:        pool.set_storage(p);
                    643:        PTRY {
                    644:                // init global variables
                    645:                globals_init(pool);
                    646:                
                    647:                //...
                    648:        } PCATCH(e) { // global problem 
                    649:                const char *body=e.comment();
1.10    ! paf       650:                // TODO: somehow report that error
1.2       paf       651:        }
                    652:        PEND_CATCH
                    653: }
                    654: 
                    655: /* 
                    656:  * This function is called when an heavy-weight process (such as a child) is
                    657:  * being run down or destroyed.  As with the child-initialisation function,
                    658:  * any information that needs to be recorded must be in static cells, since
                    659:  * there's no configuration record.
                    660:  *
                    661:  * There is no return value.
                    662:  */
                    663: 
                    664: /*
                    665:  * All our process-death routine does is add its trace to the log.
                    666:  */
1.8       paf       667: static void parser_child_exit(server_rec *s, pool *p)
1.2       paf       668: {
                    669: 
                    670:     char *note;
                    671:     char *sname = s->server_hostname;
                    672: 
                    673:     /*
                    674:      * The arbitrary text we add to our trace entry indicates for which server
                    675:      * we're being called.
                    676:      */
                    677:     sname = (sname != NULL) ? sname : "";
1.8       paf       678:     note = ap_pstrcat(p, "parser_child_exit(", sname, ")", NULL);
1.2       paf       679:     trace_add(s, NULL, NULL, note);
                    680: }
                    681: 
                    682: /*
                    683:  * This function gets called to create a per-directory configuration
                    684:  * record.  This will be called for the "default" server environment, and for
                    685:  * each directory for which the parser finds any of our directives applicable.
                    686:  * If a directory doesn't have any of our directives involved (i.e., they
                    687:  * aren't in the .htaccess file, or a <Location>, <Directory>, or related
                    688:  * block), this routine will *not* be called - the configuration for the
                    689:  * closest ancestor is used.
                    690:  *
                    691:  * The return value is a pointer to the created module-specific
                    692:  * structure.
                    693:  */
1.8       paf       694: static void *parser_create_dir_config(pool *p, char *dirspec)
1.2       paf       695: {
                    696: 
1.8       paf       697:     Parser_module_config *cfg;
1.2       paf       698:     char *dname = dirspec;
                    699: 
                    700:     /*
                    701:      * Allocate the space for our record from the pool supplied.
                    702:      */
1.8       paf       703:     cfg = (Parser_module_config *) ap_pcalloc(p, sizeof(Parser_module_config));
1.2       paf       704:     /*
                    705:      * Now fill in the defaults.  If there are any `parent' configuration
                    706:      * records, they'll get merged as part of a separate callback.
                    707:      */
1.8       paf       708:     cfg->parser_root_auto_path = 0;
                    709:        cfg->parser_site_auto_path = 0;
1.2       paf       710:     cfg->cmode = CONFIG_MODE_DIRECTORY;
                    711:     /*
                    712:      * Finally, add our trace to the callback list.
                    713:      */
                    714:     dname = (dname != NULL) ? dname : "";
                    715:     cfg->loc = ap_pstrcat(p, "DIR(", dname, ")", NULL);
1.8       paf       716:     trace_add(NULL, NULL, cfg, "parser_create_dir_config()");
1.2       paf       717:     return (void *) cfg;
                    718: }
                    719: 
                    720: /*
                    721:  * This function gets called to merge two per-directory configuration
                    722:  * records.  This is typically done to cope with things like .htaccess files
                    723:  * or <Location> directives for directories that are beneath one for which a
                    724:  * configuration record was already created.  The routine has the
                    725:  * responsibility of creating a new record and merging the contents of the
                    726:  * other two into it appropriately.  If the module doesn't declare a merge
                    727:  * routine, the record for the closest ancestor location (that has one) is
                    728:  * used exclusively.
                    729:  *
                    730:  * The routine MUST NOT modify any of its arguments!
                    731:  *
                    732:  * The return value is a pointer to the created module-specific structure
                    733:  * containing the merged values.
                    734:  */
1.8       paf       735: static void *parser_merge_dir_config(pool *p, void *parent_conf,
1.2       paf       736:                                       void *newloc_conf)
                    737: {
                    738: 
1.8       paf       739:     Parser_module_config *merged_config = (Parser_module_config *) ap_pcalloc(p, sizeof(Parser_module_config));
                    740:     Parser_module_config *pconf = (Parser_module_config *) parent_conf;
                    741:     Parser_module_config *nconf = (Parser_module_config *) newloc_conf;
1.2       paf       742:     char *note;
                    743: 
1.8       paf       744: 
                    745:        // always from parent
                    746:     merged_config->parser_root_auto_path = ap_pstrdup(p, pconf->parser_root_auto_path);
1.2       paf       747:     /*
                    748:      * Some things get copied directly from the more-specific record, rather
                    749:      * than getting merged.
                    750:      */
1.8       paf       751:     merged_config->parser_site_auto_path = ap_pstrdup(p, nconf->parser_site_auto_path?
                    752:                nconf->parser_site_auto_path:pconf->parser_site_auto_path);
                    753: 
1.2       paf       754:     merged_config->loc = ap_pstrdup(p, nconf->loc);
                    755:     /*
                    756:      * If we're merging records for two different types of environment (server
                    757:      * and directory), mark the new record appropriately.  Otherwise, inherit
                    758:      * the current value.
                    759:      */
                    760:     merged_config->cmode =
                    761:         (pconf->cmode == nconf->cmode) ? pconf->cmode : CONFIG_MODE_COMBO;
                    762:     /*
                    763:      * Now just record our being called in the trace list.  Include the
                    764:      * locations we were asked to merge.
                    765:      */
1.8       paf       766:     note = ap_pstrcat(p, "parser_merge_dir_config(\"", pconf->loc, "\",\"",
1.2       paf       767:                    nconf->loc, "\")", NULL);
                    768:     trace_add(NULL, NULL, merged_config, note);
                    769:     return (void *) merged_config;
                    770: }
                    771: 
                    772: /*
                    773:  * This function gets called to create a per-server configuration
                    774:  * record.  It will always be called for the "default" server.
                    775:  *
                    776:  * The return value is a pointer to the created module-specific
                    777:  * structure.
                    778:  */
1.8       paf       779: static void *parser_create_server_config(pool *p, server_rec *s)
1.2       paf       780: {
                    781: 
1.8       paf       782:     Parser_module_config *cfg;
1.2       paf       783:     char *sname = s->server_hostname;
                    784: 
                    785:     /*
1.8       paf       786:      * As with the parser_create_dir_config() reoutine, we allocate and fill
1.2       paf       787:      * in an empty record.
                    788:      */
1.8       paf       789:     cfg = (Parser_module_config *) ap_pcalloc(p, sizeof(Parser_module_config));
                    790:     cfg->parser_root_auto_path = 0;
                    791:        cfg->parser_site_auto_path = 0;
1.2       paf       792:     cfg->cmode = CONFIG_MODE_SERVER;
                    793:     /*
                    794:      * Note that we were called in the trace list.
                    795:      */
                    796:     sname = (sname != NULL) ? sname : "";
                    797:     cfg->loc = ap_pstrcat(p, "SVR(", sname, ")", NULL);
1.8       paf       798:     trace_add(s, NULL, cfg, "parser_create_server_config()");
1.2       paf       799:     return (void *) cfg;
                    800: }
                    801: 
                    802: /*
                    803:  * This function gets called to merge two per-server configuration
                    804:  * records.  This is typically done to cope with things like virtual hosts and
                    805:  * the default server configuration  The routine has the responsibility of
                    806:  * creating a new record and merging the contents of the other two into it
                    807:  * appropriately.  If the module doesn't declare a merge routine, the more
                    808:  * specific existing record is used exclusively.
                    809:  *
                    810:  * The routine MUST NOT modify any of its arguments!
                    811:  *
                    812:  * The return value is a pointer to the created module-specific structure
                    813:  * containing the merged values.
                    814:  */
1.8       paf       815: static void *parser_merge_server_config(pool *p, void *server1_conf,
1.2       paf       816:                                          void *server2_conf)
                    817: {
                    818: 
1.8       paf       819:     Parser_module_config *merged_config = (Parser_module_config *) ap_pcalloc(p, sizeof(Parser_module_config));
                    820:     Parser_module_config *s1conf = (Parser_module_config *) server1_conf;
                    821:     Parser_module_config *s2conf = (Parser_module_config *) server2_conf;
1.2       paf       822: 
                    823:     /*
                    824:      * Our inheritance rules are our own, and part of our module's semantics.
                    825:      * Basically, just note whence we came.
                    826:      */
                    827:     merged_config->cmode =
                    828:         (s1conf->cmode == s2conf->cmode) ? s1conf->cmode : CONFIG_MODE_COMBO;
1.8       paf       829:     merged_config->parser_root_auto_path = ap_pstrdup(p, s2conf->parser_root_auto_path?
                    830:                s2conf->parser_root_auto_path:s1conf->parser_root_auto_path);
                    831:     merged_config->parser_site_auto_path = ap_pstrdup(p, s2conf->parser_site_auto_path?
                    832:                s2conf->parser_site_auto_path:s1conf->parser_site_auto_path);
1.2       paf       833:     merged_config->loc = ap_pstrdup(p, s2conf->loc);
1.8       paf       834:  
                    835:        return (void *) merged_config;
1.2       paf       836: }
                    837: 
                    838: /*
                    839:  * This routine gives our module an opportunity to translate the URI into an
                    840:  * actual filename.  If we don't do anything special, the server's default
                    841:  * rules (Alias directives and the like) will continue to be followed.
                    842:  *
                    843:  * The return value is OK, DECLINED, or HTTP_mumble.  If we return OK, no
                    844:  * further modules are called for this phase.
                    845:  */
1.8       paf       846: static int parser_translate_handler(request_rec *r)
1.2       paf       847: {
                    848: 
1.8       paf       849:     Parser_module_config *cfg;
1.2       paf       850: 
                    851:     cfg = our_dconfig(r);
                    852:     /*
                    853:      * We don't actually *do* anything here, except note the fact that we were
                    854:      * called.
                    855:      */
1.8       paf       856:     trace_add(r->server, r, cfg, "parser_translate_handler()");
1.2       paf       857:     return DECLINED;
                    858: }
                    859: 
                    860: /*
                    861:  * This routine is called to check the authentication information sent with
                    862:  * the request (such as looking up the user in a database and verifying that
                    863:  * the [encrypted] password sent matches the one in the database).
                    864:  *
                    865:  * The return value is OK, DECLINED, or some HTTP_mumble error (typically
                    866:  * HTTP_UNAUTHORIZED).  If we return OK, no other modules are given a chance
                    867:  * at the request during this phase.
                    868:  */
1.8       paf       869: static int parser_check_user_id(request_rec *r)
1.2       paf       870: {
                    871: 
1.8       paf       872:     Parser_module_config *cfg;
1.2       paf       873: 
                    874:     cfg = our_dconfig(r);
                    875:     /*
                    876:      * Don't do anything except log the call.
                    877:      */
1.8       paf       878:     trace_add(r->server, r, cfg, "parser_check_user_id()");
1.2       paf       879:     return DECLINED;
                    880: }
                    881: 
                    882: /*
                    883:  * This routine is called to check to see if the resource being requested
                    884:  * requires authorisation.
                    885:  *
                    886:  * The return value is OK, DECLINED, or HTTP_mumble.  If we return OK, no
                    887:  * other modules are called during this phase.
                    888:  *
                    889:  * If *all* modules return DECLINED, the request is aborted with a server
                    890:  * error.
                    891:  */
1.8       paf       892: static int parser_auth_checker(request_rec *r)
1.2       paf       893: {
                    894: 
1.8       paf       895:     Parser_module_config *cfg;
1.2       paf       896: 
                    897:     cfg = our_dconfig(r);
                    898:     /*
                    899:      * Log the call and return OK, or access will be denied (even though we
                    900:      * didn't actually do anything).
                    901:      */
1.8       paf       902:     trace_add(r->server, r, cfg, "parser_auth_checker()");
1.2       paf       903:     return DECLINED;
                    904: }
                    905: 
                    906: /*
                    907:  * This routine is called to check for any module-specific restrictions placed
                    908:  * upon the requested resource.  (See the mod_access module for an example.)
                    909:  *
                    910:  * The return value is OK, DECLINED, or HTTP_mumble.  All modules with an
                    911:  * handler for this phase are called regardless of whether their predecessors
                    912:  * return OK or DECLINED.  The first one to return any other status, however,
                    913:  * will abort the sequence (and the request) as usual.
                    914:  */
1.8       paf       915: static int parser_access_checker(request_rec *r)
1.2       paf       916: {
                    917: 
1.8       paf       918:     Parser_module_config *cfg;
1.2       paf       919: 
                    920:     cfg = our_dconfig(r);
1.8       paf       921:     trace_add(r->server, r, cfg, "parser_access_checker()");
1.2       paf       922:     return DECLINED;
                    923: }
                    924: 
                    925: /*--------------------------------------------------------------------------*/
                    926: /*                                                                          */
                    927: /* All of the routines have been declared now.  Here's the list of          */
                    928: /* directives specific to our module, and information about where they      */
                    929: /* may appear and how the command parser should pass them to us for         */
                    930: /* processing.  Note that care must be taken to ensure that there are NO    */
                    931: /* collisions of directive names between modules.                           */
                    932: /*                                                                          */
                    933: /*--------------------------------------------------------------------------*/
                    934: /* 
                    935:  * List of directives specific to our module.
                    936:  */
1.8       paf       937: static const command_rec parser_cmds[] =
1.2       paf       938: {
                    939:     {
1.8       paf       940:         "parser_root_auto_path",              /* directive name */
                    941:         (const char *(*)(void))((void *)cmd_parser_auto_path), // config action routine
                    942:         (void*)true,                   /* argument to include in call */
                    943:         (int)(ACCESS_CONF|RSRC_CONF),             /* where available */
                    944:         TAKE1,                /* arguments */
                    945:         "Parser root auto.p filespec (Admin)" // directive description
                    946:     },
                    947:     {
                    948:         "parser_site_auto_path",              /* directive name */
                    949:         (const char *(*)(void))((void *)cmd_parser_auto_path), // config action routine
                    950:         (void*)false,                   /* argument to include in call */
                    951:         (int)(OR_OPTIONS),             /* where available */
                    952:         TAKE1,                /* arguments */
                    953:         "Parser site auto.p filespec" // directive description
1.2       paf       954:     },
                    955:     {NULL}
                    956: };
                    957: 
                    958: /*--------------------------------------------------------------------------*/
                    959: /*                                                                          */
                    960: /* Now the list of content handlers available from this module.             */
                    961: /*                                                                          */
                    962: /*--------------------------------------------------------------------------*/
                    963: /* 
                    964:  * List of content handlers our module supplies.  Each handler is defined by
                    965:  * two parts: a name by which it can be referenced (such as by
                    966:  * {Add,Set}Handler), and the actual routine name.  The list is terminated by
                    967:  * a NULL block, since it can be of variable length.
                    968:  *
                    969:  * Note that content-handlers are invoked on a most-specific to least-specific
                    970:  * basis; that is, a handler that is declared for "text/plain" will be
                    971:  * invoked before one that was declared for "text / *".  Note also that
                    972:  * if a content-handler returns anything except DECLINED, no other
                    973:  * content-handlers will be called.
                    974:  */
1.8       paf       975: static const handler_rec parser_handlers[] =
1.2       paf       976: {
1.8       paf       977:     {"parser3-handler", parser_handler},
1.2       paf       978:     {NULL}
                    979: };
                    980: 
                    981: /*--------------------------------------------------------------------------*/
                    982: /*                                                                          */
                    983: /* Finally, the list of callback routines and data structures that          */
                    984: /* provide the hooks into our module from the other parts of the server.    */
                    985: /*                                                                          */
                    986: /*--------------------------------------------------------------------------*/
                    987: /* 
                    988:  * Module definition for configuration.  If a particular callback is not
                    989:  * needed, replace its routine name below with the word NULL.
                    990:  *
                    991:  * The number in brackets indicates the order in which the routine is called
                    992:  * during request processing.  Note that not all routines are necessarily
                    993:  * called (such as if a resource doesn't have access restrictions).
                    994:  */
                    995: module MODULE_VAR_EXPORT parser3_module =
                    996: {
                    997:     STANDARD_MODULE_STUFF,
1.8       paf       998:     parser_init,               /* module initializer */
                    999:     parser_create_dir_config,  /* per-directory config creator */
                   1000:     parser_merge_dir_config,   /* dir config merger */
                   1001:     parser_create_server_config,       /* server config creator */
                   1002:     parser_merge_server_config,        /* server config merger */
                   1003:     parser_cmds,               /* command table */
                   1004:     parser_handlers,           /* [9] list of handlers */
                   1005:     parser_translate_handler,  /* [2] filename-to-URI translation */
                   1006:     parser_check_user_id,      /* [5] check/validate user_id */
                   1007:     parser_auth_checker,       /* [6] check user_id is valid *here* */
                   1008:     parser_access_checker,     /* [4] check access by host address */
1.5       paf      1009:     0,                          /* [7] MIME type checker/setter */
                   1010:     0,                          /* [8] fixups */
1.8       paf      1011:     0                          /* [10] logger */
1.2       paf      1012: #if MODULE_MAGIC_NUMBER >= 19970103
1.5       paf      1013:     ,0                          /* [3] header parser */
1.2       paf      1014: #endif
                   1015: #if MODULE_MAGIC_NUMBER >= 19970719
1.7       paf      1016:     ,0                          /* process initializer */
1.2       paf      1017: #endif
                   1018: #if MODULE_MAGIC_NUMBER >= 19970728
1.8       paf      1019:     ,parser_child_exit         /* process exit/cleanup */
1.2       paf      1020: #endif
                   1021: #if MODULE_MAGIC_NUMBER >= 19970902
1.5       paf      1022:     ,0   /* [1] post read_request handling */
1.2       paf      1023: #endif
                   1024: };

E-mail: