]> granicus.if.org Git - apache/blob - modules/http/http_request.c
Fix alignment in a <highlight> block.
[apache] / modules / http / http_request.c
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 /*
18  * http_request.c: functions to get and process requests
19  *
20  * Rob McCool 3/21/93
21  *
22  * Thoroughly revamped by rst for Apache.  NB this file reads
23  * best from the bottom up.
24  *
25  */
26
27 #include "apr_strings.h"
28 #include "apr_file_io.h"
29 #include "apr_fnmatch.h"
30
31 #define APR_WANT_STRFUNC
32 #include "apr_want.h"
33
34 #include "ap_config.h"
35 #include "httpd.h"
36 #include "http_config.h"
37 #include "http_request.h"
38 #include "http_core.h"
39 #include "http_protocol.h"
40 #include "http_log.h"
41 #include "http_main.h"
42 #include "mpm_common.h"
43 #include "util_filter.h"
44 #include "util_charset.h"
45 #include "scoreboard.h"
46
47 #include "mod_core.h"
48
49 #if APR_HAVE_STDARG_H
50 #include <stdarg.h>
51 #endif
52
53 APLOG_USE_MODULE(http);
54
55 /*****************************************************************
56  *
57  * Mainline request processing...
58  */
59
60 /* XXX A cleaner and faster way to do this might be to pass the request_rec
61  * down the filter chain as a parameter.  It would need to change for
62  * subrequest vs. main request filters; perhaps the subrequest filter could
63  * make the switch.
64  */
65 static void update_r_in_filters(ap_filter_t *f,
66                                 request_rec *from,
67                                 request_rec *to)
68 {
69     while (f) {
70         if (f->r == from) {
71             f->r = to;
72         }
73         f = f->next;
74     }
75 }
76
77 static void ap_die_r(int type, request_rec *r, int recursive_error)
78 {
79     char *custom_response;
80     request_rec *r_1st_err = r;
81
82     if (type == OK || type == DONE) {
83         ap_finalize_request_protocol(r);
84         return;
85     }
86
87     if (!ap_is_HTTP_VALID_RESPONSE(type)) {
88         ap_filter_t *next;
89
90         /*
91          * Check if we still have the ap_http_header_filter in place. If
92          * this is the case we should not ignore the error here because
93          * it means that we have not sent any response at all and never
94          * will. This is bad. Sent an internal server error instead.
95          */
96         next = r->output_filters;
97         while (next && (next->frec != ap_http_header_filter_handle)) {
98                next = next->next;
99         }
100
101         /*
102          * If next != NULL then we left the while above because of
103          * next->frec == ap_http_header_filter
104          */
105         if (next) {
106             if (type != AP_FILTER_ERROR) {
107                 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01579)
108                               "Invalid response status %i", type);
109             }
110             else {
111                 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(02831)
112                               "Response from AP_FILTER_ERROR");
113             }
114             type = HTTP_INTERNAL_SERVER_ERROR;
115         }
116         else {
117             return;
118         }
119     }
120
121     /*
122      * The following takes care of Apache redirects to custom response URLs
123      * Note that if we are already dealing with the response to some other
124      * error condition, we just report on the original error, and give up on
125      * any attempt to handle the other thing "intelligently"...
126      */
127     if (recursive_error != HTTP_OK) {
128         while (r_1st_err->prev && (r_1st_err->prev->status != HTTP_OK))
129             r_1st_err = r_1st_err->prev;  /* Get back to original error */
130
131         if (r_1st_err != r) {
132             /* The recursive error was caused by an ErrorDocument specifying
133              * an internal redirect to a bad URI.  ap_internal_redirect has
134              * changed the filter chains to point to the ErrorDocument's
135              * request_rec.  Back out those changes so we can safely use the
136              * original failing request_rec to send the canned error message.
137              *
138              * ap_send_error_response gets rid of existing resource filters
139              * on the output side, so we can skip those.
140              */
141             update_r_in_filters(r_1st_err->proto_output_filters, r, r_1st_err);
142             update_r_in_filters(r_1st_err->input_filters, r, r_1st_err);
143         }
144
145         custom_response = NULL; /* Do NOT retry the custom thing! */
146     }
147     else {
148         int error_index = ap_index_of_response(type);
149         custom_response = ap_response_code_string(r, error_index);
150         recursive_error = 0;
151     }
152
153     r->status = type;
154
155     /*
156      * This test is done here so that none of the auth modules needs to know
157      * about proxy authentication.  They treat it like normal auth, and then
158      * we tweak the status.
159      */
160     if (HTTP_UNAUTHORIZED == r->status && PROXYREQ_PROXY == r->proxyreq) {
161         r->status = HTTP_PROXY_AUTHENTICATION_REQUIRED;
162     }
163
164     /* If we don't want to keep the connection, make sure we mark that the
165      * connection is not eligible for keepalive.  If we want to keep the
166      * connection, be sure that the request body (if any) has been read.
167      */
168     if (ap_status_drops_connection(r->status)) {
169         r->connection->keepalive = AP_CONN_CLOSE;
170     }
171
172     /*
173      * Two types of custom redirects --- plain text, and URLs. Plain text has
174      * a leading '"', so the URL code, here, is triggered on its absence
175      */
176
177     if (custom_response && custom_response[0] != '"') {
178
179         if (ap_is_url(custom_response)) {
180             /*
181              * The URL isn't local, so lets drop through the rest of this
182              * apache code, and continue with the usual REDIRECT handler.
183              * But note that the client will ultimately see the wrong
184              * status...
185              */
186             r->status = HTTP_MOVED_TEMPORARILY;
187             apr_table_setn(r->headers_out, "Location", custom_response);
188         }
189         else if (custom_response[0] == '/') {
190             const char *error_notes, *original_method;
191             int original_method_number;
192             r->no_local_copy = 1;       /* Do NOT send HTTP_NOT_MODIFIED for
193                                          * error documents! */
194             /*
195              * This redirect needs to be a GET no matter what the original
196              * method was.
197              */
198             apr_table_setn(r->subprocess_env, "REQUEST_METHOD", r->method);
199
200             /*
201              * Provide a special method for modules to communicate
202              * more informative (than the plain canned) messages to us.
203              * Propagate them to ErrorDocuments via the ERROR_NOTES variable:
204              */
205             if ((error_notes = apr_table_get(r->notes,
206                                              "error-notes")) != NULL) {
207                 apr_table_setn(r->subprocess_env, "ERROR_NOTES", error_notes);
208             }
209             original_method = r->method;
210             original_method_number = r->method_number;
211             r->method = "GET";
212             r->method_number = M_GET;
213             ap_internal_redirect(custom_response, r);
214             /* preserve ability to see %<m in the access log */
215             r->method = original_method;
216             r->method_number = original_method_number;
217             return;
218         }
219         else {
220             /*
221              * Dumb user has given us a bad url to redirect to --- fake up
222              * dying with a recursive server error...
223              */
224             recursive_error = HTTP_INTERNAL_SERVER_ERROR;
225             ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01580)
226                         "Invalid error redirection directive: %s",
227                         custom_response);
228         }
229     }
230     ap_send_error_response(r_1st_err, recursive_error);
231 }
232
233 AP_DECLARE(void) ap_die(int type, request_rec *r)
234 {
235     ap_die_r(type, r, r->status);
236 }
237
238 AP_DECLARE(apr_status_t) ap_check_pipeline(conn_rec *c, apr_bucket_brigade *bb,
239                                            unsigned int max_blank_lines)
240 {
241     apr_status_t rv = APR_EOF;
242     ap_input_mode_t mode = AP_MODE_SPECULATIVE;
243     unsigned int num_blank_lines = 0;
244     apr_size_t cr = 0;
245     char buf[2];
246
247     while (c->keepalive != AP_CONN_CLOSE && !c->aborted) {
248         apr_size_t len = cr + 1;
249
250         apr_brigade_cleanup(bb);
251         rv = ap_get_brigade(c->input_filters, bb, mode,
252                             APR_NONBLOCK_READ, len);
253         if (rv != APR_SUCCESS || APR_BRIGADE_EMPTY(bb)) {
254             if (mode == AP_MODE_READBYTES) {
255                 /* Unexpected error, stop with this connection */
256                 ap_log_cerror(APLOG_MARK, APLOG_ERR, rv, c, APLOGNO(02967)
257                               "Can't consume pipelined empty lines");
258                 c->keepalive = AP_CONN_CLOSE;
259                 rv = APR_EGENERAL;
260             }
261             else if (rv != APR_SUCCESS && !APR_STATUS_IS_EAGAIN(rv)) {
262                 /* Pipe is dead */
263                 c->keepalive = AP_CONN_CLOSE;
264             }
265             else {
266                 /* Pipe is up and empty */
267                 rv = APR_EAGAIN;
268             }
269             break;
270         }
271         if (!max_blank_lines) {
272             apr_off_t n = 0;
273             /* Single read asked, (non-meta-)data available? */
274             rv = apr_brigade_length(bb, 0, &n);
275             if (rv == APR_SUCCESS && n <= 0) {
276                 rv = APR_EAGAIN;
277             }
278             break;
279         }
280
281         /* Lookup and consume blank lines */
282         rv = apr_brigade_flatten(bb, buf, &len);
283         if (rv != APR_SUCCESS || len != cr + 1) {
284             int log_level;
285             if (mode == AP_MODE_READBYTES) {
286                 /* Unexpected error, stop with this connection */
287                 c->keepalive = AP_CONN_CLOSE;
288                 log_level = APLOG_ERR;
289                 rv = APR_EGENERAL;
290             }
291             else {
292                 /* Let outside (non-speculative/blocking) read determine
293                  * where this possible failure comes from (metadata,
294                  * morphed EOF socket, ...). Debug only here.
295                  */
296                 log_level = APLOG_DEBUG;
297                 rv = APR_SUCCESS;
298             }
299             ap_log_cerror(APLOG_MARK, log_level, rv, c, APLOGNO(02968)
300                           "Can't check pipelined data");
301             break;
302         }
303
304         if (mode == AP_MODE_READBYTES) {
305             /* [CR]LF consumed, try next */
306             mode = AP_MODE_SPECULATIVE;
307             cr = 0;
308         }
309         else if (cr) {
310             AP_DEBUG_ASSERT(len == 2 && buf[0] == APR_ASCII_CR);
311             if (buf[1] == APR_ASCII_LF) {
312                 /* consume this CRLF */
313                 mode = AP_MODE_READBYTES;
314                 num_blank_lines++;
315             }
316             else {
317                 /* CR(?!LF) is data */
318                 break;
319             }
320         }
321         else {
322             if (buf[0] == APR_ASCII_LF) {
323                 /* consume this LF */
324                 mode = AP_MODE_READBYTES;
325                 num_blank_lines++;
326             }
327             else if (buf[0] == APR_ASCII_CR) {
328                 cr = 1;
329             }
330             else {
331                 /* Not [CR]LF, some data */
332                 break;
333             }
334         }
335         if (num_blank_lines > max_blank_lines) {
336             /* Enough blank lines with this connection,
337              * stop and don't recycle it.
338              */
339             c->keepalive = AP_CONN_CLOSE;
340             rv = APR_NOTFOUND;
341             break;
342         }
343     }
344
345     return rv;
346 }
347
348 AP_DECLARE(void) ap_process_request_after_handler(request_rec *r)
349 {
350     apr_bucket_brigade *bb;
351     apr_bucket *b;
352     conn_rec *c = r->connection;
353     ap_filter_t *f;
354
355     bb = ap_acquire_brigade(c);
356
357     /* Send an EOR bucket through the output filter chain.  When
358      * this bucket is destroyed, the request will be logged and
359      * its pool will be freed
360      */
361     b = ap_bucket_eor_create(c->bucket_alloc, r);
362     APR_BRIGADE_INSERT_HEAD(bb, b);
363
364     /* Find the last request, taking into account internal
365      * redirects. We want to send the EOR bucket at the end of
366      * all the buckets so it does not jump the queue.
367      */
368     while (r->next) {
369         r = r->next;
370     }
371
372     /* All the request filters should have bailed out on EOS, and in any
373      * case they shouldn't have to handle this EOR which will destroy the
374      * request underneath them. So go straight to the core request filter
375      * which (if any) will take care of the setaside buckets.
376      */
377     for (f = r->output_filters; f; f = f->next) {
378         if (f->frec == ap_request_core_filter_handle) {
379             break;
380         }
381     }
382     ap_pass_brigade(f ? f : c->output_filters, bb);
383
384     /* The EOR bucket has either been handled by an output filter (eg.
385      * deleted or moved to a buffered_bb => no more in bb), or an error
386      * occurred before that (eg. c->aborted => still in bb) and we ought
387      * to destroy it now. So cleanup any remaining bucket along with
388      * the orphan request (if any).
389      */
390     apr_brigade_cleanup(bb);
391
392     /* From here onward, it is no longer safe to reference r
393      * or r->pool, because r->pool may have been destroyed
394      * already by the EOR bucket's cleanup function.
395      */
396
397     /* Check pipeline consuming blank lines, they must not be interpreted as
398      * the next pipelined request, otherwise we would block on the next read
399      * without flushing data, and hence possibly delay pending response(s)
400      * until the next/real request comes in or the keepalive timeout expires.
401      */
402     (void)ap_check_pipeline(c, bb, DEFAULT_LIMIT_BLANK_LINES);
403
404     ap_release_brigade(c, bb);
405
406     if (c->cs) {
407         if (c->aborted) {
408             c->cs->state = CONN_STATE_LINGER;
409         }
410         else {
411             /* If we have still data in the output filters here it means that
412              * the last (recent) nonblocking write was EAGAIN, so tell the MPM
413              * to not try another useless/stressful one but to go straight to
414              * POLLOUT.
415             */
416             c->cs->state = CONN_STATE_WRITE_COMPLETION;
417         }
418     }
419     AP_PROCESS_REQUEST_RETURN((uintptr_t)r, r->uri, r->status);
420     if (ap_extended_status) {
421         ap_time_process_request(c->sbh, STOP_PREQUEST);
422     }
423 }
424
425 void ap_process_async_request(request_rec *r)
426 {
427     conn_rec *c = r->connection;
428     int access_status;
429
430     /* Give quick handlers a shot at serving the request on the fast
431      * path, bypassing all of the other Apache hooks.
432      *
433      * This hook was added to enable serving files out of a URI keyed
434      * content cache ( e.g., Mike Abbott's Quick Shortcut Cache,
435      * described here: http://oss.sgi.com/projects/apache/mod_qsc.html )
436      *
437      * It may have other uses as well, such as routing requests directly to
438      * content handlers that have the ability to grok HTTP and do their
439      * own access checking, etc (e.g. servlet engines).
440      *
441      * Use this hook with extreme care and only if you know what you are
442      * doing.
443      */
444     AP_PROCESS_REQUEST_ENTRY((uintptr_t)r, r->uri);
445     if (ap_extended_status) {
446         ap_time_process_request(r->connection->sbh, START_PREQUEST);
447     }
448
449     if (APLOGrtrace4(r)) {
450         int i;
451         const apr_array_header_t *t_h = apr_table_elts(r->headers_in);
452         const apr_table_entry_t *t_elt = (apr_table_entry_t *)t_h->elts;
453         ap_log_rerror(APLOG_MARK, APLOG_TRACE4, 0, r,
454                       "Headers received from client:");
455         for (i = 0; i < t_h->nelts; i++, t_elt++) {
456             ap_log_rerror(APLOG_MARK, APLOG_TRACE4, 0, r, "  %s: %s",
457                           ap_escape_logitem(r->pool, t_elt->key),
458                           ap_escape_logitem(r->pool, t_elt->val));
459         }
460     }
461
462 #if APR_HAS_THREADS
463     apr_thread_mutex_create(&r->invoke_mtx, APR_THREAD_MUTEX_DEFAULT, r->pool);
464     apr_thread_mutex_lock(r->invoke_mtx);
465 #endif
466     access_status = ap_run_quick_handler(r, 0);  /* Not a look-up request */
467     if (access_status == DECLINED) {
468         access_status = ap_process_request_internal(r);
469         if (access_status == OK) {
470             access_status = ap_invoke_handler(r);
471         }
472     }
473
474     if (access_status == SUSPENDED) {
475         /* TODO: Should move these steps into a generic function, so modules
476          * working on a suspended request can also call _ENTRY again.
477          */
478         AP_PROCESS_REQUEST_RETURN((uintptr_t)r, r->uri, access_status);
479         if (ap_extended_status) {
480             ap_time_process_request(c->sbh, STOP_PREQUEST);
481         }
482         if (c->cs)
483             c->cs->state = CONN_STATE_SUSPENDED;
484 #if APR_HAS_THREADS
485         apr_thread_mutex_unlock(r->invoke_mtx);
486 #endif
487         return;
488     }
489 #if APR_HAS_THREADS
490     apr_thread_mutex_unlock(r->invoke_mtx);
491 #endif
492
493     ap_die_r(access_status, r, HTTP_OK);
494
495     ap_process_request_after_handler(r);
496 }
497
498 AP_DECLARE(void) ap_process_request(request_rec *r)
499 {
500     apr_bucket_brigade *bb;
501     apr_bucket *b;
502     conn_rec *c = r->connection;
503     apr_status_t rv;
504
505     ap_process_async_request(r);
506
507     if (ap_run_input_pending(c) != OK) {
508         bb = ap_acquire_brigade(c);
509         b = apr_bucket_flush_create(c->bucket_alloc);
510         APR_BRIGADE_INSERT_HEAD(bb, b);
511         rv = ap_pass_brigade(c->output_filters, bb);
512         if (APR_STATUS_IS_TIMEUP(rv)) {
513             /*
514              * Notice a timeout as an error message. This might be
515              * valuable for detecting clients with broken network
516              * connections or possible DoS attacks.
517              */
518             ap_log_cerror(APLOG_MARK, APLOG_INFO, rv, c, APLOGNO(01581)
519                           "flushing data to the client");
520         }
521         ap_release_brigade(c, bb);
522     }
523     if (ap_extended_status) {
524         ap_time_process_request(c->sbh, STOP_PREQUEST);
525     }
526 }
527
528 static apr_table_t *rename_original_env(apr_pool_t *p, apr_table_t *t)
529 {
530     const apr_array_header_t *env_arr = apr_table_elts(t);
531     const apr_table_entry_t *elts = (const apr_table_entry_t *) env_arr->elts;
532     apr_table_t *new = apr_table_make(p, env_arr->nalloc);
533     int i;
534
535     for (i = 0; i < env_arr->nelts; ++i) {
536         if (!elts[i].key)
537             continue;
538         apr_table_setn(new, apr_pstrcat(p, "REDIRECT_", elts[i].key, NULL),
539                   elts[i].val);
540     }
541
542     return new;
543 }
544
545 static request_rec *internal_internal_redirect(const char *new_uri,
546                                                request_rec *r) {
547     int access_status;
548     request_rec *new;
549     const char *vary_header;
550
551     if (ap_is_recursion_limit_exceeded(r)) {
552         ap_die(HTTP_INTERNAL_SERVER_ERROR, r);
553         return NULL;
554     }
555
556     new = (request_rec *) apr_pcalloc(r->pool, sizeof(request_rec));
557
558     new->connection = r->connection;
559     new->server     = r->server;
560     new->pool       = r->pool;
561
562     /*
563      * A whole lot of this really ought to be shared with http_protocol.c...
564      * another missing cleanup.  It's particularly inappropriate to be
565      * setting header_only, etc., here.
566      */
567
568     new->method          = r->method;
569     new->method_number   = r->method_number;
570     new->allowed_methods = ap_make_method_list(new->pool, 2);
571     ap_parse_uri(new, new_uri);
572     new->parsed_uri.port_str = r->parsed_uri.port_str;
573     new->parsed_uri.port = r->parsed_uri.port;
574
575     new->request_config = ap_create_request_config(r->pool);
576
577     new->per_dir_config = r->server->lookup_defaults;
578
579     new->prev = r;
580     r->next   = new;
581
582     new->useragent_addr = r->useragent_addr;
583     new->useragent_ip = r->useragent_ip;
584
585     /* Must have prev and next pointers set before calling create_request
586      * hook.
587      */
588     ap_run_create_request(new);
589
590     /* Inherit the rest of the protocol info... */
591
592     new->the_request = r->the_request;
593
594     new->allowed         = r->allowed;
595
596     new->status          = r->status;
597     new->assbackwards    = r->assbackwards;
598     new->header_only     = r->header_only;
599     new->protocol        = r->protocol;
600     new->proto_num       = r->proto_num;
601     new->hostname        = r->hostname;
602     new->request_time    = r->request_time;
603     new->main            = r->main;
604
605     new->headers_in      = r->headers_in;
606     new->trailers_in     = r->trailers_in;
607     new->headers_out     = apr_table_make(r->pool, 12);
608     if (ap_is_HTTP_REDIRECT(new->status)) {
609         const char *location = apr_table_get(r->headers_out, "Location");
610         if (location)
611             apr_table_setn(new->headers_out, "Location", location);
612     }
613
614     /* A module (like mod_rewrite) can force an internal redirect
615      * to carry over the Vary header (if present).
616      */
617     if (apr_table_get(r->notes, "redirect-keeps-vary")) {
618         if((vary_header = apr_table_get(r->headers_out, "Vary"))) {
619             apr_table_setn(new->headers_out, "Vary", vary_header);
620         }
621     }
622
623     new->err_headers_out = r->err_headers_out;
624     new->trailers_out    = apr_table_make(r->pool, 5);
625     new->subprocess_env  = rename_original_env(r->pool, r->subprocess_env);
626     new->notes           = apr_table_make(r->pool, 5);
627
628     new->htaccess        = r->htaccess;
629     new->no_cache        = r->no_cache;
630     new->expecting_100   = r->expecting_100;
631     new->no_local_copy   = r->no_local_copy;
632     new->read_length     = r->read_length;     /* We can only read it once */
633     new->vlist_validator = r->vlist_validator;
634
635     new->proto_output_filters  = r->proto_output_filters;
636     new->proto_input_filters   = r->proto_input_filters;
637
638     new->input_filters   = new->proto_input_filters;
639
640     if (new->main) {
641         ap_filter_t *f, *nextf;
642
643         /* If this is a subrequest, the filter chain may contain a
644          * mixture of filters specific to the old request (r), and
645          * some inherited from r->main.  Here, inherit that filter
646          * chain, and remove all those which are specific to the old
647          * request; ensuring the subreq filter is left in place. */
648         new->output_filters = r->output_filters;
649
650         f = new->output_filters;
651         do {
652             nextf = f->next;
653
654             if (f->r == r && f->frec != ap_subreq_core_filter_handle) {
655                 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(01582)
656                               "dropping filter '%s' in internal redirect from %s to %s",
657                               f->frec->name, r->unparsed_uri, new_uri);
658
659                 /* To remove the filter, first set f->r to the *new*
660                  * request_rec, so that ->output_filters on 'new' is
661                  * changed (if necessary) when removing the filter. */
662                 f->r = new;
663                 ap_remove_output_filter(f);
664             }
665
666             f = nextf;
667
668             /* Stop at the protocol filters.  If a protocol filter has
669              * been newly installed for this resource, better leave it
670              * in place, though it's probably a misconfiguration or
671              * filter bug to get into this state. */
672         } while (f && f != new->proto_output_filters);
673     }
674     else {
675         /* If this is not a subrequest, clear out all
676          * resource-specific filters. */
677         new->output_filters  = new->proto_output_filters;
678     }
679
680     update_r_in_filters(new->input_filters, r, new);
681     update_r_in_filters(new->output_filters, r, new);
682
683     apr_table_setn(new->subprocess_env, "REDIRECT_STATUS",
684                    apr_itoa(r->pool, r->status));
685
686     /* Begin by presuming any module can make its own path_info assumptions,
687      * until some module interjects and changes the value.
688      */
689     new->used_path_info = AP_REQ_DEFAULT_PATH_INFO;
690
691 #if APR_HAS_THREADS
692     new->invoke_mtx = r->invoke_mtx;
693 #endif
694
695     /*
696      * XXX: hmm.  This is because mod_setenvif and mod_unique_id really need
697      * to do their thing on internal redirects as well.  Perhaps this is a
698      * misnamed function.
699      */
700     if ((access_status = ap_run_post_read_request(new))) {
701         ap_die(access_status, new);
702         return NULL;
703     }
704
705     return new;
706 }
707
708 /* XXX: Is this function is so bogus and fragile that we deep-6 it? */
709 AP_DECLARE(void) ap_internal_fast_redirect(request_rec *rr, request_rec *r)
710 {
711     /* We need to tell POOL_DEBUG that we're guaranteeing that rr->pool
712      * will exist as long as r->pool.  Otherwise we run into troubles because
713      * some values in this request will be allocated in r->pool, and others in
714      * rr->pool.
715      */
716     apr_pool_join(r->pool, rr->pool);
717     r->proxyreq = rr->proxyreq;
718     r->no_cache = (r->no_cache && rr->no_cache);
719     r->no_local_copy = (r->no_local_copy && rr->no_local_copy);
720     r->mtime = rr->mtime;
721     r->uri = rr->uri;
722     r->filename = rr->filename;
723     r->canonical_filename = rr->canonical_filename;
724     r->path_info = rr->path_info;
725     r->args = rr->args;
726     r->finfo = rr->finfo;
727     r->handler = rr->handler;
728     ap_set_content_type(r, rr->content_type);
729     r->content_encoding = rr->content_encoding;
730     r->content_languages = rr->content_languages;
731     r->per_dir_config = rr->per_dir_config;
732     /* copy output headers from subrequest, but leave negotiation headers */
733     r->notes = apr_table_overlay(r->pool, rr->notes, r->notes);
734     r->headers_out = apr_table_overlay(r->pool, rr->headers_out,
735                                        r->headers_out);
736     r->err_headers_out = apr_table_overlay(r->pool, rr->err_headers_out,
737                                            r->err_headers_out);
738     r->trailers_out = apr_table_overlay(r->pool, rr->trailers_out,
739                                            r->trailers_out);
740     r->subprocess_env = apr_table_overlay(r->pool, rr->subprocess_env,
741                                           r->subprocess_env);
742
743     r->output_filters = rr->output_filters;
744     r->input_filters = rr->input_filters;
745
746     /* If any filters pointed at the now-defunct rr, we must point them
747      * at our "new" instance of r.  In particular, some of rr's structures
748      * will now be bogus (say rr->headers_out).  If a filter tried to modify
749      * their f->r structure when it is pointing to rr, the real request_rec
750      * will not get updated.  Fix that here.
751      */
752     update_r_in_filters(r->input_filters, rr, r);
753     update_r_in_filters(r->output_filters, rr, r);
754
755     if (r->main) {
756         ap_filter_t *next = r->output_filters;
757         while (next && (next != r->proto_output_filters)) {
758             if (next->frec == ap_subreq_core_filter_handle) {
759                 break;
760             }
761             next = next->next;
762         }
763         if (!next || next == r->proto_output_filters) {
764             ap_add_output_filter_handle(ap_subreq_core_filter_handle,
765                                         NULL, r, r->connection);
766         }
767     }
768     else {
769         /*
770          * We need to check if we now have the SUBREQ_CORE filter in our filter
771          * chain. If this is the case we need to remove it since we are NO
772          * subrequest. But we need to keep in mind that the SUBREQ_CORE filter
773          * does not necessarily need to be the first filter in our chain. So we
774          * need to go through the chain. But we only need to walk up the chain
775          * until the proto_output_filters as the SUBREQ_CORE filter is below the
776          * protocol filters.
777          */
778         ap_filter_t *next;
779
780         next = r->output_filters;
781         while (next && (next->frec != ap_subreq_core_filter_handle)
782                && (next != r->proto_output_filters)) {
783                 next = next->next;
784         }
785         if (next && (next->frec == ap_subreq_core_filter_handle)) {
786             ap_remove_output_filter(next);
787         }
788     }
789 }
790
791 AP_DECLARE(void) ap_internal_redirect(const char *new_uri, request_rec *r)
792 {
793     int access_status;
794     request_rec *new = internal_internal_redirect(new_uri, r);
795
796     AP_INTERNAL_REDIRECT(r->uri, new_uri);
797
798     /* ap_die was already called, if an error occurred */
799     if (!new) {
800         return;
801     }
802
803     access_status = ap_run_quick_handler(new, 0);  /* Not a look-up request */
804     if (access_status == DECLINED) {
805         access_status = ap_process_request_internal(new);
806         if (access_status == OK) {
807             access_status = ap_invoke_handler(new);
808         }
809     }
810     ap_die(access_status, new);
811 }
812
813 /* This function is designed for things like actions or CGI scripts, when
814  * using AddHandler, and you want to preserve the content type across
815  * an internal redirect.
816  */
817 AP_DECLARE(void) ap_internal_redirect_handler(const char *new_uri, request_rec *r)
818 {
819     int access_status;
820     request_rec *new = internal_internal_redirect(new_uri, r);
821
822     /* ap_die was already called, if an error occurred */
823     if (!new) {
824         return;
825     }
826
827     if (r->handler)
828         ap_set_content_type(new, r->content_type);
829     access_status = ap_process_request_internal(new);
830     if (access_status == OK) {
831         access_status = ap_invoke_handler(new);
832     }
833     ap_die(access_status, new);
834 }
835
836 AP_DECLARE(void) ap_allow_methods(request_rec *r, int reset, ...)
837 {
838     const char *method;
839     va_list methods;
840
841     /*
842      * Get rid of any current settings if requested; not just the
843      * well-known methods but any extensions as well.
844      */
845     if (reset) {
846         ap_clear_method_list(r->allowed_methods);
847     }
848
849     va_start(methods, reset);
850     while ((method = va_arg(methods, const char *)) != NULL) {
851         ap_method_list_add(r->allowed_methods, method);
852     }
853     va_end(methods);
854 }
855
856 AP_DECLARE(void) ap_allow_standard_methods(request_rec *r, int reset, ...)
857 {
858     int method;
859     va_list methods;
860     apr_int64_t mask;
861
862     /*
863      * Get rid of any current settings if requested; not just the
864      * well-known methods but any extensions as well.
865      */
866     if (reset) {
867         ap_clear_method_list(r->allowed_methods);
868     }
869
870     mask = 0;
871     va_start(methods, reset);
872     while ((method = va_arg(methods, int)) != -1) {
873         mask |= (AP_METHOD_BIT << method);
874     }
875     va_end(methods);
876
877     r->allowed_methods->method_mask |= mask;
878 }