1 /* Licensed to the Apache Software Foundation (ASF) under one or more
2 * contributor license agreements. See the NOTICE file distributed with
3 * this work for additional information regarding copyright ownership.
4 * The ASF licenses this file to You under the Apache License, Version 2.0
5 * (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 #include "apr_strings.h"
20 #include "apr_fnmatch.h"
22 #include "apr_thread_proc.h" /* for RLIMIT stuff */
23 #include "apr_hooks.h"
25 #define APR_WANT_IOVEC
26 #define APR_WANT_STRFUNC
27 #define APR_WANT_MEMFUNC
31 #include "ap_config.h"
33 #include "http_config.h"
34 #include "http_core.h"
35 #include "http_protocol.h" /* For index_of_response(). Grump. */
36 #include "http_request.h"
37 #include "http_vhost.h"
38 #include "http_main.h" /* For the default_handler below... */
41 #include "http_connection.h"
42 #include "apr_buckets.h"
43 #include "util_filter.h"
44 #include "util_ebcdic.h"
45 #include "util_mutex.h"
47 #include "mpm_common.h"
48 #include "scoreboard.h"
50 #include "mod_proxy.h"
51 #include "ap_listen.h"
53 #include "mod_so.h" /* for ap_find_loaded_module_symbol */
55 /* LimitRequestBody handling */
56 #define AP_LIMIT_REQ_BODY_UNSET ((apr_off_t) -1)
57 #define AP_DEFAULT_LIMIT_REQ_BODY ((apr_off_t) 0)
59 /* LimitXMLRequestBody handling */
60 #define AP_LIMIT_UNSET ((long) -1)
61 #define AP_DEFAULT_LIMIT_XML_BODY ((size_t)1000000)
63 #define AP_MIN_SENDFILE_BYTES (256)
65 /* maximum include nesting level */
66 #ifndef AP_MAX_INCLUDE_DEPTH
67 #define AP_MAX_INCLUDE_DEPTH (128)
71 APR_HOOK_LINK(get_mgmt_items)
74 AP_IMPLEMENT_HOOK_RUN_ALL(int, get_mgmt_items,
75 (apr_pool_t *p, const char *val, apr_hash_t *ht),
76 (p, val, ht), OK, DECLINED)
78 /* Server core module... This module provides support for really basic
79 * server operations, including options and commands which control the
80 * operation of other modules. Consider this the bureaucracy module.
82 * The core module also defines handlers, etc., do handle just enough
83 * to allow a server with the core module ONLY to actually serve documents
84 * (though it slaps DefaultType on all of 'em); this was useful in testing,
85 * but may not be worth preserving.
87 * This file could almost be mod_core.c, except for the stuff which affects
88 * the http_conf_globals.
91 /* Handles for core filters */
92 AP_DECLARE_DATA ap_filter_rec_t *ap_subreq_core_filter_handle;
93 AP_DECLARE_DATA ap_filter_rec_t *ap_core_output_filter_handle;
94 AP_DECLARE_DATA ap_filter_rec_t *ap_content_length_filter_handle;
95 AP_DECLARE_DATA ap_filter_rec_t *ap_core_input_filter_handle;
97 /* magic pointer for ErrorDocument xxx "default" */
98 static char errordocument_default;
100 static void *create_core_dir_config(apr_pool_t *a, char *dir)
102 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;
118 conf->use_canonical_phys_port = USE_CANONICAL_PHYS_PORT_UNSET;
120 conf->hostname_lookups = HOSTNAME_LOOKUP_UNSET;
123 conf->limit_cpu = NULL;
125 #if defined(RLIMIT_DATA) || defined(RLIMIT_VMEM) || defined(RLIMIT_AS)
126 conf->limit_mem = NULL;
129 conf->limit_nproc = NULL;
132 conf->limit_req_body = AP_LIMIT_REQ_BODY_UNSET;
133 conf->limit_xml_body = AP_LIMIT_UNSET;
134 conf->sec_file = apr_array_make(a, 2, sizeof(ap_conf_vector_t *));
136 conf->server_signature = srv_sig_unset;
138 conf->add_default_charset = ADD_DEFAULT_CHARSET_UNSET;
139 conf->add_default_charset_name = DEFAULT_ADD_DEFAULT_CHARSET_NAME;
141 /* Overriding all negotiation
143 conf->mime_type = NULL;
144 conf->handler = NULL;
145 conf->output_filters = NULL;
146 conf->input_filters = NULL;
149 * Flag for use of inodes in ETags.
151 conf->etag_bits = ETAG_UNSET;
152 conf->etag_add = ETAG_UNSET;
153 conf->etag_remove = ETAG_UNSET;
155 conf->enable_mmap = ENABLE_MMAP_UNSET;
156 conf->enable_sendfile = ENABLE_SENDFILE_UNSET;
157 conf->allow_encoded_slashes = 0;
163 * Overlay one hash table of ct_output_filters onto another
165 static void *merge_ct_filters(apr_pool_t *p,
168 const void *overlay_val,
169 const void *base_val,
172 ap_filter_rec_t *cur;
173 const ap_filter_rec_t *overlay_info = (const ap_filter_rec_t *)overlay_val;
174 const ap_filter_rec_t *base_info = (const ap_filter_rec_t *)base_val;
178 while (overlay_info) {
179 ap_filter_rec_t *new;
181 new = apr_pcalloc(p, sizeof(ap_filter_rec_t));
182 new->name = apr_pstrdup(p, overlay_info->name);
185 overlay_info = overlay_info->next;
192 /* We can't have dups. */
195 if (!strcasecmp(base_info->name, f->name)) {
204 f = apr_pcalloc(p, sizeof(ap_filter_rec_t));
205 f->name = apr_pstrdup(p, base_info->name);
210 base_info = base_info->next;
216 static void *merge_core_dir_configs(apr_pool_t *a, void *basev, void *newv)
218 core_dir_config *base = (core_dir_config *)basev;
219 core_dir_config *new = (core_dir_config *)newv;
220 core_dir_config *conf;
223 /* Create this conf by duplicating the base, replacing elements
224 * (or creating copies for merging) where new-> values exist.
226 conf = (core_dir_config *)apr_palloc(a, sizeof(core_dir_config));
227 memcpy(conf, base, sizeof(core_dir_config));
230 conf->d_is_fnmatch = new->d_is_fnmatch;
231 conf->d_components = new->d_components;
234 if (new->opts & OPT_UNSET) {
235 /* there was no explicit setting of new->opts, so we merge
236 * preserve the invariant (opts_add & opts_remove) == 0
238 conf->opts_add = (conf->opts_add & ~new->opts_remove) | new->opts_add;
239 conf->opts_remove = (conf->opts_remove & ~new->opts_add)
241 conf->opts = (conf->opts & ~conf->opts_remove) | conf->opts_add;
242 if ((base->opts & OPT_INCNOEXEC) && (new->opts & OPT_INCLUDES)) {
243 conf->opts = (conf->opts & ~OPT_INCNOEXEC) | OPT_INCLUDES;
247 /* otherwise we just copy, because an explicit opts setting
248 * overrides all earlier +/- modifiers
250 conf->opts = new->opts;
251 conf->opts_add = new->opts_add;
252 conf->opts_remove = new->opts_remove;
255 if (!(new->override & OR_UNSET)) {
256 conf->override = new->override;
259 if (!(new->override_opts & OPT_UNSET)) {
260 conf->override_opts = new->override_opts;
263 if (new->ap_default_type) {
264 conf->ap_default_type = new->ap_default_type;
267 if (conf->response_code_strings == NULL) {
268 conf->response_code_strings = new->response_code_strings;
270 else if (new->response_code_strings != NULL) {
271 /* If we merge, the merge-result must have it's own array
273 conf->response_code_strings = apr_palloc(a,
274 sizeof(*conf->response_code_strings) * RESPONSE_CODES);
275 memcpy(conf->response_code_strings, base->response_code_strings,
276 sizeof(*conf->response_code_strings) * RESPONSE_CODES);
278 for (i = 0; i < RESPONSE_CODES; ++i) {
279 if (new->response_code_strings[i] != NULL) {
280 conf->response_code_strings[i] = new->response_code_strings[i];
284 /* Otherwise we simply use the base->response_code_strings array
287 if (new->hostname_lookups != HOSTNAME_LOOKUP_UNSET) {
288 conf->hostname_lookups = new->hostname_lookups;
291 if ((new->content_md5 & 2) == 0) {
292 conf->content_md5 = new->content_md5;
295 if (new->accept_path_info != 3) {
296 conf->accept_path_info = new->accept_path_info;
299 if (new->use_canonical_name != USE_CANONICAL_NAME_UNSET) {
300 conf->use_canonical_name = new->use_canonical_name;
303 if (new->use_canonical_phys_port != USE_CANONICAL_PHYS_PORT_UNSET) {
304 conf->use_canonical_phys_port = new->use_canonical_phys_port;
308 if (new->limit_cpu) {
309 conf->limit_cpu = new->limit_cpu;
313 #if defined(RLIMIT_DATA) || defined(RLIMIT_VMEM) || defined(RLIMIT_AS)
314 if (new->limit_mem) {
315 conf->limit_mem = new->limit_mem;
320 if (new->limit_nproc) {
321 conf->limit_nproc = new->limit_nproc;
325 if (new->limit_req_body != AP_LIMIT_REQ_BODY_UNSET) {
326 conf->limit_req_body = new->limit_req_body;
329 if (new->limit_xml_body != AP_LIMIT_UNSET)
330 conf->limit_xml_body = new->limit_xml_body;
332 conf->limit_xml_body = base->limit_xml_body;
334 if (!conf->sec_file) {
335 conf->sec_file = new->sec_file;
337 else if (new->sec_file) {
338 /* If we merge, the merge-result must have it's own array
340 conf->sec_file = apr_array_append(a, base->sec_file, new->sec_file);
342 /* Otherwise we simply use the base->sec_file array
345 if (new->server_signature != srv_sig_unset) {
346 conf->server_signature = new->server_signature;
349 if (new->add_default_charset != ADD_DEFAULT_CHARSET_UNSET) {
350 conf->add_default_charset = new->add_default_charset;
351 conf->add_default_charset_name = new->add_default_charset_name;
354 /* Overriding all negotiation
356 if (new->mime_type) {
357 conf->mime_type = new->mime_type;
361 conf->handler = new->handler;
364 if (new->output_filters) {
365 conf->output_filters = new->output_filters;
368 if (new->input_filters) {
369 conf->input_filters = new->input_filters;
372 if (conf->ct_output_filters && new->ct_output_filters) {
373 conf->ct_output_filters = apr_hash_merge(a,
374 new->ct_output_filters,
375 conf->ct_output_filters,
379 else if (new->ct_output_filters) {
380 conf->ct_output_filters = apr_hash_copy(a, new->ct_output_filters);
382 else if (conf->ct_output_filters) {
383 /* That memcpy above isn't enough. */
384 conf->ct_output_filters = apr_hash_copy(a, base->ct_output_filters);
388 * Now merge the setting of the FileETag directive.
390 if (new->etag_bits == ETAG_UNSET) {
392 (conf->etag_add & (~ new->etag_remove)) | new->etag_add;
394 (conf->opts_remove & (~ new->etag_add)) | new->etag_remove;
396 (conf->etag_bits & (~ conf->etag_remove)) | conf->etag_add;
399 conf->etag_bits = new->etag_bits;
400 conf->etag_add = new->etag_add;
401 conf->etag_remove = new->etag_remove;
404 if (conf->etag_bits != ETAG_NONE) {
405 conf->etag_bits &= (~ ETAG_NONE);
408 if (new->enable_mmap != ENABLE_MMAP_UNSET) {
409 conf->enable_mmap = new->enable_mmap;
412 if (new->enable_sendfile != ENABLE_SENDFILE_UNSET) {
413 conf->enable_sendfile = new->enable_sendfile;
416 conf->allow_encoded_slashes = new->allow_encoded_slashes;
421 static void *create_core_server_config(apr_pool_t *a, server_rec *s)
423 core_server_config *conf;
424 int is_virtual = s->is_virtual;
426 conf = (core_server_config *)apr_pcalloc(a, sizeof(core_server_config));
429 conf->gprof_dir = NULL;
432 conf->access_name = is_virtual ? NULL : DEFAULT_ACCESS_FNAME;
433 conf->ap_document_root = is_virtual ? NULL : DOCUMENT_LOCATION;
434 conf->sec_dir = apr_array_make(a, 40, sizeof(ap_conf_vector_t *));
435 conf->sec_url = apr_array_make(a, 40, sizeof(ap_conf_vector_t *));
437 /* recursion stopper */
438 conf->redirect_limit = 0; /* 0 == unset */
439 conf->subreq_limit = 0;
441 conf->protocol = NULL;
442 conf->accf_map = apr_table_make(a, 5);
444 #ifdef APR_TCP_DEFER_ACCEPT
445 apr_table_set(conf->accf_map, "http", "data");
446 apr_table_set(conf->accf_map, "https", "data");
449 #if APR_HAS_SO_ACCEPTFILTER
450 #ifndef ACCEPT_FILTER_NAME
451 #define ACCEPT_FILTER_NAME "httpready"
452 #ifdef __FreeBSD_version
453 #if __FreeBSD_version < 411000 /* httpready broken before 4.1.1 */
454 #undef ACCEPT_FILTER_NAME
455 #define ACCEPT_FILTER_NAME "dataready"
459 apr_table_set(conf->accf_map, "http", ACCEPT_FILTER_NAME);
460 apr_table_set(conf->accf_map, "https", "dataready");
463 conf->trace_enable = AP_TRACE_UNSET;
468 static void *merge_core_server_configs(apr_pool_t *p, void *basev, void *virtv)
470 core_server_config *base = (core_server_config *)basev;
471 core_server_config *virt = (core_server_config *)virtv;
472 core_server_config *conf;
474 conf = (core_server_config *)apr_palloc(p, sizeof(core_server_config));
475 memcpy(conf, virt, sizeof(core_server_config));
477 if (!conf->access_name) {
478 conf->access_name = base->access_name;
481 if (!conf->ap_document_root) {
482 conf->ap_document_root = base->ap_document_root;
485 if (!conf->protocol) {
486 conf->protocol = base->protocol;
489 conf->sec_dir = apr_array_append(p, base->sec_dir, virt->sec_dir);
490 conf->sec_url = apr_array_append(p, base->sec_url, virt->sec_url);
492 conf->redirect_limit = virt->redirect_limit
493 ? virt->redirect_limit
494 : base->redirect_limit;
496 conf->subreq_limit = virt->subreq_limit
498 : base->subreq_limit;
500 conf->trace_enable = (virt->trace_enable != AP_TRACE_UNSET)
502 : base->trace_enable;
507 /* Add per-directory configuration entry (for <directory> section);
508 * these are part of the core server config.
511 AP_CORE_DECLARE(void) ap_add_per_dir_conf(server_rec *s, void *dir_config)
513 core_server_config *sconf = ap_get_module_config(s->module_config,
515 void **new_space = (void **)apr_array_push(sconf->sec_dir);
517 *new_space = dir_config;
520 AP_CORE_DECLARE(void) ap_add_per_url_conf(server_rec *s, void *url_config)
522 core_server_config *sconf = ap_get_module_config(s->module_config,
524 void **new_space = (void **)apr_array_push(sconf->sec_url);
526 *new_space = url_config;
529 AP_CORE_DECLARE(void) ap_add_file_conf(core_dir_config *conf, void *url_config)
531 void **new_space = (void **)apr_array_push(conf->sec_file);
533 *new_space = url_config;
536 /* We need to do a stable sort, qsort isn't stable. So to make it stable
537 * we'll be maintaining the original index into the list, and using it
538 * as the minor key during sorting. The major key is the number of
539 * components (where the root component is zero).
541 struct reorder_sort_rec {
542 ap_conf_vector_t *elt;
546 static int reorder_sorter(const void *va, const void *vb)
548 const struct reorder_sort_rec *a = va;
549 const struct reorder_sort_rec *b = vb;
550 core_dir_config *core_a;
551 core_dir_config *core_b;
553 core_a = ap_get_module_config(a->elt, &core_module);
554 core_b = ap_get_module_config(b->elt, &core_module);
556 /* a regex always sorts after a non-regex
558 if (!core_a->r && core_b->r) {
561 else if (core_a->r && !core_b->r) {
565 /* we always sort next by the number of components
567 if (core_a->d_components < core_b->d_components) {
570 else if (core_a->d_components > core_b->d_components) {
574 /* They have the same number of components, we now have to compare
575 * the minor key to maintain the original order (from the config.)
577 return a->orig_index - b->orig_index;
580 void ap_core_reorder_directories(apr_pool_t *p, server_rec *s)
582 core_server_config *sconf;
583 apr_array_header_t *sec_dir;
584 struct reorder_sort_rec *sortbin;
586 ap_conf_vector_t **elts;
590 sconf = ap_get_module_config(s->module_config, &core_module);
591 sec_dir = sconf->sec_dir;
592 nelts = sec_dir->nelts;
593 elts = (ap_conf_vector_t **)sec_dir->elts;
596 /* simple case of already being sorted... */
597 /* We're not checking this condition to be fast... we're checking
598 * it to avoid trying to palloc zero bytes, which can trigger some
599 * memory debuggers to barf
604 /* we have to allocate tmp space to do a stable sort */
605 apr_pool_create(&tmp, p);
606 sortbin = apr_palloc(tmp, sec_dir->nelts * sizeof(*sortbin));
607 for (i = 0; i < nelts; ++i) {
608 sortbin[i].orig_index = i;
609 sortbin[i].elt = elts[i];
612 qsort(sortbin, nelts, sizeof(*sortbin), reorder_sorter);
614 /* and now copy back to the original array */
615 for (i = 0; i < nelts; ++i) {
616 elts[i] = sortbin[i].elt;
619 apr_pool_destroy(tmp);
622 /*****************************************************************
624 * There are some elements of the core config structures in which
625 * other modules have a legitimate interest (this is ugly, but necessary
626 * to preserve NCSA back-compatibility). So, we have a bunch of accessors
630 AP_DECLARE(int) ap_allow_options(request_rec *r)
632 core_dir_config *conf =
633 (core_dir_config *)ap_get_module_config(r->per_dir_config, &core_module);
638 AP_DECLARE(int) ap_allow_overrides(request_rec *r)
640 core_dir_config *conf;
641 conf = (core_dir_config *)ap_get_module_config(r->per_dir_config,
644 return conf->override;
648 * Optional function coming from mod_ident, used for looking up ident user
650 static APR_OPTIONAL_FN_TYPE(authn_ap_auth_type) *authn_ap_auth_type;
652 AP_DECLARE(const char *) ap_auth_type(request_rec *r)
654 if (authn_ap_auth_type) {
655 return authn_ap_auth_type(r);
661 * Optional function coming from mod_ident, used for looking up ident user
663 static APR_OPTIONAL_FN_TYPE(authn_ap_auth_name) *authn_ap_auth_name;
665 AP_DECLARE(const char *) ap_auth_name(request_rec *r)
667 if (authn_ap_auth_name) {
668 return authn_ap_auth_name(r);
673 AP_DECLARE(const char *) ap_default_type(request_rec *r)
675 core_dir_config *conf;
677 conf = (core_dir_config *)ap_get_module_config(r->per_dir_config,
680 return conf->ap_default_type
681 ? conf->ap_default_type
682 : DEFAULT_CONTENT_TYPE;
685 AP_DECLARE(const char *) ap_document_root(request_rec *r) /* Don't use this! */
687 core_server_config *conf;
689 conf = (core_server_config *)ap_get_module_config(r->server->module_config,
692 return conf->ap_document_root;
695 /* Should probably just get rid of this... the only code that cares is
696 * part of the core anyway (and in fact, it isn't publicised to other
700 char *ap_response_code_string(request_rec *r, int error_index)
702 core_dir_config *dirconf;
703 core_request_config *reqconf;
705 /* check for string registered via ap_custom_response() first */
706 reqconf = (core_request_config *)ap_get_module_config(r->request_config,
708 if (reqconf->response_code_strings != NULL &&
709 reqconf->response_code_strings[error_index] != NULL) {
710 return reqconf->response_code_strings[error_index];
713 /* check for string specified via ErrorDocument */
714 dirconf = (core_dir_config *)ap_get_module_config(r->per_dir_config,
717 if (dirconf->response_code_strings == NULL) {
721 if (dirconf->response_code_strings[error_index] == &errordocument_default) {
725 return dirconf->response_code_strings[error_index];
729 /* Code from Harald Hanche-Olsen <hanche@imf.unit.no> */
730 static APR_INLINE void do_double_reverse (conn_rec *conn)
735 if (conn->double_reverse) {
740 if (conn->remote_host == NULL || conn->remote_host[0] == '\0') {
741 /* single reverse failed, so don't bother */
742 conn->double_reverse = -1;
746 rv = apr_sockaddr_info_get(&sa, conn->remote_host, APR_UNSPEC, 0, 0, conn->pool);
747 if (rv == APR_SUCCESS) {
749 if (apr_sockaddr_equal(sa, conn->remote_addr)) {
750 conn->double_reverse = 1;
758 conn->double_reverse = -1;
761 AP_DECLARE(const char *) ap_get_remote_host(conn_rec *conn, void *dir_config,
762 int type, int *str_is_ip)
764 int hostname_lookups;
765 int ignored_str_is_ip;
767 if (!str_is_ip) { /* caller doesn't want to know */
768 str_is_ip = &ignored_str_is_ip;
772 /* If we haven't checked the host name, and we want to */
775 ((core_dir_config *)ap_get_module_config(dir_config, &core_module))
778 if (hostname_lookups == HOSTNAME_LOOKUP_UNSET) {
779 hostname_lookups = HOSTNAME_LOOKUP_OFF;
784 hostname_lookups = HOSTNAME_LOOKUP_OFF;
787 if (type != REMOTE_NOLOOKUP
788 && conn->remote_host == NULL
789 && (type == REMOTE_DOUBLE_REV
790 || hostname_lookups != HOSTNAME_LOOKUP_OFF)) {
792 if (apr_getnameinfo(&conn->remote_host, conn->remote_addr, 0)
794 ap_str_tolower(conn->remote_host);
796 if (hostname_lookups == HOSTNAME_LOOKUP_DOUBLE) {
797 do_double_reverse(conn);
798 if (conn->double_reverse != 1) {
799 conn->remote_host = NULL;
804 /* if failed, set it to the NULL string to indicate error */
805 if (conn->remote_host == NULL) {
806 conn->remote_host = "";
810 if (type == REMOTE_DOUBLE_REV) {
811 do_double_reverse(conn);
812 if (conn->double_reverse == -1) {
818 * Return the desired information; either the remote DNS name, if found,
819 * or either NULL (if the hostname was requested) or the IP address
820 * (if any identifier was requested).
822 if (conn->remote_host != NULL && conn->remote_host[0] != '\0') {
823 return conn->remote_host;
826 if (type == REMOTE_HOST || type == REMOTE_DOUBLE_REV) {
831 return conn->remote_ip;
837 * Optional function coming from mod_ident, used for looking up ident user
839 static APR_OPTIONAL_FN_TYPE(ap_ident_lookup) *ident_lookup;
841 AP_DECLARE(const char *) ap_get_remote_logname(request_rec *r)
843 if (r->connection->remote_logname != NULL) {
844 return r->connection->remote_logname;
848 return ident_lookup(r);
854 /* There are two options regarding what the "name" of a server is. The
855 * "canonical" name as defined by ServerName and Port, or the "client's
856 * name" as supplied by a possible Host: header or full URI.
858 * The DNS option to UseCanonicalName causes this routine to do a
859 * reverse lookup on the local IP address of the connection and use
860 * that for the ServerName. This makes its value more reliable while
861 * at the same time allowing Demon's magic virtual hosting to work.
862 * The assumption is that DNS lookups are sufficiently quick...
865 AP_DECLARE(const char *) ap_get_server_name(request_rec *r)
867 conn_rec *conn = r->connection;
871 d = (core_dir_config *)ap_get_module_config(r->per_dir_config,
874 switch (d->use_canonical_name) {
875 case USE_CANONICAL_NAME_ON:
876 retval = r->server->server_hostname;
878 case USE_CANONICAL_NAME_DNS:
879 if (conn->local_host == NULL) {
880 if (apr_getnameinfo(&conn->local_host,
881 conn->local_addr, 0) != APR_SUCCESS)
882 conn->local_host = apr_pstrdup(conn->pool,
883 r->server->server_hostname);
885 ap_str_tolower(conn->local_host);
888 retval = conn->local_host;
890 case USE_CANONICAL_NAME_OFF:
891 case USE_CANONICAL_NAME_UNSET:
892 retval = r->hostname ? r->hostname : r->server->server_hostname;
895 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
896 "ap_get_server_name: Invalid UCN Option somehow");
897 retval = "localhost";
904 * Get the current server name from the request for the purposes
905 * of using in a URL. If the server name is an IPv6 literal
906 * address, it will be returned in URL format (e.g., "[fe80::1]").
908 static const char *get_server_name_for_url(request_rec *r)
910 const char *plain_server_name = ap_get_server_name(r);
913 if (ap_strchr_c(plain_server_name, ':')) { /* IPv6 literal? */
914 return apr_psprintf(r->pool, "[%s]", plain_server_name);
917 return plain_server_name;
920 AP_DECLARE(apr_port_t) ap_get_server_port(const request_rec *r)
924 (core_dir_config *)ap_get_module_config(r->per_dir_config, &core_module);
926 switch (d->use_canonical_name) {
927 case USE_CANONICAL_NAME_OFF:
928 case USE_CANONICAL_NAME_DNS:
929 case USE_CANONICAL_NAME_UNSET:
930 if (d->use_canonical_phys_port == USE_CANONICAL_PHYS_PORT_ON)
931 port = r->parsed_uri.port_str ? r->parsed_uri.port :
932 r->connection->local_addr->port ? r->connection->local_addr->port :
933 r->server->port ? r->server->port :
935 else /* USE_CANONICAL_PHYS_PORT_OFF or USE_CANONICAL_PHYS_PORT_UNSET */
936 port = r->parsed_uri.port_str ? r->parsed_uri.port :
937 r->server->port ? r->server->port :
940 case USE_CANONICAL_NAME_ON:
941 /* With UseCanonicalName on (and in all versions prior to 1.3)
942 * Apache will use the hostname and port specified in the
943 * ServerName directive to construct a canonical name for the
944 * server. (If no port was specified in the ServerName
945 * directive, Apache uses the port supplied by the client if
946 * any is supplied, and finally the default port for the protocol
949 if (d->use_canonical_phys_port == USE_CANONICAL_PHYS_PORT_ON)
950 port = r->server->port ? r->server->port :
951 r->connection->local_addr->port ? r->connection->local_addr->port :
953 else /* USE_CANONICAL_PHYS_PORT_OFF or USE_CANONICAL_PHYS_PORT_UNSET */
954 port = r->server->port ? r->server->port :
958 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
959 "ap_get_server_port: Invalid UCN Option somehow");
960 port = ap_default_port(r);
967 AP_DECLARE(char *) ap_construct_url(apr_pool_t *p, const char *uri,
970 unsigned port = ap_get_server_port(r);
971 const char *host = get_server_name_for_url(r);
973 if (ap_is_default_port(port, r)) {
974 return apr_pstrcat(p, ap_http_scheme(r), "://", host, uri, NULL);
977 return apr_psprintf(p, "%s://%s:%u%s", ap_http_scheme(r), host, port, uri);
980 AP_DECLARE(apr_off_t) ap_get_limit_req_body(const request_rec *r)
983 (core_dir_config *)ap_get_module_config(r->per_dir_config, &core_module);
985 if (d->limit_req_body == AP_LIMIT_REQ_BODY_UNSET) {
986 return AP_DEFAULT_LIMIT_REQ_BODY;
989 return d->limit_req_body;
993 /*****************************************************************
995 * Commands... this module handles almost all of the NCSA httpd.conf
996 * commands, but most of the old srm.conf is in the the modules.
1000 /* returns a parent if it matches the given directive */
1001 static const ap_directive_t * find_parent(const ap_directive_t *dirp,
1004 while (dirp->parent != NULL) {
1005 dirp = dirp->parent;
1007 /* ### it would be nice to have atom-ized directives */
1008 if (strcasecmp(dirp->directive, what) == 0)
1015 AP_DECLARE(const char *) ap_check_cmd_context(cmd_parms *cmd,
1018 const char *gt = (cmd->cmd->name[0] == '<'
1019 && cmd->cmd->name[strlen(cmd->cmd->name)-1] != '>')
1021 const ap_directive_t *found;
1023 if ((forbidden & NOT_IN_VIRTUALHOST) && cmd->server->is_virtual) {
1024 return apr_pstrcat(cmd->pool, cmd->cmd->name, gt,
1025 " cannot occur within <VirtualHost> section", NULL);
1028 if ((forbidden & NOT_IN_LIMIT) && cmd->limited != -1) {
1029 return apr_pstrcat(cmd->pool, cmd->cmd->name, gt,
1030 " cannot occur within <Limit> section", NULL);
1033 if ((forbidden & NOT_IN_DIR_LOC_FILE) == NOT_IN_DIR_LOC_FILE) {
1034 if (cmd->path != NULL) {
1035 return apr_pstrcat(cmd->pool, cmd->cmd->name, gt,
1036 " cannot occur within <Directory/Location/Files> "
1039 if (cmd->cmd->req_override & EXEC_ON_READ) {
1040 /* EXEC_ON_READ must be NOT_IN_DIR_LOC_FILE, if not, it will
1041 * (deliberately) segfault below in the individual tests...
1047 if (((forbidden & NOT_IN_DIRECTORY)
1048 && ((found = find_parent(cmd->directive, "<Directory"))
1049 || (found = find_parent(cmd->directive, "<DirectoryMatch"))))
1050 || ((forbidden & NOT_IN_LOCATION)
1051 && ((found = find_parent(cmd->directive, "<Location"))
1052 || (found = find_parent(cmd->directive, "<LocationMatch"))))
1053 || ((forbidden & NOT_IN_FILES)
1054 && ((found = find_parent(cmd->directive, "<Files"))
1055 || (found = find_parent(cmd->directive, "<FilesMatch"))))) {
1056 return apr_pstrcat(cmd->pool, cmd->cmd->name, gt,
1057 " cannot occur within ", found->directive,
1064 static const char *set_access_name(cmd_parms *cmd, void *dummy,
1067 void *sconf = cmd->server->module_config;
1068 core_server_config *conf = ap_get_module_config(sconf, &core_module);
1070 const char *err = ap_check_cmd_context(cmd,
1071 NOT_IN_DIR_LOC_FILE|NOT_IN_LIMIT);
1076 conf->access_name = apr_pstrdup(cmd->pool, arg);
1081 static const char *set_gprof_dir(cmd_parms *cmd, void *dummy, const char *arg)
1083 void *sconf = cmd->server->module_config;
1084 core_server_config *conf = ap_get_module_config(sconf, &core_module);
1086 const char *err = ap_check_cmd_context(cmd,
1087 NOT_IN_DIR_LOC_FILE|NOT_IN_LIMIT);
1092 conf->gprof_dir = apr_pstrdup(cmd->pool, arg);
1097 static const char *set_add_default_charset(cmd_parms *cmd,
1098 void *d_, const char *arg)
1100 core_dir_config *d = d_;
1102 const char *err = ap_check_cmd_context(cmd, NOT_IN_LIMIT);
1107 if (!strcasecmp(arg, "Off")) {
1108 d->add_default_charset = ADD_DEFAULT_CHARSET_OFF;
1110 else if (!strcasecmp(arg, "On")) {
1111 d->add_default_charset = ADD_DEFAULT_CHARSET_ON;
1112 d->add_default_charset_name = DEFAULT_ADD_DEFAULT_CHARSET_NAME;
1115 d->add_default_charset = ADD_DEFAULT_CHARSET_ON;
1116 d->add_default_charset_name = arg;
1122 static const char *set_document_root(cmd_parms *cmd, void *dummy,
1125 void *sconf = cmd->server->module_config;
1126 core_server_config *conf = ap_get_module_config(sconf, &core_module);
1128 const char *err = ap_check_cmd_context(cmd,
1129 NOT_IN_DIR_LOC_FILE|NOT_IN_LIMIT);
1134 /* Make it absolute, relative to ServerRoot */
1135 arg = ap_server_root_relative(cmd->pool, arg);
1137 /* TODO: ap_configtestonly && ap_docrootcheck && */
1138 if (apr_filepath_merge((char**)&conf->ap_document_root, NULL, arg,
1139 APR_FILEPATH_TRUENAME, cmd->pool) != APR_SUCCESS
1140 || !ap_is_directory(cmd->pool, arg)) {
1141 if (cmd->server->is_virtual) {
1142 ap_log_perror(APLOG_MARK, APLOG_STARTUP, 0,
1144 "Warning: DocumentRoot [%s] does not exist",
1146 conf->ap_document_root = arg;
1149 return "DocumentRoot must be a directory";
1155 AP_DECLARE(void) ap_custom_response(request_rec *r, int status,
1158 core_request_config *conf =
1159 ap_get_module_config(r->request_config, &core_module);
1162 if (conf->response_code_strings == NULL) {
1163 conf->response_code_strings =
1164 apr_pcalloc(r->pool,
1165 sizeof(*conf->response_code_strings) * RESPONSE_CODES);
1168 idx = ap_index_of_response(status);
1170 conf->response_code_strings[idx] =
1171 ((ap_is_url(string) || (*string == '/')) && (*string != '"')) ?
1172 apr_pstrdup(r->pool, string) : apr_pstrcat(r->pool, "\"", string, NULL);
1175 static const char *set_error_document(cmd_parms *cmd, void *conf_,
1176 const char *errno_str, const char *msg)
1178 core_dir_config *conf = conf_;
1179 int error_number, index_number, idx500;
1180 enum { MSG, LOCAL_PATH, REMOTE_PATH } what = MSG;
1182 const char *err = ap_check_cmd_context(cmd, NOT_IN_LIMIT);
1187 /* 1st parameter should be a 3 digit number, which we recognize;
1188 * convert it into an array index
1190 error_number = atoi(errno_str);
1191 idx500 = ap_index_of_response(HTTP_INTERNAL_SERVER_ERROR);
1193 if (error_number == HTTP_INTERNAL_SERVER_ERROR) {
1194 index_number = idx500;
1196 else if ((index_number = ap_index_of_response(error_number)) == idx500) {
1197 return apr_pstrcat(cmd->pool, "Unsupported HTTP response code ",
1201 /* Heuristic to determine second argument. */
1202 if (ap_strchr_c(msg,' '))
1204 else if (msg[0] == '/')
1206 else if (ap_is_url(msg))
1211 /* The entry should be ignored if it is a full URL for a 401 error */
1213 if (error_number == 401 && what == REMOTE_PATH) {
1214 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, cmd->server,
1215 "cannot use a full URL in a 401 ErrorDocument "
1216 "directive --- ignoring!");
1218 else { /* Store it... */
1219 if (conf->response_code_strings == NULL) {
1220 conf->response_code_strings =
1221 apr_pcalloc(cmd->pool,
1222 sizeof(*conf->response_code_strings) *
1226 if (strcmp(msg, "default") == 0) {
1227 /* special case: ErrorDocument 404 default restores the
1228 * canned server error response
1230 conf->response_code_strings[index_number] = &errordocument_default;
1233 /* hack. Prefix a " if it is a msg; as that is what
1234 * http_protocol.c relies on to distinguish between
1235 * a msg and a (local) path.
1237 conf->response_code_strings[index_number] = (what == MSG) ?
1238 apr_pstrcat(cmd->pool, "\"",msg,NULL) :
1239 apr_pstrdup(cmd->pool, msg);
1246 static const char *set_allow_opts(cmd_parms *cmd, allow_options_t *opts,
1249 allow_options_t opt;
1252 char *w, *p = (char *) l;
1255 while ((w = apr_strtok(p, ",", &tok_state)) != NULL) {
1263 if (!strcasecmp(w, "Indexes")) {
1266 else if (!strcasecmp(w, "Includes")) {
1269 else if (!strcasecmp(w, "IncludesNOEXEC")) {
1270 opt = (OPT_INCLUDES | OPT_INCNOEXEC);
1272 else if (!strcasecmp(w, "FollowSymLinks")) {
1273 opt = OPT_SYM_LINKS;
1275 else if (!strcasecmp(w, "SymLinksIfOwnerMatch")) {
1276 opt = OPT_SYM_OWNER;
1278 else if (!strcasecmp(w, "ExecCGI")) {
1281 else if (!strcasecmp(w, "MultiViews")) {
1284 else if (!strcasecmp(w, "RunScripts")) { /* AI backcompat. Yuck */
1285 opt = OPT_MULTI|OPT_EXECCGI;
1287 else if (!strcasecmp(w, "None")) {
1290 else if (!strcasecmp(w, "All")) {
1294 return apr_pstrcat(cmd->pool, "Illegal option ", w, NULL);
1300 (*opts) &= (~OPT_UNSET);
1305 static const char *set_override(cmd_parms *cmd, void *d_, const char *l)
1307 core_dir_config *d = d_;
1311 const char *err = ap_check_cmd_context(cmd, NOT_IN_LIMIT);
1316 /* Throw a warning if we're in <Location> or <Files> */
1317 if (ap_check_cmd_context(cmd, NOT_IN_LOCATION | NOT_IN_FILES)) {
1318 ap_log_error(APLOG_MARK, APLOG_WARNING, 0, cmd->server,
1319 "Useless use of AllowOverride in line %d of %s.",
1320 cmd->directive->line_num, cmd->directive->filename);
1323 d->override = OR_NONE;
1325 w = ap_getword_conf(cmd->pool, &l);
1333 if (!strcasecmp(w, "Limit")) {
1334 d->override |= OR_LIMIT;
1336 else if (!strcasecmp(k, "Options")) {
1337 d->override |= OR_OPTIONS;
1339 set_allow_opts(cmd, &(d->override_opts), v);
1341 d->override_opts = OPT_ALL;
1343 else if (!strcasecmp(w, "FileInfo")) {
1344 d->override |= OR_FILEINFO;
1346 else if (!strcasecmp(w, "AuthConfig")) {
1347 d->override |= OR_AUTHCFG;
1349 else if (!strcasecmp(w, "Indexes")) {
1350 d->override |= OR_INDEXES;
1352 else if (!strcasecmp(w, "None")) {
1353 d->override = OR_NONE;
1355 else if (!strcasecmp(w, "All")) {
1356 d->override = OR_ALL;
1359 return apr_pstrcat(cmd->pool, "Illegal override option ", w, NULL);
1362 d->override &= ~OR_UNSET;
1368 static const char *set_options(cmd_parms *cmd, void *d_, const char *l)
1370 core_dir_config *d = d_;
1371 allow_options_t opt;
1376 char *w = ap_getword_conf(cmd->pool, &l);
1379 if (*w == '+' || *w == '-') {
1387 if (!strcasecmp(w, "Indexes")) {
1390 else if (!strcasecmp(w, "Includes")) {
1393 else if (!strcasecmp(w, "IncludesNOEXEC")) {
1394 opt = (OPT_INCLUDES | OPT_INCNOEXEC);
1396 else if (!strcasecmp(w, "FollowSymLinks")) {
1397 opt = OPT_SYM_LINKS;
1399 else if (!strcasecmp(w, "SymLinksIfOwnerMatch")) {
1400 opt = OPT_SYM_OWNER;
1402 else if (!strcasecmp(w, "ExecCGI")) {
1405 else if (!strcasecmp(w, "MultiViews")) {
1408 else if (!strcasecmp(w, "RunScripts")) { /* AI backcompat. Yuck */
1409 opt = OPT_MULTI|OPT_EXECCGI;
1411 else if (!strcasecmp(w, "None")) {
1414 else if (!strcasecmp(w, "All")) {
1418 return apr_pstrcat(cmd->pool, "Illegal option ", w, NULL);
1421 if (!(cmd->override_opts & opt) && opt != OPT_NONE) {
1422 return apr_pstrcat(cmd->pool, "Option ", w, " not allowed here", NULL);
1424 else if (action == '-') {
1425 /* we ensure the invariant (d->opts_add & d->opts_remove) == 0 */
1426 d->opts_remove |= opt;
1427 d->opts_add &= ~opt;
1430 else if (action == '+') {
1432 d->opts_remove &= ~opt;
1444 * Note what data should be used when forming file ETag values.
1445 * It would be nicer to do this as an ITERATE, but then we couldn't
1446 * remember the +/- state properly.
1448 static const char *set_etag_bits(cmd_parms *cmd, void *mconfig,
1451 core_dir_config *cfg;
1452 etag_components_t bit;
1460 cfg = (core_dir_config *)mconfig;
1465 while (args[0] != '\0') {
1469 token = ap_getword_conf(cmd->pool, &args);
1470 if ((*token == '+') || (*token == '-')) {
1476 * The occurrence of an absolute setting wipes
1477 * out any previous relative ones. The first such
1478 * occurrence forgets any inherited ones, too.
1481 cfg->etag_bits = ETAG_UNSET;
1482 cfg->etag_add = ETAG_UNSET;
1483 cfg->etag_remove = ETAG_UNSET;
1488 if (strcasecmp(token, "None") == 0) {
1489 if (action != '*') {
1493 cfg->etag_bits = bit = ETAG_NONE;
1497 else if (strcasecmp(token, "All") == 0) {
1498 if (action != '*') {
1503 cfg->etag_bits = bit = ETAG_ALL;
1506 else if (strcasecmp(token, "Size") == 0) {
1509 else if ((strcasecmp(token, "LMTime") == 0)
1510 || (strcasecmp(token, "MTime") == 0)
1511 || (strcasecmp(token, "LastModified") == 0)) {
1514 else if (strcasecmp(token, "INode") == 0) {
1518 return apr_pstrcat(cmd->pool, "Unknown keyword '",
1519 token, "' for ", cmd->cmd->name,
1520 " directive", NULL);
1524 return apr_pstrcat(cmd->pool, cmd->cmd->name, " keyword '",
1525 token, "' cannot be used with '+' or '-'",
1529 if (action == '+') {
1531 * Make sure it's in the 'add' list and absent from the
1534 cfg->etag_add |= bit;
1535 cfg->etag_remove &= (~ bit);
1537 else if (action == '-') {
1538 cfg->etag_remove |= bit;
1539 cfg->etag_add &= (~ bit);
1543 * Non-relative values wipe out any + or - values
1544 * accumulated so far.
1546 cfg->etag_bits |= bit;
1547 cfg->etag_add = ETAG_UNSET;
1548 cfg->etag_remove = ETAG_UNSET;
1554 * Any setting at all will clear the 'None' and 'Unset' bits.
1557 if (cfg->etag_add != ETAG_UNSET) {
1558 cfg->etag_add &= (~ ETAG_UNSET);
1561 if (cfg->etag_remove != ETAG_UNSET) {
1562 cfg->etag_remove &= (~ ETAG_UNSET);
1566 cfg->etag_bits &= (~ ETAG_UNSET);
1568 if ((cfg->etag_bits & ETAG_NONE) != ETAG_NONE) {
1569 cfg->etag_bits &= (~ ETAG_NONE);
1576 static const char *set_enable_mmap(cmd_parms *cmd, void *d_,
1579 core_dir_config *d = d_;
1580 const char *err = ap_check_cmd_context(cmd, NOT_IN_LIMIT);
1586 if (strcasecmp(arg, "on") == 0) {
1587 d->enable_mmap = ENABLE_MMAP_ON;
1589 else if (strcasecmp(arg, "off") == 0) {
1590 d->enable_mmap = ENABLE_MMAP_OFF;
1593 return "parameter must be 'on' or 'off'";
1599 static const char *set_enable_sendfile(cmd_parms *cmd, void *d_,
1602 core_dir_config *d = d_;
1603 const char *err = ap_check_cmd_context(cmd, NOT_IN_LIMIT);
1609 if (strcasecmp(arg, "on") == 0) {
1610 d->enable_sendfile = ENABLE_SENDFILE_ON;
1612 else if (strcasecmp(arg, "off") == 0) {
1613 d->enable_sendfile = ENABLE_SENDFILE_OFF;
1616 return "parameter must be 'on' or 'off'";
1624 * Report a missing-'>' syntax error.
1626 static char *unclosed_directive(cmd_parms *cmd)
1628 return apr_pstrcat(cmd->pool, cmd->cmd->name,
1629 "> directive missing closing '>'", NULL);
1633 * Report a missing args in '<Foo >' syntax error.
1635 static char *missing_container_arg(cmd_parms *cmd)
1637 return apr_pstrcat(cmd->pool, cmd->cmd->name,
1638 "> directive requires additional arguments", NULL);
1641 AP_CORE_DECLARE_NONSTD(const char *) ap_limit_section(cmd_parms *cmd,
1645 const char *endp = ap_strrchr_c(arg, '>');
1646 const char *limited_methods;
1647 void *tog = cmd->cmd->cmd_data;
1648 apr_int64_t limited = 0;
1651 const char *err = ap_check_cmd_context(cmd, NOT_IN_LIMIT);
1657 return unclosed_directive(cmd);
1660 limited_methods = apr_pstrndup(cmd->pool, arg, endp - arg);
1662 if (!limited_methods[0]) {
1663 return missing_container_arg(cmd);
1666 while (limited_methods[0]) {
1667 char *method = ap_getword_conf(cmd->pool, &limited_methods);
1670 /* check for builtin or module registered method number */
1671 methnum = ap_method_number_of(method);
1673 if (methnum == M_TRACE && !tog) {
1674 return "TRACE cannot be controlled by <Limit>, see TraceEnable";
1676 else if (methnum == M_INVALID) {
1677 /* method has not been registered yet, but resorce restriction
1678 * is always checked before method handling, so register it.
1680 methnum = ap_method_register(cmd->pool, method);
1683 limited |= (AP_METHOD_BIT << methnum);
1686 /* Killing two features with one function,
1687 * if (tog == NULL) <Limit>, else <LimitExcept>
1689 cmd->limited = tog ? ~limited : limited;
1691 errmsg = ap_walk_config(cmd->directive->first_child, cmd, cmd->context);
1698 /* XXX: Bogus - need to do this differently (at least OS2/Netware suffer
1699 * the same problem!!!
1700 * We use this in <DirectoryMatch> and <FilesMatch>, to ensure that
1701 * people don't get bitten by wrong-cased regex matches
1705 #define USE_ICASE AP_REG_ICASE
1710 static const char *dirsection(cmd_parms *cmd, void *mconfig, const char *arg)
1713 const char *endp = ap_strrchr_c(arg, '>');
1714 int old_overrides = cmd->override;
1715 char *old_path = cmd->path;
1716 core_dir_config *conf;
1717 ap_conf_vector_t *new_dir_conf = ap_create_per_dir_config(cmd->pool);
1718 ap_regex_t *r = NULL;
1719 const command_rec *thiscmd = cmd->cmd;
1721 const char *err = ap_check_cmd_context(cmd,
1722 NOT_IN_DIR_LOC_FILE|NOT_IN_LIMIT);
1728 return unclosed_directive(cmd);
1731 arg = apr_pstrndup(cmd->pool, arg, endp - arg);
1734 return missing_container_arg(cmd);
1738 if (thiscmd->cmd_data)
1739 return "<DirectoryMatch > block must specify a path";
1741 return "<Directory > block must specify a path";
1744 cmd->path = ap_getword_conf(cmd->pool, &arg);
1745 cmd->override = OR_ALL|ACCESS_CONF;
1747 if (!strcmp(cmd->path, "~")) {
1748 cmd->path = ap_getword_conf(cmd->pool, &arg);
1750 return "<Directory ~ > block must specify a path";
1751 r = ap_pregcomp(cmd->pool, cmd->path, AP_REG_EXTENDED|USE_ICASE);
1753 return "Regex could not be compiled";
1756 else if (thiscmd->cmd_data) { /* <DirectoryMatch> */
1757 r = ap_pregcomp(cmd->pool, cmd->path, AP_REG_EXTENDED|USE_ICASE);
1759 return "Regex could not be compiled";
1762 else if (!strcmp(cmd->path, "/") == 0)
1767 * Ensure that the pathname is canonical, and append the trailing /
1769 apr_status_t rv = apr_filepath_merge(&newpath, NULL, cmd->path,
1770 APR_FILEPATH_TRUENAME, cmd->pool);
1771 if (rv != APR_SUCCESS && rv != APR_EPATHWILD) {
1772 return apr_pstrcat(cmd->pool, "<Directory \"", cmd->path,
1773 "\"> path is invalid.", NULL);
1776 cmd->path = newpath;
1777 if (cmd->path[strlen(cmd->path) - 1] != '/')
1778 cmd->path = apr_pstrcat(cmd->pool, cmd->path, "/", NULL);
1781 /* initialize our config and fetch it */
1782 conf = ap_set_config_vectors(cmd->server, new_dir_conf, cmd->path,
1783 &core_module, cmd->pool);
1785 errmsg = ap_walk_config(cmd->directive->first_child, cmd, new_dir_conf);
1790 conf->d = cmd->path;
1791 conf->d_is_fnmatch = (apr_fnmatch_test(conf->d) != 0);
1793 /* Make this explicit - the "/" root has 0 elements, that is, we
1794 * will always merge it, and it will always sort and merge first.
1795 * All others are sorted and tested by the number of slashes.
1797 if (strcmp(conf->d, "/") == 0)
1798 conf->d_components = 0;
1800 conf->d_components = ap_count_dirs(conf->d);
1802 ap_add_per_dir_conf(cmd->server, new_dir_conf);
1805 return apr_pstrcat(cmd->pool, "Multiple ", thiscmd->name,
1806 "> arguments not (yet) supported.", NULL);
1809 cmd->path = old_path;
1810 cmd->override = old_overrides;
1815 static const char *urlsection(cmd_parms *cmd, void *mconfig, const char *arg)
1818 const char *endp = ap_strrchr_c(arg, '>');
1819 int old_overrides = cmd->override;
1820 char *old_path = cmd->path;
1821 core_dir_config *conf;
1822 ap_regex_t *r = NULL;
1823 const command_rec *thiscmd = cmd->cmd;
1824 ap_conf_vector_t *new_url_conf = ap_create_per_dir_config(cmd->pool);
1825 const char *err = ap_check_cmd_context(cmd,
1826 NOT_IN_DIR_LOC_FILE|NOT_IN_LIMIT);
1832 return unclosed_directive(cmd);
1835 arg = apr_pstrndup(cmd->pool, arg, endp - arg);
1838 return missing_container_arg(cmd);
1841 cmd->path = ap_getword_conf(cmd->pool, &arg);
1842 cmd->override = OR_ALL|ACCESS_CONF;
1844 if (thiscmd->cmd_data) { /* <LocationMatch> */
1845 r = ap_pregcomp(cmd->pool, cmd->path, AP_REG_EXTENDED);
1847 return "Regex could not be compiled";
1850 else if (!strcmp(cmd->path, "~")) {
1851 cmd->path = ap_getword_conf(cmd->pool, &arg);
1852 r = ap_pregcomp(cmd->pool, cmd->path, AP_REG_EXTENDED);
1854 return "Regex could not be compiled";
1858 /* initialize our config and fetch it */
1859 conf = ap_set_config_vectors(cmd->server, new_url_conf, cmd->path,
1860 &core_module, cmd->pool);
1862 errmsg = ap_walk_config(cmd->directive->first_child, cmd, new_url_conf);
1866 conf->d = apr_pstrdup(cmd->pool, cmd->path); /* No mangling, please */
1867 conf->d_is_fnmatch = apr_fnmatch_test(conf->d) != 0;
1870 ap_add_per_url_conf(cmd->server, new_url_conf);
1873 return apr_pstrcat(cmd->pool, "Multiple ", thiscmd->name,
1874 "> arguments not (yet) supported.", NULL);
1877 cmd->path = old_path;
1878 cmd->override = old_overrides;
1883 static const char *filesection(cmd_parms *cmd, void *mconfig, const char *arg)
1886 const char *endp = ap_strrchr_c(arg, '>');
1887 int old_overrides = cmd->override;
1888 char *old_path = cmd->path;
1889 core_dir_config *conf;
1890 ap_regex_t *r = NULL;
1891 const command_rec *thiscmd = cmd->cmd;
1892 core_dir_config *c = mconfig;
1893 ap_conf_vector_t *new_file_conf = ap_create_per_dir_config(cmd->pool);
1894 const char *err = ap_check_cmd_context(cmd, NOT_IN_LIMIT|NOT_IN_LOCATION);
1901 return unclosed_directive(cmd);
1904 arg = apr_pstrndup(cmd->pool, arg, endp - arg);
1907 return missing_container_arg(cmd);
1910 cmd->path = ap_getword_conf(cmd->pool, &arg);
1911 /* Only if not an .htaccess file */
1913 cmd->override = OR_ALL|ACCESS_CONF;
1916 if (thiscmd->cmd_data) { /* <FilesMatch> */
1917 r = ap_pregcomp(cmd->pool, cmd->path, AP_REG_EXTENDED|USE_ICASE);
1919 return "Regex could not be compiled";
1922 else if (!strcmp(cmd->path, "~")) {
1923 cmd->path = ap_getword_conf(cmd->pool, &arg);
1924 r = ap_pregcomp(cmd->pool, cmd->path, AP_REG_EXTENDED|USE_ICASE);
1926 return "Regex could not be compiled";
1931 /* Ensure that the pathname is canonical, but we
1932 * can't test the case/aliases without a fixed path */
1933 if (apr_filepath_merge(&newpath, "", cmd->path,
1934 0, cmd->pool) != APR_SUCCESS)
1935 return apr_pstrcat(cmd->pool, "<Files \"", cmd->path,
1936 "\"> is invalid.", NULL);
1937 cmd->path = newpath;
1940 /* initialize our config and fetch it */
1941 conf = ap_set_config_vectors(cmd->server, new_file_conf, cmd->path,
1942 &core_module, cmd->pool);
1944 errmsg = ap_walk_config(cmd->directive->first_child, cmd, new_file_conf);
1948 conf->d = cmd->path;
1949 conf->d_is_fnmatch = apr_fnmatch_test(conf->d) != 0;
1952 ap_add_file_conf(c, new_file_conf);
1955 return apr_pstrcat(cmd->pool, "Multiple ", thiscmd->name,
1956 "> arguments not (yet) supported.", NULL);
1959 cmd->path = old_path;
1960 cmd->override = old_overrides;
1965 static const char *start_ifmod(cmd_parms *cmd, void *mconfig, const char *arg)
1967 const char *endp = ap_strrchr_c(arg, '>');
1968 int not = (arg[0] == '!');
1972 return unclosed_directive(cmd);
1975 arg = apr_pstrndup(cmd->pool, arg, endp - arg);
1982 return missing_container_arg(cmd);
1985 found = ap_find_linked_module(arg);
1987 /* search prelinked stuff */
1989 ap_module_symbol_t *current = ap_prelinked_module_symbols;
1991 for (; current->name; ++current) {
1992 if (!strcmp(current->name, arg)) {
1993 found = current->modp;
1999 /* search dynamic stuff */
2001 APR_OPTIONAL_FN_TYPE(ap_find_loaded_module_symbol) *check_symbol =
2002 APR_RETRIEVE_OPTIONAL_FN(ap_find_loaded_module_symbol);
2005 found = check_symbol(cmd->server, arg);
2009 if ((!not && found) || (not && !found)) {
2010 ap_directive_t *parent = NULL;
2011 ap_directive_t *current = NULL;
2014 retval = ap_build_cont_config(cmd->pool, cmd->temp_pool, cmd,
2015 ¤t, &parent, "<IfModule");
2016 *(ap_directive_t **)mconfig = current;
2020 *(ap_directive_t **)mconfig = NULL;
2021 return ap_soak_end_container(cmd, "<IfModule");
2025 AP_DECLARE(int) ap_exists_config_define(const char *name)
2030 defines = (char **)ap_server_config_defines->elts;
2031 for (i = 0; i < ap_server_config_defines->nelts; i++) {
2032 if (strcmp(defines[i], name) == 0) {
2040 static const char *start_ifdefine(cmd_parms *cmd, void *dummy, const char *arg)
2046 endp = ap_strrchr_c(arg, '>');
2048 return unclosed_directive(cmd);
2051 arg = apr_pstrndup(cmd->pool, arg, endp - arg);
2053 if (arg[0] == '!') {
2059 return missing_container_arg(cmd);
2062 defined = ap_exists_config_define(arg);
2063 if ((!not && defined) || (not && !defined)) {
2064 ap_directive_t *parent = NULL;
2065 ap_directive_t *current = NULL;
2068 retval = ap_build_cont_config(cmd->pool, cmd->temp_pool, cmd,
2069 ¤t, &parent, "<IfDefine");
2070 *(ap_directive_t **)dummy = current;
2074 *(ap_directive_t **)dummy = NULL;
2075 return ap_soak_end_container(cmd, "<IfDefine");
2079 /* httpd.conf commands... beginning with the <VirtualHost> business */
2081 static const char *virtualhost_section(cmd_parms *cmd, void *dummy,
2084 server_rec *main_server = cmd->server, *s;
2086 const char *endp = ap_strrchr_c(arg, '>');
2087 apr_pool_t *p = cmd->pool;
2089 const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
2095 return unclosed_directive(cmd);
2098 arg = apr_pstrndup(cmd->pool, arg, endp - arg);
2101 return missing_container_arg(cmd);
2104 /* FIXME: There's another feature waiting to happen here -- since you
2105 can now put multiple addresses/names on a single <VirtualHost>
2106 you might want to use it to group common definitions and then
2107 define other "subhosts" with their individual differences. But
2108 personally I'd rather just do it with a macro preprocessor. -djg */
2109 if (main_server->is_virtual) {
2110 return "<VirtualHost> doesn't nest!";
2113 errmsg = ap_init_virtual_host(p, arg, main_server, &s);
2118 s->next = main_server->next;
2119 main_server->next = s;
2121 s->defn_name = cmd->directive->filename;
2122 s->defn_line_number = cmd->directive->line_num;
2126 errmsg = ap_walk_config(cmd->directive->first_child, cmd,
2127 s->lookup_defaults);
2129 cmd->server = main_server;
2134 static const char *set_server_alias(cmd_parms *cmd, void *dummy,
2137 if (!cmd->server->names) {
2138 return "ServerAlias only used in <VirtualHost>";
2142 char **item, *name = ap_getword_conf(cmd->pool, &arg);
2144 if (ap_is_matchexp(name)) {
2145 item = (char **)apr_array_push(cmd->server->wild_names);
2148 item = (char **)apr_array_push(cmd->server->names);
2157 static const char *set_accf_map(cmd_parms *cmd, void *dummy,
2158 const char *iproto, const char* iaccf)
2160 const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
2161 core_server_config *conf = ap_get_module_config(cmd->server->module_config,
2169 proto = apr_pstrdup(cmd->pool, iproto);
2170 ap_str_tolower(proto);
2171 accf = apr_pstrdup(cmd->pool, iaccf);
2172 ap_str_tolower(accf);
2173 apr_table_set(conf->accf_map, proto, accf);
2178 AP_DECLARE(const char*) ap_get_server_protocol(server_rec* s)
2180 core_server_config *conf = ap_get_module_config(s->module_config,
2182 return conf->protocol;
2185 AP_DECLARE(void) ap_set_server_protocol(server_rec* s, const char* proto)
2187 core_server_config *conf = ap_get_module_config(s->module_config,
2189 conf->protocol = proto;
2192 static const char *set_protocol(cmd_parms *cmd, void *dummy,
2195 const char *err = ap_check_cmd_context(cmd, NOT_IN_DIR_LOC_FILE|NOT_IN_LIMIT);
2196 core_server_config *conf = ap_get_module_config(cmd->server->module_config,
2204 proto = apr_pstrdup(cmd->pool, arg);
2205 ap_str_tolower(proto);
2206 conf->protocol = proto;
2211 static const char *set_server_string_slot(cmd_parms *cmd, void *dummy,
2214 /* This one's pretty generic... */
2216 int offset = (int)(long)cmd->info;
2217 char *struct_ptr = (char *)cmd->server;
2219 const char *err = ap_check_cmd_context(cmd,
2220 NOT_IN_DIR_LOC_FILE|NOT_IN_LIMIT);
2225 *(const char **)(struct_ptr + offset) = arg;
2230 * The ServerName directive takes one argument with format
2231 * [scheme://]fully-qualified-domain-name[:port], for instance
2232 * ServerName www.example.com
2233 * ServerName www.example.com:80
2234 * ServerName https://www.example.com:443
2237 static const char *server_hostname_port(cmd_parms *cmd, void *dummy, const char *arg)
2239 const char *err = ap_check_cmd_context(cmd, NOT_IN_DIR_LOC_FILE|NOT_IN_LIMIT);
2240 const char *portstr, *part;
2248 part = ap_strstr_c(arg, "://");
2251 scheme = apr_pstrndup(cmd->pool, arg, part - arg);
2252 ap_str_tolower(scheme);
2253 cmd->server->server_scheme = (const char *)scheme;
2259 portstr = ap_strchr_c(part, ':');
2261 cmd->server->server_hostname = apr_pstrndup(cmd->pool, part,
2264 port = atoi(portstr);
2265 if (port <= 0 || port >= 65536) { /* 65536 == 1<<16 */
2266 return apr_pstrcat(cmd->temp_pool, "The port number \"", arg,
2267 "\" is outside the appropriate range "
2268 "(i.e., 1..65535).", NULL);
2272 cmd->server->server_hostname = apr_pstrdup(cmd->pool, part);
2276 cmd->server->port = port;
2280 static const char *set_signature_flag(cmd_parms *cmd, void *d_,
2283 core_dir_config *d = d_;
2284 const char *err = ap_check_cmd_context(cmd, NOT_IN_LIMIT);
2290 if (strcasecmp(arg, "On") == 0) {
2291 d->server_signature = srv_sig_on;
2293 else if (strcasecmp(arg, "Off") == 0) {
2294 d->server_signature = srv_sig_off;
2296 else if (strcasecmp(arg, "EMail") == 0) {
2297 d->server_signature = srv_sig_withmail;
2300 return "ServerSignature: use one of: off | on | email";
2306 static const char *set_server_root(cmd_parms *cmd, void *dummy,
2309 const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
2315 if ((apr_filepath_merge((char**)&ap_server_root, NULL, arg,
2316 APR_FILEPATH_TRUENAME, cmd->pool) != APR_SUCCESS)
2317 || !ap_is_directory(cmd->pool, ap_server_root)) {
2318 return "ServerRoot must be a valid directory";
2324 static const char *set_timeout(cmd_parms *cmd, void *dummy, const char *arg)
2326 const char *err = ap_check_cmd_context(cmd, NOT_IN_DIR_LOC_FILE|NOT_IN_LIMIT);
2332 cmd->server->timeout = apr_time_from_sec(atoi(arg));
2336 static const char *set_allow2f(cmd_parms *cmd, void *d_, int arg)
2338 core_dir_config *d = d_;
2339 const char *err = ap_check_cmd_context(cmd, NOT_IN_LIMIT);
2345 d->allow_encoded_slashes = arg != 0;
2349 static const char *set_hostname_lookups(cmd_parms *cmd, void *d_,
2352 core_dir_config *d = d_;
2353 const char *err = ap_check_cmd_context(cmd, NOT_IN_LIMIT);
2359 if (!strcasecmp(arg, "on")) {
2360 d->hostname_lookups = HOSTNAME_LOOKUP_ON;
2362 else if (!strcasecmp(arg, "off")) {
2363 d->hostname_lookups = HOSTNAME_LOOKUP_OFF;
2365 else if (!strcasecmp(arg, "double")) {
2366 d->hostname_lookups = HOSTNAME_LOOKUP_DOUBLE;
2369 return "parameter must be 'on', 'off', or 'double'";
2375 static const char *set_serverpath(cmd_parms *cmd, void *dummy,
2378 const char *err = ap_check_cmd_context(cmd, NOT_IN_DIR_LOC_FILE|NOT_IN_LIMIT);
2384 cmd->server->path = arg;
2385 cmd->server->pathlen = (int)strlen(arg);
2389 static const char *set_content_md5(cmd_parms *cmd, void *d_, int arg)
2391 core_dir_config *d = d_;
2392 const char *err = ap_check_cmd_context(cmd, NOT_IN_LIMIT);
2398 d->content_md5 = arg != 0;
2402 static const char *set_accept_path_info(cmd_parms *cmd, void *d_, const char *arg)
2404 core_dir_config *d = d_;
2406 if (strcasecmp(arg, "on") == 0) {
2407 d->accept_path_info = AP_REQ_ACCEPT_PATH_INFO;
2409 else if (strcasecmp(arg, "off") == 0) {
2410 d->accept_path_info = AP_REQ_REJECT_PATH_INFO;
2412 else if (strcasecmp(arg, "default") == 0) {
2413 d->accept_path_info = AP_REQ_DEFAULT_PATH_INFO;
2416 return "AcceptPathInfo must be set to on, off or default";
2422 static const char *set_use_canonical_name(cmd_parms *cmd, void *d_,
2425 core_dir_config *d = d_;
2426 const char *err = ap_check_cmd_context(cmd, NOT_IN_LIMIT);
2432 if (strcasecmp(arg, "on") == 0) {
2433 d->use_canonical_name = USE_CANONICAL_NAME_ON;
2435 else if (strcasecmp(arg, "off") == 0) {
2436 d->use_canonical_name = USE_CANONICAL_NAME_OFF;
2438 else if (strcasecmp(arg, "dns") == 0) {
2439 d->use_canonical_name = USE_CANONICAL_NAME_DNS;
2442 return "parameter must be 'on', 'off', or 'dns'";
2448 static const char *set_use_canonical_phys_port(cmd_parms *cmd, void *d_,
2451 core_dir_config *d = d_;
2452 const char *err = ap_check_cmd_context(cmd, NOT_IN_LIMIT);
2458 if (strcasecmp(arg, "on") == 0) {
2459 d->use_canonical_phys_port = USE_CANONICAL_PHYS_PORT_ON;
2461 else if (strcasecmp(arg, "off") == 0) {
2462 d->use_canonical_phys_port = USE_CANONICAL_PHYS_PORT_OFF;
2465 return "parameter must be 'on' or 'off'";
2472 static const char *include_config (cmd_parms *cmd, void *dummy,
2475 ap_directive_t *conftree = NULL;
2476 const char* conffile, *error;
2477 unsigned *recursion;
2480 apr_pool_userdata_get(&data, "ap_include_sentinel", cmd->pool);
2485 data = recursion = apr_palloc(cmd->pool, sizeof(*recursion));
2487 apr_pool_userdata_setn(data, "ap_include_sentinel", NULL, cmd->pool);
2490 if (++*recursion > AP_MAX_INCLUDE_DEPTH) {
2492 return apr_psprintf(cmd->pool, "Exceeded maximum include depth of %u. "
2493 "You have probably a recursion somewhere.",
2494 AP_MAX_INCLUDE_DEPTH);
2497 conffile = ap_server_root_relative(cmd->pool, name);
2500 return apr_pstrcat(cmd->pool, "Invalid Include path ",
2504 error = ap_process_resource_config(cmd->server, conffile,
2505 &conftree, cmd->pool, cmd->temp_pool);
2511 *(ap_directive_t **)dummy = conftree;
2513 /* recursion level done */
2521 static const char *set_loglevel(cmd_parms *cmd, void *dummy, const char *arg)
2525 const char *err = ap_check_cmd_context(cmd,
2526 NOT_IN_DIR_LOC_FILE|NOT_IN_LIMIT);
2531 if ((str = ap_getword_conf(cmd->pool, &arg))) {
2532 if (!strcasecmp(str, "emerg")) {
2533 cmd->server->loglevel = APLOG_EMERG;
2535 else if (!strcasecmp(str, "alert")) {
2536 cmd->server->loglevel = APLOG_ALERT;
2538 else if (!strcasecmp(str, "crit")) {
2539 cmd->server->loglevel = APLOG_CRIT;
2541 else if (!strcasecmp(str, "error")) {
2542 cmd->server->loglevel = APLOG_ERR;
2544 else if (!strcasecmp(str, "warn")) {
2545 cmd->server->loglevel = APLOG_WARNING;
2547 else if (!strcasecmp(str, "notice")) {
2548 cmd->server->loglevel = APLOG_NOTICE;
2550 else if (!strcasecmp(str, "info")) {
2551 cmd->server->loglevel = APLOG_INFO;
2553 else if (!strcasecmp(str, "debug")) {
2554 cmd->server->loglevel = APLOG_DEBUG;
2557 return "LogLevel requires level keyword: one of "
2558 "emerg/alert/crit/error/warn/notice/info/debug";
2562 return "LogLevel requires level keyword";
2568 AP_DECLARE(const char *) ap_psignature(const char *prefix, request_rec *r)
2571 core_dir_config *conf;
2573 conf = (core_dir_config *)ap_get_module_config(r->per_dir_config,
2575 if ((conf->server_signature == srv_sig_off)
2576 || (conf->server_signature == srv_sig_unset)) {
2580 apr_snprintf(sport, sizeof sport, "%u", (unsigned) ap_get_server_port(r));
2582 if (conf->server_signature == srv_sig_withmail) {
2583 return apr_pstrcat(r->pool, prefix, "<address>",
2584 ap_get_server_banner(),
2585 " Server at <a href=\"",
2586 ap_is_url(r->server->server_admin) ? "" : "mailto:",
2587 ap_escape_html(r->pool, r->server->server_admin),
2589 ap_escape_html(r->pool, ap_get_server_name(r)),
2590 "</a> Port ", sport,
2591 "</address>\n", NULL);
2594 return apr_pstrcat(r->pool, prefix, "<address>", ap_get_server_banner(),
2596 ap_escape_html(r->pool, ap_get_server_name(r)),
2598 "</address>\n", NULL);
2602 * Handle a request to include the server's OS platform in the Server
2603 * response header field (the ServerTokens directive). Unfortunately
2604 * this requires a new global in order to communicate the setting back to
2605 * http_main so it can insert the information in the right place in the
2609 static char *server_banner = NULL;
2610 static int banner_locked = 0;
2611 static char *server_description = NULL;
2613 enum server_token_type {
2614 SrvTk_MAJOR, /* eg: Apache/2 */
2615 SrvTk_MINOR, /* eg. Apache/2.0 */
2616 SrvTk_MINIMAL, /* eg: Apache/2.0.41 */
2617 SrvTk_OS, /* eg: Apache/2.0.41 (UNIX) */
2618 SrvTk_FULL, /* eg: Apache/2.0.41 (UNIX) PHP/4.2.2 FooBar/1.2b */
2619 SrvTk_PRODUCT_ONLY /* eg: Apache */
2621 static enum server_token_type ap_server_tokens = SrvTk_FULL;
2623 static apr_status_t reset_banner(void *dummy)
2626 ap_server_tokens = SrvTk_FULL;
2627 server_banner = NULL;
2628 server_description = NULL;
2632 AP_DECLARE(void) ap_get_server_revision(ap_version_t *version)
2634 version->major = AP_SERVER_MAJORVERSION_NUMBER;
2635 version->minor = AP_SERVER_MINORVERSION_NUMBER;
2636 version->patch = AP_SERVER_PATCHLEVEL_NUMBER;
2637 version->add_string = AP_SERVER_ADD_STRING;
2640 AP_DECLARE(const char *) ap_get_server_description(void)
2642 return server_description ? server_description :
2643 AP_SERVER_BASEVERSION " (" PLATFORM ")";
2646 AP_DECLARE(const char *) ap_get_server_banner(void)
2648 return server_banner ? server_banner : AP_SERVER_BASEVERSION;
2651 AP_DECLARE(void) ap_add_version_component(apr_pool_t *pconf, const char *component)
2653 if (! banner_locked) {
2655 * If the version string is null, register our cleanup to reset the
2656 * pointer on pool destruction. We also know that, if NULL,
2657 * we are adding the original SERVER_BASEVERSION string.
2659 if (server_banner == NULL) {
2660 apr_pool_cleanup_register(pconf, NULL, reset_banner,
2661 apr_pool_cleanup_null);
2662 server_banner = apr_pstrdup(pconf, component);
2666 * Tack the given component identifier to the end of
2667 * the existing string.
2669 server_banner = apr_pstrcat(pconf, server_banner, " ",
2673 server_description = apr_pstrcat(pconf, server_description, " ",
2678 * This routine adds the real server base identity to the banner string,
2679 * and then locks out changes until the next reconfig.
2681 static void set_banner(apr_pool_t *pconf)
2683 if (ap_server_tokens == SrvTk_PRODUCT_ONLY) {
2684 ap_add_version_component(pconf, AP_SERVER_BASEPRODUCT);
2686 else if (ap_server_tokens == SrvTk_MINIMAL) {
2687 ap_add_version_component(pconf, AP_SERVER_BASEVERSION);
2689 else if (ap_server_tokens == SrvTk_MINOR) {
2690 ap_add_version_component(pconf, AP_SERVER_BASEPRODUCT "/" AP_SERVER_MINORREVISION);
2692 else if (ap_server_tokens == SrvTk_MAJOR) {
2693 ap_add_version_component(pconf, AP_SERVER_BASEPRODUCT "/" AP_SERVER_MAJORVERSION);
2696 ap_add_version_component(pconf, AP_SERVER_BASEVERSION " (" PLATFORM ")");
2700 * Lock the server_banner string if we're not displaying
2701 * the full set of tokens
2703 if (ap_server_tokens != SrvTk_FULL) {
2706 server_description = AP_SERVER_BASEVERSION " (" PLATFORM ")";
2709 static const char *set_serv_tokens(cmd_parms *cmd, void *dummy,
2712 const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
2718 if (!strcasecmp(arg, "OS")) {
2719 ap_server_tokens = SrvTk_OS;
2721 else if (!strcasecmp(arg, "Min") || !strcasecmp(arg, "Minimal")) {
2722 ap_server_tokens = SrvTk_MINIMAL;
2724 else if (!strcasecmp(arg, "Major")) {
2725 ap_server_tokens = SrvTk_MAJOR;
2727 else if (!strcasecmp(arg, "Minor") ) {
2728 ap_server_tokens = SrvTk_MINOR;
2730 else if (!strcasecmp(arg, "Prod") || !strcasecmp(arg, "ProductOnly")) {
2731 ap_server_tokens = SrvTk_PRODUCT_ONLY;
2734 ap_server_tokens = SrvTk_FULL;
2740 static const char *set_limit_req_line(cmd_parms *cmd, void *dummy,
2743 const char *err = ap_check_cmd_context(cmd,
2744 NOT_IN_DIR_LOC_FILE|NOT_IN_LIMIT);
2753 return apr_pstrcat(cmd->temp_pool, "LimitRequestLine \"", arg,
2754 "\" must be a non-negative integer", NULL);
2757 cmd->server->limit_req_line = lim;
2761 static const char *set_limit_req_fieldsize(cmd_parms *cmd, void *dummy,
2764 const char *err = ap_check_cmd_context(cmd,
2765 NOT_IN_DIR_LOC_FILE|NOT_IN_LIMIT);
2774 return apr_pstrcat(cmd->temp_pool, "LimitRequestFieldsize \"", arg,
2775 "\" must be a non-negative integer",
2779 cmd->server->limit_req_fieldsize = lim;
2783 static const char *set_limit_req_fields(cmd_parms *cmd, void *dummy,
2786 const char *err = ap_check_cmd_context(cmd,
2787 NOT_IN_DIR_LOC_FILE|NOT_IN_LIMIT);
2796 return apr_pstrcat(cmd->temp_pool, "LimitRequestFields \"", arg,
2797 "\" must be a non-negative integer (0 = no limit)",
2801 cmd->server->limit_req_fields = lim;
2805 static const char *set_limit_req_body(cmd_parms *cmd, void *conf_,
2808 core_dir_config *conf = conf_;
2809 const char *err = ap_check_cmd_context(cmd, NOT_IN_LIMIT);
2816 if (APR_SUCCESS != apr_strtoff(&conf->limit_req_body, arg, &errp, 10)) {
2817 return "LimitRequestBody argument is not parsable.";
2819 if (*errp || conf->limit_req_body < 0) {
2820 return "LimitRequestBody requires a non-negative integer.";
2826 static const char *set_limit_xml_req_body(cmd_parms *cmd, void *conf_,
2829 core_dir_config *conf = conf_;
2830 const char *err = ap_check_cmd_context(cmd, NOT_IN_LIMIT);
2836 conf->limit_xml_body = atol(arg);
2837 if (conf->limit_xml_body < 0)
2838 return "LimitXMLRequestBody requires a non-negative integer.";
2843 AP_DECLARE(size_t) ap_get_limit_xml_body(const request_rec *r)
2845 core_dir_config *conf;
2847 conf = ap_get_module_config(r->per_dir_config, &core_module);
2848 if (conf->limit_xml_body == AP_LIMIT_UNSET)
2849 return AP_DEFAULT_LIMIT_XML_BODY;
2851 return (size_t)conf->limit_xml_body;
2854 #if !defined (RLIMIT_CPU) || !(defined (RLIMIT_DATA) || defined (RLIMIT_VMEM) || defined(RLIMIT_AS)) || !defined (RLIMIT_NPROC)
2855 static const char *no_set_limit(cmd_parms *cmd, void *conf_,
2856 const char *arg, const char *arg2)
2858 ap_log_error(APLOG_MARK, APLOG_ERR, 0, cmd->server,
2859 "%s not supported on this platform", cmd->cmd->name);
2866 static const char *set_limit_cpu(cmd_parms *cmd, void *conf_,
2867 const char *arg, const char *arg2)
2869 core_dir_config *conf = conf_;
2871 unixd_set_rlimit(cmd, &conf->limit_cpu, arg, arg2, RLIMIT_CPU);
2876 #if defined (RLIMIT_DATA) || defined (RLIMIT_VMEM) || defined(RLIMIT_AS)
2877 static const char *set_limit_mem(cmd_parms *cmd, void *conf_,
2878 const char *arg, const char * arg2)
2880 core_dir_config *conf = conf_;
2882 #if defined(RLIMIT_AS)
2883 unixd_set_rlimit(cmd, &conf->limit_mem, arg, arg2 ,RLIMIT_AS);
2884 #elif defined(RLIMIT_DATA)
2885 unixd_set_rlimit(cmd, &conf->limit_mem, arg, arg2, RLIMIT_DATA);
2886 #elif defined(RLIMIT_VMEM)
2887 unixd_set_rlimit(cmd, &conf->limit_mem, arg, arg2, RLIMIT_VMEM);
2895 static const char *set_limit_nproc(cmd_parms *cmd, void *conf_,
2896 const char *arg, const char * arg2)
2898 core_dir_config *conf = conf_;
2900 unixd_set_rlimit(cmd, &conf->limit_nproc, arg, arg2, RLIMIT_NPROC);
2905 static const char *set_recursion_limit(cmd_parms *cmd, void *dummy,
2906 const char *arg1, const char *arg2)
2908 core_server_config *conf = ap_get_module_config(cmd->server->module_config,
2910 int limit = atoi(arg1);
2913 return "The recursion limit must be greater than zero.";
2916 ap_log_error(APLOG_MARK, APLOG_WARNING, 0, cmd->server,
2917 "Limiting internal redirects to very low numbers may "
2918 "cause normal requests to fail.");
2921 conf->redirect_limit = limit;
2927 return "The recursion limit must be greater than zero.";
2930 ap_log_error(APLOG_MARK, APLOG_WARNING, 0, cmd->server,
2931 "Limiting the subrequest depth to a very low level may"
2932 " cause normal requests to fail.");
2936 conf->subreq_limit = limit;
2941 static void log_backtrace(const request_rec *r)
2943 const request_rec *top = r;
2945 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
2946 "r->uri = %s", r->uri ? r->uri : "(unexpectedly NULL)");
2948 while (top && (top->prev || top->main)) {
2951 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
2952 "redirected from r->uri = %s",
2953 top->uri ? top->uri : "(unexpectedly NULL)");
2956 if (!top->prev && top->main) {
2958 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
2959 "subrequested from r->uri = %s",
2960 top->uri ? top->uri : "(unexpectedly NULL)");
2966 * check whether redirect limit is reached
2968 AP_DECLARE(int) ap_is_recursion_limit_exceeded(const request_rec *r)
2970 core_server_config *conf = ap_get_module_config(r->server->module_config,
2972 const request_rec *top = r;
2973 int redirects = 0, subreqs = 0;
2974 int rlimit = conf->redirect_limit
2975 ? conf->redirect_limit
2976 : AP_DEFAULT_MAX_INTERNAL_REDIRECTS;
2977 int slimit = conf->subreq_limit
2978 ? conf->subreq_limit
2979 : AP_DEFAULT_MAX_SUBREQ_DEPTH;
2982 while (top->prev || top->main) {
2984 if (++redirects >= rlimit) {
2985 /* uuh, too much. */
2986 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
2987 "Request exceeded the limit of %d internal "
2988 "redirects due to probable configuration error. "
2989 "Use 'LimitInternalRecursion' to increase the "
2990 "limit if necessary. Use 'LogLevel debug' to get "
2991 "a backtrace.", rlimit);
2993 /* post backtrace */
2996 /* return failure */
3003 if (!top->prev && top->main) {
3004 if (++subreqs >= slimit) {
3005 /* uuh, too much. */
3006 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
3007 "Request exceeded the limit of %d subrequest "
3008 "nesting levels due to probable confguration "
3009 "error. Use 'LimitInternalRecursion' to increase "
3010 "the limit if necessary. Use 'LogLevel debug' to "
3011 "get a backtrace.", slimit);
3013 /* post backtrace */
3016 /* return failure */
3024 /* recursion state: ok */
3028 static const char *add_ct_output_filters(cmd_parms *cmd, void *conf_,
3029 const char *arg, const char *arg2)
3031 core_dir_config *conf = conf_;
3032 ap_filter_rec_t *old, *new = NULL;
3033 const char *filter_name;
3035 if (!conf->ct_output_filters) {
3036 conf->ct_output_filters = apr_hash_make(cmd->pool);
3040 old = (ap_filter_rec_t*) apr_hash_get(conf->ct_output_filters, arg2,
3041 APR_HASH_KEY_STRING);
3042 /* find last entry */
3051 (filter_name = ap_getword(cmd->pool, &arg, ';')) &&
3052 strcmp(filter_name, "")) {
3053 new = apr_pcalloc(cmd->pool, sizeof(ap_filter_rec_t));
3054 new->name = filter_name;
3056 /* We found something, so let's append it. */
3061 apr_hash_set(conf->ct_output_filters, arg2,
3062 APR_HASH_KEY_STRING, new);
3068 return "invalid filter name";
3074 * Insert filters requested by the AddOutputFilterByType
3075 * configuration directive. We cannot add filters based
3076 * on content-type until after the handler has started
3077 * to run. Only then do we reliably know the content-type.
3079 void ap_add_output_filters_by_type(request_rec *r)
3081 core_dir_config *conf;
3084 conf = (core_dir_config *)ap_get_module_config(r->per_dir_config,
3087 /* We can't do anything with no content-type or if we don't have a
3088 * filter configured.
3090 if (!r->content_type || !conf->ct_output_filters) {
3094 /* remove c-t decoration */
3095 ctype = ap_field_noparam(r->pool, r->content_type);
3097 ap_filter_rec_t *ct_filter;
3098 ct_filter = apr_hash_get(conf->ct_output_filters, ctype,
3099 APR_HASH_KEY_STRING);
3101 ap_add_output_filter(ct_filter->name, NULL, r, r->connection);
3102 ct_filter = ct_filter->next;
3109 static const char *set_trace_enable(cmd_parms *cmd, void *dummy,
3112 core_server_config *conf = ap_get_module_config(cmd->server->module_config,
3115 if (strcasecmp(arg1, "on") == 0) {
3116 conf->trace_enable = AP_TRACE_ENABLE;
3118 else if (strcasecmp(arg1, "off") == 0) {
3119 conf->trace_enable = AP_TRACE_DISABLE;
3121 else if (strcasecmp(arg1, "extended") == 0) {
3122 conf->trace_enable = AP_TRACE_EXTENDED;
3125 return "TraceEnable must be one of 'on', 'off', or 'extended'";
3131 /* Note --- ErrorDocument will now work from .htaccess files.
3132 * The AllowOverride of Fileinfo allows webmasters to turn it off
3135 static const command_rec core_cmds[] = {
3137 /* Old access config file commands */
3139 AP_INIT_RAW_ARGS("<Directory", dirsection, NULL, RSRC_CONF,
3140 "Container for directives affecting resources located in the specified "
3142 AP_INIT_RAW_ARGS("<Location", urlsection, NULL, RSRC_CONF,
3143 "Container for directives affecting resources accessed through the "
3144 "specified URL paths"),
3145 AP_INIT_RAW_ARGS("<VirtualHost", virtualhost_section, NULL, RSRC_CONF,
3146 "Container to map directives to a particular virtual host, takes one or "
3147 "more host addresses"),
3148 AP_INIT_RAW_ARGS("<Files", filesection, NULL, OR_ALL,
3149 "Container for directives affecting files matching specified patterns"),
3150 AP_INIT_RAW_ARGS("<Limit", ap_limit_section, NULL, OR_ALL,
3151 "Container for authentication directives when accessed using specified HTTP "
3153 AP_INIT_RAW_ARGS("<LimitExcept", ap_limit_section, (void*)1, OR_ALL,
3154 "Container for authentication directives to be applied when any HTTP "
3155 "method other than those specified is used to access the resource"),
3156 AP_INIT_TAKE1("<IfModule", start_ifmod, NULL, EXEC_ON_READ | OR_ALL,
3157 "Container for directives based on existance of specified modules"),
3158 AP_INIT_TAKE1("<IfDefine", start_ifdefine, NULL, EXEC_ON_READ | OR_ALL,
3159 "Container for directives based on existance of command line defines"),
3160 AP_INIT_RAW_ARGS("<DirectoryMatch", dirsection, (void*)1, RSRC_CONF,
3161 "Container for directives affecting resources located in the "
3162 "specified directories"),
3163 AP_INIT_RAW_ARGS("<LocationMatch", urlsection, (void*)1, RSRC_CONF,
3164 "Container for directives affecting resources accessed through the "
3165 "specified URL paths"),
3166 AP_INIT_RAW_ARGS("<FilesMatch", filesection, (void*)1, OR_ALL,
3167 "Container for directives affecting files matching specified patterns"),
3169 AP_INIT_TAKE1("GprofDir", set_gprof_dir, NULL, RSRC_CONF,
3170 "Directory to plop gmon.out files"),
3172 AP_INIT_TAKE1("AddDefaultCharset", set_add_default_charset, NULL, OR_FILEINFO,
3173 "The name of the default charset to add to any Content-Type without one or 'Off' to disable"),
3174 AP_INIT_TAKE1("AcceptPathInfo", set_accept_path_info, NULL, OR_FILEINFO,
3175 "Set to on or off for PATH_INFO to be accepted by handlers, or default for the per-handler preference"),
3177 /* Old resource config file commands */
3179 AP_INIT_RAW_ARGS("AccessFileName", set_access_name, NULL, RSRC_CONF,
3180 "Name(s) of per-directory config files (default: .htaccess)"),
3181 AP_INIT_TAKE1("DocumentRoot", set_document_root, NULL, RSRC_CONF,
3182 "Root directory of the document tree"),
3183 AP_INIT_TAKE2("ErrorDocument", set_error_document, NULL, OR_FILEINFO,
3184 "Change responses for HTTP errors"),
3185 AP_INIT_RAW_ARGS("AllowOverride", set_override, NULL, ACCESS_CONF,
3186 "Controls what groups of directives can be configured by per-directory "
3188 AP_INIT_RAW_ARGS("Options", set_options, NULL, OR_OPTIONS,
3189 "Set a number of attributes for a given directory"),
3190 AP_INIT_TAKE1("DefaultType", ap_set_string_slot,
3191 (void*)APR_OFFSETOF(core_dir_config, ap_default_type),
3192 OR_FILEINFO, "the default MIME type for untypable files"),
3193 AP_INIT_RAW_ARGS("FileETag", set_etag_bits, NULL, OR_FILEINFO,
3194 "Specify components used to construct a file's ETag"),
3195 AP_INIT_TAKE1("EnableMMAP", set_enable_mmap, NULL, OR_FILEINFO,
3196 "Controls whether memory-mapping may be used to read files"),
3197 AP_INIT_TAKE1("EnableSendfile", set_enable_sendfile, NULL, OR_FILEINFO,
3198 "Controls whether sendfile may be used to transmit files"),
3200 /* Old server config file commands */
3202 AP_INIT_TAKE1("Protocol", set_protocol, NULL, RSRC_CONF,
3203 "Set the Protocol for httpd to use."),
3204 AP_INIT_TAKE2("AcceptFilter", set_accf_map, NULL, RSRC_CONF,
3205 "Set the Accept Filter to use for a protocol"),
3206 AP_INIT_TAKE1("Port", ap_set_deprecated, NULL, RSRC_CONF,
3207 "Port was replaced with Listen in Apache 2.0"),
3208 AP_INIT_TAKE1("HostnameLookups", set_hostname_lookups, NULL,
3209 ACCESS_CONF|RSRC_CONF,
3210 "\"on\" to enable, \"off\" to disable reverse DNS lookups, or \"double\" to "
3211 "enable double-reverse DNS lookups"),
3212 AP_INIT_TAKE1("ServerAdmin", set_server_string_slot,
3213 (void *)APR_OFFSETOF(server_rec, server_admin), RSRC_CONF,
3214 "The email address of the server administrator"),
3215 AP_INIT_TAKE1("ServerName", server_hostname_port, NULL, RSRC_CONF,
3216 "The hostname and port of the server"),
3217 AP_INIT_TAKE1("ServerSignature", set_signature_flag, NULL, OR_ALL,
3218 "En-/disable server signature (on|off|email)"),
3219 AP_INIT_TAKE1("ServerRoot", set_server_root, NULL, RSRC_CONF | EXEC_ON_READ,
3220 "Common directory of server-related files (logs, confs, etc.)"),
3221 AP_INIT_TAKE1("ErrorLog", set_server_string_slot,
3222 (void *)APR_OFFSETOF(server_rec, error_fname), RSRC_CONF,
3223 "The filename of the error log"),
3224 AP_INIT_RAW_ARGS("ServerAlias", set_server_alias, NULL, RSRC_CONF,
3225 "A name or names alternately used to access the server"),
3226 AP_INIT_TAKE1("ServerPath", set_serverpath, NULL, RSRC_CONF,
3227 "The pathname the server can be reached at"),
3228 AP_INIT_TAKE1("Timeout", set_timeout, NULL, RSRC_CONF,
3229 "Timeout duration (sec)"),
3230 AP_INIT_FLAG("ContentDigest", set_content_md5, NULL, OR_OPTIONS,
3231 "whether or not to send a Content-MD5 header with each request"),
3232 AP_INIT_TAKE1("UseCanonicalName", set_use_canonical_name, NULL,
3233 RSRC_CONF|ACCESS_CONF,
3234 "How to work out the ServerName : Port when constructing URLs"),
3235 AP_INIT_TAKE1("UseCanonicalPhysicalPort", set_use_canonical_phys_port, NULL,
3236 RSRC_CONF|ACCESS_CONF,
3237 "Whether to use the physical Port when constructing URLs"),
3238 /* TODO: RlimitFoo should all be part of mod_cgi, not in the core */
3239 /* TODO: ListenBacklog in MPM */
3240 AP_INIT_TAKE1("Include", include_config, NULL,
3241 (RSRC_CONF | ACCESS_CONF | EXEC_ON_READ),
3242 "Name of the config file to be included"),
3243 AP_INIT_TAKE1("LogLevel", set_loglevel, NULL, RSRC_CONF,
3244 "Level of verbosity in error logging"),
3245 AP_INIT_TAKE1("NameVirtualHost", ap_set_name_virtual_host, NULL, RSRC_CONF,
3246 "A numeric IP address:port, or the name of a host"),
3247 AP_INIT_TAKE1("ServerTokens", set_serv_tokens, NULL, RSRC_CONF,
3248 "Determine tokens displayed in the Server: header - Min(imal), OS or Full"),
3249 AP_INIT_TAKE1("LimitRequestLine", set_limit_req_line, NULL, RSRC_CONF,
3250 "Limit on maximum size of an HTTP request line"),
3251 AP_INIT_TAKE1("LimitRequestFieldsize", set_limit_req_fieldsize, NULL,
3253 "Limit on maximum size of an HTTP request header field"),
3254 AP_INIT_TAKE1("LimitRequestFields", set_limit_req_fields, NULL, RSRC_CONF,
3255 "Limit (0 = unlimited) on max number of header fields in a request message"),
3256 AP_INIT_TAKE1("LimitRequestBody", set_limit_req_body,
3257 (void*)APR_OFFSETOF(core_dir_config, limit_req_body), OR_ALL,
3258 "Limit (in bytes) on maximum size of request message body"),
3259 AP_INIT_TAKE1("LimitXMLRequestBody", set_limit_xml_req_body, NULL, OR_ALL,
3260 "Limit (in bytes) on maximum size of an XML-based request "
3263 /* System Resource Controls */
3265 AP_INIT_TAKE12("RLimitCPU", set_limit_cpu,
3266 (void*)APR_OFFSETOF(core_dir_config, limit_cpu),
3267 OR_ALL, "Soft/hard limits for max CPU usage in seconds"),
3269 AP_INIT_TAKE12("RLimitCPU", no_set_limit, NULL,
3270 OR_ALL, "Soft/hard limits for max CPU usage in seconds"),
3272 #if defined (RLIMIT_DATA) || defined (RLIMIT_VMEM) || defined (RLIMIT_AS)
3273 AP_INIT_TAKE12("RLimitMEM", set_limit_mem,
3274 (void*)APR_OFFSETOF(core_dir_config, limit_mem),
3275 OR_ALL, "Soft/hard limits for max memory usage per process"),
3277 AP_INIT_TAKE12("RLimitMEM", no_set_limit, NULL,
3278 OR_ALL, "Soft/hard limits for max memory usage per process"),
3281 AP_INIT_TAKE12("RLimitNPROC", set_limit_nproc,
3282 (void*)APR_OFFSETOF(core_dir_config, limit_nproc),
3283 OR_ALL, "soft/hard limits for max number of processes per uid"),
3285 AP_INIT_TAKE12("RLimitNPROC", no_set_limit, NULL,
3286 OR_ALL, "soft/hard limits for max number of processes per uid"),
3289 /* internal recursion stopper */
3290 AP_INIT_TAKE12("LimitInternalRecursion", set_recursion_limit, NULL, RSRC_CONF,
3291 "maximum recursion depth of internal redirects and subrequests"),
3293 AP_INIT_TAKE1("ForceType", ap_set_string_slot_lower,
3294 (void *)APR_OFFSETOF(core_dir_config, mime_type), OR_FILEINFO,
3295 "a mime type that overrides other configured type"),
3296 AP_INIT_TAKE1("SetHandler", ap_set_string_slot_lower,
3297 (void *)APR_OFFSETOF(core_dir_config, handler), OR_FILEINFO,
3298 "a handler name that overrides any other configured handler"),
3299 AP_INIT_TAKE1("SetOutputFilter", ap_set_string_slot,
3300 (void *)APR_OFFSETOF(core_dir_config, output_filters), OR_FILEINFO,
3301 "filter (or ; delimited list of filters) to be run on the request content"),
3302 AP_INIT_TAKE1("SetInputFilter", ap_set_string_slot,
3303 (void *)APR_OFFSETOF(core_dir_config, input_filters), OR_FILEINFO,
3304 "filter (or ; delimited list of filters) to be run on the request body"),
3305 AP_INIT_ITERATE2("AddOutputFilterByType", add_ct_output_filters,
3306 (void *)APR_OFFSETOF(core_dir_config, ct_output_filters), OR_FILEINFO,
3307 "output filter name followed by one or more content-types"),
3308 AP_INIT_FLAG("AllowEncodedSlashes", set_allow2f, NULL, RSRC_CONF,
3309 "Allow URLs containing '/' encoded as '%2F'"),
3312 * These are default configuration directives that mpms can/should
3313 * pay attention to. If an mpm wishes to use these, they should
3314 * #defined them in mpm.h.
3316 #ifdef AP_MPM_WANT_SET_PIDFILE
3317 AP_INIT_TAKE1("PidFile", ap_mpm_set_pidfile, NULL, RSRC_CONF,
3318 "A file for logging the server process ID"),
3320 #ifdef AP_MPM_WANT_SET_SCOREBOARD
3321 AP_INIT_TAKE1("ScoreBoardFile", ap_mpm_set_scoreboard, NULL, RSRC_CONF,
3322 "A file for Apache to maintain runtime process management information"),
3324 #ifdef AP_MPM_WANT_SET_LOCKFILE
3325 AP_INIT_TAKE1("LockFile", ap_mpm_set_lockfile, NULL, RSRC_CONF,
3326 "The lockfile used when Apache needs to lock the accept() call (depreciated)"),
3328 #ifdef AP_MPM_WANT_SET_MAX_REQUESTS
3329 AP_INIT_TAKE1("MaxRequestsPerChild", ap_mpm_set_max_requests, NULL, RSRC_CONF,
3330 "Maximum number of requests a particular child serves before dying."),
3332 #ifdef AP_MPM_WANT_SET_COREDUMPDIR
3333 AP_INIT_TAKE1("CoreDumpDirectory", ap_mpm_set_coredumpdir, NULL, RSRC_CONF,
3334 "The location of the directory Apache changes to before dumping core"),
3336 #ifdef AP_MPM_WANT_SET_ACCEPT_LOCK_MECH
3337 AP_INIT_TAKE1("AcceptMutex", ap_mpm_set_accept_lock_mech, NULL, RSRC_CONF,
3338 ap_available_mutexes_string),
3340 #ifdef AP_MPM_WANT_SET_MAX_MEM_FREE
3341 AP_INIT_TAKE1("MaxMemFree", ap_mpm_set_max_mem_free, NULL, RSRC_CONF,
3342 "Maximum number of 1k blocks a particular childs allocator may hold."),
3344 #ifdef AP_MPM_WANT_SET_STACKSIZE
3345 AP_INIT_TAKE1("ThreadStackSize", ap_mpm_set_thread_stacksize, NULL, RSRC_CONF,
3346 "Size in bytes of stack used by threads handling client connections"),
3348 #if AP_ENABLE_EXCEPTION_HOOK
3349 AP_INIT_TAKE1("EnableExceptionHook", ap_mpm_set_exception_hook, NULL, RSRC_CONF,
3350 "Controls whether exception hook may be called after a crash"),
3352 AP_INIT_TAKE1("TraceEnable", set_trace_enable, NULL, RSRC_CONF,
3353 "'on' (default), 'off' or 'extended' to trace request body content"),
3357 /*****************************************************************
3359 * Core handlers for various phases of server operation...
3362 AP_DECLARE_NONSTD(int) ap_core_translate(request_rec *r)
3364 void *sconf = r->server->module_config;
3365 core_server_config *conf = ap_get_module_config(sconf, &core_module);
3368 /* XXX this seems too specific, this should probably become
3369 * some general-case test
3372 return HTTP_FORBIDDEN;
3374 if (!r->uri || ((r->uri[0] != '/') && strcmp(r->uri, "*"))) {
3375 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
3376 "Invalid URI in request %s", r->the_request);
3377 return HTTP_BAD_REQUEST;
3381 && !strncmp(r->uri, r->server->path, r->server->pathlen)
3382 && (r->server->path[r->server->pathlen - 1] == '/'
3383 || r->uri[r->server->pathlen] == '/'
3384 || r->uri[r->server->pathlen] == '\0'))
3386 /* skip all leading /'s (e.g. http://localhost///foo)
3387 * so we are looking at only the relative path.
3389 char *path = r->uri + r->server->pathlen;
3390 while (*path == '/') {
3393 if ((rv = apr_filepath_merge(&r->filename, conf->ap_document_root, path,
3394 APR_FILEPATH_TRUENAME
3395 | APR_FILEPATH_SECUREROOT, r->pool))
3397 ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
3398 "Cannot map %s to file", r->the_request);
3399 return HTTP_FORBIDDEN;
3401 r->canonical_filename = r->filename;
3405 * Make sure that we do not mess up the translation by adding two
3406 * /'s in a row. This happens under windows when the document
3407 * root ends with a /
3409 /* skip all leading /'s (e.g. http://localhost///foo)
3410 * so we are looking at only the relative path.
3412 char *path = r->uri;
3413 while (*path == '/') {
3416 if ((rv = apr_filepath_merge(&r->filename, conf->ap_document_root, path,
3417 APR_FILEPATH_TRUENAME
3418 | APR_FILEPATH_SECUREROOT, r->pool))
3420 ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
3421 "Cannot map %s to file", r->the_request);
3422 return HTTP_FORBIDDEN;
3424 r->canonical_filename = r->filename;
3430 /*****************************************************************
3432 * Test the filesystem name through directory_walk and file_walk
3434 static int core_map_to_storage(request_rec *r)
3438 if ((access_status = ap_directory_walk(r))) {
3439 return access_status;
3442 if ((access_status = ap_file_walk(r))) {
3443 return access_status;
3450 static int do_nothing(request_rec *r) { return OK; }
3453 static int core_override_type(request_rec *r)
3455 core_dir_config *conf =
3456 (core_dir_config *)ap_get_module_config(r->per_dir_config,
3459 /* Check for overrides with ForceType / SetHandler
3461 if (conf->mime_type && strcmp(conf->mime_type, "none"))
3462 ap_set_content_type(r, (char*) conf->mime_type);
3464 if (conf->handler && strcmp(conf->handler, "none"))
3465 r->handler = conf->handler;
3467 /* Deal with the poor soul who is trying to force path_info to be
3468 * accepted within the core_handler, where they will let the subreq
3469 * address its contents. This is toggled by the user in the very
3470 * beginning of the fixup phase, so modules should override the user's
3471 * discretion in their own module fixup phase. It is tristate, if
3472 * the user doesn't specify, the result is 2 (which the module may
3473 * interpret to its own customary behavior.) It won't be touched
3474 * if the value is no longer undefined (2), so any module changing
3475 * the value prior to the fixup phase OVERRIDES the user's choice.
3477 if ((r->used_path_info == AP_REQ_DEFAULT_PATH_INFO)
3478 && (conf->accept_path_info != 3)) {
3479 r->used_path_info = conf->accept_path_info;
3485 static int default_handler(request_rec *r)
3487 conn_rec *c = r->connection;
3488 apr_bucket_brigade *bb;
3492 apr_file_t *fd = NULL;
3493 apr_status_t status;
3494 /* XXX if/when somebody writes a content-md5 filter we either need to
3495 * remove this support or coordinate when to use the filter vs.
3496 * when to use this code
3497 * The current choice of when to compute the md5 here matches the 1.3
3498 * support fairly closely (unlike 1.3, we don't handle computing md5
3499 * when the charset is translated).
3501 int bld_content_md5;
3503 d = (core_dir_config *)ap_get_module_config(r->per_dir_config,
3505 bld_content_md5 = (d->content_md5 & 1)
3506 && r->output_filters->frec->ftype != AP_FTYPE_RESOURCE;
3508 ap_allow_standard_methods(r, MERGE_ALLOW, M_GET, M_OPTIONS, M_POST, -1);
3510 /* If filters intend to consume the request body, they must
3511 * register an InputFilter to slurp the contents of the POST
3512 * data from the POST input stream. It no longer exists when
3513 * the output filters are invoked by the default handler.
3515 if ((errstatus = ap_discard_request_body(r)) != OK) {
3519 if (r->method_number == M_GET || r->method_number == M_POST) {
3520 if (r->finfo.filetype == 0) {
3521 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
3522 "File does not exist: %s", r->filename);
3523 return HTTP_NOT_FOUND;
3526 /* Don't try to serve a dir. Some OSs do weird things with
3529 if (r->finfo.filetype == APR_DIR) {
3530 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
3531 "Attempt to serve directory: %s", r->filename);
3532 return HTTP_NOT_FOUND;
3535 if ((r->used_path_info != AP_REQ_ACCEPT_PATH_INFO) &&
3536 r->path_info && *r->path_info)
3538 /* default to reject */
3539 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
3540 "File does not exist: %s",
3541 apr_pstrcat(r->pool, r->filename, r->path_info, NULL));
3542 return HTTP_NOT_FOUND;
3545 /* We understood the (non-GET) method, but it might not be legal for
3546 this particular resource. Check to see if the 'deliver_script'
3547 flag is set. If so, then we go ahead and deliver the file since
3548 it isn't really content (only GET normally returns content).
3550 Note: based on logic further above, the only possible non-GET
3551 method at this point is POST. In the future, we should enable
3552 script delivery for all methods. */
3553 if (r->method_number != M_GET) {
3554 core_request_config *req_cfg;
3556 req_cfg = ap_get_module_config(r->request_config, &core_module);
3557 if (!req_cfg->deliver_script) {
3558 /* The flag hasn't been set for this request. Punt. */
3559 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
3560 "This resource does not accept the %s method.",
3562 return HTTP_METHOD_NOT_ALLOWED;
3567 if ((status = apr_file_open(&fd, r->filename, APR_READ | APR_BINARY
3568 #if APR_HAS_SENDFILE
3569 | ((d->enable_sendfile == ENABLE_SENDFILE_OFF)
3570 ? 0 : APR_SENDFILE_ENABLED)
3572 , 0, r->pool)) != APR_SUCCESS) {
3573 ap_log_rerror(APLOG_MARK, APLOG_ERR, status, r,
3574 "file permissions deny server access: %s", r->filename);
3575 return HTTP_FORBIDDEN;
3578 ap_update_mtime(r, r->finfo.mtime);
3579 ap_set_last_modified(r);
3581 apr_table_setn(r->headers_out, "Accept-Ranges", "bytes");
3582 ap_set_content_length(r, r->finfo.size);
3584 bb = apr_brigade_create(r->pool, c->bucket_alloc);
3586 if ((errstatus = ap_meets_conditions(r)) != OK) {
3588 r->status = errstatus;
3591 if (bld_content_md5) {
3592 apr_table_setn(r->headers_out, "Content-MD5",
3593 ap_md5digest(r->pool, fd));
3596 e = apr_brigade_insert_file(bb, fd, 0, r->finfo.size, r->pool);
3599 if (d->enable_mmap == ENABLE_MMAP_OFF) {
3600 (void)apr_bucket_file_enable_mmap(e, 0);
3605 e = apr_bucket_eos_create(c->bucket_alloc);
3606 APR_BRIGADE_INSERT_TAIL(bb, e);
3608 status = ap_pass_brigade(r->output_filters, bb);
3609 if (status == APR_SUCCESS
3610 || r->status != HTTP_OK
3615 /* no way to know what type of error occurred */
3616 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, status, r,
3617 "default_handler: ap_pass_brigade returned %i",
3619 return HTTP_INTERNAL_SERVER_ERROR;
3622 else { /* unusual method (not GET or POST) */
3623 if (r->method_number == M_INVALID) {
3624 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
3625 "Invalid method in request %s", r->the_request);
3626 return HTTP_NOT_IMPLEMENTED;
3629 if (r->method_number == M_OPTIONS) {
3630 return ap_send_http_options(r);
3632 return HTTP_METHOD_NOT_ALLOWED;
3636 /* Optional function coming from mod_logio, used for logging of output
3639 APR_OPTIONAL_FN_TYPE(ap_logio_add_bytes_out) *logio_add_bytes_out;
3640 APR_OPTIONAL_FN_TYPE(authz_some_auth_required) *authz_ap_some_auth_required;
3642 static int core_post_config(apr_pool_t *pconf, apr_pool_t *plog, apr_pool_t *ptemp, server_rec *s)
3644 logio_add_bytes_out = APR_RETRIEVE_OPTIONAL_FN(ap_logio_add_bytes_out);
3645 ident_lookup = APR_RETRIEVE_OPTIONAL_FN(ap_ident_lookup);
3646 authz_ap_some_auth_required = APR_RETRIEVE_OPTIONAL_FN(authz_some_auth_required);
3647 authn_ap_auth_type = APR_RETRIEVE_OPTIONAL_FN(authn_ap_auth_type);
3648 authn_ap_auth_name = APR_RETRIEVE_OPTIONAL_FN(authn_ap_auth_name);
3651 ap_setup_make_content_type(pconf);
3655 static void core_insert_filter(request_rec *r)
3657 core_dir_config *conf = (core_dir_config *)
3658 ap_get_module_config(r->per_dir_config,
3660 const char *filter, *filters = conf->output_filters;
3663 while (*filters && (filter = ap_getword(r->pool, &filters, ';'))) {
3664 ap_add_output_filter(filter, NULL, r, r->connection);
3668 filters = conf->input_filters;
3670 while (*filters && (filter = ap_getword(r->pool, &filters, ';'))) {
3671 ap_add_input_filter(filter, NULL, r, r->connection);
3676 static apr_size_t num_request_notes = AP_NUM_STD_NOTES;
3678 static apr_status_t reset_request_notes(void *dummy)
3680 num_request_notes = AP_NUM_STD_NOTES;
3684 AP_DECLARE(apr_size_t) ap_register_request_note(void)
3686 apr_pool_cleanup_register(apr_hook_global_pool, NULL, reset_request_notes,
3687 apr_pool_cleanup_null);
3688 return num_request_notes++;
3691 AP_DECLARE(void **) ap_get_request_note(request_rec *r, apr_size_t note_num)
3693 core_request_config *req_cfg;
3695 if (note_num >= num_request_notes) {
3699 req_cfg = (core_request_config *)
3700 ap_get_module_config(r->request_config, &core_module);
3706 return &(req_cfg->notes[note_num]);
3709 static int core_create_req(request_rec *r)
3711 /* Alloc the config struct and the array of request notes in
3712 * a single block for efficiency
3714 core_request_config *req_cfg;
3716 req_cfg = apr_pcalloc(r->pool, sizeof(core_request_config) +
3717 sizeof(void *) * num_request_notes);
3718 req_cfg->notes = (void **)((char *)req_cfg + sizeof(core_request_config));
3720 /* ### temporarily enable script delivery as the default */
3721 req_cfg->deliver_script = 1;
3724 core_request_config *main_req_cfg = (core_request_config *)
3725 ap_get_module_config(r->main->request_config, &core_module);
3726 req_cfg->bb = main_req_cfg->bb;
3729 req_cfg->bb = apr_brigade_create(r->pool, r->connection->bucket_alloc);
3732 ap_set_module_config(r->request_config, &core_module, req_cfg);
3737 static int core_create_proxy_req(request_rec *r, request_rec *pr)
3739 return core_create_req(pr);
3742 static conn_rec *core_create_conn(apr_pool_t *ptrans, server_rec *server,
3743 apr_socket_t *csd, long id, void *sbh,
3744 apr_bucket_alloc_t *alloc)
3747 conn_rec *c = (conn_rec *) apr_pcalloc(ptrans, sizeof(conn_rec));
3750 (void)ap_update_child_status(c->sbh, SERVER_BUSY_READ, (request_rec *)NULL);
3752 /* Got a connection structure, so initialize what fields we can
3753 * (the rest are zeroed out by pcalloc).
3755 c->conn_config = ap_create_conn_config(ptrans);
3756 c->notes = apr_table_make(ptrans, 5);
3759 if ((rv = apr_socket_addr_get(&c->local_addr, APR_LOCAL, csd))
3761 ap_log_error(APLOG_MARK, APLOG_INFO, rv, server,
3762 "apr_socket_addr_get(APR_LOCAL)");
3763 apr_socket_close(csd);
3767 apr_sockaddr_ip_get(&c->local_ip, c->local_addr);
3768 if ((rv = apr_socket_addr_get(&c->remote_addr, APR_REMOTE, csd))
3770 ap_log_error(APLOG_MARK, APLOG_INFO, rv, server,
3771 "apr_socket_addr_get(APR_REMOTE)");
3772 apr_socket_close(csd);
3776 apr_sockaddr_ip_get(&c->remote_ip, c->remote_addr);
3777 c->base_server = server;
3780 c->bucket_alloc = alloc;
3782 c->cs = (conn_state_t *)apr_pcalloc(ptrans, sizeof(conn_state_t));
3783 APR_RING_INIT(&(c->cs->timeout_list), conn_state_t, timeout_list);
3784 c->cs->expiration_time = 0;
3785 c->cs->state = CONN_STATE_CHECK_REQUEST_LINE_READABLE;
3788 c->cs->bucket_alloc = alloc;
3793 static int core_pre_connection(conn_rec *c, void *csd)
3795 core_net_rec *net = apr_palloc(c->pool, sizeof(*net));
3798 #ifdef AP_MPM_DISABLE_NAGLE_ACCEPTED_SOCK
3799 /* The Nagle algorithm says that we should delay sending partial
3800 * packets in hopes of getting more data. We don't want to do
3801 * this; we are not telnet. There are bad interactions between
3802 * persistent connections and Nagle's algorithm that have very severe
3803 * performance penalties. (Failing to disable Nagle is not much of a
3804 * problem with simple HTTP.)
3806 rv = apr_socket_opt_set(csd, APR_TCP_NODELAY, 1);
3807 if (rv != APR_SUCCESS && rv != APR_ENOTIMPL) {
3808 /* expected cause is that the client disconnected already,
3809 * hence the debug level
3811 ap_log_cerror(APLOG_MARK, APLOG_DEBUG, rv, c,
3812 "apr_socket_opt_set(APR_TCP_NODELAY)");
3816 /* The core filter requires the timeout mode to be set, which
3817 * incidentally sets the socket to be nonblocking. If this
3818 * is not initialized correctly, Linux - for example - will
3819 * be initially blocking, while Solaris will be non blocking
3820 * and any initial read will fail.
3822 rv = apr_socket_timeout_set(csd, c->base_server->timeout);
3823 if (rv != APR_SUCCESS) {
3824 /* expected cause is that the client disconnected already */
3825 ap_log_cerror(APLOG_MARK, APLOG_DEBUG, rv, c,
3826 "apr_socket_timeout_set");
3831 net->out_ctx = NULL;
3832 net->client_socket = csd;
3834 ap_set_module_config(net->c->conn_config, &core_module, csd);
3835 ap_add_input_filter_handle(ap_core_input_filter_handle, net, NULL, net->c);
3836 ap_add_output_filter_handle(ap_core_output_filter_handle, net, NULL, net->c);
3840 static void register_hooks(apr_pool_t *p)
3842 /* create_connection and install_transport_filters are
3843 * hooks that should always be APR_HOOK_REALLY_LAST to give other
3844 * modules the opportunity to install alternate network transports
3845 * and stop other functions from being run.
3847 ap_hook_create_connection(core_create_conn, NULL, NULL,
3848 APR_HOOK_REALLY_LAST);
3849 ap_hook_pre_connection(core_pre_connection, NULL, NULL,
3850 APR_HOOK_REALLY_LAST);
3852 ap_hook_post_config(core_post_config,NULL,NULL,APR_HOOK_REALLY_FIRST);
3853 ap_hook_translate_name(ap_core_translate,NULL,NULL,APR_HOOK_REALLY_LAST);
3854 ap_hook_map_to_storage(core_map_to_storage,NULL,NULL,APR_HOOK_REALLY_LAST);
3855 ap_hook_open_logs(ap_open_logs,NULL,NULL,APR_HOOK_REALLY_FIRST);
3856 ap_hook_child_init(ap_logs_child_init,NULL,NULL,APR_HOOK_MIDDLE);
3857 ap_hook_handler(default_handler,NULL,NULL,APR_HOOK_REALLY_LAST);
3858 /* FIXME: I suspect we can eliminate the need for these do_nothings - Ben */
3859 ap_hook_type_checker(do_nothing,NULL,NULL,APR_HOOK_REALLY_LAST);
3860 ap_hook_fixups(core_override_type,NULL,NULL,APR_HOOK_REALLY_FIRST);
3861 ap_hook_access_checker(do_nothing,NULL,NULL,APR_HOOK_REALLY_LAST);
3862 ap_hook_create_request(core_create_req, NULL, NULL, APR_HOOK_MIDDLE);
3863 APR_OPTIONAL_HOOK(proxy, create_req, core_create_proxy_req, NULL, NULL,
3865 ap_hook_pre_mpm(ap_create_scoreboard, NULL, NULL, APR_HOOK_MIDDLE);
3867 /* register the core's insert_filter hook and register core-provided
3870 ap_hook_insert_filter(core_insert_filter, NULL, NULL, APR_HOOK_MIDDLE);
3872 ap_core_input_filter_handle =
3873 ap_register_input_filter("CORE_IN", ap_core_input_filter,
3874 NULL, AP_FTYPE_NETWORK);
3875 ap_content_length_filter_handle =
3876 ap_register_output_filter("CONTENT_LENGTH", ap_content_length_filter,
3877 NULL, AP_FTYPE_PROTOCOL);
3878 ap_core_output_filter_handle =
3879 ap_register_output_filter("CORE", ap_core_output_filter,
3880 NULL, AP_FTYPE_NETWORK);
3881 ap_subreq_core_filter_handle =
3882 ap_register_output_filter("SUBREQ_CORE", ap_sub_req_output_filter,
3883 NULL, AP_FTYPE_CONTENT_SET);
3885 ap_register_output_filter("OLD_WRITE", ap_old_write_filter,
3886 NULL, AP_FTYPE_RESOURCE - 10);
3889 AP_DECLARE_DATA module core_module = {
3890 STANDARD20_MODULE_STUFF,
3891 create_core_dir_config, /* create per-directory config structure */
3892 merge_core_dir_configs, /* merge per-directory config structures */
3893 create_core_server_config, /* create per-server config structure */
3894 merge_core_server_configs, /* merge per-server config structures */
3895 core_cmds, /* command apr_table_t */
3896 register_hooks /* register hooks */