1 /* ====================================================================
2 * The Apache Software License, Version 1.1
4 * Copyright (c) 2000-2002 The Apache Software Foundation. All rights
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the
19 * 3. The end-user documentation included with the redistribution,
20 * if any, must include the following acknowledgment:
21 * "This product includes software developed by the
22 * Apache Software Foundation (http://www.apache.org/)."
23 * Alternately, this acknowledgment may appear in the software itself,
24 * if and wherever such third-party acknowledgments normally appear.
26 * 4. The names "Apache" and "Apache Software Foundation" must
27 * not be used to endorse or promote products derived from this
28 * software without prior written permission. For written
29 * permission, please contact apache@apache.org.
31 * 5. Products derived from this software may not be called "Apache",
32 * nor may "Apache" appear in their name, without prior written
33 * permission of the Apache Software Foundation.
35 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
36 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
37 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
38 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
39 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
42 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
43 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
44 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
45 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47 * ====================================================================
49 * This software consists of voluntary contributions made by many
50 * individuals on behalf of the Apache Software Foundation. For more
51 * information on the Apache Software Foundation, please see
52 * <http://www.apache.org/>.
54 * Portions of this software are based upon public domain software
55 * originally written at the National Center for Supercomputing Applications,
56 * University of Illinois, Urbana-Champaign.
60 #include "apr_strings.h"
62 #include "apr_fnmatch.h"
64 #include "apr_thread_proc.h" /* for RLIMIT stuff */
65 #include "apr_hooks.h"
67 #define APR_WANT_IOVEC
68 #define APR_WANT_STRFUNC
69 #define APR_WANT_MEMFUNC
73 #include "ap_config.h"
75 #include "http_config.h"
76 #include "http_core.h"
77 #include "http_protocol.h" /* For index_of_response(). Grump. */
78 #include "http_request.h"
79 #include "http_vhost.h"
80 #include "http_main.h" /* For the default_handler below... */
84 #include "http_connection.h"
85 #include "apr_buckets.h"
86 #include "util_filter.h"
87 #include "util_ebcdic.h"
89 #include "mpm_common.h"
90 #include "scoreboard.h"
92 #include "mod_proxy.h"
93 #include "ap_listen.h"
95 /* LimitXMLRequestBody handling */
96 #define AP_LIMIT_UNSET ((long) -1)
97 #define AP_DEFAULT_LIMIT_XML_BODY ((size_t)1000000)
99 #define AP_MIN_SENDFILE_BYTES (256)
102 APR_HOOK_LINK(get_mgmt_items)
105 AP_IMPLEMENT_HOOK_RUN_ALL(int, get_mgmt_items,
106 (apr_pool_t *p, const char *val, apr_hash_t *ht),
107 (p, val, ht), OK, DECLINED)
109 /* Server core module... This module provides support for really basic
110 * server operations, including options and commands which control the
111 * operation of other modules. Consider this the bureaucracy module.
113 * The core module also defines handlers, etc., do handle just enough
114 * to allow a server with the core module ONLY to actually serve documents
115 * (though it slaps DefaultType on all of 'em); this was useful in testing,
116 * but may not be worth preserving.
118 * This file could almost be mod_core.c, except for the stuff which affects
119 * the http_conf_globals.
122 /* Handles for core filters */
123 AP_DECLARE_DATA ap_filter_rec_t *ap_subreq_core_filter_handle;
124 AP_DECLARE_DATA ap_filter_rec_t *ap_core_output_filter_handle;
125 AP_DECLARE_DATA ap_filter_rec_t *ap_content_length_filter_handle;
126 AP_DECLARE_DATA ap_filter_rec_t *ap_net_time_filter_handle;
127 AP_DECLARE_DATA ap_filter_rec_t *ap_core_input_filter_handle;
129 static void *create_core_dir_config(apr_pool_t *a, char *dir)
131 core_dir_config *conf;
133 conf = (core_dir_config *)apr_pcalloc(a, sizeof(core_dir_config));
135 /* conf->r and conf->d[_*] are initialized by dirsection() or left NULL */
137 conf->opts = dir ? OPT_UNSET : OPT_UNSET|OPT_ALL;
138 conf->opts_add = conf->opts_remove = OPT_NONE;
139 conf->override = dir ? OR_UNSET : OR_UNSET|OR_ALL;
141 conf->content_md5 = 2;
142 conf->accept_path_info = 3;
144 conf->use_canonical_name = USE_CANONICAL_NAME_UNSET;
146 conf->hostname_lookups = HOSTNAME_LOOKUP_UNSET;
147 conf->do_rfc1413 = DEFAULT_RFC1413 | 2; /* set bit 1 to indicate default */
148 conf->satisfy = SATISFY_NOSPEC;
151 conf->limit_cpu = NULL;
153 #if defined(RLIMIT_DATA) || defined(RLIMIT_VMEM) || defined(RLIMIT_AS)
154 conf->limit_mem = NULL;
157 conf->limit_nproc = NULL;
160 conf->limit_req_body = 0;
161 conf->limit_xml_body = AP_LIMIT_UNSET;
162 conf->sec_file = apr_array_make(a, 2, sizeof(ap_conf_vector_t *));
164 conf->server_signature = srv_sig_unset;
166 conf->add_default_charset = ADD_DEFAULT_CHARSET_UNSET;
167 conf->add_default_charset_name = DEFAULT_ADD_DEFAULT_CHARSET_NAME;
169 /* Overriding all negotiation
171 conf->mime_type = NULL;
172 conf->handler = NULL;
173 conf->output_filters = NULL;
174 conf->input_filters = NULL;
177 * Flag for use of inodes in ETags.
179 conf->etag_bits = ETAG_UNSET;
180 conf->etag_add = ETAG_UNSET;
181 conf->etag_remove = ETAG_UNSET;
183 conf->enable_mmap = ENABLE_MMAP_UNSET;
184 conf->enable_sendfile = ENABLE_SENDFILE_UNSET;
190 * Overlay one hash table of ct_output_filters onto another
192 static void *merge_ct_filters(apr_pool_t *p,
195 const void *overlay_val,
196 const void *base_val,
199 ap_filter_rec_t *cur;
200 const ap_filter_rec_t *overlay_info = (const ap_filter_rec_t *)overlay_val;
201 const ap_filter_rec_t *base_info = (const ap_filter_rec_t *)base_val;
205 while (overlay_info) {
206 ap_filter_rec_t *new;
208 new = apr_pcalloc(p, sizeof(ap_filter_rec_t));
209 new->name = apr_pstrdup(p, overlay_info->name);
212 overlay_info = overlay_info->next;
219 /* We can't have dups. */
222 if (!strcasecmp(base_info->name, f->name)) {
231 f = apr_pcalloc(p, sizeof(ap_filter_rec_t));
232 f->name = apr_pstrdup(p, base_info->name);
237 base_info = base_info->next;
243 static void *merge_core_dir_configs(apr_pool_t *a, void *basev, void *newv)
245 core_dir_config *base = (core_dir_config *)basev;
246 core_dir_config *new = (core_dir_config *)newv;
247 core_dir_config *conf;
250 /* Create this conf by duplicating the base, replacing elements
251 * (or creating copies for merging) where new-> values exist.
253 conf = (core_dir_config *)apr_palloc(a, sizeof(core_dir_config));
254 memcpy(conf, base, sizeof(core_dir_config));
257 conf->d_is_fnmatch = new->d_is_fnmatch;
258 conf->d_components = new->d_components;
261 if (new->opts & OPT_UNSET) {
262 /* there was no explicit setting of new->opts, so we merge
263 * preserve the invariant (opts_add & opts_remove) == 0
265 conf->opts_add = (conf->opts_add & ~new->opts_remove) | new->opts_add;
266 conf->opts_remove = (conf->opts_remove & ~new->opts_add)
268 conf->opts = (conf->opts & ~conf->opts_remove) | conf->opts_add;
269 if ((base->opts & OPT_INCNOEXEC) && (new->opts & OPT_INCLUDES)) {
270 conf->opts = (conf->opts & ~OPT_INCNOEXEC) | OPT_INCLUDES;
274 /* otherwise we just copy, because an explicit opts setting
275 * overrides all earlier +/- modifiers
277 conf->opts = new->opts;
278 conf->opts_add = new->opts_add;
279 conf->opts_remove = new->opts_remove;
282 if (!(new->override & OR_UNSET)) {
283 conf->override = new->override;
286 if (new->ap_default_type) {
287 conf->ap_default_type = new->ap_default_type;
290 if (new->ap_auth_type) {
291 conf->ap_auth_type = new->ap_auth_type;
294 if (new->ap_auth_name) {
295 conf->ap_auth_name = new->ap_auth_name;
298 if (new->ap_requires) {
299 conf->ap_requires = new->ap_requires;
302 if (conf->response_code_strings == NULL) {
303 conf->response_code_strings = new->response_code_strings;
305 else if (new->response_code_strings != NULL) {
306 /* If we merge, the merge-result must have it's own array
308 conf->response_code_strings = apr_palloc(a,
309 sizeof(*conf->response_code_strings) * RESPONSE_CODES);
310 memcpy(conf->response_code_strings, base->response_code_strings,
311 sizeof(*conf->response_code_strings) * RESPONSE_CODES);
313 for (i = 0; i < RESPONSE_CODES; ++i) {
314 if (new->response_code_strings[i] != NULL) {
315 conf->response_code_strings[i] = new->response_code_strings[i];
319 /* Otherwise we simply use the base->response_code_strings array
322 if (new->hostname_lookups != HOSTNAME_LOOKUP_UNSET) {
323 conf->hostname_lookups = new->hostname_lookups;
326 if ((new->do_rfc1413 & 2) == 0) {
327 conf->do_rfc1413 = new->do_rfc1413;
330 if ((new->content_md5 & 2) == 0) {
331 conf->content_md5 = new->content_md5;
334 if (new->accept_path_info != 3) {
335 conf->accept_path_info = new->accept_path_info;
338 if (new->use_canonical_name != USE_CANONICAL_NAME_UNSET) {
339 conf->use_canonical_name = new->use_canonical_name;
343 if (new->limit_cpu) {
344 conf->limit_cpu = new->limit_cpu;
348 #if defined(RLIMIT_DATA) || defined(RLIMIT_VMEM) || defined(RLIMIT_AS)
349 if (new->limit_mem) {
350 conf->limit_mem = new->limit_mem;
355 if (new->limit_nproc) {
356 conf->limit_nproc = new->limit_nproc;
360 if (new->limit_req_body) {
361 conf->limit_req_body = new->limit_req_body;
364 if (new->limit_xml_body != AP_LIMIT_UNSET)
365 conf->limit_xml_body = new->limit_xml_body;
367 conf->limit_xml_body = base->limit_xml_body;
369 if (!conf->sec_file) {
370 conf->sec_file = new->sec_file;
372 else if (new->sec_file) {
373 /* If we merge, the merge-result must have it's own array
375 conf->sec_file = apr_array_append(a, base->sec_file, new->sec_file);
377 /* Otherwise we simply use the base->sec_file array
380 if (new->satisfy != SATISFY_NOSPEC) {
381 conf->satisfy = new->satisfy;
384 if (new->server_signature != srv_sig_unset) {
385 conf->server_signature = new->server_signature;
388 if (new->add_default_charset != ADD_DEFAULT_CHARSET_UNSET) {
389 conf->add_default_charset = new->add_default_charset;
390 conf->add_default_charset_name = new->add_default_charset_name;
393 /* Overriding all negotiation
395 if (new->mime_type) {
396 conf->mime_type = new->mime_type;
400 conf->handler = new->handler;
403 if (new->output_filters) {
404 conf->output_filters = new->output_filters;
407 if (new->input_filters) {
408 conf->input_filters = new->input_filters;
411 if (conf->ct_output_filters && new->ct_output_filters) {
412 conf->ct_output_filters = apr_hash_merge(a,
413 new->ct_output_filters,
414 conf->ct_output_filters,
418 else if (new->ct_output_filters) {
419 conf->ct_output_filters = apr_hash_copy(a, new->ct_output_filters);
421 else if (conf->ct_output_filters) {
422 /* That memcpy above isn't enough. */
423 conf->ct_output_filters = apr_hash_copy(a, base->ct_output_filters);
427 * Now merge the setting of the FileETag directive.
429 if (new->etag_bits == ETAG_UNSET) {
431 (conf->etag_add & (~ new->etag_remove)) | new->etag_add;
433 (conf->opts_remove & (~ new->etag_add)) | new->etag_remove;
435 (conf->etag_bits & (~ conf->etag_remove)) | conf->etag_add;
438 conf->etag_bits = new->etag_bits;
439 conf->etag_add = new->etag_add;
440 conf->etag_remove = new->etag_remove;
443 if (conf->etag_bits != ETAG_NONE) {
444 conf->etag_bits &= (~ ETAG_NONE);
447 if (new->enable_mmap != ENABLE_MMAP_UNSET) {
448 conf->enable_mmap = new->enable_mmap;
451 if (new->enable_sendfile != ENABLE_SENDFILE_UNSET) {
452 conf->enable_sendfile = new->enable_sendfile;
458 static void *create_core_server_config(apr_pool_t *a, server_rec *s)
460 core_server_config *conf;
461 int is_virtual = s->is_virtual;
463 conf = (core_server_config *)apr_pcalloc(a, sizeof(core_server_config));
466 conf->gprof_dir = NULL;
469 conf->access_name = is_virtual ? NULL : DEFAULT_ACCESS_FNAME;
470 conf->ap_document_root = is_virtual ? NULL : DOCUMENT_LOCATION;
471 conf->sec_dir = apr_array_make(a, 40, sizeof(ap_conf_vector_t *));
472 conf->sec_url = apr_array_make(a, 40, sizeof(ap_conf_vector_t *));
477 static void *merge_core_server_configs(apr_pool_t *p, void *basev, void *virtv)
479 core_server_config *base = (core_server_config *)basev;
480 core_server_config *virt = (core_server_config *)virtv;
481 core_server_config *conf;
483 conf = (core_server_config *)apr_palloc(p, sizeof(core_server_config));
484 memcpy(conf, virt, sizeof(core_server_config));
486 if (!conf->access_name) {
487 conf->access_name = base->access_name;
490 if (!conf->ap_document_root) {
491 conf->ap_document_root = base->ap_document_root;
494 conf->sec_dir = apr_array_append(p, base->sec_dir, virt->sec_dir);
495 conf->sec_url = apr_array_append(p, base->sec_url, virt->sec_url);
500 /* Add per-directory configuration entry (for <directory> section);
501 * these are part of the core server config.
504 AP_CORE_DECLARE(void) ap_add_per_dir_conf(server_rec *s, void *dir_config)
506 core_server_config *sconf = ap_get_module_config(s->module_config,
508 void **new_space = (void **)apr_array_push(sconf->sec_dir);
510 *new_space = dir_config;
513 AP_CORE_DECLARE(void) ap_add_per_url_conf(server_rec *s, void *url_config)
515 core_server_config *sconf = ap_get_module_config(s->module_config,
517 void **new_space = (void **)apr_array_push(sconf->sec_url);
519 *new_space = url_config;
522 AP_CORE_DECLARE(void) ap_add_file_conf(core_dir_config *conf, void *url_config)
524 void **new_space = (void **)apr_array_push(conf->sec_file);
526 *new_space = url_config;
529 /* We need to do a stable sort, qsort isn't stable. So to make it stable
530 * we'll be maintaining the original index into the list, and using it
531 * as the minor key during sorting. The major key is the number of
532 * components (where the root component is zero).
534 struct reorder_sort_rec {
535 ap_conf_vector_t *elt;
539 static int reorder_sorter(const void *va, const void *vb)
541 const struct reorder_sort_rec *a = va;
542 const struct reorder_sort_rec *b = vb;
543 core_dir_config *core_a;
544 core_dir_config *core_b;
546 core_a = ap_get_module_config(a->elt, &core_module);
547 core_b = ap_get_module_config(b->elt, &core_module);
549 /* a regex always sorts after a non-regex
551 if (!core_a->r && core_b->r) {
554 else if (core_a->r && !core_b->r) {
558 /* we always sort next by the number of components
560 if (core_a->d_components < core_b->d_components) {
563 else if (core_a->d_components > core_b->d_components) {
567 /* They have the same number of components, we now have to compare
568 * the minor key to maintain the original order (from the config.)
570 return a->orig_index - b->orig_index;
573 void ap_core_reorder_directories(apr_pool_t *p, server_rec *s)
575 core_server_config *sconf;
576 apr_array_header_t *sec_dir;
577 struct reorder_sort_rec *sortbin;
579 ap_conf_vector_t **elts;
583 sconf = ap_get_module_config(s->module_config, &core_module);
584 sec_dir = sconf->sec_dir;
585 nelts = sec_dir->nelts;
586 elts = (ap_conf_vector_t **)sec_dir->elts;
589 /* simple case of already being sorted... */
590 /* We're not checking this condition to be fast... we're checking
591 * it to avoid trying to palloc zero bytes, which can trigger some
592 * memory debuggers to barf
597 /* we have to allocate tmp space to do a stable sort */
598 apr_pool_create(&tmp, p);
599 sortbin = apr_palloc(tmp, sec_dir->nelts * sizeof(*sortbin));
600 for (i = 0; i < nelts; ++i) {
601 sortbin[i].orig_index = i;
602 sortbin[i].elt = elts[i];
605 qsort(sortbin, nelts, sizeof(*sortbin), reorder_sorter);
607 /* and now copy back to the original array */
608 for (i = 0; i < nelts; ++i) {
609 elts[i] = sortbin[i].elt;
612 apr_pool_destroy(tmp);
615 /*****************************************************************
617 * There are some elements of the core config structures in which
618 * other modules have a legitimate interest (this is ugly, but necessary
619 * to preserve NCSA back-compatibility). So, we have a bunch of accessors
623 AP_DECLARE(int) ap_allow_options(request_rec *r)
625 core_dir_config *conf =
626 (core_dir_config *)ap_get_module_config(r->per_dir_config, &core_module);
631 AP_DECLARE(int) ap_allow_overrides(request_rec *r)
633 core_dir_config *conf;
634 conf = (core_dir_config *)ap_get_module_config(r->per_dir_config,
637 return conf->override;
640 AP_DECLARE(const char *) ap_auth_type(request_rec *r)
642 core_dir_config *conf;
644 conf = (core_dir_config *)ap_get_module_config(r->per_dir_config,
647 return conf->ap_auth_type;
650 AP_DECLARE(const char *) ap_auth_name(request_rec *r)
652 core_dir_config *conf;
654 conf = (core_dir_config *)ap_get_module_config(r->per_dir_config,
657 return conf->ap_auth_name;
660 AP_DECLARE(const char *) ap_default_type(request_rec *r)
662 core_dir_config *conf;
664 conf = (core_dir_config *)ap_get_module_config(r->per_dir_config,
667 return conf->ap_default_type
668 ? conf->ap_default_type
669 : DEFAULT_CONTENT_TYPE;
672 AP_DECLARE(const char *) ap_document_root(request_rec *r) /* Don't use this! */
674 core_server_config *conf;
676 conf = (core_server_config *)ap_get_module_config(r->server->module_config,
679 return conf->ap_document_root;
682 AP_DECLARE(const apr_array_header_t *) ap_requires(request_rec *r)
684 core_dir_config *conf;
686 conf = (core_dir_config *)ap_get_module_config(r->per_dir_config,
689 return conf->ap_requires;
692 AP_DECLARE(int) ap_satisfies(request_rec *r)
694 core_dir_config *conf;
696 conf = (core_dir_config *)ap_get_module_config(r->per_dir_config,
699 return conf->satisfy;
702 /* Should probably just get rid of this... the only code that cares is
703 * part of the core anyway (and in fact, it isn't publicised to other
707 char *ap_response_code_string(request_rec *r, int error_index)
709 core_dir_config *conf;
711 conf = (core_dir_config *)ap_get_module_config(r->per_dir_config,
714 if (conf->response_code_strings == NULL) {
718 return conf->response_code_strings[error_index];
722 /* Code from Harald Hanche-Olsen <hanche@imf.unit.no> */
723 static APR_INLINE void do_double_reverse (conn_rec *conn)
728 if (conn->double_reverse) {
733 if (conn->remote_host == NULL || conn->remote_host[0] == '\0') {
734 /* single reverse failed, so don't bother */
735 conn->double_reverse = -1;
739 rv = apr_sockaddr_info_get(&sa, conn->remote_host, APR_UNSPEC, 0, 0, conn->pool);
740 if (rv == APR_SUCCESS) {
742 if (apr_sockaddr_equal(sa, conn->remote_addr)) {
743 conn->double_reverse = 1;
751 conn->double_reverse = -1;
754 AP_DECLARE(const char *) ap_get_remote_host(conn_rec *conn, void *dir_config,
755 int type, int *str_is_ip)
757 int hostname_lookups;
759 if (str_is_ip) { /* if caller wants to know */
763 /* If we haven't checked the host name, and we want to */
766 ((core_dir_config *)ap_get_module_config(dir_config, &core_module))
769 if (hostname_lookups == HOSTNAME_LOOKUP_UNSET) {
770 hostname_lookups = HOSTNAME_LOOKUP_OFF;
775 hostname_lookups = HOSTNAME_LOOKUP_OFF;
778 if (type != REMOTE_NOLOOKUP
779 && conn->remote_host == NULL
780 && (type == REMOTE_DOUBLE_REV
781 || hostname_lookups != HOSTNAME_LOOKUP_OFF)) {
783 if (apr_getnameinfo(&conn->remote_host, conn->remote_addr, 0)
785 ap_str_tolower(conn->remote_host);
787 if (hostname_lookups == HOSTNAME_LOOKUP_DOUBLE) {
788 do_double_reverse(conn);
789 if (conn->double_reverse != 1) {
790 conn->remote_host = NULL;
795 /* if failed, set it to the NULL string to indicate error */
796 if (conn->remote_host == NULL) {
797 conn->remote_host = "";
801 if (type == REMOTE_DOUBLE_REV) {
802 do_double_reverse(conn);
803 if (conn->double_reverse == -1) {
809 * Return the desired information; either the remote DNS name, if found,
810 * or either NULL (if the hostname was requested) or the IP address
811 * (if any identifier was requested).
813 if (conn->remote_host != NULL && conn->remote_host[0] != '\0') {
814 return conn->remote_host;
817 if (type == REMOTE_HOST || type == REMOTE_DOUBLE_REV) {
821 if (str_is_ip) { /* if caller wants to know */
825 return conn->remote_ip;
830 AP_DECLARE(const char *) ap_get_remote_logname(request_rec *r)
832 core_dir_config *dir_conf;
834 if (r->connection->remote_logname != NULL) {
835 return r->connection->remote_logname;
838 /* If we haven't checked the identity, and we want to */
839 dir_conf = (core_dir_config *)ap_get_module_config(r->per_dir_config,
842 if (dir_conf->do_rfc1413 & 1) {
843 return ap_rfc1413(r->connection, r->server);
850 /* There are two options regarding what the "name" of a server is. The
851 * "canonical" name as defined by ServerName and Port, or the "client's
852 * name" as supplied by a possible Host: header or full URI. We never
853 * trust the port passed in the client's headers, we always use the
854 * port of the actual socket.
856 * The DNS option to UseCanonicalName causes this routine to do a
857 * reverse lookup on the local IP address of the connection and use
858 * that for the ServerName. This makes its value more reliable while
859 * at the same time allowing Demon's magic virtual hosting to work.
860 * The assumption is that DNS lookups are sufficiently quick...
863 AP_DECLARE(const char *) ap_get_server_name(request_rec *r)
865 conn_rec *conn = r->connection;
868 d = (core_dir_config *)ap_get_module_config(r->per_dir_config,
871 if (d->use_canonical_name == USE_CANONICAL_NAME_OFF) {
872 return r->hostname ? r->hostname : r->server->server_hostname;
875 if (d->use_canonical_name == USE_CANONICAL_NAME_DNS) {
876 if (conn->local_host == NULL) {
877 if (apr_getnameinfo(&conn->local_host,
878 conn->local_addr, 0) != APR_SUCCESS)
879 conn->local_host = apr_pstrdup(conn->pool,
880 r->server->server_hostname);
882 ap_str_tolower(conn->local_host);
886 return conn->local_host;
890 return r->server->server_hostname;
893 AP_DECLARE(apr_port_t) ap_get_server_port(const request_rec *r)
897 (core_dir_config *)ap_get_module_config(r->per_dir_config, &core_module);
899 if (d->use_canonical_name == USE_CANONICAL_NAME_OFF
900 || d->use_canonical_name == USE_CANONICAL_NAME_DNS) {
902 /* With UseCanonicalName off Apache will form self-referential
903 * URLs using the hostname and port supplied by the client if
904 * any are supplied (otherwise it will use the canonical name).
906 port = r->parsed_uri.port ? r->parsed_uri.port :
907 r->server->port ? r->server->port :
910 else { /* d->use_canonical_name == USE_CANONICAL_NAME_ON */
912 /* With UseCanonicalName on (and in all versions prior to 1.3)
913 * Apache will use the hostname and port specified in the
914 * ServerName directive to construct a canonical name for the
915 * server. (If no port was specified in the ServerName
916 * directive, Apache uses the port supplied by the client if
917 * any is supplied, and finally the default port for the protocol
920 port = r->server->port ? r->server->port :
921 r->connection->local_addr->port ? r->connection->local_addr->port :
929 AP_DECLARE(char *) ap_construct_url(apr_pool_t *p, const char *uri,
932 unsigned port = ap_get_server_port(r);
933 const char *host = ap_get_server_name(r);
935 if (ap_is_default_port(port, r)) {
936 return apr_pstrcat(p, ap_http_method(r), "://", host, uri, NULL);
939 return apr_psprintf(p, "%s://%s:%u%s", ap_http_method(r), host, port, uri);
942 AP_DECLARE(apr_off_t) ap_get_limit_req_body(const request_rec *r)
945 (core_dir_config *)ap_get_module_config(r->per_dir_config, &core_module);
947 return d->limit_req_body;
951 /*****************************************************************
953 * Commands... this module handles almost all of the NCSA httpd.conf
954 * commands, but most of the old srm.conf is in the the modules.
958 /* returns a parent if it matches the given directive */
959 static const ap_directive_t * find_parent(const ap_directive_t *dirp,
962 while (dirp->parent != NULL) {
965 /* ### it would be nice to have atom-ized directives */
966 if (strcasecmp(dirp->directive, what) == 0)
973 AP_DECLARE(const char *) ap_check_cmd_context(cmd_parms *cmd,
976 const char *gt = (cmd->cmd->name[0] == '<'
977 && cmd->cmd->name[strlen(cmd->cmd->name)-1] != '>')
979 const ap_directive_t *found;
981 if ((forbidden & NOT_IN_VIRTUALHOST) && cmd->server->is_virtual) {
982 return apr_pstrcat(cmd->pool, cmd->cmd->name, gt,
983 " cannot occur within <VirtualHost> section", NULL);
986 if ((forbidden & NOT_IN_LIMIT) && cmd->limited != -1) {
987 return apr_pstrcat(cmd->pool, cmd->cmd->name, gt,
988 " cannot occur within <Limit> section", NULL);
991 if ((forbidden & NOT_IN_DIR_LOC_FILE) == NOT_IN_DIR_LOC_FILE) {
992 if (cmd->path != NULL) {
993 return apr_pstrcat(cmd->pool, cmd->cmd->name, gt,
994 " cannot occur within <Directory/Location/Files> "
997 if (cmd->cmd->req_override & EXEC_ON_READ) {
998 /* EXEC_ON_READ must be NOT_IN_DIR_LOC_FILE, if not, it will
999 * (deliberately) segfault below in the individual tests...
1005 if (((forbidden & NOT_IN_DIRECTORY)
1006 && ((found = find_parent(cmd->directive, "<Directory"))
1007 || (found = find_parent(cmd->directive, "<DirectoryMatch"))))
1008 || ((forbidden & NOT_IN_LOCATION)
1009 && ((found = find_parent(cmd->directive, "<Location"))
1010 || (found = find_parent(cmd->directive, "<LocationMatch"))))
1011 || ((forbidden & NOT_IN_FILES)
1012 && ((found = find_parent(cmd->directive, "<Files"))
1013 || (found = find_parent(cmd->directive, "<FilesMatch"))))) {
1014 return apr_pstrcat(cmd->pool, cmd->cmd->name, gt,
1015 " cannot occur within ", found->directive,
1022 static const char *set_access_name(cmd_parms *cmd, void *dummy,
1025 void *sconf = cmd->server->module_config;
1026 core_server_config *conf = ap_get_module_config(sconf, &core_module);
1028 const char *err = ap_check_cmd_context(cmd,
1029 NOT_IN_DIR_LOC_FILE|NOT_IN_LIMIT);
1034 conf->access_name = apr_pstrdup(cmd->pool, arg);
1039 static const char *set_gprof_dir(cmd_parms *cmd, void *dummy, const char *arg)
1041 void *sconf = cmd->server->module_config;
1042 core_server_config *conf = ap_get_module_config(sconf, &core_module);
1044 const char *err = ap_check_cmd_context(cmd,
1045 NOT_IN_DIR_LOC_FILE|NOT_IN_LIMIT);
1050 conf->gprof_dir = apr_pstrdup(cmd->pool, arg);
1055 static const char *set_add_default_charset(cmd_parms *cmd,
1056 void *d_, const char *arg)
1058 core_dir_config *d = d_;
1060 const char *err = ap_check_cmd_context(cmd, NOT_IN_LIMIT);
1065 if (!strcasecmp(arg, "Off")) {
1066 d->add_default_charset = ADD_DEFAULT_CHARSET_OFF;
1068 else if (!strcasecmp(arg, "On")) {
1069 d->add_default_charset = ADD_DEFAULT_CHARSET_ON;
1070 d->add_default_charset_name = DEFAULT_ADD_DEFAULT_CHARSET_NAME;
1073 d->add_default_charset = ADD_DEFAULT_CHARSET_ON;
1074 d->add_default_charset_name = arg;
1080 static const char *set_document_root(cmd_parms *cmd, void *dummy,
1083 void *sconf = cmd->server->module_config;
1084 core_server_config *conf = ap_get_module_config(sconf, &core_module);
1086 const char *err = ap_check_cmd_context(cmd,
1087 NOT_IN_DIR_LOC_FILE|NOT_IN_LIMIT);
1092 /* TODO: ap_configtestonly && ap_docrootcheck && */
1093 /* XXX Shouldn't this be relative to ServerRoot ??? */
1094 if (apr_filepath_merge((char**)&conf->ap_document_root, NULL, arg,
1095 APR_FILEPATH_TRUENAME, cmd->pool) != APR_SUCCESS
1096 || !ap_is_directory(cmd->pool, arg)) {
1097 if (cmd->server->is_virtual) {
1098 ap_log_perror(APLOG_MARK, APLOG_STARTUP, 0,
1100 "Warning: DocumentRoot [%s] does not exist",
1102 conf->ap_document_root = arg;
1105 return "DocumentRoot must be a directory";
1111 AP_DECLARE(void) ap_custom_response(request_rec *r, int status,
1114 core_dir_config *conf =
1115 ap_get_module_config(r->per_dir_config, &core_module);
1118 if(conf->response_code_strings == NULL) {
1119 conf->response_code_strings =
1120 apr_pcalloc(r->pool,
1121 sizeof(*conf->response_code_strings) * RESPONSE_CODES);
1124 idx = ap_index_of_response(status);
1126 conf->response_code_strings[idx] =
1127 ((ap_is_url(string) || (*string == '/')) && (*string != '"')) ?
1128 apr_pstrdup(r->pool, string) : apr_pstrcat(r->pool, "\"", string, NULL);
1131 static const char *set_error_document(cmd_parms *cmd, void *conf_,
1132 const char *errno_str, const char *msg)
1134 core_dir_config *conf = conf_;
1135 int error_number, index_number, idx500;
1136 enum { MSG, LOCAL_PATH, REMOTE_PATH } what = MSG;
1138 const char *err = ap_check_cmd_context(cmd, NOT_IN_LIMIT);
1143 /* 1st parameter should be a 3 digit number, which we recognize;
1144 * convert it into an array index
1146 error_number = atoi(errno_str);
1147 idx500 = ap_index_of_response(HTTP_INTERNAL_SERVER_ERROR);
1149 if (error_number == HTTP_INTERNAL_SERVER_ERROR) {
1150 index_number = idx500;
1152 else if ((index_number = ap_index_of_response(error_number)) == idx500) {
1153 return apr_pstrcat(cmd->pool, "Unsupported HTTP response code ",
1157 /* Heuristic to determine second argument. */
1158 if (ap_strchr_c(msg,' '))
1160 else if (msg[0] == '/')
1162 else if (ap_is_url(msg))
1167 /* The entry should be ignored if it is a full URL for a 401 error */
1169 if (error_number == 401 && what == REMOTE_PATH) {
1170 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, cmd->server,
1171 "cannot use a full URL in a 401 ErrorDocument "
1172 "directive --- ignoring!");
1174 else { /* Store it... */
1175 if (conf->response_code_strings == NULL) {
1176 conf->response_code_strings =
1177 apr_pcalloc(cmd->pool,
1178 sizeof(*conf->response_code_strings) *
1182 /* hack. Prefix a " if it is a msg; as that is what
1183 * http_protocol.c relies on to distinguish between
1184 * a msg and a (local) path.
1186 conf->response_code_strings[index_number] = (what == MSG) ?
1187 apr_pstrcat(cmd->pool, "\"",msg,NULL) :
1188 apr_pstrdup(cmd->pool, msg);
1194 static const char *set_override(cmd_parms *cmd, void *d_, const char *l)
1196 core_dir_config *d = d_;
1199 const char *err = ap_check_cmd_context(cmd, NOT_IN_LIMIT);
1204 d->override = OR_NONE;
1206 w = ap_getword_conf(cmd->pool, &l);
1207 if (!strcasecmp(w, "Limit")) {
1208 d->override |= OR_LIMIT;
1210 else if (!strcasecmp(w, "Options")) {
1211 d->override |= OR_OPTIONS;
1213 else if (!strcasecmp(w, "FileInfo")) {
1214 d->override |= OR_FILEINFO;
1216 else if (!strcasecmp(w, "AuthConfig")) {
1217 d->override |= OR_AUTHCFG;
1219 else if (!strcasecmp(w, "Indexes")) {
1220 d->override |= OR_INDEXES;
1222 else if (!strcasecmp(w, "None")) {
1223 d->override = OR_NONE;
1225 else if (!strcasecmp(w, "All")) {
1226 d->override = OR_ALL;
1229 return apr_pstrcat(cmd->pool, "Illegal override option ", w, NULL);
1232 d->override &= ~OR_UNSET;
1238 static const char *set_options(cmd_parms *cmd, void *d_, const char *l)
1240 core_dir_config *d = d_;
1241 allow_options_t opt;
1246 char *w = ap_getword_conf(cmd->pool, &l);
1249 if (*w == '+' || *w == '-') {
1257 if (!strcasecmp(w, "Indexes")) {
1260 else if (!strcasecmp(w, "Includes")) {
1263 else if (!strcasecmp(w, "IncludesNOEXEC")) {
1264 opt = (OPT_INCLUDES | OPT_INCNOEXEC);
1266 else if (!strcasecmp(w, "FollowSymLinks")) {
1267 opt = OPT_SYM_LINKS;
1269 else if (!strcasecmp(w, "SymLinksIfOwnerMatch")) {
1270 opt = OPT_SYM_OWNER;
1272 else if (!strcasecmp(w, "execCGI")) {
1275 else if (!strcasecmp(w, "MultiViews")) {
1278 else if (!strcasecmp(w, "RunScripts")) { /* AI backcompat. Yuck */
1279 opt = OPT_MULTI|OPT_EXECCGI;
1281 else if (!strcasecmp(w, "None")) {
1284 else if (!strcasecmp(w, "All")) {
1288 return apr_pstrcat(cmd->pool, "Illegal option ", w, NULL);
1291 /* we ensure the invariant (d->opts_add & d->opts_remove) == 0 */
1292 if (action == '-') {
1293 d->opts_remove |= opt;
1294 d->opts_add &= ~opt;
1297 else if (action == '+') {
1299 d->opts_remove &= ~opt;
1311 * Note what data should be used when forming file ETag values.
1312 * It would be nicer to do this as an ITERATE, but then we couldn't
1313 * remember the +/- state properly.
1315 static const char *set_etag_bits(cmd_parms *cmd, void *mconfig,
1318 core_dir_config *cfg;
1319 etag_components_t bit;
1327 cfg = (core_dir_config *)mconfig;
1332 while (args[0] != '\0') {
1336 token = ap_getword_conf(cmd->pool, &args);
1337 if ((*token == '+') || (*token == '-')) {
1343 * The occurrence of an absolute setting wipes
1344 * out any previous relative ones. The first such
1345 * occurrence forgets any inherited ones, too.
1348 cfg->etag_bits = ETAG_UNSET;
1349 cfg->etag_add = ETAG_UNSET;
1350 cfg->etag_remove = ETAG_UNSET;
1355 if (strcasecmp(token, "None") == 0) {
1356 if (action != '*') {
1360 cfg->etag_bits = bit = ETAG_NONE;
1364 else if (strcasecmp(token, "All") == 0) {
1365 if (action != '*') {
1370 cfg->etag_bits = bit = ETAG_ALL;
1373 else if (strcasecmp(token, "Size") == 0) {
1376 else if ((strcasecmp(token, "LMTime") == 0)
1377 || (strcasecmp(token, "MTime") == 0)
1378 || (strcasecmp(token, "LastModified") == 0)) {
1381 else if (strcasecmp(token, "INode") == 0) {
1385 return apr_pstrcat(cmd->pool, "Unknown keyword '",
1386 token, "' for ", cmd->cmd->name,
1387 " directive", NULL);
1391 return apr_pstrcat(cmd->pool, cmd->cmd->name, " keyword '",
1392 token, "' cannot be used with '+' or '-'",
1396 if (action == '+') {
1398 * Make sure it's in the 'add' list and absent from the
1401 cfg->etag_add |= bit;
1402 cfg->etag_remove &= (~ bit);
1404 else if (action == '-') {
1405 cfg->etag_remove |= bit;
1406 cfg->etag_add &= (~ bit);
1410 * Non-relative values wipe out any + or - values
1411 * accumulated so far.
1413 cfg->etag_bits |= bit;
1414 cfg->etag_add = ETAG_UNSET;
1415 cfg->etag_remove = ETAG_UNSET;
1421 * Any setting at all will clear the 'None' and 'Unset' bits.
1424 if (cfg->etag_add != ETAG_UNSET) {
1425 cfg->etag_add &= (~ ETAG_UNSET);
1428 if (cfg->etag_remove != ETAG_UNSET) {
1429 cfg->etag_remove &= (~ ETAG_UNSET);
1433 cfg->etag_bits &= (~ ETAG_UNSET);
1435 if ((cfg->etag_bits & ETAG_NONE) != ETAG_NONE) {
1436 cfg->etag_bits &= (~ ETAG_NONE);
1443 static const char *set_enable_mmap(cmd_parms *cmd, void *d_,
1446 core_dir_config *d = d_;
1447 const char *err = ap_check_cmd_context(cmd, NOT_IN_LIMIT);
1453 if (strcasecmp(arg, "on") == 0) {
1454 d->enable_mmap = ENABLE_MMAP_ON;
1456 else if (strcasecmp(arg, "off") == 0) {
1457 d->enable_mmap = ENABLE_MMAP_OFF;
1460 return "parameter must be 'on' or 'off'";
1466 static const char *set_enable_sendfile(cmd_parms *cmd, void *d_,
1469 core_dir_config *d = d_;
1470 const char *err = ap_check_cmd_context(cmd, NOT_IN_LIMIT);
1476 if (strcasecmp(arg, "on") == 0) {
1477 d->enable_sendfile = ENABLE_SENDFILE_ON;
1479 else if (strcasecmp(arg, "off") == 0) {
1480 d->enable_sendfile = ENABLE_SENDFILE_OFF;
1483 return "parameter must be 'on' or 'off'";
1489 static const char *satisfy(cmd_parms *cmd, void *c_, const char *arg)
1491 core_dir_config *c = c_;
1493 if (!strcasecmp(arg, "all")) {
1494 c->satisfy = SATISFY_ALL;
1496 else if (!strcasecmp(arg, "any")) {
1497 c->satisfy = SATISFY_ANY;
1500 return "Satisfy either 'any' or 'all'.";
1506 static const char *require(cmd_parms *cmd, void *c_, const char *arg)
1509 core_dir_config *c = c_;
1511 if (!c->ap_requires) {
1512 c->ap_requires = apr_array_make(cmd->pool, 2, sizeof(require_line));
1515 r = (require_line *)apr_array_push(c->ap_requires);
1516 r->requirement = apr_pstrdup(cmd->pool, arg);
1517 r->method_mask = cmd->limited;
1522 AP_CORE_DECLARE_NONSTD(const char *) ap_limit_section(cmd_parms *cmd,
1526 const char *limited_methods = ap_getword(cmd->pool, &arg, '>');
1527 void *tog = cmd->cmd->cmd_data;
1528 apr_int64_t limited = 0;
1531 const char *err = ap_check_cmd_context(cmd, NOT_IN_LIMIT);
1536 while (limited_methods[0]) {
1537 char *method = ap_getword_conf(cmd->pool, &limited_methods);
1540 /* check for builtin or module registered method number */
1541 methnum = ap_method_number_of(method);
1543 if (methnum == M_TRACE && !tog) {
1544 return "TRACE cannot be controlled by <Limit>";
1546 else if (methnum == M_INVALID) {
1547 /* method has not been registered yet, but resorce restriction
1548 * is always checked before method handling, so register it.
1550 methnum = ap_method_register(cmd->pool, method);
1553 limited |= (AP_METHOD_BIT << methnum);
1556 /* Killing two features with one function,
1557 * if (tog == NULL) <Limit>, else <LimitExcept>
1559 cmd->limited = tog ? ~limited : limited;
1561 errmsg = ap_walk_config(cmd->directive->first_child, cmd, cmd->context);
1568 /* XXX: Bogus - need to do this differently (at least OS2/Netware suffer
1569 * the same problem!!!
1570 * We use this in <DirectoryMatch> and <FilesMatch>, to ensure that
1571 * people don't get bitten by wrong-cased regex matches
1575 #define USE_ICASE REG_ICASE
1581 * Report a missing-'>' syntax error.
1583 static char *unclosed_directive(cmd_parms *cmd)
1585 return apr_pstrcat(cmd->pool, cmd->cmd->name,
1586 "> directive missing closing '>'", NULL);
1589 static const char *dirsection(cmd_parms *cmd, void *mconfig, const char *arg)
1592 const char *endp = ap_strrchr_c(arg, '>');
1593 int old_overrides = cmd->override;
1594 char *old_path = cmd->path;
1595 core_dir_config *conf;
1596 ap_conf_vector_t *new_dir_conf = ap_create_per_dir_config(cmd->pool);
1598 const command_rec *thiscmd = cmd->cmd;
1600 const char *err = ap_check_cmd_context(cmd,
1601 NOT_IN_DIR_LOC_FILE|NOT_IN_LIMIT);
1607 return unclosed_directive(cmd);
1610 arg = apr_pstrndup(cmd->pool, arg, endp - arg);
1613 if (thiscmd->cmd_data)
1614 return "<DirectoryMatch > block must specify a path";
1616 return "<Directory > block must specify a path";
1619 cmd->path = ap_getword_conf(cmd->pool, &arg);
1620 cmd->override = OR_ALL|ACCESS_CONF;
1622 if (!strcmp(cmd->path, "~")) {
1623 cmd->path = ap_getword_conf(cmd->pool, &arg);
1625 return "<Directory ~ > block must specify a path";
1626 r = ap_pregcomp(cmd->pool, cmd->path, REG_EXTENDED|USE_ICASE);
1628 else if (thiscmd->cmd_data) { /* <DirectoryMatch> */
1629 r = ap_pregcomp(cmd->pool, cmd->path, REG_EXTENDED|USE_ICASE);
1631 else if (!strcmp(cmd->path, "/") == 0)
1636 * Ensure that the pathname is canonical, and append the trailing /
1638 apr_status_t rv = apr_filepath_merge(&newpath, NULL, cmd->path,
1639 APR_FILEPATH_TRUENAME, cmd->pool);
1640 if (rv != APR_SUCCESS && rv != APR_EPATHWILD) {
1641 return apr_pstrcat(cmd->pool, "<Directory \"", cmd->path,
1642 "\"> path is invalid.", NULL);
1645 cmd->path = newpath;
1646 if (cmd->path[strlen(cmd->path) - 1] != '/')
1647 cmd->path = apr_pstrcat(cmd->pool, cmd->path, "/", NULL);
1650 /* initialize our config and fetch it */
1651 conf = ap_set_config_vectors(cmd->server, new_dir_conf, cmd->path,
1652 &core_module, cmd->pool);
1654 errmsg = ap_walk_config(cmd->directive->first_child, cmd, new_dir_conf);
1659 conf->d = cmd->path;
1660 conf->d_is_fnmatch = (apr_fnmatch_test(conf->d) != 0);
1662 /* Make this explicit - the "/" root has 0 elements, that is, we
1663 * will always merge it, and it will always sort and merge first.
1664 * All others are sorted and tested by the number of slashes.
1666 if (strcmp(conf->d, "/") == 0)
1667 conf->d_components = 0;
1669 conf->d_components = ap_count_dirs(conf->d);
1671 ap_add_per_dir_conf(cmd->server, new_dir_conf);
1674 return apr_pstrcat(cmd->pool, "Multiple ", thiscmd->name,
1675 "> arguments not (yet) supported.", NULL);
1678 cmd->path = old_path;
1679 cmd->override = old_overrides;
1684 static const char *urlsection(cmd_parms *cmd, void *mconfig, const char *arg)
1687 const char *endp = ap_strrchr_c(arg, '>');
1688 int old_overrides = cmd->override;
1689 char *old_path = cmd->path;
1690 core_dir_config *conf;
1692 const command_rec *thiscmd = cmd->cmd;
1693 ap_conf_vector_t *new_url_conf = ap_create_per_dir_config(cmd->pool);
1694 const char *err = ap_check_cmd_context(cmd,
1695 NOT_IN_DIR_LOC_FILE|NOT_IN_LIMIT);
1701 return unclosed_directive(cmd);
1704 arg = apr_pstrndup(cmd->pool, arg, endp - arg);
1706 cmd->path = ap_getword_conf(cmd->pool, &arg);
1707 cmd->override = OR_ALL|ACCESS_CONF;
1709 if (thiscmd->cmd_data) { /* <LocationMatch> */
1710 r = ap_pregcomp(cmd->pool, cmd->path, REG_EXTENDED);
1712 else if (!strcmp(cmd->path, "~")) {
1713 cmd->path = ap_getword_conf(cmd->pool, &arg);
1714 r = ap_pregcomp(cmd->pool, cmd->path, REG_EXTENDED);
1717 /* initialize our config and fetch it */
1718 conf = ap_set_config_vectors(cmd->server, new_url_conf, cmd->path,
1719 &core_module, cmd->pool);
1721 errmsg = ap_walk_config(cmd->directive->first_child, cmd, new_url_conf);
1725 conf->d = apr_pstrdup(cmd->pool, cmd->path); /* No mangling, please */
1726 conf->d_is_fnmatch = apr_fnmatch_test(conf->d) != 0;
1729 ap_add_per_url_conf(cmd->server, new_url_conf);
1732 return apr_pstrcat(cmd->pool, "Multiple ", thiscmd->name,
1733 "> arguments not (yet) supported.", NULL);
1736 cmd->path = old_path;
1737 cmd->override = old_overrides;
1742 static const char *filesection(cmd_parms *cmd, void *mconfig, const char *arg)
1745 const char *endp = ap_strrchr_c(arg, '>');
1746 int old_overrides = cmd->override;
1747 char *old_path = cmd->path;
1748 core_dir_config *conf;
1750 const command_rec *thiscmd = cmd->cmd;
1751 core_dir_config *c = mconfig;
1752 ap_conf_vector_t *new_file_conf = ap_create_per_dir_config(cmd->pool);
1753 const char *err = ap_check_cmd_context(cmd, NOT_IN_LIMIT|NOT_IN_LOCATION);
1760 return unclosed_directive(cmd);
1763 arg = apr_pstrndup(cmd->pool, arg, endp - arg);
1765 cmd->path = ap_getword_conf(cmd->pool, &arg);
1766 /* Only if not an .htaccess file */
1768 cmd->override = OR_ALL|ACCESS_CONF;
1771 if (thiscmd->cmd_data) { /* <FilesMatch> */
1772 r = ap_pregcomp(cmd->pool, cmd->path, REG_EXTENDED|USE_ICASE);
1774 else if (!strcmp(cmd->path, "~")) {
1775 cmd->path = ap_getword_conf(cmd->pool, &arg);
1776 r = ap_pregcomp(cmd->pool, cmd->path, REG_EXTENDED|USE_ICASE);
1780 /* Ensure that the pathname is canonical, but we
1781 * can't test the case/aliases without a fixed path */
1782 if (apr_filepath_merge(&newpath, "", cmd->path,
1783 0, cmd->pool) != APR_SUCCESS)
1784 return apr_pstrcat(cmd->pool, "<Files \"", cmd->path,
1785 "\"> is invalid.", NULL);
1786 cmd->path = newpath;
1789 /* initialize our config and fetch it */
1790 conf = ap_set_config_vectors(cmd->server, new_file_conf, cmd->path,
1791 &core_module, cmd->pool);
1793 errmsg = ap_walk_config(cmd->directive->first_child, cmd, new_file_conf);
1797 conf->d = cmd->path;
1798 conf->d_is_fnmatch = apr_fnmatch_test(conf->d) != 0;
1801 ap_add_file_conf(c, new_file_conf);
1804 return apr_pstrcat(cmd->pool, "Multiple ", thiscmd->name,
1805 "> arguments not (yet) supported.", NULL);
1808 cmd->path = old_path;
1809 cmd->override = old_overrides;
1814 static const char *start_ifmod(cmd_parms *cmd, void *mconfig, const char *arg)
1816 const char *endp = ap_strrchr_c(arg, '>');
1817 int not = (arg[0] == '!');
1821 return unclosed_directive(cmd);
1824 arg = apr_pstrndup(cmd->pool, arg, endp - arg);
1830 found = ap_find_linked_module(arg);
1832 if ((!not && found) || (not && !found)) {
1833 ap_directive_t *parent = NULL;
1834 ap_directive_t *current = NULL;
1837 retval = ap_build_cont_config(cmd->pool, cmd->temp_pool, cmd,
1838 ¤t, &parent, "<IfModule");
1839 *(ap_directive_t **)mconfig = current;
1843 *(ap_directive_t **)mconfig = NULL;
1844 return ap_soak_end_container(cmd, "<IfModule");
1848 AP_DECLARE(int) ap_exists_config_define(const char *name)
1853 defines = (char **)ap_server_config_defines->elts;
1854 for (i = 0; i < ap_server_config_defines->nelts; i++) {
1855 if (strcmp(defines[i], name) == 0) {
1863 static const char *start_ifdefine(cmd_parms *cmd, void *dummy, const char *arg)
1869 endp = ap_strrchr_c(arg, '>');
1871 return unclosed_directive(cmd);
1874 arg = apr_pstrndup(cmd->pool, arg, endp - arg);
1876 if (arg[0] == '!') {
1881 defined = ap_exists_config_define(arg);
1882 if ((!not && defined) || (not && !defined)) {
1883 ap_directive_t *parent = NULL;
1884 ap_directive_t *current = NULL;
1887 retval = ap_build_cont_config(cmd->pool, cmd->temp_pool, cmd,
1888 ¤t, &parent, "<IfDefine");
1889 *(ap_directive_t **)dummy = current;
1893 *(ap_directive_t **)dummy = NULL;
1894 return ap_soak_end_container(cmd, "<IfDefine");
1898 /* httpd.conf commands... beginning with the <VirtualHost> business */
1900 static const char *virtualhost_section(cmd_parms *cmd, void *dummy,
1903 server_rec *main_server = cmd->server, *s;
1905 const char *endp = ap_strrchr_c(arg, '>');
1906 apr_pool_t *p = cmd->pool;
1908 const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
1914 return unclosed_directive(cmd);
1917 arg = apr_pstrndup(cmd->pool, arg, endp - arg);
1919 /* FIXME: There's another feature waiting to happen here -- since you
1920 can now put multiple addresses/names on a single <VirtualHost>
1921 you might want to use it to group common definitions and then
1922 define other "subhosts" with their individual differences. But
1923 personally I'd rather just do it with a macro preprocessor. -djg */
1924 if (main_server->is_virtual) {
1925 return "<VirtualHost> doesn't nest!";
1928 errmsg = ap_init_virtual_host(p, arg, main_server, &s);
1933 s->next = main_server->next;
1934 main_server->next = s;
1936 s->defn_name = cmd->directive->filename;
1937 s->defn_line_number = cmd->directive->line_num;
1941 errmsg = ap_walk_config(cmd->directive->first_child, cmd,
1942 s->lookup_defaults);
1944 cmd->server = main_server;
1949 static const char *set_server_alias(cmd_parms *cmd, void *dummy,
1952 if (!cmd->server->names) {
1953 return "ServerAlias only used in <VirtualHost>";
1957 char **item, *name = ap_getword_conf(cmd->pool, &arg);
1959 if (ap_is_matchexp(name)) {
1960 item = (char **)apr_array_push(cmd->server->wild_names);
1963 item = (char **)apr_array_push(cmd->server->names);
1972 static const char *set_server_string_slot(cmd_parms *cmd, void *dummy,
1975 /* This one's pretty generic... */
1977 int offset = (int)(long)cmd->info;
1978 char *struct_ptr = (char *)cmd->server;
1980 const char *err = ap_check_cmd_context(cmd,
1981 NOT_IN_DIR_LOC_FILE|NOT_IN_LIMIT);
1986 *(const char **)(struct_ptr + offset) = arg;
1990 static const char *server_hostname_port(cmd_parms *cmd, void *dummy, const char *arg)
1992 const char *err = ap_check_cmd_context(cmd, NOT_IN_DIR_LOC_FILE|NOT_IN_LIMIT);
1993 const char *portstr;
2000 portstr = ap_strchr_c(arg, ':');
2002 cmd->server->server_hostname = apr_pstrndup(cmd->pool, arg,
2005 port = atoi(portstr);
2006 if (port <= 0 || port >= 65536) { /* 65536 == 1<<16 */
2007 return apr_pstrcat(cmd->temp_pool, "The port number \"", arg,
2008 "\" is outside the appropriate range "
2009 "(i.e., 1..65535).", NULL);
2013 cmd->server->server_hostname = apr_pstrdup(cmd->pool, arg);
2017 cmd->server->port = port;
2021 static const char *set_signature_flag(cmd_parms *cmd, void *d_,
2024 core_dir_config *d = d_;
2025 const char *err = ap_check_cmd_context(cmd, NOT_IN_LIMIT);
2031 if (strcasecmp(arg, "On") == 0) {
2032 d->server_signature = srv_sig_on;
2034 else if (strcasecmp(arg, "Off") == 0) {
2035 d->server_signature = srv_sig_off;
2037 else if (strcasecmp(arg, "EMail") == 0) {
2038 d->server_signature = srv_sig_withmail;
2041 return "ServerSignature: use one of: off | on | email";
2047 static const char *set_server_root(cmd_parms *cmd, void *dummy,
2050 const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
2056 if ((apr_filepath_merge((char**)&ap_server_root, NULL, arg,
2057 APR_FILEPATH_TRUENAME, cmd->pool) != APR_SUCCESS)
2058 || !ap_is_directory(cmd->pool, ap_server_root)) {
2059 return "ServerRoot must be a valid directory";
2065 static const char *set_timeout(cmd_parms *cmd, void *dummy, const char *arg)
2067 const char *err = ap_check_cmd_context(cmd, NOT_IN_DIR_LOC_FILE|NOT_IN_LIMIT);
2073 cmd->server->timeout = apr_time_from_sec(atoi(arg));
2077 static const char *set_idcheck(cmd_parms *cmd, void *d_, int arg)
2079 core_dir_config *d = d_;
2080 const char *err = ap_check_cmd_context(cmd, NOT_IN_LIMIT);
2086 d->do_rfc1413 = arg != 0;
2090 static const char *set_hostname_lookups(cmd_parms *cmd, void *d_,
2093 core_dir_config *d = d_;
2094 const char *err = ap_check_cmd_context(cmd, NOT_IN_LIMIT);
2100 if (!strcasecmp(arg, "on")) {
2101 d->hostname_lookups = HOSTNAME_LOOKUP_ON;
2103 else if (!strcasecmp(arg, "off")) {
2104 d->hostname_lookups = HOSTNAME_LOOKUP_OFF;
2106 else if (!strcasecmp(arg, "double")) {
2107 d->hostname_lookups = HOSTNAME_LOOKUP_DOUBLE;
2110 return "parameter must be 'on', 'off', or 'double'";
2116 static const char *set_serverpath(cmd_parms *cmd, void *dummy,
2119 const char *err = ap_check_cmd_context(cmd, NOT_IN_DIR_LOC_FILE|NOT_IN_LIMIT);
2125 cmd->server->path = arg;
2126 cmd->server->pathlen = strlen(arg);
2130 static const char *set_content_md5(cmd_parms *cmd, void *d_, int arg)
2132 core_dir_config *d = d_;
2133 const char *err = ap_check_cmd_context(cmd, NOT_IN_LIMIT);
2139 d->content_md5 = arg != 0;
2143 static const char *set_accept_path_info(cmd_parms *cmd, void *d_, const char *arg)
2145 core_dir_config *d = d_;
2147 if (strcasecmp(arg, "on") == 0) {
2148 d->accept_path_info = AP_REQ_ACCEPT_PATH_INFO;
2150 else if (strcasecmp(arg, "off") == 0) {
2151 d->accept_path_info = AP_REQ_REJECT_PATH_INFO;
2153 else if (strcasecmp(arg, "default") == 0) {
2154 d->accept_path_info = AP_REQ_DEFAULT_PATH_INFO;
2157 return "AcceptPathInfo must be set to on, off or default";
2163 static const char *set_use_canonical_name(cmd_parms *cmd, void *d_,
2166 core_dir_config *d = d_;
2167 const char *err = ap_check_cmd_context(cmd, NOT_IN_LIMIT);
2173 if (strcasecmp(arg, "on") == 0) {
2174 d->use_canonical_name = USE_CANONICAL_NAME_ON;
2176 else if (strcasecmp(arg, "off") == 0) {
2177 d->use_canonical_name = USE_CANONICAL_NAME_OFF;
2179 else if (strcasecmp(arg, "dns") == 0) {
2180 d->use_canonical_name = USE_CANONICAL_NAME_DNS;
2183 return "parameter must be 'on', 'off', or 'dns'";
2190 static const char *include_config (cmd_parms *cmd, void *dummy,
2193 ap_directive_t *conftree = NULL;
2194 const char* conffile = ap_server_root_relative(cmd->pool, name);
2196 /* XXX: ap_server_root_relative won't work on the wildcard pattern...
2200 return apr_pstrcat(cmd->pool, "Invalid Include path ",
2204 ap_process_resource_config(cmd->server, conffile,
2205 &conftree, cmd->pool, cmd->temp_pool);
2206 *(ap_directive_t **)dummy = conftree;
2210 static const char *set_loglevel(cmd_parms *cmd, void *dummy, const char *arg)
2214 const char *err = ap_check_cmd_context(cmd,
2215 NOT_IN_DIR_LOC_FILE|NOT_IN_LIMIT);
2220 if ((str = ap_getword_conf(cmd->pool, &arg))) {
2221 if (!strcasecmp(str, "emerg")) {
2222 cmd->server->loglevel = APLOG_EMERG;
2224 else if (!strcasecmp(str, "alert")) {
2225 cmd->server->loglevel = APLOG_ALERT;
2227 else if (!strcasecmp(str, "crit")) {
2228 cmd->server->loglevel = APLOG_CRIT;
2230 else if (!strcasecmp(str, "error")) {
2231 cmd->server->loglevel = APLOG_ERR;
2233 else if (!strcasecmp(str, "warn")) {
2234 cmd->server->loglevel = APLOG_WARNING;
2236 else if (!strcasecmp(str, "notice")) {
2237 cmd->server->loglevel = APLOG_NOTICE;
2239 else if (!strcasecmp(str, "info")) {
2240 cmd->server->loglevel = APLOG_INFO;
2242 else if (!strcasecmp(str, "debug")) {
2243 cmd->server->loglevel = APLOG_DEBUG;
2246 return "LogLevel requires level keyword: one of "
2247 "emerg/alert/crit/error/warn/notice/info/debug";
2251 return "LogLevel requires level keyword";
2257 AP_DECLARE(const char *) ap_psignature(const char *prefix, request_rec *r)
2260 core_dir_config *conf;
2262 conf = (core_dir_config *)ap_get_module_config(r->per_dir_config,
2264 if ((conf->server_signature == srv_sig_off)
2265 || (conf->server_signature == srv_sig_unset)) {
2269 apr_snprintf(sport, sizeof sport, "%u", (unsigned) ap_get_server_port(r));
2271 if (conf->server_signature == srv_sig_withmail) {
2272 return apr_pstrcat(r->pool, prefix, "<address>",
2273 ap_get_server_version(),
2274 " Server at <a href=\"mailto:",
2275 r->server->server_admin, "\">",
2276 ap_escape_html(r->pool, ap_get_server_name(r)),
2277 "</a> Port ", sport,
2278 "</address>\n", NULL);
2281 return apr_pstrcat(r->pool, prefix, "<address>", ap_get_server_version(),
2283 ap_escape_html(r->pool, ap_get_server_name(r)),
2285 "</address>\n", NULL);
2289 * Load an authorisation realm into our location configuration, applying the
2290 * usual rules that apply to realms.
2292 static const char *set_authname(cmd_parms *cmd, void *mconfig,
2295 core_dir_config *aconfig = (core_dir_config *)mconfig;
2297 aconfig->ap_auth_name = ap_escape_quotes(cmd->pool, word1);
2301 #ifdef _OSD_POSIX /* BS2000 Logon Passwd file */
2302 static const char *set_bs2000_account(cmd_parms *cmd, void *dummy, char *name)
2304 const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
2309 return os_set_account(cmd->pool, name);
2311 #endif /*_OSD_POSIX*/
2314 * Handle a request to include the server's OS platform in the Server
2315 * response header field (the ServerTokens directive). Unfortunately
2316 * this requires a new global in order to communicate the setting back to
2317 * http_main so it can insert the information in the right place in the
2321 static char *server_version = NULL;
2322 static int version_locked = 0;
2324 enum server_token_type {
2325 SrvTk_MAJOR, /* eg: Apache/2 */
2326 SrvTk_MINOR, /* eg. Apache/2.0 */
2327 SrvTk_MINIMAL, /* eg: Apache/2.0.41 */
2328 SrvTk_OS, /* eg: Apache/2.0.41 (UNIX) */
2329 SrvTk_FULL, /* eg: Apache/2.0.41 (UNIX) PHP/4.2.2 FooBar/1.2b */
2330 SrvTk_PRODUCT_ONLY /* eg: Apache */
2332 static enum server_token_type ap_server_tokens = SrvTk_FULL;
2334 static apr_status_t reset_version(void *dummy)
2337 ap_server_tokens = SrvTk_FULL;
2338 server_version = NULL;
2342 AP_DECLARE(const char *) ap_get_server_version(void)
2344 return (server_version ? server_version : AP_SERVER_BASEVERSION);
2347 AP_DECLARE(void) ap_add_version_component(apr_pool_t *pconf, const char *component)
2349 if (! version_locked) {
2351 * If the version string is null, register our cleanup to reset the
2352 * pointer on pool destruction. We also know that, if NULL,
2353 * we are adding the original SERVER_BASEVERSION string.
2355 if (server_version == NULL) {
2356 apr_pool_cleanup_register(pconf, NULL, reset_version,
2357 apr_pool_cleanup_null);
2358 server_version = apr_pstrdup(pconf, component);
2362 * Tack the given component identifier to the end of
2363 * the existing string.
2365 server_version = apr_pstrcat(pconf, server_version, " ",
2372 * This routine adds the real server base identity to the version string,
2373 * and then locks out changes until the next reconfig.
2375 static void ap_set_version(apr_pool_t *pconf)
2377 if (ap_server_tokens == SrvTk_PRODUCT_ONLY) {
2378 ap_add_version_component(pconf, AP_SERVER_BASEPRODUCT);
2380 else if (ap_server_tokens == SrvTk_MINIMAL) {
2381 ap_add_version_component(pconf, AP_SERVER_BASEVERSION);
2383 else if (ap_server_tokens == SrvTk_MINOR) {
2384 ap_add_version_component(pconf, AP_SERVER_BASEPRODUCT "/" AP_SERVER_MINORREVISION);
2386 else if (ap_server_tokens == SrvTk_MAJOR) {
2387 ap_add_version_component(pconf, AP_SERVER_BASEPRODUCT "/" AP_SERVER_MAJORVERSION);
2390 ap_add_version_component(pconf, AP_SERVER_BASEVERSION " (" PLATFORM ")");
2394 * Lock the server_version string if we're not displaying
2395 * the full set of tokens
2397 if (ap_server_tokens != SrvTk_FULL) {
2402 static const char *set_serv_tokens(cmd_parms *cmd, void *dummy,
2405 const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
2411 if (!strcasecmp(arg, "OS")) {
2412 ap_server_tokens = SrvTk_OS;
2414 else if (!strcasecmp(arg, "Min") || !strcasecmp(arg, "Minimal")) {
2415 ap_server_tokens = SrvTk_MINIMAL;
2417 else if (!strcasecmp(arg, "Major")) {
2418 ap_server_tokens = SrvTk_MAJOR;
2420 else if (!strcasecmp(arg, "Minor") ) {
2421 ap_server_tokens = SrvTk_MINOR;
2423 else if (!strcasecmp(arg, "Prod") || !strcasecmp(arg, "ProductOnly")) {
2424 ap_server_tokens = SrvTk_PRODUCT_ONLY;
2427 ap_server_tokens = SrvTk_FULL;
2433 static const char *set_limit_req_line(cmd_parms *cmd, void *dummy,
2436 const char *err = ap_check_cmd_context(cmd,
2437 NOT_IN_DIR_LOC_FILE|NOT_IN_LIMIT);
2446 return apr_pstrcat(cmd->temp_pool, "LimitRequestLine \"", arg,
2447 "\" must be a non-negative integer", NULL);
2450 if (lim > DEFAULT_LIMIT_REQUEST_LINE) {
2451 return apr_psprintf(cmd->temp_pool, "LimitRequestLine \"%s\" "
2452 "must not exceed the precompiled maximum of %d",
2453 arg, DEFAULT_LIMIT_REQUEST_LINE);
2456 cmd->server->limit_req_line = lim;
2460 static const char *set_limit_req_fieldsize(cmd_parms *cmd, void *dummy,
2463 const char *err = ap_check_cmd_context(cmd,
2464 NOT_IN_DIR_LOC_FILE|NOT_IN_LIMIT);
2473 return apr_pstrcat(cmd->temp_pool, "LimitRequestFieldsize \"", arg,
2474 "\" must be a non-negative integer (0 = no limit)",
2478 if (lim > DEFAULT_LIMIT_REQUEST_FIELDSIZE) {
2479 return apr_psprintf(cmd->temp_pool, "LimitRequestFieldsize \"%s\" "
2480 "must not exceed the precompiled maximum of %d",
2481 arg, DEFAULT_LIMIT_REQUEST_FIELDSIZE);
2484 cmd->server->limit_req_fieldsize = lim;
2488 static const char *set_limit_req_fields(cmd_parms *cmd, void *dummy,
2491 const char *err = ap_check_cmd_context(cmd,
2492 NOT_IN_DIR_LOC_FILE|NOT_IN_LIMIT);
2501 return apr_pstrcat(cmd->temp_pool, "LimitRequestFields \"", arg,
2502 "\" must be a non-negative integer (0 = no limit)",
2506 cmd->server->limit_req_fields = lim;
2510 static const char *set_limit_req_body(cmd_parms *cmd, void *conf_,
2513 core_dir_config *conf = conf_;
2514 const char *err = ap_check_cmd_context(cmd, NOT_IN_LIMIT);
2521 /* WTF: If strtoul is not portable, then write a replacement.
2522 * Instead we have an idiotic define in httpd.h that prevents
2523 * it from being used even when it is available. Sheesh.
2525 conf->limit_req_body = (apr_off_t)strtol(arg, &errp, 10);
2526 if (*errp != '\0') {
2527 return "LimitRequestBody requires a non-negative integer.";
2533 static const char *set_limit_xml_req_body(cmd_parms *cmd, void *conf_,
2536 core_dir_config *conf = conf_;
2537 const char *err = ap_check_cmd_context(cmd, NOT_IN_LIMIT);
2543 conf->limit_xml_body = atol(arg);
2544 if (conf->limit_xml_body < 0)
2545 return "LimitXMLRequestBody requires a non-negative integer.";
2550 AP_DECLARE(size_t) ap_get_limit_xml_body(const request_rec *r)
2552 core_dir_config *conf;
2554 conf = ap_get_module_config(r->per_dir_config, &core_module);
2555 if (conf->limit_xml_body == AP_LIMIT_UNSET)
2556 return AP_DEFAULT_LIMIT_XML_BODY;
2558 return (size_t)conf->limit_xml_body;
2561 #if !defined (RLIMIT_CPU) || !(defined (RLIMIT_DATA) || defined (RLIMIT_VMEM) || defined(RLIMIT_AS)) || !defined (RLIMIT_NPROC)
2562 static const char *no_set_limit(cmd_parms *cmd, void *conf_,
2563 const char *arg, const char *arg2)
2565 ap_log_error(APLOG_MARK, APLOG_ERR, 0, cmd->server,
2566 "%s not supported on this platform", cmd->cmd->name);
2573 static const char *set_limit_cpu(cmd_parms *cmd, void *conf_,
2574 const char *arg, const char *arg2)
2576 core_dir_config *conf = conf_;
2578 unixd_set_rlimit(cmd, &conf->limit_cpu, arg, arg2, RLIMIT_CPU);
2583 #if defined (RLIMIT_DATA) || defined (RLIMIT_VMEM) || defined(RLIMIT_AS)
2584 static const char *set_limit_mem(cmd_parms *cmd, void *conf_,
2585 const char *arg, const char * arg2)
2587 core_dir_config *conf = conf_;
2589 #if defined(RLIMIT_AS)
2590 unixd_set_rlimit(cmd, &conf->limit_mem, arg, arg2 ,RLIMIT_AS);
2591 #elif defined(RLIMIT_DATA)
2592 unixd_set_rlimit(cmd, &conf->limit_mem, arg, arg2, RLIMIT_DATA);
2593 #elif defined(RLIMIT_VMEM)
2594 unixd_set_rlimit(cmd, &conf->limit_mem, arg, arg2, RLIMIT_VMEM);
2602 static const char *set_limit_nproc(cmd_parms *cmd, void *conf_,
2603 const char *arg, const char * arg2)
2605 core_dir_config *conf = conf_;
2607 unixd_set_rlimit(cmd, &conf->limit_nproc, arg, arg2, RLIMIT_NPROC);
2612 static const char *add_ct_output_filters(cmd_parms *cmd, void *conf_,
2613 const char *arg, const char *arg2)
2615 core_dir_config *conf = conf_;
2616 ap_filter_rec_t *old, *new = NULL;
2617 const char *filter_name;
2619 if (!conf->ct_output_filters) {
2620 conf->ct_output_filters = apr_hash_make(cmd->pool);
2624 old = (ap_filter_rec_t*) apr_hash_get(conf->ct_output_filters, arg2,
2625 APR_HASH_KEY_STRING);
2629 (filter_name = ap_getword(cmd->pool, &arg, ';')) &&
2630 strcmp(filter_name, "")) {
2631 new = apr_pcalloc(cmd->pool, sizeof(ap_filter_rec_t));
2632 new->name = filter_name;
2634 /* We found something, so let's append it. */
2642 return "invalid filter name";
2645 apr_hash_set(conf->ct_output_filters, arg2, APR_HASH_KEY_STRING, new);
2650 * Insert filters requested by the AddOutputFilterByType
2651 * configuration directive. We cannot add filters based
2652 * on content-type until after the handler has started
2653 * to run. Only then do we reliably know the content-type.
2655 void ap_add_output_filters_by_type(request_rec *r)
2657 core_dir_config *conf;
2658 const char *ctype, *ctypes;
2660 conf = (core_dir_config *)ap_get_module_config(r->per_dir_config,
2663 /* We can't do anything with proxy requests, no content-types or if
2664 * we don't have a filter configured.
2666 if (r->proxyreq != PROXYREQ_NONE || !r->content_type ||
2667 !conf->ct_output_filters) {
2671 ctypes = r->content_type;
2673 /* We must be able to handle decorated content-types. */
2674 while (*ctypes && (ctype = ap_getword(r->pool, &ctypes, ';'))) {
2675 ap_filter_rec_t *ct_filter;
2676 ct_filter = apr_hash_get(conf->ct_output_filters, ctype,
2677 APR_HASH_KEY_STRING);
2679 ap_add_output_filter(ct_filter->name, NULL, r, r->connection);
2680 ct_filter = ct_filter->next;
2687 static apr_status_t writev_it_all(apr_socket_t *s,
2688 struct iovec *vec, int nvec,
2689 apr_size_t len, apr_size_t *nbytes)
2691 apr_size_t bytes_written = 0;
2698 /* XXX handle checking for non-blocking socket */
2699 while (bytes_written != len) {
2700 rv = apr_sendv(s, vec + i, nvec - i, &n);
2702 if (rv != APR_SUCCESS)
2707 /* If the write did not complete, adjust the iovecs and issue
2710 if (bytes_written < len) {
2711 /* Skip over the vectors that have already been written */
2712 apr_size_t cnt = vec[i].iov_len;
2713 while (n >= cnt && i + 1 < nvec) {
2715 cnt += vec[i].iov_len;
2719 /* Handle partial write of vec i */
2720 vec[i].iov_base = (char *) vec[i].iov_base +
2721 (vec[i].iov_len - (cnt - n));
2722 vec[i].iov_len = cnt -n;
2726 n = len - bytes_written;
2732 /* sendfile_it_all()
2733 * send the entire file using sendfile()
2734 * handle partial writes
2735 * return only when all bytes have been sent or an error is encountered.
2738 #if APR_HAS_SENDFILE
2739 static apr_status_t sendfile_it_all(core_net_rec *c,
2742 apr_off_t file_offset,
2743 apr_size_t file_bytes_left,
2744 apr_size_t total_bytes_left,
2745 apr_size_t *bytes_sent,
2750 apr_interval_time_t timeout = 0;
2753 AP_DEBUG_ASSERT((apr_socket_timeout_get(c->client_socket, &timeout)
2755 && timeout > 0); /* socket must be in timeout mode */
2757 /* Reset the bytes_sent field */
2761 apr_size_t tmplen = file_bytes_left;
2763 rv = apr_sendfile(c->client_socket, fd, hdtr, &file_offset, &tmplen,
2765 *bytes_sent += tmplen;
2766 total_bytes_left -= tmplen;
2767 if (!total_bytes_left || rv != APR_SUCCESS) {
2768 return rv; /* normal case & error exit */
2771 AP_DEBUG_ASSERT(total_bytes_left > 0 && tmplen > 0);
2773 /* partial write, oooh noooo...
2774 * Skip over any header data which was written
2776 while (tmplen && hdtr->numheaders) {
2777 if (tmplen >= hdtr->headers[0].iov_len) {
2778 tmplen -= hdtr->headers[0].iov_len;
2783 char *iov_base = (char *)hdtr->headers[0].iov_base;
2785 hdtr->headers[0].iov_len -= tmplen;
2787 hdtr->headers[0].iov_base = iov_base;
2792 /* Skip over any file data which was written */
2794 if (tmplen <= file_bytes_left) {
2795 file_offset += tmplen;
2796 file_bytes_left -= tmplen;
2800 tmplen -= file_bytes_left;
2801 file_bytes_left = 0;
2804 /* Skip over any trailer data which was written */
2806 while (tmplen && hdtr->numtrailers) {
2807 if (tmplen >= hdtr->trailers[0].iov_len) {
2808 tmplen -= hdtr->trailers[0].iov_len;
2809 --hdtr->numtrailers;
2813 char *iov_base = (char *)hdtr->trailers[0].iov_base;
2815 hdtr->trailers[0].iov_len -= tmplen;
2817 hdtr->trailers[0].iov_base = iov_base;
2826 * emulate_sendfile()
2827 * Sends the contents of file fd along with header/trailer bytes, if any,
2828 * to the network. emulate_sendfile will return only when all the bytes have been
2829 * sent (i.e., it handles partial writes) or on a network error condition.
2831 static apr_status_t emulate_sendfile(core_net_rec *c, apr_file_t *fd,
2832 apr_hdtr_t *hdtr, apr_off_t offset,
2833 apr_size_t length, apr_size_t *nbytes)
2835 apr_status_t rv = APR_SUCCESS;
2836 apr_int32_t togo; /* Remaining number of bytes in the file to send */
2837 apr_size_t sendlen = 0;
2838 apr_size_t bytes_sent;
2840 apr_off_t o; /* Track the file offset for partial writes */
2846 * writev_it_all handles partial writes.
2847 * XXX: optimization... if headers are less than MIN_WRITE_SIZE, copy
2850 if (hdtr && hdtr->numheaders > 0 ) {
2851 for (i = 0; i < hdtr->numheaders; i++) {
2852 sendlen += hdtr->headers[i].iov_len;
2855 rv = writev_it_all(c->client_socket, hdtr->headers, hdtr->numheaders,
2856 sendlen, &bytes_sent);
2857 if (rv == APR_SUCCESS)
2858 *nbytes += bytes_sent; /* track total bytes sent */
2861 /* Seek the file to 'offset' */
2862 if (offset != 0 && rv == APR_SUCCESS) {
2863 rv = apr_file_seek(fd, APR_SET, &offset);
2866 /* Send the file, making sure to handle partial writes */
2868 while (rv == APR_SUCCESS && togo) {
2869 sendlen = togo > sizeof(buffer) ? sizeof(buffer) : togo;
2871 rv = apr_file_read(fd, buffer, &sendlen);
2872 while (rv == APR_SUCCESS && sendlen) {
2873 bytes_sent = sendlen;
2874 rv = apr_send(c->client_socket, &buffer[o], &bytes_sent);
2875 if (rv == APR_SUCCESS) {
2876 sendlen -= bytes_sent; /* sendlen != bytes_sent ==> partial write */
2877 o += bytes_sent; /* o is where we are in the buffer */
2878 *nbytes += bytes_sent;
2879 togo -= bytes_sent; /* track how much of the file we've sent */
2884 /* Send the trailers
2885 * XXX: optimization... if it will fit, send this on the last send in the
2889 if ( rv == APR_SUCCESS && hdtr && hdtr->numtrailers > 0 ) {
2890 for (i = 0; i < hdtr->numtrailers; i++) {
2891 sendlen += hdtr->trailers[i].iov_len;
2893 rv = writev_it_all(c->client_socket, hdtr->trailers, hdtr->numtrailers,
2894 sendlen, &bytes_sent);
2895 if (rv == APR_SUCCESS)
2896 *nbytes += bytes_sent;
2902 /* Note --- ErrorDocument will now work from .htaccess files.
2903 * The AllowOverride of Fileinfo allows webmasters to turn it off
2906 static const command_rec core_cmds[] = {
2908 /* Old access config file commands */
2910 AP_INIT_RAW_ARGS("<Directory", dirsection, NULL, RSRC_CONF,
2911 "Container for directives affecting resources located in the specified "
2913 AP_INIT_RAW_ARGS("<Location", urlsection, NULL, RSRC_CONF,
2914 "Container for directives affecting resources accessed through the "
2915 "specified URL paths"),
2916 AP_INIT_RAW_ARGS("<VirtualHost", virtualhost_section, NULL, RSRC_CONF,
2917 "Container to map directives to a particular virtual host, takes one or "
2918 "more host addresses"),
2919 AP_INIT_RAW_ARGS("<Files", filesection, NULL, OR_ALL,
2920 "Container for directives affecting files matching specified patterns"),
2921 AP_INIT_RAW_ARGS("<Limit", ap_limit_section, NULL, OR_ALL,
2922 "Container for authentication directives when accessed using specified HTTP "
2924 AP_INIT_RAW_ARGS("<LimitExcept", ap_limit_section, (void*)1, OR_ALL,
2925 "Container for authentication directives to be applied when any HTTP "
2926 "method other than those specified is used to access the resource"),
2927 AP_INIT_TAKE1("<IfModule", start_ifmod, NULL, EXEC_ON_READ | OR_ALL,
2928 "Container for directives based on existance of specified modules"),
2929 AP_INIT_TAKE1("<IfDefine", start_ifdefine, NULL, EXEC_ON_READ | OR_ALL,
2930 "Container for directives based on existance of command line defines"),
2931 AP_INIT_RAW_ARGS("<DirectoryMatch", dirsection, (void*)1, RSRC_CONF,
2932 "Container for directives affecting resources located in the "
2933 "specified directories"),
2934 AP_INIT_RAW_ARGS("<LocationMatch", urlsection, (void*)1, RSRC_CONF,
2935 "Container for directives affecting resources accessed through the "
2936 "specified URL paths"),
2937 AP_INIT_RAW_ARGS("<FilesMatch", filesection, (void*)1, OR_ALL,
2938 "Container for directives affecting files matching specified patterns"),
2939 AP_INIT_TAKE1("AuthType", ap_set_string_slot,
2940 (void*)APR_OFFSETOF(core_dir_config, ap_auth_type), OR_AUTHCFG,
2941 "An HTTP authorization type (e.g., \"Basic\")"),
2942 AP_INIT_TAKE1("AuthName", set_authname, NULL, OR_AUTHCFG,
2943 "The authentication realm (e.g. \"Members Only\")"),
2944 AP_INIT_RAW_ARGS("Require", require, NULL, OR_AUTHCFG,
2945 "Selects which authenticated users or groups may access a protected space"),
2946 AP_INIT_TAKE1("Satisfy", satisfy, NULL, OR_AUTHCFG,
2947 "access policy if both allow and require used ('all' or 'any')"),
2949 AP_INIT_TAKE1("GprofDir", set_gprof_dir, NULL, RSRC_CONF,
2950 "Directory to plop gmon.out files"),
2952 AP_INIT_TAKE1("AddDefaultCharset", set_add_default_charset, NULL, OR_FILEINFO,
2953 "The name of the default charset to add to any Content-Type without one or 'Off' to disable"),
2954 AP_INIT_TAKE1("AcceptPathInfo", set_accept_path_info, NULL, OR_FILEINFO,
2955 "Set to on or off for PATH_INFO to be accepted by handlers, or default for the per-handler preference"),
2957 /* Old resource config file commands */
2959 AP_INIT_RAW_ARGS("AccessFileName", set_access_name, NULL, RSRC_CONF,
2960 "Name(s) of per-directory config files (default: .htaccess)"),
2961 AP_INIT_TAKE1("DocumentRoot", set_document_root, NULL, RSRC_CONF,
2962 "Root directory of the document tree"),
2963 AP_INIT_TAKE2("ErrorDocument", set_error_document, NULL, OR_FILEINFO,
2964 "Change responses for HTTP errors"),
2965 AP_INIT_RAW_ARGS("AllowOverride", set_override, NULL, ACCESS_CONF,
2966 "Controls what groups of directives can be configured by per-directory "
2968 AP_INIT_RAW_ARGS("Options", set_options, NULL, OR_OPTIONS,
2969 "Set a number of attributes for a given directory"),
2970 AP_INIT_TAKE1("DefaultType", ap_set_string_slot,
2971 (void*)APR_OFFSETOF(core_dir_config, ap_default_type),
2972 OR_FILEINFO, "the default MIME type for untypable files"),
2973 AP_INIT_RAW_ARGS("FileETag", set_etag_bits, NULL, OR_FILEINFO,
2974 "Specify components used to construct a file's ETag"),
2975 AP_INIT_TAKE1("EnableMMAP", set_enable_mmap, NULL, OR_FILEINFO,
2976 "Controls whether memory-mapping may be used to read files"),
2977 AP_INIT_TAKE1("EnableSendfile", set_enable_sendfile, NULL, OR_FILEINFO,
2978 "Controls whether sendfile may be used to transmit files"),
2980 /* Old server config file commands */
2982 AP_INIT_TAKE1("Port", ap_set_deprecated, NULL, RSRC_CONF,
2983 "Port was replaced with Listen in Apache 2.0"),
2984 AP_INIT_TAKE1("HostnameLookups", set_hostname_lookups, NULL,
2985 ACCESS_CONF|RSRC_CONF,
2986 "\"on\" to enable, \"off\" to disable reverse DNS lookups, or \"double\" to "
2987 "enable double-reverse DNS lookups"),
2988 AP_INIT_TAKE1("ServerAdmin", set_server_string_slot,
2989 (void *)APR_OFFSETOF(server_rec, server_admin), RSRC_CONF,
2990 "The email address of the server administrator"),
2991 AP_INIT_TAKE1("ServerName", server_hostname_port, NULL, RSRC_CONF,
2992 "The hostname and port of the server"),
2993 AP_INIT_TAKE1("ServerSignature", set_signature_flag, NULL, OR_ALL,
2994 "En-/disable server signature (on|off|email)"),
2995 AP_INIT_TAKE1("ServerRoot", set_server_root, NULL, RSRC_CONF | EXEC_ON_READ,
2996 "Common directory of server-related files (logs, confs, etc.)"),
2997 AP_INIT_TAKE1("ErrorLog", set_server_string_slot,
2998 (void *)APR_OFFSETOF(server_rec, error_fname), RSRC_CONF,
2999 "The filename of the error log"),
3000 AP_INIT_RAW_ARGS("ServerAlias", set_server_alias, NULL, RSRC_CONF,
3001 "A name or names alternately used to access the server"),
3002 AP_INIT_TAKE1("ServerPath", set_serverpath, NULL, RSRC_CONF,
3003 "The pathname the server can be reached at"),
3004 AP_INIT_TAKE1("Timeout", set_timeout, NULL, RSRC_CONF,
3005 "Timeout duration (sec)"),
3006 AP_INIT_FLAG("IdentityCheck", set_idcheck, NULL, RSRC_CONF|ACCESS_CONF,
3007 "Enable identd (RFC 1413) user lookups - SLOW"),
3008 AP_INIT_FLAG("ContentDigest", set_content_md5, NULL, OR_OPTIONS,
3009 "whether or not to send a Content-MD5 header with each request"),
3010 AP_INIT_TAKE1("UseCanonicalName", set_use_canonical_name, NULL,
3011 RSRC_CONF|ACCESS_CONF,
3012 "How to work out the ServerName : Port when constructing URLs"),
3013 /* TODO: RlimitFoo should all be part of mod_cgi, not in the core */
3014 /* TODO: ListenBacklog in MPM */
3015 AP_INIT_TAKE1("Include", include_config, NULL,
3016 (RSRC_CONF | ACCESS_CONF | EXEC_ON_READ),
3017 "Name of the config file to be included"),
3018 AP_INIT_TAKE1("LogLevel", set_loglevel, NULL, RSRC_CONF,
3019 "Level of verbosity in error logging"),
3020 AP_INIT_TAKE1("NameVirtualHost", ap_set_name_virtual_host, NULL, RSRC_CONF,
3021 "A numeric IP address:port, or the name of a host"),
3023 AP_INIT_TAKE1("BS2000Account", set_bs2000_account, NULL, RSRC_CONF,
3024 "Name of server User's bs2000 logon account name"),
3026 AP_INIT_TAKE1("ServerTokens", set_serv_tokens, NULL, RSRC_CONF,
3027 "Determine tokens displayed in the Server: header - Min(imal), OS or Full"),
3028 AP_INIT_TAKE1("LimitRequestLine", set_limit_req_line, NULL, RSRC_CONF,
3029 "Limit on maximum size of an HTTP request line"),
3030 AP_INIT_TAKE1("LimitRequestFieldsize", set_limit_req_fieldsize, NULL,
3032 "Limit on maximum size of an HTTP request header field"),
3033 AP_INIT_TAKE1("LimitRequestFields", set_limit_req_fields, NULL, RSRC_CONF,
3034 "Limit (0 = unlimited) on max number of header fields in a request message"),
3035 AP_INIT_TAKE1("LimitRequestBody", set_limit_req_body,
3036 (void*)APR_OFFSETOF(core_dir_config, limit_req_body), OR_ALL,
3037 "Limit (in bytes) on maximum size of request message body"),
3038 AP_INIT_TAKE1("LimitXMLRequestBody", set_limit_xml_req_body, NULL, OR_ALL,
3039 "Limit (in bytes) on maximum size of an XML-based request "
3042 /* System Resource Controls */
3044 AP_INIT_TAKE12("RLimitCPU", set_limit_cpu,
3045 (void*)APR_OFFSETOF(core_dir_config, limit_cpu),
3046 OR_ALL, "Soft/hard limits for max CPU usage in seconds"),
3048 AP_INIT_TAKE12("RLimitCPU", no_set_limit, NULL,
3049 OR_ALL, "Soft/hard limits for max CPU usage in seconds"),
3051 #if defined (RLIMIT_DATA) || defined (RLIMIT_VMEM) || defined (RLIMIT_AS)
3052 AP_INIT_TAKE12("RLimitMEM", set_limit_mem,
3053 (void*)APR_OFFSETOF(core_dir_config, limit_mem),
3054 OR_ALL, "Soft/hard limits for max memory usage per process"),
3056 AP_INIT_TAKE12("RLimitMEM", no_set_limit, NULL,
3057 OR_ALL, "Soft/hard limits for max memory usage per process"),
3060 AP_INIT_TAKE12("RLimitNPROC", set_limit_nproc,
3061 (void*)APR_OFFSETOF(core_dir_config, limit_nproc),
3062 OR_ALL, "soft/hard limits for max number of processes per uid"),
3064 AP_INIT_TAKE12("RLimitNPROC", no_set_limit, NULL,
3065 OR_ALL, "soft/hard limits for max number of processes per uid"),
3068 AP_INIT_TAKE1("ForceType", ap_set_string_slot_lower,
3069 (void *)APR_OFFSETOF(core_dir_config, mime_type), OR_FILEINFO,
3070 "a mime type that overrides other configured type"),
3071 AP_INIT_TAKE1("SetHandler", ap_set_string_slot_lower,
3072 (void *)APR_OFFSETOF(core_dir_config, handler), OR_FILEINFO,
3073 "a handler name that overrides any other configured handler"),
3074 AP_INIT_TAKE1("SetOutputFilter", ap_set_string_slot,
3075 (void *)APR_OFFSETOF(core_dir_config, output_filters), OR_FILEINFO,
3076 "filter (or ; delimited list of filters) to be run on the request content"),
3077 AP_INIT_TAKE1("SetInputFilter", ap_set_string_slot,
3078 (void *)APR_OFFSETOF(core_dir_config, input_filters), OR_FILEINFO,
3079 "filter (or ; delimited list of filters) to be run on the request body"),
3080 AP_INIT_ITERATE2("AddOutputFilterByType", add_ct_output_filters,
3081 (void *)APR_OFFSETOF(core_dir_config, ct_output_filters), OR_FILEINFO,
3082 "output filter name followed by one or more content-types"),
3085 * These are default configuration directives that mpms can/should
3086 * pay attention to. If an mpm wishes to use these, they should
3087 * #defined them in mpm.h.
3089 #ifdef AP_MPM_WANT_SET_PIDFILE
3090 AP_INIT_TAKE1("PidFile", ap_mpm_set_pidfile, NULL, RSRC_CONF,
3091 "A file for logging the server process ID"),
3093 #ifdef AP_MPM_WANT_SET_SCOREBOARD
3094 AP_INIT_TAKE1("ScoreBoardFile", ap_mpm_set_scoreboard, NULL, RSRC_CONF,
3095 "A file for Apache to maintain runtime process management information"),
3097 #ifdef AP_MPM_WANT_SET_LOCKFILE
3098 AP_INIT_TAKE1("LockFile", ap_mpm_set_lockfile, NULL, RSRC_CONF,
3099 "The lockfile used when Apache needs to lock the accept() call"),
3101 #ifdef AP_MPM_WANT_SET_MAX_REQUESTS
3102 AP_INIT_TAKE1("MaxRequestsPerChild", ap_mpm_set_max_requests, NULL, RSRC_CONF,
3103 "Maximum number of requests a particular child serves before dying."),
3105 #ifdef AP_MPM_WANT_SET_COREDUMPDIR
3106 AP_INIT_TAKE1("CoreDumpDirectory", ap_mpm_set_coredumpdir, NULL, RSRC_CONF,
3107 "The location of the directory Apache changes to before dumping core"),
3109 #ifdef AP_MPM_WANT_SET_ACCEPT_LOCK_MECH
3110 AP_INIT_TAKE1("AcceptMutex", ap_mpm_set_accept_lock_mech, NULL, RSRC_CONF,
3111 ap_valid_accept_mutex_string),
3113 #ifdef AP_MPM_WANT_SET_MAX_MEM_FREE
3114 AP_INIT_TAKE1("MaxMemFree", ap_mpm_set_max_mem_free, NULL, RSRC_CONF,
3115 "Maximum number of 1k blocks a particular childs allocator may hold."),
3120 /*****************************************************************
3122 * Core handlers for various phases of server operation...
3125 AP_DECLARE_NONSTD(int) ap_core_translate(request_rec *r)
3127 void *sconf = r->server->module_config;
3128 core_server_config *conf = ap_get_module_config(sconf, &core_module);
3130 /* XXX this seems too specific, this should probably become
3131 * some general-case test
3134 return HTTP_FORBIDDEN;
3136 if (!r->uri || ((r->uri[0] != '/') && strcmp(r->uri, "*"))) {
3137 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
3138 "Invalid URI in request %s", r->the_request);
3139 return HTTP_BAD_REQUEST;
3143 && !strncmp(r->uri, r->server->path, r->server->pathlen)
3144 && (r->server->path[r->server->pathlen - 1] == '/'
3145 || r->uri[r->server->pathlen] == '/'
3146 || r->uri[r->server->pathlen] == '\0'))
3148 /* skip all leading /'s (e.g. http://localhost///foo)
3149 * so we are looking at only the relative path.
3151 char *path = r->uri + r->server->pathlen;
3152 while (*path == '/') {
3155 if (apr_filepath_merge(&r->filename, conf->ap_document_root, path,
3156 APR_FILEPATH_TRUENAME
3157 | APR_FILEPATH_SECUREROOT, r->pool)
3159 return HTTP_FORBIDDEN;
3161 r->canonical_filename = r->filename;
3165 * Make sure that we do not mess up the translation by adding two
3166 * /'s in a row. This happens under windows when the document
3167 * root ends with a /
3169 /* skip all leading /'s (e.g. http://localhost///foo)
3170 * so we are looking at only the relative path.
3172 char *path = r->uri;
3173 while (*path == '/') {
3176 if (apr_filepath_merge(&r->filename, conf->ap_document_root, path,
3177 APR_FILEPATH_TRUENAME
3178 | APR_FILEPATH_SECUREROOT, r->pool)
3180 return HTTP_FORBIDDEN;
3182 r->canonical_filename = r->filename;
3188 /*****************************************************************
3190 * Test the filesystem name through directory_walk and file_walk
3192 static int core_map_to_storage(request_rec *r)
3196 if ((access_status = ap_directory_walk(r))) {
3197 return access_status;
3200 if ((access_status = ap_file_walk(r))) {
3201 return access_status;
3208 static int do_nothing(request_rec *r) { return OK; }
3211 static int core_override_type(request_rec *r)
3213 core_dir_config *conf =
3214 (core_dir_config *)ap_get_module_config(r->per_dir_config,
3217 /* Check for overrides with ForceType / SetHandler
3219 if (conf->mime_type && strcmp(conf->mime_type, "none"))
3220 ap_set_content_type(r, (char*) conf->mime_type);
3222 if (conf->handler && strcmp(conf->handler, "none"))
3223 r->handler = conf->handler;
3225 /* Deal with the poor soul who is trying to force path_info to be
3226 * accepted within the core_handler, where they will let the subreq
3227 * address its contents. This is toggled by the user in the very
3228 * beginning of the fixup phase, so modules should override the user's
3229 * discretion in their own module fixup phase. It is tristate, if
3230 * the user doesn't specify, the result is 2 (which the module may
3231 * interpret to its own customary behavior.) It won't be touched
3232 * if the value is no longer undefined (2), so any module changing
3233 * the value prior to the fixup phase OVERRIDES the user's choice.
3235 if ((r->used_path_info == AP_REQ_DEFAULT_PATH_INFO)
3236 && (conf->accept_path_info != 3)) {
3237 r->used_path_info = conf->accept_path_info;
3245 static int default_handler(request_rec *r)
3247 conn_rec *c = r->connection;
3248 apr_bucket_brigade *bb;
3252 apr_file_t *fd = NULL;
3253 apr_status_t status;
3254 /* XXX if/when somebody writes a content-md5 filter we either need to
3255 * remove this support or coordinate when to use the filter vs.
3256 * when to use this code
3257 * The current choice of when to compute the md5 here matches the 1.3
3258 * support fairly closely (unlike 1.3, we don't handle computing md5
3259 * when the charset is translated).
3261 int bld_content_md5;
3263 d = (core_dir_config *)ap_get_module_config(r->per_dir_config,
3265 bld_content_md5 = (d->content_md5 & 1)
3266 && r->output_filters->frec->ftype != AP_FTYPE_RESOURCE;
3268 ap_allow_standard_methods(r, MERGE_ALLOW, M_GET, M_OPTIONS, M_POST, -1);
3270 /* If filters intend to consume the request body, they must
3271 * register an InputFilter to slurp the contents of the POST
3272 * data from the POST input stream. It no longer exists when
3273 * the output filters are invoked by the default handler.
3275 if ((errstatus = ap_discard_request_body(r)) != OK) {
3279 if (r->method_number == M_GET || r->method_number == M_POST) {
3280 if (r->finfo.filetype == 0) {
3281 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
3282 "File does not exist: %s", r->filename);
3283 return HTTP_NOT_FOUND;
3286 /* Don't try to serve a dir. Some OSs do weird things with
3289 if (r->finfo.filetype == APR_DIR) {
3290 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
3291 "Attempt to serve directory: %s", r->filename);
3292 return HTTP_NOT_FOUND;
3295 if ((r->used_path_info != AP_REQ_ACCEPT_PATH_INFO) &&
3296 r->path_info && *r->path_info)
3298 /* default to reject */
3299 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
3300 "File does not exist: %s",
3301 apr_pstrcat(r->pool, r->filename, r->path_info, NULL));
3302 return HTTP_NOT_FOUND;
3305 /* We understood the (non-GET) method, but it might not be legal for
3306 this particular resource. Check to see if the 'deliver_script'
3307 flag is set. If so, then we go ahead and deliver the file since
3308 it isn't really content (only GET normally returns content).
3310 Note: based on logic further above, the only possible non-GET
3311 method at this point is POST. In the future, we should enable
3312 script delivery for all methods. */
3313 if (r->method_number != M_GET) {
3314 core_request_config *req_cfg;
3316 req_cfg = ap_get_module_config(r->request_config, &core_module);
3317 if (!req_cfg->deliver_script) {
3318 /* The flag hasn't been set for this request. Punt. */
3319 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
3320 "This resource does not accept the %s method.",
3322 return HTTP_METHOD_NOT_ALLOWED;
3327 if ((status = apr_file_open(&fd, r->filename, APR_READ | APR_BINARY
3328 #if APR_HAS_SENDFILE
3329 | ((d->enable_sendfile == ENABLE_SENDFILE_OFF)
3330 ? 0 : APR_SENDFILE_ENABLED)
3332 , 0, r->pool)) != APR_SUCCESS) {
3333 ap_log_rerror(APLOG_MARK, APLOG_ERR, status, r,
3334 "file permissions deny server access: %s", r->filename);
3335 return HTTP_FORBIDDEN;
3338 ap_update_mtime(r, r->finfo.mtime);
3339 ap_set_last_modified(r);
3341 apr_table_setn(r->headers_out, "Accept-Ranges", "bytes");
3342 ap_set_content_length(r, r->finfo.size);
3343 if ((errstatus = ap_meets_conditions(r)) != OK) {
3348 if (bld_content_md5) {
3349 apr_table_setn(r->headers_out, "Content-MD5",
3350 ap_md5digest(r->pool, fd));
3353 bb = apr_brigade_create(r->pool, c->bucket_alloc);
3354 #if APR_HAS_SENDFILE && APR_HAS_LARGE_FILES
3355 if ((d->enable_sendfile != ENABLE_SENDFILE_OFF) &&
3356 (r->finfo.size > AP_MAX_SENDFILE)) {
3357 /* APR_HAS_LARGE_FILES issue; must split into mutiple buckets,
3358 * no greater than MAX(apr_size_t), and more granular than that
3359 * in case the brigade code/filters attempt to read it directly.
3361 apr_off_t fsize = r->finfo.size;
3362 e = apr_bucket_file_create(fd, 0, AP_MAX_SENDFILE, r->pool,
3364 while (fsize > AP_MAX_SENDFILE) {
3366 apr_bucket_copy(e, &ce);
3367 APR_BRIGADE_INSERT_TAIL(bb, ce);
3368 e->start += AP_MAX_SENDFILE;
3369 fsize -= AP_MAX_SENDFILE;
3371 e->length = (apr_size_t)fsize; /* Resize just the last bucket */
3375 e = apr_bucket_file_create(fd, 0, (apr_size_t)r->finfo.size,
3376 r->pool, c->bucket_alloc);
3379 if (d->enable_mmap == ENABLE_MMAP_OFF) {
3380 (void)apr_bucket_file_enable_mmap(e, 0);
3383 APR_BRIGADE_INSERT_TAIL(bb, e);
3384 e = apr_bucket_eos_create(c->bucket_alloc);
3385 APR_BRIGADE_INSERT_TAIL(bb, e);
3387 return ap_pass_brigade(r->output_filters, bb);
3389 else { /* unusual method (not GET or POST) */
3390 if (r->method_number == M_INVALID) {
3391 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
3392 "Invalid method in request %s", r->the_request);
3393 return HTTP_NOT_IMPLEMENTED;
3396 if (r->method_number == M_OPTIONS) {
3397 return ap_send_http_options(r);
3399 return HTTP_METHOD_NOT_ALLOWED;
3403 typedef struct net_time_filter_ctx {
3406 } net_time_filter_ctx_t;
3407 static int net_time_filter(ap_filter_t *f, apr_bucket_brigade *b,
3408 ap_input_mode_t mode, apr_read_type_e block,
3409 apr_off_t readbytes)
3411 net_time_filter_ctx_t *ctx = f->ctx;
3412 int keptalive = f->c->keepalive == AP_CONN_KEEPALIVE;
3415 f->ctx = ctx = apr_palloc(f->r->pool, sizeof(*ctx));
3416 ctx->first_line = 1;
3417 ctx->csd = ap_get_module_config(f->c->conn_config, &core_module);
3420 if (mode != AP_MODE_INIT && mode != AP_MODE_EATCRLF) {
3421 if (ctx->first_line) {
3422 apr_socket_timeout_set(ctx->csd,
3424 ? f->c->base_server->keep_alive_timeout
3425 : f->c->base_server->timeout);
3426 ctx->first_line = 0;
3430 apr_socket_timeout_set(ctx->csd, f->c->base_server->timeout);
3434 return ap_get_brigade(f->next, b, mode, block, readbytes);
3438 * Remove all zero length buckets from the brigade.
3440 #define BRIGADE_NORMALIZE(b) \
3442 apr_bucket *e = APR_BRIGADE_FIRST(b); \
3444 if (e->length == 0 && !APR_BUCKET_IS_METADATA(e)) { \
3446 d = APR_BUCKET_NEXT(e); \
3447 apr_bucket_delete(e); \
3450 e = APR_BUCKET_NEXT(e); \
3451 } while (!APR_BRIGADE_EMPTY(b) && (e != APR_BRIGADE_SENTINEL(b))); \
3454 static int core_input_filter(ap_filter_t *f, apr_bucket_brigade *b,
3455 ap_input_mode_t mode, apr_read_type_e block,
3456 apr_off_t readbytes)
3460 core_net_rec *net = f->ctx;
3461 core_ctx_t *ctx = net->in_ctx;
3465 if (mode == AP_MODE_INIT) {
3467 * this mode is for filters that might need to 'initialize'
3468 * a connection before reading request data from a client.
3469 * NNTP over SSL for example needs to handshake before the
3470 * server sends the welcome message.
3471 * such filters would have changed the mode before this point
3472 * is reached. however, protocol modules such as NNTP should
3473 * not need to know anything about SSL. given the example, if
3474 * SSL is not in the filter chain, AP_MODE_INIT is a noop.
3481 ctx = apr_pcalloc(f->c->pool, sizeof(*ctx));
3482 ctx->b = apr_brigade_create(f->c->pool, f->c->bucket_alloc);
3484 /* seed the brigade with the client socket. */
3485 e = apr_bucket_socket_create(net->client_socket, f->c->bucket_alloc);
3486 APR_BRIGADE_INSERT_TAIL(ctx->b, e);
3489 else if (APR_BRIGADE_EMPTY(ctx->b)) {
3493 /* ### This is bad. */
3494 BRIGADE_NORMALIZE(ctx->b);
3496 /* check for empty brigade again *AFTER* BRIGADE_NORMALIZE()
3497 * If we have lost our socket bucket (see above), we are EOF.
3499 * Ideally, this should be returning SUCCESS with EOS bucket, but
3500 * some higher-up APIs (spec. read_request_line via ap_rgetline)
3501 * want an error code. */
3502 if (APR_BRIGADE_EMPTY(ctx->b)) {
3506 if (mode == AP_MODE_GETLINE) {
3507 /* we are reading a single LF line, e.g. the HTTP headers */
3508 rv = apr_brigade_split_line(b, ctx->b, block, HUGE_STRING_LEN);
3509 /* We should treat EAGAIN here the same as we do for EOF (brigade is
3510 * empty). We do this by returning whatever we have read. This may
3511 * or may not be bogus, but is consistent (for now) with EOF logic.
3513 if (APR_STATUS_IS_EAGAIN(rv)) {
3519 /* ### AP_MODE_PEEK is a horrific name for this mode because we also
3520 * eat any CRLFs that we see. That's not the obvious intention of
3521 * this mode. Determine whether anyone actually uses this or not. */
3522 if (mode == AP_MODE_EATCRLF) {
3526 /* The purpose of this loop is to ignore any CRLF (or LF) at the end
3527 * of a request. Many browsers send extra lines at the end of POST
3528 * requests. We use the PEEK method to determine if there is more
3529 * data on the socket, so that we know if we should delay sending the
3530 * end of one request until we have served the second request in a
3531 * pipelined situation. We don't want to actually delay sending a
3532 * response if the server finds a CRLF (or LF), becuause that doesn't
3533 * mean that there is another request, just a blank line.
3536 if (APR_BRIGADE_EMPTY(ctx->b))
3539 e = APR_BRIGADE_FIRST(ctx->b);
3541 rv = apr_bucket_read(e, &str, &len, APR_NONBLOCK_READ);
3543 if (rv != APR_SUCCESS)
3547 while (c < str + len) {
3548 if (*c == APR_ASCII_LF)
3550 else if (*c == APR_ASCII_CR && *(c + 1) == APR_ASCII_LF)
3556 /* If we reach here, we were a bucket just full of CRLFs, so
3557 * just toss the bucket. */
3558 /* FIXME: Is this the right thing to do in the core? */
3559 apr_bucket_delete(e);
3564 /* If mode is EXHAUSTIVE, we want to just read everything until the end
3565 * of the brigade, which in this case means the end of the socket.
3566 * To do this, we attach the brigade that has currently been setaside to
3567 * the brigade that was passed down, and send that brigade back.
3569 * NOTE: This is VERY dangerous to use, and should only be done with
3570 * extreme caution. However, the Perchild MPM needs this feature
3571 * if it is ever going to work correctly again. With this, the Perchild
3572 * MPM can easily request the socket and all data that has been read,
3573 * which means that it can pass it to the correct child process.
3575 if (mode == AP_MODE_EXHAUSTIVE) {
3578 /* Tack on any buckets that were set aside. */
3579 APR_BRIGADE_CONCAT(b, ctx->b);
3581 /* Since we've just added all potential buckets (which will most
3582 * likely simply be the socket bucket) we know this is the end,
3583 * so tack on an EOS too. */
3584 /* We have read until the brigade was empty, so we know that we
3586 e = apr_bucket_eos_create(f->c->bucket_alloc);
3587 APR_BRIGADE_INSERT_TAIL(b, e);
3591 /* read up to the amount they specified. */
3592 if (mode == AP_MODE_READBYTES || mode == AP_MODE_SPECULATIVE) {
3594 apr_bucket_brigade *newbb;
3596 AP_DEBUG_ASSERT(readbytes > 0);
3598 e = APR_BRIGADE_FIRST(ctx->b);
3599 rv = apr_bucket_read(e, &str, &len, block);
3601 if (APR_STATUS_IS_EAGAIN(rv)) {
3604 else if (rv != APR_SUCCESS) {
3607 else if (block == APR_BLOCK_READ && len == 0) {
3608 /* We wanted to read some bytes in blocking mode. We read
3609 * 0 bytes. Hence, we now assume we are EOS.
3611 * When we are in normal mode, return an EOS bucket to the
3613 * When we are in speculative mode, leave ctx->b empty, so
3614 * that the next call returns an EOS bucket.
3616 apr_bucket_delete(e);
3618 if (mode == AP_MODE_READBYTES) {
3619 e = apr_bucket_eos_create(f->c->bucket_alloc);
3620 APR_BRIGADE_INSERT_TAIL(b, e);
3625 /* We can only return at most what we read. */
3626 if (len < readbytes) {
3630 rv = apr_brigade_partition(ctx->b, readbytes, &e);
3631 if (rv != APR_SUCCESS) {
3635 /* Must do split before CONCAT */
3636 newbb = apr_brigade_split(ctx->b, e);
3638 if (mode == AP_MODE_READBYTES) {
3639 APR_BRIGADE_CONCAT(b, ctx->b);
3641 else if (mode == AP_MODE_SPECULATIVE) {
3642 apr_bucket *copy_bucket;
3643 APR_BRIGADE_FOREACH(e, ctx->b) {
3644 rv = apr_bucket_copy(e, ©_bucket);
3645 if (rv != APR_SUCCESS) {
3648 APR_BRIGADE_INSERT_TAIL(b, copy_bucket);
3652 /* Take what was originally there and place it back on ctx->b */
3653 APR_BRIGADE_CONCAT(ctx->b, newbb);
3658 /* Default filter. This filter should almost always be used. Its only job
3659 * is to send the headers if they haven't already been sent, and then send
3662 #define MAX_IOVEC_TO_WRITE 16
3664 /* Optional function coming from mod_logio, used for logging of output
3667 static APR_OPTIONAL_FN_TYPE(ap_logio_add_bytes_out) *logio_add_bytes_out;
3669 static apr_status_t core_output_filter(ap_filter_t *f, apr_bucket_brigade *b)
3672 apr_bucket_brigade *more;
3674 core_net_rec *net = f->ctx;
3675 core_output_filter_ctx_t *ctx = net->out_ctx;
3676 apr_read_type_e eblock = APR_NONBLOCK_READ;
3677 apr_pool_t *input_pool = b->p;
3680 ctx = apr_pcalloc(c->pool, sizeof(*ctx));
3684 /* If we have a saved brigade, concatenate the new brigade to it */
3686 APR_BRIGADE_CONCAT(ctx->b, b);
3691 /* Perform multiple passes over the brigade, sending batches of output
3692 to the connection. */
3693 while (b && !APR_BRIGADE_EMPTY(b)) {
3694 apr_size_t nbytes = 0;
3695 apr_bucket *last_e = NULL; /* initialized for debugging */
3698 /* one group of iovecs per pass over the brigade */
3699 apr_size_t nvec = 0;
3700 apr_size_t nvec_trailers = 0;
3701 struct iovec vec[MAX_IOVEC_TO_WRITE];
3702 struct iovec vec_trailers[MAX_IOVEC_TO_WRITE];
3704 /* one file per pass over the brigade */
3705 apr_file_t *fd = NULL;
3706 apr_size_t flen = 0;
3707 apr_off_t foffset = 0;
3709 /* keep track of buckets that we've concatenated
3710 * to avoid small writes
3712 apr_bucket *last_merged_bucket = NULL;
3714 /* tail of brigade if we need another pass */
3717 /* Iterate over the brigade: collect iovecs and/or a file */
3718 APR_BRIGADE_FOREACH(e, b) {
3719 /* keep track of the last bucket processed */
3721 if (APR_BUCKET_IS_EOS(e)) {
3724 if (APR_BUCKET_IS_FLUSH(e)) {
3725 more = apr_brigade_split(b, APR_BUCKET_NEXT(e));
3729 /* It doesn't make any sense to use sendfile for a file bucket
3730 * that represents 10 bytes.
3732 else if (APR_BUCKET_IS_FILE(e)
3733 && (e->length >= AP_MIN_SENDFILE_BYTES)) {
3734 apr_bucket_file *a = e->data;
3736 /* We can't handle more than one file bucket at a time
3737 * so we split here and send the file we have already
3741 more = apr_brigade_split(b, e);
3753 rv = apr_bucket_read(e, &str, &n, eblock);
3754 if (APR_STATUS_IS_EAGAIN(rv)) {
3755 /* send what we have so far since we shouldn't expect more
3756 * output for a while... next time we read, block
3758 more = apr_brigade_split(b, e);
3759 eblock = APR_BLOCK_READ;
3762 eblock = APR_NONBLOCK_READ;
3765 if (nvec == MAX_IOVEC_TO_WRITE) {
3766 /* woah! too many. buffer them up, for use later. */
3767 apr_bucket *temp, *next;
3768 apr_bucket_brigade *temp_brig;
3770 if (nbytes >= AP_MIN_BYTES_TO_WRITE) {
3771 /* We have enough data in the iovec
3772 * to justify doing a writev
3774 more = apr_brigade_split(b, e);
3778 /* Create a temporary brigade as a means
3779 * of concatenating a bunch of buckets together
3781 if (last_merged_bucket) {
3782 /* If we've concatenated together small
3783 * buckets already in a previous pass,
3784 * the initial buckets in this brigade
3785 * are heap buckets that may have extra
3786 * space left in them (because they
3787 * were created by apr_brigade_write()).
3788 * We can take advantage of this by
3789 * building the new temp brigade out of
3790 * these buckets, so that the content
3791 * in them doesn't have to be copied again.
3793 apr_bucket_brigade *bb;
3794 bb = apr_brigade_split(b,
3795 APR_BUCKET_NEXT(last_merged_bucket));
3800 temp_brig = apr_brigade_create(f->c->pool,
3801 f->c->bucket_alloc);
3804 temp = APR_BRIGADE_FIRST(b);
3807 rv = apr_bucket_read(temp, &str, &n, APR_BLOCK_READ);
3808 apr_brigade_write(temp_brig, NULL, NULL, str, n);
3810 temp = APR_BUCKET_NEXT(temp);
3811 apr_bucket_delete(d);
3816 temp = APR_BRIGADE_FIRST(temp_brig);
3817 APR_BUCKET_REMOVE(temp);
3818 APR_BRIGADE_INSERT_HEAD(b, temp);
3819 apr_bucket_read(temp, &str, &n, APR_BLOCK_READ);
3820 vec[nvec].iov_base = (char*) str;
3821 vec[nvec].iov_len = n;
3824 /* Just in case the temporary brigade has
3825 * multiple buckets, recover the rest of
3826 * them and put them in the brigade that
3829 for (next = APR_BRIGADE_FIRST(temp_brig);
3830 next != APR_BRIGADE_SENTINEL(temp_brig);
3831 next = APR_BRIGADE_FIRST(temp_brig)) {
3832 APR_BUCKET_REMOVE(next);
3833 APR_BUCKET_INSERT_AFTER(temp, next);
3835 apr_bucket_read(next, &str, &n,
3837 vec[nvec].iov_base = (char*) str;
3838 vec[nvec].iov_len = n;
3842 apr_brigade_destroy(temp_brig);
3844 last_merged_bucket = temp;
3849 vec[nvec].iov_base = (char*) str;
3850 vec[nvec].iov_len = n;
3855 /* The bucket is a trailer to a file bucket */
3857 if (nvec_trailers == MAX_IOVEC_TO_WRITE) {
3858 /* woah! too many. stop now. */
3859 more = apr_brigade_split(b, e);
3863 vec_trailers[nvec_trailers].iov_base = (char*) str;
3864 vec_trailers[nvec_trailers].iov_len = n;
3874 /* Completed iterating over the brigades, now determine if we want
3875 * to buffer the brigade or send the brigade out on the network.
3877 * Save if we haven't accumulated enough bytes to send, and:
3879 * 1) we didn't see a file, we don't have more passes over the
3880 * brigade to perform, AND we didn't stop at a FLUSH bucket.
3881 * (IOW, we will save plain old bytes such as HTTP headers)
3883 * 2) we hit the EOS and have a keep-alive connection
3884 * (IOW, this response is a bit more complex, but we save it
3885 * with the hope of concatenating with another response)
3887 if (nbytes + flen < AP_MIN_BYTES_TO_WRITE
3888 && ((!fd && !more && !APR_BUCKET_IS_FLUSH(last_e))
3889 || (APR_BUCKET_IS_EOS(last_e)
3890 && c->keepalive == AP_CONN_KEEPALIVE))) {
3892 /* NEVER save an EOS in here. If we are saving a brigade with
3893 * an EOS bucket, then we are doing keepalive connections, and
3894 * we want to process to second request fully.
3896 if (APR_BUCKET_IS_EOS(last_e)) {
3898 int file_bucket_saved = 0;
3899 apr_bucket_delete(last_e);
3900 for (bucket = APR_BRIGADE_FIRST(b);
3901 bucket != APR_BRIGADE_SENTINEL(b);
3902 bucket = APR_BUCKET_NEXT(bucket)) {
3904 /* Do a read on each bucket to pull in the
3905 * data from pipe and socket buckets, so
3906 * that we don't leave their file descriptors
3907 * open indefinitely. Do the same for file
3908 * buckets, with one exception: allow the
3909 * first file bucket in the brigade to remain
3910 * a file bucket, so that we don't end up
3911 * doing an mmap+memcpy every time a client
3912 * requests a <8KB file over a keepalive
3915 if (APR_BUCKET_IS_FILE(bucket) && !file_bucket_saved) {
3916 file_bucket_saved = 1;
3921 rv = apr_bucket_read(bucket, &buf, &len,
3923 if (rv != APR_SUCCESS) {
3924 ap_log_error(APLOG_MARK, APLOG_ERR, rv,
3925 c->base_server, "core_output_filter:"
3926 " Error reading from bucket.");
3927 return HTTP_INTERNAL_SERVER_ERROR;
3932 if (!ctx->deferred_write_pool) {
3933 apr_pool_create(&ctx->deferred_write_pool, c->pool);
3935 ap_save_brigade(f, &ctx->b, &b, ctx->deferred_write_pool);
3942 apr_size_t bytes_sent;
3944 #if APR_HAS_SENDFILE
3945 apr_int32_t flags = 0;
3948 memset(&hdtr, '\0', sizeof(hdtr));
3950 hdtr.numheaders = nvec;
3954 if (nvec_trailers) {
3955 hdtr.numtrailers = nvec_trailers;
3956 hdtr.trailers = vec_trailers;
3959 #if APR_HAS_SENDFILE
3960 if (apr_file_flags_get(fd) & APR_SENDFILE_ENABLED) {
3962 if (c->keepalive == AP_CONN_CLOSE && APR_BUCKET_IS_EOS(last_e)) {
3963 /* Prepare the socket to be reused */
3964 flags |= APR_SENDFILE_DISCONNECT_SOCKET;
3967 rv = sendfile_it_all(net, /* the network information */
3968 fd, /* the file to send */
3969 &hdtr, /* header and trailer iovecs */
3970 foffset, /* offset in the file to begin
3972 flen, /* length of file */
3973 nbytes + flen, /* total length including
3975 &bytes_sent, /* how many bytes were
3977 flags); /* apr_sendfile flags */
3979 if (logio_add_bytes_out && bytes_sent > 0)
3980 logio_add_bytes_out(c, bytes_sent);
3985 rv = emulate_sendfile(net, fd, &hdtr, foffset, flen,
3988 if (logio_add_bytes_out && bytes_sent > 0)
3989 logio_add_bytes_out(c, bytes_sent);
3995 apr_size_t bytes_sent;
3997 rv = writev_it_all(net->client_socket,
3999 nbytes, &bytes_sent);
4001 if (logio_add_bytes_out && bytes_sent > 0)
4002 logio_add_bytes_out(c, bytes_sent);
4005 apr_brigade_destroy(b);
4007 /* drive cleanups for resources which were set aside
4008 * this may occur before or after termination of the request which
4009 * created the resource
4011 if (ctx->deferred_write_pool) {
4012 if (more && more->p == ctx->deferred_write_pool) {
4013 /* "more" belongs to the deferred_write_pool,
4014 * which is about to be cleared.
4016 if (APR_BRIGADE_EMPTY(more)) {
4020 /* uh oh... change more's lifetime
4021 * to the input brigade's lifetime
4023 apr_bucket_brigade *tmp_more = more;
4025 ap_save_brigade(f, &more, &tmp_more, input_pool);
4028 apr_pool_clear(ctx->deferred_write_pool);
4031 if (rv != APR_SUCCESS) {
4032 ap_log_error(APLOG_MARK, APLOG_INFO, rv, c->base_server,
4033 "core_output_filter: writing data to the network");
4036 apr_brigade_destroy(more);
4038 /* No need to check for SUCCESS, we did that above. */
4039 if (!APR_STATUS_IS_EAGAIN(rv)) {
4043 /* The client has aborted, but the request was successful. We
4044 * will report success, and leave it to the access and error
4045 * logs to note that the connection was aborted.
4052 } /* end while () */
4057 static int core_post_config(apr_pool_t *pconf, apr_pool_t *plog, apr_pool_t *ptemp, server_rec *s)
4059 logio_add_bytes_out = APR_RETRIEVE_OPTIONAL_FN(ap_logio_add_bytes_out);
4061 ap_set_version(pconf);
4062 ap_setup_make_content_type(pconf);
4066 static void core_insert_filter(request_rec *r)
4068 core_dir_config *conf = (core_dir_config *)
4069 ap_get_module_config(r->per_dir_config,
4071 const char *filter, *filters = conf->output_filters;
4074 while (*filters && (filter = ap_getword(r->pool, &filters, ';'))) {
4075 ap_add_output_filter(filter, NULL, r, r->connection);
4079 filters = conf->input_filters;
4081 while (*filters && (filter = ap_getword(r->pool, &filters, ';'))) {
4082 ap_add_input_filter(filter, NULL, r, r->connection);
4087 static apr_size_t num_request_notes = AP_NUM_STD_NOTES;
4089 static apr_status_t reset_request_notes(void *dummy)
4091 num_request_notes = AP_NUM_STD_NOTES;
4095 AP_DECLARE(apr_size_t) ap_register_request_note(void)
4097 apr_pool_cleanup_register(apr_hook_global_pool, NULL, reset_request_notes,
4098 apr_pool_cleanup_null);
4099 return num_request_notes++;
4102 AP_DECLARE(void **) ap_get_request_note(request_rec *r, apr_size_t note_num)
4104 core_request_config *req_cfg;
4106 if (note_num >= num_request_notes) {
4110 req_cfg = (core_request_config *)
4111 ap_get_module_config(r->request_config, &core_module);
4117 return &(req_cfg->notes[note_num]);
4120 static int core_create_req(request_rec *r)
4122 /* Alloc the config struct and the array of request notes in
4123 * a single block for efficiency
4125 core_request_config *req_cfg;
4127 req_cfg = apr_pcalloc(r->pool, sizeof(core_request_config) +
4128 sizeof(void *) * num_request_notes);
4129 req_cfg->notes = (void **)((char *)req_cfg + sizeof(core_request_config));
4131 /* ### temporarily enable script delivery as the default */
4132 req_cfg->deliver_script = 1;
4135 core_request_config *main_req_cfg = (core_request_config *)
4136 ap_get_module_config(r->main->request_config, &core_module);
4137 req_cfg->bb = main_req_cfg->bb;
4140 req_cfg->bb = apr_brigade_create(r->pool, r->connection->bucket_alloc);
4142 ap_add_input_filter_handle(ap_net_time_filter_handle,
4143 NULL, r, r->connection);
4147 ap_set_module_config(r->request_config, &core_module, req_cfg);
4149 /* Begin by presuming any module can make its own path_info assumptions,
4150 * until some module interjects and changes the value.
4152 r->used_path_info = AP_REQ_DEFAULT_PATH_INFO;
4157 static int core_create_proxy_req(request_rec *r, request_rec *pr)
4159 return core_create_req(pr);
4162 static conn_rec *core_create_conn(apr_pool_t *ptrans, server_rec *server,
4163 apr_socket_t *csd, long id, void *sbh,
4164 apr_bucket_alloc_t *alloc)
4167 conn_rec *c = (conn_rec *) apr_pcalloc(ptrans, sizeof(conn_rec));
4170 (void)ap_update_child_status(c->sbh, SERVER_BUSY_READ, (request_rec *)NULL);
4172 /* Got a connection structure, so initialize what fields we can
4173 * (the rest are zeroed out by pcalloc).
4175 c->conn_config = ap_create_conn_config(ptrans);
4176 c->notes = apr_table_make(ptrans, 5);
4179 if ((rv = apr_socket_addr_get(&c->local_addr, APR_LOCAL, csd))
4181 ap_log_error(APLOG_MARK, APLOG_INFO, rv, server,
4182 "apr_socket_addr_get(APR_LOCAL)");
4183 apr_socket_close(csd);
4187 apr_sockaddr_ip_get(&c->local_ip, c->local_addr);
4188 if ((rv = apr_socket_addr_get(&c->remote_addr, APR_REMOTE, csd))
4190 ap_log_error(APLOG_MARK, APLOG_INFO, rv, server,
4191 "apr_socket_addr_get(APR_REMOTE)");
4192 apr_socket_close(csd);
4196 apr_sockaddr_ip_get(&c->remote_ip, c->remote_addr);
4197 c->base_server = server;
4200 c->bucket_alloc = alloc;
4205 static int core_pre_connection(conn_rec *c, void *csd)
4207 core_net_rec *net = apr_palloc(c->pool, sizeof(*net));
4209 #ifdef AP_MPM_DISABLE_NAGLE_ACCEPTED_SOCK
4210 /* BillS says perhaps this should be moved to the MPMs. Some OSes
4211 * allow listening socket attributes to be inherited by the
4212 * accept sockets which means this call only needs to be made
4213 * once on the listener
4215 ap_sock_disable_nagle(csd);
4219 net->out_ctx = NULL;
4220 net->client_socket = csd;
4222 ap_set_module_config(net->c->conn_config, &core_module, csd);
4223 ap_add_input_filter_handle(ap_core_input_filter_handle, net, NULL, net->c);
4224 ap_add_output_filter_handle(ap_core_output_filter_handle, net, NULL, net->c);
4228 static void register_hooks(apr_pool_t *p)
4230 /* create_connection and install_transport_filters are
4231 * hooks that should always be APR_HOOK_REALLY_LAST to give other
4232 * modules the opportunity to install alternate network transports
4233 * and stop other functions from being run.
4235 ap_hook_create_connection(core_create_conn, NULL, NULL,
4236 APR_HOOK_REALLY_LAST);
4237 ap_hook_pre_connection(core_pre_connection, NULL, NULL,
4238 APR_HOOK_REALLY_LAST);
4240 ap_hook_post_config(core_post_config,NULL,NULL,APR_HOOK_REALLY_FIRST);
4241 ap_hook_translate_name(ap_core_translate,NULL,NULL,APR_HOOK_REALLY_LAST);
4242 ap_hook_map_to_storage(core_map_to_storage,NULL,NULL,APR_HOOK_REALLY_LAST);
4243 ap_hook_open_logs(ap_open_logs,NULL,NULL,APR_HOOK_REALLY_FIRST);
4244 ap_hook_handler(default_handler,NULL,NULL,APR_HOOK_REALLY_LAST);
4245 /* FIXME: I suspect we can eliminate the need for these do_nothings - Ben */
4246 ap_hook_type_checker(do_nothing,NULL,NULL,APR_HOOK_REALLY_LAST);
4247 ap_hook_fixups(core_override_type,NULL,NULL,APR_HOOK_REALLY_FIRST);
4248 ap_hook_access_checker(do_nothing,NULL,NULL,APR_HOOK_REALLY_LAST);
4249 ap_hook_create_request(core_create_req, NULL, NULL, APR_HOOK_MIDDLE);
4250 APR_OPTIONAL_HOOK(proxy, create_req, core_create_proxy_req, NULL, NULL,
4252 ap_hook_pre_mpm(ap_create_scoreboard, NULL, NULL, APR_HOOK_MIDDLE);
4254 /* register the core's insert_filter hook and register core-provided
4257 ap_hook_insert_filter(core_insert_filter, NULL, NULL, APR_HOOK_MIDDLE);
4259 ap_core_input_filter_handle =
4260 ap_register_input_filter("CORE_IN", core_input_filter,
4261 NULL, AP_FTYPE_NETWORK);
4262 ap_net_time_filter_handle =
4263 ap_register_input_filter("NET_TIME", net_time_filter,
4264 NULL, AP_FTYPE_PROTOCOL);
4265 ap_content_length_filter_handle =
4266 ap_register_output_filter("CONTENT_LENGTH", ap_content_length_filter,
4267 NULL, AP_FTYPE_PROTOCOL);
4268 ap_core_output_filter_handle =
4269 ap_register_output_filter("CORE", core_output_filter,
4270 NULL, AP_FTYPE_NETWORK);
4271 ap_subreq_core_filter_handle =
4272 ap_register_output_filter("SUBREQ_CORE", ap_sub_req_output_filter,
4273 NULL, AP_FTYPE_CONTENT_SET);
4275 ap_register_output_filter("OLD_WRITE", ap_old_write_filter,
4276 NULL, AP_FTYPE_RESOURCE - 10);
4279 AP_DECLARE_DATA module core_module = {
4280 STANDARD20_MODULE_STUFF,
4281 create_core_dir_config, /* create per-directory config structure */
4282 merge_core_dir_configs, /* merge per-directory config structures */
4283 create_core_server_config, /* create per-server config structure */
4284 merge_core_server_configs, /* merge per-server config structures */
4285 core_cmds, /* command apr_table_t */
4286 register_hooks /* register hooks */