Coverage Report

Created: 2023-06-07 06:38

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