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

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

E-mail: