View | Details | Raw Unified | Return to bug 56541
Collapse All | Expand All

(-)modules/proxy/proxy_util.c (-1 / +3 lines)
Lines 3230-3236 PROXY_DECLARE(int) ap_proxy_create_hdrbrgd(apr_poo Link Here
3230
        buf = apr_pstrcat(p, r->method, " ", url, " HTTP/1.1" CRLF, NULL);
3230
        buf = apr_pstrcat(p, r->method, " ", url, " HTTP/1.1" CRLF, NULL);
3231
    }
3231
    }
3232
    if (apr_table_get(r->subprocess_env, "proxy-nokeepalive")) {
3232
    if (apr_table_get(r->subprocess_env, "proxy-nokeepalive")) {
3233
        origin->keepalive = AP_CONN_CLOSE;
3233
        if (origin) {
3234
            origin->keepalive = AP_CONN_CLOSE;
3235
        }
3234
        p_conn->close = 1;
3236
        p_conn->close = 1;
3235
    }
3237
    }
3236
    ap_xlate_proto_to_ascii(buf, strlen(buf));
3238
    ap_xlate_proto_to_ascii(buf, strlen(buf));
(-)modules/proxy/mod_proxy_http.c (-93 / +253 lines)
Lines 234-240 static int stream_reqbody_chunked(apr_pool_t *p, Link Here
234
                                           proxy_conn_rec *p_conn,
234
                                           proxy_conn_rec *p_conn,
235
                                           conn_rec *origin,
235
                                           conn_rec *origin,
236
                                           apr_bucket_brigade *header_brigade,
236
                                           apr_bucket_brigade *header_brigade,
237
                                           apr_bucket_brigade *input_brigade)
237
                                           apr_bucket_brigade *input_brigade,
238
                                           int flushall)
238
{
239
{
239
    int seen_eos = 0, rv = OK;
240
    int seen_eos = 0, rv = OK;
240
    apr_size_t hdr_len;
241
    apr_size_t hdr_len;
Lines 247-260 static int stream_reqbody_chunked(apr_pool_t *p, Link Here
247
    add_te_chunked(p, bucket_alloc, header_brigade);
248
    add_te_chunked(p, bucket_alloc, header_brigade);
248
    terminate_headers(bucket_alloc, header_brigade);
249
    terminate_headers(bucket_alloc, header_brigade);
249
250
250
    while (!APR_BUCKET_IS_EOS(APR_BRIGADE_FIRST(input_brigade)))
251
    while (APR_BRIGADE_EMPTY(input_brigade) ||
252
            !APR_BUCKET_IS_EOS(APR_BRIGADE_FIRST(input_brigade)))
251
    {
253
    {
254
        int flush = flushall;
255
256
        if (!APR_BRIGADE_EMPTY(input_brigade)) {
252
        char chunk_hdr[20];  /* must be here due to transient bucket. */
257
        char chunk_hdr[20];  /* must be here due to transient bucket. */
253
258
254
        /* If this brigade contains EOS, either stop or remove it. */
259
        /* If this brigade contains EOS, either stop or remove it. */
255
        if (APR_BUCKET_IS_EOS(APR_BRIGADE_LAST(input_brigade))) {
260
        if (APR_BUCKET_IS_EOS(APR_BRIGADE_LAST(input_brigade))) {
256
            seen_eos = 1;
261
            seen_eos = 1;
257
262
263
            /* The request is flushed below this loop with the EOS chunk */
264
            flush = 0;
265
258
            /* We can't pass this EOS to the output_filters. */
266
            /* We can't pass this EOS to the output_filters. */
259
            e = APR_BRIGADE_LAST(input_brigade);
267
            e = APR_BRIGADE_LAST(input_brigade);
260
            apr_bucket_delete(e);
268
            apr_bucket_delete(e);
Lines 276-281 static int stream_reqbody_chunked(apr_pool_t *p, Link Here
276
         */
284
         */
277
        e = apr_bucket_immortal_create(ASCII_CRLF, 2, bucket_alloc);
285
        e = apr_bucket_immortal_create(ASCII_CRLF, 2, bucket_alloc);
278
        APR_BRIGADE_INSERT_TAIL(input_brigade, e);
286
        APR_BRIGADE_INSERT_TAIL(input_brigade, e);
287
        }
279
288
280
        if (header_brigade) {
289
        if (header_brigade) {
281
            /* we never sent the header brigade, so go ahead and
290
            /* we never sent the header brigade, so go ahead and
Lines 283-288 static int stream_reqbody_chunked(apr_pool_t *p, Link Here
283
             */
292
             */
284
            bb = header_brigade;
293
            bb = header_brigade;
285
294
295
            /* Flush the prefeched data now to minimize the delay between
296
             * connect (or ap_proxy_is_socket_connected) and the first bytes
297
             * sent, unless it is done below this loop with the EOS chunk.
298
             */
299
            flush = !seen_eos;
300
286
            /*
301
            /*
287
             * Save input_brigade in bb brigade. (At least) in the SSL case
302
             * Save input_brigade in bb brigade. (At least) in the SSL case
288
             * input_brigade contains transient buckets whose data would get
303
             * input_brigade contains transient buckets whose data would get
Lines 303-310 static int stream_reqbody_chunked(apr_pool_t *p, Link Here
303
            bb = input_brigade;
318
            bb = input_brigade;
304
        }
319
        }
305
320
306
        /* The request is flushed below this loop with chunk EOS header */
321
        rv = ap_proxy_pass_brigade(bucket_alloc, r, p_conn, origin, bb, flush);
307
        rv = ap_proxy_pass_brigade(bucket_alloc, r, p_conn, origin, bb, 0);
308
        if (rv != OK) {
322
        if (rv != OK) {
309
            return rv;
323
            return rv;
310
        }
324
        }
Lines 366-372 static int stream_reqbody_cl(apr_pool_t *p, Link Here
366
                                      conn_rec *origin,
380
                                      conn_rec *origin,
367
                                      apr_bucket_brigade *header_brigade,
381
                                      apr_bucket_brigade *header_brigade,
368
                                      apr_bucket_brigade *input_brigade,
382
                                      apr_bucket_brigade *input_brigade,
369
                                      char *old_cl_val)
383
                                      char *old_cl_val, int flushall)
370
{
384
{
371
    int seen_eos = 0, rv = 0;
385
    int seen_eos = 0, rv = 0;
372
    apr_status_t status = APR_SUCCESS;
386
    apr_status_t status = APR_SUCCESS;
Lines 392-399 static int stream_reqbody_cl(apr_pool_t *p, Link Here
392
    }
406
    }
393
    terminate_headers(bucket_alloc, header_brigade);
407
    terminate_headers(bucket_alloc, header_brigade);
394
408
395
    while (!APR_BUCKET_IS_EOS(APR_BRIGADE_FIRST(input_brigade)))
409
    while (APR_BRIGADE_EMPTY(input_brigade) ||
410
            !APR_BUCKET_IS_EOS(APR_BRIGADE_FIRST(input_brigade)))
396
    {
411
    {
412
        int flush = flushall;
413
414
        if (!APR_BRIGADE_EMPTY(input_brigade)) {
397
        apr_brigade_length(input_brigade, 1, &bytes);
415
        apr_brigade_length(input_brigade, 1, &bytes);
398
        bytes_streamed += bytes;
416
        bytes_streamed += bytes;
399
417
Lines 401-406 static int stream_reqbody_cl(apr_pool_t *p, Link Here
401
        if (APR_BUCKET_IS_EOS(APR_BRIGADE_LAST(input_brigade))) {
419
        if (APR_BUCKET_IS_EOS(APR_BRIGADE_LAST(input_brigade))) {
402
            seen_eos = 1;
420
            seen_eos = 1;
403
421
422
            /* Once we hit EOS, we are ready to flush. */
423
            flush = 1;
424
404
            /* We can't pass this EOS to the output_filters. */
425
            /* We can't pass this EOS to the output_filters. */
405
            e = APR_BRIGADE_LAST(input_brigade);
426
            e = APR_BRIGADE_LAST(input_brigade);
406
            apr_bucket_delete(e);
427
            apr_bucket_delete(e);
Lines 427-432 static int stream_reqbody_cl(apr_pool_t *p, Link Here
427
                          bytes_streamed, cl_val);
448
                          bytes_streamed, cl_val);
428
            return HTTP_INTERNAL_SERVER_ERROR;
449
            return HTTP_INTERNAL_SERVER_ERROR;
429
        }
450
        }
451
        }
430
452
431
        if (header_brigade) {
453
        if (header_brigade) {
432
            /* we never sent the header brigade, so go ahead and
454
            /* we never sent the header brigade, so go ahead and
Lines 434-439 static int stream_reqbody_cl(apr_pool_t *p, Link Here
434
             */
456
             */
435
            bb = header_brigade;
457
            bb = header_brigade;
436
458
459
            /* Flush prefeched data now to minimize the delay between connect,
460
             * or ap_proxy_is_socket_connected, and the first bytes sent.
461
             */
462
            flush = 1;
463
437
            /*
464
            /*
438
             * Save input_brigade in bb brigade. (At least) in the SSL case
465
             * Save input_brigade in bb brigade. (At least) in the SSL case
439
             * input_brigade contains transient buckets whose data would get
466
             * input_brigade contains transient buckets whose data would get
Lines 454-463 static int stream_reqbody_cl(apr_pool_t *p, Link Here
454
            bb = input_brigade;
481
            bb = input_brigade;
455
        }
482
        }
456
483
457
        /* Once we hit EOS, we are ready to flush. */
484
        rv = ap_proxy_pass_brigade(bucket_alloc, r, p_conn, origin, bb, flush);
458
        rv = ap_proxy_pass_brigade(bucket_alloc, r, p_conn, origin, bb, seen_eos);
459
        if (rv != OK) {
485
        if (rv != OK) {
460
            return rv ;
486
            return rv;
461
        }
487
        }
462
488
463
        if (seen_eos) {
489
        if (seen_eos) {
Lines 499-506 static int stream_reqbody_cl(apr_pool_t *p, Link Here
499
525
500
static int spool_reqbody_cl(apr_pool_t *p,
526
static int spool_reqbody_cl(apr_pool_t *p,
501
                                     request_rec *r,
527
                                     request_rec *r,
502
                                     proxy_conn_rec *p_conn,
503
                                     conn_rec *origin,
504
                                     apr_bucket_brigade *header_brigade,
528
                                     apr_bucket_brigade *header_brigade,
505
                                     apr_bucket_brigade *input_brigade,
529
                                     apr_bucket_brigade *input_brigade,
506
                                     int force_cl)
530
                                     int force_cl)
Lines 518-525 static int spool_reqbody_cl(apr_pool_t *p, Link Here
518
542
519
    limit = ap_get_limit_req_body(r);
543
    limit = ap_get_limit_req_body(r);
520
544
521
    while (!APR_BUCKET_IS_EOS(APR_BRIGADE_FIRST(input_brigade)))
545
    while (APR_BRIGADE_EMPTY(input_brigade) ||
546
            !APR_BUCKET_IS_EOS(APR_BRIGADE_FIRST(input_brigade)))
522
    {
547
    {
548
        if (!APR_BRIGADE_EMPTY(input_brigade)) {
523
        /* If this brigade contains EOS, either stop or remove it. */
549
        /* If this brigade contains EOS, either stop or remove it. */
524
        if (APR_BUCKET_IS_EOS(APR_BRIGADE_LAST(input_brigade))) {
550
        if (APR_BUCKET_IS_EOS(APR_BRIGADE_LAST(input_brigade))) {
525
            seen_eos = 1;
551
            seen_eos = 1;
Lines 612-617 static int spool_reqbody_cl(apr_pool_t *p, Link Here
612
        if (seen_eos) {
638
        if (seen_eos) {
613
            break;
639
            break;
614
        }
640
        }
641
        }
615
642
616
        status = ap_get_brigade(r->input_filters, input_brigade,
643
        status = ap_get_brigade(r->input_filters, input_brigade,
617
                                AP_MODE_READBYTES, APR_BLOCK_READ,
644
                                AP_MODE_READBYTES, APR_BLOCK_READ,
Lines 620-628 static int spool_reqbody_cl(apr_pool_t *p, Link Here
620
        if (status != APR_SUCCESS) {
647
        if (status != APR_SUCCESS) {
621
            conn_rec *c = r->connection;
648
            conn_rec *c = r->connection;
622
            ap_log_rerror(APLOG_MARK, APLOG_ERR, status, r, APLOGNO(02610)
649
            ap_log_rerror(APLOG_MARK, APLOG_ERR, status, r, APLOGNO(02610)
623
                          "read request body failed to %pI (%s)"
650
                          "read request body failed from %s (%s)",
624
                          " from %s (%s)", p_conn->addr,
625
                          p_conn->hostname ? p_conn->hostname: "",
626
                          c->client_ip, c->remote_host ? c->remote_host: "");
651
                          c->client_ip, c->remote_host ? c->remote_host: "");
627
            return HTTP_BAD_REQUEST;
652
            return HTTP_BAD_REQUEST;
628
        }
653
        }
Lines 640-647 static int spool_reqbody_cl(apr_pool_t *p, Link Here
640
        e = apr_bucket_immortal_create(ASCII_CRLF, 2, bucket_alloc);
665
        e = apr_bucket_immortal_create(ASCII_CRLF, 2, bucket_alloc);
641
        APR_BRIGADE_INSERT_TAIL(header_brigade, e);
666
        APR_BRIGADE_INSERT_TAIL(header_brigade, e);
642
    }
667
    }
643
    /* This is all a single brigade, pass with flush flagged */
668
    return OK;
644
    return(ap_proxy_pass_brigade(bucket_alloc, r, p_conn, origin, header_brigade, 1));
645
}
669
}
646
670
647
/*
671
/*
Lines 654-661 static int spool_reqbody_cl(apr_pool_t *p, Link Here
654
 * FLUSH
678
 * FLUSH
655
 * EOS
679
 * EOS
656
 *
680
 *
657
 * If an other bucket type is found its type is logged as a debug message
681
 * If an other bucket type is found its type is logged as an error message
658
 * and APR_EGENERAL is returned.
682
 * and APR_INCOMPLETE is returned.
659
 */
683
 */
660
static apr_status_t proxy_buckets_lifetime_transform(request_rec *r,
684
static apr_status_t proxy_buckets_lifetime_transform(request_rec *r,
661
        apr_bucket_brigade *from, apr_bucket_brigade *to)
685
        apr_bucket_brigade *from, apr_bucket_brigade *to)
Lines 664-721 static apr_status_t proxy_buckets_lifetime_transfo Link Here
664
    apr_bucket *new;
688
    apr_bucket *new;
665
    const char *data;
689
    const char *data;
666
    apr_size_t bytes;
690
    apr_size_t bytes;
667
    apr_status_t rv = APR_SUCCESS;
691
    apr_status_t status, rv = APR_SUCCESS;
692
    int to_ancestor = apr_pool_is_ancestor(to->p, from->p);
668
693
669
    apr_brigade_cleanup(to);
694
    apr_brigade_cleanup(to);
670
    for (e = APR_BRIGADE_FIRST(from);
695
    for (e = APR_BRIGADE_FIRST(from);
671
         e != APR_BRIGADE_SENTINEL(from);
696
         e != APR_BRIGADE_SENTINEL(from);
672
         e = APR_BUCKET_NEXT(e)) {
697
         e = APR_BUCKET_NEXT(e)) {
673
        if (!APR_BUCKET_IS_METADATA(e)) {
698
        if (!APR_BUCKET_IS_METADATA(e)) {
674
            apr_bucket_read(e, &data, &bytes, APR_BLOCK_READ);
699
            if (to_ancestor && e->list == to->bucket_alloc) {
675
            new = apr_bucket_transient_create(data, bytes, r->connection->bucket_alloc);
700
                status = apr_bucket_copy(e, &new);
701
                if (status != APR_SUCCESS) {
702
                    ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO()
703
                                  "Can't copy bucket of type %s in"
704
                                  " proxy_buckets_lifetime_transform",
705
                                  e->type->name);
706
                    return status;
707
                }
708
            }
709
            else {
710
                status = apr_bucket_read(e, &data, &bytes, APR_BLOCK_READ);
711
                if (status != APR_SUCCESS) {
712
                    ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO()
713
                                  "Can't read bucket of type %s in"
714
                                  " proxy_buckets_lifetime_transform",
715
                                  e->type->name);
716
                    return status;
717
                }
718
                new = apr_bucket_transient_create(data, bytes,
719
                                                  to->bucket_alloc);
720
            }
676
            APR_BRIGADE_INSERT_TAIL(to, new);
721
            APR_BRIGADE_INSERT_TAIL(to, new);
677
        }
722
        }
678
        else if (APR_BUCKET_IS_FLUSH(e)) {
723
        else if (APR_BUCKET_IS_FLUSH(e)) {
679
            new = apr_bucket_flush_create(r->connection->bucket_alloc);
724
            new = apr_bucket_flush_create(to->bucket_alloc);
680
            APR_BRIGADE_INSERT_TAIL(to, new);
725
            APR_BRIGADE_INSERT_TAIL(to, new);
681
        }
726
        }
682
        else if (APR_BUCKET_IS_EOS(e)) {
727
        else if (APR_BUCKET_IS_EOS(e)) {
683
            new = apr_bucket_eos_create(r->connection->bucket_alloc);
728
            new = apr_bucket_eos_create(to->bucket_alloc);
684
            APR_BRIGADE_INSERT_TAIL(to, new);
729
            APR_BRIGADE_INSERT_TAIL(to, new);
685
        }
730
        }
686
        else {
731
        else {
687
            ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(00964)
732
            ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(00964)
688
                          "Unhandled bucket type of type %s in"
733
                          "Unhandled bucket of type %s in"
689
                          " proxy_buckets_lifetime_transform", e->type->name);
734
                          " proxy_buckets_lifetime_transform",
735
                          e->type->name);
690
            apr_bucket_delete(e);
736
            apr_bucket_delete(e);
691
            rv = APR_EGENERAL;
737
            rv = APR_INCOMPLETE;
692
        }
738
        }
693
    }
739
    }
694
    return rv;
740
    return rv;
695
}
741
}
696
742
697
static
743
enum rb_methods {
698
int ap_proxy_http_request(apr_pool_t *p, request_rec *r,
744
    RB_INIT,
699
                                   proxy_conn_rec *p_conn, proxy_worker *worker,
745
    RB_STREAM_CL,
700
                                   proxy_server_conf *conf,
746
    RB_STREAM_CHUNKED,
701
                                   apr_uri_t *uri,
747
    RB_SPOOL_CL
702
                                   char *url, char *server_portstr)
748
};
749
750
static int ap_proxy_http_prefetch(apr_pool_t *p, request_rec *r,
751
                                  proxy_conn_rec *p_conn, proxy_worker *worker,
752
                                  proxy_server_conf *conf,
753
                                  apr_uri_t *uri,
754
                                  char *url, char *server_portstr,
755
                                  apr_bucket_brigade *header_brigade,
756
                                  apr_bucket_brigade *input_brigade,
757
                                  char **old_cl_val, char **old_te_val,
758
                                  enum rb_methods *rb_method, int flushall)
703
{
759
{
704
    conn_rec *c = r->connection;
760
    conn_rec *c = r->connection;
705
    apr_bucket_alloc_t *bucket_alloc = c->bucket_alloc;
761
    apr_bucket_alloc_t *bucket_alloc = c->bucket_alloc;
706
    apr_bucket_brigade *header_brigade;
707
    apr_bucket_brigade *input_brigade;
708
    apr_bucket_brigade *temp_brigade;
762
    apr_bucket_brigade *temp_brigade;
709
    apr_bucket *e;
763
    apr_bucket *e;
710
    char *buf;
764
    char *buf;
711
    apr_status_t status;
765
    apr_status_t status;
712
    enum rb_methods {RB_INIT, RB_STREAM_CL, RB_STREAM_CHUNKED, RB_SPOOL_CL};
713
    enum rb_methods rb_method = RB_INIT;
714
    char *old_cl_val = NULL;
715
    char *old_te_val = NULL;
716
    apr_off_t bytes_read = 0;
766
    apr_off_t bytes_read = 0;
717
    apr_off_t bytes;
767
    apr_off_t bytes;
718
    int force10, rv;
768
    int force10, rv;
769
    apr_read_type_e block;
719
    conn_rec *origin = p_conn->connection;
770
    conn_rec *origin = p_conn->connection;
720
771
721
    if (apr_table_get(r->subprocess_env, "force-proxy-request-1.0")) {
772
    if (apr_table_get(r->subprocess_env, "force-proxy-request-1.0")) {
Lines 727-743 static apr_status_t proxy_buckets_lifetime_transfo Link Here
727
        force10 = 0;
778
        force10 = 0;
728
    }
779
    }
729
780
730
    header_brigade = apr_brigade_create(p, bucket_alloc);
731
    rv = ap_proxy_create_hdrbrgd(p, header_brigade, r, p_conn,
781
    rv = ap_proxy_create_hdrbrgd(p, header_brigade, r, p_conn,
732
                                 worker, conf, uri, url, server_portstr,
782
                                 worker, conf, uri, url, server_portstr,
733
                                 &old_cl_val, &old_te_val);
783
                                 old_cl_val, old_te_val);
734
    if (rv != OK) {
784
    if (rv != OK) {
735
        return rv;
785
        return rv;
736
    }
786
    }
737
787
738
    /* We have headers, let's figure out our request body... */
739
    input_brigade = apr_brigade_create(p, bucket_alloc);
740
741
    /* sub-requests never use keepalives, and mustn't pass request bodies.
788
    /* sub-requests never use keepalives, and mustn't pass request bodies.
742
     * Because the new logic looks at input_brigade, we will self-terminate
789
     * Because the new logic looks at input_brigade, we will self-terminate
743
     * input_brigade and jump past all of the request body logic...
790
     * input_brigade and jump past all of the request body logic...
Lines 750-764 static apr_status_t proxy_buckets_lifetime_transfo Link Here
750
    if (!r->kept_body && r->main) {
797
    if (!r->kept_body && r->main) {
751
        /* XXX: Why DON'T sub-requests use keepalives? */
798
        /* XXX: Why DON'T sub-requests use keepalives? */
752
        p_conn->close = 1;
799
        p_conn->close = 1;
753
        if (old_cl_val) {
800
        *old_cl_val = NULL;
754
            old_cl_val = NULL;
801
        *old_te_val = NULL;
755
            apr_table_unset(r->headers_in, "Content-Length");
802
        *rb_method = RB_STREAM_CL;
756
        }
757
        if (old_te_val) {
758
            old_te_val = NULL;
759
            apr_table_unset(r->headers_in, "Transfer-Encoding");
760
        }
761
        rb_method = RB_STREAM_CL;
762
        e = apr_bucket_eos_create(input_brigade->bucket_alloc);
803
        e = apr_bucket_eos_create(input_brigade->bucket_alloc);
763
        APR_BRIGADE_INSERT_TAIL(input_brigade, e);
804
        APR_BRIGADE_INSERT_TAIL(input_brigade, e);
764
        goto skip_body;
805
        goto skip_body;
Lines 772-790 static apr_status_t proxy_buckets_lifetime_transfo Link Here
772
     * encoding has been done by the extensions' handler, and
813
     * encoding has been done by the extensions' handler, and
773
     * do not modify add_te_chunked's logic
814
     * do not modify add_te_chunked's logic
774
     */
815
     */
775
    if (old_te_val && strcasecmp(old_te_val, "chunked") != 0) {
816
    if (*old_te_val && strcasecmp(*old_te_val, "chunked") != 0) {
776
        ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01093)
817
        ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01093)
777
                      "%s Transfer-Encoding is not supported", old_te_val);
818
                      "%s Transfer-Encoding is not supported", *old_te_val);
778
        return HTTP_INTERNAL_SERVER_ERROR;
819
        return HTTP_INTERNAL_SERVER_ERROR;
779
    }
820
    }
780
821
781
    if (old_cl_val && old_te_val) {
822
    if (*old_cl_val && *old_te_val) {
782
        ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(01094)
823
        ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(01094)
783
                      "client %s (%s) requested Transfer-Encoding "
824
                      "client %s (%s) requested Transfer-Encoding "
784
                      "chunked body with Content-Length (C-L ignored)",
825
                      "chunked body with Content-Length (C-L ignored)",
785
                      c->client_ip, c->remote_host ? c->remote_host: "");
826
                      c->client_ip, c->remote_host ? c->remote_host: "");
786
        apr_table_unset(r->headers_in, "Content-Length");
827
        apr_table_unset(r->headers_in, "Content-Length");
787
        old_cl_val = NULL;
828
        *old_cl_val = NULL;
788
        origin->keepalive = AP_CONN_CLOSE;
829
        origin->keepalive = AP_CONN_CLOSE;
789
        p_conn->close = 1;
830
        p_conn->close = 1;
790
    }
831
    }
Lines 798-807 static apr_status_t proxy_buckets_lifetime_transfo Link Here
798
     * reasonable size.
839
     * reasonable size.
799
     */
840
     */
800
    temp_brigade = apr_brigade_create(p, bucket_alloc);
841
    temp_brigade = apr_brigade_create(p, bucket_alloc);
842
    block = (flushall) ? APR_NONBLOCK_READ : APR_BLOCK_READ;
801
    do {
843
    do {
802
        status = ap_get_brigade(r->input_filters, temp_brigade,
844
        status = ap_get_brigade(r->input_filters, temp_brigade,
803
                                AP_MODE_READBYTES, APR_BLOCK_READ,
845
                                AP_MODE_READBYTES, block,
804
                                MAX_MEM_SPOOL - bytes_read);
846
                                MAX_MEM_SPOOL - bytes_read);
847
        /* ap_get_brigade will return success with an empty brigade
848
         * for a non-blocking read which would block
849
         */
850
        if (block == APR_NONBLOCK_READ
851
            && (APR_STATUS_IS_EAGAIN(status)
852
                || (status == APR_SUCCESS && APR_BRIGADE_EMPTY(temp_brigade)))) {
853
            status = APR_SUCCESS;
854
            break;
855
        }
805
        if (status != APR_SUCCESS) {
856
        if (status != APR_SUCCESS) {
806
            ap_log_rerror(APLOG_MARK, APLOG_ERR, status, r, APLOGNO(01095)
857
            ap_log_rerror(APLOG_MARK, APLOG_ERR, status, r, APLOGNO(01095)
807
                          "prefetch request body failed to %pI (%s)"
858
                          "prefetch request body failed to %pI (%s)"
Lines 839-845 static apr_status_t proxy_buckets_lifetime_transfo Link Here
839
     * (an arbitrary value.)
890
     * (an arbitrary value.)
840
     */
891
     */
841
    } while ((bytes_read < MAX_MEM_SPOOL - 80)
892
    } while ((bytes_read < MAX_MEM_SPOOL - 80)
842
              && !APR_BUCKET_IS_EOS(APR_BRIGADE_LAST(input_brigade)));
893
              && !APR_BUCKET_IS_EOS(APR_BRIGADE_LAST(input_brigade))
894
              && !flushall);
843
895
844
    /* Use chunked request body encoding or send a content-length body?
896
    /* Use chunked request body encoding or send a content-length body?
845
     *
897
     *
Lines 876-882 static apr_status_t proxy_buckets_lifetime_transfo Link Here
876
     * is absent, and the filters are unchanged (the body won't
928
     * is absent, and the filters are unchanged (the body won't
877
     * be resized by another content filter).
929
     * be resized by another content filter).
878
     */
930
     */
879
    if (APR_BUCKET_IS_EOS(APR_BRIGADE_LAST(input_brigade))) {
931
    if (!APR_BRIGADE_EMPTY(input_brigade) &&
932
            APR_BUCKET_IS_EOS(APR_BRIGADE_LAST(input_brigade))) {
880
        /* The whole thing fit, so our decision is trivial, use
933
        /* The whole thing fit, so our decision is trivial, use
881
         * the filtered bytes read from the client for the request
934
         * the filtered bytes read from the client for the request
882
         * body Content-Length.
935
         * body Content-Length.
Lines 884-927 static apr_status_t proxy_buckets_lifetime_transfo Link Here
884
         * If we expected no body, and read no body, do not set
937
         * If we expected no body, and read no body, do not set
885
         * the Content-Length.
938
         * the Content-Length.
886
         */
939
         */
887
        if (old_cl_val || old_te_val || bytes_read) {
940
        if (*old_cl_val || *old_te_val || bytes_read) {
888
            old_cl_val = apr_off_t_toa(r->pool, bytes_read);
941
            *old_cl_val = apr_off_t_toa(r->pool, bytes_read);
889
        }
942
        }
890
        rb_method = RB_STREAM_CL;
943
        *rb_method = RB_STREAM_CL;
891
    }
944
    }
892
    else if (old_te_val) {
945
    else if (*old_te_val) {
893
        if (force10
946
        if (force10
894
             || (apr_table_get(r->subprocess_env, "proxy-sendcl")
947
             || (!flushall
895
                  && !apr_table_get(r->subprocess_env, "proxy-sendchunks")
948
                 && apr_table_get(r->subprocess_env, "proxy-sendcl")
896
                  && !apr_table_get(r->subprocess_env, "proxy-sendchunked"))) {
949
                 && !apr_table_get(r->subprocess_env, "proxy-sendchunks")
897
            rb_method = RB_SPOOL_CL;
950
                 && !apr_table_get(r->subprocess_env, "proxy-sendchunked"))) {
951
            *rb_method = RB_SPOOL_CL;
898
        }
952
        }
899
        else {
953
        else {
900
            rb_method = RB_STREAM_CHUNKED;
954
            *rb_method = RB_STREAM_CHUNKED;
901
        }
955
        }
902
    }
956
    }
903
    else if (old_cl_val) {
957
    else if (*old_cl_val) {
904
        if (r->input_filters == r->proto_input_filters) {
958
        if (r->input_filters == r->proto_input_filters) {
905
            rb_method = RB_STREAM_CL;
959
            *rb_method = RB_STREAM_CL;
906
        }
960
        }
907
        else if (!force10
961
        else if (!force10
908
                  && (apr_table_get(r->subprocess_env, "proxy-sendchunks")
962
                  && (flushall
963
                      || apr_table_get(r->subprocess_env, "proxy-sendchunks")
909
                      || apr_table_get(r->subprocess_env, "proxy-sendchunked"))
964
                      || apr_table_get(r->subprocess_env, "proxy-sendchunked"))
910
                  && !apr_table_get(r->subprocess_env, "proxy-sendcl")) {
965
                  && !apr_table_get(r->subprocess_env, "proxy-sendcl")) {
911
            rb_method = RB_STREAM_CHUNKED;
966
            *rb_method = RB_STREAM_CHUNKED;
912
        }
967
        }
913
        else {
968
        else {
914
            rb_method = RB_SPOOL_CL;
969
            *rb_method = RB_SPOOL_CL;
915
        }
970
        }
916
    }
971
    }
917
    else {
972
    else {
918
        /* This is an appropriate default; very efficient for no-body
973
        /* This is an appropriate default; very efficient for no-body
919
         * requests, and has the behavior that it will not add any C-L
974
         * requests, and has the behavior that it will not add any C-L
920
         * when the old_cl_val is NULL.
975
         * when the *old_cl_val is NULL.
921
         */
976
         */
922
        rb_method = RB_SPOOL_CL;
977
        *rb_method = RB_SPOOL_CL;
923
    }
978
    }
924
979
980
    /* If we have to spool the body, do it now, before connecting/reusing
981
     * the backend connection, not in the connect/reuse-then-forward window.
982
     */
983
    if (*rb_method == RB_SPOOL_CL) {
984
        rv = spool_reqbody_cl(p, r, header_brigade, input_brigade,
985
                              (bytes_read > 0)
986
                              || (*old_cl_val != NULL)
987
                              || (*old_te_val != NULL));
988
        if (rv != OK) {
989
            return rv;
990
        }
991
    }
992
925
/* Yes I hate gotos.  This is the subrequest shortcut */
993
/* Yes I hate gotos.  This is the subrequest shortcut */
926
skip_body:
994
skip_body:
927
    /*
995
    /*
Lines 941-961 skip_body: Link Here
941
        APR_BRIGADE_INSERT_TAIL(header_brigade, e);
1009
        APR_BRIGADE_INSERT_TAIL(header_brigade, e);
942
    }
1010
    }
943
1011
1012
    return OK;
1013
}
1014
1015
static
1016
int ap_proxy_http_request(apr_pool_t *p, request_rec *r,
1017
                                   proxy_conn_rec *p_conn,
1018
                                   apr_bucket_brigade *header_brigade,
1019
                                   apr_bucket_brigade *input_brigade,
1020
                                   char *old_cl_val, char *old_te_val,
1021
                                   enum rb_methods rb_method, int flushall)
1022
{
1023
    int rv;
1024
    conn_rec *origin = p_conn->connection;
1025
944
    /* send the request body, if any. */
1026
    /* send the request body, if any. */
945
    switch(rb_method) {
1027
    switch(rb_method) {
946
    case RB_STREAM_CHUNKED:
1028
    case RB_STREAM_CHUNKED:
947
        rv = stream_reqbody_chunked(p, r, p_conn, origin, header_brigade,
1029
        rv = stream_reqbody_chunked(p, r, p_conn, origin, header_brigade,
948
                                        input_brigade);
1030
                                    input_brigade, flushall);
949
        break;
1031
        break;
950
    case RB_STREAM_CL:
1032
    case RB_STREAM_CL:
951
        rv = stream_reqbody_cl(p, r, p_conn, origin, header_brigade,
1033
        rv = stream_reqbody_cl(p, r, p_conn, origin, header_brigade,
952
                                   input_brigade, old_cl_val);
1034
                               input_brigade, old_cl_val, flushall);
953
        break;
1035
        break;
954
    case RB_SPOOL_CL:
1036
    case RB_SPOOL_CL:
955
        rv = spool_reqbody_cl(p, r, p_conn, origin, header_brigade,
1037
        /* This is all a single brigade, pass with flush flagged */
956
                                  input_brigade, (old_cl_val != NULL)
1038
        rv = ap_proxy_pass_brigade(r->connection->bucket_alloc,
957
                                              || (old_te_val != NULL)
1039
                                   r, p_conn, origin, header_brigade, 1);
958
                                              || (bytes_read > 0));
959
        break;
1040
        break;
960
    default:
1041
    default:
961
        /* shouldn't be possible */
1042
        /* shouldn't be possible */
Lines 964-969 skip_body: Link Here
964
    }
1045
    }
965
1046
966
    if (rv != OK) {
1047
    if (rv != OK) {
1048
        conn_rec *c = r->connection;
967
        /* apr_status_t value has been logged in lower level method */
1049
        /* apr_status_t value has been logged in lower level method */
968
        ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01097)
1050
        ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01097)
969
                      "pass request body failed to %pI (%s) from %s (%s)",
1051
                      "pass request body failed to %pI (%s) from %s (%s)",
Lines 1878-1887 static int proxy_http_handler(request_rec *r, prox Link Here
1878
    char *scheme;
1960
    char *scheme;
1879
    const char *proxy_function;
1961
    const char *proxy_function;
1880
    const char *u;
1962
    const char *u;
1963
    apr_bucket_brigade *header_brigade, *header_bb = NULL;
1964
    apr_bucket_brigade *input_brigade, *input_bb = NULL;
1881
    proxy_conn_rec *backend = NULL;
1965
    proxy_conn_rec *backend = NULL;
1882
    int is_ssl = 0;
1966
    int is_ssl = 0;
1883
    conn_rec *c = r->connection;
1967
    conn_rec *c = r->connection;
1884
    int retry = 0;
1968
    int retry = 0;
1969
    char *old_cl_val = NULL, *old_te_val = NULL;
1970
    enum rb_methods rb_method = RB_INIT;
1971
    char *locurl = url;
1972
    int flushall = 0;
1973
    int toclose = 0;
1885
    /*
1974
    /*
1886
     * Use a shorter-lived pool to reduce memory usage
1975
     * Use a shorter-lived pool to reduce memory usage
1887
     * and avoid a memory leak
1976
     * and avoid a memory leak
Lines 1948-1963 static int proxy_http_handler(request_rec *r, prox Link Here
1948
        backend->close = 1;
2037
        backend->close = 1;
1949
    }
2038
    }
1950
2039
2040
    if (apr_table_get(r->subprocess_env, "proxy-flushall")) {
2041
        flushall = 1;
2042
    }
2043
2044
    /* Step One: Determine Who To Connect To */
2045
    if ((status = ap_proxy_determine_connection(p, r, conf, worker, backend,
2046
                                            uri, &locurl, proxyname,
2047
                                            proxyport, server_portstr,
2048
                                            sizeof(server_portstr))) != OK)
2049
        goto cleanup;
2050
2051
    /* Step Once: Prefetch (partially) the request body so to increase the
2052
     * chances to get the whole (or enough) body and determine Content-Length
2053
     * vs chunked or spool. By doing this before connecting or reusing a
2054
     * backend connection, minimize the delay between checking whether this
2055
     * connection is still alive and the first packet sent, should the link be
2056
     * slow or some input filter retain the data.
2057
     */
2058
    input_brigade = apr_brigade_create(p, c->bucket_alloc);
2059
    header_brigade = apr_brigade_create(p, c->bucket_alloc);
2060
    if ((status = ap_proxy_http_prefetch(p, r, backend, worker, conf, uri,
2061
                                         locurl, server_portstr,
2062
                                         header_brigade, input_brigade,
2063
                                         &old_cl_val, &old_te_val, &rb_method,
2064
                                         flushall)) != OK) {
2065
        goto cleanup;
2066
    }
2067
2068
    /* XXX: Reset backend->close now, since ap_proxy_http_prefetch() sets it to
2069
     * disable the reuse of the connection after this request (no keep-alive),
2070
     * not to close any reusable connection before this request. However assure
2071
     * what is expected later by using a local flag and do the right thing when
2072
     * ap_proxy_connect_backend (below) provides the connection to close.
2073
     */
2074
    toclose = backend->close;
2075
    backend->close = 0;
2076
1951
    while (retry < 2) {
2077
    while (retry < 2) {
1952
        char *locurl = url;
2078
        conn_rec *backconn;
1953
2079
1954
        /* Step One: Determine Who To Connect To */
2080
        if (retry) {
1955
        if ((status = ap_proxy_determine_connection(p, r, conf, worker, backend,
2081
            char *newurl = url;
1956
                                                uri, &locurl, proxyname,
1957
                                                proxyport, server_portstr,
1958
                                                sizeof(server_portstr))) != OK)
1959
            break;
1960
2082
2083
            /* Step One-Retry: Redetermine Who To Connect To */
2084
            if ((status = ap_proxy_determine_connection(p, r, conf, worker,
2085
                            backend, uri, &newurl, proxyname, proxyport,
2086
                            server_portstr, sizeof(server_portstr))) != OK)
2087
                break;
2088
2089
            /* XXX: the code assumes locurl is not changed during the loop,
2090
             * or ap_proxy_http_prefetch() would have to be called every time,
2091
             * and header_brigade be changed accordingly...
2092
             */
2093
            AP_DEBUG_ASSERT(strcmp(newurl, locurl) == 0);
2094
        }
2095
1961
        /* Step Two: Make the Connection */
2096
        /* Step Two: Make the Connection */
1962
        if (ap_proxy_connect_backend(proxy_function, backend, worker, r->server)) {
2097
        if (ap_proxy_connect_backend(proxy_function, backend, worker, r->server)) {
1963
            ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01114)
2098
            ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01114)
Lines 1968-1977 static int proxy_http_handler(request_rec *r, prox Link Here
1968
        }
2103
        }
1969
2104
1970
        /* Step Three: Create conn_rec */
2105
        /* Step Three: Create conn_rec */
1971
        if (!backend->connection) {
2106
        backconn = backend->connection;
2107
        if (!backconn) {
1972
            if ((status = ap_proxy_connection_create(proxy_function, backend,
2108
            if ((status = ap_proxy_connection_create(proxy_function, backend,
1973
                                                     c, r->server)) != OK)
2109
                                                     c, r->server)) != OK)
1974
                break;
2110
                break;
2111
            backconn = backend->connection;
2112
1975
            /*
2113
            /*
1976
             * On SSL connections set a note on the connection what CN is
2114
             * On SSL connections set a note on the connection what CN is
1977
             * requested, such that mod_ssl can check if it is requested to do
2115
             * requested, such that mod_ssl can check if it is requested to do
Lines 1984-1995 static int proxy_http_handler(request_rec *r, prox Link Here
1984
            }
2122
            }
1985
        }
2123
        }
1986
2124
2125
        /* Don't recycle the connection if prefetch (above) told not to do so */
2126
        if (toclose) {
2127
            backend->close = 1;
2128
            backconn->keepalive = AP_CONN_CLOSE;
2129
        }
2130
2131
        /* Preserve the header/input brigades since they may be retried. */
2132
        if (header_bb) {
2133
            apr_brigade_cleanup(header_bb);
2134
            apr_brigade_cleanup(input_bb);
2135
        }
2136
        else {
2137
            header_bb = apr_brigade_create(p, c->bucket_alloc);
2138
            input_bb = apr_brigade_create(p, c->bucket_alloc);
2139
        }
2140
        proxy_buckets_lifetime_transform(r, header_brigade, header_bb);
2141
        proxy_buckets_lifetime_transform(r, input_brigade, input_bb);
2142
1987
        /* Step Four: Send the Request
2143
        /* Step Four: Send the Request
1988
         * On the off-chance that we forced a 100-Continue as a
2144
         * On the off-chance that we forced a 100-Continue as a
1989
         * kinda HTTP ping test, allow for retries
2145
         * kinda HTTP ping test, allow for retries
1990
         */
2146
         */
1991
        if ((status = ap_proxy_http_request(p, r, backend, worker,
2147
        if ((status = ap_proxy_http_request(p, r, backend, header_bb, input_bb,
1992
                                        conf, uri, locurl, server_portstr)) != OK) {
2148
                                            old_cl_val, old_te_val, rb_method,
2149
                                            flushall)) != OK) {
1993
            if ((status == HTTP_SERVICE_UNAVAILABLE) && worker->s->ping_timeout_set) {
2150
            if ((status == HTTP_SERVICE_UNAVAILABLE) && worker->s->ping_timeout_set) {
1994
                backend->close = 1;
2151
                backend->close = 1;
1995
                ap_log_rerror(APLOG_MARK, APLOG_INFO, status, r, APLOGNO(01115)
2152
                ap_log_rerror(APLOG_MARK, APLOG_INFO, status, r, APLOGNO(01115)
Lines 2000-2006 static int proxy_http_handler(request_rec *r, prox Link Here
2000
            } else {
2157
            } else {
2001
                break;
2158
                break;
2002
            }
2159
            }
2003
2004
        }
2160
        }
2005
2161
2006
        /* Step Five: Receive the Response... Fall thru to cleanup */
2162
        /* Step Five: Receive the Response... Fall thru to cleanup */
Lines 2011-2016 static int proxy_http_handler(request_rec *r, prox Link Here
2011
    }
2167
    }
2012
2168
2013
    /* Step Six: Clean Up */
2169
    /* Step Six: Clean Up */
2170
    if (header_bb) {
2171
        apr_brigade_destroy(header_bb);
2172
        apr_brigade_destroy(input_bb);
2173
    }
2014
cleanup:
2174
cleanup:
2015
    if (backend) {
2175
    if (backend) {
2016
        if (status != OK)
2176
        if (status != OK)

Return to bug 56541