Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* Apache example_hooks module. Provide demonstrations of how modules do things.
* It is not meant to be used in a production server. Since it participates
* in all of the processing phases, it could conceivable interfere with
* the proper operation of other modules -- particularly the ones related
* to security.
*
* In the interest of brevity, all functions and structures internal to
* this module, but which may have counterparts in *real* modules, are
* prefixed with 'x_' instead of 'example_'.
*
* To use mod_example_hooks, configure the Apache build with
* --enable-example-hooks and compile. Set up a <Location> block in your
* configuration file like so:
*
* <Location /example>
* SetHandler example-hooks-handler
* </Location>
*
* When you look at that location on your server, you will see a backtrace of
* the callbacks that have been invoked up to that point. See the ErrorLog for
* more information on code paths that touch mod_example_hooks.
*
* IMPORTANT NOTES
* ===============
*
* Do NOT use this module on a production server. It attaches itself to every
* phase of the server runtime operations including startup, shutdown and
* request processing, and produces copious amounts of logging data. This will
* negatively affect server performance.
*
* Do NOT use mod_example_hooks as the basis for your own code. This module
* implements every callback hook offered by the Apache core, and your
* module will almost certainly not have to implement this much. If you
* want a simple module skeleton to start development, use apxs -g.
*
* XXX TO DO XXX
* =============
*
* * Enable HTML backtrace entries for more callbacks that are not directly
* associated with a request
* * Make sure every callback that posts an HTML backtrace entry does so in the * right category, so nothing gets overwritten
* * Implement some logic to show what happens in the parent, and what in the
* child(ren)
*/
#include "httpd.h"
#include "http_config.h"
#include "http_core.h"
#include "http_log.h"
#include "http_main.h"
#include "http_protocol.h"
#include "http_request.h"
#include "util_script.h"
#include "http_connection.h"
#ifdef HAVE_UNIX_SUEXEC
#include "unixd.h"
#endif
#include "scoreboard.h"
#include "mpm_common.h"
#include "apr_strings.h"
#include <stdio.h>
/*--------------------------------------------------------------------------*/
/* */
/* Data declarations. */
/* */
/* Here are the static cells and structure declarations private to our */
/* module. */
/* */
/*--------------------------------------------------------------------------*/
/*
* Sample configuration record. Used for both per-directory and per-server
* configuration data.
*
* It's perfectly reasonable to have two different structures for the two
* different environments. The same command handlers will be called for
* both, though, so the handlers need to be able to tell them apart. One
* possibility is for both structures to start with an int which is 0 for
* one and 1 for the other.
*
* Note that while the per-directory and per-server configuration records are
* available to most of the module handlers, they should be treated as
* READ-ONLY by all except the command and merge handlers. Sometimes handlers
* are handed a record that applies to the current location by implication or
* inheritance, and modifying it will change the rules for other locations.
*/
typedef struct x_cfg {
int cmode; /* Environment to which record applies
* (directory, server, or combination).
*/
#define CONFIG_MODE_SERVER 1
#define CONFIG_MODE_DIRECTORY 2
#define CONFIG_MODE_COMBO 3 /* Shouldn't ever happen. */
int local; /* Boolean: "Example" directive declared
* here?
*/
int congenital; /* Boolean: did we inherit an "Example"? */
char *trace; /* Pointer to trace string. */
char *loc; /* Location to which this record applies. */
} x_cfg;
/*
* String pointer to hold the startup trace. No harm working with a global until
* the server is (may be) multi-threaded.
*/
static const char *trace = NULL;
/*
* Declare ourselves so the configuration routines can find and know us.
* We'll fill it in at the end of the module.
*/
module AP_MODULE_DECLARE_DATA example_hooks_module;
/*--------------------------------------------------------------------------*/
/* */
/* The following pseudo-prototype declarations illustrate the parameters */
/* passed to command handlers for the different types of directive */
/* syntax. If an argument was specified in the directive definition */
/* (look for "command_rec" below), it's available to the command handler */
/* via the (void *) info field in the cmd_parms argument passed to the */
/* handler (cmd->info for the examples below). */
/* */
/*--------------------------------------------------------------------------*/
/*
* Command handler for a NO_ARGS directive. Declared in the command_rec
* list with
* AP_INIT_NO_ARGS("directive", function, mconfig, where, help)
*
* static const char *handle_NO_ARGS(cmd_parms *cmd, void *mconfig);
*/
/*
* Command handler for a RAW_ARGS directive. The "args" argument is the text
* of the commandline following the directive itself. Declared in the
* command_rec list with
* AP_INIT_RAW_ARGS("directive", function, mconfig, where, help)
*
* static const char *handle_RAW_ARGS(cmd_parms *cmd, void *mconfig,
* const char *args);
*/
/*
* Command handler for a FLAG directive. The single parameter is passed in
* "bool", which is either zero or not for Off or On respectively.
* Declared in the command_rec list with
* AP_INIT_FLAG("directive", function, mconfig, where, help)
*
* static const char *handle_FLAG(cmd_parms *cmd, void *mconfig, int bool);
*/
/*
* Command handler for a TAKE1 directive. The single parameter is passed in
* "word1". Declared in the command_rec list with
* AP_INIT_TAKE1("directive", function, mconfig, where, help)
*
* static const char *handle_TAKE1(cmd_parms *cmd, void *mconfig,
* char *word1);
*/
/*
* Command handler for a TAKE2 directive. TAKE2 commands must always have
* exactly two arguments. Declared in the command_rec list with
* AP_INIT_TAKE2("directive", function, mconfig, where, help)
*
* static const char *handle_TAKE2(cmd_parms *cmd, void *mconfig,
* char *word1, char *word2);
*/
/*
* Command handler for a TAKE3 directive. Like TAKE2, these must have exactly
* three arguments, or the parser complains and doesn't bother calling us.
* Declared in the command_rec list with
* AP_INIT_TAKE3("directive", function, mconfig, where, help)
*
* static const char *handle_TAKE3(cmd_parms *cmd, void *mconfig,
* char *word1, char *word2, char *word3);
*/
/*
* Command handler for a TAKE12 directive. These can take either one or two
* arguments.
* - word2 is a NULL pointer if no second argument was specified.
* Declared in the command_rec list with
* AP_INIT_TAKE12("directive", function, mconfig, where, help)
*
* static const char *handle_TAKE12(cmd_parms *cmd, void *mconfig,
* char *word1, char *word2);
*/
/*
* Command handler for a TAKE123 directive. A TAKE123 directive can be given,
* as might be expected, one, two, or three arguments.
* - word2 is a NULL pointer if no second argument was specified.
* - word3 is a NULL pointer if no third argument was specified.
* Declared in the command_rec list with
* AP_INIT_TAKE123("directive", function, mconfig, where, help)
*
* static const char *handle_TAKE123(cmd_parms *cmd, void *mconfig,
* char *word1, char *word2, char *word3);
*/
/*
* Command handler for a TAKE13 directive. Either one or three arguments are
* permitted - no two-parameters-only syntax is allowed.
* - word2 and word3 are NULL pointers if only one argument was specified.
* Declared in the command_rec list with
* AP_INIT_TAKE13("directive", function, mconfig, where, help)
*
* static const char *handle_TAKE13(cmd_parms *cmd, void *mconfig,
* char *word1, char *word2, char *word3);
*/
/*
* Command handler for a TAKE23 directive. At least two and as many as three
* arguments must be specified.
* - word3 is a NULL pointer if no third argument was specified.
* Declared in the command_rec list with
* AP_INIT_TAKE23("directive", function, mconfig, where, help)
*
* static const char *handle_TAKE23(cmd_parms *cmd, void *mconfig,
* char *word1, char *word2, char *word3);
*/
/*
* Command handler for a ITERATE directive.
* - Handler is called once for each of n arguments given to the directive.
* - word1 points to each argument in turn.
* Declared in the command_rec list with
* AP_INIT_ITERATE("directive", function, mconfig, where, help)
*
* static const char *handle_ITERATE(cmd_parms *cmd, void *mconfig,
* char *word1);
*/
/*
* Command handler for a ITERATE2 directive.
* - Handler is called once for each of the second and subsequent arguments
* given to the directive.
* - word1 is the same for each call for a particular directive instance (the
* first argument).
* - word2 points to each of the second and subsequent arguments in turn.
* Declared in the command_rec list with
* AP_INIT_ITERATE2("directive", function, mconfig, where, help)
*
* static const char *handle_ITERATE2(cmd_parms *cmd, void *mconfig,
* char *word1, char *word2);
*/
/*--------------------------------------------------------------------------*/
/* */
/* These routines are strictly internal to this module, and support its */
/* operation. They are not referenced by any external portion of the */
/* server. */
/* */
/*--------------------------------------------------------------------------*/
/*
* Locate our directory configuration record for the current request.
*/
static x_cfg *our_dconfig(const request_rec *r)
{
return (x_cfg *) ap_get_module_config(r->per_dir_config, &example_hooks_module);
}
/*
* The following utility routines are not used in the module. Don't
* compile them so -Wall doesn't complain about functions that are
* defined but not used.
*/
#if 0
/*
* Locate our server configuration record for the specified server.
*/
static x_cfg *our_sconfig(const server_rec *s)
{
return (x_cfg *) ap_get_module_config(s->module_config, &example_hooks_module);
}
/*
* Likewise for our configuration record for the specified request.
*/
static x_cfg *our_rconfig(const request_rec *r)
{
return (x_cfg *) ap_get_module_config(r->request_config, &example_hooks_module);
}
#endif /* if 0 */
/*
* Likewise for our configuration record for a connection.
*/
static x_cfg *our_cconfig(const conn_rec *c)
{
return (x_cfg *) ap_get_module_config(c->conn_config, &example_hooks_module);
}
/*
* You *could* change the following if you wanted to see the calling
* sequence reported in the server's error_log, but beware - almost all of
* these co-routines are called for every single request, and the impact
* on the size (and readability) of the error_log is considerable.
*/
#ifndef EXAMPLE_LOG_EACH
#define EXAMPLE_LOG_EACH 0
#endif
#if EXAMPLE_LOG_EACH
static void example_log_each(apr_pool_t *p, server_rec *s, const char *note)
{
if (s != NULL) {
ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, APLOGNO(02991)
"mod_example_hooks: %s", note);
}
else {
apr_file_t *out = NULL;
apr_file_open_stderr(&out, p);
apr_file_printf(out, "mod_example_hooks traced in non-loggable "
"context: %s\n", note);
}
}
#endif
/*
* This utility routine traces the hooks called when the server starts up.
* It leaves a trace in a global variable, so it should not be called from
* a hook handler that runs in a multi-threaded situation.
*/
static void trace_startup(apr_pool_t *p, server_rec *s, x_cfg *mconfig,
const char *note)
{
const char *sofar;
char *where, *addon;
#if EXAMPLE_LOG_EACH
example_log_each(p, s, note);
#endif
/*
* If we weren't passed a configuration record, we can't figure out to
* what location this call applies. This only happens for co-routines
* that don't operate in a particular directory or server context. If we
* got a valid record, extract the location (directory or server) to which
* it applies.
*/
where = (mconfig != NULL) ? mconfig->loc : "nowhere";
where = (where != NULL) ? where : "";
addon = apr_pstrcat(p,
" <li>\n"
" <dl>\n"
" <dt><samp>", note, "</samp></dt>\n"
" <dd><samp>[", where, "]</samp></dd>\n"
" </dl>\n"
" </li>\n",
NULL);
/*
* Make sure that we start with a valid string, even if we have never been
* called.
*/
sofar = (trace == NULL) ? "" : trace;
trace = apr_pstrcat(p, sofar, addon, NULL);
}
/*
* This utility route traces the hooks called as a request is handled.
* It takes the current request as argument
*/
#define TRACE_NOTE "example-hooks-trace"
static void trace_request(const request_rec *r, const char *note)
{
const char *trace_copy, *sofar;
char *addon, *where;
x_cfg *cfg;
#if EXAMPLE_LOG_EACH
example_log_each(r->pool, r->server, note);
#endif
if ((sofar = apr_table_get(r->notes, TRACE_NOTE)) == NULL) {
sofar = "";
}
cfg = our_dconfig(r);
where = (cfg != NULL) ? cfg->loc : "nowhere";
where = (where != NULL) ? where : "";
addon = apr_pstrcat(r->pool,
" <li>\n"
" <dl>\n"
" <dt><samp>", note, "</samp></dt>\n"
" <dd><samp>[", where, "]</samp></dd>\n"
" </dl>\n"
" </li>\n",
NULL);
trace_copy = apr_pstrcat(r->pool, sofar, addon, NULL);
apr_table_set(r->notes, TRACE_NOTE, trace_copy);
}
/*
* This utility routine traces the hooks called while processing a
* Connection. Its trace is kept in the pool notes of the pool associated
* with the Connection.
*/
/*
* Key to get and set the userdata. We should be able to get away
* with a constant key, since in prefork mode the process will have
* the connection and its pool to itself entirely, and in
* multi-threaded mode each connection will have its own pool.
*/
#define CONN_NOTE "example-hooks-connection"
static void trace_connection(conn_rec *c, const char *note)
{
const char *trace_copy, *sofar;
char *addon, *where;
void *data;
x_cfg *cfg;
#if EXAMPLE_LOG_EACH
example_log_each(c->pool, c->base_server, note);
#endif
cfg = our_cconfig(c);
where = (cfg != NULL) ? cfg->loc : "nowhere";
where = (where != NULL) ? where : "";
addon = apr_pstrcat(c->pool,
" <li>\n"
" <dl>\n"
" <dt><samp>", note, "</samp></dt>\n"
" <dd><samp>[", where, "]</samp></dd>\n"
" </dl>\n"
" </li>\n",
NULL);
/* Find existing notes and copy */
apr_pool_userdata_get(&data, CONN_NOTE, c->pool);
sofar = (data == NULL) ? "" : (const char *) data;
/* Tack addon onto copy */
trace_copy = apr_pstrcat(c->pool, sofar, addon, NULL);
/*
* Stash copy back into pool notes. This call has a cleanup
* parameter, but we're not using it because the string has been
* allocated from that same pool. There is also an unused return
* value: we have nowhere to communicate any error that might
* occur, and will have to check for the existence of this data on
* the other end.
*/
apr_pool_userdata_set((const void *) trace_copy, CONN_NOTE,
NULL, c->pool);
}
static void trace_nocontext(apr_pool_t *p, const char *file, int line,
const char *note)
{
/*
* Since we have no request or connection to trace, or any idea
* from where this routine was called, there's really not much we
* can do. If we are not logging everything by way of the
* EXAMPLE_LOG_EACH constant, do nothing in this routine.
*/
#ifdef EXAMPLE_LOG_EACH
ap_log_perror(file, line, APLOG_MODULE_INDEX, APLOG_NOTICE, 0, p, "%s", note);
#endif
}
/*--------------------------------------------------------------------------*/
/* We prototyped the various syntax for command handlers (routines that */
/* are called when the configuration parser detects a directive declared */
/* by our module) earlier. Now we actually declare a "real" routine that */
/* will be invoked by the parser when our "real" directive is */
/* encountered. */
/* */
/* If a command handler encounters a problem processing the directive, it */
/* signals this fact by returning a non-NULL pointer to a string */
/* describing the problem. */
/* */
/* The magic return value DECLINE_CMD is used to deal with directives */
/* that might be declared by multiple modules. If the command handler */
/* returns NULL, the directive was processed; if it returns DECLINE_CMD, */
/* the next module (if any) that declares the directive is given a chance */
/* at it. If it returns any other value, it's treated as the text of an */
/* error message. */
/*--------------------------------------------------------------------------*/
/*
* Command handler for the NO_ARGS "Example" directive. All we do is mark the
* call in the trace log, and flag the applicability of the directive to the
* current location in that location's configuration record.
*/
static const char *cmd_example(cmd_parms *cmd, void *mconfig)
{
x_cfg *cfg = (x_cfg *) mconfig;
/*
* "Example Wuz Here"
*/
cfg->local = 1;
trace_startup(cmd->pool, cmd->server, cfg, "cmd_example()");
return NULL;
}
/*
* This function gets called to create a per-directory configuration
* record. This will be called for the "default" server environment, and for
* each directory for which the parser finds any of our directives applicable.
* If a directory doesn't have any of our directives involved (i.e., they
* aren't in the .htaccess file, or a <Location>, <Directory>, or related
* block), this routine will *not* be called - the configuration for the
* closest ancestor is used.
*
* The return value is a pointer to the created module-specific
* structure.
*/
static void *x_create_dir_config(apr_pool_t *p, char *dirspec)
{
x_cfg *cfg;
char *dname = dirspec;
char *note;
/*
* Allocate the space for our record from the pool supplied.
*/
cfg = (x_cfg *) apr_pcalloc(p, sizeof(x_cfg));
/*
* Now fill in the defaults. If there are any `parent' configuration
* records, they'll get merged as part of a separate callback.
*/
cfg->local = 0;
cfg->congenital = 0;
cfg->cmode = CONFIG_MODE_DIRECTORY;
/*
* Finally, add our trace to the callback list.
*/
dname = (dname != NULL) ? dname : "";
cfg->loc = apr_pstrcat(p, "DIR(", dname, ")", NULL);
note = apr_psprintf(p, "x_create_dir_config(p == %pp, dirspec == %s)",
(void*) p, dirspec);
trace_startup(p, NULL, cfg, note);
return (void *) cfg;
}
/*
* This function gets called to merge two per-directory configuration
* records. This is typically done to cope with things like .htaccess files
* or <Location> directives for directories that are beneath one for which a
* configuration record was already created. The routine has the
* responsibility of creating a new record and merging the contents of the
* other two into it appropriately. If the module doesn't declare a merge
* routine, the record for the closest ancestor location (that has one) is
* used exclusively.
*
* The routine MUST NOT modify any of its arguments!
*
* The return value is a pointer to the created module-specific structure
* containing the merged values.
*/
static void *x_merge_dir_config(apr_pool_t *p, void *parent_conf,
void *newloc_conf)
{
x_cfg *merged_config = (x_cfg *) apr_pcalloc(p, sizeof(x_cfg));
x_cfg *pconf = (x_cfg *) parent_conf;
x_cfg *nconf = (x_cfg *) newloc_conf;
char *note;
/*
* Some things get copied directly from the more-specific record, rather
* than getting merged.
*/
merged_config->local = nconf->local;
merged_config->loc = apr_pstrdup(p, nconf->loc);
/*
* Others, like the setting of the `congenital' flag, get ORed in. The
* setting of that particular flag, for instance, is TRUE if it was ever
* true anywhere in the upstream configuration.
*/
merged_config->congenital = (pconf->congenital | pconf->local);
/*
* If we're merging records for two different types of environment (server
* and directory), mark the new record appropriately. Otherwise, inherit
* the current value.
*/
merged_config->cmode =
(pconf->cmode == nconf->cmode) ? pconf->cmode : CONFIG_MODE_COMBO;
/*
* Now just record our being called in the trace list. Include the
* locations we were asked to merge.
*/
note = apr_psprintf(p, "x_merge_dir_config(p == %pp, parent_conf == "
"%pp, newloc_conf == %pp)", (void*) p,
(void*) parent_conf, (void*) newloc_conf);
trace_startup(p, NULL, merged_config, note);
return (void *) merged_config;
}
/*
* This function gets called to create a per-server configuration
* record. It will always be called for the "default" server.
*
* The return value is a pointer to the created module-specific
* structure.
*/
static void *x_create_server_config(apr_pool_t *p, server_rec *s)
{
x_cfg *cfg;
char *sname = s->server_hostname;
/*
* As with the x_create_dir_config() reoutine, we allocate and fill
* in an empty record.
*/
cfg = (x_cfg *) apr_pcalloc(p, sizeof(x_cfg));
cfg->local = 0;
cfg->congenital = 0;
cfg->cmode = CONFIG_MODE_SERVER;
/*
* Note that we were called in the trace list.
*/
sname = (sname != NULL) ? sname : "";
cfg->loc = apr_pstrcat(p, "SVR(", sname, ")", NULL);
trace_startup(p, s, cfg, "x_create_server_config()");
return (void *) cfg;
}
/*
* This function gets called to merge two per-server configuration
* records. This is typically done to cope with things like virtual hosts and
* the default server configuration The routine has the responsibility of
* creating a new record and merging the contents of the other two into it
* appropriately. If the module doesn't declare a merge routine, the more
* specific existing record is used exclusively.
*
* The routine MUST NOT modify any of its arguments!
*
* The return value is a pointer to the created module-specific structure
* containing the merged values.
*/
static void *x_merge_server_config(apr_pool_t *p, void *server1_conf,
void *server2_conf)
{
x_cfg *merged_config = (x_cfg *) apr_pcalloc(p, sizeof(x_cfg));
x_cfg *s1conf = (x_cfg *) server1_conf;
x_cfg *s2conf = (x_cfg *) server2_conf;
char *note;
/*
* Our inheritance rules are our own, and part of our module's semantics.
* Basically, just note whence we came.
*/
merged_config->cmode =
(s1conf->cmode == s2conf->cmode) ? s1conf->cmode : CONFIG_MODE_COMBO;
merged_config->local = s2conf->local;
merged_config->congenital = (s1conf->congenital | s1conf->local);
merged_config->loc = apr_pstrdup(p, s2conf->loc);
/*
* Trace our call, including what we were asked to merge.
*/
note = apr_pstrcat(p, "x_merge_server_config(\"", s1conf->loc, "\",\"",
s2conf->loc, "\")", NULL);
trace_startup(p, NULL, merged_config, note);
return (void *) merged_config;
}
/*--------------------------------------------------------------------------*
* *
* Now let's declare routines for each of the callback hooks in order. *
* (That's the order in which they're listed in the callback list, *not *
* the order in which the server calls them! See the command_rec *
* declaration near the bottom of this file.) Note that these may be *
* called for situations that don't relate primarily to our function - in *
* other words, the fixup handler shouldn't assume that the request has *
* to do with "example_hooks" stuff. *
* *
* With the exception of the content handler, all of our routines will be *
* called for each request, unless an earlier handler from another module *
* aborted the sequence. *
* *
* There are three types of hooks (see include/ap_config.h): *
* *
* VOID : No return code, run all handlers declared by any module *
* RUN_FIRST : Run all handlers until one returns something other *
* than DECLINED. Hook runner result is result of last callback *
* RUN_ALL : Run all handlers until one returns something other than OK *
* or DECLINED. The hook runner returns that other value. If *
* all hooks run, the hook runner returns OK. *
* *
* Handlers that are declared as "int" can return the following: *
* *
* OK Handler accepted the request and did its thing with it. *
* DECLINED Handler took no action. *
* HTTP_mumble Handler looked at request and found it wanting. *
* *
* See include/httpd.h for a list of HTTP_mumble status codes. Handlers *
* that are not declared as int return a valid pointer, or NULL if they *
* DECLINE to handle their phase for that specific request. Exceptions, if *
* any, are noted with each routine. *
*--------------------------------------------------------------------------*/
/*
* This routine is called before the server processes the configuration
* files. There is no return value.
*/
static int x_pre_config(apr_pool_t *pconf, apr_pool_t *plog,
apr_pool_t *ptemp)
{
/*
* Log the call and exit.
*/
trace_startup(ptemp, NULL, NULL, "x_pre_config()");
return OK;
}
/*
* This routine is called after the server processes the configuration
* files. At this point the module may review and adjust its configuration
* settings in relation to one another and report any problems. On restart,
* this routine will be called twice, once in the startup process (which
* exits shortly after this phase) and once in the running server process.
*
* The return value is OK, DECLINED, or HTTP_mumble. If we return OK, the
* server will still call any remaining modules with an handler for this
* phase.
*/
static int x_check_config(apr_pool_t *pconf, apr_pool_t *plog,
apr_pool_t *ptemp, server_rec *s)
{
/*
* Log the call and exit.
*/
trace_startup(ptemp, s, NULL, "x_check_config()");
return OK;
}
/*
* This routine is called when the -t command-line option is supplied.
* It executes only once, in the startup process, after the check_config
* phase and just before the process exits. At this point the module
* may output any information useful in configuration testing.
*
* This is a VOID hook: all defined handlers get called.
*/
static void x_test_config(apr_pool_t *pconf, server_rec *s)
{
apr_file_t *out = NULL;
apr_file_open_stderr(&out, pconf);
apr_file_printf(out, "Example module configuration test routine\n");
trace_startup(pconf, s, NULL, "x_test_config()");
}
/*
* This routine is called to perform any module-specific log file
* openings. It is invoked just before the post_config phase
*
* The return value is OK, DECLINED, or HTTP_mumble. If we return OK, the
* server will still call any remaining modules with an handler for this
* phase.
*/
static int x_open_logs(apr_pool_t *pconf, apr_pool_t *plog,
apr_pool_t *ptemp, server_rec *s)
{
/*
* Log the call and exit.
*/
trace_startup(ptemp, s, NULL, "x_open_logs()");
return OK;
}
/*
* This routine is called after the server finishes the configuration
* process. At this point the module may review and adjust its configuration
* settings in relation to one another and report any problems. On restart,
* this routine will be called only once, in the running server process.
*
* The return value is OK, DECLINED, or HTTP_mumble. If we return OK, the
* server will still call any remaining modules with an handler for this
* phase.
*/
static int x_post_config(apr_pool_t *pconf, apr_pool_t *plog,
apr_pool_t *ptemp, server_rec *s)
{
/*
* Log the call and exit.
*/
trace_startup(ptemp, s, NULL, "x_post_config()");
return OK;
}
/*
* All our process-death routine does is add its trace to the log.
*/
static apr_status_t x_child_exit(void *data)
{
char *note;
server_rec *s = data;
char *sname = s->server_hostname;
/*
* The arbitrary text we add to our trace entry indicates for which server
* we're being called.
*/
sname = (sname != NULL) ? sname : "";
note = apr_pstrcat(s->process->pool, "x_child_exit(", sname, ")", NULL);
trace_startup(s->process->pool, s, NULL, note);
return APR_SUCCESS;
}
/*
* All our process initialiser does is add its trace to the log.
*
* This is a VOID hook: all defined handlers get called.
*/
static void x_child_init(apr_pool_t *p, server_rec *s)
{
char *note;
char *sname = s->server_hostname;
/*
* The arbitrary text we add to our trace entry indicates for which server
* we're being called.
*/
sname = (sname != NULL) ? sname : "";
note = apr_pstrcat(p, "x_child_init(", sname, ")", NULL);
trace_startup(p, s, NULL, note);
apr_pool_cleanup_register(p, s, x_child_exit, x_child_exit);
}
/*
* The hook runner for ap_hook_http_scheme is aliased to ap_http_scheme(),
* a routine that the core and other modules call when they need to know
* the URL scheme for the request. For instance, mod_ssl returns "https"
* if the server_rec associated with the request has SSL enabled.
*
* This hook was named 'ap_hook_http_method' in httpd 2.0.
*
* This is a RUN_FIRST hook: the first handler to return a non NULL
* value aborts the handler chain. The http_core module inserts a
* fallback handler (with APR_HOOK_REALLY_LAST preference) that returns
* "http".
*/
static const char *x_http_scheme(const request_rec *r)
{
/*
* Log the call and exit.
*/
trace_request(r, "x_http_scheme()");
/* We have no claims to make about the request scheme */
return NULL;
}
/*
* The runner for this hook is aliased to ap_default_port(), which the
* core and other modules call when they need to know the default port
* for a particular server. This is used for instance to omit the
* port number from a Redirect response Location header URL if the port
* number is equal to the default port for the service (like 80 for http).
*
* This is a RUN_FIRST hook: the first handler to return a non-zero
* value is the last one executed. The http_core module inserts a
* fallback handler (with APR_HOOK_REALLY_LAST order specifier) that
* returns 80.
*/
static apr_port_t x_default_port(const request_rec *r)
{
/*
* Log the call and exit.
*/
trace_request(r, "x_default_port()");
return 0;
}
/*
* This routine is called just before the handler gets invoked. It allows
* a module to insert a previously defined filter into the filter chain.
*
* No filter has been defined by this module, so we just log the call
* and exit.
*
* This is a VOID hook: all defined handlers get called.
*/
static void x_insert_filter(request_rec *r)
{
/*
* Log the call and exit.
*/
trace_request(r, "x_insert_filter()");
}
/*
* This routine is called to insert a previously defined error filter into
* the filter chain as the request is being processed.
*
* For the purpose of this example, we don't have a filter to insert,
* so just add to the trace and exit.
*
* This is a VOID hook: all defined handlers get called.
*/
static void x_insert_error_filter(request_rec *r)
{
trace_request(r, "x_insert_error_filter()");
}
/*--------------------------------------------------------------------------*/
/* */
/* Now we declare our content handlers, which are invoked when the server */
/* encounters a document which our module is supposed to have a chance to */
/* see. (See mod_mime's SetHandler and AddHandler directives, and the */
/* mod_info and mod_status examples, for more details.) */
/* */
/* Since content handlers are dumping data directly into the connection */
/* (using the r*() routines, such as rputs() and rprintf()) without */
/* intervention by other parts of the server, they need to make */
/* sure any accumulated HTTP headers are sent first. This is done by */
/* calling send_http_header(). Otherwise, no header will be sent at all, */
/* and the output sent to the client will actually be HTTP-uncompliant. */
/*--------------------------------------------------------------------------*/
/*
* Sample content handler. All this does is display the call list that has
* been built up so far.
*
* This routine gets called for every request, unless another handler earlier
* in the callback chain has already handled the request. It is up to us to
* test the request_rec->handler field and see whether we are meant to handle
* this request.
*
* The content handler gets to write directly to the client using calls like
* ap_rputs() and ap_rprintf()
*
* This is a RUN_FIRST hook.
*/
static int x_handler(request_rec *r)
{
x_cfg *dcfg;
char *note;
void *conn_data;
apr_status_t status;
dcfg = our_dconfig(r);
/*
* Add our trace to the log, and whether we get to write
* content for this request.
*/
note = apr_pstrcat(r->pool, "x_handler(), handler is \"",
r->handler, "\"", NULL);
trace_request(r, note);
/* If it's not for us, get out as soon as possible. */
if (strcmp(r->handler, "example-hooks-handler")) {
return DECLINED;
}
/*
* Set the Content-type header. Note that we do not actually have to send
* the headers: this is done by the http core.
*/
ap_set_content_type(r, "text/html");
/*
* If we're only supposed to send header information (HEAD request), we're
* already there.
*/