1 /* Licensed to the Apache Software Foundation (ASF) under one or more
2 * contributor license agreements. See the NOTICE file distributed with
3 * this work for additional information regarding copyright ownership.
4 * The ASF licenses this file to You under the Apache License, Version 2.0
5 * (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 * @file http_request.h
19 * @brief Apache Request library
21 * @defgroup APACHE_CORE_REQ Apache Request Processing
22 * @ingroup APACHE_CORE
27 * request.c is the code which handles the main line of request
28 * processing, once a request has been read in (finding the right per-
29 * directory configuration, building it if necessary, and calling all
30 * the module dispatch functions in the right order).
32 * The pieces here which are public to the modules, allow them to learn
33 * how the server would handle some other file or URI, or perhaps even
34 * direct the server to serve that other file instead of the one the
35 * client requested directly.
37 * There are two ways to do that. The first is the sub_request mechanism,
38 * which handles looking up files and URIs as adjuncts to some other
39 * request (e.g., directory entries for multiviews and directory listings);
40 * the lookup functions stop short of actually running the request, but
41 * (e.g., for includes), a module may call for the request to be run
42 * by calling run_sub_req. The space allocated to create sub_reqs can be
43 * reclaimed by calling destroy_sub_req --- be sure to copy anything you care
44 * about which was allocated in its apr_pool_t elsewhere before doing this.
47 #ifndef APACHE_HTTP_REQUEST_H
48 #define APACHE_HTTP_REQUEST_H
50 #include "apr_optional.h"
51 #include "util_filter.h"
57 #define AP_SUBREQ_NO_ARGS 0
58 #define AP_SUBREQ_MERGE_ARGS 1
61 * An internal handler used by the ap_process_request, all subrequest mechanisms
62 * and the redirect mechanism.
63 * @param r The request, subrequest or internal redirect to pre-process
64 * @return The return code for the request
66 AP_DECLARE(int) ap_process_request_internal(request_rec *r);
69 * Create a subrequest from the given URI. This subrequest can be
70 * inspected to find information about the requested URI
71 * @param new_uri The URI to lookup
72 * @param r The current request
73 * @param next_filter The first filter the sub_request should use. If this is
74 * NULL, it defaults to the first filter for the main request
75 * @return The new request record
77 AP_DECLARE(request_rec *) ap_sub_req_lookup_uri(const char *new_uri,
79 ap_filter_t *next_filter);
82 * Create a subrequest for the given file. This subrequest can be
83 * inspected to find information about the requested file
84 * @param new_file The file to lookup
85 * @param r The current request
86 * @param next_filter The first filter the sub_request should use. If this is
87 * NULL, it defaults to the first filter for the main request
88 * @return The new request record
90 AP_DECLARE(request_rec *) ap_sub_req_lookup_file(const char *new_file,
92 ap_filter_t *next_filter);
94 * Create a subrequest for the given apr_dir_read result. This subrequest
95 * can be inspected to find information about the requested file
96 * @param finfo The apr_dir_read result to lookup
97 * @param r The current request
98 * @param subtype What type of subrequest to perform, one of;
100 * AP_SUBREQ_NO_ARGS ignore r->args and r->path_info
101 * AP_SUBREQ_MERGE_ARGS merge r->args and r->path_info
103 * @param next_filter The first filter the sub_request should use. If this is
104 * NULL, it defaults to the first filter for the main request
105 * @return The new request record
106 * @note The apr_dir_read flags value APR_FINFO_MIN|APR_FINFO_NAME flag is the
107 * minimum recommended query if the results will be passed to apr_dir_read.
108 * The file info passed must include the name, and must have the same relative
109 * directory as the current request.
111 AP_DECLARE(request_rec *) ap_sub_req_lookup_dirent(const apr_finfo_t *finfo,
112 const request_rec *r,
114 ap_filter_t *next_filter);
116 * Create a subrequest for the given URI using a specific method. This
117 * subrequest can be inspected to find information about the requested URI
118 * @param method The method to use in the new subrequest
119 * @param new_uri The URI to lookup
120 * @param r The current request
121 * @param next_filter The first filter the sub_request should use. If this is
122 * NULL, it defaults to the first filter for the main request
123 * @return The new request record
125 AP_DECLARE(request_rec *) ap_sub_req_method_uri(const char *method,
127 const request_rec *r,
128 ap_filter_t *next_filter);
130 * An output filter to strip EOS buckets from sub-requests. This always
131 * has to be inserted at the end of a sub-requests filter stack.
132 * @param f The current filter
133 * @param bb The brigade to filter
134 * @return status code
136 AP_CORE_DECLARE_NONSTD(apr_status_t) ap_sub_req_output_filter(ap_filter_t *f,
137 apr_bucket_brigade *bb);
140 * Run the handler for the subrequest
141 * @param r The subrequest to run
142 * @return The return code for the subrequest
144 AP_DECLARE(int) ap_run_sub_req(request_rec *r);
147 * Free the memory associated with a subrequest
148 * @param r The subrequest to finish
150 AP_DECLARE(void) ap_destroy_sub_req(request_rec *r);
153 * An output filter to ensure that we avoid passing morphing buckets to
154 * connection filters and in so doing defeat async write completion when
155 * they are set aside. This should be inserted at the end of a request
157 * @param f The current filter
158 * @param bb The brigade to filter
159 * @return status code
161 AP_CORE_DECLARE_NONSTD(apr_status_t) ap_request_core_filter(ap_filter_t *f,
162 apr_bucket_brigade *bb);
165 * Then there's the case that you want some other request to be served
166 * as the top-level request INSTEAD of what the client requested directly.
167 * If so, call this from a handler, and then immediately return OK.
171 * Redirect the current request to some other uri
172 * @param new_uri The URI to replace the current request with
173 * @param r The current request
175 AP_DECLARE(void) ap_internal_redirect(const char *new_uri, request_rec *r);
178 * This function is designed for things like actions or CGI scripts, when
179 * using AddHandler, and you want to preserve the content type across
180 * an internal redirect.
181 * @param new_uri The URI to replace the current request with.
182 * @param r The current request
184 AP_DECLARE(void) ap_internal_redirect_handler(const char *new_uri, request_rec *r);
187 * Redirect the current request to a sub_req, merging the pools
188 * @param sub_req A subrequest created from this request
189 * @param r The current request
190 * @note the sub_req's pool will be merged into r's pool, be very careful
191 * not to destroy this subrequest, it will be destroyed with the main request!
193 AP_DECLARE(void) ap_internal_fast_redirect(request_rec *sub_req, request_rec *r);
196 * Can be used within any handler to determine if any authentication
197 * is required for the current request
198 * @param r The current request
199 * @return 1 if authentication is required, 0 otherwise
200 * @bug Behavior changed in 2.4.x refactoring, API no longer usable
201 * @deprecated @see ap_some_authn_required()
204 AP_DECLARE(int) ap_some_auth_required(request_rec *r);
207 * @defgroup APACHE_CORE_REQ_AUTH Access Control for Sub-Requests and
209 * @ingroup APACHE_CORE_REQ
213 #define AP_AUTH_INTERNAL_PER_URI 0 /**< Run access control hooks on all
214 internal requests with URIs
215 distinct from that of initial
217 #define AP_AUTH_INTERNAL_PER_CONF 1 /**< Run access control hooks only on
218 internal requests with
219 configurations distinct from
220 that of initial request */
221 #define AP_AUTH_INTERNAL_MASK 0x000F /**< mask to extract internal request
225 * Clear flag which determines when access control hooks will be run for
228 AP_DECLARE(void) ap_clear_auth_internal(void);
231 * Determine whether access control hooks will be run for all internal
232 * requests with URIs distinct from that of the initial request, or only
233 * those for which different configurations apply than those which applied
234 * to the initial request. To accommodate legacy external modules which
235 * may expect access control hooks to be run for all internal requests
236 * with distinct URIs, this is the default behaviour unless all access
237 * control hooks and authentication and authorization providers are
238 * registered with AP_AUTH_INTERNAL_PER_CONF.
239 * @param ptemp Pool used for temporary allocations
241 AP_DECLARE(void) ap_setup_auth_internal(apr_pool_t *ptemp);
244 * Register an authentication or authorization provider with the global
246 * @param pool The pool to create any storage from
247 * @param provider_group The group to store the provider in
248 * @param provider_name The name for this provider
249 * @param provider_version The version for this provider
250 * @param provider Opaque structure for this provider
251 * @param type Internal request processing mode, either
252 * AP_AUTH_INTERNAL_PER_URI or AP_AUTH_INTERNAL_PER_CONF
253 * @return APR_SUCCESS if all went well
255 AP_DECLARE(apr_status_t) ap_register_auth_provider(apr_pool_t *pool,
256 const char *provider_group,
257 const char *provider_name,
258 const char *provider_version,
259 const void *provider,
264 /* Optional functions coming from mod_authn_core and mod_authz_core
265 * that list all registered authn/z providers.
267 APR_DECLARE_OPTIONAL_FN(apr_array_header_t *, authn_ap_list_provider_names,
268 (apr_pool_t *ptemp));
269 APR_DECLARE_OPTIONAL_FN(apr_array_header_t *, authz_ap_list_provider_names,
270 (apr_pool_t *ptemp));
273 * Determine if the current request is the main request or a subrequest
274 * @param r The current request
275 * @return 1 if this is the main request, 0 otherwise
277 AP_DECLARE(int) ap_is_initial_req(request_rec *r);
280 * Function to set the r->mtime field to the specified value if it's later
281 * than what's already there.
282 * @param r The current request
283 * @param dependency_mtime Time to set the mtime to
285 AP_DECLARE(void) ap_update_mtime(request_rec *r, apr_time_t dependency_mtime);
288 * Add one or more methods to the list permitted to access the resource.
289 * Usually executed by the content handler before the response header is
290 * sent, but sometimes invoked at an earlier phase if a module knows it
291 * can set the list authoritatively. Note that the methods are ADDED
292 * to any already permitted unless the reset flag is non-zero. The
293 * list is used to generate the Allow response header field when it
295 * @param r The pointer to the request identifying the resource.
296 * @param reset Boolean flag indicating whether this list should
297 * completely replace any current settings.
298 * @param ... A NULL-terminated list of strings, each identifying a
299 * method name to add.
302 AP_DECLARE(void) ap_allow_methods(request_rec *r, int reset, ...)
306 * Add one or more methods to the list permitted to access the resource.
307 * Usually executed by the content handler before the response header is
308 * sent, but sometimes invoked at an earlier phase if a module knows it
309 * can set the list authoritatively. Note that the methods are ADDED
310 * to any already permitted unless the reset flag is non-zero. The
311 * list is used to generate the Allow response header field when it
313 * @param r The pointer to the request identifying the resource.
314 * @param reset Boolean flag indicating whether this list should
315 * completely replace any current settings.
316 * @param ... A list of method identifiers, from the "M_" series
317 * defined in httpd.h, terminated with a value of -1
318 * (e.g., "M_GET, M_POST, M_OPTIONS, -1")
321 AP_DECLARE(void) ap_allow_standard_methods(request_rec *r, int reset, ...);
323 #define MERGE_ALLOW 0
324 #define REPLACE_ALLOW 1
327 * Process a top-level request from a client, and synchronously write
328 * the response to the client
329 * @param r The current request
331 AP_DECLARE(void) ap_process_request(request_rec *r);
333 /* For post-processing after a handler has finished with a request.
334 * (Commonly used after it was suspended)
336 AP_DECLARE(void) ap_process_request_after_handler(request_rec *r);
339 * Process a top-level request from a client, allowing some or all of
340 * the response to remain buffered in the core output filter for later,
341 * asynchronous write completion
342 * @param r The current request
344 void ap_process_async_request(request_rec *r);
347 * Kill the current request
348 * @param type Why the request is dieing
349 * @param r The current request
351 AP_DECLARE(void) ap_die(int type, request_rec *r);
354 * Check whether a connection is still established and has data available,
355 * optionnaly consuming blank lines ([CR]LF).
356 * @param c The current connection
357 * @param bb The brigade to filter
358 * @param max_blank_lines Max number of blank lines to consume, or zero
359 * to consider them as data (single read).
360 * @return APR_SUCCESS: connection established with data available,
361 * APR_EAGAIN: connection established and empty,
362 * APR_NOTFOUND: too much blank lines,
363 * APR_E*: connection/general error.
365 AP_DECLARE(apr_status_t) ap_check_pipeline(conn_rec *c, apr_bucket_brigade *bb,
366 unsigned int max_blank_lines);
371 * Gives modules a chance to create their request_config entry when the
372 * request is created.
373 * @param r The current request
376 AP_DECLARE_HOOK(int,create_request,(request_rec *r))
379 * This hook allow modules an opportunity to translate the URI into an
380 * actual filename. If no modules do anything special, the server's default
381 * rules will be followed.
382 * @param r The current request
383 * @return OK, DECLINED, or HTTP_...
386 AP_DECLARE_HOOK(int,translate_name,(request_rec *r))
389 * This hook allow modules to set the per_dir_config based on their own
390 * context (such as "<Proxy>" sections) and responds to contextless requests
391 * such as TRACE that need no security or filesystem mapping.
392 * based on the filesystem.
393 * @param r The current request
394 * @return DONE (or HTTP_) if this contextless request was just fulfilled
395 * (such as TRACE), OK if this is not a file, and DECLINED if this is a file.
396 * The core map_to_storage (HOOK_RUN_REALLY_LAST) will directory_walk
397 * and file_walk the r->filename.
401 AP_DECLARE_HOOK(int,map_to_storage,(request_rec *r))
404 * This hook is used to analyze the request headers, authenticate the user,
405 * and set the user information in the request record (r->user and
406 * r->ap_auth_type). This hook is only run when Apache determines that
407 * authentication/authorization is required for this resource (as determined
408 * by the 'Require' directive). It runs after the access_checker hook, and
409 * before the auth_checker hook. This hook should be registered with
410 * ap_hook_check_authn().
411 * If "Satisfy any" is in effect, this hook may be skipped.
413 * @param r The current request
414 * @return OK, DECLINED, or HTTP_...
416 * @see ap_hook_check_authn
418 AP_DECLARE_HOOK(int,check_user_id,(request_rec *r))
421 * Allows modules to perform module-specific fixing of header fields. This
422 * is invoked just before any content-handler
423 * @param r The current request
424 * @return OK, DECLINED, or HTTP_...
427 AP_DECLARE_HOOK(int,fixups,(request_rec *r))
430 * This routine is called to determine and/or set the various document type
431 * information bits, like Content-type (via r->content_type), language, et
433 * @param r the current request
434 * @return OK, DECLINED, or HTTP_...
437 AP_DECLARE_HOOK(int,type_checker,(request_rec *r))
440 * This hook is used to apply additional access control to this resource.
441 * It runs *before* a user is authenticated, so this hook is really to
442 * apply additional restrictions independent of a user. It also runs
443 * independent of 'Require' directive usage. This hook should be registered
444 * with ap_hook_check_access().
446 * @param r the current request
447 * @return OK, DECLINED, or HTTP_...
449 * @see ap_hook_check_access
451 AP_DECLARE_HOOK(int,access_checker,(request_rec *r))
454 * This hook is used to apply additional access control and/or bypass
455 * authentication for this resource. It runs *before* a user is authenticated,
456 * but after the access_checker hook.
457 * This hook should be registered with ap_hook_check_access_ex().
458 * If "Satisfy any" is in effect, this hook may be skipped.
460 * @param r the current request
461 * @return OK (allow acces), DECLINED (let later modules decide),
462 * or HTTP_... (deny access)
464 * @see ap_hook_check_access_ex
466 AP_DECLARE_HOOK(int,access_checker_ex,(request_rec *r))
469 * This hook is used to check to see if the resource being requested
470 * is available for the authenticated user (r->user and r->ap_auth_type).
471 * It runs after the access_checker and check_user_id hooks. Note that
472 * it will *only* be called if Apache determines that access control has
473 * been applied to this resource (through a 'Require' directive). This
474 * hook should be registered with ap_hook_check_authz().
475 * If "Satisfy any" is in effect, this hook may be skipped.
477 * @param r the current request
478 * @return OK, DECLINED, or HTTP_...
480 * @see ap_hook_check_authz
482 AP_DECLARE_HOOK(int,auth_checker,(request_rec *r))
485 * Register a hook function that will apply additional access control to
486 * the current request.
487 * @param pf An access_checker hook function
488 * @param aszPre A NULL-terminated array of strings that name modules whose
489 * hooks should precede this one
490 * @param aszSucc A NULL-terminated array of strings that name modules whose
491 * hooks should succeed this one
492 * @param nOrder An integer determining order before honouring aszPre and
493 * aszSucc (for example, HOOK_MIDDLE)
494 * @param type Internal request processing mode, either
495 * AP_AUTH_INTERNAL_PER_URI or AP_AUTH_INTERNAL_PER_CONF
497 AP_DECLARE(void) ap_hook_check_access(ap_HOOK_access_checker_t *pf,
498 const char * const *aszPre,
499 const char * const *aszSucc,
500 int nOrder, int type);
503 * Register a hook function that will apply additional access control
504 * and/or bypass authentication for the current request.
505 * @param pf An access_checker_ex hook function
506 * @param aszPre A NULL-terminated array of strings that name modules whose
507 * hooks should precede this one
508 * @param aszSucc A NULL-terminated array of strings that name modules whose
509 * hooks should succeed this one
510 * @param nOrder An integer determining order before honouring aszPre and
511 * aszSucc (for example, HOOK_MIDDLE)
512 * @param type Internal request processing mode, either
513 * AP_AUTH_INTERNAL_PER_URI or AP_AUTH_INTERNAL_PER_CONF
515 AP_DECLARE(void) ap_hook_check_access_ex(ap_HOOK_access_checker_ex_t *pf,
516 const char * const *aszPre,
517 const char * const *aszSucc,
518 int nOrder, int type);
522 * Register a hook function that will analyze the request headers,
523 * authenticate the user, and set the user information in the request record.
524 * @param pf A check_user_id hook function
525 * @param aszPre A NULL-terminated array of strings that name modules whose
526 * hooks should precede this one
527 * @param aszSucc A NULL-terminated array of strings that name modules whose
528 * hooks should succeed this one
529 * @param nOrder An integer determining order before honouring aszPre and
530 * aszSucc (for example, HOOK_MIDDLE)
531 * @param type Internal request processing mode, either
532 * AP_AUTH_INTERNAL_PER_URI or AP_AUTH_INTERNAL_PER_CONF
534 AP_DECLARE(void) ap_hook_check_authn(ap_HOOK_check_user_id_t *pf,
535 const char * const *aszPre,
536 const char * const *aszSucc,
537 int nOrder, int type);
540 * Register a hook function that determine if the resource being requested
541 * is available for the currently authenticated user.
542 * @param pf An auth_checker hook function
543 * @param aszPre A NULL-terminated array of strings that name modules whose
544 * hooks should precede this one
545 * @param aszSucc A NULL-terminated array of strings that name modules whose
546 * hooks should succeed this one
547 * @param nOrder An integer determining order before honouring aszPre and
548 * aszSucc (for example, HOOK_MIDDLE)
549 * @param type Internal request processing mode, either
550 * AP_AUTH_INTERNAL_PER_URI or AP_AUTH_INTERNAL_PER_CONF
552 AP_DECLARE(void) ap_hook_check_authz(ap_HOOK_auth_checker_t *pf,
553 const char * const *aszPre,
554 const char * const *aszSucc,
555 int nOrder, int type);
558 * This hook allows modules to insert filters for the current request
559 * @param r the current request
562 AP_DECLARE_HOOK(void,insert_filter,(request_rec *r))
565 * This hook allows modules to affect the request immediately after the
566 * per-directory configuration for the request has been generated.
567 * @param r The current request
568 * @return OK (allow acces), DECLINED (let later modules decide),
569 * or HTTP_... (deny access)
572 AP_DECLARE_HOOK(int,post_perdir_config,(request_rec *r))
575 * This hook allows a module to force authn to be required when
576 * processing a request.
577 * This hook should be registered with ap_hook_force_authn().
578 * @param r The current request
579 * @return OK (force authn), DECLINED (let later modules decide)
582 AP_DECLARE_HOOK(int,force_authn,(request_rec *r))
585 * This hook allows modules to handle/emulate the apr_stat() calls
586 * needed for directory walk.
587 * @param finfo where to put the stat data
588 * @param r The current request
589 * @param wanted APR_FINFO_* flags to pass to apr_stat()
590 * @return apr_status_t or AP_DECLINED (let later modules decide)
593 AP_DECLARE_HOOK(apr_status_t,dirwalk_stat,(apr_finfo_t *finfo, request_rec *r, apr_int32_t wanted))
595 AP_DECLARE(int) ap_location_walk(request_rec *r);
596 AP_DECLARE(int) ap_directory_walk(request_rec *r);
597 AP_DECLARE(int) ap_file_walk(request_rec *r);
598 AP_DECLARE(int) ap_if_walk(request_rec *r);
600 /** End Of REQUEST (EOR) bucket */
601 AP_DECLARE_DATA extern const apr_bucket_type_t ap_bucket_type_eor;
604 * Determine if a bucket is an End Of REQUEST (EOR) bucket
605 * @param e The bucket to inspect
606 * @return true or false
608 #define AP_BUCKET_IS_EOR(e) (e->type == &ap_bucket_type_eor)
611 * Make the bucket passed in an End Of REQUEST (EOR) bucket
612 * @param b The bucket to make into an EOR bucket
613 * @param r The request to destroy when this bucket is destroyed
614 * @return The new bucket, or NULL if allocation failed
616 AP_DECLARE(apr_bucket *) ap_bucket_eor_make(apr_bucket *b, request_rec *r);
619 * Create a bucket referring to an End Of REQUEST (EOR). This bucket
620 * holds a pointer to the request_rec, so that the request can be
621 * destroyed right after all of the output has been sent to the client.
623 * @param list The freelist from which this bucket should be allocated
624 * @param r The request to destroy when this bucket is destroyed
625 * @return The new bucket, or NULL if allocation failed
627 AP_DECLARE(apr_bucket *) ap_bucket_eor_create(apr_bucket_alloc_t *list,
631 * Can be used within any handler to determine if any authentication
632 * is required for the current request. Note that if used with an
633 * access_checker hook, an access_checker_ex hook or an authz provider; the
634 * caller should take steps to avoid a loop since this function is
635 * implemented by calling these hooks.
636 * @param r The current request
637 * @return TRUE if authentication is required, FALSE otherwise
639 AP_DECLARE(int) ap_some_authn_required(request_rec *r);
645 #endif /* !APACHE_HTTP_REQUEST_H */