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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
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
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_'.
30 #include "http_config.h"
31 #include "http_core.h"
33 #include "http_main.h"
34 #include "http_protocol.h"
35 #include "http_request.h"
36 #include "util_script.h"
37 #include "http_connection.h"
39 #include "apr_strings.h"
43 /*--------------------------------------------------------------------------*/
45 /* Data declarations. */
47 /* Here are the static cells and structure declarations private to our */
50 /*--------------------------------------------------------------------------*/
53 * Sample configuration record. Used for both per-directory and per-server
56 * It's perfectly reasonable to have two different structures for the two
57 * different environments. The same command handlers will be called for
58 * both, though, so the handlers need to be able to tell them apart. One
59 * possibility is for both structures to start with an int which is 0 for
60 * one and 1 for the other.
62 * Note that while the per-directory and per-server configuration records are
63 * available to most of the module handlers, they should be treated as
64 * READ-ONLY by all except the command and merge handlers. Sometimes handlers
65 * are handed a record that applies to the current location by implication or
66 * inheritance, and modifying it will change the rules for other locations.
68 typedef struct x_cfg {
69 int cmode; /* Environment to which record applies
70 * (directory, server, or combination).
72 #define CONFIG_MODE_SERVER 1
73 #define CONFIG_MODE_DIRECTORY 2
74 #define CONFIG_MODE_COMBO 3 /* Shouldn't ever happen. */
75 int local; /* Boolean: "Example" directive declared
78 int congenital; /* Boolean: did we inherit an "Example"? */
79 char *trace; /* Pointer to trace string. */
80 char *loc; /* Location to which this record applies. */
84 * Let's set up a module-local static cell to point to the accreting callback
85 * trace. As each API callback is made to us, we'll tack on the particulars
86 * to whatever we've already recorded. To avoid massive memory bloat as
87 * directories are walked again and again, we record the routine/environment
88 * the first time (non-request context only), and ignore subsequent calls for
89 * the same routine/environment.
91 static const char *trace = NULL;
92 static apr_table_t *static_calls_made = NULL;
95 * To avoid leaking memory from pools other than the per-request one, we
96 * allocate a module-private pool, and then use a sub-pool of that which gets
97 * freed each time we modify the trace. That way previous layers of trace
98 * data don't get lost.
100 static apr_pool_t *x_pool = NULL;
101 static apr_pool_t *x_subpool = NULL;
104 * Declare ourselves so the configuration routines can find and know us.
105 * We'll fill it in at the end of the module.
107 module AP_MODULE_DECLARE_DATA example_module;
109 /*--------------------------------------------------------------------------*/
111 /* The following pseudo-prototype declarations illustrate the parameters */
112 /* passed to command handlers for the different types of directive */
113 /* syntax. If an argument was specified in the directive definition */
114 /* (look for "command_rec" below), it's available to the command handler */
115 /* via the (void *) info field in the cmd_parms argument passed to the */
116 /* handler (cmd->info for the examples below). */
118 /*--------------------------------------------------------------------------*/
121 * Command handler for a NO_ARGS directive. Declared in the command_rec
123 * AP_INIT_NO_ARGS("directive", function, mconfig, where, help)
125 * static const char *handle_NO_ARGS(cmd_parms *cmd, void *mconfig);
129 * Command handler for a RAW_ARGS directive. The "args" argument is the text
130 * of the commandline following the directive itself. Declared in the
131 * command_rec list with
132 * AP_INIT_RAW_ARGS("directive", function, mconfig, where, help)
134 * static const char *handle_RAW_ARGS(cmd_parms *cmd, void *mconfig,
139 * Command handler for a FLAG directive. The single parameter is passed in
140 * "bool", which is either zero or not for Off or On respectively.
141 * Declared in the command_rec list with
142 * AP_INIT_FLAG("directive", function, mconfig, where, help)
144 * static const char *handle_FLAG(cmd_parms *cmd, void *mconfig, int bool);
148 * Command handler for a TAKE1 directive. The single parameter is passed in
149 * "word1". Declared in the command_rec list with
150 * AP_INIT_TAKE1("directive", function, mconfig, where, help)
152 * static const char *handle_TAKE1(cmd_parms *cmd, void *mconfig,
157 * Command handler for a TAKE2 directive. TAKE2 commands must always have
158 * exactly two arguments. Declared in the command_rec list with
159 * AP_INIT_TAKE2("directive", function, mconfig, where, help)
161 * static const char *handle_TAKE2(cmd_parms *cmd, void *mconfig,
162 * char *word1, char *word2);
166 * Command handler for a TAKE3 directive. Like TAKE2, these must have exactly
167 * three arguments, or the parser complains and doesn't bother calling us.
168 * Declared in the command_rec list with
169 * AP_INIT_TAKE3("directive", function, mconfig, where, help)
171 * static const char *handle_TAKE3(cmd_parms *cmd, void *mconfig,
172 * char *word1, char *word2, char *word3);
176 * Command handler for a TAKE12 directive. These can take either one or two
178 * - word2 is a NULL pointer if no second argument was specified.
179 * Declared in the command_rec list with
180 * AP_INIT_TAKE12("directive", function, mconfig, where, help)
182 * static const char *handle_TAKE12(cmd_parms *cmd, void *mconfig,
183 * char *word1, char *word2);
187 * Command handler for a TAKE123 directive. A TAKE123 directive can be given,
188 * as might be expected, one, two, or three arguments.
189 * - word2 is a NULL pointer if no second argument was specified.
190 * - word3 is a NULL pointer if no third argument was specified.
191 * Declared in the command_rec list with
192 * AP_INIT_TAKE123("directive", function, mconfig, where, help)
194 * static const char *handle_TAKE123(cmd_parms *cmd, void *mconfig,
195 * char *word1, char *word2, char *word3);
199 * Command handler for a TAKE13 directive. Either one or three arguments are
200 * permitted - no two-parameters-only syntax is allowed.
201 * - word2 and word3 are NULL pointers if only one argument was specified.
202 * Declared in the command_rec list with
203 * AP_INIT_TAKE13("directive", function, mconfig, where, help)
205 * static const char *handle_TAKE13(cmd_parms *cmd, void *mconfig,
206 * char *word1, char *word2, char *word3);
210 * Command handler for a TAKE23 directive. At least two and as many as three
211 * arguments must be specified.
212 * - word3 is a NULL pointer if no third argument was specified.
213 * Declared in the command_rec list with
214 * AP_INIT_TAKE23("directive", function, mconfig, where, help)
216 * static const char *handle_TAKE23(cmd_parms *cmd, void *mconfig,
217 * char *word1, char *word2, char *word3);
221 * Command handler for a ITERATE directive.
222 * - Handler is called once for each of n arguments given to the directive.
223 * - word1 points to each argument in turn.
224 * Declared in the command_rec list with
225 * AP_INIT_ITERATE("directive", function, mconfig, where, help)
227 * static const char *handle_ITERATE(cmd_parms *cmd, void *mconfig,
232 * Command handler for a ITERATE2 directive.
233 * - Handler is called once for each of the second and subsequent arguments
234 * given to the directive.
235 * - word1 is the same for each call for a particular directive instance (the
237 * - word2 points to each of the second and subsequent arguments in turn.
238 * Declared in the command_rec list with
239 * AP_INIT_ITERATE2("directive", function, mconfig, where, help)
241 * static const char *handle_ITERATE2(cmd_parms *cmd, void *mconfig,
242 * char *word1, char *word2);
245 /*--------------------------------------------------------------------------*/
247 /* These routines are strictly internal to this module, and support its */
248 /* operation. They are not referenced by any external portion of the */
251 /*--------------------------------------------------------------------------*/
254 * Locate our directory configuration record for the current request.
256 static x_cfg *our_dconfig(const request_rec *r)
258 return (x_cfg *) ap_get_module_config(r->per_dir_config, &example_module);
263 * Locate our server configuration record for the specified server.
265 static x_cfg *our_sconfig(const server_rec *s)
267 return (x_cfg *) ap_get_module_config(s->module_config, &example_module);
271 * Likewise for our configuration record for the specified request.
273 static x_cfg *our_rconfig(const request_rec *r)
275 return (x_cfg *) ap_get_module_config(r->request_config, &example_module);
280 * Likewise for our configuration record for a connection.
282 static x_cfg *our_cconfig(const conn_rec *c)
284 return (x_cfg *) ap_get_module_config(c->conn_config, &example_module);
288 * This routine sets up some module-wide cells if they haven't been already.
290 static void setup_module_cells(void)
293 * If we haven't already allocated our module-private pool, do so now.
295 if (x_pool == NULL) {
296 apr_pool_create(&x_pool, NULL);
299 * Likewise for the table of routine/environment pairs we visit outside of
302 if (static_calls_made == NULL) {
303 static_calls_made = apr_table_make(x_pool, 16);
308 * This routine is used to add a trace of a callback to the list. We're
309 * passed the server record (if available), the request record (if available),
310 * a pointer to our private configuration record (if available) for the
311 * environment to which the callback is supposed to apply, and some text. We
312 * turn this into a textual representation and add it to the tail of the list.
313 * The list can be displayed by the x_handler() routine.
315 * If the call occurs within a request context (i.e., we're passed a request
316 * record), we put the trace into the request apr_pool_t and attach it to the
317 * request via the notes mechanism. Otherwise, the trace gets added
318 * to the static (non-request-specific) list.
320 * Note that the r->notes table is only for storing strings; if you need to
321 * maintain per-request data of any other type, you need to use another
325 #define TRACE_NOTE "example-trace"
327 static void trace_add(server_rec *s, request_rec *r, x_cfg *mconfig,
334 const char *trace_copy;
337 * Make sure our pools and tables are set up - we need 'em.
339 setup_module_cells();
341 * Now, if we're in request-context, we use the request pool.
345 if ((trace_copy = apr_table_get(r->notes, TRACE_NOTE)) == NULL) {
351 * We're not in request context, so the trace gets attached to our
352 * module-wide pool. We do the create/destroy every time we're called
353 * in non-request context; this avoids leaking memory in some of
354 * the subsequent calls that allocate memory only once (such as the
355 * key formation below).
357 * Make a new sub-pool and copy any existing trace to it. Point the
358 * trace cell at the copied value.
360 apr_pool_create(&p, x_pool);
362 trace = apr_pstrdup(p, trace);
365 * Now, if we have a sub-pool from before, nuke it and replace with
366 * the one we just allocated.
368 if (x_subpool != NULL) {
369 apr_pool_destroy(x_subpool);
375 * If we weren't passed a configuration record, we can't figure out to
376 * what location this call applies. This only happens for co-routines
377 * that don't operate in a particular directory or server context. If we
378 * got a valid record, extract the location (directory or server) to which
381 where = (mconfig != NULL) ? mconfig->loc : "nowhere";
382 where = (where != NULL) ? where : "";
384 * Now, if we're not in request context, see if we've been called with
385 * this particular combination before. The apr_table_t is allocated in the
386 * module's private pool, which doesn't get destroyed.
391 key = apr_pstrcat(p, note, ":", where, NULL);
392 if (apr_table_get(static_calls_made, key) != NULL) {
394 * Been here, done this.
400 * First time for this combination of routine and environment -
401 * log it so we don't do it again.
403 apr_table_set(static_calls_made, key, "been here");
406 addon = apr_pstrcat(p,
409 " <dt><samp>", note, "</samp></dt>\n"
410 " <dd><samp>[", where, "]</samp></dd>\n"
414 sofar = (trace_copy == NULL) ? "" : trace_copy;
415 trace_copy = apr_pstrcat(p, sofar, addon, NULL);
417 apr_table_set(r->notes, TRACE_NOTE, trace_copy);
423 * You *could* change the following if you wanted to see the calling
424 * sequence reported in the server's error_log, but beware - almost all of
425 * these co-routines are called for every single request, and the impact
426 * on the size (and readability) of the error_log is considerable.
428 #define EXAMPLE_LOG_EACH 0
429 if (EXAMPLE_LOG_EACH && (s != NULL)) {
430 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, "mod_example: %s", note);
434 /*--------------------------------------------------------------------------*/
435 /* We prototyped the various syntax for command handlers (routines that */
436 /* are called when the configuration parser detects a directive declared */
437 /* by our module) earlier. Now we actually declare a "real" routine that */
438 /* will be invoked by the parser when our "real" directive is */
441 /* If a command handler encounters a problem processing the directive, it */
442 /* signals this fact by returning a non-NULL pointer to a string */
443 /* describing the problem. */
445 /* The magic return value DECLINE_CMD is used to deal with directives */
446 /* that might be declared by multiple modules. If the command handler */
447 /* returns NULL, the directive was processed; if it returns DECLINE_CMD, */
448 /* the next module (if any) that declares the directive is given a chance */
449 /* at it. If it returns any other value, it's treated as the text of an */
451 /*--------------------------------------------------------------------------*/
453 * Command handler for the NO_ARGS "Example" directive. All we do is mark the
454 * call in the trace log, and flag the applicability of the directive to the
455 * current location in that location's configuration record.
457 static const char *cmd_example(cmd_parms *cmd, void *mconfig)
459 x_cfg *cfg = (x_cfg *) mconfig;
465 trace_add(cmd->server, NULL, cfg, "cmd_example()");
469 /*--------------------------------------------------------------------------*/
471 /* Now we declare our content handlers, which are invoked when the server */
472 /* encounters a document which our module is supposed to have a chance to */
473 /* see. (See mod_mime's SetHandler and AddHandler directives, and the */
474 /* mod_info and mod_status examples, for more details.) */
476 /* Since content handlers are dumping data directly into the connection */
477 /* (using the r*() routines, such as rputs() and rprintf()) without */
478 /* intervention by other parts of the server, they need to make */
479 /* sure any accumulated HTTP headers are sent first. This is done by */
480 /* calling send_http_header(). Otherwise, no header will be sent at all, */
481 /* and the output sent to the client will actually be HTTP-uncompliant. */
482 /*--------------------------------------------------------------------------*/
484 * Sample content handler. All this does is display the call list that has
485 * been built up so far.
487 * The return value instructs the caller concerning what happened and what to
489 * OK ("we did our thing")
490 * DECLINED ("this isn't something with which we want to get involved")
491 * HTTP_mumble ("an error status should be reported")
493 static int x_handler(request_rec *r)
497 if (strcmp(r->handler, "example-handler")) {
501 dcfg = our_dconfig(r);
502 trace_add(r->server, r, dcfg, "x_handler()");
504 * We're about to start sending content, so we need to force the HTTP
505 * headers to be sent at this point. Otherwise, no headers will be sent
506 * at all. We can set any we like first, of course. **NOTE** Here's
507 * where you set the "Content-type" header, and you do so by putting it in
508 * r->content_type, *not* r->headers_out("Content-type"). If you don't
509 * set it, it will be filled in with the server's default type (typically
510 * "text/plain"). You *must* also ensure that r->content_type is lower
513 * We also need to start a timer so the server can know if the connexion
516 ap_set_content_type(r, "text/html");
518 * If we're only supposed to send header information (HEAD request), we're
521 if (r->header_only) {
526 * Now send our actual output. Since we tagged this as being
527 * "text/html", we need to embed any HTML.
529 ap_rputs(DOCTYPE_HTML_3_2, r);
530 ap_rputs("<HTML>\n", r);
531 ap_rputs(" <HEAD>\n", r);
532 ap_rputs(" <TITLE>mod_example Module Content-Handler Output\n", r);
533 ap_rputs(" </TITLE>\n", r);
534 ap_rputs(" </HEAD>\n", r);
535 ap_rputs(" <BODY>\n", r);
536 ap_rputs(" <H1><SAMP>mod_example</SAMP> Module Content-Handler Output\n", r);
537 ap_rputs(" </H1>\n", r);
538 ap_rputs(" <P>\n", r);
539 ap_rprintf(r, " Apache HTTP Server version: \"%s\"\n",
540 ap_get_server_version());
541 ap_rputs(" <BR>\n", r);
542 ap_rprintf(r, " Server built: \"%s\"\n", ap_get_server_built());
543 ap_rputs(" </P>\n", r);;
544 ap_rputs(" <P>\n", r);
545 ap_rputs(" The format for the callback trace is:\n", r);
546 ap_rputs(" </P>\n", r);
547 ap_rputs(" <DL>\n", r);
548 ap_rputs(" <DT><EM>n</EM>.<SAMP><routine-name>", r);
549 ap_rputs("(<routine-data>)</SAMP>\n", r);
550 ap_rputs(" </DT>\n", r);
551 ap_rputs(" <DD><SAMP>[<applies-to>]</SAMP>\n", r);
552 ap_rputs(" </DD>\n", r);
553 ap_rputs(" </DL>\n", r);
554 ap_rputs(" <P>\n", r);
555 ap_rputs(" The <SAMP><routine-data></SAMP> is supplied by\n", r);
556 ap_rputs(" the routine when it requests the trace,\n", r);
557 ap_rputs(" and the <SAMP><applies-to></SAMP> is extracted\n", r);
558 ap_rputs(" from the configuration record at the time of the trace.\n", r);
559 ap_rputs(" <STRONG>SVR()</STRONG> indicates a server environment\n", r);
560 ap_rputs(" (blank means the main or default server, otherwise it's\n", r);
561 ap_rputs(" the name of the VirtualHost); <STRONG>DIR()</STRONG>\n", r);
562 ap_rputs(" indicates a location in the URL or filesystem\n", r);
563 ap_rputs(" namespace.\n", r);
564 ap_rputs(" </P>\n", r);
565 ap_rprintf(r, " <H2>Static callbacks so far:</H2>\n <OL>\n%s </OL>\n",
567 ap_rputs(" <H2>Request-specific callbacks so far:</H2>\n", r);
568 ap_rprintf(r, " <OL>\n%s </OL>\n", apr_table_get(r->notes, TRACE_NOTE));
569 ap_rputs(" <H2>Environment for <EM>this</EM> call:</H2>\n", r);
570 ap_rputs(" <UL>\n", r);
571 ap_rprintf(r, " <LI>Applies-to: <SAMP>%s</SAMP>\n </LI>\n", dcfg->loc);
572 ap_rprintf(r, " <LI>\"Example\" directive declared here: %s\n </LI>\n",
573 (dcfg->local ? "YES" : "NO"));
574 ap_rprintf(r, " <LI>\"Example\" inherited: %s\n </LI>\n",
575 (dcfg->congenital ? "YES" : "NO"));
576 ap_rputs(" </UL>\n", r);
577 ap_rputs(" </BODY>\n", r);
578 ap_rputs("</HTML>\n", r);
580 * We're all done, so cancel the timeout we set. Since this is probably
581 * the end of the request we *could* assume this would be done during
582 * post-processing - but it's possible that another handler might be
583 * called and inherit our outstanding timer. Not good; to each its own.
586 * We did what we wanted to do, so tell the rest of the server we
592 /*--------------------------------------------------------------------------*/
594 /* Now let's declare routines for each of the callback phase in order. */
595 /* (That's the order in which they're listed in the callback list, *not */
596 /* the order in which the server calls them! See the command_rec */
597 /* declaration near the bottom of this file.) Note that these may be */
598 /* called for situations that don't relate primarily to our function - in */
599 /* other words, the fixup handler shouldn't assume that the request has */
600 /* to do with "example" stuff. */
602 /* With the exception of the content handler, all of our routines will be */
603 /* called for each request, unless an earlier handler from another module */
604 /* aborted the sequence. */
606 /* Handlers that are declared as "int" can return the following: */
608 /* OK Handler accepted the request and did its thing with it. */
609 /* DECLINED Handler took no action. */
610 /* HTTP_mumble Handler looked at request and found it wanting. */
612 /* What the server does after calling a module handler depends upon the */
613 /* handler's return value. In all cases, if the handler returns */
614 /* DECLINED, the server will continue to the next module with an handler */
615 /* for the current phase. However, if the handler return a non-OK, */
616 /* non-DECLINED status, the server aborts the request right there. If */
617 /* the handler returns OK, the server's next action is phase-specific; */
618 /* see the individual handler comments below for details. */
620 /*--------------------------------------------------------------------------*/
622 * This function is called during server initialisation. Any information
623 * that needs to be recorded must be in static cells, since there's no
624 * configuration record.
626 * There is no return value.
630 * This function is called when an heavy-weight process (such as a child) is
631 * being run down or destroyed. As with the child initialisation function,
632 * any information that needs to be recorded must be in static cells, since
633 * there's no configuration record.
635 * There is no return value.
639 * This function is called during server initialisation when an heavy-weight
640 * process (such as a child) is being initialised. As with the
641 * module initialisation function, any information that needs to be recorded
642 * must be in static cells, since there's no configuration record.
644 * There is no return value.
648 * This function gets called to create a per-directory configuration
649 * record. This will be called for the "default" server environment, and for
650 * each directory for which the parser finds any of our directives applicable.
651 * If a directory doesn't have any of our directives involved (i.e., they
652 * aren't in the .htaccess file, or a <Location>, <Directory>, or related
653 * block), this routine will *not* be called - the configuration for the
654 * closest ancestor is used.
656 * The return value is a pointer to the created module-specific
659 static void *x_create_dir_config(apr_pool_t *p, char *dirspec)
662 char *dname = dirspec;
665 * Allocate the space for our record from the pool supplied.
667 cfg = (x_cfg *) apr_pcalloc(p, sizeof(x_cfg));
669 * Now fill in the defaults. If there are any `parent' configuration
670 * records, they'll get merged as part of a separate callback.
674 cfg->cmode = CONFIG_MODE_DIRECTORY;
676 * Finally, add our trace to the callback list.
678 dname = (dname != NULL) ? dname : "";
679 cfg->loc = apr_pstrcat(p, "DIR(", dname, ")", NULL);
680 trace_add(NULL, NULL, cfg, "x_create_dir_config()");
685 * This function gets called to merge two per-directory configuration
686 * records. This is typically done to cope with things like .htaccess files
687 * or <Location> directives for directories that are beneath one for which a
688 * configuration record was already created. The routine has the
689 * responsibility of creating a new record and merging the contents of the
690 * other two into it appropriately. If the module doesn't declare a merge
691 * routine, the record for the closest ancestor location (that has one) is
694 * The routine MUST NOT modify any of its arguments!
696 * The return value is a pointer to the created module-specific structure
697 * containing the merged values.
699 static void *x_merge_dir_config(apr_pool_t *p, void *parent_conf,
703 x_cfg *merged_config = (x_cfg *) apr_pcalloc(p, sizeof(x_cfg));
704 x_cfg *pconf = (x_cfg *) parent_conf;
705 x_cfg *nconf = (x_cfg *) newloc_conf;
709 * Some things get copied directly from the more-specific record, rather
710 * than getting merged.
712 merged_config->local = nconf->local;
713 merged_config->loc = apr_pstrdup(p, nconf->loc);
715 * Others, like the setting of the `congenital' flag, get ORed in. The
716 * setting of that particular flag, for instance, is TRUE if it was ever
717 * true anywhere in the upstream configuration.
719 merged_config->congenital = (pconf->congenital | pconf->local);
721 * If we're merging records for two different types of environment (server
722 * and directory), mark the new record appropriately. Otherwise, inherit
725 merged_config->cmode =
726 (pconf->cmode == nconf->cmode) ? pconf->cmode : CONFIG_MODE_COMBO;
728 * Now just record our being called in the trace list. Include the
729 * locations we were asked to merge.
731 note = apr_pstrcat(p, "x_merge_dir_config(\"", pconf->loc, "\",\"",
732 nconf->loc, "\")", NULL);
733 trace_add(NULL, NULL, merged_config, note);
734 return (void *) merged_config;
738 * This function gets called to create a per-server configuration
739 * record. It will always be called for the "default" server.
741 * The return value is a pointer to the created module-specific
744 static void *x_create_server_config(apr_pool_t *p, server_rec *s)
748 char *sname = s->server_hostname;
751 * As with the x_create_dir_config() reoutine, we allocate and fill
752 * in an empty record.
754 cfg = (x_cfg *) apr_pcalloc(p, sizeof(x_cfg));
757 cfg->cmode = CONFIG_MODE_SERVER;
759 * Note that we were called in the trace list.
761 sname = (sname != NULL) ? sname : "";
762 cfg->loc = apr_pstrcat(p, "SVR(", sname, ")", NULL);
763 trace_add(s, NULL, cfg, "x_create_server_config()");
768 * This function gets called to merge two per-server configuration
769 * records. This is typically done to cope with things like virtual hosts and
770 * the default server configuration The routine has the responsibility of
771 * creating a new record and merging the contents of the other two into it
772 * appropriately. If the module doesn't declare a merge routine, the more
773 * specific existing record is used exclusively.
775 * The routine MUST NOT modify any of its arguments!
777 * The return value is a pointer to the created module-specific structure
778 * containing the merged values.
780 static void *x_merge_server_config(apr_pool_t *p, void *server1_conf,
784 x_cfg *merged_config = (x_cfg *) apr_pcalloc(p, sizeof(x_cfg));
785 x_cfg *s1conf = (x_cfg *) server1_conf;
786 x_cfg *s2conf = (x_cfg *) server2_conf;
790 * Our inheritance rules are our own, and part of our module's semantics.
791 * Basically, just note whence we came.
793 merged_config->cmode =
794 (s1conf->cmode == s2conf->cmode) ? s1conf->cmode : CONFIG_MODE_COMBO;
795 merged_config->local = s2conf->local;
796 merged_config->congenital = (s1conf->congenital | s1conf->local);
797 merged_config->loc = apr_pstrdup(p, s2conf->loc);
799 * Trace our call, including what we were asked to merge.
801 note = apr_pstrcat(p, "x_merge_server_config(\"", s1conf->loc, "\",\"",
802 s2conf->loc, "\")", NULL);
803 trace_add(NULL, NULL, merged_config, note);
804 return (void *) merged_config;
808 * This routine is called before the server processes the configuration
809 * files. There is no return value.
811 static int x_pre_config(apr_pool_t *pconf, apr_pool_t *plog,
815 * Log the call and exit.
817 trace_add(NULL, NULL, NULL, "x_pre_config()");
823 * This routine is called to perform any module-specific fixing of header
824 * fields, et cetera. It is invoked just before any content-handler.
826 * The return value is OK, DECLINED, or HTTP_mumble. If we return OK, the
827 * server will still call any remaining modules with an handler for this
830 static int x_post_config(apr_pool_t *pconf, apr_pool_t *plog,
831 apr_pool_t *ptemp, server_rec *s)
834 * Log the call and exit.
836 trace_add(NULL, NULL, NULL, "x_post_config()");
841 * This routine is called to perform any module-specific log file
842 * openings. It is invoked just before the post_config phase
844 * The return value is OK, DECLINED, or HTTP_mumble. If we return OK, the
845 * server will still call any remaining modules with an handler for this
848 static int x_open_logs(apr_pool_t *pconf, apr_pool_t *plog,
849 apr_pool_t *ptemp, server_rec *s)
852 * Log the call and exit.
854 trace_add(s, NULL, NULL, "x_open_logs()");
859 * All our process-death routine does is add its trace to the log.
861 static apr_status_t x_child_exit(void *data)
864 server_rec *s = data;
865 char *sname = s->server_hostname;
868 * The arbitrary text we add to our trace entry indicates for which server
869 * we're being called.
871 sname = (sname != NULL) ? sname : "";
872 note = apr_pstrcat(s->process->pool, "x_child_exit(", sname, ")", NULL);
873 trace_add(s, NULL, NULL, note);
878 * All our process initialiser does is add its trace to the log.
880 static void x_child_init(apr_pool_t *p, server_rec *s)
883 char *sname = s->server_hostname;
886 * Set up any module cells that ought to be initialised.
888 setup_module_cells();
890 * The arbitrary text we add to our trace entry indicates for which server
891 * we're being called.
893 sname = (sname != NULL) ? sname : "";
894 note = apr_pstrcat(p, "x_child_init(", sname, ")", NULL);
895 trace_add(s, NULL, NULL, note);
897 apr_pool_cleanup_register(p, s, x_child_exit, x_child_exit);
901 * XXX: This routine is called XXX
903 * The return value is OK, DECLINED, or HTTP_mumble. If we return OK, the
904 * server will still call any remaining modules with an handler for this
908 static const char *x_http_method(const request_rec *r)
912 cfg = our_dconfig(r);
914 * Log the call and exit.
916 trace_add(r->server, NULL, cfg, "x_http_method()");
921 * XXX: This routine is called XXX
923 * The return value is OK, DECLINED, or HTTP_mumble. If we return OK, the
924 * server will still call any remaining modules with an handler for this
927 static apr_port_t x_default_port(const request_rec *r)
931 cfg = our_dconfig(r);
933 * Log the call and exit.
935 trace_add(r->server, NULL, cfg, "x_default_port()");
941 * XXX: This routine is called XXX
943 * The return value is OK, DECLINED, or HTTP_mumble. If we return OK, the
944 * server will still call any remaining modules with an handler for this
947 static void x_insert_filter(request_rec *r)
951 cfg = our_dconfig(r);
953 * Log the call and exit.
955 trace_add(r->server, NULL, cfg, "x_insert_filter()");
959 * XXX: This routine is called XXX
961 * The return value is OK, DECLINED, or HTTP_mumble. If we return OK, the
962 * server will still call any remaining modules with an handler for this
965 static int x_quick_handler(request_rec *r, int lookup_uri)
969 cfg = our_dconfig(r);
971 * Log the call and exit.
973 trace_add(r->server, NULL, cfg, "x_post_config()");
978 * This routine is called just after the server accepts the connection,
979 * but before it is handed off to a protocol module to be served. The point
980 * of this hook is to allow modules an opportunity to modify the connection
981 * as soon as possible. The core server uses this phase to setup the
982 * connection record based on the type of connection that is being used.
984 * The return value is OK, DECLINED, or HTTP_mumble. If we return OK, the
985 * server will still call any remaining modules with an handler for this
988 static int x_pre_connection(conn_rec *c, void *csd)
992 cfg = our_cconfig(c);
995 * Log the call and exit.
997 trace_add(r->server, NULL, cfg, "x_post_config()");
1002 /* This routine is used to actually process the connection that was received.
1003 * Only protocol modules should implement this hook, as it gives them an
1004 * opportunity to replace the standard HTTP processing with processing for
1005 * some other protocol. Both echo and POP3 modules are available as
1008 * The return VALUE is OK, DECLINED, or HTTP_mumble. If we return OK, no
1009 * further modules are called for this phase.
1011 static int x_process_connection(conn_rec *c)
1017 * This routine is called after the request has been read but before any other
1018 * phases have been processed. This allows us to make decisions based upon
1019 * the input header fields.
1021 * The return value is OK, DECLINED, or HTTP_mumble. If we return OK, no
1022 * further modules are called for this phase.
1024 static int x_post_read_request(request_rec *r)
1028 cfg = our_dconfig(r);
1030 * We don't actually *do* anything here, except note the fact that we were
1033 trace_add(r->server, r, cfg, "x_post_read_request()");
1038 * This routine gives our module an opportunity to translate the URI into an
1039 * actual filename. If we don't do anything special, the server's default
1040 * rules (Alias directives and the like) will continue to be followed.
1042 * The return value is OK, DECLINED, or HTTP_mumble. If we return OK, no
1043 * further modules are called for this phase.
1045 static int x_translate_handler(request_rec *r)
1050 cfg = our_dconfig(r);
1052 * We don't actually *do* anything here, except note the fact that we were
1055 trace_add(r->server, r, cfg, "x_translate_handler()");
1060 * this routine gives our module another chance to examine the request
1061 * headers and to take special action. This is the first phase whose
1062 * hooks' configuration directives can appear inside the <Directory>
1063 * and similar sections, because at this stage the URI has been mapped
1064 * to the filename. For example this phase can be used to block evil
1065 * clients, while little resources were wasted on these.
1067 * The return value is OK, DECLINED, or HTTP_mumble. If we return OK,
1068 * the server will still call any remaining modules with an handler
1071 static int x_header_parser_handler(request_rec *r)
1076 cfg = our_dconfig(r);
1078 * We don't actually *do* anything here, except note the fact that we were
1081 trace_add(r->server, r, cfg, "header_parser_handler()");
1087 * This routine is called to check the authentication information sent with
1088 * the request (such as looking up the user in a database and verifying that
1089 * the [encrypted] password sent matches the one in the database).
1091 * The return value is OK, DECLINED, or some HTTP_mumble error (typically
1092 * HTTP_UNAUTHORIZED). If we return OK, no other modules are given a chance
1093 * at the request during this phase.
1095 static int x_check_user_id(request_rec *r)
1100 cfg = our_dconfig(r);
1102 * Don't do anything except log the call.
1104 trace_add(r->server, r, cfg, "x_check_user_id()");
1109 * This routine is called to check to see if the resource being requested
1110 * requires authorisation.
1112 * The return value is OK, DECLINED, or HTTP_mumble. If we return OK, no
1113 * other modules are called during this phase.
1115 * If *all* modules return DECLINED, the request is aborted with a server
1118 static int x_auth_checker(request_rec *r)
1123 cfg = our_dconfig(r);
1125 * Log the call and return OK, or access will be denied (even though we
1126 * didn't actually do anything).
1128 trace_add(r->server, r, cfg, "x_auth_checker()");
1133 * This routine is called to check for any module-specific restrictions placed
1134 * upon the requested resource. (See the mod_access module for an example.)
1136 * The return value is OK, DECLINED, or HTTP_mumble. All modules with an
1137 * handler for this phase are called regardless of whether their predecessors
1138 * return OK or DECLINED. The first one to return any other status, however,
1139 * will abort the sequence (and the request) as usual.
1141 static int x_access_checker(request_rec *r)
1146 cfg = our_dconfig(r);
1147 trace_add(r->server, r, cfg, "x_access_checker()");
1152 * This routine is called to determine and/or set the various document type
1153 * information bits, like Content-type (via r->content_type), language, et
1156 * The return value is OK, DECLINED, or HTTP_mumble. If we return OK, no
1157 * further modules are given a chance at the request for this phase.
1159 static int x_type_checker(request_rec *r)
1164 cfg = our_dconfig(r);
1166 * Log the call, but don't do anything else - and report truthfully that
1167 * we didn't do anything.
1169 trace_add(r->server, r, cfg, "x_type_checker()");
1174 * This routine is called to perform any module-specific fixing of header
1175 * fields, et cetera. It is invoked just before any content-handler.
1177 * The return value is OK, DECLINED, or HTTP_mumble. If we return OK, the
1178 * server will still call any remaining modules with an handler for this
1181 static int x_fixer_upper(request_rec *r)
1186 cfg = our_dconfig(r);
1188 * Log the call and exit.
1190 trace_add(r->server, r, cfg, "x_fixer_upper()");
1195 * This routine is called to perform any module-specific logging activities
1196 * over and above the normal server things.
1198 * The return value is OK, DECLINED, or HTTP_mumble. If we return OK, any
1199 * remaining modules with an handler for this phase will still be called.
1201 static int x_logger(request_rec *r)
1206 cfg = our_dconfig(r);
1207 trace_add(r->server, r, cfg, "x_logger()");
1211 /*--------------------------------------------------------------------------*/
1213 /* Which functions are responsible for which hooks in the server. */
1215 /*--------------------------------------------------------------------------*/
1217 * Each function our module provides to handle a particular hook is
1218 * specified here. The functions are registered using
1219 * ap_hook_foo(name, predecessors, successors, position)
1220 * where foo is the name of the hook.
1222 * The args are as follows:
1223 * name -> the name of the function to call.
1224 * predecessors -> a list of modules whose calls to this hook must be
1225 * invoked before this module.
1226 * successors -> a list of modules whose calls to this hook must be
1227 * invoked after this module.
1228 * position -> The relative position of this module. One of
1229 * APR_HOOK_FIRST, APR_HOOK_MIDDLE, or APR_HOOK_LAST.
1230 * Most modules will use APR_HOOK_MIDDLE. If multiple
1231 * modules use the same relative position, Apache will
1232 * determine which to call first.
1233 * If your module relies on another module to run first,
1234 * or another module running after yours, use the
1235 * predecessors and/or successors.
1237 * The number in brackets indicates the order in which the routine is called
1238 * during request processing. Note that not all routines are necessarily
1239 * called (such as if a resource doesn't have access restrictions).
1240 * The actual delivery of content to the browser [9] is not handled by
1241 * a hook; see the handler declarations below.
1243 static void x_register_hooks(apr_pool_t *p)
1245 ap_hook_pre_config(x_pre_config, NULL, NULL, APR_HOOK_MIDDLE);
1246 ap_hook_post_config(x_post_config, NULL, NULL, APR_HOOK_MIDDLE);
1247 ap_hook_open_logs(x_open_logs, NULL, NULL, APR_HOOK_MIDDLE);
1248 ap_hook_child_init(x_child_init, NULL, NULL, APR_HOOK_MIDDLE);
1249 ap_hook_handler(x_handler, NULL, NULL, APR_HOOK_MIDDLE);
1250 ap_hook_quick_handler(x_quick_handler, NULL, NULL, APR_HOOK_MIDDLE);
1251 ap_hook_pre_connection(x_pre_connection, NULL, NULL, APR_HOOK_MIDDLE);
1252 ap_hook_process_connection(x_process_connection, NULL, NULL, APR_HOOK_MIDDLE);
1253 /* [1] post read_request handling */
1254 ap_hook_post_read_request(x_post_read_request, NULL, NULL,
1256 ap_hook_log_transaction(x_logger, NULL, NULL, APR_HOOK_MIDDLE);
1258 ap_hook_http_method(x_http_method, NULL, NULL, APR_HOOK_MIDDLE);
1259 ap_hook_default_port(x_default_port, NULL, NULL, APR_HOOK_MIDDLE);
1261 ap_hook_translate_name(x_translate_handler, NULL, NULL, APR_HOOK_MIDDLE);
1262 ap_hook_header_parser(x_header_parser_handler, NULL, NULL, APR_HOOK_MIDDLE);
1263 ap_hook_check_user_id(x_check_user_id, NULL, NULL, APR_HOOK_MIDDLE);
1264 ap_hook_fixups(x_fixer_upper, NULL, NULL, APR_HOOK_MIDDLE);
1265 ap_hook_type_checker(x_type_checker, NULL, NULL, APR_HOOK_MIDDLE);
1266 ap_hook_access_checker(x_access_checker, NULL, NULL, APR_HOOK_MIDDLE);
1267 ap_hook_auth_checker(x_auth_checker, NULL, NULL, APR_HOOK_MIDDLE);
1268 ap_hook_insert_filter(x_insert_filter, NULL, NULL, APR_HOOK_MIDDLE);
1271 /*--------------------------------------------------------------------------*/
1273 /* All of the routines have been declared now. Here's the list of */
1274 /* directives specific to our module, and information about where they */
1275 /* may appear and how the command parser should pass them to us for */
1276 /* processing. Note that care must be taken to ensure that there are NO */
1277 /* collisions of directive names between modules. */
1279 /*--------------------------------------------------------------------------*/
1281 * List of directives specific to our module.
1283 static const command_rec x_cmds[] =
1286 "Example", /* directive name */
1287 cmd_example, /* config action routine */
1288 NULL, /* argument to include in call */
1289 OR_OPTIONS, /* where available */
1290 "Example directive - no arguments" /* directive description */
1294 /*--------------------------------------------------------------------------*/
1296 /* Finally, the list of callback routines and data structures that provide */
1297 /* the static hooks into our module from the other parts of the server. */
1299 /*--------------------------------------------------------------------------*/
1301 * Module definition for configuration. If a particular callback is not
1302 * needed, replace its routine name below with the word NULL.
1304 module AP_MODULE_DECLARE_DATA example_module =
1306 STANDARD20_MODULE_STUFF,
1307 x_create_dir_config, /* per-directory config creator */
1308 x_merge_dir_config, /* dir config merger */
1309 x_create_server_config, /* server config creator */
1310 x_merge_server_config, /* server config merger */
1311 x_cmds, /* command table */
1312 x_register_hooks, /* set up other request processing hooks */