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

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

E-mail: