1 /* Copyright 2001-2004 The Apache Software Foundation
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
17 #include "apr_strings.h"
19 #include "apr_fnmatch.h"
21 #include "apr_thread_proc.h" /* for RLIMIT stuff */
22 #include "apr_hooks.h"
24 #define APR_WANT_IOVEC
25 #define APR_WANT_STRFUNC
26 #define APR_WANT_MEMFUNC
30 #include "ap_config.h"
32 #include "http_config.h"
33 #include "http_core.h"
34 #include "http_protocol.h" /* For index_of_response(). Grump. */
35 #include "http_request.h"
36 #include "http_vhost.h"
37 #include "http_main.h" /* For the default_handler below... */
40 #include "http_connection.h"
41 #include "apr_buckets.h"
42 #include "util_filter.h"
43 #include "util_ebcdic.h"
45 #include "mpm_common.h"
46 #include "scoreboard.h"
48 #include "mod_proxy.h"
49 #include "ap_listen.h"
51 #include "mod_so.h" /* for ap_find_loaded_module_symbol */
53 /* LimitRequestBody handling */
54 #define AP_LIMIT_REQ_BODY_UNSET ((apr_off_t) -1)
55 #define AP_DEFAULT_LIMIT_REQ_BODY ((apr_off_t) 0)
57 /* LimitXMLRequestBody handling */
58 #define AP_LIMIT_UNSET ((long) -1)
59 #define AP_DEFAULT_LIMIT_XML_BODY ((size_t)1000000)
61 #define AP_MIN_SENDFILE_BYTES (256)
63 /* maximum include nesting level */
64 #ifndef AP_MAX_INCLUDE_DEPTH
65 #define AP_MAX_INCLUDE_DEPTH (128)
69 APR_HOOK_LINK(get_mgmt_items)
72 AP_IMPLEMENT_HOOK_RUN_ALL(int, get_mgmt_items,
73 (apr_pool_t *p, const char *val, apr_hash_t *ht),
74 (p, val, ht), OK, DECLINED)
76 /* Server core module... This module provides support for really basic
77 * server operations, including options and commands which control the
78 * operation of other modules. Consider this the bureaucracy module.
80 * The core module also defines handlers, etc., do handle just enough
81 * to allow a server with the core module ONLY to actually serve documents
82 * (though it slaps DefaultType on all of 'em); this was useful in testing,
83 * but may not be worth preserving.
85 * This file could almost be mod_core.c, except for the stuff which affects
86 * the http_conf_globals.
89 /* Handles for core filters */
90 AP_DECLARE_DATA ap_filter_rec_t *ap_subreq_core_filter_handle;
91 AP_DECLARE_DATA ap_filter_rec_t *ap_core_output_filter_handle;
92 AP_DECLARE_DATA ap_filter_rec_t *ap_content_length_filter_handle;
93 AP_DECLARE_DATA ap_filter_rec_t *ap_net_time_filter_handle;
94 AP_DECLARE_DATA ap_filter_rec_t *ap_core_input_filter_handle;
96 /* magic pointer for ErrorDocument xxx "default" */
97 static char errordocument_default;
99 static void *create_core_dir_config(apr_pool_t *a, char *dir)
101 core_dir_config *conf;
104 conf = (core_dir_config *)apr_pcalloc(a, sizeof(core_dir_config));
106 /* conf->r and conf->d[_*] are initialized by dirsection() or left NULL */
108 conf->opts = dir ? OPT_UNSET : OPT_UNSET|OPT_ALL;
109 conf->opts_add = conf->opts_remove = OPT_NONE;
110 conf->override = dir ? OR_UNSET : OR_UNSET|OR_ALL;
111 conf->override_opts = OPT_UNSET | OPT_ALL | OPT_INCNOEXEC | OPT_SYM_OWNER
114 conf->content_md5 = 2;
115 conf->accept_path_info = 3;
117 conf->use_canonical_name = USE_CANONICAL_NAME_UNSET;
119 conf->hostname_lookups = HOSTNAME_LOOKUP_UNSET;
120 conf->satisfy = apr_palloc(a, sizeof(*conf->satisfy) * METHODS);
121 for (i = 0; i < METHODS; ++i) {
122 conf->satisfy[i] = SATISFY_NOSPEC;
126 conf->limit_cpu = NULL;
128 #if defined(RLIMIT_DATA) || defined(RLIMIT_VMEM) || defined(RLIMIT_AS)
129 conf->limit_mem = NULL;
132 conf->limit_nproc = NULL;
135 conf->limit_req_body = AP_LIMIT_REQ_BODY_UNSET;
136 conf->limit_xml_body = AP_LIMIT_UNSET;
137 conf->sec_file = apr_array_make(a, 2, sizeof(ap_conf_vector_t *));
139 conf->server_signature = srv_sig_unset;
141 conf->add_default_charset = ADD_DEFAULT_CHARSET_UNSET;
142 conf->add_default_charset_name = DEFAULT_ADD_DEFAULT_CHARSET_NAME;
144 /* Overriding all negotiation
146 conf->mime_type = NULL;
147 conf->handler = NULL;
148 conf->output_filters = NULL;
149 conf->input_filters = NULL;
152 * Flag for use of inodes in ETags.
154 conf->etag_bits = ETAG_UNSET;
155 conf->etag_add = ETAG_UNSET;
156 conf->etag_remove = ETAG_UNSET;
158 conf->enable_mmap = ENABLE_MMAP_UNSET;
159 conf->enable_sendfile = ENABLE_SENDFILE_UNSET;
160 conf->allow_encoded_slashes = 0;
166 * Overlay one hash table of ct_output_filters onto another
168 static void *merge_ct_filters(apr_pool_t *p,
171 const void *overlay_val,
172 const void *base_val,
175 ap_filter_rec_t *cur;
176 const ap_filter_rec_t *overlay_info = (const ap_filter_rec_t *)overlay_val;
177 const ap_filter_rec_t *base_info = (const ap_filter_rec_t *)base_val;
181 while (overlay_info) {
182 ap_filter_rec_t *new;
184 new = apr_pcalloc(p, sizeof(ap_filter_rec_t));
185 new->name = apr_pstrdup(p, overlay_info->name);
188 overlay_info = overlay_info->next;
195 /* We can't have dups. */
198 if (!strcasecmp(base_info->name, f->name)) {
207 f = apr_pcalloc(p, sizeof(ap_filter_rec_t));
208 f->name = apr_pstrdup(p, base_info->name);
213 base_info = base_info->next;
219 static void *merge_core_dir_configs(apr_pool_t *a, void *basev, void *newv)
221 core_dir_config *base = (core_dir_config *)basev;
222 core_dir_config *new = (core_dir_config *)newv;
223 core_dir_config *conf;
226 /* Create this conf by duplicating the base, replacing elements
227 * (or creating copies for merging) where new-> values exist.
229 conf = (core_dir_config *)apr_palloc(a, sizeof(core_dir_config));
230 memcpy(conf, base, sizeof(core_dir_config));
233 conf->d_is_fnmatch = new->d_is_fnmatch;
234 conf->d_components = new->d_components;
237 if (new->opts & OPT_UNSET) {
238 /* there was no explicit setting of new->opts, so we merge
239 * preserve the invariant (opts_add & opts_remove) == 0
241 conf->opts_add = (conf->opts_add & ~new->opts_remove) | new->opts_add;
242 conf->opts_remove = (conf->opts_remove & ~new->opts_add)
244 conf->opts = (conf->opts & ~conf->opts_remove) | conf->opts_add;
245 if ((base->opts & OPT_INCNOEXEC) && (new->opts & OPT_INCLUDES)) {
246 conf->opts = (conf->opts & ~OPT_INCNOEXEC) | OPT_INCLUDES;
250 /* otherwise we just copy, because an explicit opts setting
251 * overrides all earlier +/- modifiers
253 conf->opts = new->opts;
254 conf->opts_add = new->opts_add;
255 conf->opts_remove = new->opts_remove;
258 if (!(new->override & OR_UNSET)) {
259 conf->override = new->override;
262 if (!(new->override_opts & OPT_UNSET)) {
263 conf->override_opts = new->override_opts;
266 if (new->ap_default_type) {
267 conf->ap_default_type = new->ap_default_type;
270 if (new->ap_auth_type) {
271 conf->ap_auth_type = new->ap_auth_type;
274 if (new->ap_auth_name) {
275 conf->ap_auth_name = new->ap_auth_name;
278 if (new->ap_requires) {
279 conf->ap_requires = new->ap_requires;
282 if (conf->response_code_strings == NULL) {
283 conf->response_code_strings = new->response_code_strings;
285 else if (new->response_code_strings != NULL) {
286 /* If we merge, the merge-result must have it's own array
288 conf->response_code_strings = apr_palloc(a,
289 sizeof(*conf->response_code_strings) * RESPONSE_CODES);
290 memcpy(conf->response_code_strings, base->response_code_strings,
291 sizeof(*conf->response_code_strings) * RESPONSE_CODES);
293 for (i = 0; i < RESPONSE_CODES; ++i) {
294 if (new->response_code_strings[i] != NULL) {
295 conf->response_code_strings[i] = new->response_code_strings[i];
299 /* Otherwise we simply use the base->response_code_strings array
302 if (new->hostname_lookups != HOSTNAME_LOOKUP_UNSET) {
303 conf->hostname_lookups = new->hostname_lookups;
306 if ((new->content_md5 & 2) == 0) {
307 conf->content_md5 = new->content_md5;
310 if (new->accept_path_info != 3) {
311 conf->accept_path_info = new->accept_path_info;
314 if (new->use_canonical_name != USE_CANONICAL_NAME_UNSET) {
315 conf->use_canonical_name = new->use_canonical_name;
319 if (new->limit_cpu) {
320 conf->limit_cpu = new->limit_cpu;
324 #if defined(RLIMIT_DATA) || defined(RLIMIT_VMEM) || defined(RLIMIT_AS)
325 if (new->limit_mem) {
326 conf->limit_mem = new->limit_mem;
331 if (new->limit_nproc) {
332 conf->limit_nproc = new->limit_nproc;
336 if (new->limit_req_body != AP_LIMIT_REQ_BODY_UNSET) {
337 conf->limit_req_body = new->limit_req_body;
340 if (new->limit_xml_body != AP_LIMIT_UNSET)
341 conf->limit_xml_body = new->limit_xml_body;
343 conf->limit_xml_body = base->limit_xml_body;
345 if (!conf->sec_file) {
346 conf->sec_file = new->sec_file;
348 else if (new->sec_file) {
349 /* If we merge, the merge-result must have it's own array
351 conf->sec_file = apr_array_append(a, base->sec_file, new->sec_file);
353 /* Otherwise we simply use the base->sec_file array
356 /* use a separate ->satisfy[] array either way */
357 conf->satisfy = apr_palloc(a, sizeof(*conf->satisfy) * METHODS);
358 for (i = 0; i < METHODS; ++i) {
359 if (new->satisfy[i] != SATISFY_NOSPEC) {
360 conf->satisfy[i] = new->satisfy[i];
362 conf->satisfy[i] = base->satisfy[i];
366 if (new->server_signature != srv_sig_unset) {
367 conf->server_signature = new->server_signature;
370 if (new->add_default_charset != ADD_DEFAULT_CHARSET_UNSET) {
371 conf->add_default_charset = new->add_default_charset;
372 conf->add_default_charset_name = new->add_default_charset_name;
375 /* Overriding all negotiation
377 if (new->mime_type) {
378 conf->mime_type = new->mime_type;
382 conf->handler = new->handler;
385 if (new->output_filters) {
386 conf->output_filters = new->output_filters;
389 if (new->input_filters) {
390 conf->input_filters = new->input_filters;
393 if (conf->ct_output_filters && new->ct_output_filters) {
394 conf->ct_output_filters = apr_hash_merge(a,
395 new->ct_output_filters,
396 conf->ct_output_filters,
400 else if (new->ct_output_filters) {
401 conf->ct_output_filters = apr_hash_copy(a, new->ct_output_filters);
403 else if (conf->ct_output_filters) {
404 /* That memcpy above isn't enough. */
405 conf->ct_output_filters = apr_hash_copy(a, base->ct_output_filters);
409 * Now merge the setting of the FileETag directive.
411 if (new->etag_bits == ETAG_UNSET) {
413 (conf->etag_add & (~ new->etag_remove)) | new->etag_add;
415 (conf->opts_remove & (~ new->etag_add)) | new->etag_remove;
417 (conf->etag_bits & (~ conf->etag_remove)) | conf->etag_add;
420 conf->etag_bits = new->etag_bits;
421 conf->etag_add = new->etag_add;
422 conf->etag_remove = new->etag_remove;
425 if (conf->etag_bits != ETAG_NONE) {
426 conf->etag_bits &= (~ ETAG_NONE);
429 if (new->enable_mmap != ENABLE_MMAP_UNSET) {
430 conf->enable_mmap = new->enable_mmap;
433 if (new->enable_sendfile != ENABLE_SENDFILE_UNSET) {
434 conf->enable_sendfile = new->enable_sendfile;
437 conf->allow_encoded_slashes = new->allow_encoded_slashes;
442 static void *create_core_server_config(apr_pool_t *a, server_rec *s)
444 core_server_config *conf;
445 int is_virtual = s->is_virtual;
447 conf = (core_server_config *)apr_pcalloc(a, sizeof(core_server_config));
450 conf->gprof_dir = NULL;
453 conf->access_name = is_virtual ? NULL : DEFAULT_ACCESS_FNAME;
454 conf->ap_document_root = is_virtual ? NULL : DOCUMENT_LOCATION;
455 conf->sec_dir = apr_array_make(a, 40, sizeof(ap_conf_vector_t *));
456 conf->sec_url = apr_array_make(a, 40, sizeof(ap_conf_vector_t *));
458 /* recursion stopper */
459 conf->redirect_limit = 0; /* 0 == unset */
460 conf->subreq_limit = 0;
465 static void *merge_core_server_configs(apr_pool_t *p, void *basev, void *virtv)
467 core_server_config *base = (core_server_config *)basev;
468 core_server_config *virt = (core_server_config *)virtv;
469 core_server_config *conf;
471 conf = (core_server_config *)apr_palloc(p, sizeof(core_server_config));
472 memcpy(conf, virt, sizeof(core_server_config));
474 if (!conf->access_name) {
475 conf->access_name = base->access_name;
478 if (!conf->ap_document_root) {
479 conf->ap_document_root = base->ap_document_root;
482 conf->sec_dir = apr_array_append(p, base->sec_dir, virt->sec_dir);
483 conf->sec_url = apr_array_append(p, base->sec_url, virt->sec_url);
485 conf->redirect_limit = virt->redirect_limit
486 ? virt->redirect_limit
487 : base->redirect_limit;
489 conf->subreq_limit = virt->subreq_limit
491 : base->subreq_limit;
496 /* Add per-directory configuration entry (for <directory> section);
497 * these are part of the core server config.
500 AP_CORE_DECLARE(void) ap_add_per_dir_conf(server_rec *s, void *dir_config)
502 core_server_config *sconf = ap_get_module_config(s->module_config,
504 void **new_space = (void **)apr_array_push(sconf->sec_dir);
506 *new_space = dir_config;
509 AP_CORE_DECLARE(void) ap_add_per_url_conf(server_rec *s, void *url_config)
511 core_server_config *sconf = ap_get_module_config(s->module_config,
513 void **new_space = (void **)apr_array_push(sconf->sec_url);
515 *new_space = url_config;
518 AP_CORE_DECLARE(void) ap_add_file_conf(core_dir_config *conf, void *url_config)
520 void **new_space = (void **)apr_array_push(conf->sec_file);
522 *new_space = url_config;
525 /* We need to do a stable sort, qsort isn't stable. So to make it stable
526 * we'll be maintaining the original index into the list, and using it
527 * as the minor key during sorting. The major key is the number of
528 * components (where the root component is zero).
530 struct reorder_sort_rec {
531 ap_conf_vector_t *elt;
535 static int reorder_sorter(const void *va, const void *vb)
537 const struct reorder_sort_rec *a = va;
538 const struct reorder_sort_rec *b = vb;
539 core_dir_config *core_a;
540 core_dir_config *core_b;
542 core_a = ap_get_module_config(a->elt, &core_module);
543 core_b = ap_get_module_config(b->elt, &core_module);
545 /* a regex always sorts after a non-regex
547 if (!core_a->r && core_b->r) {
550 else if (core_a->r && !core_b->r) {
554 /* we always sort next by the number of components
556 if (core_a->d_components < core_b->d_components) {
559 else if (core_a->d_components > core_b->d_components) {
563 /* They have the same number of components, we now have to compare
564 * the minor key to maintain the original order (from the config.)
566 return a->orig_index - b->orig_index;
569 void ap_core_reorder_directories(apr_pool_t *p, server_rec *s)
571 core_server_config *sconf;
572 apr_array_header_t *sec_dir;
573 struct reorder_sort_rec *sortbin;
575 ap_conf_vector_t **elts;
579 sconf = ap_get_module_config(s->module_config, &core_module);
580 sec_dir = sconf->sec_dir;
581 nelts = sec_dir->nelts;
582 elts = (ap_conf_vector_t **)sec_dir->elts;
585 /* simple case of already being sorted... */
586 /* We're not checking this condition to be fast... we're checking
587 * it to avoid trying to palloc zero bytes, which can trigger some
588 * memory debuggers to barf
593 /* we have to allocate tmp space to do a stable sort */
594 apr_pool_create(&tmp, p);
595 sortbin = apr_palloc(tmp, sec_dir->nelts * sizeof(*sortbin));
596 for (i = 0; i < nelts; ++i) {
597 sortbin[i].orig_index = i;
598 sortbin[i].elt = elts[i];
601 qsort(sortbin, nelts, sizeof(*sortbin), reorder_sorter);
603 /* and now copy back to the original array */
604 for (i = 0; i < nelts; ++i) {
605 elts[i] = sortbin[i].elt;
608 apr_pool_destroy(tmp);
611 /*****************************************************************
613 * There are some elements of the core config structures in which
614 * other modules have a legitimate interest (this is ugly, but necessary
615 * to preserve NCSA back-compatibility). So, we have a bunch of accessors
619 AP_DECLARE(int) ap_allow_options(request_rec *r)
621 core_dir_config *conf =
622 (core_dir_config *)ap_get_module_config(r->per_dir_config, &core_module);
627 AP_DECLARE(int) ap_allow_overrides(request_rec *r)
629 core_dir_config *conf;
630 conf = (core_dir_config *)ap_get_module_config(r->per_dir_config,
633 return conf->override;
636 AP_DECLARE(const char *) ap_auth_type(request_rec *r)
638 core_dir_config *conf;
640 conf = (core_dir_config *)ap_get_module_config(r->per_dir_config,
643 return conf->ap_auth_type;
646 AP_DECLARE(const char *) ap_auth_name(request_rec *r)
648 core_dir_config *conf;
650 conf = (core_dir_config *)ap_get_module_config(r->per_dir_config,
653 return conf->ap_auth_name;
656 AP_DECLARE(const char *) ap_default_type(request_rec *r)
658 core_dir_config *conf;
660 conf = (core_dir_config *)ap_get_module_config(r->per_dir_config,
663 return conf->ap_default_type
664 ? conf->ap_default_type
665 : DEFAULT_CONTENT_TYPE;
668 AP_DECLARE(const char *) ap_document_root(request_rec *r) /* Don't use this! */
670 core_server_config *conf;
672 conf = (core_server_config *)ap_get_module_config(r->server->module_config,
675 return conf->ap_document_root;
678 AP_DECLARE(const apr_array_header_t *) ap_requires(request_rec *r)
680 core_dir_config *conf;
682 conf = (core_dir_config *)ap_get_module_config(r->per_dir_config,
685 return conf->ap_requires;
688 AP_DECLARE(int) ap_satisfies(request_rec *r)
690 core_dir_config *conf;
692 conf = (core_dir_config *)ap_get_module_config(r->per_dir_config,
695 return conf->satisfy[r->method_number];
698 /* Should probably just get rid of this... the only code that cares is
699 * part of the core anyway (and in fact, it isn't publicised to other
703 char *ap_response_code_string(request_rec *r, int error_index)
705 core_dir_config *dirconf;
706 core_request_config *reqconf;
708 /* check for string registered via ap_custom_response() first */
709 reqconf = (core_request_config *)ap_get_module_config(r->request_config,
711 if (reqconf->response_code_strings != NULL &&
712 reqconf->response_code_strings[error_index] != NULL) {
713 return reqconf->response_code_strings[error_index];
716 /* check for string specified via ErrorDocument */
717 dirconf = (core_dir_config *)ap_get_module_config(r->per_dir_config,
720 if (dirconf->response_code_strings == NULL) {
724 if (dirconf->response_code_strings[error_index] == &errordocument_default) {
728 return dirconf->response_code_strings[error_index];
732 /* Code from Harald Hanche-Olsen <hanche@imf.unit.no> */
733 static APR_INLINE void do_double_reverse (conn_rec *conn)
738 if (conn->double_reverse) {
743 if (conn->remote_host == NULL || conn->remote_host[0] == '\0') {
744 /* single reverse failed, so don't bother */
745 conn->double_reverse = -1;
749 rv = apr_sockaddr_info_get(&sa, conn->remote_host, APR_UNSPEC, 0, 0, conn->pool);
750 if (rv == APR_SUCCESS) {
752 if (apr_sockaddr_equal(sa, conn->remote_addr)) {
753 conn->double_reverse = 1;
761 conn->double_reverse = -1;
764 AP_DECLARE(const char *) ap_get_remote_host(conn_rec *conn, void *dir_config,
765 int type, int *str_is_ip)
767 int hostname_lookups;
768 int ignored_str_is_ip;
770 if (!str_is_ip) { /* caller doesn't want to know */
771 str_is_ip = &ignored_str_is_ip;
775 /* If we haven't checked the host name, and we want to */
778 ((core_dir_config *)ap_get_module_config(dir_config, &core_module))
781 if (hostname_lookups == HOSTNAME_LOOKUP_UNSET) {
782 hostname_lookups = HOSTNAME_LOOKUP_OFF;
787 hostname_lookups = HOSTNAME_LOOKUP_OFF;
790 if (type != REMOTE_NOLOOKUP
791 && conn->remote_host == NULL
792 && (type == REMOTE_DOUBLE_REV
793 || hostname_lookups != HOSTNAME_LOOKUP_OFF)) {
795 if (apr_getnameinfo(&conn->remote_host, conn->remote_addr, 0)
797 ap_str_tolower(conn->remote_host);
799 if (hostname_lookups == HOSTNAME_LOOKUP_DOUBLE) {
800 do_double_reverse(conn);
801 if (conn->double_reverse != 1) {
802 conn->remote_host = NULL;
807 /* if failed, set it to the NULL string to indicate error */
808 if (conn->remote_host == NULL) {
809 conn->remote_host = "";
813 if (type == REMOTE_DOUBLE_REV) {
814 do_double_reverse(conn);
815 if (conn->double_reverse == -1) {
821 * Return the desired information; either the remote DNS name, if found,
822 * or either NULL (if the hostname was requested) or the IP address
823 * (if any identifier was requested).
825 if (conn->remote_host != NULL && conn->remote_host[0] != '\0') {
826 return conn->remote_host;
829 if (type == REMOTE_HOST || type == REMOTE_DOUBLE_REV) {
834 return conn->remote_ip;
840 * Optional function coming from mod_ident, used for looking up ident user
842 static APR_OPTIONAL_FN_TYPE(ap_ident_lookup) *ident_lookup;
844 AP_DECLARE(const char *) ap_get_remote_logname(request_rec *r)
846 if (r->connection->remote_logname != NULL) {
847 return r->connection->remote_logname;
851 return ident_lookup(r);
857 /* There are two options regarding what the "name" of a server is. The
858 * "canonical" name as defined by ServerName and Port, or the "client's
859 * name" as supplied by a possible Host: header or full URI. We never
860 * trust the port passed in the client's headers, we always use the
861 * port of the actual socket.
863 * The DNS option to UseCanonicalName causes this routine to do a
864 * reverse lookup on the local IP address of the connection and use
865 * that for the ServerName. This makes its value more reliable while
866 * at the same time allowing Demon's magic virtual hosting to work.
867 * The assumption is that DNS lookups are sufficiently quick...
870 AP_DECLARE(const char *) ap_get_server_name(request_rec *r)
872 conn_rec *conn = r->connection;
875 d = (core_dir_config *)ap_get_module_config(r->per_dir_config,
878 if (d->use_canonical_name == USE_CANONICAL_NAME_OFF) {
879 return r->hostname ? r->hostname : r->server->server_hostname;
882 if (d->use_canonical_name == USE_CANONICAL_NAME_DNS) {
883 if (conn->local_host == NULL) {
884 if (apr_getnameinfo(&conn->local_host,
885 conn->local_addr, 0) != APR_SUCCESS)
886 conn->local_host = apr_pstrdup(conn->pool,
887 r->server->server_hostname);
889 ap_str_tolower(conn->local_host);
893 return conn->local_host;
897 return r->server->server_hostname;
901 * Get the current server name from the request for the purposes
902 * of using in a URL. If the server name is an IPv6 literal
903 * address, it will be returned in URL format (e.g., "[fe80::1]").
905 static const char *get_server_name_for_url(request_rec *r)
907 const char *plain_server_name = ap_get_server_name(r);
910 if (ap_strchr_c(plain_server_name, ':')) { /* IPv6 literal? */
911 return apr_psprintf(r->pool, "[%s]", plain_server_name);
914 return plain_server_name;
917 AP_DECLARE(apr_port_t) ap_get_server_port(const request_rec *r)
921 (core_dir_config *)ap_get_module_config(r->per_dir_config, &core_module);
923 if (d->use_canonical_name == USE_CANONICAL_NAME_OFF
924 || d->use_canonical_name == USE_CANONICAL_NAME_DNS) {
926 /* With UseCanonicalName off Apache will form self-referential
927 * URLs using the hostname and port supplied by the client if
928 * any are supplied (otherwise it will use the canonical name).
930 port = r->parsed_uri.port_str ? r->parsed_uri.port :
931 r->connection->local_addr->port ? r->connection->local_addr->port :
932 r->server->port ? r->server->port :
935 else { /* d->use_canonical_name == USE_CANONICAL_NAME_ON */
937 /* With UseCanonicalName on (and in all versions prior to 1.3)
938 * Apache will use the hostname and port specified in the
939 * ServerName directive to construct a canonical name for the
940 * server. (If no port was specified in the ServerName
941 * directive, Apache uses the port supplied by the client if
942 * any is supplied, and finally the default port for the protocol
945 port = r->server->port ? r->server->port :
946 r->connection->local_addr->port ? r->connection->local_addr->port :
954 AP_DECLARE(char *) ap_construct_url(apr_pool_t *p, const char *uri,
957 unsigned port = ap_get_server_port(r);
958 const char *host = get_server_name_for_url(r);
960 if (ap_is_default_port(port, r)) {
961 return apr_pstrcat(p, ap_http_scheme(r), "://", host, uri, NULL);
964 return apr_psprintf(p, "%s://%s:%u%s", ap_http_scheme(r), host, port, uri);
967 AP_DECLARE(apr_off_t) ap_get_limit_req_body(const request_rec *r)
970 (core_dir_config *)ap_get_module_config(r->per_dir_config, &core_module);
972 if (d->limit_req_body == AP_LIMIT_REQ_BODY_UNSET) {
973 return AP_DEFAULT_LIMIT_REQ_BODY;
976 return d->limit_req_body;
980 /*****************************************************************
982 * Commands... this module handles almost all of the NCSA httpd.conf
983 * commands, but most of the old srm.conf is in the the modules.
987 /* returns a parent if it matches the given directive */
988 static const ap_directive_t * find_parent(const ap_directive_t *dirp,
991 while (dirp->parent != NULL) {
994 /* ### it would be nice to have atom-ized directives */
995 if (strcasecmp(dirp->directive, what) == 0)
1002 AP_DECLARE(const char *) ap_check_cmd_context(cmd_parms *cmd,
1005 const char *gt = (cmd->cmd->name[0] == '<'
1006 && cmd->cmd->name[strlen(cmd->cmd->name)-1] != '>')
1008 const ap_directive_t *found;
1010 if ((forbidden & NOT_IN_VIRTUALHOST) && cmd->server->is_virtual) {
1011 return apr_pstrcat(cmd->pool, cmd->cmd->name, gt,
1012 " cannot occur within <VirtualHost> section", NULL);
1015 if ((forbidden & NOT_IN_LIMIT) && cmd->limited != -1) {
1016 return apr_pstrcat(cmd->pool, cmd->cmd->name, gt,
1017 " cannot occur within <Limit> section", NULL);
1020 if ((forbidden & NOT_IN_DIR_LOC_FILE) == NOT_IN_DIR_LOC_FILE) {
1021 if (cmd->path != NULL) {
1022 return apr_pstrcat(cmd->pool, cmd->cmd->name, gt,
1023 " cannot occur within <Directory/Location/Files> "
1026 if (cmd->cmd->req_override & EXEC_ON_READ) {
1027 /* EXEC_ON_READ must be NOT_IN_DIR_LOC_FILE, if not, it will
1028 * (deliberately) segfault below in the individual tests...
1034 if (((forbidden & NOT_IN_DIRECTORY)
1035 && ((found = find_parent(cmd->directive, "<Directory"))
1036 || (found = find_parent(cmd->directive, "<DirectoryMatch"))))
1037 || ((forbidden & NOT_IN_LOCATION)
1038 && ((found = find_parent(cmd->directive, "<Location"))
1039 || (found = find_parent(cmd->directive, "<LocationMatch"))))
1040 || ((forbidden & NOT_IN_FILES)
1041 && ((found = find_parent(cmd->directive, "<Files"))
1042 || (found = find_parent(cmd->directive, "<FilesMatch"))))) {
1043 return apr_pstrcat(cmd->pool, cmd->cmd->name, gt,
1044 " cannot occur within ", found->directive,
1051 static const char *set_access_name(cmd_parms *cmd, void *dummy,
1054 void *sconf = cmd->server->module_config;
1055 core_server_config *conf = ap_get_module_config(sconf, &core_module);
1057 const char *err = ap_check_cmd_context(cmd,
1058 NOT_IN_DIR_LOC_FILE|NOT_IN_LIMIT);
1063 conf->access_name = apr_pstrdup(cmd->pool, arg);
1068 static const char *set_gprof_dir(cmd_parms *cmd, void *dummy, const char *arg)
1070 void *sconf = cmd->server->module_config;
1071 core_server_config *conf = ap_get_module_config(sconf, &core_module);
1073 const char *err = ap_check_cmd_context(cmd,
1074 NOT_IN_DIR_LOC_FILE|NOT_IN_LIMIT);
1079 conf->gprof_dir = apr_pstrdup(cmd->pool, arg);
1084 static const char *set_add_default_charset(cmd_parms *cmd,
1085 void *d_, const char *arg)
1087 core_dir_config *d = d_;
1089 const char *err = ap_check_cmd_context(cmd, NOT_IN_LIMIT);
1094 if (!strcasecmp(arg, "Off")) {
1095 d->add_default_charset = ADD_DEFAULT_CHARSET_OFF;
1097 else if (!strcasecmp(arg, "On")) {
1098 d->add_default_charset = ADD_DEFAULT_CHARSET_ON;
1099 d->add_default_charset_name = DEFAULT_ADD_DEFAULT_CHARSET_NAME;
1102 d->add_default_charset = ADD_DEFAULT_CHARSET_ON;
1103 d->add_default_charset_name = arg;
1109 static const char *set_document_root(cmd_parms *cmd, void *dummy,
1112 void *sconf = cmd->server->module_config;
1113 core_server_config *conf = ap_get_module_config(sconf, &core_module);
1115 const char *err = ap_check_cmd_context(cmd,
1116 NOT_IN_DIR_LOC_FILE|NOT_IN_LIMIT);
1121 /* Make it absolute, relative to ServerRoot */
1122 arg = ap_server_root_relative(cmd->pool, arg);
1124 /* TODO: ap_configtestonly && ap_docrootcheck && */
1125 if (apr_filepath_merge((char**)&conf->ap_document_root, NULL, arg,
1126 APR_FILEPATH_TRUENAME, cmd->pool) != APR_SUCCESS
1127 || !ap_is_directory(cmd->pool, arg)) {
1128 if (cmd->server->is_virtual) {
1129 ap_log_perror(APLOG_MARK, APLOG_STARTUP, 0,
1131 "Warning: DocumentRoot [%s] does not exist",
1133 conf->ap_document_root = arg;
1136 return "DocumentRoot must be a directory";
1142 AP_DECLARE(void) ap_custom_response(request_rec *r, int status,
1145 core_request_config *conf =
1146 ap_get_module_config(r->request_config, &core_module);
1149 if (conf->response_code_strings == NULL) {
1150 conf->response_code_strings =
1151 apr_pcalloc(r->pool,
1152 sizeof(*conf->response_code_strings) * RESPONSE_CODES);
1155 idx = ap_index_of_response(status);
1157 conf->response_code_strings[idx] =
1158 ((ap_is_url(string) || (*string == '/')) && (*string != '"')) ?
1159 apr_pstrdup(r->pool, string) : apr_pstrcat(r->pool, "\"", string, NULL);
1162 static const char *set_error_document(cmd_parms *cmd, void *conf_,
1163 const char *errno_str, const char *msg)
1165 core_dir_config *conf = conf_;
1166 int error_number, index_number, idx500;
1167 enum { MSG, LOCAL_PATH, REMOTE_PATH } what = MSG;
1169 const char *err = ap_check_cmd_context(cmd, NOT_IN_LIMIT);
1174 /* 1st parameter should be a 3 digit number, which we recognize;
1175 * convert it into an array index
1177 error_number = atoi(errno_str);
1178 idx500 = ap_index_of_response(HTTP_INTERNAL_SERVER_ERROR);
1180 if (error_number == HTTP_INTERNAL_SERVER_ERROR) {
1181 index_number = idx500;
1183 else if ((index_number = ap_index_of_response(error_number)) == idx500) {
1184 return apr_pstrcat(cmd->pool, "Unsupported HTTP response code ",
1188 /* Heuristic to determine second argument. */
1189 if (ap_strchr_c(msg,' '))
1191 else if (msg[0] == '/')
1193 else if (ap_is_url(msg))
1198 /* The entry should be ignored if it is a full URL for a 401 error */
1200 if (error_number == 401 && what == REMOTE_PATH) {
1201 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, cmd->server,
1202 "cannot use a full URL in a 401 ErrorDocument "
1203 "directive --- ignoring!");
1205 else { /* Store it... */
1206 if (conf->response_code_strings == NULL) {
1207 conf->response_code_strings =
1208 apr_pcalloc(cmd->pool,
1209 sizeof(*conf->response_code_strings) *
1213 if (strcmp(msg, "default") == 0) {
1214 /* special case: ErrorDocument 404 default restores the
1215 * canned server error response
1217 conf->response_code_strings[index_number] = &errordocument_default;
1220 /* hack. Prefix a " if it is a msg; as that is what
1221 * http_protocol.c relies on to distinguish between
1222 * a msg and a (local) path.
1224 conf->response_code_strings[index_number] = (what == MSG) ?
1225 apr_pstrcat(cmd->pool, "\"",msg,NULL) :
1226 apr_pstrdup(cmd->pool, msg);
1233 static const char *set_allow_opts(cmd_parms *cmd, allow_options_t *opts,
1236 allow_options_t opt;
1239 char *w, *p = (char *) l;
1242 while ((w = apr_strtok(p, ",", &tok_state)) != NULL) {
1250 if (!strcasecmp(w, "Indexes")) {
1253 else if (!strcasecmp(w, "Includes")) {
1256 else if (!strcasecmp(w, "IncludesNOEXEC")) {
1257 opt = (OPT_INCLUDES | OPT_INCNOEXEC);
1259 else if (!strcasecmp(w, "FollowSymLinks")) {
1260 opt = OPT_SYM_LINKS;
1262 else if (!strcasecmp(w, "SymLinksIfOwnerMatch")) {
1263 opt = OPT_SYM_OWNER;
1265 else if (!strcasecmp(w, "ExecCGI")) {
1268 else if (!strcasecmp(w, "MultiViews")) {
1271 else if (!strcasecmp(w, "RunScripts")) { /* AI backcompat. Yuck */
1272 opt = OPT_MULTI|OPT_EXECCGI;
1274 else if (!strcasecmp(w, "None")) {
1277 else if (!strcasecmp(w, "All")) {
1281 return apr_pstrcat(cmd->pool, "Illegal option ", w, NULL);
1287 (*opts) &= (~OPT_UNSET);
1292 static const char *set_override(cmd_parms *cmd, void *d_, const char *l)
1294 core_dir_config *d = d_;
1298 const char *err = ap_check_cmd_context(cmd, NOT_IN_LIMIT);
1303 /* Throw a warning if we're in <Location> or <Files> */
1304 if (ap_check_cmd_context(cmd, NOT_IN_LOCATION | NOT_IN_FILES)) {
1305 ap_log_error(APLOG_MARK, APLOG_WARNING, 0, cmd->server,
1306 "Useless use of AllowOverride in line %d.",
1307 cmd->directive->line_num);
1310 d->override = OR_NONE;
1312 w = ap_getword_conf(cmd->pool, &l);
1320 if (!strcasecmp(w, "Limit")) {
1321 d->override |= OR_LIMIT;
1323 else if (!strcasecmp(k, "Options")) {
1324 d->override |= OR_OPTIONS;
1326 set_allow_opts(cmd, &(d->override_opts), v);
1328 d->override_opts = OPT_ALL;
1330 else if (!strcasecmp(w, "FileInfo")) {
1331 d->override |= OR_FILEINFO;
1333 else if (!strcasecmp(w, "AuthConfig")) {
1334 d->override |= OR_AUTHCFG;
1336 else if (!strcasecmp(w, "Indexes")) {
1337 d->override |= OR_INDEXES;
1339 else if (!strcasecmp(w, "None")) {
1340 d->override = OR_NONE;
1342 else if (!strcasecmp(w, "All")) {
1343 d->override = OR_ALL;
1346 return apr_pstrcat(cmd->pool, "Illegal override option ", w, NULL);
1349 d->override &= ~OR_UNSET;
1355 static const char *set_options(cmd_parms *cmd, void *d_, const char *l)
1357 core_dir_config *d = d_;
1358 allow_options_t opt;
1363 char *w = ap_getword_conf(cmd->pool, &l);
1366 if (*w == '+' || *w == '-') {
1374 if (!strcasecmp(w, "Indexes")) {
1377 else if (!strcasecmp(w, "Includes")) {
1380 else if (!strcasecmp(w, "IncludesNOEXEC")) {
1381 opt = (OPT_INCLUDES | OPT_INCNOEXEC);
1383 else if (!strcasecmp(w, "FollowSymLinks")) {
1384 opt = OPT_SYM_LINKS;
1386 else if (!strcasecmp(w, "SymLinksIfOwnerMatch")) {
1387 opt = OPT_SYM_OWNER;
1389 else if (!strcasecmp(w, "ExecCGI")) {
1392 else if (!strcasecmp(w, "MultiViews")) {
1395 else if (!strcasecmp(w, "RunScripts")) { /* AI backcompat. Yuck */
1396 opt = OPT_MULTI|OPT_EXECCGI;
1398 else if (!strcasecmp(w, "None")) {
1401 else if (!strcasecmp(w, "All")) {
1405 return apr_pstrcat(cmd->pool, "Illegal option ", w, NULL);
1408 if (!(cmd->override_opts & opt) && opt != OPT_NONE) {
1409 return apr_pstrcat(cmd->pool, "Option ", w, " not allowed here", NULL);
1411 else if (action == '-') {
1412 /* we ensure the invariant (d->opts_add & d->opts_remove) == 0 */
1413 d->opts_remove |= opt;
1414 d->opts_add &= ~opt;
1417 else if (action == '+') {
1419 d->opts_remove &= ~opt;
1431 * Note what data should be used when forming file ETag values.
1432 * It would be nicer to do this as an ITERATE, but then we couldn't
1433 * remember the +/- state properly.
1435 static const char *set_etag_bits(cmd_parms *cmd, void *mconfig,
1438 core_dir_config *cfg;
1439 etag_components_t bit;
1447 cfg = (core_dir_config *)mconfig;
1452 while (args[0] != '\0') {
1456 token = ap_getword_conf(cmd->pool, &args);
1457 if ((*token == '+') || (*token == '-')) {
1463 * The occurrence of an absolute setting wipes
1464 * out any previous relative ones. The first such
1465 * occurrence forgets any inherited ones, too.
1468 cfg->etag_bits = ETAG_UNSET;
1469 cfg->etag_add = ETAG_UNSET;
1470 cfg->etag_remove = ETAG_UNSET;
1475 if (strcasecmp(token, "None") == 0) {
1476 if (action != '*') {
1480 cfg->etag_bits = bit = ETAG_NONE;
1484 else if (strcasecmp(token, "All") == 0) {
1485 if (action != '*') {
1490 cfg->etag_bits = bit = ETAG_ALL;
1493 else if (strcasecmp(token, "Size") == 0) {
1496 else if ((strcasecmp(token, "LMTime") == 0)
1497 || (strcasecmp(token, "MTime") == 0)
1498 || (strcasecmp(token, "LastModified") == 0)) {
1501 else if (strcasecmp(token, "INode") == 0) {
1505 return apr_pstrcat(cmd->pool, "Unknown keyword '",
1506 token, "' for ", cmd->cmd->name,
1507 " directive", NULL);
1511 return apr_pstrcat(cmd->pool, cmd->cmd->name, " keyword '",
1512 token, "' cannot be used with '+' or '-'",
1516 if (action == '+') {
1518 * Make sure it's in the 'add' list and absent from the
1521 cfg->etag_add |= bit;
1522 cfg->etag_remove &= (~ bit);
1524 else if (action == '-') {
1525 cfg->etag_remove |= bit;
1526 cfg->etag_add &= (~ bit);
1530 * Non-relative values wipe out any + or - values
1531 * accumulated so far.
1533 cfg->etag_bits |= bit;
1534 cfg->etag_add = ETAG_UNSET;
1535 cfg->etag_remove = ETAG_UNSET;
1541 * Any setting at all will clear the 'None' and 'Unset' bits.
1544 if (cfg->etag_add != ETAG_UNSET) {
1545 cfg->etag_add &= (~ ETAG_UNSET);
1548 if (cfg->etag_remove != ETAG_UNSET) {
1549 cfg->etag_remove &= (~ ETAG_UNSET);
1553 cfg->etag_bits &= (~ ETAG_UNSET);
1555 if ((cfg->etag_bits & ETAG_NONE) != ETAG_NONE) {
1556 cfg->etag_bits &= (~ ETAG_NONE);
1563 static const char *set_enable_mmap(cmd_parms *cmd, void *d_,
1566 core_dir_config *d = d_;
1567 const char *err = ap_check_cmd_context(cmd, NOT_IN_LIMIT);
1573 if (strcasecmp(arg, "on") == 0) {
1574 d->enable_mmap = ENABLE_MMAP_ON;
1576 else if (strcasecmp(arg, "off") == 0) {
1577 d->enable_mmap = ENABLE_MMAP_OFF;
1580 return "parameter must be 'on' or 'off'";
1586 static const char *set_enable_sendfile(cmd_parms *cmd, void *d_,
1589 core_dir_config *d = d_;
1590 const char *err = ap_check_cmd_context(cmd, NOT_IN_LIMIT);
1596 if (strcasecmp(arg, "on") == 0) {
1597 d->enable_sendfile = ENABLE_SENDFILE_ON;
1599 else if (strcasecmp(arg, "off") == 0) {
1600 d->enable_sendfile = ENABLE_SENDFILE_OFF;
1603 return "parameter must be 'on' or 'off'";
1609 static const char *satisfy(cmd_parms *cmd, void *c_, const char *arg)
1611 core_dir_config *c = c_;
1612 int satisfy = SATISFY_NOSPEC;
1615 if (!strcasecmp(arg, "all")) {
1616 satisfy = SATISFY_ALL;
1618 else if (!strcasecmp(arg, "any")) {
1619 satisfy = SATISFY_ANY;
1622 return "Satisfy either 'any' or 'all'.";
1625 for (i = 0; i < METHODS; ++i) {
1626 if (cmd->limited & (AP_METHOD_BIT << i)) {
1627 c->satisfy[i] = satisfy;
1634 static const char *require(cmd_parms *cmd, void *c_, const char *arg)
1637 core_dir_config *c = c_;
1639 if (!c->ap_requires) {
1640 c->ap_requires = apr_array_make(cmd->pool, 2, sizeof(require_line));
1643 r = (require_line *)apr_array_push(c->ap_requires);
1644 r->requirement = apr_pstrdup(cmd->pool, arg);
1645 r->method_mask = cmd->limited;
1651 * Report a missing-'>' syntax error.
1653 static char *unclosed_directive(cmd_parms *cmd)
1655 return apr_pstrcat(cmd->pool, cmd->cmd->name,
1656 "> directive missing closing '>'", NULL);
1660 * Report a missing args in '<Foo >' syntax error.
1662 static char *missing_container_arg(cmd_parms *cmd)
1664 return apr_pstrcat(cmd->pool, cmd->cmd->name,
1665 "> directive requires additional arguments", NULL);
1668 AP_CORE_DECLARE_NONSTD(const char *) ap_limit_section(cmd_parms *cmd,
1672 const char *endp = ap_strrchr_c(arg, '>');
1673 const char *limited_methods;
1674 void *tog = cmd->cmd->cmd_data;
1675 apr_int64_t limited = 0;
1678 const char *err = ap_check_cmd_context(cmd, NOT_IN_LIMIT);
1684 return unclosed_directive(cmd);
1687 limited_methods = apr_pstrndup(cmd->pool, arg, endp - arg);
1689 if (!limited_methods[0]) {
1690 return missing_container_arg(cmd);
1693 while (limited_methods[0]) {
1694 char *method = ap_getword_conf(cmd->pool, &limited_methods);
1697 /* check for builtin or module registered method number */
1698 methnum = ap_method_number_of(method);
1700 if (methnum == M_TRACE && !tog) {
1701 return "TRACE cannot be controlled by <Limit>";
1703 else if (methnum == M_INVALID) {
1704 /* method has not been registered yet, but resorce restriction
1705 * is always checked before method handling, so register it.
1707 methnum = ap_method_register(cmd->pool, method);
1710 limited |= (AP_METHOD_BIT << methnum);
1713 /* Killing two features with one function,
1714 * if (tog == NULL) <Limit>, else <LimitExcept>
1716 cmd->limited = tog ? ~limited : limited;
1718 errmsg = ap_walk_config(cmd->directive->first_child, cmd, cmd->context);
1725 /* XXX: Bogus - need to do this differently (at least OS2/Netware suffer
1726 * the same problem!!!
1727 * We use this in <DirectoryMatch> and <FilesMatch>, to ensure that
1728 * people don't get bitten by wrong-cased regex matches
1732 #define USE_ICASE REG_ICASE
1737 static const char *dirsection(cmd_parms *cmd, void *mconfig, const char *arg)
1740 const char *endp = ap_strrchr_c(arg, '>');
1741 int old_overrides = cmd->override;
1742 char *old_path = cmd->path;
1743 core_dir_config *conf;
1744 ap_conf_vector_t *new_dir_conf = ap_create_per_dir_config(cmd->pool);
1746 const command_rec *thiscmd = cmd->cmd;
1748 const char *err = ap_check_cmd_context(cmd,
1749 NOT_IN_DIR_LOC_FILE|NOT_IN_LIMIT);
1755 return unclosed_directive(cmd);
1758 arg = apr_pstrndup(cmd->pool, arg, endp - arg);
1761 return missing_container_arg(cmd);
1765 if (thiscmd->cmd_data)
1766 return "<DirectoryMatch > block must specify a path";
1768 return "<Directory > block must specify a path";
1771 cmd->path = ap_getword_conf(cmd->pool, &arg);
1772 cmd->override = OR_ALL|ACCESS_CONF;
1774 if (!strcmp(cmd->path, "~")) {
1775 cmd->path = ap_getword_conf(cmd->pool, &arg);
1777 return "<Directory ~ > block must specify a path";
1778 r = ap_pregcomp(cmd->pool, cmd->path, REG_EXTENDED|USE_ICASE);
1780 return "Regex could not be compiled";
1783 else if (thiscmd->cmd_data) { /* <DirectoryMatch> */
1784 r = ap_pregcomp(cmd->pool, cmd->path, REG_EXTENDED|USE_ICASE);
1786 return "Regex could not be compiled";
1789 else if (!strcmp(cmd->path, "/") == 0)
1794 * Ensure that the pathname is canonical, and append the trailing /
1796 apr_status_t rv = apr_filepath_merge(&newpath, NULL, cmd->path,
1797 APR_FILEPATH_TRUENAME, cmd->pool);
1798 if (rv != APR_SUCCESS && rv != APR_EPATHWILD) {
1799 return apr_pstrcat(cmd->pool, "<Directory \"", cmd->path,
1800 "\"> path is invalid.", NULL);
1803 cmd->path = newpath;
1804 if (cmd->path[strlen(cmd->path) - 1] != '/')
1805 cmd->path = apr_pstrcat(cmd->pool, cmd->path, "/", NULL);
1808 /* initialize our config and fetch it */
1809 conf = ap_set_config_vectors(cmd->server, new_dir_conf, cmd->path,
1810 &core_module, cmd->pool);
1812 errmsg = ap_walk_config(cmd->directive->first_child, cmd, new_dir_conf);
1817 conf->d = cmd->path;
1818 conf->d_is_fnmatch = (apr_fnmatch_test(conf->d) != 0);
1820 /* Make this explicit - the "/" root has 0 elements, that is, we
1821 * will always merge it, and it will always sort and merge first.
1822 * All others are sorted and tested by the number of slashes.
1824 if (strcmp(conf->d, "/") == 0)
1825 conf->d_components = 0;
1827 conf->d_components = ap_count_dirs(conf->d);
1829 ap_add_per_dir_conf(cmd->server, new_dir_conf);
1832 return apr_pstrcat(cmd->pool, "Multiple ", thiscmd->name,
1833 "> arguments not (yet) supported.", NULL);
1836 cmd->path = old_path;
1837 cmd->override = old_overrides;
1842 static const char *urlsection(cmd_parms *cmd, void *mconfig, const char *arg)
1845 const char *endp = ap_strrchr_c(arg, '>');
1846 int old_overrides = cmd->override;
1847 char *old_path = cmd->path;
1848 core_dir_config *conf;
1850 const command_rec *thiscmd = cmd->cmd;
1851 ap_conf_vector_t *new_url_conf = ap_create_per_dir_config(cmd->pool);
1852 const char *err = ap_check_cmd_context(cmd,
1853 NOT_IN_DIR_LOC_FILE|NOT_IN_LIMIT);
1859 return unclosed_directive(cmd);
1862 arg = apr_pstrndup(cmd->pool, arg, endp - arg);
1865 return missing_container_arg(cmd);
1868 cmd->path = ap_getword_conf(cmd->pool, &arg);
1869 cmd->override = OR_ALL|ACCESS_CONF;
1871 if (thiscmd->cmd_data) { /* <LocationMatch> */
1872 r = ap_pregcomp(cmd->pool, cmd->path, REG_EXTENDED);
1874 return "Regex could not be compiled";
1877 else if (!strcmp(cmd->path, "~")) {
1878 cmd->path = ap_getword_conf(cmd->pool, &arg);
1879 r = ap_pregcomp(cmd->pool, cmd->path, REG_EXTENDED);
1881 return "Regex could not be compiled";
1885 /* initialize our config and fetch it */
1886 conf = ap_set_config_vectors(cmd->server, new_url_conf, cmd->path,
1887 &core_module, cmd->pool);
1889 errmsg = ap_walk_config(cmd->directive->first_child, cmd, new_url_conf);
1893 conf->d = apr_pstrdup(cmd->pool, cmd->path); /* No mangling, please */
1894 conf->d_is_fnmatch = apr_fnmatch_test(conf->d) != 0;
1897 ap_add_per_url_conf(cmd->server, new_url_conf);
1900 return apr_pstrcat(cmd->pool, "Multiple ", thiscmd->name,
1901 "> arguments not (yet) supported.", NULL);
1904 cmd->path = old_path;
1905 cmd->override = old_overrides;
1910 static const char *filesection(cmd_parms *cmd, void *mconfig, const char *arg)
1913 const char *endp = ap_strrchr_c(arg, '>');
1914 int old_overrides = cmd->override;
1915 char *old_path = cmd->path;
1916 core_dir_config *conf;
1918 const command_rec *thiscmd = cmd->cmd;
1919 core_dir_config *c = mconfig;
1920 ap_conf_vector_t *new_file_conf = ap_create_per_dir_config(cmd->pool);
1921 const char *err = ap_check_cmd_context(cmd, NOT_IN_LIMIT|NOT_IN_LOCATION);
1928 return unclosed_directive(cmd);
1931 arg = apr_pstrndup(cmd->pool, arg, endp - arg);
1934 return missing_container_arg(cmd);
1937 cmd->path = ap_getword_conf(cmd->pool, &arg);
1938 /* Only if not an .htaccess file */
1940 cmd->override = OR_ALL|ACCESS_CONF;
1943 if (thiscmd->cmd_data) { /* <FilesMatch> */
1944 r = ap_pregcomp(cmd->pool, cmd->path, REG_EXTENDED|USE_ICASE);
1946 return "Regex could not be compiled";
1949 else if (!strcmp(cmd->path, "~")) {
1950 cmd->path = ap_getword_conf(cmd->pool, &arg);
1951 r = ap_pregcomp(cmd->pool, cmd->path, REG_EXTENDED|USE_ICASE);
1953 return "Regex could not be compiled";
1958 /* Ensure that the pathname is canonical, but we
1959 * can't test the case/aliases without a fixed path */
1960 if (apr_filepath_merge(&newpath, "", cmd->path,
1961 0, cmd->pool) != APR_SUCCESS)
1962 return apr_pstrcat(cmd->pool, "<Files \"", cmd->path,
1963 "\"> is invalid.", NULL);
1964 cmd->path = newpath;
1967 /* initialize our config and fetch it */
1968 conf = ap_set_config_vectors(cmd->server, new_file_conf, cmd->path,
1969 &core_module, cmd->pool);
1971 errmsg = ap_walk_config(cmd->directive->first_child, cmd, new_file_conf);
1975 conf->d = cmd->path;
1976 conf->d_is_fnmatch = apr_fnmatch_test(conf->d) != 0;
1979 ap_add_file_conf(c, new_file_conf);
1982 return apr_pstrcat(cmd->pool, "Multiple ", thiscmd->name,
1983 "> arguments not (yet) supported.", NULL);
1986 cmd->path = old_path;
1987 cmd->override = old_overrides;
1992 static const char *start_ifmod(cmd_parms *cmd, void *mconfig, const char *arg)
1994 const char *endp = ap_strrchr_c(arg, '>');
1995 int not = (arg[0] == '!');
1999 return unclosed_directive(cmd);
2002 arg = apr_pstrndup(cmd->pool, arg, endp - arg);
2009 return missing_container_arg(cmd);
2012 found = ap_find_linked_module(arg);
2014 /* search prelinked stuff */
2016 ap_module_symbol_t *current = ap_prelinked_module_symbols;
2018 for (; current->name; ++current) {
2019 if (!strcmp(current->name, arg)) {
2020 found = current->modp;
2026 /* search dynamic stuff */
2028 APR_OPTIONAL_FN_TYPE(ap_find_loaded_module_symbol) *check_symbol =
2029 APR_RETRIEVE_OPTIONAL_FN(ap_find_loaded_module_symbol);
2032 found = check_symbol(cmd->server, arg);
2036 if ((!not && found) || (not && !found)) {
2037 ap_directive_t *parent = NULL;
2038 ap_directive_t *current = NULL;
2041 retval = ap_build_cont_config(cmd->pool, cmd->temp_pool, cmd,
2042 ¤t, &parent, "<IfModule");
2043 *(ap_directive_t **)mconfig = current;
2047 *(ap_directive_t **)mconfig = NULL;
2048 return ap_soak_end_container(cmd, "<IfModule");
2052 AP_DECLARE(int) ap_exists_config_define(const char *name)
2057 defines = (char **)ap_server_config_defines->elts;
2058 for (i = 0; i < ap_server_config_defines->nelts; i++) {
2059 if (strcmp(defines[i], name) == 0) {
2067 static const char *start_ifdefine(cmd_parms *cmd, void *dummy, const char *arg)
2073 endp = ap_strrchr_c(arg, '>');
2075 return unclosed_directive(cmd);
2078 arg = apr_pstrndup(cmd->pool, arg, endp - arg);
2080 if (arg[0] == '!') {
2086 return missing_container_arg(cmd);
2089 defined = ap_exists_config_define(arg);
2090 if ((!not && defined) || (not && !defined)) {
2091 ap_directive_t *parent = NULL;
2092 ap_directive_t *current = NULL;
2095 retval = ap_build_cont_config(cmd->pool, cmd->temp_pool, cmd,
2096 ¤t, &parent, "<IfDefine");
2097 *(ap_directive_t **)dummy = current;
2101 *(ap_directive_t **)dummy = NULL;
2102 return ap_soak_end_container(cmd, "<IfDefine");
2106 /* httpd.conf commands... beginning with the <VirtualHost> business */
2108 static const char *virtualhost_section(cmd_parms *cmd, void *dummy,
2111 server_rec *main_server = cmd->server, *s;
2113 const char *endp = ap_strrchr_c(arg, '>');
2114 apr_pool_t *p = cmd->pool;
2116 const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
2122 return unclosed_directive(cmd);
2125 arg = apr_pstrndup(cmd->pool, arg, endp - arg);
2128 return missing_container_arg(cmd);
2131 /* FIXME: There's another feature waiting to happen here -- since you
2132 can now put multiple addresses/names on a single <VirtualHost>
2133 you might want to use it to group common definitions and then
2134 define other "subhosts" with their individual differences. But
2135 personally I'd rather just do it with a macro preprocessor. -djg */
2136 if (main_server->is_virtual) {
2137 return "<VirtualHost> doesn't nest!";
2140 errmsg = ap_init_virtual_host(p, arg, main_server, &s);
2145 s->next = main_server->next;
2146 main_server->next = s;
2148 s->defn_name = cmd->directive->filename;
2149 s->defn_line_number = cmd->directive->line_num;
2153 errmsg = ap_walk_config(cmd->directive->first_child, cmd,
2154 s->lookup_defaults);
2156 cmd->server = main_server;
2161 static const char *set_server_alias(cmd_parms *cmd, void *dummy,
2164 if (!cmd->server->names) {
2165 return "ServerAlias only used in <VirtualHost>";
2169 char **item, *name = ap_getword_conf(cmd->pool, &arg);
2171 if (ap_is_matchexp(name)) {
2172 item = (char **)apr_array_push(cmd->server->wild_names);
2175 item = (char **)apr_array_push(cmd->server->names);
2184 static const char *set_server_string_slot(cmd_parms *cmd, void *dummy,
2187 /* This one's pretty generic... */
2189 int offset = (int)(long)cmd->info;
2190 char *struct_ptr = (char *)cmd->server;
2192 const char *err = ap_check_cmd_context(cmd,
2193 NOT_IN_DIR_LOC_FILE|NOT_IN_LIMIT);
2198 *(const char **)(struct_ptr + offset) = arg;
2202 static const char *server_hostname_port(cmd_parms *cmd, void *dummy, const char *arg)
2204 const char *err = ap_check_cmd_context(cmd, NOT_IN_DIR_LOC_FILE|NOT_IN_LIMIT);
2205 const char *portstr;
2212 portstr = ap_strchr_c(arg, ':');
2214 cmd->server->server_hostname = apr_pstrndup(cmd->pool, arg,
2217 port = atoi(portstr);
2218 if (port <= 0 || port >= 65536) { /* 65536 == 1<<16 */
2219 return apr_pstrcat(cmd->temp_pool, "The port number \"", arg,
2220 "\" is outside the appropriate range "
2221 "(i.e., 1..65535).", NULL);
2225 cmd->server->server_hostname = apr_pstrdup(cmd->pool, arg);
2229 cmd->server->port = port;
2233 static const char *set_signature_flag(cmd_parms *cmd, void *d_,
2236 core_dir_config *d = d_;
2237 const char *err = ap_check_cmd_context(cmd, NOT_IN_LIMIT);
2243 if (strcasecmp(arg, "On") == 0) {
2244 d->server_signature = srv_sig_on;
2246 else if (strcasecmp(arg, "Off") == 0) {
2247 d->server_signature = srv_sig_off;
2249 else if (strcasecmp(arg, "EMail") == 0) {
2250 d->server_signature = srv_sig_withmail;
2253 return "ServerSignature: use one of: off | on | email";
2259 static const char *set_server_root(cmd_parms *cmd, void *dummy,
2262 const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
2268 if ((apr_filepath_merge((char**)&ap_server_root, NULL, arg,
2269 APR_FILEPATH_TRUENAME, cmd->pool) != APR_SUCCESS)
2270 || !ap_is_directory(cmd->pool, ap_server_root)) {
2271 return "ServerRoot must be a valid directory";
2277 static const char *set_timeout(cmd_parms *cmd, void *dummy, const char *arg)
2279 const char *err = ap_check_cmd_context(cmd, NOT_IN_DIR_LOC_FILE|NOT_IN_LIMIT);
2285 cmd->server->timeout = apr_time_from_sec(atoi(arg));
2289 static const char *set_allow2f(cmd_parms *cmd, void *d_, int arg)
2291 core_dir_config *d = d_;
2292 const char *err = ap_check_cmd_context(cmd, NOT_IN_LIMIT);
2298 d->allow_encoded_slashes = arg != 0;
2302 static const char *set_hostname_lookups(cmd_parms *cmd, void *d_,
2305 core_dir_config *d = d_;
2306 const char *err = ap_check_cmd_context(cmd, NOT_IN_LIMIT);
2312 if (!strcasecmp(arg, "on")) {
2313 d->hostname_lookups = HOSTNAME_LOOKUP_ON;
2315 else if (!strcasecmp(arg, "off")) {
2316 d->hostname_lookups = HOSTNAME_LOOKUP_OFF;
2318 else if (!strcasecmp(arg, "double")) {
2319 d->hostname_lookups = HOSTNAME_LOOKUP_DOUBLE;
2322 return "parameter must be 'on', 'off', or 'double'";
2328 static const char *set_serverpath(cmd_parms *cmd, void *dummy,
2331 const char *err = ap_check_cmd_context(cmd, NOT_IN_DIR_LOC_FILE|NOT_IN_LIMIT);
2337 cmd->server->path = arg;
2338 cmd->server->pathlen = (int)strlen(arg);
2342 static const char *set_content_md5(cmd_parms *cmd, void *d_, int arg)
2344 core_dir_config *d = d_;
2345 const char *err = ap_check_cmd_context(cmd, NOT_IN_LIMIT);
2351 d->content_md5 = arg != 0;
2355 static const char *set_accept_path_info(cmd_parms *cmd, void *d_, const char *arg)
2357 core_dir_config *d = d_;
2359 if (strcasecmp(arg, "on") == 0) {
2360 d->accept_path_info = AP_REQ_ACCEPT_PATH_INFO;
2362 else if (strcasecmp(arg, "off") == 0) {
2363 d->accept_path_info = AP_REQ_REJECT_PATH_INFO;
2365 else if (strcasecmp(arg, "default") == 0) {
2366 d->accept_path_info = AP_REQ_DEFAULT_PATH_INFO;
2369 return "AcceptPathInfo must be set to on, off or default";
2375 static const char *set_use_canonical_name(cmd_parms *cmd, void *d_,
2378 core_dir_config *d = d_;
2379 const char *err = ap_check_cmd_context(cmd, NOT_IN_LIMIT);
2385 if (strcasecmp(arg, "on") == 0) {
2386 d->use_canonical_name = USE_CANONICAL_NAME_ON;
2388 else if (strcasecmp(arg, "off") == 0) {
2389 d->use_canonical_name = USE_CANONICAL_NAME_OFF;
2391 else if (strcasecmp(arg, "dns") == 0) {
2392 d->use_canonical_name = USE_CANONICAL_NAME_DNS;
2395 return "parameter must be 'on', 'off', or 'dns'";
2402 static const char *include_config (cmd_parms *cmd, void *dummy,
2405 ap_directive_t *conftree = NULL;
2406 const char* conffile, *error;
2407 unsigned *recursion;
2410 apr_pool_userdata_get(&data, "ap_include_sentinel", cmd->pool);
2415 data = recursion = apr_palloc(cmd->pool, sizeof(*recursion));
2417 apr_pool_userdata_setn(data, "ap_include_sentinel", NULL, cmd->pool);
2420 if (++*recursion > AP_MAX_INCLUDE_DEPTH) {
2422 return apr_psprintf(cmd->pool, "Exceeded maximum include depth of %u. "
2423 "You have probably a recursion somewhere.",
2424 AP_MAX_INCLUDE_DEPTH);
2427 conffile = ap_server_root_relative(cmd->pool, name);
2430 return apr_pstrcat(cmd->pool, "Invalid Include path ",
2434 error = ap_process_resource_config(cmd->server, conffile,
2435 &conftree, cmd->pool, cmd->temp_pool);
2441 *(ap_directive_t **)dummy = conftree;
2443 /* recursion level done */
2451 static const char *set_loglevel(cmd_parms *cmd, void *dummy, const char *arg)
2455 const char *err = ap_check_cmd_context(cmd,
2456 NOT_IN_DIR_LOC_FILE|NOT_IN_LIMIT);
2461 if ((str = ap_getword_conf(cmd->pool, &arg))) {
2462 if (!strcasecmp(str, "emerg")) {
2463 cmd->server->loglevel = APLOG_EMERG;
2465 else if (!strcasecmp(str, "alert")) {
2466 cmd->server->loglevel = APLOG_ALERT;
2468 else if (!strcasecmp(str, "crit")) {
2469 cmd->server->loglevel = APLOG_CRIT;
2471 else if (!strcasecmp(str, "error")) {
2472 cmd->server->loglevel = APLOG_ERR;
2474 else if (!strcasecmp(str, "warn")) {
2475 cmd->server->loglevel = APLOG_WARNING;
2477 else if (!strcasecmp(str, "notice")) {
2478 cmd->server->loglevel = APLOG_NOTICE;
2480 else if (!strcasecmp(str, "info")) {
2481 cmd->server->loglevel = APLOG_INFO;
2483 else if (!strcasecmp(str, "debug")) {
2484 cmd->server->loglevel = APLOG_DEBUG;
2487 return "LogLevel requires level keyword: one of "
2488 "emerg/alert/crit/error/warn/notice/info/debug";
2492 return "LogLevel requires level keyword";
2498 AP_DECLARE(const char *) ap_psignature(const char *prefix, request_rec *r)
2501 core_dir_config *conf;
2503 conf = (core_dir_config *)ap_get_module_config(r->per_dir_config,
2505 if ((conf->server_signature == srv_sig_off)
2506 || (conf->server_signature == srv_sig_unset)) {
2510 apr_snprintf(sport, sizeof sport, "%u", (unsigned) ap_get_server_port(r));
2512 if (conf->server_signature == srv_sig_withmail) {
2513 return apr_pstrcat(r->pool, prefix, "<address>",
2514 ap_get_server_version(),
2515 " Server at <a href=\"",
2516 ap_is_url(r->server->server_admin) ? "" : "mailto:",
2517 ap_escape_html(r->pool, r->server->server_admin),
2519 ap_escape_html(r->pool, ap_get_server_name(r)),
2520 "</a> Port ", sport,
2521 "</address>\n", NULL);
2524 return apr_pstrcat(r->pool, prefix, "<address>", ap_get_server_version(),
2526 ap_escape_html(r->pool, ap_get_server_name(r)),
2528 "</address>\n", NULL);
2532 * Load an authorisation realm into our location configuration, applying the
2533 * usual rules that apply to realms.
2535 static const char *set_authname(cmd_parms *cmd, void *mconfig,
2538 core_dir_config *aconfig = (core_dir_config *)mconfig;
2540 aconfig->ap_auth_name = ap_escape_quotes(cmd->pool, word1);
2545 * Handle a request to include the server's OS platform in the Server
2546 * response header field (the ServerTokens directive). Unfortunately
2547 * this requires a new global in order to communicate the setting back to
2548 * http_main so it can insert the information in the right place in the
2552 static char *server_version = NULL;
2553 static int version_locked = 0;
2555 enum server_token_type {
2556 SrvTk_MAJOR, /* eg: Apache/2 */
2557 SrvTk_MINOR, /* eg. Apache/2.0 */
2558 SrvTk_MINIMAL, /* eg: Apache/2.0.41 */
2559 SrvTk_OS, /* eg: Apache/2.0.41 (UNIX) */
2560 SrvTk_FULL, /* eg: Apache/2.0.41 (UNIX) PHP/4.2.2 FooBar/1.2b */
2561 SrvTk_PRODUCT_ONLY /* eg: Apache */
2563 static enum server_token_type ap_server_tokens = SrvTk_FULL;
2565 static apr_status_t reset_version(void *dummy)
2568 ap_server_tokens = SrvTk_FULL;
2569 server_version = NULL;
2573 AP_DECLARE(void) ap_get_server_revision(ap_version_t *version)
2575 version->major = AP_SERVER_MAJORVERSION_NUMBER;
2576 version->minor = AP_SERVER_MINORVERSION_NUMBER;
2577 version->patch = AP_SERVER_PATCHLEVEL_NUMBER;
2578 version->add_string = AP_SERVER_ADD_STRING;
2581 AP_DECLARE(const char *) ap_get_server_version(void)
2583 return (server_version ? server_version : AP_SERVER_BASEVERSION);
2586 AP_DECLARE(void) ap_add_version_component(apr_pool_t *pconf, const char *component)
2588 if (! version_locked) {
2590 * If the version string is null, register our cleanup to reset the
2591 * pointer on pool destruction. We also know that, if NULL,
2592 * we are adding the original SERVER_BASEVERSION string.
2594 if (server_version == NULL) {
2595 apr_pool_cleanup_register(pconf, NULL, reset_version,
2596 apr_pool_cleanup_null);
2597 server_version = apr_pstrdup(pconf, component);
2601 * Tack the given component identifier to the end of
2602 * the existing string.
2604 server_version = apr_pstrcat(pconf, server_version, " ",
2611 * This routine adds the real server base identity to the version string,
2612 * and then locks out changes until the next reconfig.
2614 static void ap_set_version(apr_pool_t *pconf)
2616 if (ap_server_tokens == SrvTk_PRODUCT_ONLY) {
2617 ap_add_version_component(pconf, AP_SERVER_BASEPRODUCT);
2619 else if (ap_server_tokens == SrvTk_MINIMAL) {
2620 ap_add_version_component(pconf, AP_SERVER_BASEVERSION);
2622 else if (ap_server_tokens == SrvTk_MINOR) {
2623 ap_add_version_component(pconf, AP_SERVER_BASEPRODUCT "/" AP_SERVER_MINORREVISION);
2625 else if (ap_server_tokens == SrvTk_MAJOR) {
2626 ap_add_version_component(pconf, AP_SERVER_BASEPRODUCT "/" AP_SERVER_MAJORVERSION);
2629 ap_add_version_component(pconf, AP_SERVER_BASEVERSION " (" PLATFORM ")");
2633 * Lock the server_version string if we're not displaying
2634 * the full set of tokens
2636 if (ap_server_tokens != SrvTk_FULL) {
2641 static const char *set_serv_tokens(cmd_parms *cmd, void *dummy,
2644 const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
2650 if (!strcasecmp(arg, "OS")) {
2651 ap_server_tokens = SrvTk_OS;
2653 else if (!strcasecmp(arg, "Min") || !strcasecmp(arg, "Minimal")) {
2654 ap_server_tokens = SrvTk_MINIMAL;
2656 else if (!strcasecmp(arg, "Major")) {
2657 ap_server_tokens = SrvTk_MAJOR;
2659 else if (!strcasecmp(arg, "Minor") ) {
2660 ap_server_tokens = SrvTk_MINOR;
2662 else if (!strcasecmp(arg, "Prod") || !strcasecmp(arg, "ProductOnly")) {
2663 ap_server_tokens = SrvTk_PRODUCT_ONLY;
2666 ap_server_tokens = SrvTk_FULL;
2672 static const char *set_limit_req_line(cmd_parms *cmd, void *dummy,
2675 const char *err = ap_check_cmd_context(cmd,
2676 NOT_IN_DIR_LOC_FILE|NOT_IN_LIMIT);
2685 return apr_pstrcat(cmd->temp_pool, "LimitRequestLine \"", arg,
2686 "\" must be a non-negative integer", NULL);
2689 cmd->server->limit_req_line = lim;
2693 static const char *set_limit_req_fieldsize(cmd_parms *cmd, void *dummy,
2696 const char *err = ap_check_cmd_context(cmd,
2697 NOT_IN_DIR_LOC_FILE|NOT_IN_LIMIT);
2706 return apr_pstrcat(cmd->temp_pool, "LimitRequestFieldsize \"", arg,
2707 "\" must be a non-negative integer (0 = no limit)",
2711 if (lim > DEFAULT_LIMIT_REQUEST_FIELDSIZE) {
2712 return apr_psprintf(cmd->temp_pool, "LimitRequestFieldsize \"%s\" "
2713 "must not exceed the precompiled maximum of %d",
2714 arg, DEFAULT_LIMIT_REQUEST_FIELDSIZE);
2717 cmd->server->limit_req_fieldsize = lim;
2721 static const char *set_limit_req_fields(cmd_parms *cmd, void *dummy,
2724 const char *err = ap_check_cmd_context(cmd,
2725 NOT_IN_DIR_LOC_FILE|NOT_IN_LIMIT);
2734 return apr_pstrcat(cmd->temp_pool, "LimitRequestFields \"", arg,
2735 "\" must be a non-negative integer (0 = no limit)",
2739 cmd->server->limit_req_fields = lim;
2743 static const char *set_limit_req_body(cmd_parms *cmd, void *conf_,
2746 core_dir_config *conf = conf_;
2747 const char *err = ap_check_cmd_context(cmd, NOT_IN_LIMIT);
2754 if (APR_SUCCESS != apr_strtoff(&conf->limit_req_body, arg, &errp, 10)) {
2755 return "LimitRequestBody argument is not parsable.";
2757 if (*errp || conf->limit_req_body < 0) {
2758 return "LimitRequestBody requires a non-negative integer.";
2764 static const char *set_limit_xml_req_body(cmd_parms *cmd, void *conf_,
2767 core_dir_config *conf = conf_;
2768 const char *err = ap_check_cmd_context(cmd, NOT_IN_LIMIT);
2774 conf->limit_xml_body = atol(arg);
2775 if (conf->limit_xml_body < 0)
2776 return "LimitXMLRequestBody requires a non-negative integer.";
2781 AP_DECLARE(size_t) ap_get_limit_xml_body(const request_rec *r)
2783 core_dir_config *conf;
2785 conf = ap_get_module_config(r->per_dir_config, &core_module);
2786 if (conf->limit_xml_body == AP_LIMIT_UNSET)
2787 return AP_DEFAULT_LIMIT_XML_BODY;
2789 return (size_t)conf->limit_xml_body;
2792 #if !defined (RLIMIT_CPU) || !(defined (RLIMIT_DATA) || defined (RLIMIT_VMEM) || defined(RLIMIT_AS)) || !defined (RLIMIT_NPROC)
2793 static const char *no_set_limit(cmd_parms *cmd, void *conf_,
2794 const char *arg, const char *arg2)
2796 ap_log_error(APLOG_MARK, APLOG_ERR, 0, cmd->server,
2797 "%s not supported on this platform", cmd->cmd->name);
2804 static const char *set_limit_cpu(cmd_parms *cmd, void *conf_,
2805 const char *arg, const char *arg2)
2807 core_dir_config *conf = conf_;
2809 unixd_set_rlimit(cmd, &conf->limit_cpu, arg, arg2, RLIMIT_CPU);
2814 #if defined (RLIMIT_DATA) || defined (RLIMIT_VMEM) || defined(RLIMIT_AS)
2815 static const char *set_limit_mem(cmd_parms *cmd, void *conf_,
2816 const char *arg, const char * arg2)
2818 core_dir_config *conf = conf_;
2820 #if defined(RLIMIT_AS)
2821 unixd_set_rlimit(cmd, &conf->limit_mem, arg, arg2 ,RLIMIT_AS);
2822 #elif defined(RLIMIT_DATA)
2823 unixd_set_rlimit(cmd, &conf->limit_mem, arg, arg2, RLIMIT_DATA);
2824 #elif defined(RLIMIT_VMEM)
2825 unixd_set_rlimit(cmd, &conf->limit_mem, arg, arg2, RLIMIT_VMEM);
2833 static const char *set_limit_nproc(cmd_parms *cmd, void *conf_,
2834 const char *arg, const char * arg2)
2836 core_dir_config *conf = conf_;
2838 unixd_set_rlimit(cmd, &conf->limit_nproc, arg, arg2, RLIMIT_NPROC);
2843 static const char *set_recursion_limit(cmd_parms *cmd, void *dummy,
2844 const char *arg1, const char *arg2)
2846 core_server_config *conf = ap_get_module_config(cmd->server->module_config,
2848 int limit = atoi(arg1);
2851 return "The recursion limit must be greater than zero.";
2854 ap_log_error(APLOG_MARK, APLOG_WARNING, 0, cmd->server,
2855 "Limiting internal redirects to very low numbers may "
2856 "cause normal requests to fail.");
2859 conf->redirect_limit = limit;
2865 return "The recursion limit must be greater than zero.";
2868 ap_log_error(APLOG_MARK, APLOG_WARNING, 0, cmd->server,
2869 "Limiting the subrequest depth to a very low level may"
2870 " cause normal requests to fail.");
2874 conf->subreq_limit = limit;
2879 static void log_backtrace(const request_rec *r)
2881 const request_rec *top = r;
2883 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
2884 "r->uri = %s", r->uri ? r->uri : "(unexpectedly NULL)");
2886 while (top && (top->prev || top->main)) {
2889 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
2890 "redirected from r->uri = %s",
2891 top->uri ? top->uri : "(unexpectedly NULL)");
2894 if (!top->prev && top->main) {
2896 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
2897 "subrequested from r->uri = %s",
2898 top->uri ? top->uri : "(unexpectedly NULL)");
2904 * check whether redirect limit is reached
2906 AP_DECLARE(int) ap_is_recursion_limit_exceeded(const request_rec *r)
2908 core_server_config *conf = ap_get_module_config(r->server->module_config,
2910 const request_rec *top = r;
2911 int redirects = 0, subreqs = 0;
2912 int rlimit = conf->redirect_limit
2913 ? conf->redirect_limit
2914 : AP_DEFAULT_MAX_INTERNAL_REDIRECTS;
2915 int slimit = conf->subreq_limit
2916 ? conf->subreq_limit
2917 : AP_DEFAULT_MAX_SUBREQ_DEPTH;
2920 while (top->prev || top->main) {
2922 if (++redirects >= rlimit) {
2923 /* uuh, too much. */
2924 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
2925 "Request exceeded the limit of %d internal "
2926 "redirects due to probable configuration error. "
2927 "Use 'LimitInternalRecursion' to increase the "
2928 "limit if necessary. Use 'LogLevel debug' to get "
2929 "a backtrace.", rlimit);
2931 /* post backtrace */
2934 /* return failure */
2941 if (!top->prev && top->main) {
2942 if (++subreqs >= slimit) {
2943 /* uuh, too much. */
2944 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
2945 "Request exceeded the limit of %d subrequest "
2946 "nesting levels due to probable confguration "
2947 "error. Use 'LimitInternalRecursion' to increase "
2948 "the limit if necessary. Use 'LogLevel debug' to "
2949 "get a backtrace.", slimit);
2951 /* post backtrace */
2954 /* return failure */
2962 /* recursion state: ok */
2966 static const char *add_ct_output_filters(cmd_parms *cmd, void *conf_,
2967 const char *arg, const char *arg2)
2969 core_dir_config *conf = conf_;
2970 ap_filter_rec_t *old, *new = NULL;
2971 const char *filter_name;
2973 if (!conf->ct_output_filters) {
2974 conf->ct_output_filters = apr_hash_make(cmd->pool);
2978 old = (ap_filter_rec_t*) apr_hash_get(conf->ct_output_filters, arg2,
2979 APR_HASH_KEY_STRING);
2980 /* find last entry */
2989 (filter_name = ap_getword(cmd->pool, &arg, ';')) &&
2990 strcmp(filter_name, "")) {
2991 new = apr_pcalloc(cmd->pool, sizeof(ap_filter_rec_t));
2992 new->name = filter_name;
2994 /* We found something, so let's append it. */
2999 apr_hash_set(conf->ct_output_filters, arg2,
3000 APR_HASH_KEY_STRING, new);
3006 return "invalid filter name";
3012 * Insert filters requested by the AddOutputFilterByType
3013 * configuration directive. We cannot add filters based
3014 * on content-type until after the handler has started
3015 * to run. Only then do we reliably know the content-type.
3017 void ap_add_output_filters_by_type(request_rec *r)
3019 core_dir_config *conf;
3022 conf = (core_dir_config *)ap_get_module_config(r->per_dir_config,
3025 /* We can't do anything with proxy requests, no content-types or if
3026 * we don't have a filter configured.
3028 if (r->proxyreq != PROXYREQ_NONE || !r->content_type ||
3029 !conf->ct_output_filters) {
3033 /* remove c-t decoration */
3034 ctype = ap_field_noparam(r->pool, r->content_type);
3036 ap_filter_rec_t *ct_filter;
3037 ct_filter = apr_hash_get(conf->ct_output_filters, ctype,
3038 APR_HASH_KEY_STRING);
3040 ap_add_output_filter(ct_filter->name, NULL, r, r->connection);
3041 ct_filter = ct_filter->next;
3048 /* Note --- ErrorDocument will now work from .htaccess files.
3049 * The AllowOverride of Fileinfo allows webmasters to turn it off
3052 static const command_rec core_cmds[] = {
3054 /* Old access config file commands */
3056 AP_INIT_RAW_ARGS("<Directory", dirsection, NULL, RSRC_CONF,
3057 "Container for directives affecting resources located in the specified "
3059 AP_INIT_RAW_ARGS("<Location", urlsection, NULL, RSRC_CONF,
3060 "Container for directives affecting resources accessed through the "
3061 "specified URL paths"),
3062 AP_INIT_RAW_ARGS("<VirtualHost", virtualhost_section, NULL, RSRC_CONF,
3063 "Container to map directives to a particular virtual host, takes one or "
3064 "more host addresses"),
3065 AP_INIT_RAW_ARGS("<Files", filesection, NULL, OR_ALL,
3066 "Container for directives affecting files matching specified patterns"),
3067 AP_INIT_RAW_ARGS("<Limit", ap_limit_section, NULL, OR_ALL,
3068 "Container for authentication directives when accessed using specified HTTP "
3070 AP_INIT_RAW_ARGS("<LimitExcept", ap_limit_section, (void*)1, OR_ALL,
3071 "Container for authentication directives to be applied when any HTTP "
3072 "method other than those specified is used to access the resource"),
3073 AP_INIT_TAKE1("<IfModule", start_ifmod, NULL, EXEC_ON_READ | OR_ALL,
3074 "Container for directives based on existance of specified modules"),
3075 AP_INIT_TAKE1("<IfDefine", start_ifdefine, NULL, EXEC_ON_READ | OR_ALL,
3076 "Container for directives based on existance of command line defines"),
3077 AP_INIT_RAW_ARGS("<DirectoryMatch", dirsection, (void*)1, RSRC_CONF,
3078 "Container for directives affecting resources located in the "
3079 "specified directories"),
3080 AP_INIT_RAW_ARGS("<LocationMatch", urlsection, (void*)1, RSRC_CONF,
3081 "Container for directives affecting resources accessed through the "
3082 "specified URL paths"),
3083 AP_INIT_RAW_ARGS("<FilesMatch", filesection, (void*)1, OR_ALL,
3084 "Container for directives affecting files matching specified patterns"),
3085 AP_INIT_TAKE1("AuthType", ap_set_string_slot,
3086 (void*)APR_OFFSETOF(core_dir_config, ap_auth_type), OR_AUTHCFG,
3087 "An HTTP authorization type (e.g., \"Basic\")"),
3088 AP_INIT_TAKE1("AuthName", set_authname, NULL, OR_AUTHCFG,
3089 "The authentication realm (e.g. \"Members Only\")"),
3090 AP_INIT_RAW_ARGS("Require", require, NULL, OR_AUTHCFG,
3091 "Selects which authenticated users or groups may access a protected space"),
3092 AP_INIT_TAKE1("Satisfy", satisfy, NULL, OR_AUTHCFG,
3093 "access policy if both allow and require used ('all' or 'any')"),
3095 AP_INIT_TAKE1("GprofDir", set_gprof_dir, NULL, RSRC_CONF,
3096 "Directory to plop gmon.out files"),
3098 AP_INIT_TAKE1("AddDefaultCharset", set_add_default_charset, NULL, OR_FILEINFO,
3099 "The name of the default charset to add to any Content-Type without one or 'Off' to disable"),
3100 AP_INIT_TAKE1("AcceptPathInfo", set_accept_path_info, NULL, OR_FILEINFO,
3101 "Set to on or off for PATH_INFO to be accepted by handlers, or default for the per-handler preference"),
3103 /* Old resource config file commands */
3105 AP_INIT_RAW_ARGS("AccessFileName", set_access_name, NULL, RSRC_CONF,
3106 "Name(s) of per-directory config files (default: .htaccess)"),
3107 AP_INIT_TAKE1("DocumentRoot", set_document_root, NULL, RSRC_CONF,
3108 "Root directory of the document tree"),
3109 AP_INIT_TAKE2("ErrorDocument", set_error_document, NULL, OR_FILEINFO,
3110 "Change responses for HTTP errors"),
3111 AP_INIT_RAW_ARGS("AllowOverride", set_override, NULL, ACCESS_CONF,
3112 "Controls what groups of directives can be configured by per-directory "
3114 AP_INIT_RAW_ARGS("Options", set_options, NULL, OR_OPTIONS,
3115 "Set a number of attributes for a given directory"),
3116 AP_INIT_TAKE1("DefaultType", ap_set_string_slot,
3117 (void*)APR_OFFSETOF(core_dir_config, ap_default_type),
3118 OR_FILEINFO, "the default MIME type for untypable files"),
3119 AP_INIT_RAW_ARGS("FileETag", set_etag_bits, NULL, OR_FILEINFO,
3120 "Specify components used to construct a file's ETag"),
3121 AP_INIT_TAKE1("EnableMMAP", set_enable_mmap, NULL, OR_FILEINFO,
3122 "Controls whether memory-mapping may be used to read files"),
3123 AP_INIT_TAKE1("EnableSendfile", set_enable_sendfile, NULL, OR_FILEINFO,
3124 "Controls whether sendfile may be used to transmit files"),
3126 /* Old server config file commands */
3128 AP_INIT_TAKE1("Port", ap_set_deprecated, NULL, RSRC_CONF,
3129 "Port was replaced with Listen in Apache 2.0"),
3130 AP_INIT_TAKE1("HostnameLookups", set_hostname_lookups, NULL,
3131 ACCESS_CONF|RSRC_CONF,
3132 "\"on\" to enable, \"off\" to disable reverse DNS lookups, or \"double\" to "
3133 "enable double-reverse DNS lookups"),
3134 AP_INIT_TAKE1("ServerAdmin", set_server_string_slot,
3135 (void *)APR_OFFSETOF(server_rec, server_admin), RSRC_CONF,
3136 "The email address of the server administrator"),
3137 AP_INIT_TAKE1("ServerName", server_hostname_port, NULL, RSRC_CONF,
3138 "The hostname and port of the server"),
3139 AP_INIT_TAKE1("ServerSignature", set_signature_flag, NULL, OR_ALL,
3140 "En-/disable server signature (on|off|email)"),
3141 AP_INIT_TAKE1("ServerRoot", set_server_root, NULL, RSRC_CONF | EXEC_ON_READ,
3142 "Common directory of server-related files (logs, confs, etc.)"),
3143 AP_INIT_TAKE1("ErrorLog", set_server_string_slot,
3144 (void *)APR_OFFSETOF(server_rec, error_fname), RSRC_CONF,
3145 "The filename of the error log"),
3146 AP_INIT_RAW_ARGS("ServerAlias", set_server_alias, NULL, RSRC_CONF,
3147 "A name or names alternately used to access the server"),
3148 AP_INIT_TAKE1("ServerPath", set_serverpath, NULL, RSRC_CONF,
3149 "The pathname the server can be reached at"),
3150 AP_INIT_TAKE1("Timeout", set_timeout, NULL, RSRC_CONF,
3151 "Timeout duration (sec)"),
3152 AP_INIT_FLAG("ContentDigest", set_content_md5, NULL, OR_OPTIONS,
3153 "whether or not to send a Content-MD5 header with each request"),
3154 AP_INIT_TAKE1("UseCanonicalName", set_use_canonical_name, NULL,
3155 RSRC_CONF|ACCESS_CONF,
3156 "How to work out the ServerName : Port when constructing URLs"),
3157 /* TODO: RlimitFoo should all be part of mod_cgi, not in the core */
3158 /* TODO: ListenBacklog in MPM */
3159 AP_INIT_TAKE1("Include", include_config, NULL,
3160 (RSRC_CONF | ACCESS_CONF | EXEC_ON_READ),
3161 "Name of the config file to be included"),
3162 AP_INIT_TAKE1("LogLevel", set_loglevel, NULL, RSRC_CONF,
3163 "Level of verbosity in error logging"),
3164 AP_INIT_TAKE1("NameVirtualHost", ap_set_name_virtual_host, NULL, RSRC_CONF,
3165 "A numeric IP address:port, or the name of a host"),
3166 AP_INIT_TAKE1("ServerTokens", set_serv_tokens, NULL, RSRC_CONF,
3167 "Determine tokens displayed in the Server: header - Min(imal), OS or Full"),
3168 AP_INIT_TAKE1("LimitRequestLine", set_limit_req_line, NULL, RSRC_CONF,
3169 "Limit on maximum size of an HTTP request line"),
3170 AP_INIT_TAKE1("LimitRequestFieldsize", set_limit_req_fieldsize, NULL,
3172 "Limit on maximum size of an HTTP request header field"),
3173 AP_INIT_TAKE1("LimitRequestFields", set_limit_req_fields, NULL, RSRC_CONF,
3174 "Limit (0 = unlimited) on max number of header fields in a request message"),
3175 AP_INIT_TAKE1("LimitRequestBody", set_limit_req_body,
3176 (void*)APR_OFFSETOF(core_dir_config, limit_req_body), OR_ALL,
3177 "Limit (in bytes) on maximum size of request message body"),
3178 AP_INIT_TAKE1("LimitXMLRequestBody", set_limit_xml_req_body, NULL, OR_ALL,
3179 "Limit (in bytes) on maximum size of an XML-based request "
3182 /* System Resource Controls */
3184 AP_INIT_TAKE12("RLimitCPU", set_limit_cpu,
3185 (void*)APR_OFFSETOF(core_dir_config, limit_cpu),
3186 OR_ALL, "Soft/hard limits for max CPU usage in seconds"),
3188 AP_INIT_TAKE12("RLimitCPU", no_set_limit, NULL,
3189 OR_ALL, "Soft/hard limits for max CPU usage in seconds"),
3191 #if defined (RLIMIT_DATA) || defined (RLIMIT_VMEM) || defined (RLIMIT_AS)
3192 AP_INIT_TAKE12("RLimitMEM", set_limit_mem,
3193 (void*)APR_OFFSETOF(core_dir_config, limit_mem),
3194 OR_ALL, "Soft/hard limits for max memory usage per process"),
3196 AP_INIT_TAKE12("RLimitMEM", no_set_limit, NULL,
3197 OR_ALL, "Soft/hard limits for max memory usage per process"),
3200 AP_INIT_TAKE12("RLimitNPROC", set_limit_nproc,
3201 (void*)APR_OFFSETOF(core_dir_config, limit_nproc),
3202 OR_ALL, "soft/hard limits for max number of processes per uid"),
3204 AP_INIT_TAKE12("RLimitNPROC", no_set_limit, NULL,
3205 OR_ALL, "soft/hard limits for max number of processes per uid"),
3208 /* internal recursion stopper */
3209 AP_INIT_TAKE12("LimitInternalRecursion", set_recursion_limit, NULL, RSRC_CONF,
3210 "maximum recursion depth of internal redirects and subrequests"),
3212 AP_INIT_TAKE1("ForceType", ap_set_string_slot_lower,
3213 (void *)APR_OFFSETOF(core_dir_config, mime_type), OR_FILEINFO,
3214 "a mime type that overrides other configured type"),
3215 AP_INIT_TAKE1("SetHandler", ap_set_string_slot_lower,
3216 (void *)APR_OFFSETOF(core_dir_config, handler), OR_FILEINFO,
3217 "a handler name that overrides any other configured handler"),
3218 AP_INIT_TAKE1("SetOutputFilter", ap_set_string_slot,
3219 (void *)APR_OFFSETOF(core_dir_config, output_filters), OR_FILEINFO,
3220 "filter (or ; delimited list of filters) to be run on the request content"),
3221 AP_INIT_TAKE1("SetInputFilter", ap_set_string_slot,
3222 (void *)APR_OFFSETOF(core_dir_config, input_filters), OR_FILEINFO,
3223 "filter (or ; delimited list of filters) to be run on the request body"),
3224 AP_INIT_ITERATE2("AddOutputFilterByType", add_ct_output_filters,
3225 (void *)APR_OFFSETOF(core_dir_config, ct_output_filters), OR_FILEINFO,
3226 "output filter name followed by one or more content-types"),
3227 AP_INIT_FLAG("AllowEncodedSlashes", set_allow2f, NULL, RSRC_CONF,
3228 "Allow URLs containing '/' encoded as '%2F'"),
3231 * These are default configuration directives that mpms can/should
3232 * pay attention to. If an mpm wishes to use these, they should
3233 * #defined them in mpm.h.
3235 #ifdef AP_MPM_WANT_SET_PIDFILE
3236 AP_INIT_TAKE1("PidFile", ap_mpm_set_pidfile, NULL, RSRC_CONF,
3237 "A file for logging the server process ID"),
3239 #ifdef AP_MPM_WANT_SET_SCOREBOARD
3240 AP_INIT_TAKE1("ScoreBoardFile", ap_mpm_set_scoreboard, NULL, RSRC_CONF,
3241 "A file for Apache to maintain runtime process management information"),
3243 #ifdef AP_MPM_WANT_SET_LOCKFILE
3244 AP_INIT_TAKE1("LockFile", ap_mpm_set_lockfile, NULL, RSRC_CONF,
3245 "The lockfile used when Apache needs to lock the accept() call"),
3247 #ifdef AP_MPM_WANT_SET_MAX_REQUESTS
3248 AP_INIT_TAKE1("MaxRequestsPerChild", ap_mpm_set_max_requests, NULL, RSRC_CONF,
3249 "Maximum number of requests a particular child serves before dying."),
3251 #ifdef AP_MPM_WANT_SET_COREDUMPDIR
3252 AP_INIT_TAKE1("CoreDumpDirectory", ap_mpm_set_coredumpdir, NULL, RSRC_CONF,
3253 "The location of the directory Apache changes to before dumping core"),
3255 #ifdef AP_MPM_WANT_SET_ACCEPT_LOCK_MECH
3256 AP_INIT_TAKE1("AcceptMutex", ap_mpm_set_accept_lock_mech, NULL, RSRC_CONF,
3257 ap_valid_accept_mutex_string),
3259 #ifdef AP_MPM_WANT_SET_MAX_MEM_FREE
3260 AP_INIT_TAKE1("MaxMemFree", ap_mpm_set_max_mem_free, NULL, RSRC_CONF,
3261 "Maximum number of 1k blocks a particular childs allocator may hold."),
3263 #ifdef AP_MPM_WANT_SET_STACKSIZE
3264 AP_INIT_TAKE1("ThreadStackSize", ap_mpm_set_thread_stacksize, NULL, RSRC_CONF,
3265 "Size in bytes of stack used by threads handling client connections"),
3267 #if AP_ENABLE_EXCEPTION_HOOK
3268 AP_INIT_TAKE1("EnableExceptionHook", ap_mpm_set_exception_hook, NULL, RSRC_CONF,
3269 "Controls whether exception hook may be called after a crash"),
3274 /*****************************************************************
3276 * Core handlers for various phases of server operation...
3279 AP_DECLARE_NONSTD(int) ap_core_translate(request_rec *r)
3281 void *sconf = r->server->module_config;
3282 core_server_config *conf = ap_get_module_config(sconf, &core_module);
3285 /* XXX this seems too specific, this should probably become
3286 * some general-case test
3289 return HTTP_FORBIDDEN;
3291 if (!r->uri || ((r->uri[0] != '/') && strcmp(r->uri, "*"))) {
3292 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
3293 "Invalid URI in request %s", r->the_request);
3294 return HTTP_BAD_REQUEST;
3298 && !strncmp(r->uri, r->server->path, r->server->pathlen)
3299 && (r->server->path[r->server->pathlen - 1] == '/'
3300 || r->uri[r->server->pathlen] == '/'
3301 || r->uri[r->server->pathlen] == '\0'))
3303 /* skip all leading /'s (e.g. http://localhost///foo)
3304 * so we are looking at only the relative path.
3306 char *path = r->uri + r->server->pathlen;
3307 while (*path == '/') {
3310 if ((rv = apr_filepath_merge(&r->filename, conf->ap_document_root, path,
3311 APR_FILEPATH_TRUENAME
3312 | APR_FILEPATH_SECUREROOT, r->pool))
3314 ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
3315 "Cannot map %s to file", r->the_request);
3316 return HTTP_FORBIDDEN;
3318 r->canonical_filename = r->filename;
3322 * Make sure that we do not mess up the translation by adding two
3323 * /'s in a row. This happens under windows when the document
3324 * root ends with a /
3326 /* skip all leading /'s (e.g. http://localhost///foo)
3327 * so we are looking at only the relative path.
3329 char *path = r->uri;
3330 while (*path == '/') {
3333 if ((rv = apr_filepath_merge(&r->filename, conf->ap_document_root, path,
3334 APR_FILEPATH_TRUENAME
3335 | APR_FILEPATH_SECUREROOT, r->pool))
3337 ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
3338 "Cannot map %s to file", r->the_request);
3339 return HTTP_FORBIDDEN;
3341 r->canonical_filename = r->filename;
3347 /*****************************************************************
3349 * Test the filesystem name through directory_walk and file_walk
3351 static int core_map_to_storage(request_rec *r)
3355 if ((access_status = ap_directory_walk(r))) {
3356 return access_status;
3359 if ((access_status = ap_file_walk(r))) {
3360 return access_status;
3367 static int do_nothing(request_rec *r) { return OK; }
3370 static int core_override_type(request_rec *r)
3372 core_dir_config *conf =
3373 (core_dir_config *)ap_get_module_config(r->per_dir_config,
3376 /* Check for overrides with ForceType / SetHandler
3378 if (conf->mime_type && strcmp(conf->mime_type, "none"))
3379 ap_set_content_type(r, (char*) conf->mime_type);
3381 if (conf->handler && strcmp(conf->handler, "none"))
3382 r->handler = conf->handler;
3384 /* Deal with the poor soul who is trying to force path_info to be
3385 * accepted within the core_handler, where they will let the subreq
3386 * address its contents. This is toggled by the user in the very
3387 * beginning of the fixup phase, so modules should override the user's
3388 * discretion in their own module fixup phase. It is tristate, if
3389 * the user doesn't specify, the result is 2 (which the module may
3390 * interpret to its own customary behavior.) It won't be touched
3391 * if the value is no longer undefined (2), so any module changing
3392 * the value prior to the fixup phase OVERRIDES the user's choice.
3394 if ((r->used_path_info == AP_REQ_DEFAULT_PATH_INFO)
3395 && (conf->accept_path_info != 3)) {
3396 r->used_path_info = conf->accept_path_info;
3402 static int default_handler(request_rec *r)
3404 conn_rec *c = r->connection;
3405 apr_bucket_brigade *bb;
3409 apr_file_t *fd = NULL;
3410 apr_status_t status;
3411 /* XXX if/when somebody writes a content-md5 filter we either need to
3412 * remove this support or coordinate when to use the filter vs.
3413 * when to use this code
3414 * The current choice of when to compute the md5 here matches the 1.3
3415 * support fairly closely (unlike 1.3, we don't handle computing md5
3416 * when the charset is translated).
3418 int bld_content_md5;
3420 d = (core_dir_config *)ap_get_module_config(r->per_dir_config,
3422 bld_content_md5 = (d->content_md5 & 1)
3423 && r->output_filters->frec->ftype != AP_FTYPE_RESOURCE;
3425 ap_allow_standard_methods(r, MERGE_ALLOW, M_GET, M_OPTIONS, M_POST, -1);
3427 /* If filters intend to consume the request body, they must
3428 * register an InputFilter to slurp the contents of the POST
3429 * data from the POST input stream. It no longer exists when
3430 * the output filters are invoked by the default handler.
3432 if ((errstatus = ap_discard_request_body(r)) != OK) {
3436 if (r->method_number == M_GET || r->method_number == M_POST) {
3437 if (r->finfo.filetype == 0) {
3438 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
3439 "File does not exist: %s", r->filename);
3440 return HTTP_NOT_FOUND;
3443 /* Don't try to serve a dir. Some OSs do weird things with
3446 if (r->finfo.filetype == APR_DIR) {
3447 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
3448 "Attempt to serve directory: %s", r->filename);
3449 return HTTP_NOT_FOUND;
3452 if ((r->used_path_info != AP_REQ_ACCEPT_PATH_INFO) &&
3453 r->path_info && *r->path_info)
3455 /* default to reject */
3456 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
3457 "File does not exist: %s",
3458 apr_pstrcat(r->pool, r->filename, r->path_info, NULL));
3459 return HTTP_NOT_FOUND;
3462 /* We understood the (non-GET) method, but it might not be legal for
3463 this particular resource. Check to see if the 'deliver_script'
3464 flag is set. If so, then we go ahead and deliver the file since
3465 it isn't really content (only GET normally returns content).
3467 Note: based on logic further above, the only possible non-GET
3468 method at this point is POST. In the future, we should enable
3469 script delivery for all methods. */
3470 if (r->method_number != M_GET) {
3471 core_request_config *req_cfg;
3473 req_cfg = ap_get_module_config(r->request_config, &core_module);
3474 if (!req_cfg->deliver_script) {
3475 /* The flag hasn't been set for this request. Punt. */
3476 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
3477 "This resource does not accept the %s method.",
3479 return HTTP_METHOD_NOT_ALLOWED;
3484 if ((status = apr_file_open(&fd, r->filename, APR_READ | APR_BINARY
3485 #if APR_HAS_SENDFILE
3486 | ((d->enable_sendfile == ENABLE_SENDFILE_OFF)
3487 ? 0 : APR_SENDFILE_ENABLED)
3489 , 0, r->pool)) != APR_SUCCESS) {
3490 ap_log_rerror(APLOG_MARK, APLOG_ERR, status, r,
3491 "file permissions deny server access: %s", r->filename);
3492 return HTTP_FORBIDDEN;
3495 ap_update_mtime(r, r->finfo.mtime);
3496 ap_set_last_modified(r);
3498 apr_table_setn(r->headers_out, "Accept-Ranges", "bytes");
3499 ap_set_content_length(r, r->finfo.size);
3500 if ((errstatus = ap_meets_conditions(r)) != OK) {
3505 if (bld_content_md5) {
3506 apr_table_setn(r->headers_out, "Content-MD5",
3507 ap_md5digest(r->pool, fd));
3510 bb = apr_brigade_create(r->pool, c->bucket_alloc);
3512 /* For platforms where the size of the file may be larger than
3513 * that which can be stored in a single bucket (where the
3514 * length field is an apr_size_t), split it into several
3516 if (sizeof(apr_off_t) > sizeof(apr_size_t)
3517 && r->finfo.size > AP_MAX_SENDFILE) {
3518 apr_off_t fsize = r->finfo.size;
3519 e = apr_bucket_file_create(fd, 0, AP_MAX_SENDFILE, r->pool,
3521 while (fsize > AP_MAX_SENDFILE) {
3523 apr_bucket_copy(e, &ce);
3524 APR_BRIGADE_INSERT_TAIL(bb, ce);
3525 e->start += AP_MAX_SENDFILE;
3526 fsize -= AP_MAX_SENDFILE;
3528 e->length = (apr_size_t)fsize; /* Resize just the last bucket */
3531 e = apr_bucket_file_create(fd, 0, (apr_size_t)r->finfo.size,
3532 r->pool, c->bucket_alloc);
3535 if (d->enable_mmap == ENABLE_MMAP_OFF) {
3536 (void)apr_bucket_file_enable_mmap(e, 0);
3539 APR_BRIGADE_INSERT_TAIL(bb, e);
3540 e = apr_bucket_eos_create(c->bucket_alloc);
3541 APR_BRIGADE_INSERT_TAIL(bb, e);
3543 return ap_pass_brigade(r->output_filters, bb);
3545 else { /* unusual method (not GET or POST) */
3546 if (r->method_number == M_INVALID) {
3547 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
3548 "Invalid method in request %s", r->the_request);
3549 return HTTP_NOT_IMPLEMENTED;
3552 if (r->method_number == M_OPTIONS) {
3553 return ap_send_http_options(r);
3555 return HTTP_METHOD_NOT_ALLOWED;
3559 /* Optional function coming from mod_logio, used for logging of output
3562 APR_OPTIONAL_FN_TYPE(ap_logio_add_bytes_out) *logio_add_bytes_out;
3564 static int core_post_config(apr_pool_t *pconf, apr_pool_t *plog, apr_pool_t *ptemp, server_rec *s)
3566 logio_add_bytes_out = APR_RETRIEVE_OPTIONAL_FN(ap_logio_add_bytes_out);
3567 ident_lookup = APR_RETRIEVE_OPTIONAL_FN(ap_ident_lookup);
3569 ap_set_version(pconf);
3570 ap_setup_make_content_type(pconf);
3574 static void core_insert_filter(request_rec *r)
3576 core_dir_config *conf = (core_dir_config *)
3577 ap_get_module_config(r->per_dir_config,
3579 const char *filter, *filters = conf->output_filters;
3582 while (*filters && (filter = ap_getword(r->pool, &filters, ';'))) {
3583 ap_add_output_filter(filter, NULL, r, r->connection);
3587 filters = conf->input_filters;
3589 while (*filters && (filter = ap_getword(r->pool, &filters, ';'))) {
3590 ap_add_input_filter(filter, NULL, r, r->connection);
3595 static apr_size_t num_request_notes = AP_NUM_STD_NOTES;
3597 static apr_status_t reset_request_notes(void *dummy)
3599 num_request_notes = AP_NUM_STD_NOTES;
3603 AP_DECLARE(apr_size_t) ap_register_request_note(void)
3605 apr_pool_cleanup_register(apr_hook_global_pool, NULL, reset_request_notes,
3606 apr_pool_cleanup_null);
3607 return num_request_notes++;
3610 AP_DECLARE(void **) ap_get_request_note(request_rec *r, apr_size_t note_num)
3612 core_request_config *req_cfg;
3614 if (note_num >= num_request_notes) {
3618 req_cfg = (core_request_config *)
3619 ap_get_module_config(r->request_config, &core_module);
3625 return &(req_cfg->notes[note_num]);
3628 static int core_create_req(request_rec *r)
3630 /* Alloc the config struct and the array of request notes in
3631 * a single block for efficiency
3633 core_request_config *req_cfg;
3635 req_cfg = apr_pcalloc(r->pool, sizeof(core_request_config) +
3636 sizeof(void *) * num_request_notes);
3637 req_cfg->notes = (void **)((char *)req_cfg + sizeof(core_request_config));
3639 /* ### temporarily enable script delivery as the default */
3640 req_cfg->deliver_script = 1;
3643 core_request_config *main_req_cfg = (core_request_config *)
3644 ap_get_module_config(r->main->request_config, &core_module);
3645 req_cfg->bb = main_req_cfg->bb;
3648 req_cfg->bb = apr_brigade_create(r->pool, r->connection->bucket_alloc);
3650 ap_add_input_filter_handle(ap_net_time_filter_handle,
3651 NULL, r, r->connection);
3655 ap_set_module_config(r->request_config, &core_module, req_cfg);
3657 /* Begin by presuming any module can make its own path_info assumptions,
3658 * until some module interjects and changes the value.
3660 r->used_path_info = AP_REQ_DEFAULT_PATH_INFO;
3665 static int core_create_proxy_req(request_rec *r, request_rec *pr)
3667 return core_create_req(pr);
3670 static conn_rec *core_create_conn(apr_pool_t *ptrans, server_rec *server,
3671 apr_socket_t *csd, long id, void *sbh,
3672 apr_bucket_alloc_t *alloc)
3675 conn_rec *c = (conn_rec *) apr_pcalloc(ptrans, sizeof(conn_rec));
3678 (void)ap_update_child_status(c->sbh, SERVER_BUSY_READ, (request_rec *)NULL);
3680 /* Got a connection structure, so initialize what fields we can
3681 * (the rest are zeroed out by pcalloc).
3683 c->conn_config = ap_create_conn_config(ptrans);
3684 c->notes = apr_table_make(ptrans, 5);
3687 if ((rv = apr_socket_addr_get(&c->local_addr, APR_LOCAL, csd))
3689 ap_log_error(APLOG_MARK, APLOG_INFO, rv, server,
3690 "apr_socket_addr_get(APR_LOCAL)");
3691 apr_socket_close(csd);
3695 apr_sockaddr_ip_get(&c->local_ip, c->local_addr);
3696 if ((rv = apr_socket_addr_get(&c->remote_addr, APR_REMOTE, csd))
3698 ap_log_error(APLOG_MARK, APLOG_INFO, rv, server,
3699 "apr_socket_addr_get(APR_REMOTE)");
3700 apr_socket_close(csd);
3704 apr_sockaddr_ip_get(&c->remote_ip, c->remote_addr);
3705 c->base_server = server;
3708 c->bucket_alloc = alloc;
3713 static int core_pre_connection(conn_rec *c, void *csd)
3715 core_net_rec *net = apr_palloc(c->pool, sizeof(*net));
3717 #ifdef AP_MPM_DISABLE_NAGLE_ACCEPTED_SOCK
3720 /* The Nagle algorithm says that we should delay sending partial
3721 * packets in hopes of getting more data. We don't want to do
3722 * this; we are not telnet. There are bad interactions between
3723 * persistent connections and Nagle's algorithm that have very severe
3724 * performance penalties. (Failing to disable Nagle is not much of a
3725 * problem with simple HTTP.)
3727 rv = apr_socket_opt_set(csd, APR_TCP_NODELAY, 1);
3728 if (rv != APR_SUCCESS && rv != APR_ENOTIMPL) {
3729 /* expected cause is that the client disconnected already,
3730 * hence the debug level
3732 ap_log_cerror(APLOG_MARK, APLOG_DEBUG, rv, c,
3733 "apr_socket_opt_set(APR_TCP_NODELAY)");
3738 net->out_ctx = NULL;
3739 net->client_socket = csd;
3741 ap_set_module_config(net->c->conn_config, &core_module, csd);
3742 ap_add_input_filter_handle(ap_core_input_filter_handle, net, NULL, net->c);
3743 ap_add_output_filter_handle(ap_core_output_filter_handle, net, NULL, net->c);
3747 static void register_hooks(apr_pool_t *p)
3749 /* create_connection and install_transport_filters are
3750 * hooks that should always be APR_HOOK_REALLY_LAST to give other
3751 * modules the opportunity to install alternate network transports
3752 * and stop other functions from being run.
3754 ap_hook_create_connection(core_create_conn, NULL, NULL,
3755 APR_HOOK_REALLY_LAST);
3756 ap_hook_pre_connection(core_pre_connection, NULL, NULL,
3757 APR_HOOK_REALLY_LAST);
3759 ap_hook_post_config(core_post_config,NULL,NULL,APR_HOOK_REALLY_FIRST);
3760 ap_hook_translate_name(ap_core_translate,NULL,NULL,APR_HOOK_REALLY_LAST);
3761 ap_hook_map_to_storage(core_map_to_storage,NULL,NULL,APR_HOOK_REALLY_LAST);
3762 ap_hook_open_logs(ap_open_logs,NULL,NULL,APR_HOOK_REALLY_FIRST);
3763 ap_hook_handler(default_handler,NULL,NULL,APR_HOOK_REALLY_LAST);
3764 /* FIXME: I suspect we can eliminate the need for these do_nothings - Ben */
3765 ap_hook_type_checker(do_nothing,NULL,NULL,APR_HOOK_REALLY_LAST);
3766 ap_hook_fixups(core_override_type,NULL,NULL,APR_HOOK_REALLY_FIRST);
3767 ap_hook_access_checker(do_nothing,NULL,NULL,APR_HOOK_REALLY_LAST);
3768 ap_hook_create_request(core_create_req, NULL, NULL, APR_HOOK_MIDDLE);
3769 APR_OPTIONAL_HOOK(proxy, create_req, core_create_proxy_req, NULL, NULL,
3771 ap_hook_pre_mpm(ap_create_scoreboard, NULL, NULL, APR_HOOK_MIDDLE);
3773 /* register the core's insert_filter hook and register core-provided
3776 ap_hook_insert_filter(core_insert_filter, NULL, NULL, APR_HOOK_MIDDLE);
3778 ap_core_input_filter_handle =
3779 ap_register_input_filter("CORE_IN", ap_core_input_filter,
3780 NULL, AP_FTYPE_NETWORK);
3781 ap_net_time_filter_handle =
3782 ap_register_input_filter("NET_TIME", ap_net_time_filter,
3783 NULL, AP_FTYPE_PROTOCOL);
3784 ap_content_length_filter_handle =
3785 ap_register_output_filter("CONTENT_LENGTH", ap_content_length_filter,
3786 NULL, AP_FTYPE_PROTOCOL);
3787 ap_core_output_filter_handle =
3788 ap_register_output_filter("CORE", ap_core_output_filter,
3789 NULL, AP_FTYPE_NETWORK);
3790 ap_subreq_core_filter_handle =
3791 ap_register_output_filter("SUBREQ_CORE", ap_sub_req_output_filter,
3792 NULL, AP_FTYPE_CONTENT_SET);
3794 ap_register_output_filter("OLD_WRITE", ap_old_write_filter,
3795 NULL, AP_FTYPE_RESOURCE - 10);
3798 AP_DECLARE_DATA module core_module = {
3799 STANDARD20_MODULE_STUFF,
3800 create_core_dir_config, /* create per-directory config structure */
3801 merge_core_dir_configs, /* merge per-directory config structures */
3802 create_core_server_config, /* create per-server config structure */
3803 merge_core_server_configs, /* merge per-server config structures */
3804 core_cmds, /* command apr_table_t */
3805 register_hooks /* register hooks */