1/* Licensed to the Apache Software Foundation (ASF) under one or more
2 * contributor license agreements.  See the NOTICE file distributed with
3 * this work for additional information regarding copyright ownership.
4 * The ASF licenses this file to You under the Apache License, Version 2.0
5 * (the "License"); you may not use this file except in compliance with
6 * the License.  You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/*
18 * Apache example module.  Provide demonstrations of how modules do things.
19 * It is not meant to be used in a production server.  Since it participates
20 * in all of the processing phases, it could conceivable interfere with
21 * the proper operation of other modules -- particularly the ones related
22 * to security.
23 *
24 * In the interest of brevity, all functions and structures internal to
25 * this module, but which may have counterparts in *real* modules, are
26 * prefixed with 'x_' instead of 'example_'.
27 *
28 * To use mod_example_hooks, configure the Apache build with
29 * --enable-example and compile.  Set up a <Location> block in your
30 * configuration file like so:
31 *
32 * <Location /example>
33 *    SetHandler example-hooks-handler
34 * </Location>
35 *
36 * When you look at that location on your server, you will see a backtrace of
37 * the callbacks that have been invoked up to that point.  See the ErrorLog for
38 * more information on code paths that  touch mod_example_hooks.
39 *
40 * IMPORTANT NOTES
41 * ===============
42 *
43 * Do NOT use this module on a production server. It attaches itself to every
44 * phase of the server runtime operations including startup, shutdown and
45 * request processing, and produces copious amounts of logging data.  This will
46 * negatively affect server performance.
47 *
48 * Do NOT use mod_example_hooks as the basis for your own code.  This module
49 * implements every callback hook offered by the Apache core, and your
50 * module will almost certainly not have to implement this much.  If you
51 * want a simple module skeleton to start development, use apxs -g.
52 *
53 * XXX TO DO XXX
54 * =============
55 *
56 * * Enable HTML backtrace entries for more callbacks that are not directly
57 *   associated with a request
58 * * Make sure every callback that posts an HTML backtrace entry does so in the *   right category, so nothing gets overwritten
59 * * Implement some logic to show what happens in the parent, and what in the
60 *   child(ren)
61 */
62
63#include "httpd.h"
64#include "http_config.h"
65#include "http_core.h"
66#include "http_log.h"
67#include "http_main.h"
68#include "http_protocol.h"
69#include "http_request.h"
70#include "util_script.h"
71#include "http_connection.h"
72#ifdef HAVE_UNIX_SUEXEC
73#include "unixd.h"
74#endif
75#include "scoreboard.h"
76#include "mpm_common.h"
77
78#include "apr_strings.h"
79
80#include <stdio.h>
81
82/*--------------------------------------------------------------------------*/
83/*                                                                          */
84/* Data declarations.                                                       */
85/*                                                                          */
86/* Here are the static cells and structure declarations private to our      */
87/* module.                                                                  */
88/*                                                                          */
89/*--------------------------------------------------------------------------*/
90
91/*
92 * Sample configuration record.  Used for both per-directory and per-server
93 * configuration data.
94 *
95 * It's perfectly reasonable to have two different structures for the two
96 * different environments.  The same command handlers will be called for
97 * both, though, so the handlers need to be able to tell them apart.  One
98 * possibility is for both structures to start with an int which is 0 for
99 * one and 1 for the other.
100 *
101 * Note that while the per-directory and per-server configuration records are
102 * available to most of the module handlers, they should be treated as
103 * READ-ONLY by all except the command and merge handlers.  Sometimes handlers
104 * are handed a record that applies to the current location by implication or
105 * inheritance, and modifying it will change the rules for other locations.
106 */
107typedef struct x_cfg {
108    int cmode;                  /* Environment to which record applies
109                                 * (directory, server, or combination).
110                                 */
111#define CONFIG_MODE_SERVER 1
112#define CONFIG_MODE_DIRECTORY 2
113#define CONFIG_MODE_COMBO 3     /* Shouldn't ever happen. */
114    int local;                  /* Boolean: "Example" directive declared
115                                 * here?
116                                 */
117    int congenital;             /* Boolean: did we inherit an "Example"? */
118    char *trace;                /* Pointer to trace string. */
119    char *loc;                  /* Location to which this record applies. */
120} x_cfg;
121
122/*
123 * String pointer to hold the startup trace. No harm working with a global until
124 * the server is (may be) multi-threaded.
125 */
126static const char *trace = NULL;
127
128/*
129 * Declare ourselves so the configuration routines can find and know us.
130 * We'll fill it in at the end of the module.
131 */
132module AP_MODULE_DECLARE_DATA example_hooks_module;
133
134/*--------------------------------------------------------------------------*/
135/*                                                                          */
136/* The following pseudo-prototype declarations illustrate the parameters    */
137/* passed to command handlers for the different types of directive          */
138/* syntax.  If an argument was specified in the directive definition        */
139/* (look for "command_rec" below), it's available to the command handler    */
140/* via the (void *) info field in the cmd_parms argument passed to the      */
141/* handler (cmd->info for the examples below).                              */
142/*                                                                          */
143/*--------------------------------------------------------------------------*/
144
145/*
146 * Command handler for a NO_ARGS directive.  Declared in the command_rec
147 * list with
148 *   AP_INIT_NO_ARGS("directive", function, mconfig, where, help)
149 *
150 * static const char *handle_NO_ARGS(cmd_parms *cmd, void *mconfig);
151 */
152
153/*
154 * Command handler for a RAW_ARGS directive.  The "args" argument is the text
155 * of the commandline following the directive itself.  Declared in the
156 * command_rec list with
157 *   AP_INIT_RAW_ARGS("directive", function, mconfig, where, help)
158 *
159 * static const char *handle_RAW_ARGS(cmd_parms *cmd, void *mconfig,
160 *                                    const char *args);
161 */
162
163/*
164 * Command handler for a FLAG directive.  The single parameter is passed in
165 * "bool", which is either zero or not for Off or On respectively.
166 * Declared in the command_rec list with
167 *   AP_INIT_FLAG("directive", function, mconfig, where, help)
168 *
169 * static const char *handle_FLAG(cmd_parms *cmd, void *mconfig, int bool);
170 */
171
172/*
173 * Command handler for a TAKE1 directive.  The single parameter is passed in
174 * "word1".  Declared in the command_rec list with
175 *   AP_INIT_TAKE1("directive", function, mconfig, where, help)
176 *
177 * static const char *handle_TAKE1(cmd_parms *cmd, void *mconfig,
178 *                                 char *word1);
179 */
180
181/*
182 * Command handler for a TAKE2 directive.  TAKE2 commands must always have
183 * exactly two arguments.  Declared in the command_rec list with
184 *   AP_INIT_TAKE2("directive", function, mconfig, where, help)
185 *
186 * static const char *handle_TAKE2(cmd_parms *cmd, void *mconfig,
187 *                                 char *word1, char *word2);
188 */
189
190/*
191 * Command handler for a TAKE3 directive.  Like TAKE2, these must have exactly
192 * three arguments, or the parser complains and doesn't bother calling us.
193 * Declared in the command_rec list with
194 *   AP_INIT_TAKE3("directive", function, mconfig, where, help)
195 *
196 * static const char *handle_TAKE3(cmd_parms *cmd, void *mconfig,
197 *                                 char *word1, char *word2, char *word3);
198 */
199
200/*
201 * Command handler for a TAKE12 directive.  These can take either one or two
202 * arguments.
203 * - word2 is a NULL pointer if no second argument was specified.
204 * Declared in the command_rec list with
205 *   AP_INIT_TAKE12("directive", function, mconfig, where, help)
206 *
207 * static const char *handle_TAKE12(cmd_parms *cmd, void *mconfig,
208 *                                  char *word1, char *word2);
209 */
210
211/*
212 * Command handler for a TAKE123 directive.  A TAKE123 directive can be given,
213 * as might be expected, one, two, or three arguments.
214 * - word2 is a NULL pointer if no second argument was specified.
215 * - word3 is a NULL pointer if no third argument was specified.
216 * Declared in the command_rec list with
217 *   AP_INIT_TAKE123("directive", function, mconfig, where, help)
218 *
219 * static const char *handle_TAKE123(cmd_parms *cmd, void *mconfig,
220 *                                   char *word1, char *word2, char *word3);
221 */
222
223/*
224 * Command handler for a TAKE13 directive.  Either one or three arguments are
225 * permitted - no two-parameters-only syntax is allowed.
226 * - word2 and word3 are NULL pointers if only one argument was specified.
227 * Declared in the command_rec list with
228 *   AP_INIT_TAKE13("directive", function, mconfig, where, help)
229 *
230 * static const char *handle_TAKE13(cmd_parms *cmd, void *mconfig,
231 *                                  char *word1, char *word2, char *word3);
232 */
233
234/*
235 * Command handler for a TAKE23 directive.  At least two and as many as three
236 * arguments must be specified.
237 * - word3 is a NULL pointer if no third argument was specified.
238 * Declared in the command_rec list with
239 *   AP_INIT_TAKE23("directive", function, mconfig, where, help)
240 *
241 * static const char *handle_TAKE23(cmd_parms *cmd, void *mconfig,
242 *                                  char *word1, char *word2, char *word3);
243 */
244
245/*
246 * Command handler for a ITERATE directive.
247 * - Handler is called once for each of n arguments given to the directive.
248 * - word1 points to each argument in turn.
249 * Declared in the command_rec list with
250 *   AP_INIT_ITERATE("directive", function, mconfig, where, help)
251 *
252 * static const char *handle_ITERATE(cmd_parms *cmd, void *mconfig,
253 *                                   char *word1);
254 */
255
256/*
257 * Command handler for a ITERATE2 directive.
258 * - Handler is called once for each of the second and subsequent arguments
259 *   given to the directive.
260 * - word1 is the same for each call for a particular directive instance (the
261 *   first argument).
262 * - word2 points to each of the second and subsequent arguments in turn.
263 * Declared in the command_rec list with
264 *   AP_INIT_ITERATE2("directive", function, mconfig, where, help)
265 *
266 * static const char *handle_ITERATE2(cmd_parms *cmd, void *mconfig,
267 *                                    char *word1, char *word2);
268 */
269
270/*--------------------------------------------------------------------------*/
271/*                                                                          */
272/* These routines are strictly internal to this module, and support its     */
273/* operation.  They are not referenced by any external portion of the       */
274/* server.                                                                  */
275/*                                                                          */
276/*--------------------------------------------------------------------------*/
277
278/*
279 * Locate our directory configuration record for the current request.
280 */
281static x_cfg *our_dconfig(const request_rec *r)
282{
283    return (x_cfg *) ap_get_module_config(r->per_dir_config, &example_hooks_module);
284}
285
286/*
287 * The following utility routines are not used in the module. Don't
288 * compile them so -Wall doesn't complain about functions that are
289 * defined but not used.
290 */
291#if 0
292/*
293 * Locate our server configuration record for the specified server.
294 */
295static x_cfg *our_sconfig(const server_rec *s)
296{
297    return (x_cfg *) ap_get_module_config(s->module_config, &example_hooks_module);
298}
299
300/*
301 * Likewise for our configuration record for the specified request.
302 */
303static x_cfg *our_rconfig(const request_rec *r)
304{
305    return (x_cfg *) ap_get_module_config(r->request_config, &example_hooks_module);
306}
307#endif /* if 0 */
308
309/*
310 * Likewise for our configuration record for a connection.
311 */
312static x_cfg *our_cconfig(const conn_rec *c)
313{
314    return (x_cfg *) ap_get_module_config(c->conn_config, &example_hooks_module);
315}
316
317/*
318 * You *could* change the following if you wanted to see the calling
319 * sequence reported in the server's error_log, but beware - almost all of
320 * these co-routines are called for every single request, and the impact
321 * on the size (and readability) of the error_log is considerable.
322 */
323#ifndef EXAMPLE_LOG_EACH
324#define EXAMPLE_LOG_EACH 0
325#endif
326
327#if EXAMPLE_LOG_EACH
328static void example_log_each(apr_pool_t *p, server_rec *s, const char *note)
329{
330    if (s != NULL) {
331        ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, "mod_example: %s", note);
332    } else {
333        apr_file_t *out = NULL;
334        apr_file_open_stderr(&out, p);
335        apr_file_printf(out, "mod_example traced in non-loggable "
336                        "context: %s\n", note);
337    }
338}
339#endif
340
341/*
342 * This utility routine traces the hooks called when the server starts up.
343 * It leaves a trace in a global variable, so it should not be called from
344 * a hook handler that runs in a multi-threaded situation.
345 */
346
347static void trace_startup(apr_pool_t *p, server_rec *s, x_cfg *mconfig,
348                          const char *note)
349{
350    const char *sofar;
351    char *where, *addon;
352
353#if EXAMPLE_LOG_EACH
354    example_log_each(p, s, note);
355#endif
356
357    /*
358     * If we weren't passed a configuration record, we can't figure out to
359     * what location this call applies.  This only happens for co-routines
360     * that don't operate in a particular directory or server context.  If we
361     * got a valid record, extract the location (directory or server) to which
362     * it applies.
363     */
364    where = (mconfig != NULL) ? mconfig->loc : "nowhere";
365    where = (where != NULL) ? where : "";
366
367    addon = apr_pstrcat(p,
368                        "   <li>\n"
369                        "    <dl>\n"
370                        "     <dt><samp>", note, "</samp></dt>\n"
371                        "     <dd><samp>[", where, "]</samp></dd>\n"
372                        "    </dl>\n"
373                        "   </li>\n",
374                        NULL);
375
376    /*
377     * Make sure that we start with a valid string, even if we have never been
378     * called.
379     */
380    sofar = (trace == NULL) ? "" : trace;
381
382    trace = apr_pstrcat(p, sofar, addon, NULL);
383}
384
385
386/*
387 * This utility route traces the hooks called as a request is handled.
388 * It takes the current request as argument
389 */
390#define TRACE_NOTE "example-hooks-trace"
391
392static void trace_request(const request_rec *r, const char *note)
393{
394    const char *trace_copy, *sofar;
395    char *addon, *where;
396    x_cfg *cfg;
397
398#if EXAMPLE_LOG_EACH
399    example_log_each(r->pool, r->server, note);
400#endif
401
402    if ((sofar = apr_table_get(r->notes, TRACE_NOTE)) == NULL) {
403        sofar = "";
404    }
405
406    cfg = our_dconfig(r);
407
408    where = (cfg != NULL) ? cfg->loc : "nowhere";
409    where = (where != NULL) ? where : "";
410
411    addon = apr_pstrcat(r->pool,
412                        "   <li>\n"
413                        "    <dl>\n"
414                        "     <dt><samp>", note, "</samp></dt>\n"
415                        "     <dd><samp>[", where, "]</samp></dd>\n"
416                        "    </dl>\n"
417                        "   </li>\n",
418                        NULL);
419
420    trace_copy = apr_pstrcat(r->pool, sofar, addon, NULL);
421    apr_table_set(r->notes, TRACE_NOTE, trace_copy);
422}
423
424/*
425 * This utility routine traces the hooks called while processing a
426 * Connection. Its trace is kept in the pool notes of the pool associated
427 * with the Connection.
428 */
429
430/*
431 * Key to get and set the userdata.  We should be able to get away
432 * with a constant key, since in prefork mode the process will have
433 * the connection and its pool to itself entirely, and in
434 * multi-threaded mode each connection will have its own pool.
435 */
436#define CONN_NOTE "example-hooks-connection"
437
438static void trace_connection(conn_rec *c, const char *note)
439{
440    const char *trace_copy, *sofar;
441    char *addon, *where;
442    void *data;
443    x_cfg *cfg;
444
445#if EXAMPLE_LOG_EACH
446    example_log_each(c->pool, c->base_server, note);
447#endif
448
449    cfg = our_cconfig(c);
450
451    where = (cfg != NULL) ? cfg->loc : "nowhere";
452    where = (where != NULL) ? where : "";
453
454    addon = apr_pstrcat(c->pool,
455                        "   <li>\n"
456                        "    <dl>\n"
457                        "     <dt><samp>", note, "</samp></dt>\n"
458                        "     <dd><samp>[", where, "]</samp></dd>\n"
459                        "    </dl>\n"
460                        "   </li>\n",
461                        NULL);
462
463    /* Find existing notes and copy */
464    apr_pool_userdata_get(&data, CONN_NOTE, c->pool);
465    sofar = (data == NULL) ? "" : (const char *) data;
466
467    /* Tack addon onto copy */
468    trace_copy = apr_pstrcat(c->pool, sofar, addon, NULL);
469
470    /*
471     * Stash copy back into pool notes.  This call has a cleanup
472     * parameter, but we're not using it because the string has been
473     * allocated from that same pool.  There is also an unused return
474     * value: we have nowhere to communicate any error that might
475     * occur, and will have to check for the existence of this data on
476     * the other end.
477     */
478    apr_pool_userdata_set((const void *) trace_copy, CONN_NOTE,
479                          NULL, c->pool);
480}
481
482static void trace_nocontext(apr_pool_t *p, const char *file, int line,
483                            const char *note)
484{
485    /*
486     * Since we have no request or connection to trace, or any idea
487     * from where this routine was called, there's really not much we
488     * can do.  If we are not logging everything by way of the
489     * EXAMPLE_LOG_EACH constant, do nothing in this routine.
490     */
491
492#ifdef EXAMPLE_LOG_EACH
493    ap_log_perror(file, line, APLOG_MODULE_INDEX, APLOG_NOTICE, 0, p, "%s", note);
494#endif
495}
496
497
498/*--------------------------------------------------------------------------*/
499/* We prototyped the various syntax for command handlers (routines that     */
500/* are called when the configuration parser detects a directive declared    */
501/* by our module) earlier.  Now we actually declare a "real" routine that   */
502/* will be invoked by the parser when our "real" directive is               */
503/* encountered.                                                             */
504/*                                                                          */
505/* If a command handler encounters a problem processing the directive, it   */
506/* signals this fact by returning a non-NULL pointer to a string            */
507/* describing the problem.                                                  */
508/*                                                                          */
509/* The magic return value DECLINE_CMD is used to deal with directives       */
510/* that might be declared by multiple modules.  If the command handler      */
511/* returns NULL, the directive was processed; if it returns DECLINE_CMD,    */
512/* the next module (if any) that declares the directive is given a chance   */
513/* at it.  If it returns any other value, it's treated as the text of an    */
514/* error message.                                                           */
515/*--------------------------------------------------------------------------*/
516/*
517 * Command handler for the NO_ARGS "Example" directive.  All we do is mark the
518 * call in the trace log, and flag the applicability of the directive to the
519 * current location in that location's configuration record.
520 */
521static const char *cmd_example(cmd_parms *cmd, void *mconfig)
522{
523    x_cfg *cfg = (x_cfg *) mconfig;
524
525    /*
526     * "Example Wuz Here"
527     */
528    cfg->local = 1;
529    trace_startup(cmd->pool, cmd->server, cfg, "cmd_example()");
530    return NULL;
531}
532
533/*
534 * This function gets called to create a per-directory configuration
535 * record.  This will be called for the "default" server environment, and for
536 * each directory for which the parser finds any of our directives applicable.
537 * If a directory doesn't have any of our directives involved (i.e., they
538 * aren't in the .htaccess file, or a <Location>, <Directory>, or related
539 * block), this routine will *not* be called - the configuration for the
540 * closest ancestor is used.
541 *
542 * The return value is a pointer to the created module-specific
543 * structure.
544 */
545static void *x_create_dir_config(apr_pool_t *p, char *dirspec)
546{
547    x_cfg *cfg;
548    char *dname = dirspec;
549    char *note;
550
551    /*
552     * Allocate the space for our record from the pool supplied.
553     */
554    cfg = (x_cfg *) apr_pcalloc(p, sizeof(x_cfg));
555    /*
556     * Now fill in the defaults.  If there are any `parent' configuration
557     * records, they'll get merged as part of a separate callback.
558     */
559    cfg->local = 0;
560    cfg->congenital = 0;
561    cfg->cmode = CONFIG_MODE_DIRECTORY;
562    /*
563     * Finally, add our trace to the callback list.
564     */
565    dname = (dname != NULL) ? dname : "";
566    cfg->loc = apr_pstrcat(p, "DIR(", dname, ")", NULL);
567    note = apr_psprintf(p, "x_create_dir_config(p == %pp, dirspec == %s)",
568                        (void*) p, dirspec);
569    trace_startup(p, NULL, cfg, note);
570    return (void *) cfg;
571}
572
573/*
574 * This function gets called to merge two per-directory configuration
575 * records.  This is typically done to cope with things like .htaccess files
576 * or <Location> directives for directories that are beneath one for which a
577 * configuration record was already created.  The routine has the
578 * responsibility of creating a new record and merging the contents of the
579 * other two into it appropriately.  If the module doesn't declare a merge
580 * routine, the record for the closest ancestor location (that has one) is
581 * used exclusively.
582 *
583 * The routine MUST NOT modify any of its arguments!
584 *
585 * The return value is a pointer to the created module-specific structure
586 * containing the merged values.
587 */
588static void *x_merge_dir_config(apr_pool_t *p, void *parent_conf,
589                                      void *newloc_conf)
590{
591
592    x_cfg *merged_config = (x_cfg *) apr_pcalloc(p, sizeof(x_cfg));
593    x_cfg *pconf = (x_cfg *) parent_conf;
594    x_cfg *nconf = (x_cfg *) newloc_conf;
595    char *note;
596
597    /*
598     * Some things get copied directly from the more-specific record, rather
599     * than getting merged.
600     */
601    merged_config->local = nconf->local;
602    merged_config->loc = apr_pstrdup(p, nconf->loc);
603    /*
604     * Others, like the setting of the `congenital' flag, get ORed in.  The
605     * setting of that particular flag, for instance, is TRUE if it was ever
606     * true anywhere in the upstream configuration.
607     */
608    merged_config->congenital = (pconf->congenital | pconf->local);
609    /*
610     * If we're merging records for two different types of environment (server
611     * and directory), mark the new record appropriately.  Otherwise, inherit
612     * the current value.
613     */
614    merged_config->cmode =
615        (pconf->cmode == nconf->cmode) ? pconf->cmode : CONFIG_MODE_COMBO;
616    /*
617     * Now just record our being called in the trace list.  Include the
618     * locations we were asked to merge.
619     */
620    note = apr_psprintf(p, "x_merge_dir_config(p == %pp, parent_conf == "
621                        "%pp, newloc_conf == %pp)", (void*) p,
622                        (void*) parent_conf, (void*) newloc_conf);
623    trace_startup(p, NULL, merged_config, note);
624    return (void *) merged_config;
625}
626
627/*
628 * This function gets called to create a per-server configuration
629 * record.  It will always be called for the "default" server.
630 *
631 * The return value is a pointer to the created module-specific
632 * structure.
633 */
634static void *x_create_server_config(apr_pool_t *p, server_rec *s)
635{
636
637    x_cfg *cfg;
638    char *sname = s->server_hostname;
639
640    /*
641     * As with the x_create_dir_config() reoutine, we allocate and fill
642     * in an empty record.
643     */
644    cfg = (x_cfg *) apr_pcalloc(p, sizeof(x_cfg));
645    cfg->local = 0;
646    cfg->congenital = 0;
647    cfg->cmode = CONFIG_MODE_SERVER;
648    /*
649     * Note that we were called in the trace list.
650     */
651    sname = (sname != NULL) ? sname : "";
652    cfg->loc = apr_pstrcat(p, "SVR(", sname, ")", NULL);
653    trace_startup(p, s, cfg, "x_create_server_config()");
654    return (void *) cfg;
655}
656
657/*
658 * This function gets called to merge two per-server configuration
659 * records.  This is typically done to cope with things like virtual hosts and
660 * the default server configuration  The routine has the responsibility of
661 * creating a new record and merging the contents of the other two into it
662 * appropriately.  If the module doesn't declare a merge routine, the more
663 * specific existing record is used exclusively.
664 *
665 * The routine MUST NOT modify any of its arguments!
666 *
667 * The return value is a pointer to the created module-specific structure
668 * containing the merged values.
669 */
670static void *x_merge_server_config(apr_pool_t *p, void *server1_conf,
671                                         void *server2_conf)
672{
673
674    x_cfg *merged_config = (x_cfg *) apr_pcalloc(p, sizeof(x_cfg));
675    x_cfg *s1conf = (x_cfg *) server1_conf;
676    x_cfg *s2conf = (x_cfg *) server2_conf;
677    char *note;
678
679    /*
680     * Our inheritance rules are our own, and part of our module's semantics.
681     * Basically, just note whence we came.
682     */
683    merged_config->cmode =
684        (s1conf->cmode == s2conf->cmode) ? s1conf->cmode : CONFIG_MODE_COMBO;
685    merged_config->local = s2conf->local;
686    merged_config->congenital = (s1conf->congenital | s1conf->local);
687    merged_config->loc = apr_pstrdup(p, s2conf->loc);
688    /*
689     * Trace our call, including what we were asked to merge.
690     */
691    note = apr_pstrcat(p, "x_merge_server_config(\"", s1conf->loc, "\",\"",
692                   s2conf->loc, "\")", NULL);
693    trace_startup(p, NULL, merged_config, note);
694    return (void *) merged_config;
695}
696
697
698/*--------------------------------------------------------------------------*
699 *                                                                          *
700 * Now let's declare routines for each of the callback hooks in order.      *
701 * (That's the order in which they're listed in the callback list, *not     *
702 * the order in which the server calls them!  See the command_rec           *
703 * declaration near the bottom of this file.)  Note that these may be       *
704 * called for situations that don't relate primarily to our function - in   *
705 * other words, the fixup handler shouldn't assume that the request has     *
706 * to do with "example" stuff.                                              *
707 *                                                                          *
708 * With the exception of the content handler, all of our routines will be   *
709 * called for each request, unless an earlier handler from another module   *
710 * aborted the sequence.                                                    *
711 *                                                                          *
712 * There are three types of hooks (see include/ap_config.h):                *
713 *                                                                          *
714 * VOID      : No return code, run all handlers declared by any module      *
715 * RUN_FIRST : Run all handlers until one returns something other           *
716 *             than DECLINED. Hook runner result is result of last callback *
717 * RUN_ALL   : Run all handlers until one returns something other than OK   *
718 *             or DECLINED. The hook runner returns that other value. If    *
719 *             all hooks run, the hook runner returns OK.                   *
720 *                                                                          *
721 * Handlers that are declared as "int" can return the following:            *
722 *                                                                          *
723 *  OK          Handler accepted the request and did its thing with it.     *
724 *  DECLINED    Handler took no action.                                     *
725 *  HTTP_mumble Handler looked at request and found it wanting.             *
726 *                                                                          *
727 * See include/httpd.h for a list of HTTP_mumble status codes.  Handlers    *
728 * that are not declared as int return a valid pointer, or NULL if they     *
729 * DECLINE to handle their phase for that specific request.  Exceptions, if *
730 * any, are noted with each routine.                                        *
731 *--------------------------------------------------------------------------*/
732
733/*
734 * This routine is called before the server processes the configuration
735 * files.  There is no return value.
736 */
737static int x_pre_config(apr_pool_t *pconf, apr_pool_t *plog,
738                        apr_pool_t *ptemp)
739{
740    /*
741     * Log the call and exit.
742     */
743    trace_startup(ptemp, NULL, NULL, "x_pre_config()");
744
745    return OK;
746}
747
748/*
749 * This routine is called after the server processes the configuration
750 * files.  At this point the module may review and adjust its configuration
751 * settings in relation to one another and report any problems.  On restart,
752 * this routine will be called twice, once in the startup process (which
753 * exits shortly after this phase) and once in the running server process.
754 *
755 * The return value is OK, DECLINED, or HTTP_mumble.  If we return OK, the
756 * server will still call any remaining modules with an handler for this
757 * phase.
758 */
759static int x_check_config(apr_pool_t *pconf, apr_pool_t *plog,
760                          apr_pool_t *ptemp, server_rec *s)
761{
762    /*
763     * Log the call and exit.
764     */
765    trace_startup(ptemp, s, NULL, "x_check_config()");
766    return OK;
767}
768
769/*
770 * This routine is called when the -t command-line option is supplied.
771 * It executes only once, in the startup process, after the check_config
772 * phase and just before the process exits.  At this point the module
773 * may output any information useful in configuration testing.
774 *
775 * This is a VOID hook: all defined handlers get called.
776 */
777static void x_test_config(apr_pool_t *pconf, server_rec *s)
778{
779    apr_file_t *out = NULL;
780
781    apr_file_open_stderr(&out, pconf);
782
783    apr_file_printf(out, "Example module configuration test routine\n");
784
785    trace_startup(pconf, s, NULL, "x_test_config()");
786}
787
788/*
789 * This routine is called to perform any module-specific log file
790 * openings. It is invoked just before the post_config phase
791 *
792 * The return value is OK, DECLINED, or HTTP_mumble.  If we return OK, the
793 * server will still call any remaining modules with an handler for this
794 * phase.
795 */
796static int x_open_logs(apr_pool_t *pconf, apr_pool_t *plog,
797                        apr_pool_t *ptemp, server_rec *s)
798{
799    /*
800     * Log the call and exit.
801     */
802    trace_startup(ptemp, s, NULL, "x_open_logs()");
803    return OK;
804}
805
806/*
807 * This routine is called after the server finishes the configuration
808 * process.  At this point the module may review and adjust its configuration
809 * settings in relation to one another and report any problems.  On restart,
810 * this routine will be called only once, in the running server process.
811 *
812 * The return value is OK, DECLINED, or HTTP_mumble.  If we return OK, the
813 * server will still call any remaining modules with an handler for this
814 * phase.
815 */
816static int x_post_config(apr_pool_t *pconf, apr_pool_t *plog,
817                          apr_pool_t *ptemp, server_rec *s)
818{
819    /*
820     * Log the call and exit.
821     */
822    trace_startup(ptemp, s, NULL, "x_post_config()");
823    return OK;
824}
825
826/*
827 * All our process-death routine does is add its trace to the log.
828 */
829static apr_status_t x_child_exit(void *data)
830{
831    char *note;
832    server_rec *s = data;
833    char *sname = s->server_hostname;
834
835    /*
836     * The arbitrary text we add to our trace entry indicates for which server
837     * we're being called.
838     */
839    sname = (sname != NULL) ? sname : "";
840    note = apr_pstrcat(s->process->pool, "x_child_exit(", sname, ")", NULL);
841    trace_startup(s->process->pool, s, NULL, note);
842    return APR_SUCCESS;
843}
844
845/*
846 * All our process initialiser does is add its trace to the log.
847 *
848 * This is a VOID hook: all defined handlers get called.
849 */
850static void x_child_init(apr_pool_t *p, server_rec *s)
851{
852    char *note;
853    char *sname = s->server_hostname;
854
855    /*
856     * The arbitrary text we add to our trace entry indicates for which server
857     * we're being called.
858     */
859    sname = (sname != NULL) ? sname : "";
860    note = apr_pstrcat(p, "x_child_init(", sname, ")", NULL);
861    trace_startup(p, s, NULL, note);
862
863    apr_pool_cleanup_register(p, s, x_child_exit, x_child_exit);
864}
865
866/*
867 * The hook runner for ap_hook_http_scheme is aliased to ap_http_scheme(),
868 * a routine that the core and other modules call when they need to know
869 * the URL scheme for the request.  For instance, mod_ssl returns "https"
870 * if the server_rec associated with the request has SSL enabled.
871 *
872 * This hook was named 'ap_hook_http_method' in httpd 2.0.
873 *
874 * This is a RUN_FIRST hook: the first handler to return a non NULL
875 * value aborts the handler chain.  The http_core module inserts a
876 * fallback handler (with APR_HOOK_REALLY_LAST preference) that returns
877 * "http".
878 */
879static const char *x_http_scheme(const request_rec *r)
880{
881    /*
882     * Log the call and exit.
883     */
884    trace_request(r, "x_http_scheme()");
885
886    /* We have no claims to make about the request scheme */
887    return NULL;
888}
889
890/*
891 * The runner for this hook is aliased to ap_default_port(), which the
892 * core and other modules call when they need to know the default port
893 * for a particular server.  This is used for instance to omit the
894 * port number from a Redirect response Location header URL if the port
895 * number is equal to the default port for the service (like 80 for http).
896 *
897 * This is a RUN_FIRST hook: the first handler to return a non-zero
898 * value is the last one executed.  The http_core module inserts a
899 * fallback handler (with APR_HOOK_REALLY_LAST order specifier) that
900 * returns 80.
901 */
902static apr_port_t x_default_port(const request_rec *r)
903{
904    /*
905     * Log the call and exit.
906     */
907    trace_request(r, "x_default_port()");
908    return 0;
909}
910
911/*
912 * This routine is called just before the handler gets invoked. It allows
913 * a module to insert a previously defined filter into the filter chain.
914 *
915 * No filter has been defined by this module, so we just log the call
916 * and exit.
917 *
918 * This is a VOID hook: all defined handlers get called.
919 */
920static void x_insert_filter(request_rec *r)
921{
922    /*
923     * Log the call and exit.
924     */
925    trace_request(r, "x_insert_filter()");
926}
927
928/*
929 * This routine is called to insert a previously defined error filter into
930 * the filter chain as the request is being processed.
931 *
932 * For the purpose of this example, we don't have a filter to insert,
933 * so just add to the trace and exit.
934 *
935 * This is a VOID hook: all defined handlers get called.
936 */
937static void x_insert_error_filter(request_rec *r)
938{
939    trace_request(r, "x_insert_error_filter()");
940}
941
942/*--------------------------------------------------------------------------*/
943/*                                                                          */
944/* Now we declare our content handlers, which are invoked when the server   */
945/* encounters a document which our module is supposed to have a chance to   */
946/* see.  (See mod_mime's SetHandler and AddHandler directives, and the      */
947/* mod_info and mod_status examples, for more details.)                     */
948/*                                                                          */
949/* Since content handlers are dumping data directly into the connection     */
950/* (using the r*() routines, such as rputs() and rprintf()) without         */
951/* intervention by other parts of the server, they need to make             */
952/* sure any accumulated HTTP headers are sent first.  This is done by       */
953/* calling send_http_header().  Otherwise, no header will be sent at all,   */
954/* and the output sent to the client will actually be HTTP-uncompliant.     */
955/*--------------------------------------------------------------------------*/
956/*
957 * Sample content handler.  All this does is display the call list that has
958 * been built up so far.
959 *
960 * This routine gets called for every request, unless another handler earlier
961 * in the callback chain has already handled the request. It is up to us to
962 * test the request_rec->handler field and see whether we are meant to handle
963 * this request.
964 *
965 * The content handler gets to write directly to the client using calls like
966 * ap_rputs() and ap_rprintf()
967 *
968 * This is a RUN_FIRST hook.
969 */
970static int x_handler(request_rec *r)
971{
972    x_cfg *dcfg;
973    char *note;
974    void *conn_data;
975    apr_status_t status;
976
977    dcfg = our_dconfig(r);
978    /*
979     * Add our trace to the log, and whether we get to write
980     * content for this request.
981     */
982    note = apr_pstrcat(r->pool, "x_handler(), handler is \"",
983                      r->handler, "\"", NULL);
984    trace_request(r, note);
985
986    /* If it's not for us, get out as soon as possible. */
987    if (strcmp(r->handler, "example-hooks-handler")) {
988        return DECLINED;
989    }
990
991    /*
992     * Set the Content-type header. Note that we do not actually have to send
993     * the headers: this is done by the http core.
994     */
995    ap_set_content_type(r, "text/html");
996    /*
997     * If we're only supposed to send header information (HEAD request), we're
998     * already there.
999     */
1000    if (r->header_only) {
1001        return OK;
1002    }
1003
1004    /*
1005     * Now send our actual output.  Since we tagged this as being
1006     * "text/html", we need to embed any HTML.
1007     */
1008    ap_rputs(DOCTYPE_HTML_3_2, r);
1009    ap_rputs("<HTML>\n", r);
1010    ap_rputs(" <HEAD>\n", r);
1011    ap_rputs("  <TITLE>mod_example_hooks Module Content-Handler Output\n", r);
1012    ap_rputs("  </TITLE>\n", r);
1013    ap_rputs(" </HEAD>\n", r);
1014    ap_rputs(" <BODY>\n", r);
1015    ap_rputs("  <H1><SAMP>mod_example_hooks</SAMP> Module Content-Handler Output\n", r);
1016    ap_rputs("  </H1>\n", r);
1017    ap_rputs("  <P>\n", r);
1018    ap_rprintf(r, "  Apache HTTP Server version: \"%s\"\n",
1019            ap_get_server_banner());
1020    ap_rputs("  <BR>\n", r);
1021    ap_rprintf(r, "  Server built: \"%s\"\n", ap_get_server_built());
1022    ap_rputs("  </P>\n", r);
1023    ap_rputs("  <P>\n", r);
1024    ap_rputs("  The format for the callback trace is:\n", r);
1025    ap_rputs("  </P>\n", r);
1026    ap_rputs("  <DL>\n", r);
1027    ap_rputs("   <DT><EM>n</EM>.<SAMP>&lt;routine-name&gt;", r);
1028    ap_rputs("(&lt;routine-data&gt;)</SAMP>\n", r);
1029    ap_rputs("   </DT>\n", r);
1030    ap_rputs("   <DD><SAMP>[&lt;applies-to&gt;]</SAMP>\n", r);
1031    ap_rputs("   </DD>\n", r);
1032    ap_rputs("  </DL>\n", r);
1033    ap_rputs("  <P>\n", r);
1034    ap_rputs("  The <SAMP>&lt;routine-data&gt;</SAMP> is supplied by\n", r);
1035    ap_rputs("  the routine when it requests the trace,\n", r);
1036    ap_rputs("  and the <SAMP>&lt;applies-to&gt;</SAMP> is extracted\n", r);
1037    ap_rputs("  from the configuration record at the time of the trace.\n", r);
1038    ap_rputs("  <STRONG>SVR()</STRONG> indicates a server environment\n", r);
1039    ap_rputs("  (blank means the main or default server, otherwise it's\n", r);
1040    ap_rputs("  the name of the VirtualHost); <STRONG>DIR()</STRONG>\n", r);
1041    ap_rputs("  indicates a location in the URL or filesystem\n", r);
1042    ap_rputs("  namespace.\n", r);
1043    ap_rputs("  </P>\n", r);
1044    ap_rprintf(r, "  <H2>Startup callbacks so far:</H2>\n  <OL>\n%s  </OL>\n",
1045            trace);
1046    ap_rputs("  <H2>Connection-specific callbacks so far:</H2>\n", r);
1047
1048    status =  apr_pool_userdata_get(&conn_data, CONN_NOTE,
1049                                    r->connection->pool);
1050    if ((status == APR_SUCCESS) && conn_data) {
1051        ap_rprintf(r, "  <OL>\n%s  </OL>\n", (char *) conn_data);
1052    } else {
1053        ap_rputs("  <P>No connection-specific callback information was "
1054                 "retrieved.</P>\n", r);
1055    }
1056
1057    ap_rputs("  <H2>Request-specific callbacks so far:</H2>\n", r);
1058    ap_rprintf(r, "  <OL>\n%s  </OL>\n", apr_table_get(r->notes, TRACE_NOTE));
1059    ap_rputs("  <H2>Environment for <EM>this</EM> call:</H2>\n", r);
1060    ap_rputs("  <UL>\n", r);
1061    ap_rprintf(r, "   <LI>Applies-to: <SAMP>%s</SAMP>\n   </LI>\n", dcfg->loc);
1062    ap_rprintf(r, "   <LI>\"Example\" directive declared here: %s\n   </LI>\n",
1063            (dcfg->local ? "YES" : "NO"));
1064    ap_rprintf(r, "   <LI>\"Example\" inherited: %s\n   </LI>\n",
1065            (dcfg->congenital ? "YES" : "NO"));
1066    ap_rputs("  </UL>\n", r);
1067    ap_rputs(" </BODY>\n", r);
1068    ap_rputs("</HTML>\n", r);
1069    /*
1070     * We're all done, so cancel the timeout we set.  Since this is probably
1071     * the end of the request we *could* assume this would be done during
1072     * post-processing - but it's possible that another handler might be
1073     * called and inherit our outstanding timer.  Not good; to each its own.
1074     */
1075    /*
1076     * We did what we wanted to do, so tell the rest of the server we
1077     * succeeded.
1078     */
1079    return OK;
1080}
1081
1082/*
1083 * The quick_handler hook presents modules with a very powerful opportunity to
1084 * serve their content in a very early request phase.  Note that this handler
1085 * can not serve any requests from the file system because hooks like
1086 * map_to_storage have not run.  The quick_handler hook also runs before any
1087 * authentication and access control.
1088 *
1089 * This hook is used by mod_cache to serve cached content.
1090 *
1091 * This is a RUN_FIRST hook. Return OK if you have served the request,
1092 * DECLINED if you want processing to continue, or a HTTP_* error code to stop
1093 * processing the request.
1094 */
1095static int x_quick_handler(request_rec *r, int lookup_uri)
1096{
1097    /*
1098     * Log the call and exit.
1099     */
1100    trace_request(r, "x_quick_handler()");
1101    return DECLINED;
1102}
1103
1104/*
1105 * This routine is called just after the server accepts the connection,
1106 * but before it is handed off to a protocol module to be served.  The point
1107 * of this hook is to allow modules an opportunity to modify the connection
1108 * as soon as possible. The core server uses this phase to setup the
1109 * connection record based on the type of connection that is being used.
1110 *
1111 * This is a RUN_ALL hook.
1112 */
1113static int x_pre_connection(conn_rec *c, void *csd)
1114{
1115    char *note;
1116
1117    /*
1118     * Log the call and exit.
1119     */
1120    note = apr_psprintf(c->pool, "x_pre_connection(c = %pp, p = %pp)",
1121                        (void*) c, (void*) c->pool);
1122    trace_connection(c, note);
1123
1124    return OK;
1125}
1126
1127/* This routine is used to actually process the connection that was received.
1128 * Only protocol modules should implement this hook, as it gives them an
1129 * opportunity to replace the standard HTTP processing with processing for
1130 * some other protocol.  Both echo and POP3 modules are available as
1131 * examples.
1132 *
1133 * This is a RUN_FIRST hook.
1134 */
1135static int x_process_connection(conn_rec *c)
1136{
1137    trace_connection(c, "x_process_connection()");
1138
1139    return DECLINED;
1140}
1141
1142/*
1143 * This routine is called after the request has been read but before any other
1144 * phases have been processed.  This allows us to make decisions based upon
1145 * the input header fields.
1146 *
1147 * This is a HOOK_VOID hook.
1148 */
1149static void x_pre_read_request(request_rec *r, conn_rec *c)
1150{
1151    /*
1152     * We don't actually *do* anything here, except note the fact that we were
1153     * called.
1154     */
1155    trace_request(r, "x_pre_read_request()");
1156}
1157
1158/*
1159 * This routine is called after the request has been read but before any other
1160 * phases have been processed.  This allows us to make decisions based upon
1161 * the input header fields.
1162 *
1163 * This is a RUN_ALL hook.
1164 */
1165static int x_post_read_request(request_rec *r)
1166{
1167    /*
1168     * We don't actually *do* anything here, except note the fact that we were
1169     * called.
1170     */
1171    trace_request(r, "x_post_read_request()");
1172    return DECLINED;
1173}
1174
1175/*
1176 * This routine gives our module an opportunity to translate the URI into an
1177 * actual filename.  If we don't do anything special, the server's default
1178 * rules (Alias directives and the like) will continue to be followed.
1179 *
1180 * This is a RUN_FIRST hook.
1181 */
1182static int x_translate_name(request_rec *r)
1183{
1184
1185    /*
1186     * We don't actually *do* anything here, except note the fact that we were
1187     * called.
1188     */
1189    trace_request(r, "x_translate_name()");
1190    return DECLINED;
1191}
1192
1193/*
1194 * This routine maps r->filename to a physical file on disk.  Useful for
1195 * overriding default core behavior, including skipping mapping for
1196 * requests that are not file based.
1197 *
1198 * This is a RUN_FIRST hook.
1199 */
1200static int x_map_to_storage(request_rec *r)
1201{
1202    /*
1203     * We don't actually *do* anything here, except note the fact that we were
1204     * called.
1205     */
1206    trace_request(r, "x_map_to_storage()");
1207    return DECLINED;
1208}
1209
1210/*
1211 * this routine gives our module another chance to examine the request
1212 * headers and to take special action. This is the first phase whose
1213 * hooks' configuration directives can appear inside the <Directory>
1214 * and similar sections, because at this stage the URI has been mapped
1215 * to the filename. For example this phase can be used to block evil
1216 * clients, while little resources were wasted on these.
1217 *
1218 * This is a RUN_ALL hook.
1219 */
1220static int x_header_parser(request_rec *r)
1221{
1222    /*
1223     * We don't actually *do* anything here, except note the fact that we were
1224     * called.
1225     */
1226    trace_request(r, "x_header_parser()");
1227    return DECLINED;
1228}
1229
1230
1231/*
1232 * This routine is called to check for any module-specific restrictions placed
1233 * upon the requested resource.  (See the mod_access_compat module for an
1234 * example.)
1235 *
1236 * This is a RUN_ALL hook. The first handler to return a status other than OK
1237 * or DECLINED (for instance, HTTP_FORBIDDEN) aborts the callback chain.
1238 */
1239static int x_check_access(request_rec *r)
1240{
1241    trace_request(r, "x_check_access()");
1242    return DECLINED;
1243}
1244
1245/*
1246 * This routine is called to check the authentication information sent with
1247 * the request (such as looking up the user in a database and verifying that
1248 * the [encrypted] password sent matches the one in the database).
1249 *
1250 * This is a RUN_FIRST hook. The return value is OK, DECLINED, or some
1251 * HTTP_mumble error (typically HTTP_UNAUTHORIZED).
1252 */
1253static int x_check_authn(request_rec *r)
1254{
1255    /*
1256     * Don't do anything except log the call.
1257     */
1258    trace_request(r, "x_check_authn()");
1259    return DECLINED;
1260}
1261
1262/*
1263 * This routine is called to check to see if the resource being requested
1264 * requires authorisation.
1265 *
1266 * This is a RUN_FIRST hook. The return value is OK, DECLINED, or
1267 * HTTP_mumble.  If we return OK, no other modules are called during this
1268 * phase.
1269 *
1270 * If *all* modules return DECLINED, the request is aborted with a server
1271 * error.
1272 */
1273static int x_check_authz(request_rec *r)
1274{
1275    /*
1276     * Log the call and return OK, or access will be denied (even though we
1277     * didn't actually do anything).
1278     */
1279    trace_request(r, "x_check_authz()");
1280    return DECLINED;
1281}
1282
1283/*
1284 * This routine is called to determine and/or set the various document type
1285 * information bits, like Content-type (via r->content_type), language, et
1286 * cetera.
1287 *
1288 * This is a RUN_FIRST hook.
1289 */
1290static int x_type_checker(request_rec *r)
1291{
1292    /*
1293     * Log the call, but don't do anything else - and report truthfully that
1294     * we didn't do anything.
1295     */
1296    trace_request(r, "x_type_checker()");
1297    return DECLINED;
1298}
1299
1300/*
1301 * This routine is called to perform any module-specific fixing of header
1302 * fields, et cetera.  It is invoked just before any content-handler.
1303 *
1304 * This is a RUN_ALL HOOK.
1305 */
1306static int x_fixups(request_rec *r)
1307{
1308    /*
1309     * Log the call and exit.
1310     */
1311    trace_request(r, "x_fixups()");
1312    return DECLINED;
1313}
1314
1315/*
1316 * This routine is called to perform any module-specific logging activities
1317 * over and above the normal server things.
1318 *
1319 * This is a RUN_ALL hook.
1320 */
1321static int x_log_transaction(request_rec *r)
1322{
1323    trace_request(r, "x_log_transaction()");
1324    return DECLINED;
1325}
1326
1327#ifdef HAVE_UNIX_SUEXEC
1328
1329/*
1330 * This routine is called to find out under which user id to run suexec
1331 * Unless our module runs CGI programs, there is no reason for us to
1332 * mess with this information.
1333 *
1334 * This is a RUN_FIRST hook. The return value is a pointer to an
1335 * ap_unix_identity_t or NULL.
1336 */
1337static ap_unix_identity_t *x_get_suexec_identity(const request_rec *r)
1338{
1339    trace_request(r, "x_get_suexec_identity()");
1340    return NULL;
1341}
1342#endif
1343
1344/*
1345 * This routine is called to create a connection. This hook is implemented
1346 * by the Apache core: there is no known reason a module should override
1347 * it.
1348 *
1349 * This is a RUN_FIRST hook.
1350 *
1351 * Return NULL to decline, a valid conn_rec pointer to accept.
1352 */
1353static conn_rec *x_create_connection(apr_pool_t *p, server_rec *server,
1354                                     apr_socket_t *csd, long conn_id,
1355                                     void *sbh, apr_bucket_alloc_t *alloc)
1356{
1357    trace_nocontext(p, __FILE__, __LINE__, "x_create_connection()");
1358    return NULL;
1359}
1360
1361/*
1362 * This hook is defined in server/core.c, but it is not actually called
1363 * or documented.
1364 *
1365 * This is a RUN_ALL hook.
1366 */
1367static int x_get_mgmt_items(apr_pool_t *p, const char *val, apr_hash_t *ht)
1368{
1369    /* We have nothing to do here but trace the call, and no context
1370     * in which to trace it.
1371     */
1372    trace_nocontext(p, __FILE__, __LINE__, "x_check_config()");
1373    return DECLINED;
1374}
1375
1376/*
1377 * This routine gets called shortly after the request_rec structure
1378 * is created. It provides the opportunity to manipulae the request
1379 * at a very early stage.
1380 *
1381 * This is a RUN_ALL hook.
1382 */
1383static int x_create_request(request_rec *r)
1384{
1385    /*
1386     * We have a request_rec, but it is not filled in enough to give
1387     * us a usable configuration. So, add a trace without context.
1388     */
1389    trace_nocontext( r->pool, __FILE__, __LINE__, "x_create_request()");
1390    return DECLINED;
1391}
1392
1393/*
1394 * This routine gets called during the startup of the MPM.
1395 * No known existing module implements this hook.
1396 *
1397 * This is a RUN_ALL hook.
1398 */
1399static int x_pre_mpm(apr_pool_t *p, ap_scoreboard_e sb_type)
1400{
1401    trace_nocontext(p, __FILE__, __LINE__, "x_pre_mpm()");
1402    return DECLINED;
1403}
1404
1405/*
1406 * This hook gets run periodically by a maintenance function inside
1407 * the MPM. Its exact purpose is unknown and undocumented at this time.
1408 *
1409 * This is a RUN_ALL hook.
1410 */
1411static int x_monitor(apr_pool_t *p, server_rec *s)
1412{
1413    trace_nocontext(p, __FILE__, __LINE__, "x_monitor()");
1414    return DECLINED;
1415}
1416
1417/*--------------------------------------------------------------------------*/
1418/*                                                                          */
1419/* Which functions are responsible for which hooks in the server.           */
1420/*                                                                          */
1421/*--------------------------------------------------------------------------*/
1422/*
1423 * Each function our module provides to handle a particular hook is
1424 * specified here.  The functions are registered using
1425 * ap_hook_foo(name, predecessors, successors, position)
1426 * where foo is the name of the hook.
1427 *
1428 * The args are as follows:
1429 * name         -> the name of the function to call.
1430 * predecessors -> a list of modules whose calls to this hook must be
1431 *                 invoked before this module.
1432 * successors   -> a list of modules whose calls to this hook must be
1433 *                 invoked after this module.
1434 * position     -> The relative position of this module.  One of
1435 *                 APR_HOOK_FIRST, APR_HOOK_MIDDLE, or APR_HOOK_LAST.
1436 *                 Most modules will use APR_HOOK_MIDDLE.  If multiple
1437 *                 modules use the same relative position, Apache will
1438 *                 determine which to call first.
1439 *                 If your module relies on another module to run first,
1440 *                 or another module running after yours, use the
1441 *                 predecessors and/or successors.
1442 *
1443 * The number in brackets indicates the order in which the routine is called
1444 * during request processing.  Note that not all routines are necessarily
1445 * called (such as if a resource doesn't have access restrictions).
1446 * The actual delivery of content to the browser [9] is not handled by
1447 * a hook; see the handler declarations below.
1448 */
1449static void x_register_hooks(apr_pool_t *p)
1450{
1451    ap_hook_pre_config(x_pre_config, NULL, NULL, APR_HOOK_MIDDLE);
1452    ap_hook_check_config(x_check_config, NULL, NULL, APR_HOOK_MIDDLE);
1453    ap_hook_test_config(x_test_config, NULL, NULL, APR_HOOK_MIDDLE);
1454    ap_hook_open_logs(x_open_logs, NULL, NULL, APR_HOOK_MIDDLE);
1455    ap_hook_post_config(x_post_config, NULL, NULL, APR_HOOK_MIDDLE);
1456    ap_hook_child_init(x_child_init, NULL, NULL, APR_HOOK_MIDDLE);
1457    ap_hook_handler(x_handler, NULL, NULL, APR_HOOK_MIDDLE);
1458    ap_hook_quick_handler(x_quick_handler, NULL, NULL, APR_HOOK_MIDDLE);
1459    ap_hook_pre_connection(x_pre_connection, NULL, NULL, APR_HOOK_MIDDLE);
1460    ap_hook_process_connection(x_process_connection, NULL, NULL, APR_HOOK_MIDDLE);
1461    ap_hook_pre_read_request(x_pre_read_request, NULL, NULL,
1462                              APR_HOOK_MIDDLE);
1463    /* [1] post read_request handling */
1464    ap_hook_post_read_request(x_post_read_request, NULL, NULL,
1465                              APR_HOOK_MIDDLE);
1466    ap_hook_log_transaction(x_log_transaction, NULL, NULL, APR_HOOK_MIDDLE);
1467    ap_hook_http_scheme(x_http_scheme, NULL, NULL, APR_HOOK_MIDDLE);
1468    ap_hook_default_port(x_default_port, NULL, NULL, APR_HOOK_MIDDLE);
1469    ap_hook_translate_name(x_translate_name, NULL, NULL, APR_HOOK_MIDDLE);
1470    ap_hook_map_to_storage(x_map_to_storage, NULL,NULL, APR_HOOK_MIDDLE);
1471    ap_hook_header_parser(x_header_parser, NULL, NULL, APR_HOOK_MIDDLE);
1472    ap_hook_fixups(x_fixups, NULL, NULL, APR_HOOK_MIDDLE);
1473    ap_hook_type_checker(x_type_checker, NULL, NULL, APR_HOOK_MIDDLE);
1474    ap_hook_check_access(x_check_access, NULL, NULL, APR_HOOK_MIDDLE,
1475                         AP_AUTH_INTERNAL_PER_CONF);
1476    ap_hook_check_authn(x_check_authn, NULL, NULL, APR_HOOK_MIDDLE,
1477                        AP_AUTH_INTERNAL_PER_CONF);
1478    ap_hook_check_authz(x_check_authz, NULL, NULL, APR_HOOK_MIDDLE,
1479                        AP_AUTH_INTERNAL_PER_CONF);
1480    ap_hook_insert_filter(x_insert_filter, NULL, NULL, APR_HOOK_MIDDLE);
1481    ap_hook_insert_error_filter(x_insert_error_filter, NULL, NULL, APR_HOOK_MIDDLE);
1482#ifdef HAVE_UNIX_SUEXEC
1483    ap_hook_get_suexec_identity(x_get_suexec_identity, NULL, NULL, APR_HOOK_MIDDLE);
1484#endif
1485    ap_hook_create_connection(x_create_connection, NULL, NULL, APR_HOOK_MIDDLE);
1486    ap_hook_get_mgmt_items(x_get_mgmt_items, NULL, NULL, APR_HOOK_MIDDLE);
1487    ap_hook_create_request(x_create_request, NULL, NULL, APR_HOOK_MIDDLE);
1488    ap_hook_pre_mpm(x_pre_mpm, NULL, NULL, APR_HOOK_MIDDLE);
1489    ap_hook_monitor(x_monitor, NULL, NULL, APR_HOOK_MIDDLE);
1490}
1491
1492/*--------------------------------------------------------------------------*/
1493/*                                                                          */
1494/* All of the routines have been declared now.  Here's the list of          */
1495/* directives specific to our module, and information about where they      */
1496/* may appear and how the command parser should pass them to us for         */
1497/* processing.  Note that care must be taken to ensure that there are NO    */
1498/* collisions of directive names between modules.                           */
1499/*                                                                          */
1500/*--------------------------------------------------------------------------*/
1501/*
1502 * List of directives specific to our module.
1503 */
1504static const command_rec x_cmds[] =
1505{
1506    AP_INIT_NO_ARGS(
1507        "Example",                          /* directive name */
1508        cmd_example,                        /* config action routine */
1509        NULL,                               /* argument to include in call */
1510        OR_OPTIONS,                         /* where available */
1511        "Example directive - no arguments"  /* directive description */
1512    ),
1513    {NULL}
1514};
1515/*--------------------------------------------------------------------------*/
1516/*                                                                          */
1517/* Finally, the list of callback routines and data structures that provide  */
1518/* the static hooks into our module from the other parts of the server.     */
1519/*                                                                          */
1520/*--------------------------------------------------------------------------*/
1521/*
1522 * Module definition for configuration.  If a particular callback is not
1523 * needed, replace its routine name below with the word NULL.
1524 */
1525AP_DECLARE_MODULE(example_hooks) =
1526{
1527    STANDARD20_MODULE_STUFF,
1528    x_create_dir_config,    /* per-directory config creator */
1529    x_merge_dir_config,     /* dir config merger */
1530    x_create_server_config, /* server config creator */
1531    x_merge_server_config,  /* server config merger */
1532    x_cmds,                 /* command table */
1533    x_register_hooks,       /* set up other request processing hooks */
1534};
1535