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_pmemdup(a, base, sizeof(core_dir_config));
229 conf->d_is_fnmatch = new->d_is_fnmatch;
230 conf->d_components = new->d_components;
233 if (new->opts & OPT_UNSET) {
234 /* there was no explicit setting of new->opts, so we merge
235 * preserve the invariant (opts_add & opts_remove) == 0
237 conf->opts_add = (conf->opts_add & ~new->opts_remove) | new->opts_add;
238 conf->opts_remove = (conf->opts_remove & ~new->opts_add)
240 conf->opts = (conf->opts & ~conf->opts_remove) | conf->opts_add;
241 if ((base->opts & OPT_INCNOEXEC) && (new->opts & OPT_INCLUDES)) {
242 conf->opts = (conf->opts & ~OPT_INCNOEXEC) | OPT_INCLUDES;
246 /* otherwise we just copy, because an explicit opts setting
247 * overrides all earlier +/- modifiers
249 conf->opts = new->opts;
250 conf->opts_add = new->opts_add;
251 conf->opts_remove = new->opts_remove;
254 if (!(new->override & OR_UNSET)) {
255 conf->override = new->override;
258 if (!(new->override_opts & OPT_UNSET)) {
259 conf->override_opts = new->override_opts;
262 if (new->ap_default_type) {
263 conf->ap_default_type = new->ap_default_type;
266 if (conf->response_code_strings == NULL) {
267 conf->response_code_strings = new->response_code_strings;
269 else if (new->response_code_strings != NULL) {
270 /* If we merge, the merge-result must have it's own array
272 conf->response_code_strings = apr_pmemdup(a,
273 base->response_code_strings,
274 sizeof(*conf->response_code_strings) * RESPONSE_CODES);
276 for (i = 0; i < RESPONSE_CODES; ++i) {
277 if (new->response_code_strings[i] != NULL) {
278 conf->response_code_strings[i] = new->response_code_strings[i];
282 /* Otherwise we simply use the base->response_code_strings array
285 if (new->hostname_lookups != HOSTNAME_LOOKUP_UNSET) {
286 conf->hostname_lookups = new->hostname_lookups;
289 if ((new->content_md5 & 2) == 0) {
290 conf->content_md5 = new->content_md5;
293 if (new->accept_path_info != 3) {
294 conf->accept_path_info = new->accept_path_info;
297 if (new->use_canonical_name != USE_CANONICAL_NAME_UNSET) {
298 conf->use_canonical_name = new->use_canonical_name;
301 if (new->use_canonical_phys_port != USE_CANONICAL_PHYS_PORT_UNSET) {
302 conf->use_canonical_phys_port = new->use_canonical_phys_port;
306 if (new->limit_cpu) {
307 conf->limit_cpu = new->limit_cpu;
311 #if defined(RLIMIT_DATA) || defined(RLIMIT_VMEM) || defined(RLIMIT_AS)
312 if (new->limit_mem) {
313 conf->limit_mem = new->limit_mem;
318 if (new->limit_nproc) {
319 conf->limit_nproc = new->limit_nproc;
323 if (new->limit_req_body != AP_LIMIT_REQ_BODY_UNSET) {
324 conf->limit_req_body = new->limit_req_body;
327 if (new->limit_xml_body != AP_LIMIT_UNSET)
328 conf->limit_xml_body = new->limit_xml_body;
330 conf->limit_xml_body = base->limit_xml_body;
332 if (!conf->sec_file) {
333 conf->sec_file = new->sec_file;
335 else if (new->sec_file) {
336 /* If we merge, the merge-result must have it's own array
338 conf->sec_file = apr_array_append(a, base->sec_file, new->sec_file);
340 /* Otherwise we simply use the base->sec_file array
343 if (new->server_signature != srv_sig_unset) {
344 conf->server_signature = new->server_signature;
347 if (new->add_default_charset != ADD_DEFAULT_CHARSET_UNSET) {
348 conf->add_default_charset = new->add_default_charset;
349 conf->add_default_charset_name = new->add_default_charset_name;
352 /* Overriding all negotiation
354 if (new->mime_type) {
355 conf->mime_type = new->mime_type;
359 conf->handler = new->handler;
362 if (new->output_filters) {
363 conf->output_filters = new->output_filters;
366 if (new->input_filters) {
367 conf->input_filters = new->input_filters;
370 if (conf->ct_output_filters && new->ct_output_filters) {
371 conf->ct_output_filters = apr_hash_merge(a,
372 new->ct_output_filters,
373 conf->ct_output_filters,
377 else if (new->ct_output_filters) {
378 conf->ct_output_filters = apr_hash_copy(a, new->ct_output_filters);
380 else if (conf->ct_output_filters) {
381 /* That memcpy above isn't enough. */
382 conf->ct_output_filters = apr_hash_copy(a, base->ct_output_filters);
386 * Now merge the setting of the FileETag directive.
388 if (new->etag_bits == ETAG_UNSET) {
390 (conf->etag_add & (~ new->etag_remove)) | new->etag_add;
392 (conf->etag_remove & (~ new->etag_add)) | new->etag_remove;
394 (conf->etag_bits & (~ conf->etag_remove)) | conf->etag_add;
397 conf->etag_bits = new->etag_bits;
398 conf->etag_add = new->etag_add;
399 conf->etag_remove = new->etag_remove;
402 if (conf->etag_bits != ETAG_NONE) {
403 conf->etag_bits &= (~ ETAG_NONE);
406 if (new->enable_mmap != ENABLE_MMAP_UNSET) {
407 conf->enable_mmap = new->enable_mmap;
410 if (new->enable_sendfile != ENABLE_SENDFILE_UNSET) {
411 conf->enable_sendfile = new->enable_sendfile;
414 conf->allow_encoded_slashes = new->allow_encoded_slashes;
419 static void *create_core_server_config(apr_pool_t *a, server_rec *s)
421 core_server_config *conf;
422 int is_virtual = s->is_virtual;
424 conf = (core_server_config *)apr_pcalloc(a, sizeof(core_server_config));
427 conf->gprof_dir = NULL;
430 conf->access_name = is_virtual ? NULL : DEFAULT_ACCESS_FNAME;
431 conf->ap_document_root = is_virtual ? NULL : DOCUMENT_LOCATION;
432 conf->sec_dir = apr_array_make(a, 40, sizeof(ap_conf_vector_t *));
433 conf->sec_url = apr_array_make(a, 40, sizeof(ap_conf_vector_t *));
435 /* recursion stopper */
436 conf->redirect_limit = 0; /* 0 == unset */
437 conf->subreq_limit = 0;
439 conf->protocol = NULL;
440 conf->accf_map = apr_table_make(a, 5);
442 #ifdef APR_TCP_DEFER_ACCEPT
443 apr_table_set(conf->accf_map, "http", "data");
444 apr_table_set(conf->accf_map, "https", "data");
447 #if APR_HAS_SO_ACCEPTFILTER
448 #ifndef ACCEPT_FILTER_NAME
449 #define ACCEPT_FILTER_NAME "httpready"
450 #ifdef __FreeBSD_version
451 #if __FreeBSD_version < 411000 /* httpready broken before 4.1.1 */
452 #undef ACCEPT_FILTER_NAME
453 #define ACCEPT_FILTER_NAME "dataready"
457 apr_table_set(conf->accf_map, "http", ACCEPT_FILTER_NAME);
458 apr_table_set(conf->accf_map, "https", "dataready");
461 conf->trace_enable = AP_TRACE_UNSET;
466 static void *merge_core_server_configs(apr_pool_t *p, void *basev, void *virtv)
468 core_server_config *base = (core_server_config *)basev;
469 core_server_config *virt = (core_server_config *)virtv;
470 core_server_config *conf;
472 conf = (core_server_config *)apr_pmemdup(p, virt, sizeof(core_server_config));
474 if (!conf->access_name) {
475 conf->access_name = base->access_name;
478 if (!conf->ap_document_root) {
479 conf->ap_document_root = base->ap_document_root;
482 if (!conf->protocol) {
483 conf->protocol = base->protocol;
486 conf->sec_dir = apr_array_append(p, base->sec_dir, virt->sec_dir);
487 conf->sec_url = apr_array_append(p, base->sec_url, virt->sec_url);
489 conf->redirect_limit = virt->redirect_limit
490 ? virt->redirect_limit
491 : base->redirect_limit;
493 conf->subreq_limit = virt->subreq_limit
495 : base->subreq_limit;
497 conf->trace_enable = (virt->trace_enable != AP_TRACE_UNSET)
499 : base->trace_enable;
504 /* Add per-directory configuration entry (for <directory> section);
505 * these are part of the core server config.
508 AP_CORE_DECLARE(void) ap_add_per_dir_conf(server_rec *s, void *dir_config)
510 core_server_config *sconf = ap_get_module_config(s->module_config,
512 void **new_space = (void **)apr_array_push(sconf->sec_dir);
514 *new_space = dir_config;
517 AP_CORE_DECLARE(void) ap_add_per_url_conf(server_rec *s, void *url_config)
519 core_server_config *sconf = ap_get_module_config(s->module_config,
521 void **new_space = (void **)apr_array_push(sconf->sec_url);
523 *new_space = url_config;
526 AP_CORE_DECLARE(void) ap_add_file_conf(core_dir_config *conf, void *url_config)
528 void **new_space = (void **)apr_array_push(conf->sec_file);
530 *new_space = url_config;
533 /* We need to do a stable sort, qsort isn't stable. So to make it stable
534 * we'll be maintaining the original index into the list, and using it
535 * as the minor key during sorting. The major key is the number of
536 * components (where the root component is zero).
538 struct reorder_sort_rec {
539 ap_conf_vector_t *elt;
543 static int reorder_sorter(const void *va, const void *vb)
545 const struct reorder_sort_rec *a = va;
546 const struct reorder_sort_rec *b = vb;
547 core_dir_config *core_a;
548 core_dir_config *core_b;
550 core_a = ap_get_module_config(a->elt, &core_module);
551 core_b = ap_get_module_config(b->elt, &core_module);
553 /* a regex always sorts after a non-regex
555 if (!core_a->r && core_b->r) {
558 else if (core_a->r && !core_b->r) {
562 /* we always sort next by the number of components
564 if (core_a->d_components < core_b->d_components) {
567 else if (core_a->d_components > core_b->d_components) {
571 /* They have the same number of components, we now have to compare
572 * the minor key to maintain the original order (from the config.)
574 return a->orig_index - b->orig_index;
577 void ap_core_reorder_directories(apr_pool_t *p, server_rec *s)
579 core_server_config *sconf;
580 apr_array_header_t *sec_dir;
581 struct reorder_sort_rec *sortbin;
583 ap_conf_vector_t **elts;
587 sconf = ap_get_module_config(s->module_config, &core_module);
588 sec_dir = sconf->sec_dir;
589 nelts = sec_dir->nelts;
590 elts = (ap_conf_vector_t **)sec_dir->elts;
593 /* simple case of already being sorted... */
594 /* We're not checking this condition to be fast... we're checking
595 * it to avoid trying to palloc zero bytes, which can trigger some
596 * memory debuggers to barf
601 /* we have to allocate tmp space to do a stable sort */
602 apr_pool_create(&tmp, p);
603 sortbin = apr_palloc(tmp, sec_dir->nelts * sizeof(*sortbin));
604 for (i = 0; i < nelts; ++i) {
605 sortbin[i].orig_index = i;
606 sortbin[i].elt = elts[i];
609 qsort(sortbin, nelts, sizeof(*sortbin), reorder_sorter);
611 /* and now copy back to the original array */
612 for (i = 0; i < nelts; ++i) {
613 elts[i] = sortbin[i].elt;
616 apr_pool_destroy(tmp);
619 /*****************************************************************
621 * There are some elements of the core config structures in which
622 * other modules have a legitimate interest (this is ugly, but necessary
623 * to preserve NCSA back-compatibility). So, we have a bunch of accessors
627 AP_DECLARE(int) ap_allow_options(request_rec *r)
629 core_dir_config *conf =
630 (core_dir_config *)ap_get_module_config(r->per_dir_config, &core_module);
635 AP_DECLARE(int) ap_allow_overrides(request_rec *r)
637 core_dir_config *conf;
638 conf = (core_dir_config *)ap_get_module_config(r->per_dir_config,
641 return conf->override;
645 * Optional function coming from mod_authn_core, used for
646 * retrieving the type of autorization
648 static APR_OPTIONAL_FN_TYPE(authn_ap_auth_type) *authn_ap_auth_type;
650 AP_DECLARE(const char *) ap_auth_type(request_rec *r)
652 if (authn_ap_auth_type) {
653 return authn_ap_auth_type(r);
659 * Optional function coming from mod_authn_core, used for
660 * retrieving the authorization realm
662 static APR_OPTIONAL_FN_TYPE(authn_ap_auth_name) *authn_ap_auth_name;
664 AP_DECLARE(const char *) ap_auth_name(request_rec *r)
666 if (authn_ap_auth_name) {
667 return authn_ap_auth_name(r);
673 * Optional function coming from mod_access_compat, used to determine how
674 access control interacts with authentication/authorization
676 static APR_OPTIONAL_FN_TYPE(access_compat_ap_satisfies) *access_compat_ap_satisfies;
678 AP_DECLARE(int) ap_satisfies(request_rec *r)
680 if (access_compat_ap_satisfies) {
681 return access_compat_ap_satisfies(r);
683 return SATISFY_NOSPEC;
686 AP_DECLARE(const char *) ap_default_type(request_rec *r)
688 core_dir_config *conf;
690 conf = (core_dir_config *)ap_get_module_config(r->per_dir_config,
693 return conf->ap_default_type
694 ? conf->ap_default_type
695 : DEFAULT_CONTENT_TYPE;
698 AP_DECLARE(const char *) ap_document_root(request_rec *r) /* Don't use this! */
700 core_server_config *conf;
702 conf = (core_server_config *)ap_get_module_config(r->server->module_config,
705 return conf->ap_document_root;
708 /* Should probably just get rid of this... the only code that cares is
709 * part of the core anyway (and in fact, it isn't publicised to other
713 char *ap_response_code_string(request_rec *r, int error_index)
715 core_dir_config *dirconf;
716 core_request_config *reqconf;
718 /* check for string registered via ap_custom_response() first */
719 reqconf = (core_request_config *)ap_get_module_config(r->request_config,
721 if (reqconf->response_code_strings != NULL &&
722 reqconf->response_code_strings[error_index] != NULL) {
723 return reqconf->response_code_strings[error_index];
726 /* check for string specified via ErrorDocument */
727 dirconf = (core_dir_config *)ap_get_module_config(r->per_dir_config,
730 if (dirconf->response_code_strings == NULL) {
734 if (dirconf->response_code_strings[error_index] == &errordocument_default) {
738 return dirconf->response_code_strings[error_index];
742 /* Code from Harald Hanche-Olsen <hanche@imf.unit.no> */
743 static APR_INLINE void do_double_reverse (conn_rec *conn)
748 if (conn->double_reverse) {
753 if (conn->remote_host == NULL || conn->remote_host[0] == '\0') {
754 /* single reverse failed, so don't bother */
755 conn->double_reverse = -1;
759 rv = apr_sockaddr_info_get(&sa, conn->remote_host, APR_UNSPEC, 0, 0, conn->pool);
760 if (rv == APR_SUCCESS) {
762 if (apr_sockaddr_equal(sa, conn->remote_addr)) {
763 conn->double_reverse = 1;
771 conn->double_reverse = -1;
774 AP_DECLARE(const char *) ap_get_remote_host(conn_rec *conn, void *dir_config,
775 int type, int *str_is_ip)
777 int hostname_lookups;
778 int ignored_str_is_ip;
780 if (!str_is_ip) { /* caller doesn't want to know */
781 str_is_ip = &ignored_str_is_ip;
785 /* If we haven't checked the host name, and we want to */
788 ((core_dir_config *)ap_get_module_config(dir_config, &core_module))
791 if (hostname_lookups == HOSTNAME_LOOKUP_UNSET) {
792 hostname_lookups = HOSTNAME_LOOKUP_OFF;
797 hostname_lookups = HOSTNAME_LOOKUP_OFF;
800 if (type != REMOTE_NOLOOKUP
801 && conn->remote_host == NULL
802 && (type == REMOTE_DOUBLE_REV
803 || hostname_lookups != HOSTNAME_LOOKUP_OFF)) {
805 if (apr_getnameinfo(&conn->remote_host, conn->remote_addr, 0)
807 ap_str_tolower(conn->remote_host);
809 if (hostname_lookups == HOSTNAME_LOOKUP_DOUBLE) {
810 do_double_reverse(conn);
811 if (conn->double_reverse != 1) {
812 conn->remote_host = NULL;
817 /* if failed, set it to the NULL string to indicate error */
818 if (conn->remote_host == NULL) {
819 conn->remote_host = "";
823 if (type == REMOTE_DOUBLE_REV) {
824 do_double_reverse(conn);
825 if (conn->double_reverse == -1) {
831 * Return the desired information; either the remote DNS name, if found,
832 * or either NULL (if the hostname was requested) or the IP address
833 * (if any identifier was requested).
835 if (conn->remote_host != NULL && conn->remote_host[0] != '\0') {
836 return conn->remote_host;
839 if (type == REMOTE_HOST || type == REMOTE_DOUBLE_REV) {
844 return conn->remote_ip;
850 * Optional function coming from mod_ident, used for looking up ident user
852 static APR_OPTIONAL_FN_TYPE(ap_ident_lookup) *ident_lookup;
854 AP_DECLARE(const char *) ap_get_remote_logname(request_rec *r)
856 if (r->connection->remote_logname != NULL) {
857 return r->connection->remote_logname;
861 return ident_lookup(r);
867 /* There are two options regarding what the "name" of a server is. The
868 * "canonical" name as defined by ServerName and Port, or the "client's
869 * name" as supplied by a possible Host: header or full URI.
871 * The DNS option to UseCanonicalName causes this routine to do a
872 * reverse lookup on the local IP address of the connection and use
873 * that for the ServerName. This makes its value more reliable while
874 * at the same time allowing Demon's magic virtual hosting to work.
875 * The assumption is that DNS lookups are sufficiently quick...
878 AP_DECLARE(const char *) ap_get_server_name(request_rec *r)
880 conn_rec *conn = r->connection;
884 d = (core_dir_config *)ap_get_module_config(r->per_dir_config,
887 switch (d->use_canonical_name) {
888 case USE_CANONICAL_NAME_ON:
889 retval = r->server->server_hostname;
891 case USE_CANONICAL_NAME_DNS:
892 if (conn->local_host == NULL) {
893 if (apr_getnameinfo(&conn->local_host,
894 conn->local_addr, 0) != APR_SUCCESS)
895 conn->local_host = apr_pstrdup(conn->pool,
896 r->server->server_hostname);
898 ap_str_tolower(conn->local_host);
901 retval = conn->local_host;
903 case USE_CANONICAL_NAME_OFF:
904 case USE_CANONICAL_NAME_UNSET:
905 retval = r->hostname ? r->hostname : r->server->server_hostname;
908 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
909 "ap_get_server_name: Invalid UCN Option somehow");
910 retval = "localhost";
917 * Get the current server name from the request for the purposes
918 * of using in a URL. If the server name is an IPv6 literal
919 * address, it will be returned in URL format (e.g., "[fe80::1]").
921 static const char *get_server_name_for_url(request_rec *r)
923 const char *plain_server_name = ap_get_server_name(r);
926 if (ap_strchr_c(plain_server_name, ':')) { /* IPv6 literal? */
927 return apr_psprintf(r->pool, "[%s]", plain_server_name);
930 return plain_server_name;
933 AP_DECLARE(apr_port_t) ap_get_server_port(const request_rec *r)
937 (core_dir_config *)ap_get_module_config(r->per_dir_config, &core_module);
939 switch (d->use_canonical_name) {
940 case USE_CANONICAL_NAME_OFF:
941 case USE_CANONICAL_NAME_DNS:
942 case USE_CANONICAL_NAME_UNSET:
943 if (d->use_canonical_phys_port == USE_CANONICAL_PHYS_PORT_ON)
944 port = r->parsed_uri.port_str ? r->parsed_uri.port :
945 r->connection->local_addr->port ? r->connection->local_addr->port :
946 r->server->port ? r->server->port :
948 else /* USE_CANONICAL_PHYS_PORT_OFF or USE_CANONICAL_PHYS_PORT_UNSET */
949 port = r->parsed_uri.port_str ? r->parsed_uri.port :
950 r->server->port ? r->server->port :
953 case USE_CANONICAL_NAME_ON:
954 /* With UseCanonicalName on (and in all versions prior to 1.3)
955 * Apache will use the hostname and port specified in the
956 * ServerName directive to construct a canonical name for the
957 * server. (If no port was specified in the ServerName
958 * directive, Apache uses the port supplied by the client if
959 * any is supplied, and finally the default port for the protocol
962 if (d->use_canonical_phys_port == USE_CANONICAL_PHYS_PORT_ON)
963 port = r->server->port ? r->server->port :
964 r->connection->local_addr->port ? r->connection->local_addr->port :
966 else /* USE_CANONICAL_PHYS_PORT_OFF or USE_CANONICAL_PHYS_PORT_UNSET */
967 port = r->server->port ? r->server->port :
971 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
972 "ap_get_server_port: Invalid UCN Option somehow");
973 port = ap_default_port(r);
980 AP_DECLARE(char *) ap_construct_url(apr_pool_t *p, const char *uri,
983 unsigned port = ap_get_server_port(r);
984 const char *host = get_server_name_for_url(r);
986 if (ap_is_default_port(port, r)) {
987 return apr_pstrcat(p, ap_http_scheme(r), "://", host, uri, NULL);
990 return apr_psprintf(p, "%s://%s:%u%s", ap_http_scheme(r), host, port, uri);
993 AP_DECLARE(apr_off_t) ap_get_limit_req_body(const request_rec *r)
996 (core_dir_config *)ap_get_module_config(r->per_dir_config, &core_module);
998 if (d->limit_req_body == AP_LIMIT_REQ_BODY_UNSET) {
999 return AP_DEFAULT_LIMIT_REQ_BODY;
1002 return d->limit_req_body;
1006 /*****************************************************************
1008 * Commands... this module handles almost all of the NCSA httpd.conf
1009 * commands, but most of the old srm.conf is in the the modules.
1013 /* returns a parent if it matches the given directive */
1014 static const ap_directive_t * find_parent(const ap_directive_t *dirp,
1017 while (dirp->parent != NULL) {
1018 dirp = dirp->parent;
1020 /* ### it would be nice to have atom-ized directives */
1021 if (strcasecmp(dirp->directive, what) == 0)
1028 AP_DECLARE(const char *) ap_check_cmd_context(cmd_parms *cmd,
1031 const char *gt = (cmd->cmd->name[0] == '<'
1032 && cmd->cmd->name[strlen(cmd->cmd->name)-1] != '>')
1034 const ap_directive_t *found;
1036 if ((forbidden & NOT_IN_VIRTUALHOST) && cmd->server->is_virtual) {
1037 return apr_pstrcat(cmd->pool, cmd->cmd->name, gt,
1038 " cannot occur within <VirtualHost> section", NULL);
1041 if ((forbidden & NOT_IN_LIMIT) && cmd->limited != -1) {
1042 return apr_pstrcat(cmd->pool, cmd->cmd->name, gt,
1043 " cannot occur within <Limit> section", NULL);
1046 if ((forbidden & NOT_IN_DIR_LOC_FILE) == NOT_IN_DIR_LOC_FILE) {
1047 if (cmd->path != NULL) {
1048 return apr_pstrcat(cmd->pool, cmd->cmd->name, gt,
1049 " cannot occur within <Directory/Location/Files> "
1052 if (cmd->cmd->req_override & EXEC_ON_READ) {
1053 /* EXEC_ON_READ must be NOT_IN_DIR_LOC_FILE, if not, it will
1054 * (deliberately) segfault below in the individual tests...
1060 if (((forbidden & NOT_IN_DIRECTORY)
1061 && ((found = find_parent(cmd->directive, "<Directory"))
1062 || (found = find_parent(cmd->directive, "<DirectoryMatch"))))
1063 || ((forbidden & NOT_IN_LOCATION)
1064 && ((found = find_parent(cmd->directive, "<Location"))
1065 || (found = find_parent(cmd->directive, "<LocationMatch"))))
1066 || ((forbidden & NOT_IN_FILES)
1067 && ((found = find_parent(cmd->directive, "<Files"))
1068 || (found = find_parent(cmd->directive, "<FilesMatch"))))) {
1069 return apr_pstrcat(cmd->pool, cmd->cmd->name, gt,
1070 " cannot occur within ", found->directive,
1077 static const char *set_access_name(cmd_parms *cmd, void *dummy,
1080 void *sconf = cmd->server->module_config;
1081 core_server_config *conf = ap_get_module_config(sconf, &core_module);
1083 const char *err = ap_check_cmd_context(cmd,
1084 NOT_IN_DIR_LOC_FILE|NOT_IN_LIMIT);
1089 conf->access_name = apr_pstrdup(cmd->pool, arg);
1094 static const char *set_define(cmd_parms *cmd, void *dummy,
1099 const char *err = ap_check_cmd_context(cmd,
1105 newv = (char **)apr_array_push(ap_server_config_defines);
1106 *newv = apr_pstrdup(cmd->pool, optarg);
1112 static const char *set_gprof_dir(cmd_parms *cmd, void *dummy, const char *arg)
1114 void *sconf = cmd->server->module_config;
1115 core_server_config *conf = ap_get_module_config(sconf, &core_module);
1117 const char *err = ap_check_cmd_context(cmd,
1118 NOT_IN_DIR_LOC_FILE|NOT_IN_LIMIT);
1123 conf->gprof_dir = apr_pstrdup(cmd->pool, arg);
1128 static const char *set_add_default_charset(cmd_parms *cmd,
1129 void *d_, const char *arg)
1131 core_dir_config *d = d_;
1133 const char *err = ap_check_cmd_context(cmd, NOT_IN_LIMIT);
1138 if (!strcasecmp(arg, "Off")) {
1139 d->add_default_charset = ADD_DEFAULT_CHARSET_OFF;
1141 else if (!strcasecmp(arg, "On")) {
1142 d->add_default_charset = ADD_DEFAULT_CHARSET_ON;
1143 d->add_default_charset_name = DEFAULT_ADD_DEFAULT_CHARSET_NAME;
1146 d->add_default_charset = ADD_DEFAULT_CHARSET_ON;
1147 d->add_default_charset_name = arg;
1153 static const char *set_document_root(cmd_parms *cmd, void *dummy,
1156 void *sconf = cmd->server->module_config;
1157 core_server_config *conf = ap_get_module_config(sconf, &core_module);
1159 const char *err = ap_check_cmd_context(cmd,
1160 NOT_IN_DIR_LOC_FILE|NOT_IN_LIMIT);
1165 /* Make it absolute, relative to ServerRoot */
1166 arg = ap_server_root_relative(cmd->pool, arg);
1168 return "DocumentRoot must be a directory";
1171 /* TODO: ap_configtestonly && ap_docrootcheck && */
1172 if (apr_filepath_merge((char**)&conf->ap_document_root, NULL, arg,
1173 APR_FILEPATH_TRUENAME, cmd->pool) != APR_SUCCESS
1174 || !ap_is_directory(cmd->pool, arg)) {
1175 if (cmd->server->is_virtual) {
1176 ap_log_perror(APLOG_MARK, APLOG_STARTUP, 0,
1178 "Warning: DocumentRoot [%s] does not exist",
1180 conf->ap_document_root = arg;
1183 return "DocumentRoot must be a directory";
1189 AP_DECLARE(void) ap_custom_response(request_rec *r, int status,
1192 core_request_config *conf =
1193 ap_get_module_config(r->request_config, &core_module);
1196 if (conf->response_code_strings == NULL) {
1197 conf->response_code_strings =
1198 apr_pcalloc(r->pool,
1199 sizeof(*conf->response_code_strings) * RESPONSE_CODES);
1202 idx = ap_index_of_response(status);
1204 conf->response_code_strings[idx] =
1205 ((ap_is_url(string) || (*string == '/')) && (*string != '"')) ?
1206 apr_pstrdup(r->pool, string) : apr_pstrcat(r->pool, "\"", string, NULL);
1209 static const char *set_error_document(cmd_parms *cmd, void *conf_,
1210 const char *errno_str, const char *msg)
1212 core_dir_config *conf = conf_;
1213 int error_number, index_number, idx500;
1214 enum { MSG, LOCAL_PATH, REMOTE_PATH } what = MSG;
1216 const char *err = ap_check_cmd_context(cmd, NOT_IN_LIMIT);
1221 /* 1st parameter should be a 3 digit number, which we recognize;
1222 * convert it into an array index
1224 error_number = atoi(errno_str);
1225 idx500 = ap_index_of_response(HTTP_INTERNAL_SERVER_ERROR);
1227 if (error_number == HTTP_INTERNAL_SERVER_ERROR) {
1228 index_number = idx500;
1230 else if ((index_number = ap_index_of_response(error_number)) == idx500) {
1231 return apr_pstrcat(cmd->pool, "Unsupported HTTP response code ",
1235 /* Heuristic to determine second argument. */
1236 if (ap_strchr_c(msg,' '))
1238 else if (msg[0] == '/')
1240 else if (ap_is_url(msg))
1245 /* The entry should be ignored if it is a full URL for a 401 error */
1247 if (error_number == 401 && what == REMOTE_PATH) {
1248 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, cmd->server,
1249 "cannot use a full URL in a 401 ErrorDocument "
1250 "directive --- ignoring!");
1252 else { /* Store it... */
1253 if (conf->response_code_strings == NULL) {
1254 conf->response_code_strings =
1255 apr_pcalloc(cmd->pool,
1256 sizeof(*conf->response_code_strings) *
1260 if (strcmp(msg, "default") == 0) {
1261 /* special case: ErrorDocument 404 default restores the
1262 * canned server error response
1264 conf->response_code_strings[index_number] = &errordocument_default;
1267 /* hack. Prefix a " if it is a msg; as that is what
1268 * http_protocol.c relies on to distinguish between
1269 * a msg and a (local) path.
1271 conf->response_code_strings[index_number] = (what == MSG) ?
1272 apr_pstrcat(cmd->pool, "\"",msg,NULL) :
1273 apr_pstrdup(cmd->pool, msg);
1280 static const char *set_allow_opts(cmd_parms *cmd, allow_options_t *opts,
1283 allow_options_t opt;
1286 char *w, *p = (char *) l;
1289 while ((w = apr_strtok(p, ",", &tok_state)) != NULL) {
1297 if (!strcasecmp(w, "Indexes")) {
1300 else if (!strcasecmp(w, "Includes")) {
1303 else if (!strcasecmp(w, "IncludesNOEXEC")) {
1304 opt = (OPT_INCLUDES | OPT_INCNOEXEC);
1306 else if (!strcasecmp(w, "FollowSymLinks")) {
1307 opt = OPT_SYM_LINKS;
1309 else if (!strcasecmp(w, "SymLinksIfOwnerMatch")) {
1310 opt = OPT_SYM_OWNER;
1312 else if (!strcasecmp(w, "ExecCGI")) {
1315 else if (!strcasecmp(w, "MultiViews")) {
1318 else if (!strcasecmp(w, "RunScripts")) { /* AI backcompat. Yuck */
1319 opt = OPT_MULTI|OPT_EXECCGI;
1321 else if (!strcasecmp(w, "None")) {
1324 else if (!strcasecmp(w, "All")) {
1328 return apr_pstrcat(cmd->pool, "Illegal option ", w, NULL);
1334 (*opts) &= (~OPT_UNSET);
1339 static const char *set_override(cmd_parms *cmd, void *d_, const char *l)
1341 core_dir_config *d = d_;
1345 const char *err = ap_check_cmd_context(cmd, NOT_IN_LIMIT);
1350 /* Throw a warning if we're in <Location> or <Files> */
1351 if (ap_check_cmd_context(cmd, NOT_IN_LOCATION | NOT_IN_FILES)) {
1352 ap_log_error(APLOG_MARK, APLOG_WARNING, 0, cmd->server,
1353 "Useless use of AllowOverride in line %d of %s.",
1354 cmd->directive->line_num, cmd->directive->filename);
1357 d->override = OR_NONE;
1359 w = ap_getword_conf(cmd->pool, &l);
1367 if (!strcasecmp(w, "Limit")) {
1368 d->override |= OR_LIMIT;
1370 else if (!strcasecmp(k, "Options")) {
1371 d->override |= OR_OPTIONS;
1373 set_allow_opts(cmd, &(d->override_opts), v);
1375 d->override_opts = OPT_ALL;
1377 else if (!strcasecmp(w, "FileInfo")) {
1378 d->override |= OR_FILEINFO;
1380 else if (!strcasecmp(w, "AuthConfig")) {
1381 d->override |= OR_AUTHCFG;
1383 else if (!strcasecmp(w, "Indexes")) {
1384 d->override |= OR_INDEXES;
1386 else if (!strcasecmp(w, "None")) {
1387 d->override = OR_NONE;
1389 else if (!strcasecmp(w, "All")) {
1390 d->override = OR_ALL;
1393 return apr_pstrcat(cmd->pool, "Illegal override option ", w, NULL);
1396 d->override &= ~OR_UNSET;
1402 static const char *set_options(cmd_parms *cmd, void *d_, const char *l)
1404 core_dir_config *d = d_;
1405 allow_options_t opt;
1410 char *w = ap_getword_conf(cmd->pool, &l);
1413 if (*w == '+' || *w == '-') {
1421 if (!strcasecmp(w, "Indexes")) {
1424 else if (!strcasecmp(w, "Includes")) {
1427 else if (!strcasecmp(w, "IncludesNOEXEC")) {
1428 opt = (OPT_INCLUDES | OPT_INCNOEXEC);
1430 else if (!strcasecmp(w, "FollowSymLinks")) {
1431 opt = OPT_SYM_LINKS;
1433 else if (!strcasecmp(w, "SymLinksIfOwnerMatch")) {
1434 opt = OPT_SYM_OWNER;
1436 else if (!strcasecmp(w, "ExecCGI")) {
1439 else if (!strcasecmp(w, "MultiViews")) {
1442 else if (!strcasecmp(w, "RunScripts")) { /* AI backcompat. Yuck */
1443 opt = OPT_MULTI|OPT_EXECCGI;
1445 else if (!strcasecmp(w, "None")) {
1448 else if (!strcasecmp(w, "All")) {
1452 return apr_pstrcat(cmd->pool, "Illegal option ", w, NULL);
1455 if (!(cmd->override_opts & opt) && opt != OPT_NONE) {
1456 return apr_pstrcat(cmd->pool, "Option ", w, " not allowed here", NULL);
1458 else if (action == '-') {
1459 /* we ensure the invariant (d->opts_add & d->opts_remove) == 0 */
1460 d->opts_remove |= opt;
1461 d->opts_add &= ~opt;
1464 else if (action == '+') {
1466 d->opts_remove &= ~opt;
1478 * Note what data should be used when forming file ETag values.
1479 * It would be nicer to do this as an ITERATE, but then we couldn't
1480 * remember the +/- state properly.
1482 static const char *set_etag_bits(cmd_parms *cmd, void *mconfig,
1485 core_dir_config *cfg;
1486 etag_components_t bit;
1494 cfg = (core_dir_config *)mconfig;
1499 while (args[0] != '\0') {
1503 token = ap_getword_conf(cmd->pool, &args);
1504 if ((*token == '+') || (*token == '-')) {
1510 * The occurrence of an absolute setting wipes
1511 * out any previous relative ones. The first such
1512 * occurrence forgets any inherited ones, too.
1515 cfg->etag_bits = ETAG_UNSET;
1516 cfg->etag_add = ETAG_UNSET;
1517 cfg->etag_remove = ETAG_UNSET;
1522 if (strcasecmp(token, "None") == 0) {
1523 if (action != '*') {
1527 cfg->etag_bits = bit = ETAG_NONE;
1531 else if (strcasecmp(token, "All") == 0) {
1532 if (action != '*') {
1537 cfg->etag_bits = bit = ETAG_ALL;
1540 else if (strcasecmp(token, "Size") == 0) {
1543 else if ((strcasecmp(token, "LMTime") == 0)
1544 || (strcasecmp(token, "MTime") == 0)
1545 || (strcasecmp(token, "LastModified") == 0)) {
1548 else if (strcasecmp(token, "INode") == 0) {
1552 return apr_pstrcat(cmd->pool, "Unknown keyword '",
1553 token, "' for ", cmd->cmd->name,
1554 " directive", NULL);
1558 return apr_pstrcat(cmd->pool, cmd->cmd->name, " keyword '",
1559 token, "' cannot be used with '+' or '-'",
1563 if (action == '+') {
1565 * Make sure it's in the 'add' list and absent from the
1568 cfg->etag_add |= bit;
1569 cfg->etag_remove &= (~ bit);
1571 else if (action == '-') {
1572 cfg->etag_remove |= bit;
1573 cfg->etag_add &= (~ bit);
1577 * Non-relative values wipe out any + or - values
1578 * accumulated so far.
1580 cfg->etag_bits |= bit;
1581 cfg->etag_add = ETAG_UNSET;
1582 cfg->etag_remove = ETAG_UNSET;
1588 * Any setting at all will clear the 'None' and 'Unset' bits.
1591 if (cfg->etag_add != ETAG_UNSET) {
1592 cfg->etag_add &= (~ ETAG_UNSET);
1595 if (cfg->etag_remove != ETAG_UNSET) {
1596 cfg->etag_remove &= (~ ETAG_UNSET);
1600 cfg->etag_bits &= (~ ETAG_UNSET);
1602 if ((cfg->etag_bits & ETAG_NONE) != ETAG_NONE) {
1603 cfg->etag_bits &= (~ ETAG_NONE);
1610 static const char *set_enable_mmap(cmd_parms *cmd, void *d_,
1613 core_dir_config *d = d_;
1614 const char *err = ap_check_cmd_context(cmd, NOT_IN_LIMIT);
1620 if (strcasecmp(arg, "on") == 0) {
1621 d->enable_mmap = ENABLE_MMAP_ON;
1623 else if (strcasecmp(arg, "off") == 0) {
1624 d->enable_mmap = ENABLE_MMAP_OFF;
1627 return "parameter must be 'on' or 'off'";
1633 static const char *set_enable_sendfile(cmd_parms *cmd, void *d_,
1636 core_dir_config *d = d_;
1637 const char *err = ap_check_cmd_context(cmd, NOT_IN_LIMIT);
1643 if (strcasecmp(arg, "on") == 0) {
1644 d->enable_sendfile = ENABLE_SENDFILE_ON;
1646 else if (strcasecmp(arg, "off") == 0) {
1647 d->enable_sendfile = ENABLE_SENDFILE_OFF;
1650 return "parameter must be 'on' or 'off'";
1658 * Report a missing-'>' syntax error.
1660 static char *unclosed_directive(cmd_parms *cmd)
1662 return apr_pstrcat(cmd->pool, cmd->cmd->name,
1663 "> directive missing closing '>'", NULL);
1667 * Report a missing args in '<Foo >' syntax error.
1669 static char *missing_container_arg(cmd_parms *cmd)
1671 return apr_pstrcat(cmd->pool, cmd->cmd->name,
1672 "> directive requires additional arguments", NULL);
1675 AP_CORE_DECLARE_NONSTD(const char *) ap_limit_section(cmd_parms *cmd,
1679 const char *endp = ap_strrchr_c(arg, '>');
1680 const char *limited_methods;
1681 void *tog = cmd->cmd->cmd_data;
1682 apr_int64_t limited = 0;
1685 const char *err = ap_check_cmd_context(cmd, NOT_IN_LIMIT);
1691 return unclosed_directive(cmd);
1694 limited_methods = apr_pstrndup(cmd->pool, arg, endp - arg);
1696 if (!limited_methods[0]) {
1697 return missing_container_arg(cmd);
1700 while (limited_methods[0]) {
1701 char *method = ap_getword_conf(cmd->pool, &limited_methods);
1704 /* check for builtin or module registered method number */
1705 methnum = ap_method_number_of(method);
1707 if (methnum == M_TRACE && !tog) {
1708 return "TRACE cannot be controlled by <Limit>, see TraceEnable";
1710 else if (methnum == M_INVALID) {
1711 /* method has not been registered yet, but resorce restriction
1712 * is always checked before method handling, so register it.
1714 methnum = ap_method_register(cmd->pool, method);
1717 limited |= (AP_METHOD_BIT << methnum);
1720 /* Killing two features with one function,
1721 * if (tog == NULL) <Limit>, else <LimitExcept>
1723 cmd->limited = tog ? ~limited : limited;
1725 errmsg = ap_walk_config(cmd->directive->first_child, cmd, cmd->context);
1732 /* XXX: Bogus - need to do this differently (at least OS2/Netware suffer
1733 * the same problem!!!
1734 * We use this in <DirectoryMatch> and <FilesMatch>, to ensure that
1735 * people don't get bitten by wrong-cased regex matches
1739 #define USE_ICASE AP_REG_ICASE
1744 static const char *dirsection(cmd_parms *cmd, void *mconfig, const char *arg)
1747 const char *endp = ap_strrchr_c(arg, '>');
1748 int old_overrides = cmd->override;
1749 char *old_path = cmd->path;
1750 core_dir_config *conf;
1751 ap_conf_vector_t *new_dir_conf = ap_create_per_dir_config(cmd->pool);
1752 ap_regex_t *r = NULL;
1753 const command_rec *thiscmd = cmd->cmd;
1755 const char *err = ap_check_cmd_context(cmd,
1756 NOT_IN_DIR_LOC_FILE|NOT_IN_LIMIT);
1762 return unclosed_directive(cmd);
1765 arg = apr_pstrndup(cmd->pool, arg, endp - arg);
1768 return missing_container_arg(cmd);
1772 if (thiscmd->cmd_data)
1773 return "<DirectoryMatch > block must specify a path";
1775 return "<Directory > block must specify a path";
1778 cmd->path = ap_getword_conf(cmd->pool, &arg);
1779 cmd->override = OR_ALL|ACCESS_CONF;
1781 if (!strcmp(cmd->path, "~")) {
1782 cmd->path = ap_getword_conf(cmd->pool, &arg);
1784 return "<Directory ~ > block must specify a path";
1785 r = ap_pregcomp(cmd->pool, cmd->path, AP_REG_EXTENDED|USE_ICASE);
1787 return "Regex could not be compiled";
1790 else if (thiscmd->cmd_data) { /* <DirectoryMatch> */
1791 r = ap_pregcomp(cmd->pool, cmd->path, AP_REG_EXTENDED|USE_ICASE);
1793 return "Regex could not be compiled";
1796 else if (!strcmp(cmd->path, "/") == 0)
1801 * Ensure that the pathname is canonical, and append the trailing /
1803 apr_status_t rv = apr_filepath_merge(&newpath, NULL, cmd->path,
1804 APR_FILEPATH_TRUENAME, cmd->pool);
1805 if (rv != APR_SUCCESS && rv != APR_EPATHWILD) {
1806 return apr_pstrcat(cmd->pool, "<Directory \"", cmd->path,
1807 "\"> path is invalid.", NULL);
1810 cmd->path = newpath;
1811 if (cmd->path[strlen(cmd->path) - 1] != '/')
1812 cmd->path = apr_pstrcat(cmd->pool, cmd->path, "/", NULL);
1815 /* initialize our config and fetch it */
1816 conf = ap_set_config_vectors(cmd->server, new_dir_conf, cmd->path,
1817 &core_module, cmd->pool);
1819 errmsg = ap_walk_config(cmd->directive->first_child, cmd, new_dir_conf);
1824 conf->d = cmd->path;
1825 conf->d_is_fnmatch = (apr_fnmatch_test(conf->d) != 0);
1827 /* Make this explicit - the "/" root has 0 elements, that is, we
1828 * will always merge it, and it will always sort and merge first.
1829 * All others are sorted and tested by the number of slashes.
1831 if (strcmp(conf->d, "/") == 0)
1832 conf->d_components = 0;
1834 conf->d_components = ap_count_dirs(conf->d);
1836 ap_add_per_dir_conf(cmd->server, new_dir_conf);
1839 return apr_pstrcat(cmd->pool, "Multiple ", thiscmd->name,
1840 "> arguments not (yet) supported.", NULL);
1843 cmd->path = old_path;
1844 cmd->override = old_overrides;
1849 static const char *urlsection(cmd_parms *cmd, void *mconfig, const char *arg)
1852 const char *endp = ap_strrchr_c(arg, '>');
1853 int old_overrides = cmd->override;
1854 char *old_path = cmd->path;
1855 core_dir_config *conf;
1856 ap_regex_t *r = NULL;
1857 const command_rec *thiscmd = cmd->cmd;
1858 ap_conf_vector_t *new_url_conf = ap_create_per_dir_config(cmd->pool);
1859 const char *err = ap_check_cmd_context(cmd,
1860 NOT_IN_DIR_LOC_FILE|NOT_IN_LIMIT);
1866 return unclosed_directive(cmd);
1869 arg = apr_pstrndup(cmd->pool, arg, endp - arg);
1872 return missing_container_arg(cmd);
1875 cmd->path = ap_getword_conf(cmd->pool, &arg);
1876 cmd->override = OR_ALL|ACCESS_CONF;
1878 if (thiscmd->cmd_data) { /* <LocationMatch> */
1879 r = ap_pregcomp(cmd->pool, cmd->path, AP_REG_EXTENDED);
1881 return "Regex could not be compiled";
1884 else if (!strcmp(cmd->path, "~")) {
1885 cmd->path = ap_getword_conf(cmd->pool, &arg);
1886 r = ap_pregcomp(cmd->pool, cmd->path, AP_REG_EXTENDED);
1888 return "Regex could not be compiled";
1892 /* initialize our config and fetch it */
1893 conf = ap_set_config_vectors(cmd->server, new_url_conf, cmd->path,
1894 &core_module, cmd->pool);
1896 errmsg = ap_walk_config(cmd->directive->first_child, cmd, new_url_conf);
1900 conf->d = apr_pstrdup(cmd->pool, cmd->path); /* No mangling, please */
1901 conf->d_is_fnmatch = apr_fnmatch_test(conf->d) != 0;
1904 ap_add_per_url_conf(cmd->server, new_url_conf);
1907 return apr_pstrcat(cmd->pool, "Multiple ", thiscmd->name,
1908 "> arguments not (yet) supported.", NULL);
1911 cmd->path = old_path;
1912 cmd->override = old_overrides;
1917 static const char *filesection(cmd_parms *cmd, void *mconfig, const char *arg)
1920 const char *endp = ap_strrchr_c(arg, '>');
1921 int old_overrides = cmd->override;
1922 char *old_path = cmd->path;
1923 core_dir_config *conf;
1924 ap_regex_t *r = NULL;
1925 const command_rec *thiscmd = cmd->cmd;
1926 core_dir_config *c = mconfig;
1927 ap_conf_vector_t *new_file_conf = ap_create_per_dir_config(cmd->pool);
1928 const char *err = ap_check_cmd_context(cmd, NOT_IN_LIMIT|NOT_IN_LOCATION);
1935 return unclosed_directive(cmd);
1938 arg = apr_pstrndup(cmd->pool, arg, endp - arg);
1941 return missing_container_arg(cmd);
1944 cmd->path = ap_getword_conf(cmd->pool, &arg);
1945 /* Only if not an .htaccess file */
1947 cmd->override = OR_ALL|ACCESS_CONF;
1950 if (thiscmd->cmd_data) { /* <FilesMatch> */
1951 r = ap_pregcomp(cmd->pool, cmd->path, AP_REG_EXTENDED|USE_ICASE);
1953 return "Regex could not be compiled";
1956 else if (!strcmp(cmd->path, "~")) {
1957 cmd->path = ap_getword_conf(cmd->pool, &arg);
1958 r = ap_pregcomp(cmd->pool, cmd->path, AP_REG_EXTENDED|USE_ICASE);
1960 return "Regex could not be compiled";
1965 /* Ensure that the pathname is canonical, but we
1966 * can't test the case/aliases without a fixed path */
1967 if (apr_filepath_merge(&newpath, "", cmd->path,
1968 0, cmd->pool) != APR_SUCCESS)
1969 return apr_pstrcat(cmd->pool, "<Files \"", cmd->path,
1970 "\"> is invalid.", NULL);
1971 cmd->path = newpath;
1974 /* initialize our config and fetch it */
1975 conf = ap_set_config_vectors(cmd->server, new_file_conf, cmd->path,
1976 &core_module, cmd->pool);
1978 errmsg = ap_walk_config(cmd->directive->first_child, cmd, new_file_conf);
1982 conf->d = cmd->path;
1983 conf->d_is_fnmatch = apr_fnmatch_test(conf->d) != 0;
1986 ap_add_file_conf(c, new_file_conf);
1989 return apr_pstrcat(cmd->pool, "Multiple ", thiscmd->name,
1990 "> arguments not (yet) supported.", NULL);
1993 cmd->path = old_path;
1994 cmd->override = old_overrides;
1999 static const char *start_ifmod(cmd_parms *cmd, void *mconfig, const char *arg)
2001 const char *endp = ap_strrchr_c(arg, '>');
2002 int not = (arg[0] == '!');
2006 return unclosed_directive(cmd);
2009 arg = apr_pstrndup(cmd->pool, arg, endp - arg);
2016 return missing_container_arg(cmd);
2019 found = ap_find_linked_module(arg);
2021 /* search prelinked stuff */
2023 ap_module_symbol_t *current = ap_prelinked_module_symbols;
2025 for (; current->name; ++current) {
2026 if (!strcmp(current->name, arg)) {
2027 found = current->modp;
2033 /* search dynamic stuff */
2035 APR_OPTIONAL_FN_TYPE(ap_find_loaded_module_symbol) *check_symbol =
2036 APR_RETRIEVE_OPTIONAL_FN(ap_find_loaded_module_symbol);
2039 found = check_symbol(cmd->server, arg);
2043 if ((!not && found) || (not && !found)) {
2044 ap_directive_t *parent = NULL;
2045 ap_directive_t *current = NULL;
2048 retval = ap_build_cont_config(cmd->pool, cmd->temp_pool, cmd,
2049 ¤t, &parent, "<IfModule");
2050 *(ap_directive_t **)mconfig = current;
2054 *(ap_directive_t **)mconfig = NULL;
2055 return ap_soak_end_container(cmd, "<IfModule");
2059 AP_DECLARE(int) ap_exists_config_define(const char *name)
2064 defines = (char **)ap_server_config_defines->elts;
2065 for (i = 0; i < ap_server_config_defines->nelts; i++) {
2066 if (strcmp(defines[i], name) == 0) {
2074 static const char *start_ifdefine(cmd_parms *cmd, void *dummy, const char *arg)
2080 endp = ap_strrchr_c(arg, '>');
2082 return unclosed_directive(cmd);
2085 arg = apr_pstrndup(cmd->pool, arg, endp - arg);
2087 if (arg[0] == '!') {
2093 return missing_container_arg(cmd);
2096 defined = ap_exists_config_define(arg);
2097 if ((!not && defined) || (not && !defined)) {
2098 ap_directive_t *parent = NULL;
2099 ap_directive_t *current = NULL;
2102 retval = ap_build_cont_config(cmd->pool, cmd->temp_pool, cmd,
2103 ¤t, &parent, "<IfDefine");
2104 *(ap_directive_t **)dummy = current;
2108 *(ap_directive_t **)dummy = NULL;
2109 return ap_soak_end_container(cmd, "<IfDefine");
2113 /* httpd.conf commands... beginning with the <VirtualHost> business */
2115 static const char *virtualhost_section(cmd_parms *cmd, void *dummy,
2118 server_rec *main_server = cmd->server, *s;
2120 const char *endp = ap_strrchr_c(arg, '>');
2121 apr_pool_t *p = cmd->pool;
2123 const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
2129 return unclosed_directive(cmd);
2132 arg = apr_pstrndup(cmd->pool, arg, endp - arg);
2135 return missing_container_arg(cmd);
2138 /* FIXME: There's another feature waiting to happen here -- since you
2139 can now put multiple addresses/names on a single <VirtualHost>
2140 you might want to use it to group common definitions and then
2141 define other "subhosts" with their individual differences. But
2142 personally I'd rather just do it with a macro preprocessor. -djg */
2143 if (main_server->is_virtual) {
2144 return "<VirtualHost> doesn't nest!";
2147 errmsg = ap_init_virtual_host(p, arg, main_server, &s);
2152 s->next = main_server->next;
2153 main_server->next = s;
2155 s->defn_name = cmd->directive->filename;
2156 s->defn_line_number = cmd->directive->line_num;
2160 errmsg = ap_walk_config(cmd->directive->first_child, cmd,
2161 s->lookup_defaults);
2163 cmd->server = main_server;
2168 static const char *set_server_alias(cmd_parms *cmd, void *dummy,
2171 if (!cmd->server->names) {
2172 return "ServerAlias only used in <VirtualHost>";
2176 char **item, *name = ap_getword_conf(cmd->pool, &arg);
2178 if (ap_is_matchexp(name)) {
2179 item = (char **)apr_array_push(cmd->server->wild_names);
2182 item = (char **)apr_array_push(cmd->server->names);
2191 static const char *set_accf_map(cmd_parms *cmd, void *dummy,
2192 const char *iproto, const char* iaccf)
2194 const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
2195 core_server_config *conf = ap_get_module_config(cmd->server->module_config,
2203 proto = apr_pstrdup(cmd->pool, iproto);
2204 ap_str_tolower(proto);
2205 accf = apr_pstrdup(cmd->pool, iaccf);
2206 ap_str_tolower(accf);
2207 apr_table_set(conf->accf_map, proto, accf);
2212 AP_DECLARE(const char*) ap_get_server_protocol(server_rec* s)
2214 core_server_config *conf = ap_get_module_config(s->module_config,
2216 return conf->protocol;
2219 AP_DECLARE(void) ap_set_server_protocol(server_rec* s, const char* proto)
2221 core_server_config *conf = ap_get_module_config(s->module_config,
2223 conf->protocol = proto;
2226 static const char *set_protocol(cmd_parms *cmd, void *dummy,
2229 const char *err = ap_check_cmd_context(cmd, NOT_IN_DIR_LOC_FILE|NOT_IN_LIMIT);
2230 core_server_config *conf = ap_get_module_config(cmd->server->module_config,
2238 proto = apr_pstrdup(cmd->pool, arg);
2239 ap_str_tolower(proto);
2240 conf->protocol = proto;
2245 static const char *set_server_string_slot(cmd_parms *cmd, void *dummy,
2248 /* This one's pretty generic... */
2250 int offset = (int)(long)cmd->info;
2251 char *struct_ptr = (char *)cmd->server;
2253 const char *err = ap_check_cmd_context(cmd,
2254 NOT_IN_DIR_LOC_FILE|NOT_IN_LIMIT);
2259 *(const char **)(struct_ptr + offset) = arg;
2264 * The ServerName directive takes one argument with format
2265 * [scheme://]fully-qualified-domain-name[:port], for instance
2266 * ServerName www.example.com
2267 * ServerName www.example.com:80
2268 * ServerName https://www.example.com:443
2271 static const char *server_hostname_port(cmd_parms *cmd, void *dummy, const char *arg)
2273 const char *err = ap_check_cmd_context(cmd, NOT_IN_DIR_LOC_FILE|NOT_IN_LIMIT);
2274 const char *portstr, *part;
2282 part = ap_strstr_c(arg, "://");
2285 scheme = apr_pstrndup(cmd->pool, arg, part - arg);
2286 ap_str_tolower(scheme);
2287 cmd->server->server_scheme = (const char *)scheme;
2293 portstr = ap_strchr_c(part, ':');
2295 cmd->server->server_hostname = apr_pstrndup(cmd->pool, part,
2298 port = atoi(portstr);
2299 if (port <= 0 || port >= 65536) { /* 65536 == 1<<16 */
2300 return apr_pstrcat(cmd->temp_pool, "The port number \"", arg,
2301 "\" is outside the appropriate range "
2302 "(i.e., 1..65535).", NULL);
2306 cmd->server->server_hostname = apr_pstrdup(cmd->pool, part);
2310 cmd->server->port = port;
2314 static const char *set_signature_flag(cmd_parms *cmd, void *d_,
2317 core_dir_config *d = d_;
2318 const char *err = ap_check_cmd_context(cmd, NOT_IN_LIMIT);
2324 if (strcasecmp(arg, "On") == 0) {
2325 d->server_signature = srv_sig_on;
2327 else if (strcasecmp(arg, "Off") == 0) {
2328 d->server_signature = srv_sig_off;
2330 else if (strcasecmp(arg, "EMail") == 0) {
2331 d->server_signature = srv_sig_withmail;
2334 return "ServerSignature: use one of: off | on | email";
2340 static const char *set_server_root(cmd_parms *cmd, void *dummy,
2343 const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
2349 if ((apr_filepath_merge((char**)&ap_server_root, NULL, arg,
2350 APR_FILEPATH_TRUENAME, cmd->pool) != APR_SUCCESS)
2351 || !ap_is_directory(cmd->pool, ap_server_root)) {
2352 return "ServerRoot must be a valid directory";
2358 static const char *set_timeout(cmd_parms *cmd, void *dummy, const char *arg)
2360 const char *err = ap_check_cmd_context(cmd, NOT_IN_DIR_LOC_FILE|NOT_IN_LIMIT);
2366 cmd->server->timeout = apr_time_from_sec(atoi(arg));
2370 static const char *set_allow2f(cmd_parms *cmd, void *d_, int arg)
2372 core_dir_config *d = d_;
2373 const char *err = ap_check_cmd_context(cmd, NOT_IN_LIMIT);
2379 d->allow_encoded_slashes = arg != 0;
2383 static const char *set_hostname_lookups(cmd_parms *cmd, void *d_,
2386 core_dir_config *d = d_;
2387 const char *err = ap_check_cmd_context(cmd, NOT_IN_LIMIT);
2393 if (!strcasecmp(arg, "on")) {
2394 d->hostname_lookups = HOSTNAME_LOOKUP_ON;
2396 else if (!strcasecmp(arg, "off")) {
2397 d->hostname_lookups = HOSTNAME_LOOKUP_OFF;
2399 else if (!strcasecmp(arg, "double")) {
2400 d->hostname_lookups = HOSTNAME_LOOKUP_DOUBLE;
2403 return "parameter must be 'on', 'off', or 'double'";
2409 static const char *set_serverpath(cmd_parms *cmd, void *dummy,
2412 const char *err = ap_check_cmd_context(cmd, NOT_IN_DIR_LOC_FILE|NOT_IN_LIMIT);
2418 cmd->server->path = arg;
2419 cmd->server->pathlen = (int)strlen(arg);
2423 static const char *set_content_md5(cmd_parms *cmd, void *d_, int arg)
2425 core_dir_config *d = d_;
2426 const char *err = ap_check_cmd_context(cmd, NOT_IN_LIMIT);
2432 d->content_md5 = arg != 0;
2436 static const char *set_accept_path_info(cmd_parms *cmd, void *d_, const char *arg)
2438 core_dir_config *d = d_;
2440 if (strcasecmp(arg, "on") == 0) {
2441 d->accept_path_info = AP_REQ_ACCEPT_PATH_INFO;
2443 else if (strcasecmp(arg, "off") == 0) {
2444 d->accept_path_info = AP_REQ_REJECT_PATH_INFO;
2446 else if (strcasecmp(arg, "default") == 0) {
2447 d->accept_path_info = AP_REQ_DEFAULT_PATH_INFO;
2450 return "AcceptPathInfo must be set to on, off or default";
2456 static const char *set_use_canonical_name(cmd_parms *cmd, void *d_,
2459 core_dir_config *d = d_;
2460 const char *err = ap_check_cmd_context(cmd, NOT_IN_LIMIT);
2466 if (strcasecmp(arg, "on") == 0) {
2467 d->use_canonical_name = USE_CANONICAL_NAME_ON;
2469 else if (strcasecmp(arg, "off") == 0) {
2470 d->use_canonical_name = USE_CANONICAL_NAME_OFF;
2472 else if (strcasecmp(arg, "dns") == 0) {
2473 d->use_canonical_name = USE_CANONICAL_NAME_DNS;
2476 return "parameter must be 'on', 'off', or 'dns'";
2482 static const char *set_use_canonical_phys_port(cmd_parms *cmd, void *d_,
2485 core_dir_config *d = d_;
2486 const char *err = ap_check_cmd_context(cmd, NOT_IN_LIMIT);
2492 if (strcasecmp(arg, "on") == 0) {
2493 d->use_canonical_phys_port = USE_CANONICAL_PHYS_PORT_ON;
2495 else if (strcasecmp(arg, "off") == 0) {
2496 d->use_canonical_phys_port = USE_CANONICAL_PHYS_PORT_OFF;
2499 return "parameter must be 'on' or 'off'";
2506 static const char *include_config (cmd_parms *cmd, void *dummy,
2509 ap_directive_t *conftree = NULL;
2510 const char* conffile, *error;
2511 unsigned *recursion;
2514 apr_pool_userdata_get(&data, "ap_include_sentinel", cmd->pool);
2519 data = recursion = apr_palloc(cmd->pool, sizeof(*recursion));
2521 apr_pool_userdata_setn(data, "ap_include_sentinel", NULL, cmd->pool);
2524 if (++*recursion > AP_MAX_INCLUDE_DEPTH) {
2526 return apr_psprintf(cmd->pool, "Exceeded maximum include depth of %u. "
2527 "You have probably a recursion somewhere.",
2528 AP_MAX_INCLUDE_DEPTH);
2531 conffile = ap_server_root_relative(cmd->pool, name);
2534 return apr_pstrcat(cmd->pool, "Invalid Include path ",
2538 error = ap_process_resource_config(cmd->server, conffile,
2539 &conftree, cmd->pool, cmd->temp_pool);
2545 *(ap_directive_t **)dummy = conftree;
2547 /* recursion level done */
2555 static const char *set_loglevel(cmd_parms *cmd, void *dummy, const char *arg)
2559 const char *err = ap_check_cmd_context(cmd,
2560 NOT_IN_DIR_LOC_FILE|NOT_IN_LIMIT);
2565 if ((str = ap_getword_conf(cmd->pool, &arg))) {
2566 if (!strcasecmp(str, "emerg")) {
2567 cmd->server->loglevel = APLOG_EMERG;
2569 else if (!strcasecmp(str, "alert")) {
2570 cmd->server->loglevel = APLOG_ALERT;
2572 else if (!strcasecmp(str, "crit")) {
2573 cmd->server->loglevel = APLOG_CRIT;
2575 else if (!strcasecmp(str, "error")) {
2576 cmd->server->loglevel = APLOG_ERR;
2578 else if (!strcasecmp(str, "warn")) {
2579 cmd->server->loglevel = APLOG_WARNING;
2581 else if (!strcasecmp(str, "notice")) {
2582 cmd->server->loglevel = APLOG_NOTICE;
2584 else if (!strcasecmp(str, "info")) {
2585 cmd->server->loglevel = APLOG_INFO;
2587 else if (!strcasecmp(str, "debug")) {
2588 cmd->server->loglevel = APLOG_DEBUG;
2591 return "LogLevel requires level keyword: one of "
2592 "emerg/alert/crit/error/warn/notice/info/debug";
2596 return "LogLevel requires level keyword";
2602 AP_DECLARE(const char *) ap_psignature(const char *prefix, request_rec *r)
2605 core_dir_config *conf;
2607 conf = (core_dir_config *)ap_get_module_config(r->per_dir_config,
2609 if ((conf->server_signature == srv_sig_off)
2610 || (conf->server_signature == srv_sig_unset)) {
2614 apr_snprintf(sport, sizeof sport, "%u", (unsigned) ap_get_server_port(r));
2616 if (conf->server_signature == srv_sig_withmail) {
2617 return apr_pstrcat(r->pool, prefix, "<address>",
2618 ap_get_server_banner(),
2619 " Server at <a href=\"",
2620 ap_is_url(r->server->server_admin) ? "" : "mailto:",
2621 ap_escape_html(r->pool, r->server->server_admin),
2623 ap_escape_html(r->pool, ap_get_server_name(r)),
2624 "</a> Port ", sport,
2625 "</address>\n", NULL);
2628 return apr_pstrcat(r->pool, prefix, "<address>", ap_get_server_banner(),
2630 ap_escape_html(r->pool, ap_get_server_name(r)),
2632 "</address>\n", NULL);
2636 * Handle a request to include the server's OS platform in the Server
2637 * response header field (the ServerTokens directive). Unfortunately
2638 * this requires a new global in order to communicate the setting back to
2639 * http_main so it can insert the information in the right place in the
2643 static char *server_banner = NULL;
2644 static int banner_locked = 0;
2645 static char *server_description = NULL;
2647 enum server_token_type {
2648 SrvTk_MAJOR, /* eg: Apache/2 */
2649 SrvTk_MINOR, /* eg. Apache/2.0 */
2650 SrvTk_MINIMAL, /* eg: Apache/2.0.41 */
2651 SrvTk_OS, /* eg: Apache/2.0.41 (UNIX) */
2652 SrvTk_FULL, /* eg: Apache/2.0.41 (UNIX) PHP/4.2.2 FooBar/1.2b */
2653 SrvTk_PRODUCT_ONLY /* eg: Apache */
2655 static enum server_token_type ap_server_tokens = SrvTk_FULL;
2657 static apr_status_t reset_banner(void *dummy)
2660 ap_server_tokens = SrvTk_FULL;
2661 server_banner = NULL;
2662 server_description = NULL;
2666 AP_DECLARE(void) ap_get_server_revision(ap_version_t *version)
2668 version->major = AP_SERVER_MAJORVERSION_NUMBER;
2669 version->minor = AP_SERVER_MINORVERSION_NUMBER;
2670 version->patch = AP_SERVER_PATCHLEVEL_NUMBER;
2671 version->add_string = AP_SERVER_ADD_STRING;
2674 AP_DECLARE(const char *) ap_get_server_description(void)
2676 return server_description ? server_description :
2677 AP_SERVER_BASEVERSION " (" PLATFORM ")";
2680 AP_DECLARE(const char *) ap_get_server_banner(void)
2682 return server_banner ? server_banner : AP_SERVER_BASEVERSION;
2685 AP_DECLARE(void) ap_add_version_component(apr_pool_t *pconf, const char *component)
2687 if (! banner_locked) {
2689 * If the version string is null, register our cleanup to reset the
2690 * pointer on pool destruction. We also know that, if NULL,
2691 * we are adding the original SERVER_BASEVERSION string.
2693 if (server_banner == NULL) {
2694 apr_pool_cleanup_register(pconf, NULL, reset_banner,
2695 apr_pool_cleanup_null);
2696 server_banner = apr_pstrdup(pconf, component);
2700 * Tack the given component identifier to the end of
2701 * the existing string.
2703 server_banner = apr_pstrcat(pconf, server_banner, " ",
2707 server_description = apr_pstrcat(pconf, server_description, " ",
2712 * This routine adds the real server base identity to the banner string,
2713 * and then locks out changes until the next reconfig.
2715 static void set_banner(apr_pool_t *pconf)
2717 if (ap_server_tokens == SrvTk_PRODUCT_ONLY) {
2718 ap_add_version_component(pconf, AP_SERVER_BASEPRODUCT);
2720 else if (ap_server_tokens == SrvTk_MINIMAL) {
2721 ap_add_version_component(pconf, AP_SERVER_BASEVERSION);
2723 else if (ap_server_tokens == SrvTk_MINOR) {
2724 ap_add_version_component(pconf, AP_SERVER_BASEPRODUCT "/" AP_SERVER_MINORREVISION);
2726 else if (ap_server_tokens == SrvTk_MAJOR) {
2727 ap_add_version_component(pconf, AP_SERVER_BASEPRODUCT "/" AP_SERVER_MAJORVERSION);
2730 ap_add_version_component(pconf, AP_SERVER_BASEVERSION " (" PLATFORM ")");
2734 * Lock the server_banner string if we're not displaying
2735 * the full set of tokens
2737 if (ap_server_tokens != SrvTk_FULL) {
2740 server_description = AP_SERVER_BASEVERSION " (" PLATFORM ")";
2743 static const char *set_serv_tokens(cmd_parms *cmd, void *dummy,
2746 const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
2752 if (!strcasecmp(arg, "OS")) {
2753 ap_server_tokens = SrvTk_OS;
2755 else if (!strcasecmp(arg, "Min") || !strcasecmp(arg, "Minimal")) {
2756 ap_server_tokens = SrvTk_MINIMAL;
2758 else if (!strcasecmp(arg, "Major")) {
2759 ap_server_tokens = SrvTk_MAJOR;
2761 else if (!strcasecmp(arg, "Minor") ) {
2762 ap_server_tokens = SrvTk_MINOR;
2764 else if (!strcasecmp(arg, "Prod") || !strcasecmp(arg, "ProductOnly")) {
2765 ap_server_tokens = SrvTk_PRODUCT_ONLY;
2768 ap_server_tokens = SrvTk_FULL;
2774 static const char *set_limit_req_line(cmd_parms *cmd, void *dummy,
2777 const char *err = ap_check_cmd_context(cmd,
2778 NOT_IN_DIR_LOC_FILE|NOT_IN_LIMIT);
2787 return apr_pstrcat(cmd->temp_pool, "LimitRequestLine \"", arg,
2788 "\" must be a non-negative integer", NULL);
2791 cmd->server->limit_req_line = lim;
2795 static const char *set_limit_req_fieldsize(cmd_parms *cmd, void *dummy,
2798 const char *err = ap_check_cmd_context(cmd,
2799 NOT_IN_DIR_LOC_FILE|NOT_IN_LIMIT);
2808 return apr_pstrcat(cmd->temp_pool, "LimitRequestFieldsize \"", arg,
2809 "\" must be a non-negative integer",
2813 cmd->server->limit_req_fieldsize = lim;
2817 static const char *set_limit_req_fields(cmd_parms *cmd, void *dummy,
2820 const char *err = ap_check_cmd_context(cmd,
2821 NOT_IN_DIR_LOC_FILE|NOT_IN_LIMIT);
2830 return apr_pstrcat(cmd->temp_pool, "LimitRequestFields \"", arg,
2831 "\" must be a non-negative integer (0 = no limit)",
2835 cmd->server->limit_req_fields = lim;
2839 static const char *set_limit_req_body(cmd_parms *cmd, void *conf_,
2842 core_dir_config *conf = conf_;
2843 const char *err = ap_check_cmd_context(cmd, NOT_IN_LIMIT);
2850 if (APR_SUCCESS != apr_strtoff(&conf->limit_req_body, arg, &errp, 10)) {
2851 return "LimitRequestBody argument is not parsable.";
2853 if (*errp || conf->limit_req_body < 0) {
2854 return "LimitRequestBody requires a non-negative integer.";
2860 static const char *set_limit_xml_req_body(cmd_parms *cmd, void *conf_,
2863 core_dir_config *conf = conf_;
2864 const char *err = ap_check_cmd_context(cmd, NOT_IN_LIMIT);
2870 conf->limit_xml_body = atol(arg);
2871 if (conf->limit_xml_body < 0)
2872 return "LimitXMLRequestBody requires a non-negative integer.";
2877 AP_DECLARE(size_t) ap_get_limit_xml_body(const request_rec *r)
2879 core_dir_config *conf;
2881 conf = ap_get_module_config(r->per_dir_config, &core_module);
2882 if (conf->limit_xml_body == AP_LIMIT_UNSET)
2883 return AP_DEFAULT_LIMIT_XML_BODY;
2885 return (size_t)conf->limit_xml_body;
2888 #if !defined (RLIMIT_CPU) || !(defined (RLIMIT_DATA) || defined (RLIMIT_VMEM) || defined(RLIMIT_AS)) || !defined (RLIMIT_NPROC)
2889 static const char *no_set_limit(cmd_parms *cmd, void *conf_,
2890 const char *arg, const char *arg2)
2892 ap_log_error(APLOG_MARK, APLOG_ERR, 0, cmd->server,
2893 "%s not supported on this platform", cmd->cmd->name);
2900 static const char *set_limit_cpu(cmd_parms *cmd, void *conf_,
2901 const char *arg, const char *arg2)
2903 core_dir_config *conf = conf_;
2905 unixd_set_rlimit(cmd, &conf->limit_cpu, arg, arg2, RLIMIT_CPU);
2910 #if defined (RLIMIT_DATA) || defined (RLIMIT_VMEM) || defined(RLIMIT_AS)
2911 static const char *set_limit_mem(cmd_parms *cmd, void *conf_,
2912 const char *arg, const char * arg2)
2914 core_dir_config *conf = conf_;
2916 #if defined(RLIMIT_AS)
2917 unixd_set_rlimit(cmd, &conf->limit_mem, arg, arg2 ,RLIMIT_AS);
2918 #elif defined(RLIMIT_DATA)
2919 unixd_set_rlimit(cmd, &conf->limit_mem, arg, arg2, RLIMIT_DATA);
2920 #elif defined(RLIMIT_VMEM)
2921 unixd_set_rlimit(cmd, &conf->limit_mem, arg, arg2, RLIMIT_VMEM);
2929 static const char *set_limit_nproc(cmd_parms *cmd, void *conf_,
2930 const char *arg, const char * arg2)
2932 core_dir_config *conf = conf_;
2934 unixd_set_rlimit(cmd, &conf->limit_nproc, arg, arg2, RLIMIT_NPROC);
2939 static const char *set_recursion_limit(cmd_parms *cmd, void *dummy,
2940 const char *arg1, const char *arg2)
2942 core_server_config *conf = ap_get_module_config(cmd->server->module_config,
2944 int limit = atoi(arg1);
2947 return "The recursion limit must be greater than zero.";
2950 ap_log_error(APLOG_MARK, APLOG_WARNING, 0, cmd->server,
2951 "Limiting internal redirects to very low numbers may "
2952 "cause normal requests to fail.");
2955 conf->redirect_limit = limit;
2961 return "The recursion limit must be greater than zero.";
2964 ap_log_error(APLOG_MARK, APLOG_WARNING, 0, cmd->server,
2965 "Limiting the subrequest depth to a very low level may"
2966 " cause normal requests to fail.");
2970 conf->subreq_limit = limit;
2975 static void log_backtrace(const request_rec *r)
2977 const request_rec *top = r;
2979 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
2980 "r->uri = %s", r->uri ? r->uri : "(unexpectedly NULL)");
2982 while (top && (top->prev || top->main)) {
2985 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
2986 "redirected from r->uri = %s",
2987 top->uri ? top->uri : "(unexpectedly NULL)");
2990 if (!top->prev && top->main) {
2992 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
2993 "subrequested from r->uri = %s",
2994 top->uri ? top->uri : "(unexpectedly NULL)");
3000 * check whether redirect limit is reached
3002 AP_DECLARE(int) ap_is_recursion_limit_exceeded(const request_rec *r)
3004 core_server_config *conf = ap_get_module_config(r->server->module_config,
3006 const request_rec *top = r;
3007 int redirects = 0, subreqs = 0;
3008 int rlimit = conf->redirect_limit
3009 ? conf->redirect_limit
3010 : AP_DEFAULT_MAX_INTERNAL_REDIRECTS;
3011 int slimit = conf->subreq_limit
3012 ? conf->subreq_limit
3013 : AP_DEFAULT_MAX_SUBREQ_DEPTH;
3016 while (top->prev || top->main) {
3018 if (++redirects >= rlimit) {
3019 /* uuh, too much. */
3020 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
3021 "Request exceeded the limit of %d internal "
3022 "redirects due to probable configuration error. "
3023 "Use 'LimitInternalRecursion' to increase the "
3024 "limit if necessary. Use 'LogLevel debug' to get "
3025 "a backtrace.", rlimit);
3027 /* post backtrace */
3030 /* return failure */
3037 if (!top->prev && top->main) {
3038 if (++subreqs >= slimit) {
3039 /* uuh, too much. */
3040 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
3041 "Request exceeded the limit of %d subrequest "
3042 "nesting levels due to probable configuration "
3043 "error. Use 'LimitInternalRecursion' to increase "
3044 "the limit if necessary. Use 'LogLevel debug' to "
3045 "get a backtrace.", slimit);
3047 /* post backtrace */
3050 /* return failure */
3058 /* recursion state: ok */
3062 static const char *add_ct_output_filters(cmd_parms *cmd, void *conf_,
3063 const char *arg, const char *arg2)
3065 core_dir_config *conf = conf_;
3066 ap_filter_rec_t *old, *new = NULL;
3067 const char *filter_name;
3069 if (!conf->ct_output_filters) {
3070 conf->ct_output_filters = apr_hash_make(cmd->pool);
3074 old = (ap_filter_rec_t*) apr_hash_get(conf->ct_output_filters, arg2,
3075 APR_HASH_KEY_STRING);
3076 /* find last entry */
3085 (filter_name = ap_getword(cmd->pool, &arg, ';')) &&
3086 strcmp(filter_name, "")) {
3087 new = apr_pcalloc(cmd->pool, sizeof(ap_filter_rec_t));
3088 new->name = filter_name;
3090 /* We found something, so let's append it. */
3095 apr_hash_set(conf->ct_output_filters, arg2,
3096 APR_HASH_KEY_STRING, new);
3102 return "invalid filter name";
3108 * Insert filters requested by the AddOutputFilterByType
3109 * configuration directive. We cannot add filters based
3110 * on content-type until after the handler has started
3111 * to run. Only then do we reliably know the content-type.
3113 void ap_add_output_filters_by_type(request_rec *r)
3115 core_dir_config *conf;
3118 conf = (core_dir_config *)ap_get_module_config(r->per_dir_config,
3121 /* We can't do anything with no content-type or if we don't have a
3122 * filter configured.
3124 if (!r->content_type || !conf->ct_output_filters) {
3128 /* remove c-t decoration */
3129 ctype = ap_field_noparam(r->pool, r->content_type);
3131 ap_filter_rec_t *ct_filter;
3132 ct_filter = apr_hash_get(conf->ct_output_filters, ctype,
3133 APR_HASH_KEY_STRING);
3135 ap_add_output_filter(ct_filter->name, NULL, r, r->connection);
3136 ct_filter = ct_filter->next;
3143 static const char *set_trace_enable(cmd_parms *cmd, void *dummy,
3146 core_server_config *conf = ap_get_module_config(cmd->server->module_config,
3149 if (strcasecmp(arg1, "on") == 0) {
3150 conf->trace_enable = AP_TRACE_ENABLE;
3152 else if (strcasecmp(arg1, "off") == 0) {
3153 conf->trace_enable = AP_TRACE_DISABLE;
3155 else if (strcasecmp(arg1, "extended") == 0) {
3156 conf->trace_enable = AP_TRACE_EXTENDED;
3159 return "TraceEnable must be one of 'on', 'off', or 'extended'";
3165 /* Note --- ErrorDocument will now work from .htaccess files.
3166 * The AllowOverride of Fileinfo allows webmasters to turn it off
3169 static const command_rec core_cmds[] = {
3171 /* Old access config file commands */
3173 AP_INIT_RAW_ARGS("<Directory", dirsection, NULL, RSRC_CONF,
3174 "Container for directives affecting resources located in the specified "
3176 AP_INIT_RAW_ARGS("<Location", urlsection, NULL, RSRC_CONF,
3177 "Container for directives affecting resources accessed through the "
3178 "specified URL paths"),
3179 AP_INIT_RAW_ARGS("<VirtualHost", virtualhost_section, NULL, RSRC_CONF,
3180 "Container to map directives to a particular virtual host, takes one or "
3181 "more host addresses"),
3182 AP_INIT_RAW_ARGS("<Files", filesection, NULL, OR_ALL,
3183 "Container for directives affecting files matching specified patterns"),
3184 AP_INIT_RAW_ARGS("<Limit", ap_limit_section, NULL, OR_ALL,
3185 "Container for authentication directives when accessed using specified HTTP "
3187 AP_INIT_RAW_ARGS("<LimitExcept", ap_limit_section, (void*)1, OR_ALL,
3188 "Container for authentication directives to be applied when any HTTP "
3189 "method other than those specified is used to access the resource"),
3190 AP_INIT_TAKE1("<IfModule", start_ifmod, NULL, EXEC_ON_READ | OR_ALL,
3191 "Container for directives based on existance of specified modules"),
3192 AP_INIT_TAKE1("<IfDefine", start_ifdefine, NULL, EXEC_ON_READ | OR_ALL,
3193 "Container for directives based on existance of command line defines"),
3194 AP_INIT_RAW_ARGS("<DirectoryMatch", dirsection, (void*)1, RSRC_CONF,
3195 "Container for directives affecting resources located in the "
3196 "specified directories"),
3197 AP_INIT_RAW_ARGS("<LocationMatch", urlsection, (void*)1, RSRC_CONF,
3198 "Container for directives affecting resources accessed through the "
3199 "specified URL paths"),
3200 AP_INIT_RAW_ARGS("<FilesMatch", filesection, (void*)1, OR_ALL,
3201 "Container for directives affecting files matching specified patterns"),
3203 AP_INIT_TAKE1("GprofDir", set_gprof_dir, NULL, RSRC_CONF,
3204 "Directory to plop gmon.out files"),
3206 AP_INIT_TAKE1("AddDefaultCharset", set_add_default_charset, NULL, OR_FILEINFO,
3207 "The name of the default charset to add to any Content-Type without one or 'Off' to disable"),
3208 AP_INIT_TAKE1("AcceptPathInfo", set_accept_path_info, NULL, OR_FILEINFO,
3209 "Set to on or off for PATH_INFO to be accepted by handlers, or default for the per-handler preference"),
3210 AP_INIT_TAKE1("Define", set_define, NULL, RSRC_CONF,
3211 "Define the existance of a variable. Same as passing -D to the command line."),
3213 /* Old resource config file commands */
3215 AP_INIT_RAW_ARGS("AccessFileName", set_access_name, NULL, RSRC_CONF,
3216 "Name(s) of per-directory config files (default: .htaccess)"),
3217 AP_INIT_TAKE1("DocumentRoot", set_document_root, NULL, RSRC_CONF,
3218 "Root directory of the document tree"),
3219 AP_INIT_TAKE2("ErrorDocument", set_error_document, NULL, OR_FILEINFO,
3220 "Change responses for HTTP errors"),
3221 AP_INIT_RAW_ARGS("AllowOverride", set_override, NULL, ACCESS_CONF,
3222 "Controls what groups of directives can be configured by per-directory "
3224 AP_INIT_RAW_ARGS("Options", set_options, NULL, OR_OPTIONS,
3225 "Set a number of attributes for a given directory"),
3226 AP_INIT_TAKE1("DefaultType", ap_set_string_slot,
3227 (void*)APR_OFFSETOF(core_dir_config, ap_default_type),
3228 OR_FILEINFO, "the default MIME type for untypable files"),
3229 AP_INIT_RAW_ARGS("FileETag", set_etag_bits, NULL, OR_FILEINFO,
3230 "Specify components used to construct a file's ETag"),
3231 AP_INIT_TAKE1("EnableMMAP", set_enable_mmap, NULL, OR_FILEINFO,
3232 "Controls whether memory-mapping may be used to read files"),
3233 AP_INIT_TAKE1("EnableSendfile", set_enable_sendfile, NULL, OR_FILEINFO,
3234 "Controls whether sendfile may be used to transmit files"),
3236 /* Old server config file commands */
3238 AP_INIT_TAKE1("Protocol", set_protocol, NULL, RSRC_CONF,
3239 "Set the Protocol for httpd to use."),
3240 AP_INIT_TAKE2("AcceptFilter", set_accf_map, NULL, RSRC_CONF,
3241 "Set the Accept Filter to use for a protocol"),
3242 AP_INIT_TAKE1("Port", ap_set_deprecated, NULL, RSRC_CONF,
3243 "Port was replaced with Listen in Apache 2.0"),
3244 AP_INIT_TAKE1("HostnameLookups", set_hostname_lookups, NULL,
3245 ACCESS_CONF|RSRC_CONF,
3246 "\"on\" to enable, \"off\" to disable reverse DNS lookups, or \"double\" to "
3247 "enable double-reverse DNS lookups"),
3248 AP_INIT_TAKE1("ServerAdmin", set_server_string_slot,
3249 (void *)APR_OFFSETOF(server_rec, server_admin), RSRC_CONF,
3250 "The email address of the server administrator"),
3251 AP_INIT_TAKE1("ServerName", server_hostname_port, NULL, RSRC_CONF,
3252 "The hostname and port of the server"),
3253 AP_INIT_TAKE1("ServerSignature", set_signature_flag, NULL, OR_ALL,
3254 "En-/disable server signature (on|off|email)"),
3255 AP_INIT_TAKE1("ServerRoot", set_server_root, NULL, RSRC_CONF | EXEC_ON_READ,
3256 "Common directory of server-related files (logs, confs, etc.)"),
3257 AP_INIT_TAKE1("ErrorLog", set_server_string_slot,
3258 (void *)APR_OFFSETOF(server_rec, error_fname), RSRC_CONF,
3259 "The filename of the error log"),
3260 AP_INIT_RAW_ARGS("ServerAlias", set_server_alias, NULL, RSRC_CONF,
3261 "A name or names alternately used to access the server"),
3262 AP_INIT_TAKE1("ServerPath", set_serverpath, NULL, RSRC_CONF,
3263 "The pathname the server can be reached at"),
3264 AP_INIT_TAKE1("Timeout", set_timeout, NULL, RSRC_CONF,
3265 "Timeout duration (sec)"),
3266 AP_INIT_FLAG("ContentDigest", set_content_md5, NULL, OR_OPTIONS,
3267 "whether or not to send a Content-MD5 header with each request"),
3268 AP_INIT_TAKE1("UseCanonicalName", set_use_canonical_name, NULL,
3269 RSRC_CONF|ACCESS_CONF,
3270 "How to work out the ServerName : Port when constructing URLs"),
3271 AP_INIT_TAKE1("UseCanonicalPhysicalPort", set_use_canonical_phys_port, NULL,
3272 RSRC_CONF|ACCESS_CONF,
3273 "Whether to use the physical Port when constructing URLs"),
3274 /* TODO: RlimitFoo should all be part of mod_cgi, not in the core */
3275 /* TODO: ListenBacklog in MPM */
3276 AP_INIT_TAKE1("Include", include_config, NULL,
3277 (RSRC_CONF | ACCESS_CONF | EXEC_ON_READ),
3278 "Name of the config file to be included"),
3279 AP_INIT_TAKE1("LogLevel", set_loglevel, NULL, RSRC_CONF,
3280 "Level of verbosity in error logging"),
3281 AP_INIT_TAKE1("NameVirtualHost", ap_set_name_virtual_host, NULL, RSRC_CONF,
3282 "A numeric IP address:port, or the name of a host"),
3283 AP_INIT_TAKE1("ServerTokens", set_serv_tokens, NULL, RSRC_CONF,
3284 "Determine tokens displayed in the Server: header - Min(imal), OS or Full"),
3285 AP_INIT_TAKE1("LimitRequestLine", set_limit_req_line, NULL, RSRC_CONF,
3286 "Limit on maximum size of an HTTP request line"),
3287 AP_INIT_TAKE1("LimitRequestFieldsize", set_limit_req_fieldsize, NULL,
3289 "Limit on maximum size of an HTTP request header field"),
3290 AP_INIT_TAKE1("LimitRequestFields", set_limit_req_fields, NULL, RSRC_CONF,
3291 "Limit (0 = unlimited) on max number of header fields in a request message"),
3292 AP_INIT_TAKE1("LimitRequestBody", set_limit_req_body,
3293 (void*)APR_OFFSETOF(core_dir_config, limit_req_body), OR_ALL,
3294 "Limit (in bytes) on maximum size of request message body"),
3295 AP_INIT_TAKE1("LimitXMLRequestBody", set_limit_xml_req_body, NULL, OR_ALL,
3296 "Limit (in bytes) on maximum size of an XML-based request "
3299 /* System Resource Controls */
3301 AP_INIT_TAKE12("RLimitCPU", set_limit_cpu,
3302 (void*)APR_OFFSETOF(core_dir_config, limit_cpu),
3303 OR_ALL, "Soft/hard limits for max CPU usage in seconds"),
3305 AP_INIT_TAKE12("RLimitCPU", no_set_limit, NULL,
3306 OR_ALL, "Soft/hard limits for max CPU usage in seconds"),
3308 #if defined (RLIMIT_DATA) || defined (RLIMIT_VMEM) || defined (RLIMIT_AS)
3309 AP_INIT_TAKE12("RLimitMEM", set_limit_mem,
3310 (void*)APR_OFFSETOF(core_dir_config, limit_mem),
3311 OR_ALL, "Soft/hard limits for max memory usage per process"),
3313 AP_INIT_TAKE12("RLimitMEM", no_set_limit, NULL,
3314 OR_ALL, "Soft/hard limits for max memory usage per process"),
3317 AP_INIT_TAKE12("RLimitNPROC", set_limit_nproc,
3318 (void*)APR_OFFSETOF(core_dir_config, limit_nproc),
3319 OR_ALL, "soft/hard limits for max number of processes per uid"),
3321 AP_INIT_TAKE12("RLimitNPROC", no_set_limit, NULL,
3322 OR_ALL, "soft/hard limits for max number of processes per uid"),
3325 /* internal recursion stopper */
3326 AP_INIT_TAKE12("LimitInternalRecursion", set_recursion_limit, NULL, RSRC_CONF,
3327 "maximum recursion depth of internal redirects and subrequests"),
3329 AP_INIT_TAKE1("ForceType", ap_set_string_slot_lower,
3330 (void *)APR_OFFSETOF(core_dir_config, mime_type), OR_FILEINFO,
3331 "a mime type that overrides other configured type"),
3332 AP_INIT_TAKE1("SetHandler", ap_set_string_slot_lower,
3333 (void *)APR_OFFSETOF(core_dir_config, handler), OR_FILEINFO,
3334 "a handler name that overrides any other configured handler"),
3335 AP_INIT_TAKE1("SetOutputFilter", ap_set_string_slot,
3336 (void *)APR_OFFSETOF(core_dir_config, output_filters), OR_FILEINFO,
3337 "filter (or ; delimited list of filters) to be run on the request content"),
3338 AP_INIT_TAKE1("SetInputFilter", ap_set_string_slot,
3339 (void *)APR_OFFSETOF(core_dir_config, input_filters), OR_FILEINFO,
3340 "filter (or ; delimited list of filters) to be run on the request body"),
3341 AP_INIT_ITERATE2("AddOutputFilterByType", add_ct_output_filters,
3342 (void *)APR_OFFSETOF(core_dir_config, ct_output_filters), OR_FILEINFO,
3343 "output filter name followed by one or more content-types"),
3344 AP_INIT_FLAG("AllowEncodedSlashes", set_allow2f, NULL, RSRC_CONF,
3345 "Allow URLs containing '/' encoded as '%2F'"),
3348 * These are default configuration directives that mpms can/should
3349 * pay attention to. If an mpm wishes to use these, they should
3350 * #defined them in mpm.h.
3352 #ifdef AP_MPM_WANT_SET_PIDFILE
3353 AP_INIT_TAKE1("PidFile", ap_mpm_set_pidfile, NULL, RSRC_CONF,
3354 "A file for logging the server process ID"),
3356 #ifdef AP_MPM_WANT_SET_SCOREBOARD
3357 AP_INIT_TAKE1("ScoreBoardFile", ap_mpm_set_scoreboard, NULL, RSRC_CONF,
3358 "A file for Apache to maintain runtime process management information"),
3360 #ifdef AP_MPM_WANT_SET_LOCKFILE
3361 AP_INIT_TAKE1("LockFile", ap_mpm_set_lockfile, NULL, RSRC_CONF,
3362 "The lockfile used when Apache needs to lock the accept() call (deprecated)"),
3364 #ifdef AP_MPM_WANT_SET_MAX_REQUESTS
3365 AP_INIT_TAKE1("MaxRequestsPerChild", ap_mpm_set_max_requests, NULL, RSRC_CONF,
3366 "Maximum number of requests a particular child serves before dying."),
3368 #ifdef AP_MPM_WANT_SET_COREDUMPDIR
3369 AP_INIT_TAKE1("CoreDumpDirectory", ap_mpm_set_coredumpdir, NULL, RSRC_CONF,
3370 "The location of the directory Apache changes to before dumping core"),
3372 #ifdef AP_MPM_WANT_SET_ACCEPT_LOCK_MECH
3373 AP_INIT_TAKE1("AcceptMutex", ap_mpm_set_accept_lock_mech, NULL, RSRC_CONF,
3374 AP_AVAILABLE_MUTEXES_STRING),
3376 #ifdef AP_MPM_WANT_SET_MAX_MEM_FREE
3377 AP_INIT_TAKE1("MaxMemFree", ap_mpm_set_max_mem_free, NULL, RSRC_CONF,
3378 "Maximum number of 1k blocks a particular childs allocator may hold."),
3380 #ifdef AP_MPM_WANT_SET_STACKSIZE
3381 AP_INIT_TAKE1("ThreadStackSize", ap_mpm_set_thread_stacksize, NULL, RSRC_CONF,
3382 "Size in bytes of stack used by threads handling client connections"),
3384 #if AP_ENABLE_EXCEPTION_HOOK
3385 AP_INIT_TAKE1("EnableExceptionHook", ap_mpm_set_exception_hook, NULL, RSRC_CONF,
3386 "Controls whether exception hook may be called after a crash"),
3388 AP_INIT_TAKE1("TraceEnable", set_trace_enable, NULL, RSRC_CONF,
3389 "'on' (default), 'off' or 'extended' to trace request body content"),
3393 /*****************************************************************
3395 * Core handlers for various phases of server operation...
3398 AP_DECLARE_NONSTD(int) ap_core_translate(request_rec *r)
3400 void *sconf = r->server->module_config;
3401 core_server_config *conf = ap_get_module_config(sconf, &core_module);
3404 /* XXX this seems too specific, this should probably become
3405 * some general-case test
3408 return HTTP_FORBIDDEN;
3410 if (!r->uri || ((r->uri[0] != '/') && strcmp(r->uri, "*"))) {
3411 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
3412 "Invalid URI in request %s", r->the_request);
3413 return HTTP_BAD_REQUEST;
3417 && !strncmp(r->uri, r->server->path, r->server->pathlen)
3418 && (r->server->path[r->server->pathlen - 1] == '/'
3419 || r->uri[r->server->pathlen] == '/'
3420 || r->uri[r->server->pathlen] == '\0'))
3422 /* skip all leading /'s (e.g. http://localhost///foo)
3423 * so we are looking at only the relative path.
3425 char *path = r->uri + r->server->pathlen;
3426 while (*path == '/') {
3429 if ((rv = apr_filepath_merge(&r->filename, conf->ap_document_root, path,
3430 APR_FILEPATH_TRUENAME
3431 | APR_FILEPATH_SECUREROOT, r->pool))
3433 ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
3434 "Cannot map %s to file", r->the_request);
3435 return HTTP_FORBIDDEN;
3437 r->canonical_filename = r->filename;
3441 * Make sure that we do not mess up the translation by adding two
3442 * /'s in a row. This happens under windows when the document
3443 * root ends with a /
3445 /* skip all leading /'s (e.g. http://localhost///foo)
3446 * so we are looking at only the relative path.
3448 char *path = r->uri;
3449 while (*path == '/') {
3452 if ((rv = apr_filepath_merge(&r->filename, conf->ap_document_root, path,
3453 APR_FILEPATH_TRUENAME
3454 | APR_FILEPATH_SECUREROOT, r->pool))
3456 ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
3457 "Cannot map %s to file", r->the_request);
3458 return HTTP_FORBIDDEN;
3460 r->canonical_filename = r->filename;
3466 /*****************************************************************
3468 * Test the filesystem name through directory_walk and file_walk
3470 static int core_map_to_storage(request_rec *r)
3474 if ((access_status = ap_directory_walk(r))) {
3475 return access_status;
3478 if ((access_status = ap_file_walk(r))) {
3479 return access_status;
3486 static int do_nothing(request_rec *r) { return OK; }
3489 static int core_override_type(request_rec *r)
3491 core_dir_config *conf =
3492 (core_dir_config *)ap_get_module_config(r->per_dir_config,
3495 /* Check for overrides with ForceType / SetHandler
3497 if (conf->mime_type && strcmp(conf->mime_type, "none"))
3498 ap_set_content_type(r, (char*) conf->mime_type);
3500 if (conf->handler && strcmp(conf->handler, "none"))
3501 r->handler = conf->handler;
3503 /* Deal with the poor soul who is trying to force path_info to be
3504 * accepted within the core_handler, where they will let the subreq
3505 * address its contents. This is toggled by the user in the very
3506 * beginning of the fixup phase, so modules should override the user's
3507 * discretion in their own module fixup phase. It is tristate, if
3508 * the user doesn't specify, the result is 2 (which the module may
3509 * interpret to its own customary behavior.) It won't be touched
3510 * if the value is no longer undefined (2), so any module changing
3511 * the value prior to the fixup phase OVERRIDES the user's choice.
3513 if ((r->used_path_info == AP_REQ_DEFAULT_PATH_INFO)
3514 && (conf->accept_path_info != 3)) {
3515 r->used_path_info = conf->accept_path_info;
3521 static int default_handler(request_rec *r)
3523 conn_rec *c = r->connection;
3524 apr_bucket_brigade *bb;
3528 apr_file_t *fd = NULL;
3529 apr_status_t status;
3530 /* XXX if/when somebody writes a content-md5 filter we either need to
3531 * remove this support or coordinate when to use the filter vs.
3532 * when to use this code
3533 * The current choice of when to compute the md5 here matches the 1.3
3534 * support fairly closely (unlike 1.3, we don't handle computing md5
3535 * when the charset is translated).
3537 int bld_content_md5;
3539 d = (core_dir_config *)ap_get_module_config(r->per_dir_config,
3541 bld_content_md5 = (d->content_md5 & 1)
3542 && r->output_filters->frec->ftype != AP_FTYPE_RESOURCE;
3544 ap_allow_standard_methods(r, MERGE_ALLOW, M_GET, M_OPTIONS, M_POST, -1);
3546 /* If filters intend to consume the request body, they must
3547 * register an InputFilter to slurp the contents of the POST
3548 * data from the POST input stream. It no longer exists when
3549 * the output filters are invoked by the default handler.
3551 if ((errstatus = ap_discard_request_body(r)) != OK) {
3555 if (r->method_number == M_GET || r->method_number == M_POST) {
3556 if (r->finfo.filetype == 0) {
3557 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
3558 "File does not exist: %s", r->filename);
3559 return HTTP_NOT_FOUND;
3562 /* Don't try to serve a dir. Some OSs do weird things with
3565 if (r->finfo.filetype == APR_DIR) {
3566 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
3567 "Attempt to serve directory: %s", r->filename);
3568 return HTTP_NOT_FOUND;
3571 if ((r->used_path_info != AP_REQ_ACCEPT_PATH_INFO) &&
3572 r->path_info && *r->path_info)
3574 /* default to reject */
3575 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
3576 "File does not exist: %s",
3577 apr_pstrcat(r->pool, r->filename, r->path_info, NULL));
3578 return HTTP_NOT_FOUND;
3581 /* We understood the (non-GET) method, but it might not be legal for
3582 this particular resource. Check to see if the 'deliver_script'
3583 flag is set. If so, then we go ahead and deliver the file since
3584 it isn't really content (only GET normally returns content).
3586 Note: based on logic further above, the only possible non-GET
3587 method at this point is POST. In the future, we should enable
3588 script delivery for all methods. */
3589 if (r->method_number != M_GET) {
3590 core_request_config *req_cfg;
3592 req_cfg = ap_get_module_config(r->request_config, &core_module);
3593 if (!req_cfg->deliver_script) {
3594 /* The flag hasn't been set for this request. Punt. */
3595 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
3596 "This resource does not accept the %s method.",
3598 return HTTP_METHOD_NOT_ALLOWED;
3603 if ((status = apr_file_open(&fd, r->filename, APR_READ | APR_BINARY
3604 #if APR_HAS_SENDFILE
3605 | ((d->enable_sendfile == ENABLE_SENDFILE_OFF)
3606 ? 0 : APR_SENDFILE_ENABLED)
3608 , 0, r->pool)) != APR_SUCCESS) {
3609 ap_log_rerror(APLOG_MARK, APLOG_ERR, status, r,
3610 "file permissions deny server access: %s", r->filename);
3611 return HTTP_FORBIDDEN;
3614 ap_update_mtime(r, r->finfo.mtime);
3615 ap_set_last_modified(r);
3617 apr_table_setn(r->headers_out, "Accept-Ranges", "bytes");
3618 ap_set_content_length(r, r->finfo.size);
3620 bb = apr_brigade_create(r->pool, c->bucket_alloc);
3622 if ((errstatus = ap_meets_conditions(r)) != OK) {
3624 r->status = errstatus;
3627 if (bld_content_md5) {
3628 apr_table_setn(r->headers_out, "Content-MD5",
3629 ap_md5digest(r->pool, fd));
3632 e = apr_brigade_insert_file(bb, fd, 0, r->finfo.size, r->pool);
3635 if (d->enable_mmap == ENABLE_MMAP_OFF) {
3636 (void)apr_bucket_file_enable_mmap(e, 0);
3641 e = apr_bucket_eos_create(c->bucket_alloc);
3642 APR_BRIGADE_INSERT_TAIL(bb, e);
3644 status = ap_pass_brigade(r->output_filters, bb);
3645 if (status == APR_SUCCESS
3646 || r->status != HTTP_OK
3651 /* no way to know what type of error occurred */
3652 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, status, r,
3653 "default_handler: ap_pass_brigade returned %i",
3655 return HTTP_INTERNAL_SERVER_ERROR;
3658 else { /* unusual method (not GET or POST) */
3659 if (r->method_number == M_INVALID) {
3660 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
3661 "Invalid method in request %s", r->the_request);
3662 return HTTP_NOT_IMPLEMENTED;
3665 if (r->method_number == M_OPTIONS) {
3666 return ap_send_http_options(r);
3668 return HTTP_METHOD_NOT_ALLOWED;
3672 /* Optional function coming from mod_logio, used for logging of output
3675 APR_OPTIONAL_FN_TYPE(ap_logio_add_bytes_out) *logio_add_bytes_out;
3676 APR_OPTIONAL_FN_TYPE(authz_some_auth_required) *authz_ap_some_auth_required;
3678 static int core_post_config(apr_pool_t *pconf, apr_pool_t *plog, apr_pool_t *ptemp, server_rec *s)
3680 logio_add_bytes_out = APR_RETRIEVE_OPTIONAL_FN(ap_logio_add_bytes_out);
3681 ident_lookup = APR_RETRIEVE_OPTIONAL_FN(ap_ident_lookup);
3682 authz_ap_some_auth_required = APR_RETRIEVE_OPTIONAL_FN(authz_some_auth_required);
3683 authn_ap_auth_type = APR_RETRIEVE_OPTIONAL_FN(authn_ap_auth_type);
3684 authn_ap_auth_name = APR_RETRIEVE_OPTIONAL_FN(authn_ap_auth_name);
3685 access_compat_ap_satisfies = APR_RETRIEVE_OPTIONAL_FN(access_compat_ap_satisfies);
3688 ap_setup_make_content_type(pconf);
3692 static void core_insert_filter(request_rec *r)
3694 core_dir_config *conf = (core_dir_config *)
3695 ap_get_module_config(r->per_dir_config,
3697 const char *filter, *filters = conf->output_filters;
3700 while (*filters && (filter = ap_getword(r->pool, &filters, ';'))) {
3701 ap_add_output_filter(filter, NULL, r, r->connection);
3705 filters = conf->input_filters;
3707 while (*filters && (filter = ap_getword(r->pool, &filters, ';'))) {
3708 ap_add_input_filter(filter, NULL, r, r->connection);
3713 static apr_size_t num_request_notes = AP_NUM_STD_NOTES;
3715 static apr_status_t reset_request_notes(void *dummy)
3717 num_request_notes = AP_NUM_STD_NOTES;
3721 AP_DECLARE(apr_size_t) ap_register_request_note(void)
3723 apr_pool_cleanup_register(apr_hook_global_pool, NULL, reset_request_notes,
3724 apr_pool_cleanup_null);
3725 return num_request_notes++;
3728 AP_DECLARE(void **) ap_get_request_note(request_rec *r, apr_size_t note_num)
3730 core_request_config *req_cfg;
3732 if (note_num >= num_request_notes) {
3736 req_cfg = (core_request_config *)
3737 ap_get_module_config(r->request_config, &core_module);
3743 return &(req_cfg->notes[note_num]);
3746 static int core_create_req(request_rec *r)
3748 /* Alloc the config struct and the array of request notes in
3749 * a single block for efficiency
3751 core_request_config *req_cfg;
3753 req_cfg = apr_pcalloc(r->pool, sizeof(core_request_config) +
3754 sizeof(void *) * num_request_notes);
3755 req_cfg->notes = (void **)((char *)req_cfg + sizeof(core_request_config));
3757 /* ### temporarily enable script delivery as the default */
3758 req_cfg->deliver_script = 1;
3761 core_request_config *main_req_cfg = (core_request_config *)
3762 ap_get_module_config(r->main->request_config, &core_module);
3763 req_cfg->bb = main_req_cfg->bb;
3766 req_cfg->bb = apr_brigade_create(r->pool, r->connection->bucket_alloc);
3769 ap_set_module_config(r->request_config, &core_module, req_cfg);
3774 static int core_create_proxy_req(request_rec *r, request_rec *pr)
3776 return core_create_req(pr);
3779 static conn_rec *core_create_conn(apr_pool_t *ptrans, server_rec *server,
3780 apr_socket_t *csd, long id, void *sbh,
3781 apr_bucket_alloc_t *alloc)
3784 conn_rec *c = (conn_rec *) apr_pcalloc(ptrans, sizeof(conn_rec));
3787 (void)ap_update_child_status(c->sbh, SERVER_BUSY_READ, (request_rec *)NULL);
3789 /* Got a connection structure, so initialize what fields we can
3790 * (the rest are zeroed out by pcalloc).
3792 c->conn_config = ap_create_conn_config(ptrans);
3793 c->notes = apr_table_make(ptrans, 5);
3796 if ((rv = apr_socket_addr_get(&c->local_addr, APR_LOCAL, csd))
3798 ap_log_error(APLOG_MARK, APLOG_INFO, rv, server,
3799 "apr_socket_addr_get(APR_LOCAL)");
3800 apr_socket_close(csd);
3804 apr_sockaddr_ip_get(&c->local_ip, c->local_addr);
3805 if ((rv = apr_socket_addr_get(&c->remote_addr, APR_REMOTE, csd))
3807 ap_log_error(APLOG_MARK, APLOG_INFO, rv, server,
3808 "apr_socket_addr_get(APR_REMOTE)");
3809 apr_socket_close(csd);
3813 apr_sockaddr_ip_get(&c->remote_ip, c->remote_addr);
3814 c->base_server = server;
3817 c->bucket_alloc = alloc;
3819 c->cs = (conn_state_t *)apr_pcalloc(ptrans, sizeof(conn_state_t));
3820 APR_RING_INIT(&(c->cs->timeout_list), conn_state_t, timeout_list);
3821 c->cs->expiration_time = 0;
3822 c->cs->state = CONN_STATE_CHECK_REQUEST_LINE_READABLE;
3825 c->cs->bucket_alloc = alloc;
3826 c->clogging_input_filters = 0;
3831 static int core_pre_connection(conn_rec *c, void *csd)
3833 core_net_rec *net = apr_palloc(c->pool, sizeof(*net));
3836 #ifdef AP_MPM_DISABLE_NAGLE_ACCEPTED_SOCK
3837 /* The Nagle algorithm says that we should delay sending partial
3838 * packets in hopes of getting more data. We don't want to do
3839 * this; we are not telnet. There are bad interactions between
3840 * persistent connections and Nagle's algorithm that have very severe
3841 * performance penalties. (Failing to disable Nagle is not much of a
3842 * problem with simple HTTP.)
3844 rv = apr_socket_opt_set(csd, APR_TCP_NODELAY, 1);
3845 if (rv != APR_SUCCESS && rv != APR_ENOTIMPL) {
3846 /* expected cause is that the client disconnected already,
3847 * hence the debug level
3849 ap_log_cerror(APLOG_MARK, APLOG_DEBUG, rv, c,
3850 "apr_socket_opt_set(APR_TCP_NODELAY)");
3854 /* The core filter requires the timeout mode to be set, which
3855 * incidentally sets the socket to be nonblocking. If this
3856 * is not initialized correctly, Linux - for example - will
3857 * be initially blocking, while Solaris will be non blocking
3858 * and any initial read will fail.
3860 rv = apr_socket_timeout_set(csd, c->base_server->timeout);
3861 if (rv != APR_SUCCESS) {
3862 /* expected cause is that the client disconnected already */
3863 ap_log_cerror(APLOG_MARK, APLOG_DEBUG, rv, c,
3864 "apr_socket_timeout_set");
3869 net->out_ctx = NULL;
3870 net->client_socket = csd;
3872 ap_set_module_config(net->c->conn_config, &core_module, csd);
3873 ap_add_input_filter_handle(ap_core_input_filter_handle, net, NULL, net->c);
3874 ap_add_output_filter_handle(ap_core_output_filter_handle, net, NULL, net->c);
3878 static void register_hooks(apr_pool_t *p)
3880 /* create_connection and install_transport_filters are
3881 * hooks that should always be APR_HOOK_REALLY_LAST to give other
3882 * modules the opportunity to install alternate network transports
3883 * and stop other functions from being run.
3885 ap_hook_create_connection(core_create_conn, NULL, NULL,
3886 APR_HOOK_REALLY_LAST);
3887 ap_hook_pre_connection(core_pre_connection, NULL, NULL,
3888 APR_HOOK_REALLY_LAST);
3890 ap_hook_post_config(core_post_config,NULL,NULL,APR_HOOK_REALLY_FIRST);
3891 ap_hook_translate_name(ap_core_translate,NULL,NULL,APR_HOOK_REALLY_LAST);
3892 ap_hook_map_to_storage(core_map_to_storage,NULL,NULL,APR_HOOK_REALLY_LAST);
3893 ap_hook_open_logs(ap_open_logs,NULL,NULL,APR_HOOK_REALLY_FIRST);
3894 ap_hook_child_init(ap_logs_child_init,NULL,NULL,APR_HOOK_MIDDLE);
3895 ap_hook_handler(default_handler,NULL,NULL,APR_HOOK_REALLY_LAST);
3896 /* FIXME: I suspect we can eliminate the need for these do_nothings - Ben */
3897 ap_hook_type_checker(do_nothing,NULL,NULL,APR_HOOK_REALLY_LAST);
3898 ap_hook_fixups(core_override_type,NULL,NULL,APR_HOOK_REALLY_FIRST);
3899 ap_hook_access_checker(do_nothing,NULL,NULL,APR_HOOK_REALLY_LAST);
3900 ap_hook_create_request(core_create_req, NULL, NULL, APR_HOOK_MIDDLE);
3901 APR_OPTIONAL_HOOK(proxy, create_req, core_create_proxy_req, NULL, NULL,
3903 ap_hook_pre_mpm(ap_create_scoreboard, NULL, NULL, APR_HOOK_MIDDLE);
3905 /* register the core's insert_filter hook and register core-provided
3908 ap_hook_insert_filter(core_insert_filter, NULL, NULL, APR_HOOK_MIDDLE);
3910 ap_core_input_filter_handle =
3911 ap_register_input_filter("CORE_IN", ap_core_input_filter,
3912 NULL, AP_FTYPE_NETWORK);
3913 ap_content_length_filter_handle =
3914 ap_register_output_filter("CONTENT_LENGTH", ap_content_length_filter,
3915 NULL, AP_FTYPE_PROTOCOL);
3916 ap_core_output_filter_handle =
3917 ap_register_output_filter("CORE", ap_core_output_filter,
3918 NULL, AP_FTYPE_NETWORK);
3919 ap_subreq_core_filter_handle =
3920 ap_register_output_filter("SUBREQ_CORE", ap_sub_req_output_filter,
3921 NULL, AP_FTYPE_CONTENT_SET);
3923 ap_register_output_filter("OLD_WRITE", ap_old_write_filter,
3924 NULL, AP_FTYPE_RESOURCE - 10);
3927 AP_DECLARE_DATA module core_module = {
3928 STANDARD20_MODULE_STUFF,
3929 create_core_dir_config, /* create per-directory config structure */
3930 merge_core_dir_configs, /* merge per-directory config structures */
3931 create_core_server_config, /* create per-server config structure */
3932 merge_core_server_configs, /* merge per-server config structures */
3933 core_cmds, /* command apr_table_t */
3934 register_hooks /* register hooks */