1 /* Licensed to the Apache Software Foundation (ASF) under one or more
2 * contributor license agreements. See the NOTICE file distributed with
3 * this work for additional information regarding copyright ownership.
4 * The ASF licenses this file to You under the Apache License, Version 2.0
5 * (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 * @brief CORE HTTP Daemon
21 * @defgroup APACHE_CORE_HTTPD Core HTTP Daemon
22 * @ingroup APACHE_CORE
26 #ifndef APACHE_HTTP_CORE_H
27 #define APACHE_HTTP_CORE_H
31 #include "apr_optional.h"
32 #include "util_filter.h"
35 #include "http_config.h"
37 #if APR_HAVE_STRUCT_RLIMIT
39 #include <sys/resource.h>
47 /* ****************************************************************
49 * The most basic server code is encapsulated in a single module
50 * known as the core, which is just *barely* functional enough to
51 * serve documents, though not terribly well.
53 * Largely for NCSA back-compatibility reasons, the core needs to
54 * make pieces of its config structures available to other modules.
55 * The accessors are declared here, along with the interpretation
56 * of one of them (allow_options).
60 * @defgroup APACHE_CORE_HTTPD_ACESSORS Acessors
62 * @brief File/Directory Accessor directives
69 /** Indexes directive */
71 /** SSI is enabled without exec= permission */
72 #define OPT_INCLUDES 2
73 /** FollowSymLinks directive */
74 #define OPT_SYM_LINKS 4
75 /** ExecCGI directive */
77 /** directive unset */
79 /** SSI exec= permission is permitted, iff OPT_INCLUDES is also set */
80 #define OPT_INC_WITH_EXEC 32
81 /** SymLinksIfOwnerMatch directive */
82 #define OPT_SYM_OWNER 64
83 /** MultiViews directive */
86 #define OPT_ALL (OPT_INDEXES|OPT_INCLUDES|OPT_INC_WITH_EXEC|OPT_SYM_LINKS|OPT_EXECCGI)
90 * @defgroup get_remote_host Remote Host Resolution
91 * @ingroup APACHE_CORE_HTTPD
94 /** REMOTE_HOST returns the hostname, or NULL if the hostname
95 * lookup fails. It will force a DNS lookup according to the
96 * HostnameLookups setting.
98 #define REMOTE_HOST (0)
100 /** REMOTE_NAME returns the hostname, or the dotted quad if the
101 * hostname lookup fails. It will force a DNS lookup according
102 * to the HostnameLookups setting.
104 #define REMOTE_NAME (1)
106 /** REMOTE_NOLOOKUP is like REMOTE_NAME except that a DNS lookup is
109 #define REMOTE_NOLOOKUP (2)
111 /** REMOTE_DOUBLE_REV will always force a DNS lookup, and also force
112 * a double reverse lookup, regardless of the HostnameLookups
113 * setting. The result is the (double reverse checked) hostname,
114 * or NULL if any of the lookups fail.
116 #define REMOTE_DOUBLE_REV (3)
118 /** @} // get_remote_host */
120 /** all of the requirements must be met */
121 #define SATISFY_ALL 0
122 /** any of the requirements must be met */
123 #define SATISFY_ANY 1
124 /** There are no applicable satisfy lines */
125 #define SATISFY_NOSPEC 2
127 /** Make sure we don't write less than 8000 bytes at any one time.
129 #define AP_MIN_BYTES_TO_WRITE 8000
131 /** default maximum of internal redirects */
132 # define AP_DEFAULT_MAX_INTERNAL_REDIRECTS 10
134 /** default maximum subrequest nesting level */
135 # define AP_DEFAULT_MAX_SUBREQ_DEPTH 10
138 * Retrieve the value of Options for this request
139 * @param r The current request
140 * @return the Options bitmask
142 AP_DECLARE(int) ap_allow_options(request_rec *r);
145 * Retrieve the value of the AllowOverride for this request
146 * @param r The current request
147 * @return the overrides bitmask
149 AP_DECLARE(int) ap_allow_overrides(request_rec *r);
152 * Retrieve the document root for this server
153 * @param r The current request
154 * @warning Don't use this! If your request went through a Userdir, or
155 * something like that, it'll screw you. But it's back-compatible...
156 * @return The document root
158 AP_DECLARE(const char *) ap_document_root(request_rec *r);
161 * Lookup the remote client's DNS name or IP address
162 * @ingroup get_remote_host
163 * @param conn The current connection
164 * @param dir_config The directory config vector from the request
165 * @param type The type of lookup to perform. One of:
167 * REMOTE_HOST returns the hostname, or NULL if the hostname
168 * lookup fails. It will force a DNS lookup according to the
169 * HostnameLookups setting.
170 * REMOTE_NAME returns the hostname, or the dotted quad if the
171 * hostname lookup fails. It will force a DNS lookup according
172 * to the HostnameLookups setting.
173 * REMOTE_NOLOOKUP is like REMOTE_NAME except that a DNS lookup is
175 * REMOTE_DOUBLE_REV will always force a DNS lookup, and also force
176 * a double reverse lookup, regardless of the HostnameLookups
177 * setting. The result is the (double reverse checked)
178 * hostname, or NULL if any of the lookups fail.
180 * @param str_is_ip unless NULL is passed, this will be set to non-zero on output when an IP address
182 * @return The remote hostname
184 AP_DECLARE(const char *) ap_get_remote_host(conn_rec *conn, void *dir_config, int type, int *str_is_ip);
187 * Retrieve the login name of the remote user. Undef if it could not be
189 * @param r The current request
190 * @return The user logged in to the client machine
192 AP_DECLARE(const char *) ap_get_remote_logname(request_rec *r);
194 /* Used for constructing self-referencing URLs, and things like SERVER_PORT,
198 * build a fully qualified URL from the uri and information in the request rec
199 * @param p The pool to allocate the URL from
200 * @param uri The path to the requested file
201 * @param r The current request
202 * @return A fully qualified URL
204 AP_DECLARE(char *) ap_construct_url(apr_pool_t *p, const char *uri, request_rec *r);
207 * Get the current server name from the request
208 * @param r The current request
209 * @return the server name
211 AP_DECLARE(const char *) ap_get_server_name(request_rec *r);
214 * Get the current server name from the request for the purposes
215 * of using in a URL. If the server name is an IPv6 literal
216 * address, it will be returned in URL format (e.g., "[fe80::1]").
217 * @param r The current request
218 * @return the server name
220 AP_DECLARE(const char *) ap_get_server_name_for_url(request_rec *r);
223 * Get the current server port
224 * @param r The current request
225 * @return The server's port
227 AP_DECLARE(apr_port_t) ap_get_server_port(const request_rec *r);
230 * Return the limit on bytes in request msg body
231 * @param r The current request
232 * @return the maximum number of bytes in the request msg body
234 AP_DECLARE(apr_off_t) ap_get_limit_req_body(const request_rec *r);
237 * Return the limit on bytes in XML request msg body
238 * @param r The current request
239 * @return the maximum number of bytes in XML request msg body
241 AP_DECLARE(size_t) ap_get_limit_xml_body(const request_rec *r);
244 * Install a custom response handler for a given status
245 * @param r The current request
246 * @param status The status for which the custom response should be used
247 * @param string The custom response. This can be a static string, a file
250 AP_DECLARE(void) ap_custom_response(request_rec *r, int status, const char *string);
253 * Check if the current request is beyond the configured max. number of redirects or subrequests
254 * @param r The current request
255 * @return true (is exceeded) or false
257 AP_DECLARE(int) ap_is_recursion_limit_exceeded(const request_rec *r);
260 * Check for a definition from the server command line
261 * @param name The define to check for
262 * @return 1 if defined, 0 otherwise
264 AP_DECLARE(int) ap_exists_config_define(const char *name);
265 /* FIXME! See STATUS about how */
266 AP_DECLARE_NONSTD(int) ap_core_translate(request_rec *r);
268 /* Authentication stuff. This is one of the places where compatibility
269 * with the old config files *really* hurts; they don't discriminate at
270 * all between different authentication schemes, meaning that we need
271 * to maintain common state for all of them in the core, and make it
272 * available to the other modules through interfaces.
275 /** @see require_line */
276 typedef struct require_line require_line;
279 * @brief A structure to keep track of authorization requirements
281 struct require_line {
282 /** Where the require line is in the config file. */
283 apr_int64_t method_mask;
284 /** The complete string from the command line */
289 * Return the type of authorization required for this request
290 * @param r The current request
291 * @return The authorization required
293 AP_DECLARE(const char *) ap_auth_type(request_rec *r);
296 * Return the current Authorization realm
297 * @param r The current request
298 * @return The current authorization realm
300 AP_DECLARE(const char *) ap_auth_name(request_rec *r);
303 * How the requires lines must be met.
304 * @param r The current request
305 * @return How the requirements must be met. One of:
307 * SATISFY_ANY -- any of the requirements must be met.
308 * SATISFY_ALL -- all of the requirements must be met.
309 * SATISFY_NOSPEC -- There are no applicable satisfy lines
312 AP_DECLARE(int) ap_satisfies(request_rec *r);
315 * Core is also unlike other modules in being implemented in more than
316 * one file... so, data structures are declared here, even though most of
317 * the code that cares really is in http_core.c. Also, another accessor.
319 AP_DECLARE_DATA extern module core_module;
322 * @brief Per-request configuration
325 /** bucket brigade used by getline for look-ahead and
326 * ap_get_client_block for holding left-over request body */
327 struct apr_bucket_brigade *bb;
329 /** an array of per-request working data elements, accessed
330 * by ID using ap_get_request_note()
331 * (Use ap_register_request_note() during initialization
336 /** Custom response strings registered via ap_custom_response(),
337 * or NULL; check per-dir config if nothing found here
339 char **response_code_strings; /* from ap_custom_response(), not from
343 /** There is a script processor installed on the output filter chain,
344 * so it needs the default_handler to deliver a (script) file into
345 * the chain so it can process it. Normally, default_handler only
346 * serves files on a GET request (assuming the file is actual content),
347 * since other methods are not content-retrieval. This flag overrides
348 * that behavior, stating that the "content" is actually a script and
349 * won't actually be delivered as the response for the non-GET method.
353 /** Should addition of charset= be suppressed for this request?
355 int suppress_charset;
356 } core_request_config;
358 /* Standard entries that are guaranteed to be accessible via
359 * ap_get_request_note() for each request (additional entries
360 * can be added with ap_register_request_note())
362 #define AP_NOTE_DIRECTORY_WALK 0
363 #define AP_NOTE_LOCATION_WALK 1
364 #define AP_NOTE_FILE_WALK 2
365 #define AP_NUM_STD_NOTES 3
368 * Reserve an element in the core_request_config->notes array
369 * for some application-specific data
370 * @return An integer key that can be passed to ap_get_request_note()
371 * during request processing to access this element for the
374 AP_DECLARE(apr_size_t) ap_register_request_note(void);
377 * Retrieve a pointer to an element in the core_request_config->notes array
378 * @param r The request
379 * @param note_num A key for the element: either a value obtained from
380 * ap_register_request_note() or one of the predefined AP_NOTE_*
382 * @return NULL if the note_num is invalid, otherwise a pointer to the
383 * requested note element.
384 * @remark At the start of a request, each note element is NULL. The
385 * handle provided by ap_get_request_note() is a pointer-to-pointer
386 * so that the caller can point the element to some app-specific
387 * data structure. The caller should guarantee that any such
388 * structure will last as long as the request itself.
390 AP_DECLARE(void **) ap_get_request_note(request_rec *r, apr_size_t note_num);
393 typedef unsigned char allow_options_t;
394 typedef unsigned char overrides_t;
397 * Bits of info that go into making an ETag for a file
398 * document. Why a long? Because char historically
399 * proved too short for Options, and int can be different
400 * sizes on different platforms.
402 typedef unsigned long etag_components_t;
405 #define ETAG_NONE (1 << 0)
406 #define ETAG_MTIME (1 << 1)
407 #define ETAG_INODE (1 << 2)
408 #define ETAG_SIZE (1 << 3)
409 #define ETAG_BACKWARD (ETAG_MTIME | ETAG_INODE | ETAG_SIZE)
410 #define ETAG_ALL (ETAG_MTIME | ETAG_INODE | ETAG_SIZE)
413 * @brief Server Signature Enumeration
420 } server_signature_e;
423 * @brief Per-directory configuration
426 /** path of the directory/regex/etc. see also d_is_fnmatch/absolute below */
428 /** the number of slashes in d */
429 unsigned d_components;
431 /** If (opts & OPT_UNSET) then no absolute assignment to options has
433 * invariant: (opts_add & opts_remove) == 0
434 * Which said another way means that the last relative (options + or -)
435 * assignment made to each bit is recorded in exactly one of opts_add
438 allow_options_t opts;
439 allow_options_t opts_add;
440 allow_options_t opts_remove;
441 overrides_t override;
442 allow_options_t override_opts;
444 /* Custom response config. These can contain text or a URL to redirect to.
445 * if response_code_strings is NULL then there are none in the config,
446 * if it's not null then it's allocated to sizeof(char*)*RESPONSE_CODES.
447 * This lets us do quick merges in merge_core_dir_configs().
450 char **response_code_strings; /* from ErrorDocument, not from
451 * ap_custom_response() */
453 /* Hostname resolution etc */
454 #define HOSTNAME_LOOKUP_OFF 0
455 #define HOSTNAME_LOOKUP_ON 1
456 #define HOSTNAME_LOOKUP_DOUBLE 2
457 #define HOSTNAME_LOOKUP_UNSET 3
458 unsigned int hostname_lookups : 4;
460 unsigned int content_md5 : 2; /* calculate Content-MD5? */
462 #define USE_CANONICAL_NAME_OFF (0)
463 #define USE_CANONICAL_NAME_ON (1)
464 #define USE_CANONICAL_NAME_DNS (2)
465 #define USE_CANONICAL_NAME_UNSET (3)
466 unsigned use_canonical_name : 2;
468 /* since is_fnmatch(conf->d) was being called so frequently in
469 * directory_walk() and its relatives, this field was created and
470 * is set to the result of that call.
472 unsigned d_is_fnmatch : 1;
474 /* should we force a charset on any outgoing parameterless content-type?
475 * if so, which charset?
477 #define ADD_DEFAULT_CHARSET_OFF (0)
478 #define ADD_DEFAULT_CHARSET_ON (1)
479 #define ADD_DEFAULT_CHARSET_UNSET (2)
480 unsigned add_default_charset : 2;
481 const char *add_default_charset_name;
483 /* System Resource Control */
485 struct rlimit *limit_cpu;
487 #if defined (RLIMIT_DATA) || defined (RLIMIT_VMEM) || defined(RLIMIT_AS)
488 struct rlimit *limit_mem;
491 struct rlimit *limit_nproc;
493 apr_off_t limit_req_body; /* limit on bytes in request msg body */
494 long limit_xml_body; /* limit on bytes in XML request msg body */
496 /* logging options */
498 server_signature_e server_signature;
501 apr_array_header_t *sec_file;
504 const char *mime_type; /* forced with ForceType */
505 const char *handler; /* forced with SetHandler */
506 const char *output_filters; /* forced with SetOutputFilters */
507 const char *input_filters; /* forced with SetInputFilters */
508 int accept_path_info; /* forced with AcceptPathInfo */
511 * What attributes/data should be included in ETag generation?
513 etag_components_t etag_bits;
514 etag_components_t etag_add;
515 etag_components_t etag_remove;
518 * Run-time performance tuning
520 #define ENABLE_MMAP_OFF (0)
521 #define ENABLE_MMAP_ON (1)
522 #define ENABLE_MMAP_UNSET (2)
523 unsigned int enable_mmap : 2; /* whether files in this dir can be mmap'ed */
525 #define ENABLE_SENDFILE_OFF (0)
526 #define ENABLE_SENDFILE_ON (1)
527 #define ENABLE_SENDFILE_UNSET (2)
528 unsigned int enable_sendfile : 2; /* files in this dir can be sendfile'ed */
529 unsigned int allow_encoded_slashes : 1; /* URLs may contain %2f w/o being
530 * pitched indiscriminately */
532 #define USE_CANONICAL_PHYS_PORT_OFF (0)
533 #define USE_CANONICAL_PHYS_PORT_ON (1)
534 #define USE_CANONICAL_PHYS_PORT_UNSET (2)
535 unsigned use_canonical_phys_port : 2;
537 ap_parse_node_t *condition; /* Conditionally merge <If> sections */
539 /** per-dir log config */
540 struct ap_logconf *log;
543 /* macro to implement off by default behaviour */
544 #define AP_SENDFILE_ENABLED(x) \
545 ((x) == ENABLE_SENDFILE_ON ? APR_SENDFILE_ENABLED : 0)
547 /* Per-server core configuration */
553 /* Name translations --- we want the core to be able to do *something*
554 * so it's at least a minimally functional web server on its own (and
555 * can be tested that way). But let's keep it to the bare minimum:
557 const char *ap_document_root;
562 apr_array_header_t *sec_dir;
563 apr_array_header_t *sec_url;
565 /* recursion backstopper */
566 int redirect_limit; /* maximum number of internal redirects */
567 int subreq_limit; /* maximum nesting level of subrequests */
569 const char *protocol;
570 apr_table_t *accf_map;
572 /* array of ap_errorlog_format_item for error log format string */
573 apr_array_header_t *error_log_format;
575 * two arrays of arrays of ap_errorlog_format_item for additional information
576 * logged to the error log once per connection/request
578 apr_array_header_t *error_log_conn;
579 apr_array_header_t *error_log_req;
582 #define AP_TRACE_UNSET -1
583 #define AP_TRACE_DISABLE 0
584 #define AP_TRACE_ENABLE 1
585 #define AP_TRACE_EXTENDED 2
588 } core_server_config;
590 /* for AddOutputFiltersByType in core.c */
591 void ap_add_output_filters_by_type(request_rec *r);
593 /* for http_config.c */
594 void ap_core_reorder_directories(apr_pool_t *, server_rec *);
597 AP_CORE_DECLARE(void) ap_add_per_dir_conf(server_rec *s, void *dir_config);
598 AP_CORE_DECLARE(void) ap_add_per_url_conf(server_rec *s, void *url_config);
599 AP_CORE_DECLARE(void) ap_add_file_conf(core_dir_config *conf, void *url_config);
600 AP_CORE_DECLARE_NONSTD(const char *) ap_limit_section(cmd_parms *cmd, void *dummy, const char *arg);
602 /* Core filters; not exported. */
603 int ap_core_input_filter(ap_filter_t *f, apr_bucket_brigade *b,
604 ap_input_mode_t mode, apr_read_type_e block,
605 apr_off_t readbytes);
606 apr_status_t ap_core_output_filter(ap_filter_t *f, apr_bucket_brigade *b);
609 AP_DECLARE(const char*) ap_get_server_protocol(server_rec* s);
610 AP_DECLARE(void) ap_set_server_protocol(server_rec* s, const char* proto);
612 /* ----------------------------------------------------------------------
614 * Runtime status/management
630 const char *description;
632 ap_mgmt_type_e vtype;
636 /* Handles for core filters */
637 extern AP_DECLARE_DATA ap_filter_rec_t *ap_subreq_core_filter_handle;
638 extern AP_DECLARE_DATA ap_filter_rec_t *ap_core_output_filter_handle;
639 extern AP_DECLARE_DATA ap_filter_rec_t *ap_content_length_filter_handle;
640 extern AP_DECLARE_DATA ap_filter_rec_t *ap_core_input_filter_handle;
643 * This hook provdes a way for modules to provide metrics/statistics about
644 * their operational status.
646 * @param p A pool to use to create entries in the hash table
647 * @param val The name of the parameter(s) that is wanted. This is
648 * tree-structured would be in the form ('*' is all the tree,
649 * 'module.*' all of the module , 'module.foo.*', or
651 * @param ht The hash table to store the results. Keys are item names, and
652 * the values point to ap_mgmt_item_t structures.
655 AP_DECLARE_HOOK(int, get_mgmt_items,
656 (apr_pool_t *p, const char * val, apr_hash_t *ht))
658 /* ---------------------------------------------------------------------- */
660 /* ----------------------------------------------------------------------
662 * I/O logging with mod_logio
665 APR_DECLARE_OPTIONAL_FN(void, ap_logio_add_bytes_out,
666 (conn_rec *c, apr_off_t bytes));
668 APR_DECLARE_OPTIONAL_FN(void, ap_logio_add_bytes_in,
669 (conn_rec *c, apr_off_t bytes));
671 APR_DECLARE_OPTIONAL_FN(apr_off_t, ap_logio_get_last_bytes, (conn_rec *c));
673 /* ----------------------------------------------------------------------
679 * info structure passed to callback functions of errorlog handlers
681 typedef struct ap_errorlog_info {
684 const request_rec *r;
685 const request_rec *rmain;
696 * callback function prototype for a external errorlog handler
697 * @note To avoid unbounded memory usage, these functions must not allocate
698 * memory from the server, connection, or request pools. If an errorlog
699 * handler absolutely needs a pool to pass to other functions, it must create
700 * and destroy a sub-pool.
702 typedef int ap_errorlog_handler_fn_t(const ap_errorlog_info *info,
703 const char *arg, char *buf, int buflen);
706 * Register external errorlog handler
707 * @param p config pool to use
708 * @param tag the new format specifier (i.e. the letter after the %)
709 * @param handler the handler function
710 * @param flags flags (reserved, set to 0)
712 AP_DECLARE(void) ap_register_errorlog_handler(apr_pool_t *p, char *tag,
713 ap_errorlog_handler_fn_t *handler,
716 typedef struct ap_errorlog_handler {
717 ap_errorlog_handler_fn_t *func;
719 } ap_errorlog_handler;
722 ap_errorlog_handler_fn_t *func;
724 #define AP_ERRORLOG_FLAG_FIELD_SEP 1
725 #define AP_ERRORLOG_FLAG_MESSAGE 2
726 #define AP_ERRORLOG_FLAG_REQUIRED 4
727 #define AP_ERRORLOG_FLAG_NULL_AS_HYPHEN 8
729 } ap_errorlog_format_item;
731 AP_DECLARE(void) ap_register_builtin_errorlog_handlers(apr_pool_t *p);
733 /* ----------------------------------------------------------------------
735 * ident lookups with mod_ident
738 APR_DECLARE_OPTIONAL_FN(const char *, ap_ident_lookup,
741 /* ----------------------------------------------------------------------
743 * authorization values with mod_authz_core
746 APR_DECLARE_OPTIONAL_FN(int, authz_some_auth_required, (request_rec *r));
747 APR_DECLARE_OPTIONAL_FN(const char *, authn_ap_auth_type, (request_rec *r));
748 APR_DECLARE_OPTIONAL_FN(const char *, authn_ap_auth_name, (request_rec *r));
750 /* ----------------------------------------------------------------------
752 * authorization values with mod_access_compat
755 APR_DECLARE_OPTIONAL_FN(int, access_compat_ap_satisfies, (request_rec *r));
757 /* ---------------------------------------------------------------------- */
763 #endif /* !APACHE_HTTP_CORE_H */