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 * http_request.c: functions to get and process requests
22 * Thoroughly revamped by rst for Apache. NB this file reads
23 * best from the bottom up.
27 #include "apr_strings.h"
28 #include "apr_file_io.h"
29 #include "apr_fnmatch.h"
31 #define APR_WANT_STRFUNC
35 #include "ap_config.h"
37 #include "http_config.h"
38 #include "http_request.h"
39 #include "http_core.h"
40 #include "http_protocol.h"
42 #include "http_main.h"
43 #include "util_filter.h"
44 #include "util_charset.h"
45 #include "scoreboard.h"
53 /*****************************************************************
55 * Mainline request processing...
58 /* XXX A cleaner and faster way to do this might be to pass the request_rec
59 * down the filter chain as a parameter. It would need to change for
60 * subrequest vs. main request filters; perhaps the subrequest filter could
63 static void update_r_in_filters(ap_filter_t *f,
75 AP_DECLARE(void) ap_die(int type, request_rec *r)
77 int error_index = ap_index_of_response(type);
78 char *custom_response = ap_response_code_string(r, error_index);
79 int recursive_error = 0;
80 request_rec *r_1st_err = r;
82 if (type == AP_FILTER_ERROR) {
87 ap_finalize_request_protocol(r);
92 * The following takes care of Apache redirects to custom response URLs
93 * Note that if we are already dealing with the response to some other
94 * error condition, we just report on the original error, and give up on
95 * any attempt to handle the other thing "intelligently"...
97 if (r->status != HTTP_OK) {
98 recursive_error = type;
100 while (r_1st_err->prev && (r_1st_err->prev->status != HTTP_OK))
101 r_1st_err = r_1st_err->prev; /* Get back to original error */
103 if (r_1st_err != r) {
104 /* The recursive error was caused by an ErrorDocument specifying
105 * an internal redirect to a bad URI. ap_internal_redirect has
106 * changed the filter chains to point to the ErrorDocument's
107 * request_rec. Back out those changes so we can safely use the
108 * original failing request_rec to send the canned error message.
110 * ap_send_error_response gets rid of existing resource filters
111 * on the output side, so we can skip those.
113 update_r_in_filters(r_1st_err->proto_output_filters, r, r_1st_err);
114 update_r_in_filters(r_1st_err->input_filters, r, r_1st_err);
117 custom_response = NULL; /* Do NOT retry the custom thing! */
123 * This test is done here so that none of the auth modules needs to know
124 * about proxy authentication. They treat it like normal auth, and then
125 * we tweak the status.
127 if (HTTP_UNAUTHORIZED == r->status && PROXYREQ_PROXY == r->proxyreq) {
128 r->status = HTTP_PROXY_AUTHENTICATION_REQUIRED;
131 /* If we don't want to keep the connection, make sure we mark that the
132 * connection is not eligible for keepalive. If we want to keep the
133 * connection, be sure that the request body (if any) has been read.
135 if (ap_status_drops_connection(r->status)) {
136 r->connection->keepalive = AP_CONN_CLOSE;
140 * Two types of custom redirects --- plain text, and URLs. Plain text has
141 * a leading '"', so the URL code, here, is triggered on its absence
144 if (custom_response && custom_response[0] != '"') {
146 if (ap_is_url(custom_response)) {
148 * The URL isn't local, so lets drop through the rest of this
149 * apache code, and continue with the usual REDIRECT handler.
150 * But note that the client will ultimately see the wrong
153 r->status = HTTP_MOVED_TEMPORARILY;
154 apr_table_setn(r->headers_out, "Location", custom_response);
156 else if (custom_response[0] == '/') {
157 const char *error_notes;
158 r->no_local_copy = 1; /* Do NOT send HTTP_NOT_MODIFIED for
159 * error documents! */
161 * This redirect needs to be a GET no matter what the original
164 apr_table_setn(r->subprocess_env, "REQUEST_METHOD", r->method);
167 * Provide a special method for modules to communicate
168 * more informative (than the plain canned) messages to us.
169 * Propagate them to ErrorDocuments via the ERROR_NOTES variable:
171 if ((error_notes = apr_table_get(r->notes,
172 "error-notes")) != NULL) {
173 apr_table_setn(r->subprocess_env, "ERROR_NOTES", error_notes);
175 r->method = apr_pstrdup(r->pool, "GET");
176 r->method_number = M_GET;
177 ap_internal_redirect(custom_response, r);
182 * Dumb user has given us a bad url to redirect to --- fake up
183 * dying with a recursive server error...
185 recursive_error = HTTP_INTERNAL_SERVER_ERROR;
186 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
187 "Invalid error redirection directive: %s",
191 ap_send_error_response(r_1st_err, recursive_error);
194 static void check_pipeline(conn_rec *c)
196 if (c->keepalive != AP_CONN_CLOSE) {
198 apr_bucket_brigade *bb = apr_brigade_create(c->pool, c->bucket_alloc);
200 rv = ap_get_brigade(c->input_filters, bb, AP_MODE_SPECULATIVE,
201 APR_NONBLOCK_READ, 1);
202 if (rv != APR_SUCCESS || APR_BRIGADE_EMPTY(bb)) {
204 * Error or empty brigade: There is no data present in the input
207 c->data_in_input_filters = 0;
210 c->data_in_input_filters = 1;
212 apr_brigade_destroy(bb);
217 void ap_process_async_request(request_rec *r)
220 apr_bucket_brigade *bb;
222 conn_rec *c = r->connection;
224 /* Give quick handlers a shot at serving the request on the fast
225 * path, bypassing all of the other Apache hooks.
227 * This hook was added to enable serving files out of a URI keyed
228 * content cache ( e.g., Mike Abbott's Quick Shortcut Cache,
229 * described here: http://oss.sgi.com/projects/apache/mod_qsc.html )
231 * It may have other uses as well, such as routing requests directly to
232 * content handlers that have the ability to grok HTTP and do their
233 * own access checking, etc (e.g. servlet engines).
235 * Use this hook with extreme care and only if you know what you are
238 if (ap_extended_status)
239 ap_time_process_request(r->connection->sbh, START_PREQUEST);
240 access_status = ap_run_quick_handler(r, 0); /* Not a look-up request */
241 if (access_status == DECLINED) {
242 access_status = ap_process_request_internal(r);
243 if (access_status == OK) {
244 access_status = ap_invoke_handler(r);
248 if (access_status == DONE) {
249 /* e.g., something not in storage like TRACE */
253 if (access_status == OK) {
254 ap_finalize_request_protocol(r);
258 ap_die(access_status, r);
261 /* Send an EOR bucket through the output filter chain. When
262 * this bucket is destroyed, the request will be logged and
263 * its pool will be freed
265 bb = apr_brigade_create(r->connection->pool, r->connection->bucket_alloc);
266 b = ap_bucket_eor_create(r->connection->bucket_alloc, r);
267 APR_BRIGADE_INSERT_HEAD(bb, b);
268 ap_pass_brigade(r->connection->output_filters, bb);
270 /* From here onward, it is no longer safe to reference r
271 * or r->pool, because r->pool may have been destroyed
272 * already by the EOR bucket's cleanup function.
275 c->cs->state = CONN_STATE_WRITE_COMPLETION;
277 if (ap_extended_status)
278 ap_time_process_request(c->sbh, STOP_PREQUEST);
281 void ap_process_request(request_rec *r)
283 apr_bucket_brigade *bb;
285 conn_rec *c = r->connection;
288 ap_process_async_request(r);
290 if (!c->data_in_input_filters) {
291 bb = apr_brigade_create(c->pool, c->bucket_alloc);
292 b = apr_bucket_flush_create(c->bucket_alloc);
293 APR_BRIGADE_INSERT_HEAD(bb, b);
294 rv = ap_pass_brigade(c->output_filters, bb);
295 if (APR_STATUS_IS_TIMEUP(rv)) {
297 * Notice a timeout as an error message. This might be
298 * valuable for detecting clients with broken network
299 * connections or possible DoS attacks.
301 * It is still save to use r / r->pool here as the eor bucket
302 * could not have been destroyed in the event of a timeout.
304 ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
305 "Timeout while writing data for URI %s to the"
306 " client", r->unparsed_uri);
309 if (ap_extended_status) {
310 ap_time_process_request(c->sbh, STOP_PREQUEST);
314 static apr_table_t *rename_original_env(apr_pool_t *p, apr_table_t *t)
316 const apr_array_header_t *env_arr = apr_table_elts(t);
317 const apr_table_entry_t *elts = (const apr_table_entry_t *) env_arr->elts;
318 apr_table_t *new = apr_table_make(p, env_arr->nalloc);
321 for (i = 0; i < env_arr->nelts; ++i) {
324 apr_table_setn(new, apr_pstrcat(p, "REDIRECT_", elts[i].key, NULL),
331 static request_rec *internal_internal_redirect(const char *new_uri,
336 if (ap_is_recursion_limit_exceeded(r)) {
337 ap_die(HTTP_INTERNAL_SERVER_ERROR, r);
341 new = (request_rec *) apr_pcalloc(r->pool, sizeof(request_rec));
343 new->connection = r->connection;
344 new->server = r->server;
348 * A whole lot of this really ought to be shared with http_protocol.c...
349 * another missing cleanup. It's particularly inappropriate to be
350 * setting header_only, etc., here.
353 new->method = r->method;
354 new->method_number = r->method_number;
355 new->allowed_methods = ap_make_method_list(new->pool, 2);
356 ap_parse_uri(new, new_uri);
358 new->request_config = ap_create_request_config(r->pool);
360 new->per_dir_config = r->server->lookup_defaults;
365 /* Must have prev and next pointers set before calling create_request
368 ap_run_create_request(new);
370 /* Inherit the rest of the protocol info... */
372 new->the_request = r->the_request;
374 new->allowed = r->allowed;
376 new->status = r->status;
377 new->assbackwards = r->assbackwards;
378 new->header_only = r->header_only;
379 new->protocol = r->protocol;
380 new->proto_num = r->proto_num;
381 new->hostname = r->hostname;
382 new->request_time = r->request_time;
385 new->headers_in = r->headers_in;
386 new->headers_out = apr_table_make(r->pool, 12);
387 new->err_headers_out = r->err_headers_out;
388 new->subprocess_env = rename_original_env(r->pool, r->subprocess_env);
389 new->notes = apr_table_make(r->pool, 5);
390 new->allowed_methods = ap_make_method_list(new->pool, 2);
392 new->htaccess = r->htaccess;
393 new->no_cache = r->no_cache;
394 new->expecting_100 = r->expecting_100;
395 new->no_local_copy = r->no_local_copy;
396 new->read_length = r->read_length; /* We can only read it once */
397 new->vlist_validator = r->vlist_validator;
399 new->proto_output_filters = r->proto_output_filters;
400 new->proto_input_filters = r->proto_input_filters;
402 new->output_filters = new->proto_output_filters;
403 new->input_filters = new->proto_input_filters;
406 /* Add back the subrequest filter, which we lost when
407 * we set output_filters to include only the protocol
408 * output filters from the original request.
410 ap_add_output_filter_handle(ap_subreq_core_filter_handle,
411 NULL, new, new->connection);
414 update_r_in_filters(new->input_filters, r, new);
415 update_r_in_filters(new->output_filters, r, new);
417 apr_table_setn(new->subprocess_env, "REDIRECT_STATUS",
418 apr_itoa(r->pool, r->status));
421 * XXX: hmm. This is because mod_setenvif and mod_unique_id really need
422 * to do their thing on internal redirects as well. Perhaps this is a
425 if ((access_status = ap_run_post_read_request(new))) {
426 ap_die(access_status, new);
433 /* XXX: Is this function is so bogus and fragile that we deep-6 it? */
434 AP_DECLARE(void) ap_internal_fast_redirect(request_rec *rr, request_rec *r)
436 /* We need to tell POOL_DEBUG that we're guaranteeing that rr->pool
437 * will exist as long as r->pool. Otherwise we run into troubles because
438 * some values in this request will be allocated in r->pool, and others in
441 apr_pool_join(r->pool, rr->pool);
442 r->proxyreq = rr->proxyreq;
443 r->no_cache = (r->no_cache && rr->no_cache);
444 r->no_local_copy = (r->no_local_copy && rr->no_local_copy);
445 r->mtime = rr->mtime;
447 r->filename = rr->filename;
448 r->canonical_filename = rr->canonical_filename;
449 r->path_info = rr->path_info;
451 r->finfo = rr->finfo;
452 r->handler = rr->handler;
453 ap_set_content_type(r, rr->content_type);
454 r->content_encoding = rr->content_encoding;
455 r->content_languages = rr->content_languages;
456 r->per_dir_config = rr->per_dir_config;
457 /* copy output headers from subrequest, but leave negotiation headers */
458 r->notes = apr_table_overlay(r->pool, rr->notes, r->notes);
459 r->headers_out = apr_table_overlay(r->pool, rr->headers_out,
461 r->err_headers_out = apr_table_overlay(r->pool, rr->err_headers_out,
463 r->subprocess_env = apr_table_overlay(r->pool, rr->subprocess_env,
466 r->output_filters = rr->output_filters;
467 r->input_filters = rr->input_filters;
470 ap_add_output_filter_handle(ap_subreq_core_filter_handle,
471 NULL, r, r->connection);
473 else if (r->output_filters->frec == ap_subreq_core_filter_handle) {
474 ap_remove_output_filter(r->output_filters);
475 r->output_filters = r->output_filters->next;
478 /* If any filters pointed at the now-defunct rr, we must point them
479 * at our "new" instance of r. In particular, some of rr's structures
480 * will now be bogus (say rr->headers_out). If a filter tried to modify
481 * their f->r structure when it is pointing to rr, the real request_rec
482 * will not get updated. Fix that here.
484 update_r_in_filters(r->input_filters, rr, r);
485 update_r_in_filters(r->output_filters, rr, r);
488 AP_DECLARE(void) ap_internal_redirect(const char *new_uri, request_rec *r)
490 request_rec *new = internal_internal_redirect(new_uri, r);
493 /* ap_die was already called, if an error occured */
498 access_status = ap_run_quick_handler(new, 0); /* Not a look-up request */
499 if (access_status == DECLINED) {
500 access_status = ap_process_request_internal(new);
501 if (access_status == OK) {
502 access_status = ap_invoke_handler(new);
505 if (access_status == OK) {
506 ap_finalize_request_protocol(new);
509 ap_die(access_status, new);
513 /* This function is designed for things like actions or CGI scripts, when
514 * using AddHandler, and you want to preserve the content type across
515 * an internal redirect.
517 AP_DECLARE(void) ap_internal_redirect_handler(const char *new_uri, request_rec *r)
520 request_rec *new = internal_internal_redirect(new_uri, r);
522 /* ap_die was already called, if an error occured */
528 ap_set_content_type(new, r->content_type);
529 access_status = ap_process_request_internal(new);
530 if (access_status == OK) {
531 if ((access_status = ap_invoke_handler(new)) != 0) {
532 ap_die(access_status, new);
535 ap_finalize_request_protocol(new);
538 ap_die(access_status, new);
542 AP_DECLARE(void) ap_allow_methods(request_rec *r, int reset, ...)
548 * Get rid of any current settings if requested; not just the
549 * well-known methods but any extensions as well.
552 ap_clear_method_list(r->allowed_methods);
555 va_start(methods, reset);
556 while ((method = va_arg(methods, const char *)) != NULL) {
557 ap_method_list_add(r->allowed_methods, method);
562 AP_DECLARE(void) ap_allow_standard_methods(request_rec *r, int reset, ...)
569 * Get rid of any current settings if requested; not just the
570 * well-known methods but any extensions as well.
573 ap_clear_method_list(r->allowed_methods);
577 va_start(methods, reset);
578 while ((method = va_arg(methods, int)) != -1) {
579 mask |= (AP_METHOD_BIT << method);
583 r->allowed_methods->method_mask |= mask;