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_'.
31
* Some of the code in this module has problems.
32
* Before using it to base your work on, see
34
* http://issues.apache.org/bugzilla/show_bug.cgi?id=29709
35
* http://issues.apache.org/bugzilla/show_bug.cgi?id=32051
39
#include "http_config.h"
40
#include "http_core.h"
42
#include "http_main.h"
43
#include "http_protocol.h"
44
#include "http_request.h"
45
#include "util_script.h"
46
#include "http_connection.h"
48
#include "apr_strings.h"
52
/*--------------------------------------------------------------------------*/
54
/* Data declarations. */
56
/* Here are the static cells and structure declarations private to our */
59
/*--------------------------------------------------------------------------*/
62
* Sample configuration record. Used for both per-directory and per-server
65
* It's perfectly reasonable to have two different structures for the two
66
* different environments. The same command handlers will be called for
67
* both, though, so the handlers need to be able to tell them apart. One
68
* possibility is for both structures to start with an int which is 0 for
69
* one and 1 for the other.
71
* Note that while the per-directory and per-server configuration records are
72
* available to most of the module handlers, they should be treated as
73
* READ-ONLY by all except the command and merge handlers. Sometimes handlers
74
* are handed a record that applies to the current location by implication or
75
* inheritance, and modifying it will change the rules for other locations.
77
typedef struct x_cfg {
78
int cmode; /* Environment to which record applies
79
* (directory, server, or combination).
81
#define CONFIG_MODE_SERVER 1
82
#define CONFIG_MODE_DIRECTORY 2
83
#define CONFIG_MODE_COMBO 3 /* Shouldn't ever happen. */
84
int local; /* Boolean: "Example" directive declared
87
int congenital; /* Boolean: did we inherit an "Example"? */
88
char *trace; /* Pointer to trace string. */
89
char *loc; /* Location to which this record applies. */
93
* Let's set up a module-local static cell to point to the accreting callback
94
* trace. As each API callback is made to us, we'll tack on the particulars
95
* to whatever we've already recorded. To avoid massive memory bloat as
96
* directories are walked again and again, we record the routine/environment
97
* the first time (non-request context only), and ignore subsequent calls for
98
* the same routine/environment.
100
static const char *trace = NULL;
101
static apr_table_t *static_calls_made = NULL;
104
* To avoid leaking memory from pools other than the per-request one, we
105
* allocate a module-private pool, and then use a sub-pool of that which gets
106
* freed each time we modify the trace. That way previous layers of trace
107
* data don't get lost.
109
static apr_pool_t *x_pool = NULL;
110
static apr_pool_t *x_subpool = NULL;
113
* Declare ourselves so the configuration routines can find and know us.
114
* We'll fill it in at the end of the module.
116
module AP_MODULE_DECLARE_DATA example_module;
118
/*--------------------------------------------------------------------------*/
120
/* The following pseudo-prototype declarations illustrate the parameters */
121
/* passed to command handlers for the different types of directive */
122
/* syntax. If an argument was specified in the directive definition */
123
/* (look for "command_rec" below), it's available to the command handler */
124
/* via the (void *) info field in the cmd_parms argument passed to the */
125
/* handler (cmd->info for the examples below). */
127
/*--------------------------------------------------------------------------*/
130
* Command handler for a NO_ARGS directive. Declared in the command_rec
132
* AP_INIT_NO_ARGS("directive", function, mconfig, where, help)
134
* static const char *handle_NO_ARGS(cmd_parms *cmd, void *mconfig);
138
* Command handler for a RAW_ARGS directive. The "args" argument is the text
139
* of the commandline following the directive itself. Declared in the
140
* command_rec list with
141
* AP_INIT_RAW_ARGS("directive", function, mconfig, where, help)
143
* static const char *handle_RAW_ARGS(cmd_parms *cmd, void *mconfig,
148
* Command handler for a FLAG directive. The single parameter is passed in
149
* "bool", which is either zero or not for Off or On respectively.
150
* Declared in the command_rec list with
151
* AP_INIT_FLAG("directive", function, mconfig, where, help)
153
* static const char *handle_FLAG(cmd_parms *cmd, void *mconfig, int bool);
157
* Command handler for a TAKE1 directive. The single parameter is passed in
158
* "word1". Declared in the command_rec list with
159
* AP_INIT_TAKE1("directive", function, mconfig, where, help)
161
* static const char *handle_TAKE1(cmd_parms *cmd, void *mconfig,
166
* Command handler for a TAKE2 directive. TAKE2 commands must always have
167
* exactly two arguments. Declared in the command_rec list with
168
* AP_INIT_TAKE2("directive", function, mconfig, where, help)
170
* static const char *handle_TAKE2(cmd_parms *cmd, void *mconfig,
171
* char *word1, char *word2);
175
* Command handler for a TAKE3 directive. Like TAKE2, these must have exactly
176
* three arguments, or the parser complains and doesn't bother calling us.
177
* Declared in the command_rec list with
178
* AP_INIT_TAKE3("directive", function, mconfig, where, help)
180
* static const char *handle_TAKE3(cmd_parms *cmd, void *mconfig,
181
* char *word1, char *word2, char *word3);
185
* Command handler for a TAKE12 directive. These can take either one or two
187
* - word2 is a NULL pointer if no second argument was specified.
188
* Declared in the command_rec list with
189
* AP_INIT_TAKE12("directive", function, mconfig, where, help)
191
* static const char *handle_TAKE12(cmd_parms *cmd, void *mconfig,
192
* char *word1, char *word2);
196
* Command handler for a TAKE123 directive. A TAKE123 directive can be given,
197
* as might be expected, one, two, or three arguments.
198
* - word2 is a NULL pointer if no second argument was specified.
199
* - word3 is a NULL pointer if no third argument was specified.
200
* Declared in the command_rec list with
201
* AP_INIT_TAKE123("directive", function, mconfig, where, help)
203
* static const char *handle_TAKE123(cmd_parms *cmd, void *mconfig,
204
* char *word1, char *word2, char *word3);
208
* Command handler for a TAKE13 directive. Either one or three arguments are
209
* permitted - no two-parameters-only syntax is allowed.
210
* - word2 and word3 are NULL pointers if only one argument was specified.
211
* Declared in the command_rec list with
212
* AP_INIT_TAKE13("directive", function, mconfig, where, help)
214
* static const char *handle_TAKE13(cmd_parms *cmd, void *mconfig,
215
* char *word1, char *word2, char *word3);
219
* Command handler for a TAKE23 directive. At least two and as many as three
220
* arguments must be specified.
221
* - word3 is a NULL pointer if no third argument was specified.
222
* Declared in the command_rec list with
223
* AP_INIT_TAKE23("directive", function, mconfig, where, help)
225
* static const char *handle_TAKE23(cmd_parms *cmd, void *mconfig,
226
* char *word1, char *word2, char *word3);
230
* Command handler for a ITERATE directive.
231
* - Handler is called once for each of n arguments given to the directive.
232
* - word1 points to each argument in turn.
233
* Declared in the command_rec list with
234
* AP_INIT_ITERATE("directive", function, mconfig, where, help)
236
* static const char *handle_ITERATE(cmd_parms *cmd, void *mconfig,
241
* Command handler for a ITERATE2 directive.
242
* - Handler is called once for each of the second and subsequent arguments
243
* given to the directive.
244
* - word1 is the same for each call for a particular directive instance (the
246
* - word2 points to each of the second and subsequent arguments in turn.
247
* Declared in the command_rec list with
248
* AP_INIT_ITERATE2("directive", function, mconfig, where, help)
250
* static const char *handle_ITERATE2(cmd_parms *cmd, void *mconfig,
251
* char *word1, char *word2);
254
/*--------------------------------------------------------------------------*/
256
/* These routines are strictly internal to this module, and support its */
257
/* operation. They are not referenced by any external portion of the */
260
/*--------------------------------------------------------------------------*/
263
* Locate our directory configuration record for the current request.
265
static x_cfg *our_dconfig(const request_rec *r)
267
return (x_cfg *) ap_get_module_config(r->per_dir_config, &example_module);
272
* Locate our server configuration record for the specified server.
274
static x_cfg *our_sconfig(const server_rec *s)
276
return (x_cfg *) ap_get_module_config(s->module_config, &example_module);
280
* Likewise for our configuration record for the specified request.
282
static x_cfg *our_rconfig(const request_rec *r)
284
return (x_cfg *) ap_get_module_config(r->request_config, &example_module);
289
* Likewise for our configuration record for a connection.
291
static x_cfg *our_cconfig(const conn_rec *c)
293
return (x_cfg *) ap_get_module_config(c->conn_config, &example_module);
297
* This routine sets up some module-wide cells if they haven't been already.
299
static void setup_module_cells(void)
302
* If we haven't already allocated our module-private pool, do so now.
304
if (x_pool == NULL) {
305
apr_pool_create(&x_pool, NULL);
308
* Likewise for the table of routine/environment pairs we visit outside of
311
if (static_calls_made == NULL) {
312
static_calls_made = apr_table_make(x_pool, 16);
317
* This routine is used to add a trace of a callback to the list. We're
318
* passed the server record (if available), the request record (if available),
319
* a pointer to our private configuration record (if available) for the
320
* environment to which the callback is supposed to apply, and some text. We
321
* turn this into a textual representation and add it to the tail of the list.
322
* The list can be displayed by the x_handler() routine.
324
* If the call occurs within a request context (i.e., we're passed a request
325
* record), we put the trace into the request apr_pool_t and attach it to the
326
* request via the notes mechanism. Otherwise, the trace gets added
327
* to the static (non-request-specific) list.
329
* Note that the r->notes table is only for storing strings; if you need to
330
* maintain per-request data of any other type, you need to use another
334
#define TRACE_NOTE "example-trace"
336
static void trace_add(server_rec *s, request_rec *r, x_cfg *mconfig,
343
const char *trace_copy;
346
* Make sure our pools and tables are set up - we need 'em.
348
setup_module_cells();
350
* Now, if we're in request-context, we use the request pool.
354
if ((trace_copy = apr_table_get(r->notes, TRACE_NOTE)) == NULL) {
360
* We're not in request context, so the trace gets attached to our
361
* module-wide pool. We do the create/destroy every time we're called
362
* in non-request context; this avoids leaking memory in some of
363
* the subsequent calls that allocate memory only once (such as the
364
* key formation below).
366
* Make a new sub-pool and copy any existing trace to it. Point the
367
* trace cell at the copied value.
369
apr_pool_create(&p, x_pool);
371
trace = apr_pstrdup(p, trace);
374
* Now, if we have a sub-pool from before, nuke it and replace with
375
* the one we just allocated.
377
if (x_subpool != NULL) {
378
apr_pool_destroy(x_subpool);
384
* If we weren't passed a configuration record, we can't figure out to
385
* what location this call applies. This only happens for co-routines
386
* that don't operate in a particular directory or server context. If we
387
* got a valid record, extract the location (directory or server) to which
390
where = (mconfig != NULL) ? mconfig->loc : "nowhere";
391
where = (where != NULL) ? where : "";
393
* Now, if we're not in request context, see if we've been called with
394
* this particular combination before. The apr_table_t is allocated in the
395
* module's private pool, which doesn't get destroyed.
400
key = apr_pstrcat(p, note, ":", where, NULL);
401
if (apr_table_get(static_calls_made, key) != NULL) {
403
* Been here, done this.
409
* First time for this combination of routine and environment -
410
* log it so we don't do it again.
412
apr_table_set(static_calls_made, key, "been here");
415
addon = apr_pstrcat(p,
418
" <dt><samp>", note, "</samp></dt>\n"
419
" <dd><samp>[", where, "]</samp></dd>\n"
423
sofar = (trace_copy == NULL) ? "" : trace_copy;
424
trace_copy = apr_pstrcat(p, sofar, addon, NULL);
426
apr_table_set(r->notes, TRACE_NOTE, trace_copy);
432
* You *could* change the following if you wanted to see the calling
433
* sequence reported in the server's error_log, but beware - almost all of
434
* these co-routines are called for every single request, and the impact
435
* on the size (and readability) of the error_log is considerable.
437
#define EXAMPLE_LOG_EACH 0
438
if (EXAMPLE_LOG_EACH && (s != NULL)) {
439
ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, "mod_example: %s", note);
443
/*--------------------------------------------------------------------------*/
444
/* We prototyped the various syntax for command handlers (routines that */
445
/* are called when the configuration parser detects a directive declared */
446
/* by our module) earlier. Now we actually declare a "real" routine that */
447
/* will be invoked by the parser when our "real" directive is */
450
/* If a command handler encounters a problem processing the directive, it */
451
/* signals this fact by returning a non-NULL pointer to a string */
452
/* describing the problem. */
454
/* The magic return value DECLINE_CMD is used to deal with directives */
455
/* that might be declared by multiple modules. If the command handler */
456
/* returns NULL, the directive was processed; if it returns DECLINE_CMD, */
457
/* the next module (if any) that declares the directive is given a chance */
458
/* at it. If it returns any other value, it's treated as the text of an */
460
/*--------------------------------------------------------------------------*/
462
* Command handler for the NO_ARGS "Example" directive. All we do is mark the
463
* call in the trace log, and flag the applicability of the directive to the
464
* current location in that location's configuration record.
466
static const char *cmd_example(cmd_parms *cmd, void *mconfig)
468
x_cfg *cfg = (x_cfg *) mconfig;
474
trace_add(cmd->server, NULL, cfg, "cmd_example()");
478
/*--------------------------------------------------------------------------*/
480
/* Now we declare our content handlers, which are invoked when the server */
481
/* encounters a document which our module is supposed to have a chance to */
482
/* see. (See mod_mime's SetHandler and AddHandler directives, and the */
483
/* mod_info and mod_status examples, for more details.) */
485
/* Since content handlers are dumping data directly into the connection */
486
/* (using the r*() routines, such as rputs() and rprintf()) without */
487
/* intervention by other parts of the server, they need to make */
488
/* sure any accumulated HTTP headers are sent first. This is done by */
489
/* calling send_http_header(). Otherwise, no header will be sent at all, */
490
/* and the output sent to the client will actually be HTTP-uncompliant. */
491
/*--------------------------------------------------------------------------*/
493
* Sample content handler. All this does is display the call list that has
494
* been built up so far.
496
* The return value instructs the caller concerning what happened and what to
498
* OK ("we did our thing")
499
* DECLINED ("this isn't something with which we want to get involved")
500
* HTTP_mumble ("an error status should be reported")
502
static int x_handler(request_rec *r)
506
if (strcmp(r->handler, "example-handler")) {
510
dcfg = our_dconfig(r);
511
trace_add(r->server, r, dcfg, "x_handler()");
513
* We're about to start sending content, so we need to force the HTTP
514
* headers to be sent at this point. Otherwise, no headers will be sent
515
* at all. We can set any we like first, of course. **NOTE** Here's
516
* where you set the "Content-type" header, and you do so by putting it in
517
* r->content_type, *not* r->headers_out("Content-type"). If you don't
518
* set it, it will be filled in with the server's default type (typically
519
* "text/plain"). You *must* also ensure that r->content_type is lower
522
* We also need to start a timer so the server can know if the connexion
525
ap_set_content_type(r, "text/html");
527
* If we're only supposed to send header information (HEAD request), we're
530
if (r->header_only) {
535
* Now send our actual output. Since we tagged this as being
536
* "text/html", we need to embed any HTML.
538
ap_rputs(DOCTYPE_HTML_3_2, r);
539
ap_rputs("<HTML>\n", r);
540
ap_rputs(" <HEAD>\n", r);
541
ap_rputs(" <TITLE>mod_example Module Content-Handler Output\n", r);
542
ap_rputs(" </TITLE>\n", r);
543
ap_rputs(" </HEAD>\n", r);
544
ap_rputs(" <BODY>\n", r);
545
ap_rputs(" <H1><SAMP>mod_example</SAMP> Module Content-Handler Output\n", r);
546
ap_rputs(" </H1>\n", r);
547
ap_rputs(" <P>\n", r);
548
ap_rprintf(r, " Apache HTTP Server version: \"%s\"\n",
549
ap_get_server_version());
550
ap_rputs(" <BR>\n", r);
551
ap_rprintf(r, " Server built: \"%s\"\n", ap_get_server_built());
552
ap_rputs(" </P>\n", r);;
553
ap_rputs(" <P>\n", r);
554
ap_rputs(" The format for the callback trace is:\n", r);
555
ap_rputs(" </P>\n", r);
556
ap_rputs(" <DL>\n", r);
557
ap_rputs(" <DT><EM>n</EM>.<SAMP><routine-name>", r);
558
ap_rputs("(<routine-data>)</SAMP>\n", r);
559
ap_rputs(" </DT>\n", r);
560
ap_rputs(" <DD><SAMP>[<applies-to>]</SAMP>\n", r);
561
ap_rputs(" </DD>\n", r);
562
ap_rputs(" </DL>\n", r);
563
ap_rputs(" <P>\n", r);
564
ap_rputs(" The <SAMP><routine-data></SAMP> is supplied by\n", r);
565
ap_rputs(" the routine when it requests the trace,\n", r);
566
ap_rputs(" and the <SAMP><applies-to></SAMP> is extracted\n", r);
567
ap_rputs(" from the configuration record at the time of the trace.\n", r);
568
ap_rputs(" <STRONG>SVR()</STRONG> indicates a server environment\n", r);
569
ap_rputs(" (blank means the main or default server, otherwise it's\n", r);
570
ap_rputs(" the name of the VirtualHost); <STRONG>DIR()</STRONG>\n", r);
571
ap_rputs(" indicates a location in the URL or filesystem\n", r);
572
ap_rputs(" namespace.\n", r);
573
ap_rputs(" </P>\n", r);
574
ap_rprintf(r, " <H2>Static callbacks so far:</H2>\n <OL>\n%s </OL>\n",
576
ap_rputs(" <H2>Request-specific callbacks so far:</H2>\n", r);
577
ap_rprintf(r, " <OL>\n%s </OL>\n", apr_table_get(r->notes, TRACE_NOTE));
578
ap_rputs(" <H2>Environment for <EM>this</EM> call:</H2>\n", r);
579
ap_rputs(" <UL>\n", r);
580
ap_rprintf(r, " <LI>Applies-to: <SAMP>%s</SAMP>\n </LI>\n", dcfg->loc);
581
ap_rprintf(r, " <LI>\"Example\" directive declared here: %s\n </LI>\n",
582
(dcfg->local ? "YES" : "NO"));
583
ap_rprintf(r, " <LI>\"Example\" inherited: %s\n </LI>\n",
584
(dcfg->congenital ? "YES" : "NO"));
585
ap_rputs(" </UL>\n", r);
586
ap_rputs(" </BODY>\n", r);
587
ap_rputs("</HTML>\n", r);
589
* We're all done, so cancel the timeout we set. Since this is probably
590
* the end of the request we *could* assume this would be done during
591
* post-processing - but it's possible that another handler might be
592
* called and inherit our outstanding timer. Not good; to each its own.
595
* We did what we wanted to do, so tell the rest of the server we
601
/*--------------------------------------------------------------------------*/
603
/* Now let's declare routines for each of the callback phase in order. */
604
/* (That's the order in which they're listed in the callback list, *not */
605
/* the order in which the server calls them! See the command_rec */
606
/* declaration near the bottom of this file.) Note that these may be */
607
/* called for situations that don't relate primarily to our function - in */
608
/* other words, the fixup handler shouldn't assume that the request has */
609
/* to do with "example" stuff. */
611
/* With the exception of the content handler, all of our routines will be */
612
/* called for each request, unless an earlier handler from another module */
613
/* aborted the sequence. */
615
/* Handlers that are declared as "int" can return the following: */
617
/* OK Handler accepted the request and did its thing with it. */
618
/* DECLINED Handler took no action. */
619
/* HTTP_mumble Handler looked at request and found it wanting. */
621
/* What the server does after calling a module handler depends upon the */
622
/* handler's return value. In all cases, if the handler returns */
623
/* DECLINED, the server will continue to the next module with an handler */
624
/* for the current phase. However, if the handler return a non-OK, */
625
/* non-DECLINED status, the server aborts the request right there. If */
626
/* the handler returns OK, the server's next action is phase-specific; */
627
/* see the individual handler comments below for details. */
629
/*--------------------------------------------------------------------------*/
631
* This function is called during server initialisation. Any information
632
* that needs to be recorded must be in static cells, since there's no
633
* configuration record.
635
* There is no return value.
639
* This function is called when an heavy-weight process (such as a child) is
640
* being run down or destroyed. As with the child initialisation function,
641
* any information that needs to be recorded must be in static cells, since
642
* there's no configuration record.
644
* There is no return value.
648
* This function is called during server initialisation when an heavy-weight
649
* process (such as a child) is being initialised. As with the
650
* module initialisation function, any information that needs to be recorded
651
* must be in static cells, since there's no configuration record.
653
* There is no return value.
657
* This function gets called to create a per-directory configuration
658
* record. This will be called for the "default" server environment, and for
659
* each directory for which the parser finds any of our directives applicable.
660
* If a directory doesn't have any of our directives involved (i.e., they
661
* aren't in the .htaccess file, or a <Location>, <Directory>, or related
662
* block), this routine will *not* be called - the configuration for the
663
* closest ancestor is used.
665
* The return value is a pointer to the created module-specific
668
static void *x_create_dir_config(apr_pool_t *p, char *dirspec)
671
char *dname = dirspec;
674
* Allocate the space for our record from the pool supplied.
676
cfg = (x_cfg *) apr_pcalloc(p, sizeof(x_cfg));
678
* Now fill in the defaults. If there are any `parent' configuration
679
* records, they'll get merged as part of a separate callback.
683
cfg->cmode = CONFIG_MODE_DIRECTORY;
685
* Finally, add our trace to the callback list.
687
dname = (dname != NULL) ? dname : "";
688
cfg->loc = apr_pstrcat(p, "DIR(", dname, ")", NULL);
689
trace_add(NULL, NULL, cfg, "x_create_dir_config()");
694
* This function gets called to merge two per-directory configuration
695
* records. This is typically done to cope with things like .htaccess files
696
* or <Location> directives for directories that are beneath one for which a
697
* configuration record was already created. The routine has the
698
* responsibility of creating a new record and merging the contents of the
699
* other two into it appropriately. If the module doesn't declare a merge
700
* routine, the record for the closest ancestor location (that has one) is
703
* The routine MUST NOT modify any of its arguments!
705
* The return value is a pointer to the created module-specific structure
706
* containing the merged values.
708
static void *x_merge_dir_config(apr_pool_t *p, void *parent_conf,
712
x_cfg *merged_config = (x_cfg *) apr_pcalloc(p, sizeof(x_cfg));
713
x_cfg *pconf = (x_cfg *) parent_conf;
714
x_cfg *nconf = (x_cfg *) newloc_conf;
718
* Some things get copied directly from the more-specific record, rather
719
* than getting merged.
721
merged_config->local = nconf->local;
722
merged_config->loc = apr_pstrdup(p, nconf->loc);
724
* Others, like the setting of the `congenital' flag, get ORed in. The
725
* setting of that particular flag, for instance, is TRUE if it was ever
726
* true anywhere in the upstream configuration.
728
merged_config->congenital = (pconf->congenital | pconf->local);
730
* If we're merging records for two different types of environment (server
731
* and directory), mark the new record appropriately. Otherwise, inherit
734
merged_config->cmode =
735
(pconf->cmode == nconf->cmode) ? pconf->cmode : CONFIG_MODE_COMBO;
737
* Now just record our being called in the trace list. Include the
738
* locations we were asked to merge.
740
note = apr_pstrcat(p, "x_merge_dir_config(\"", pconf->loc, "\",\"",
741
nconf->loc, "\")", NULL);
742
trace_add(NULL, NULL, merged_config, note);
743
return (void *) merged_config;
747
* This function gets called to create a per-server configuration
748
* record. It will always be called for the "default" server.
750
* The return value is a pointer to the created module-specific
753
static void *x_create_server_config(apr_pool_t *p, server_rec *s)
757
char *sname = s->server_hostname;
760
* As with the x_create_dir_config() reoutine, we allocate and fill
761
* in an empty record.
763
cfg = (x_cfg *) apr_pcalloc(p, sizeof(x_cfg));
766
cfg->cmode = CONFIG_MODE_SERVER;
768
* Note that we were called in the trace list.
770
sname = (sname != NULL) ? sname : "";
771
cfg->loc = apr_pstrcat(p, "SVR(", sname, ")", NULL);
772
trace_add(s, NULL, cfg, "x_create_server_config()");
777
* This function gets called to merge two per-server configuration
778
* records. This is typically done to cope with things like virtual hosts and
779
* the default server configuration The routine has the responsibility of
780
* creating a new record and merging the contents of the other two into it
781
* appropriately. If the module doesn't declare a merge routine, the more
782
* specific existing record is used exclusively.
784
* The routine MUST NOT modify any of its arguments!
786
* The return value is a pointer to the created module-specific structure
787
* containing the merged values.
789
static void *x_merge_server_config(apr_pool_t *p, void *server1_conf,
793
x_cfg *merged_config = (x_cfg *) apr_pcalloc(p, sizeof(x_cfg));
794
x_cfg *s1conf = (x_cfg *) server1_conf;
795
x_cfg *s2conf = (x_cfg *) server2_conf;
799
* Our inheritance rules are our own, and part of our module's semantics.
800
* Basically, just note whence we came.
802
merged_config->cmode =
803
(s1conf->cmode == s2conf->cmode) ? s1conf->cmode : CONFIG_MODE_COMBO;
804
merged_config->local = s2conf->local;
805
merged_config->congenital = (s1conf->congenital | s1conf->local);
806
merged_config->loc = apr_pstrdup(p, s2conf->loc);
808
* Trace our call, including what we were asked to merge.
810
note = apr_pstrcat(p, "x_merge_server_config(\"", s1conf->loc, "\",\"",
811
s2conf->loc, "\")", NULL);
812
trace_add(NULL, NULL, merged_config, note);
813
return (void *) merged_config;
817
* This routine is called before the server processes the configuration
818
* files. There is no return value.
820
static int x_pre_config(apr_pool_t *pconf, apr_pool_t *plog,
824
* Log the call and exit.
826
trace_add(NULL, NULL, NULL, "x_pre_config()");
832
* This routine is called to perform any module-specific fixing of header
833
* fields, et cetera. It is invoked just before any content-handler.
835
* The return value is OK, DECLINED, or HTTP_mumble. If we return OK, the
836
* server will still call any remaining modules with an handler for this
839
static int x_post_config(apr_pool_t *pconf, apr_pool_t *plog,
840
apr_pool_t *ptemp, server_rec *s)
843
* Log the call and exit.
845
trace_add(NULL, NULL, NULL, "x_post_config()");
850
* This routine is called to perform any module-specific log file
851
* openings. It is invoked just before the post_config phase
853
* The return value is OK, DECLINED, or HTTP_mumble. If we return OK, the
854
* server will still call any remaining modules with an handler for this
857
static int x_open_logs(apr_pool_t *pconf, apr_pool_t *plog,
858
apr_pool_t *ptemp, server_rec *s)
861
* Log the call and exit.
863
trace_add(s, NULL, NULL, "x_open_logs()");
868
* All our process-death routine does is add its trace to the log.
870
static apr_status_t x_child_exit(void *data)
873
server_rec *s = data;
874
char *sname = s->server_hostname;
877
* The arbitrary text we add to our trace entry indicates for which server
878
* we're being called.
880
sname = (sname != NULL) ? sname : "";
881
note = apr_pstrcat(s->process->pool, "x_child_exit(", sname, ")", NULL);
882
trace_add(s, NULL, NULL, note);
887
* All our process initialiser does is add its trace to the log.
889
static void x_child_init(apr_pool_t *p, server_rec *s)
892
char *sname = s->server_hostname;
895
* Set up any module cells that ought to be initialised.
897
setup_module_cells();
899
* The arbitrary text we add to our trace entry indicates for which server
900
* we're being called.
902
sname = (sname != NULL) ? sname : "";
903
note = apr_pstrcat(p, "x_child_init(", sname, ")", NULL);
904
trace_add(s, NULL, NULL, note);
906
apr_pool_cleanup_register(p, s, x_child_exit, x_child_exit);
910
* XXX: This routine is called XXX
912
* The return value is OK, DECLINED, or HTTP_mumble. If we return OK, the
913
* server will still call any remaining modules with an handler for this
917
static const char *x_http_scheme(const request_rec *r)
921
cfg = our_dconfig(r);
923
* Log the call and exit.
925
trace_add(r->server, NULL, cfg, "x_http_scheme()");
930
* XXX: This routine is called XXX
932
* The return value is OK, DECLINED, or HTTP_mumble. If we return OK, the
933
* server will still call any remaining modules with an handler for this
936
static apr_port_t x_default_port(const request_rec *r)
940
cfg = our_dconfig(r);
942
* Log the call and exit.
944
trace_add(r->server, NULL, cfg, "x_default_port()");
950
* XXX: This routine is called XXX
952
* The return value is OK, DECLINED, or HTTP_mumble. If we return OK, the
953
* server will still call any remaining modules with an handler for this
956
static void x_insert_filter(request_rec *r)
960
cfg = our_dconfig(r);
962
* Log the call and exit.
964
trace_add(r->server, NULL, cfg, "x_insert_filter()");
968
* XXX: This routine is called XXX
970
* The return value is OK, DECLINED, or HTTP_mumble. If we return OK, the
971
* server will still call any remaining modules with an handler for this
974
static int x_quick_handler(request_rec *r, int lookup_uri)
978
cfg = our_dconfig(r);
980
* Log the call and exit.
982
trace_add(r->server, NULL, cfg, "x_quick_handler()");
987
* This routine is called just after the server accepts the connection,
988
* but before it is handed off to a protocol module to be served. The point
989
* of this hook is to allow modules an opportunity to modify the connection
990
* as soon as possible. The core server uses this phase to setup the
991
* connection record based on the type of connection that is being used.
993
* The return value is OK, DECLINED, or HTTP_mumble. If we return OK, the
994
* server will still call any remaining modules with an handler for this
997
static int x_pre_connection(conn_rec *c, void *csd)
1001
cfg = our_cconfig(c);
1004
* Log the call and exit.
1006
trace_add(r->server, NULL, cfg, "x_pre_connection()");
1011
/* This routine is used to actually process the connection that was received.
1012
* Only protocol modules should implement this hook, as it gives them an
1013
* opportunity to replace the standard HTTP processing with processing for
1014
* some other protocol. Both echo and POP3 modules are available as
1017
* The return VALUE is OK, DECLINED, or HTTP_mumble. If we return OK, no
1018
* further modules are called for this phase.
1020
static int x_process_connection(conn_rec *c)
1026
* This routine is called after the request has been read but before any other
1027
* phases have been processed. This allows us to make decisions based upon
1028
* the input header fields.
1030
* The return value is OK, DECLINED, or HTTP_mumble. If we return OK, no
1031
* further modules are called for this phase.
1033
static int x_post_read_request(request_rec *r)
1037
cfg = our_dconfig(r);
1039
* We don't actually *do* anything here, except note the fact that we were
1042
trace_add(r->server, r, cfg, "x_post_read_request()");
1047
* This routine gives our module an opportunity to translate the URI into an
1048
* actual filename. If we don't do anything special, the server's default
1049
* rules (Alias directives and the like) will continue to be followed.
1051
* The return value is OK, DECLINED, or HTTP_mumble. If we return OK, no
1052
* further modules are called for this phase.
1054
static int x_translate_handler(request_rec *r)
1059
cfg = our_dconfig(r);
1061
* We don't actually *do* anything here, except note the fact that we were
1064
trace_add(r->server, r, cfg, "x_translate_handler()");
1069
* This routine maps r->filename to a physical file on disk. Useful for
1070
* overriding default core behavior, including skipping mapping for
1071
* requests that are not file based.
1073
* The return value is OK, DECLINED, or HTTP_mumble. If we return OK, no
1074
* further modules are called for this phase.
1076
static int x_map_to_storage_handler(request_rec *r)
1081
cfg = our_dconfig(r);
1083
* We don't actually *do* anything here, except note the fact that we were
1086
trace_add(r->server, r, cfg, "x_map_to_storage_handler()");
1091
* this routine gives our module another chance to examine the request
1092
* headers and to take special action. This is the first phase whose
1093
* hooks' configuration directives can appear inside the <Directory>
1094
* and similar sections, because at this stage the URI has been mapped
1095
* to the filename. For example this phase can be used to block evil
1096
* clients, while little resources were wasted on these.
1098
* The return value is OK, DECLINED, or HTTP_mumble. If we return OK,
1099
* the server will still call any remaining modules with an handler
1102
static int x_header_parser_handler(request_rec *r)
1107
cfg = our_dconfig(r);
1109
* We don't actually *do* anything here, except note the fact that we were
1112
trace_add(r->server, r, cfg, "header_parser_handler()");
1118
* This routine is called to check the authentication information sent with
1119
* the request (such as looking up the user in a database and verifying that
1120
* the [encrypted] password sent matches the one in the database).
1122
* The return value is OK, DECLINED, or some HTTP_mumble error (typically
1123
* HTTP_UNAUTHORIZED). If we return OK, no other modules are given a chance
1124
* at the request during this phase.
1126
static int x_check_user_id(request_rec *r)
1131
cfg = our_dconfig(r);
1133
* Don't do anything except log the call.
1135
trace_add(r->server, r, cfg, "x_check_user_id()");
1140
* This routine is called to check to see if the resource being requested
1141
* requires authorisation.
1143
* The return value is OK, DECLINED, or HTTP_mumble. If we return OK, no
1144
* other modules are called during this phase.
1146
* If *all* modules return DECLINED, the request is aborted with a server
1149
static int x_auth_checker(request_rec *r)
1154
cfg = our_dconfig(r);
1156
* Log the call and return OK, or access will be denied (even though we
1157
* didn't actually do anything).
1159
trace_add(r->server, r, cfg, "x_auth_checker()");
1164
* This routine is called to check for any module-specific restrictions placed
1165
* upon the requested resource. (See the mod_access module for an example.)
1167
* The return value is OK, DECLINED, or HTTP_mumble. All modules with an
1168
* handler for this phase are called regardless of whether their predecessors
1169
* return OK or DECLINED. The first one to return any other status, however,
1170
* will abort the sequence (and the request) as usual.
1172
static int x_access_checker(request_rec *r)
1177
cfg = our_dconfig(r);
1178
trace_add(r->server, r, cfg, "x_access_checker()");
1183
* This routine is called to determine and/or set the various document type
1184
* information bits, like Content-type (via r->content_type), language, et
1187
* The return value is OK, DECLINED, or HTTP_mumble. If we return OK, no
1188
* further modules are given a chance at the request for this phase.
1190
static int x_type_checker(request_rec *r)
1195
cfg = our_dconfig(r);
1197
* Log the call, but don't do anything else - and report truthfully that
1198
* we didn't do anything.
1200
trace_add(r->server, r, cfg, "x_type_checker()");
1205
* This routine is called to perform any module-specific fixing of header
1206
* fields, et cetera. It is invoked just before any content-handler.
1208
* The return value is OK, DECLINED, or HTTP_mumble. If we return OK, the
1209
* server will still call any remaining modules with an handler for this
1212
static int x_fixer_upper(request_rec *r)
1217
cfg = our_dconfig(r);
1219
* Log the call and exit.
1221
trace_add(r->server, r, cfg, "x_fixer_upper()");
1226
* This routine is called to perform any module-specific logging activities
1227
* over and above the normal server things.
1229
* The return value is OK, DECLINED, or HTTP_mumble. If we return OK, any
1230
* remaining modules with an handler for this phase will still be called.
1232
static int x_logger(request_rec *r)
1237
cfg = our_dconfig(r);
1238
trace_add(r->server, r, cfg, "x_logger()");
1242
/*--------------------------------------------------------------------------*/
1244
/* Which functions are responsible for which hooks in the server. */
1246
/*--------------------------------------------------------------------------*/
1248
* Each function our module provides to handle a particular hook is
1249
* specified here. The functions are registered using
1250
* ap_hook_foo(name, predecessors, successors, position)
1251
* where foo is the name of the hook.
1253
* The args are as follows:
1254
* name -> the name of the function to call.
1255
* predecessors -> a list of modules whose calls to this hook must be
1256
* invoked before this module.
1257
* successors -> a list of modules whose calls to this hook must be
1258
* invoked after this module.
1259
* position -> The relative position of this module. One of
1260
* APR_HOOK_FIRST, APR_HOOK_MIDDLE, or APR_HOOK_LAST.
1261
* Most modules will use APR_HOOK_MIDDLE. If multiple
1262
* modules use the same relative position, Apache will
1263
* determine which to call first.
1264
* If your module relies on another module to run first,
1265
* or another module running after yours, use the
1266
* predecessors and/or successors.
1268
* The number in brackets indicates the order in which the routine is called
1269
* during request processing. Note that not all routines are necessarily
1270
* called (such as if a resource doesn't have access restrictions).
1271
* The actual delivery of content to the browser [9] is not handled by
1272
* a hook; see the handler declarations below.
1274
static void x_register_hooks(apr_pool_t *p)
1276
ap_hook_pre_config(x_pre_config, NULL, NULL, APR_HOOK_MIDDLE);
1277
ap_hook_post_config(x_post_config, NULL, NULL, APR_HOOK_MIDDLE);
1278
ap_hook_open_logs(x_open_logs, NULL, NULL, APR_HOOK_MIDDLE);
1279
ap_hook_child_init(x_child_init, NULL, NULL, APR_HOOK_MIDDLE);
1280
ap_hook_handler(x_handler, NULL, NULL, APR_HOOK_MIDDLE);
1281
ap_hook_quick_handler(x_quick_handler, NULL, NULL, APR_HOOK_MIDDLE);
1282
ap_hook_pre_connection(x_pre_connection, NULL, NULL, APR_HOOK_MIDDLE);
1283
ap_hook_process_connection(x_process_connection, NULL, NULL, APR_HOOK_MIDDLE);
1284
/* [1] post read_request handling */
1285
ap_hook_post_read_request(x_post_read_request, NULL, NULL,
1287
ap_hook_log_transaction(x_logger, NULL, NULL, APR_HOOK_MIDDLE);
1289
ap_hook_http_scheme(x_http_scheme, NULL, NULL, APR_HOOK_MIDDLE);
1290
ap_hook_default_port(x_default_port, NULL, NULL, APR_HOOK_MIDDLE);
1292
ap_hook_translate_name(x_translate_handler, NULL, NULL, APR_HOOK_MIDDLE);
1293
ap_hook_map_to_storage(x_map_to_storage_handler, NULL,NULL, APR_HOOK_MIDDLE);
1294
ap_hook_header_parser(x_header_parser_handler, NULL, NULL, APR_HOOK_MIDDLE);
1295
ap_hook_check_user_id(x_check_user_id, NULL, NULL, APR_HOOK_MIDDLE);
1296
ap_hook_fixups(x_fixer_upper, NULL, NULL, APR_HOOK_MIDDLE);
1297
ap_hook_type_checker(x_type_checker, NULL, NULL, APR_HOOK_MIDDLE);
1298
ap_hook_access_checker(x_access_checker, NULL, NULL, APR_HOOK_MIDDLE);
1299
ap_hook_auth_checker(x_auth_checker, NULL, NULL, APR_HOOK_MIDDLE);
1300
ap_hook_insert_filter(x_insert_filter, NULL, NULL, APR_HOOK_MIDDLE);
1303
/*--------------------------------------------------------------------------*/
1305
/* All of the routines have been declared now. Here's the list of */
1306
/* directives specific to our module, and information about where they */
1307
/* may appear and how the command parser should pass them to us for */
1308
/* processing. Note that care must be taken to ensure that there are NO */
1309
/* collisions of directive names between modules. */
1311
/*--------------------------------------------------------------------------*/
1313
* List of directives specific to our module.
1315
static const command_rec x_cmds[] =
1318
"Example", /* directive name */
1319
cmd_example, /* config action routine */
1320
NULL, /* argument to include in call */
1321
OR_OPTIONS, /* where available */
1322
"Example directive - no arguments" /* directive description */
1326
/*--------------------------------------------------------------------------*/
1328
/* Finally, the list of callback routines and data structures that provide */
1329
/* the static hooks into our module from the other parts of the server. */
1331
/*--------------------------------------------------------------------------*/
1333
* Module definition for configuration. If a particular callback is not
1334
* needed, replace its routine name below with the word NULL.
1336
module AP_MODULE_DECLARE_DATA example_module =
1338
STANDARD20_MODULE_STUFF,
1339
x_create_dir_config, /* per-directory config creator */
1340
x_merge_dir_config, /* dir config merger */
1341
x_create_server_config, /* server config creator */
1342
x_merge_server_config, /* server config merger */
1343
x_cmds, /* command table */
1344
x_register_hooks, /* set up other request processing hooks */