Apache HTTPD
mod_proxy_http.c
Go to the documentation of this file.
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/* HTTP routines for Apache proxy */
18
19#include "mod_proxy.h"
20#include "ap_regex.h"
21
22module AP_MODULE_DECLARE_DATA proxy_http_module;
23
25 NULL;
26
27static apr_status_t ap_proxy_http_cleanup(const char *scheme,
29 proxy_conn_rec *backend);
30
32 request_rec *r, int flags, int *read);
33
34static const char *get_url_scheme(const char **url, int *is_ssl)
35{
36 const char *u = *url;
37
38 switch (u[0]) {
39 case 'h':
40 case 'H':
41 if (strncasecmp(u + 1, "ttp", 3) == 0) {
42 if (u[4] == ':') {
43 *is_ssl = 0;
44 *url = u + 5;
45 return "http";
46 }
47 if (apr_tolower(u[4]) == 's' && u[5] == ':') {
48 *is_ssl = 1;
49 *url = u + 6;
50 return "https";
51 }
52 }
53 break;
54
55 case 'w':
56 case 'W':
57 if (apr_tolower(u[1]) == 's') {
58 if (u[2] == ':') {
59 *is_ssl = 0;
60 *url = u + 3;
61 return "ws";
62 }
63 if (apr_tolower(u[2]) == 's' && u[3] == ':') {
64 *is_ssl = 1;
65 *url = u + 4;
66 return "wss";
67 }
68 }
69 break;
70 }
71
72 *is_ssl = 0;
73 return NULL;
74}
75
76/*
77 * Canonicalise http-like URLs.
78 * scheme is the scheme for the URL
79 * url is the URL starting with the first '/'
80 */
81static int proxy_http_canon(request_rec *r, char *url)
82{
83 const char *base_url = url;
84 char *host, *path, sport[7];
85 char *search = NULL;
86 const char *err;
87 const char *scheme;
89 int is_ssl = 0;
90
91 scheme = get_url_scheme((const char **)&url, &is_ssl);
92 if (!scheme) {
93 return DECLINED;
94 }
96
98 "HTTP: canonicalising URL %s", base_url);
99
100 /* do syntatic check.
101 * We break the URL into host, port, path, search
102 */
104 if (err) {
106 "error parsing URL %s: %s", base_url, err);
107 return HTTP_BAD_REQUEST;
108 }
109
110 /*
111 * now parse path/search args, according to rfc1738:
112 * process the path.
113 *
114 * In a reverse proxy, our URL has been processed, so canonicalise
115 * unless proxy-nocanon is set to say it's raw
116 * In a forward proxy, we have and MUST NOT MANGLE the original.
117 */
118 switch (r->proxyreq) {
119 default: /* wtf are we doing here? */
120 case PROXYREQ_REVERSE:
121 if (apr_table_get(r->notes, "proxy-nocanon")) {
122 path = url; /* this is the raw path */
123 }
124 else if (apr_table_get(r->notes, "proxy-noencode")) {
125 path = url; /* this is the encoded path already */
126 search = r->args;
127 }
128 else {
130 int flags = d->allow_encoded_slashes && !d->decode_encoded_slashes ? PROXY_CANONENC_NOENCODEDSLASHENCODING : 0;
131
133 flags, r->proxyreq);
134 if (!path) {
135 return HTTP_BAD_REQUEST;
136 }
137 search = r->args;
138 }
139 break;
140 case PROXYREQ_PROXY:
141 path = url;
142 break;
143 }
144 /*
145 * If we have a raw control character or a ' ' in nocanon path or
146 * r->args, correct encoding was missed.
147 */
148 if (path == url && *ap_scan_vchar_obstext(path)) {
150 "To be forwarded path contains control "
151 "characters or spaces");
152 return HTTP_FORBIDDEN;
153 }
156 "To be forwarded query string contains control "
157 "characters or spaces");
158 return HTTP_FORBIDDEN;
159 }
160
161 if (port != def_port)
162 apr_snprintf(sport, sizeof(sport), ":%d", port);
163 else
164 sport[0] = '\0';
165
166 if (ap_strchr_c(host, ':')) { /* if literal IPv6 address */
167 host = apr_pstrcat(r->pool, "[", host, "]", NULL);
168 }
169
170 r->filename = apr_pstrcat(r->pool, "proxy:", scheme, "://", host, sport,
171 "/", path, (search) ? "?" : "", search, NULL);
172 return OK;
173}
174
175/* Clear all connection-based headers from the incoming headers table */
182static int clean_warning_headers(void *data, const char *key, const char *val)
183{
184 apr_table_t *headers = ((header_dptr*)data)->table;
185 apr_pool_t *pool = ((header_dptr*)data)->pool;
186 char *warning;
187 char *date;
189 const int nmatch = 3;
191
192 if (headers == NULL) {
193 ((header_dptr*)data)->table = headers = apr_table_make(pool, 2);
194 }
195/*
196 * Parse this, suckers!
197 *
198 * Warning = "Warning" ":" 1#warning-value
199 *
200 * warning-value = warn-code SP warn-agent SP warn-text
201 * [SP warn-date]
202 *
203 * warn-code = 3DIGIT
204 * warn-agent = ( host [ ":" port ] ) | pseudonym
205 * ; the name or pseudonym of the server adding
206 * ; the Warning header, for use in debugging
207 * warn-text = quoted-string
208 * warn-date = <"> HTTP-date <">
209 *
210 * Buggrit, use a bloomin' regexp!
211 * (\d{3}\s+\S+\s+\".*?\"(\s+\"(.*?)\")?) --> whole in $1, date in $3
212 */
213 while (!ap_regexec(warn_rx, val, nmatch, pmatch, 0)) {
214 warning = apr_pstrndup(pool, val+pmatch[0].rm_so,
215 pmatch[0].rm_eo - pmatch[0].rm_so);
216 warn_time = 0;
217 if (pmatch[2].rm_eo > pmatch[2].rm_so) {
218 /* OK, we have a date here */
219 date = apr_pstrndup(pool, val+pmatch[2].rm_so,
220 pmatch[2].rm_eo - pmatch[2].rm_so);
222 }
223 if (!warn_time || (warn_time == ((header_dptr*)data)->time)) {
224 apr_table_addn(headers, key, warning);
225 }
226 val += pmatch[0].rm_eo;
227 }
228 return 1;
229}
231{
232 header_dptr x;
233 x.pool = p;
234 x.table = NULL;
235 x.time = apr_date_parse_http(apr_table_get(headers, "Date"));
236 apr_table_do(clean_warning_headers, &x, headers, "Warning", NULL);
237 if (x.table != NULL) {
238 apr_table_unset(headers, "Warning");
239 return apr_table_overlay(p, headers, x.table);
240 }
241 else {
242 return headers;
243 }
244}
245
247 apr_bucket_alloc_t *bucket_alloc,
248 apr_bucket_brigade *header_brigade)
249{
250 apr_bucket *e;
251 char *buf;
252 const char te_hdr[] = "Transfer-Encoding: chunked" CRLF;
253
254 buf = apr_pmemdup(p, te_hdr, sizeof(te_hdr)-1);
256
257 e = apr_bucket_pool_create(buf, sizeof(te_hdr)-1, p, bucket_alloc);
258 APR_BRIGADE_INSERT_TAIL(header_brigade, e);
259}
260
261static void add_cl(apr_pool_t *p,
262 apr_bucket_alloc_t *bucket_alloc,
263 apr_bucket_brigade *header_brigade,
264 const char *cl_val)
265{
266 apr_bucket *e;
267 char *buf;
268
269 buf = apr_pstrcat(p, "Content-Length: ",
270 cl_val,
271 CRLF,
272 NULL);
274 e = apr_bucket_pool_create(buf, strlen(buf), p, bucket_alloc);
275 APR_BRIGADE_INSERT_TAIL(header_brigade, e);
276}
277
278#ifndef CRLF_ASCII
279#define CRLF_ASCII "\015\012"
280#endif
281#ifndef ZERO_ASCII
282#define ZERO_ASCII "\060"
283#endif
284
285#define MAX_MEM_SPOOL 16384
286
293
319
321{
322 request_rec *r = req->r;
323 int seen_eos = 0, rv = OK;
325 char chunk_hdr[20]; /* must be here due to transient bucket. */
326 conn_rec *origin = req->origin;
327 proxy_conn_rec *p_conn = req->backend;
328 apr_bucket_alloc_t *bucket_alloc = req->bucket_alloc;
329 apr_bucket_brigade *header_brigade = req->header_brigade;
330 apr_bucket_brigade *input_brigade = req->input_brigade;
331 rb_methods rb_method = req->rb_method;
333 apr_bucket *e;
334
335 do {
336 if (APR_BRIGADE_EMPTY(input_brigade)
337 && APR_BRIGADE_EMPTY(header_brigade)) {
338 rv = ap_proxy_read_input(r, p_conn, input_brigade,
340 if (rv != OK) {
341 return rv;
342 }
343 }
344
345 if (!APR_BRIGADE_EMPTY(input_brigade)) {
346 /* If this brigade contains EOS, remove it and be done. */
347 if (APR_BUCKET_IS_EOS(APR_BRIGADE_LAST(input_brigade))) {
348 seen_eos = 1;
349
350 /* We can't pass this EOS to the output_filters. */
351 e = APR_BRIGADE_LAST(input_brigade);
353 }
354
355 apr_brigade_length(input_brigade, 1, &bytes);
357
358 if (rb_method == RB_STREAM_CHUNKED) {
359 if (bytes) {
360 /*
361 * Prepend the size of the chunk
362 */
368 bucket_alloc);
369 APR_BRIGADE_INSERT_HEAD(input_brigade, e);
370
371 /*
372 * Append the end-of-chunk CRLF
373 */
374 e = apr_bucket_immortal_create(CRLF_ASCII, 2, bucket_alloc);
375 APR_BRIGADE_INSERT_TAIL(input_brigade, e);
376 }
377 if (seen_eos) {
378 /*
379 * Append the tailing 0-size chunk
380 */
382 /* <trailers> */
384 5, bucket_alloc);
385 APR_BRIGADE_INSERT_TAIL(input_brigade, e);
386 }
387 }
388 else if (rb_method == RB_STREAM_CL
389 && (bytes_streamed > req->cl_val
390 || (seen_eos && bytes_streamed < req->cl_val))) {
391 /* C-L != bytes streamed?!?
392 *
393 * Prevent HTTP Request/Response Splitting.
394 *
395 * We can't stream more (or less) bytes at the back end since
396 * they could be interpreted in separate requests (more bytes
397 * now would start a new request, less bytes would make the
398 * first bytes of the next request be part of the current one).
399 *
400 * It can't happen from the client connection here thanks to
401 * ap_http_filter(), but some module's filter may be playing
402 * bad games, hence the HTTP_INTERNAL_SERVER_ERROR.
403 */
405 "read %s bytes of request body than expected "
406 "(got %" APR_OFF_T_FMT ", expected "
407 "%" APR_OFF_T_FMT ")",
408 bytes_streamed > req->cl_val ? "more" : "less",
409 bytes_streamed, req->cl_val);
411 }
412
413 if (seen_eos && apr_table_get(r->subprocess_env,
414 "proxy-sendextracrlf")) {
415 e = apr_bucket_immortal_create(CRLF_ASCII, 2, bucket_alloc);
416 APR_BRIGADE_INSERT_TAIL(input_brigade, e);
417 }
418 }
419
420 /* If we never sent the header brigade, go ahead and take care of
421 * that now by prepending it (once only since header_brigade will be
422 * empty afterward).
423 */
424 APR_BRIGADE_PREPEND(input_brigade, header_brigade);
425
426 /* Flush here on EOS because we won't ap_proxy_read_input() again. */
427 rv = ap_proxy_pass_brigade(bucket_alloc, r, p_conn, origin,
428 input_brigade, seen_eos);
429 if (rv != OK) {
430 return rv;
431 }
432 } while (!seen_eos);
433
434 return OK;
435}
436
438{
439 apr_bucket_alloc_t *bucket_alloc = req->bucket_alloc;
440 apr_bucket *e;
441 char *buf;
442
443 /*
444 * Handle Connection: header if we do HTTP/1.1 request:
445 * If we plan to close the backend connection sent Connection: close
446 * otherwise sent Connection: Keep-Alive.
447 */
448 if (!req->force10) {
449 if (req->upgrade) {
450 buf = apr_pstrdup(req->p, "Connection: Upgrade" CRLF);
452 e = apr_bucket_pool_create(buf, strlen(buf), req->p, bucket_alloc);
454
455 /* Tell the backend that it can upgrade the connection. */
456 buf = apr_pstrcat(req->p, "Upgrade: ", req->upgrade, CRLF, NULL);
457 }
458 else if (ap_proxy_connection_reusable(req->backend)) {
459 buf = apr_pstrdup(req->p, "Connection: Keep-Alive" CRLF);
460 }
461 else {
462 buf = apr_pstrdup(req->p, "Connection: close" CRLF);
463 }
465 e = apr_bucket_pool_create(buf, strlen(buf), req->p, bucket_alloc);
467 }
468
469 /* add empty line at the end of the headers */
470 e = apr_bucket_immortal_create(CRLF_ASCII, 2, bucket_alloc);
472}
473
475 apr_uri_t *uri, char *url)
476{
477 apr_pool_t *p = req->p;
478 request_rec *r = req->r;
480 proxy_conn_rec *p_conn = req->backend;
481 apr_bucket_alloc_t *bucket_alloc = req->bucket_alloc;
482 apr_bucket_brigade *header_brigade = req->header_brigade;
483 apr_bucket_brigade *input_brigade = req->input_brigade;
484 apr_bucket *e;
487 int rv;
488
489 rv = ap_proxy_create_hdrbrgd(p, header_brigade, r, p_conn,
490 req->worker, req->sconf,
491 uri, url, req->server_portstr,
492 &req->old_cl_val, &req->old_te_val);
493 if (rv != OK) {
494 return rv;
495 }
496
497 /* sub-requests never use keepalives, and mustn't pass request bodies.
498 * Because the new logic looks at input_brigade, we will self-terminate
499 * input_brigade and jump past all of the request body logic...
500 * Reading anything with ap_get_brigade is likely to consume the
501 * main request's body or read beyond EOS - which would be unpleasant.
502 *
503 * An exception: when a kept_body is present, then subrequest CAN use
504 * pass request bodies, and we DONT skip the body.
505 */
506 if (!r->kept_body && r->main) {
507 /* XXX: Why DON'T sub-requests use keepalives? */
508 p_conn->close = 1;
509 req->old_te_val = NULL;
510 req->old_cl_val = NULL;
511 req->rb_method = RB_STREAM_CL;
512 e = apr_bucket_eos_create(input_brigade->bucket_alloc);
513 APR_BRIGADE_INSERT_TAIL(input_brigade, e);
514 goto skip_body;
515 }
516
517 /* WE only understand chunked. Other modules might inject
518 * (and therefore, decode) other flavors but we don't know
519 * that the can and have done so unless they remove
520 * their decoding from the headers_in T-E list.
521 * XXX: Make this extensible, but in doing so, presume the
522 * encoding has been done by the extensions' handler, and
523 * do not modify add_te_chunked's logic
524 */
525 if (req->old_te_val && ap_cstr_casecmp(req->old_te_val, "chunked") != 0) {
527 "%s Transfer-Encoding is not supported",
528 req->old_te_val);
530 }
531
532 if (req->old_cl_val && req->old_te_val) {
534 "client %s (%s) requested Transfer-Encoding "
535 "chunked body with Content-Length (C-L ignored)",
536 c->client_ip, c->remote_host ? c->remote_host: "");
537 req->old_cl_val = NULL;
538 p_conn->close = 1;
539 }
540
541 rv = ap_proxy_prefetch_input(r, req->backend, input_brigade,
545 if (rv != OK) {
546 return rv;
547 }
548
549 /* Use chunked request body encoding or send a content-length body?
550 *
551 * Prefer C-L when:
552 *
553 * We have no request body (handled by RB_STREAM_CL)
554 *
555 * We have a request body length <= MAX_MEM_SPOOL
556 *
557 * The administrator has setenv force-proxy-request-1.0
558 *
559 * The client sent a C-L body, and the administrator has
560 * not setenv proxy-sendchunked or has set setenv proxy-sendcl
561 *
562 * The client sent a T-E body, and the administrator has
563 * setenv proxy-sendcl, and not setenv proxy-sendchunked
564 *
565 * If both proxy-sendcl and proxy-sendchunked are set, the
566 * behavior is the same as if neither were set, large bodies
567 * that can't be read will be forwarded in their original
568 * form of C-L, or T-E.
569 *
570 * To ensure maximum compatibility, setenv proxy-sendcl
571 * To reduce server resource use, setenv proxy-sendchunked
572 *
573 * Then address specific servers with conditional setenv
574 * options to restore the default behavior where desirable.
575 *
576 * We have to compute content length by reading the entire request
577 * body; if request body is not small, we'll spool the remaining
578 * input to a temporary file. Chunked is always preferable.
579 *
580 * We can only trust the client-provided C-L if the T-E header
581 * is absent, and the filters are unchanged (the body won't
582 * be resized by another content filter).
583 */
584 if (!APR_BRIGADE_EMPTY(input_brigade)
585 && APR_BUCKET_IS_EOS(APR_BRIGADE_LAST(input_brigade))) {
586 /* The whole thing fit, so our decision is trivial, use
587 * the filtered bytes read from the client for the request
588 * body Content-Length.
589 *
590 * If we expected no body, and read no body, do not set
591 * the Content-Length.
592 */
593 if (req->old_cl_val || req->old_te_val || bytes_read) {
595 req->cl_val = bytes_read;
596 }
597 req->rb_method = RB_STREAM_CL;
598 }
599 else if (req->old_te_val) {
600 if (req->force10
601 || (apr_table_get(r->subprocess_env, "proxy-sendcl")
602 && !apr_table_get(r->subprocess_env, "proxy-sendchunks")
603 && !apr_table_get(r->subprocess_env, "proxy-sendchunked"))) {
604 req->rb_method = RB_SPOOL_CL;
605 }
606 else {
608 }
609 }
610 else if (req->old_cl_val) {
612 if (!ap_parse_strict_length(&req->cl_val, req->old_cl_val)) {
614 "could not parse request Content-Length (%s)",
615 req->old_cl_val);
616 return HTTP_BAD_REQUEST;
617 }
618 req->rb_method = RB_STREAM_CL;
619 }
620 else if (!req->force10
621 && (apr_table_get(r->subprocess_env, "proxy-sendchunks")
622 || apr_table_get(r->subprocess_env, "proxy-sendchunked"))
623 && !apr_table_get(r->subprocess_env, "proxy-sendcl")) {
625 }
626 else {
627 req->rb_method = RB_SPOOL_CL;
628 }
629 }
630 else {
631 /* This is an appropriate default; very efficient for no-body
632 * requests, and has the behavior that it will not add any C-L
633 * when the old_cl_val is NULL.
634 */
635 req->rb_method = RB_SPOOL_CL;
636 }
637
638 switch (req->rb_method) {
640 add_te_chunked(req->p, bucket_alloc, header_brigade);
641 break;
642
643 case RB_STREAM_CL:
644 if (req->old_cl_val) {
645 add_cl(req->p, bucket_alloc, header_brigade, req->old_cl_val);
646 }
647 break;
648
649 default: /* => RB_SPOOL_CL */
650 /* If we have to spool the body, do it now, before connecting or
651 * reusing the backend connection.
652 */
653 rv = ap_proxy_spool_input(r, p_conn, input_brigade,
655 if (rv != OK) {
656 return rv;
657 }
658 if (bytes || req->old_te_val || req->old_cl_val) {
659 add_cl(p, bucket_alloc, header_brigade, apr_off_t_toa(p, bytes));
660 }
661 }
662
663/* Yes I hate gotos. This is the subrequest shortcut */
666
667 return OK;
668}
669
671{
672 int rv;
673 request_rec *r = req->r;
674
675 /* send the request header/body, if any. */
676 switch (req->rb_method) {
677 case RB_SPOOL_CL:
678 case RB_STREAM_CL:
680 if (req->do_100_continue) {
682 req->origin, req->header_brigade, 1);
683 }
684 else {
685 rv = stream_reqbody(req);
686 }
687 break;
688
689 default:
690 /* shouldn't be possible */
692 break;
693 }
694
695 if (rv != OK) {
697 /* apr_status_t value has been logged in lower level method */
699 "pass request body failed to %pI (%s) from %s (%s)",
700 req->backend->addr,
701 req->backend->hostname ? req->backend->hostname: "",
702 c->client_ip, c->remote_host ? c->remote_host: "");
703 return rv;
704 }
705
706 return OK;
707}
708
709/*
710 * If the date is a valid RFC 850 date or asctime() date, then it
711 * is converted to the RFC 1123 format.
712 */
713static const char *date_canon(apr_pool_t *p, const char *date)
714{
715 apr_status_t rv;
716 char* ndate;
717
718 apr_time_t time = apr_date_parse_http(date);
719 if (!time) {
720 return date;
721 }
722
724 rv = apr_rfc822_date(ndate, time);
725 if (rv != APR_SUCCESS) {
726 return date;
727 }
728
729 return ndate;
730}
731
733{
736
737 apr_pool_create(&pool, c->pool);
738 apr_pool_tag(pool, "proxy_http_rp");
739
740 rp = apr_pcalloc(pool, sizeof(*r));
741
742 rp->pool = pool;
743 rp->status = HTTP_OK;
744
745 rp->headers_in = apr_table_make(pool, 50);
746 rp->trailers_in = apr_table_make(pool, 5);
747
748 rp->subprocess_env = apr_table_make(pool, 50);
749 rp->headers_out = apr_table_make(pool, 12);
750 rp->trailers_out = apr_table_make(pool, 5);
751 rp->err_headers_out = apr_table_make(pool, 5);
752 rp->notes = apr_table_make(pool, 5);
753
754 rp->server = r->server;
755 rp->log = r->log;
756 rp->proxyreq = r->proxyreq;
757 rp->request_time = r->request_time;
758 rp->connection = c;
759 rp->output_filters = c->output_filters;
760 rp->input_filters = c->input_filters;
761 rp->proto_output_filters = c->output_filters;
762 rp->proto_input_filters = c->input_filters;
763 rp->useragent_ip = c->client_ip;
764 rp->useragent_addr = c->client_addr;
765
766 rp->request_config = ap_create_request_config(pool);
768
769 return rp;
770}
771
773 const char *key, const char *value)
774{
775 static const char *date_hdrs[]
776 = { "Date", "Expires", "Last-Modified", NULL };
777 static const struct {
778 const char *name;
780 } transform_hdrs[] = {
781 { "Location", ap_proxy_location_reverse_map },
782 { "Content-Location", ap_proxy_location_reverse_map },
784 { "Destination", ap_proxy_location_reverse_map },
785 { "Set-Cookie", ap_proxy_cookie_reverse_map },
786 { NULL, NULL }
787 };
788 int i;
789 for (i = 0; date_hdrs[i]; ++i) {
790 if (!ap_cstr_casecmp(date_hdrs[i], key)) {
793 return;
794 }
795 }
796 for (i = 0; transform_hdrs[i].name; ++i) {
799 (*transform_hdrs[i].func)(r, c, value));
800 return;
801 }
802 }
804}
805
806/*
807 * Note: pread_len is the length of the response that we've mistakenly
808 * read (assuming that we don't consider that an error via
809 * ProxyBadHeader StartBody). This depends on buffer actually being
810 * local storage to the calling code in order for pread_len to make
811 * any sense at all, since we depend on buffer still containing
812 * what was read by ap_getline() upon return.
813 */
815 char *buffer, int size,
816 conn_rec *c, int *pread_len)
817{
818 int len;
819 char *value, *end;
820 int saw_headers = 0;
821 void *sconf = r->server->module_config;
823 proxy_dir_conf *dconf;
825 apr_bucket_brigade *tmp_bb;
826
827 dconf = ap_get_module_config(r->per_dir_config, &proxy_module);
828 psc = (proxy_server_conf *) ap_get_module_config(sconf, &proxy_module);
829
832 *pread_len = 0;
833
834 /*
835 * Read header lines until we get the empty separator line, a read error,
836 * the connection closes (EOF), or we timeout.
837 */
839 "Headers received from backend:");
840
841 tmp_bb = apr_brigade_create(r->pool, c->bucket_alloc);
842 while (1) {
843 rc = ap_proxygetline(tmp_bb, buffer, size, rr,
845
846
847 if (rc != APR_SUCCESS) {
849 int trunc = (len > 128 ? 128 : len) / 2;
851 "header size is over the limit allowed by "
852 "ResponseFieldSize (%d bytes). "
853 "Bad response header: '%.*s[...]%s'",
855 }
856 else {
858 "Error reading headers from backend");
859 }
860 r->headers_out = NULL;
861 return rc;
862 }
863
864 if (len <= 0) {
865 break;
866 }
867 else {
869 }
870
871 if (!(value = strchr(buffer, ':'))) { /* Find the colon separator */
872
873 /* We may encounter invalid headers, usually from buggy
874 * MS IIS servers, so we need to determine just how to handle
875 * them. We can either ignore them, assume that they mark the
876 * start-of-body (eg: a missing CRLF) or (the default) mark
877 * the headers as totally bogus and return a 500. The sole
878 * exception is an extra "HTTP/1.0 200, OK" line sprinkled
879 * in between the usual MIME headers, which is a favorite
880 * IIS bug.
881 */
882 /* XXX: The mask check is buggy if we ever see an HTTP/1.10 */
883
884 if (!apr_date_checkmask(buffer, "HTTP/#.# ###*")) {
885 if (psc->badopt == bad_error) {
886 /* Nope, it wasn't even an extra HTTP header. Give up. */
887 r->headers_out = NULL;
888 return APR_EINVAL;
889 }
890 else if (psc->badopt == bad_body) {
891 /* if we've already started loading headers_out, then
892 * return what we've accumulated so far, in the hopes
893 * that they are useful; also note that we likely pre-read
894 * the first line of the response.
895 */
896 if (saw_headers) {
898 "Starting body due to bogus non-header "
899 "in headers returned by %s (%s)",
900 r->uri, r->method);
901 *pread_len = len;
902 return APR_SUCCESS;
903 }
904 else {
906 "No HTTP headers returned by %s (%s)",
907 r->uri, r->method);
908 return APR_SUCCESS;
909 }
910 }
911 }
912 /* this is the psc->badopt == bad_ignore case */
914 "Ignoring bogus HTTP header returned by %s (%s)",
915 r->uri, r->method);
916 continue;
917 }
918
919 *value = '\0';
920 ++value;
921 /* XXX: RFC2068 defines only SP and HT as whitespace, this test is
922 * wrong... and so are many others probably.
923 */
924 while (apr_isspace(*value))
925 ++value; /* Skip to start of value */
926
927 /* should strip trailing whitespace as well */
928 for (end = &value[strlen(value)-1]; end > value && apr_isspace(*end); --end)
929 *end = '\0';
930
931 /* make sure we add so as not to destroy duplicated headers
932 * Modify headers requiring canonicalisation and/or affected
933 * by ProxyPassReverse and family with process_proxy_header
934 */
936 saw_headers = 1;
937 }
938 return APR_SUCCESS;
939}
940
941
942
943static int addit_dammit(void *v, const char *key, const char *val)
944{
946 return 1;
947}
948
950 request_rec *r, int flags, int *read)
951{
952 apr_status_t rv;
954
955 rv = ap_rgetline(&s, n, &len, r, flags, bb);
957
958 if (rv == APR_SUCCESS || APR_STATUS_IS_ENOSPC(rv)) {
959 *read = (int)len;
960 } else {
961 *read = -1;
962 }
963
964 return rv;
965}
966
967/*
968 * Limit the number of interim responses we sent back to the client. Otherwise
969 * we suffer from a memory build up. Besides there is NO sense in sending back
970 * an unlimited number of interim responses to the client. Thus if we cross
971 * this limit send back a 502 (Bad Gateway).
972 */
973#ifndef AP_MAX_INTERIM_RESPONSES
974#define AP_MAX_INTERIM_RESPONSES 10
975#endif
976
977static int add_trailers(void *data, const char *key, const char *val)
978{
979 if (val) {
981 }
982 return 1;
983}
984
986{
987 int status;
988
989 /* Send the request body (fully). */
990 switch(req->rb_method) {
991 case RB_SPOOL_CL:
992 case RB_STREAM_CL:
994 status = stream_reqbody(req);
995 break;
996 default:
997 /* Shouldn't happen */
999 break;
1000 }
1001 if (status != OK) {
1002 conn_rec *c = req->r->connection;
1004 APLOGNO(10154) "pass request body failed "
1005 "to %pI (%s) from %s (%s) with status %i",
1006 req->backend->addr,
1007 req->backend->hostname ? req->backend->hostname : "",
1008 c->client_ip, c->remote_host ? c->remote_host : "",
1009 status);
1010 req->backend->close = 1;
1011 }
1012 return status;
1013}
1014
1015static
1017{
1018 apr_pool_t *p = req->p;
1019 request_rec *r = req->r;
1020 conn_rec *c = r->connection;
1021 proxy_worker *worker = req->worker;
1022 proxy_conn_rec *backend = req->backend;
1023 conn_rec *origin = req->origin;
1024 int do_100_continue = req->do_100_continue;
1025 int status;
1026
1027 char *buffer;
1029 const char *buf;
1030 char keepchar;
1031 apr_bucket *e;
1034 int len, backasswards;
1035 int interim_response = 0; /* non-zero whilst interim 1xx responses
1036 * are being read. */
1037 apr_size_t response_field_size = 0;
1038 int pread_len = 0;
1040 int backend_broke = 0;
1041 static const char *hop_by_hop_hdrs[] =
1042 {"Keep-Alive", "Proxy-Authenticate", "TE", "Trailer", "Upgrade", NULL};
1043 int i;
1044 const char *te = NULL;
1045 int original_status = r->status;
1046 int proxy_status = OK;
1047 const char *original_status_line = r->status_line;
1048 const char *proxy_status_line = NULL;
1050 proxy_dir_conf *dconf;
1051
1052 dconf = ap_get_module_config(r->per_dir_config, &proxy_module);
1053
1054 bb = apr_brigade_create(p, c->bucket_alloc);
1055 pass_bb = apr_brigade_create(p, c->bucket_alloc);
1056
1057 /* Only use dynamically sized buffer if user specifies ResponseFieldSize */
1058 if(backend->worker->s->response_field_size_set) {
1059 response_field_size = backend->worker->s->response_field_size;
1060
1061 if (response_field_size != HUGE_STRING_LEN)
1062 buffer = apr_pcalloc(p, response_field_size);
1063 else
1065 }
1066 else {
1067 response_field_size = HUGE_STRING_LEN;
1069 }
1070
1071 /* Setup for 100-Continue timeout if appropriate */
1072 if (do_100_continue && worker->s->ping_timeout_set) {
1074 if (worker->s->ping_timeout != old_timeout) {
1076 rc = apr_socket_timeout_set(backend->sock, worker->s->ping_timeout);
1077 if (rc != APR_SUCCESS) {
1079 "could not set 100-Continue timeout");
1080 }
1081 }
1082 }
1083
1084 /* Get response from the remote server, and pass it up the
1085 * filter chain
1086 */
1087
1088 backend->r = make_fake_req(origin, r);
1089 /* In case anyone needs to know, this is a fake request that is really a
1090 * response.
1091 */
1092 backend->r->proxyreq = PROXYREQ_RESPONSE;
1093 apr_table_setn(r->notes, "proxy-source-port", apr_psprintf(r->pool, "%hu",
1094 origin->local_addr->port));
1095 do {
1097 const char *upgrade = NULL;
1098 int major = 0, minor = 0;
1099 int toclose = 0;
1100
1102
1103 rc = ap_proxygetline(backend->tmp_bb, buffer, response_field_size,
1104 backend->r, 0, &len);
1105 if (len == 0) {
1106 /* handle one potential stray CRLF */
1107 rc = ap_proxygetline(backend->tmp_bb, buffer, response_field_size,
1108 backend->r, 0, &len);
1109 }
1110 if (len <= 0) {
1112 "error reading status line from remote "
1113 "server %s:%d", backend->hostname, backend->port);
1114 if (APR_STATUS_IS_TIMEUP(rc)) {
1115 apr_table_setn(r->notes, "proxy_timedout", "1");
1116 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(01103) "read timeout");
1117 if (do_100_continue) {
1119 "Timeout on 100-Continue");
1120 }
1121 }
1122 /*
1123 * If we are a reverse proxy request shutdown the connection
1124 * WITHOUT ANY response to trigger a retry by the client
1125 * if allowed (as for idempotent requests).
1126 * BUT currently we should not do this if the request is the
1127 * first request on a keepalive connection as browsers like
1128 * seamonkey only display an empty page in this case and do
1129 * not do a retry. We should also not do this on a
1130 * connection which times out; instead handle as
1131 * we normally would handle timeouts
1132 */
1133 if (r->proxyreq == PROXYREQ_REVERSE && c->keepalives &&
1135 apr_bucket *eos;
1136
1138 "Closing connection to client because"
1139 " reading from backend server %s:%d failed."
1140 " Number of keepalives %i", backend->hostname,
1141 backend->port, c->keepalives);
1143 /*
1144 * Add an EOC bucket to signal the ap_http_header_filter
1145 * that it should get out of our way, BUT ensure that the
1146 * EOC bucket is inserted BEFORE an EOS bucket in bb as
1147 * some resource filters like mod_deflate pass everything
1148 * up to the EOS down the chain immediately and sent the
1149 * remainder of the brigade later (or even never). But in
1150 * this case the ap_http_header_filter does not get out of
1151 * our way soon enough.
1152 */
1153 e = ap_bucket_eoc_create(c->bucket_alloc);
1154 eos = APR_BRIGADE_LAST(bb);
1155 while ((APR_BRIGADE_SENTINEL(bb) != eos)
1156 && !APR_BUCKET_IS_EOS(eos)) {
1158 }
1159 if (eos == APR_BRIGADE_SENTINEL(bb)) {
1161 }
1162 else {
1164 }
1166 /* Mark the backend connection for closing */
1167 backend->close = 1;
1168 if (origin->keepalives) {
1169 /* We already had a request on this backend connection and
1170 * might just have run into a keepalive race. Hence we
1171 * think positive and assume that the backend is fine and
1172 * we do not need to signal an error on backend side.
1173 */
1174 return OK;
1175 }
1176 /*
1177 * This happened on our first request on this connection to the
1178 * backend. This indicates something fishy with the backend.
1179 * Return HTTP_INTERNAL_SERVER_ERROR to signal an unrecoverable
1180 * server error. We do not worry about r->status code and a
1181 * possible error response here as the ap_http_outerror_filter
1182 * will fix all of this for us.
1183 */
1185 }
1186 if (!c->keepalives) {
1188 "NOT Closing connection to client"
1189 " although reading from backend server %s:%d"
1190 " failed.",
1191 backend->hostname, backend->port);
1192 }
1194 "Error reading from remote server");
1195 }
1196 /* XXX: Is this a real headers length send from remote? */
1197 backend->worker->s->read += len;
1198
1199 /* Is it an HTTP/1 response?
1200 * This is buggy if we ever see an HTTP/1.10
1201 */
1202 if (apr_date_checkmask(buffer, "HTTP/#.# ###*")) {
1203 major = buffer[5] - '0';
1204 minor = buffer[7] - '0';
1205
1206 /* If not an HTTP/1 message or
1207 * if the status line was > 8192 bytes
1208 */
1209 if ((major != 1) || (len >= response_field_size - 1)) {
1211 apr_pstrcat(p, "Corrupt status line returned "
1212 "by remote server: ", buffer, NULL));
1213 }
1214 backasswards = 0;
1215
1216 keepchar = buffer[12];
1217 buffer[12] = '\0';
1218 proxy_status = atoi(&buffer[9]);
1219 apr_table_setn(r->notes, "proxy-status",
1220 apr_pstrdup(r->pool, &buffer[9]));
1221
1222 if (keepchar != '\0') {
1223 buffer[12] = keepchar;
1224 } else {
1225 /* 2616 requires the space in Status-Line; the origin
1226 * server may have sent one but ap_rgetline_core will
1227 * have stripped it. */
1228 buffer[12] = ' ';
1229 buffer[13] = '\0';
1230 }
1232
1233 /* The status out of the front is the same as the status coming in
1234 * from the back, until further notice.
1235 */
1236 r->status = proxy_status;
1238
1240 "Status from backend: %d", proxy_status);
1241
1242 /* read the headers. */
1243 /* N.B. for HTTP/1.0 clients, we have to fold line-wrapped headers*/
1244 /* Also, take care with headers with multiple occurrences. */
1245
1246 /* First, tuck away all already existing cookies */
1249 "Set-Cookie", NULL);
1250
1251 /* shove the headers direct into r->headers_out */
1252 rc = ap_proxy_read_headers(r, backend->r, buffer, response_field_size,
1253 origin, &pread_len);
1254
1255 if (rc != APR_SUCCESS || r->headers_out == NULL) {
1257 "bad HTTP/%d.%d header returned by %s (%s)",
1258 major, minor, r->uri, r->method);
1259 backend->close = 1;
1260 /*
1261 * ap_send_error relies on a headers_out to be present. we
1262 * are in a bad position here.. so force everything we send out
1263 * to have nothing to do with the incoming packet
1264 */
1267 r->status_line = "bad gateway";
1268 return r->status;
1269 }
1270
1271 /* Now, add in the just read cookies */
1273 "Set-Cookie", NULL);
1274
1275 /* and now load 'em all in */
1277 apr_table_unset(r->headers_out, "Set-Cookie");
1279 r->headers_out,
1280 save_table);
1281 }
1282
1283 /*
1284 * Save a possible Transfer-Encoding header as we need it later for
1285 * ap_http_filter to know where to end.
1286 */
1287 te = apr_table_get(r->headers_out, "Transfer-Encoding");
1288
1289 /* can't have both Content-Length and Transfer-Encoding */
1290 if (te && apr_table_get(r->headers_out, "Content-Length")) {
1291 /*
1292 * 2616 section 4.4, point 3: "if both Transfer-Encoding
1293 * and Content-Length are received, the latter MUST be
1294 * ignored";
1295 *
1296 * To help mitigate HTTP Splitting, unset Content-Length
1297 * and shut down the backend server connection
1298 * XXX: We aught to treat such a response as uncachable
1299 */
1300 apr_table_unset(r->headers_out, "Content-Length");
1302 "server %s:%d returned Transfer-Encoding"
1303 " and Content-Length",
1304 backend->hostname, backend->port);
1305 backend->close = 1;
1306 }
1307
1308 upgrade = apr_table_get(r->headers_out, "Upgrade");
1309 if (proxy_status == HTTP_SWITCHING_PROTOCOLS) {
1310 if (!upgrade || !req->upgrade || (strcasecmp(req->upgrade,
1311 upgrade) != 0)) {
1313 apr_pstrcat(p, "Unexpected Upgrade: ",
1314 upgrade ? upgrade : "n/a",
1315 " (expecting ",
1316 req->upgrade ? req->upgrade
1317 : "n/a", ")",
1318 NULL));
1319 }
1320 backend->close = 1;
1321 }
1322
1323 /* strip connection listed hop-by-hop headers from response */
1325 if (toclose) {
1326 backend->close = 1;
1327 if (toclose < 0) {
1329 "Malformed connection header");
1330 }
1331 }
1332
1333 if ((buf = apr_table_get(r->headers_out, "Content-Type"))) {
1335 }
1336 if (!ap_is_HTTP_INFO(proxy_status)) {
1337 ap_proxy_pre_http_request(origin, backend->r);
1338 }
1339
1340 /* Clear hop-by-hop headers */
1341 for (i=0; hop_by_hop_hdrs[i]; ++i) {
1343 }
1344
1345 /* Delete warnings with wrong date */
1347
1348 /* handle Via header in response */
1349 if (req->sconf->viaopt != via_off
1350 && req->sconf->viaopt != via_block) {
1351 const char *server_name = ap_get_server_name(r);
1352 /* If USE_CANONICAL_NAME_OFF was configured for the proxy virtual host,
1353 * then the server name returned by ap_get_server_name() is the
1354 * origin server name (which does make too much sense with Via: headers)
1355 * so we use the proxy vhost's name instead.
1356 */
1357 if (server_name == r->hostname)
1359 /* create a "Via:" response header entry and merge it */
1361 (req->sconf->viaopt == via_full)
1362 ? apr_psprintf(p, "%d.%d %s%s (%s)",
1366 req->server_portstr,
1368 : apr_psprintf(p, "%d.%d %s%s",
1372 req->server_portstr)
1373 );
1374 }
1375
1376 /* cancel keepalive if HTTP/1.0 or less */
1377 if ((major < 1) || (minor < 1)) {
1378 backend->close = 1;
1379 origin->keepalive = AP_CONN_CLOSE;
1380 }
1381 else {
1382 /*
1383 * Keep track of the number of keepalives we processed on this
1384 * connection.
1385 */
1386 origin->keepalives++;
1387 }
1388
1389 } else {
1390 /* an http/0.9 response */
1391 backasswards = 1;
1392 r->status = proxy_status = 200;
1393 r->status_line = "200 OK";
1394 backend->close = 1;
1395 }
1396
1397 if (ap_is_HTTP_INFO(proxy_status)) {
1398 const char *policy = NULL;
1399
1400 /* RFC2616 tells us to forward this.
1401 *
1402 * OTOH, an interim response here may mean the backend
1403 * is playing sillybuggers. The Client didn't ask for
1404 * it within the defined HTTP/1.1 mechanisms, and if
1405 * it's an extension, it may also be unsupported by us.
1406 *
1407 * There's also the possibility that changing existing
1408 * behaviour here might break something.
1409 *
1410 * So let's make it configurable.
1411 *
1412 * We need to force "r->expecting_100 = 1" for RFC behaviour
1413 * otherwise ap_send_interim_response() does nothing when
1414 * the client did not ask for 100-continue.
1415 *
1416 * 101 Switching Protocol has its own configuration which
1417 * shouldn't be interfered by "proxy-interim-response".
1418 */
1419 if (proxy_status != HTTP_SWITCHING_PROTOCOLS) {
1421 "proxy-interim-response");
1422 }
1424 "HTTP: received interim %d response (policy: %s)",
1425 r->status, policy ? policy : "n/a");
1426 if (!policy
1427 || (!strcasecmp(policy, "RFC")
1428 && (proxy_status != HTTP_CONTINUE
1429 || (r->expecting_100 = 1)))) {
1430 switch (proxy_status) {
1432 AP_DEBUG_ASSERT(upgrade != NULL);
1433 apr_table_setn(r->headers_out, "Connection", "Upgrade");
1434 apr_table_setn(r->headers_out, "Upgrade",
1435 apr_pstrdup(p, upgrade));
1436 break;
1437 }
1439 }
1440 /* FIXME: refine this to be able to specify per-response-status
1441 * policies and maybe also add option to bail out with 502
1442 */
1443 else if (strcasecmp(policy, "Suppress")) {
1445 "undefined proxy interim response policy");
1446 }
1448 }
1449 else {
1450 interim_response = 0;
1451 }
1452
1453 /* If we still do 100-continue (end-to-end or ping), either the
1454 * current response is the expected "100 Continue" and we are done
1455 * with this mode, or this is another interim response and we'll wait
1456 * for the next one, or this is a final response and hence the backend
1457 * did not honor our expectation.
1458 */
1459 if (do_100_continue && (!interim_response
1460 || proxy_status == HTTP_CONTINUE)) {
1461 /* RFC 7231 - Section 5.1.1 - Expect - Requirement for servers
1462 * A server that responds with a final status code before
1463 * reading the entire message body SHOULD indicate in that
1464 * response whether it intends to close the connection or
1465 * continue reading and discarding the request message.
1466 *
1467 * So, if this response is not an interim 100 Continue, we can
1468 * avoid sending the request body if the backend responded with
1469 * "Connection: close" or HTTP < 1.1, and either let the core
1470 * discard it or the caller try another balancer member with the
1471 * same body (given status 503, though not implemented yet).
1472 */
1473 int do_send_body = (proxy_status == HTTP_CONTINUE
1474 || (!toclose && major > 0 && minor > 0));
1475
1476 /* Reset to old timeout iff we've adjusted it. */
1477 if (worker->s->ping_timeout_set) {
1479 }
1480
1482 "HTTP: %s100 continue sent by %pI (%s): "
1483 "%ssending body (response: HTTP/%i.%i %s)",
1484 proxy_status != HTTP_CONTINUE ? "no " : "",
1485 backend->addr,
1486 backend->hostname ? backend->hostname : "",
1487 do_send_body ? "" : "not ",
1488 major, minor, proxy_status_line);
1489
1490 if (do_send_body) {
1492 if (status != OK) {
1493 return status;
1494 }
1495 }
1496 else {
1497 /* If we don't read the client connection any further, since
1498 * there are pending data it should be "Connection: close"d to
1499 * prevent reuse. We don't exactly c->keepalive = AP_CONN_CLOSE
1500 * here though, because error_override or a potential retry on
1501 * another backend could finally read that data and finalize
1502 * the request processing, making keep-alive possible. So what
1503 * we do is leaving r->expecting_100 alone, ap_set_keepalive()
1504 * will do the right thing according to the final response and
1505 * any later update of r->expecting_100.
1506 */
1507 }
1508
1509 /* Once only! */
1510 do_100_continue = 0;
1511 }
1512
1513 if (proxy_status == HTTP_SWITCHING_PROTOCOLS) {
1514 apr_status_t rv;
1517 backend_timeout = -1;
1518
1519 /* If we didn't send the full body yet, do it now */
1520 if (do_100_continue) {
1521 r->expecting_100 = 0;
1523 if (status != OK) {
1524 return status;
1525 }
1526 }
1527
1529 "HTTP: tunneling protocol %s", upgrade);
1530
1531 rv = ap_proxy_tunnel_create(&tunnel, r, origin, upgrade);
1532 if (rv != APR_SUCCESS) {
1534 "can't create tunnel for %s", upgrade);
1536 }
1537
1538 /* Set timeout to the highest configured for client or backend */
1542 tunnel->timeout = backend_timeout;
1543 }
1544 else {
1545 tunnel->timeout = client_timeout;
1546 }
1547
1548 /* Let proxy tunnel forward everything */
1550
1551 /* We are done with both connections */
1552 return DONE;
1553 }
1554
1555 if (interim_response) {
1556 /* Already forwarded above, read next response */
1557 continue;
1558 }
1559
1560 /* Moved the fixups of Date headers and those affected by
1561 * ProxyPassReverse/etc from here to ap_proxy_read_headers
1562 */
1563
1564 /* PR 41646: get HEAD right with ProxyErrorOverride */
1565 if (ap_proxy_should_override(dconf, proxy_status)) {
1566 if (proxy_status == HTTP_UNAUTHORIZED) {
1567 const char *buf;
1568 const char *wa = "WWW-Authenticate";
1569 if ((buf = apr_table_get(r->headers_out, wa))) {
1571 } else {
1573 "origin server sent 401 without "
1574 "WWW-Authenticate header");
1575 }
1576 }
1577
1578 /* clear r->status for override error, otherwise ErrorDocument
1579 * thinks that this is a recursive error, and doesn't find the
1580 * custom error page
1581 */
1582 r->status = HTTP_OK;
1583 /* Discard body, if one is expected */
1584 if (!r->header_only && !AP_STATUS_IS_HEADER_ONLY(proxy_status)) {
1585 const char *tmp;
1586 /* Add minimal headers needed to allow http_in filter
1587 * detecting end of body without waiting for a timeout. */
1588 if ((tmp = apr_table_get(r->headers_out, "Transfer-Encoding"))) {
1589 apr_table_set(backend->r->headers_in, "Transfer-Encoding", tmp);
1590 }
1591 else if ((tmp = apr_table_get(r->headers_out, "Content-Length"))) {
1592 apr_table_set(backend->r->headers_in, "Content-Length", tmp);
1593 }
1594 else if (te) {
1595 apr_table_set(backend->r->headers_in, "Transfer-Encoding", te);
1596 }
1597 ap_discard_request_body(backend->r);
1598 }
1599 /*
1600 * prevent proxy_handler() from treating this as an
1601 * internal error.
1602 */
1603 apr_table_setn(r->notes, "proxy-error-override", "1");
1604 return proxy_status;
1605 }
1606
1607 /* Forward back Upgrade header if it matches the configured one(s), it
1608 * may be an HTTP_UPGRADE_REQUIRED response or some other status where
1609 * Upgrade makes sense to negotiate the protocol by other means.
1610 */
1611 if (upgrade && ap_proxy_worker_can_upgrade(p, worker, upgrade,
1612 (*req->proto == 'w')
1613 ? "WebSocket" : NULL)) {
1614 apr_table_setn(r->headers_out, "Connection", "Upgrade");
1615 apr_table_setn(r->headers_out, "Upgrade", apr_pstrdup(p, upgrade));
1616 }
1617
1618 r->sent_bodyct = 1;
1619 /*
1620 * Is it an HTTP/0.9 response or did we maybe preread the 1st line of
1621 * the response? If so, load the extra data. These are 2 mutually
1622 * exclusive possibilities, that just happen to require very
1623 * similar behavior.
1624 */
1625 if (backasswards || pread_len) {
1627 if (backasswards) {
1628 /*@@@FIXME:
1629 * At this point in response processing of a 0.9 response,
1630 * we don't know yet whether data is binary or not.
1631 * mod_charset_lite will get control later on, so it cannot
1632 * decide on the conversion of this buffer full of data.
1633 * However, chances are that we are not really talking to an
1634 * HTTP/0.9 server, but to some different protocol, therefore
1635 * the best guess IMHO is to always treat the buffer as "text/x":
1636 */
1638 cntr = (apr_ssize_t)len;
1639 }
1640 e = apr_bucket_heap_create(buffer, cntr, NULL, c->bucket_alloc);
1642 }
1643
1644 /* send body - but only if a body is expected */
1645 if ((!r->header_only) && /* not HEAD request */
1646 (proxy_status != HTTP_NO_CONTENT) && /* not 204 */
1647 (proxy_status != HTTP_NOT_MODIFIED)) { /* not 304 */
1649 int finish;
1650
1651 /* We need to copy the output headers and treat them as input
1652 * headers as well. BUT, we need to do this before we remove
1653 * TE, so that they are preserved accordingly for
1654 * ap_http_filter to know where to end.
1655 */
1656 backend->r->headers_in = apr_table_clone(backend->r->pool, r->headers_out);
1657 /*
1658 * Restore Transfer-Encoding header from response if we saved
1659 * one before and there is none left. We need it for the
1660 * ap_http_filter. See above.
1661 */
1662 if (te && !apr_table_get(backend->r->headers_in, "Transfer-Encoding")) {
1663 apr_table_add(backend->r->headers_in, "Transfer-Encoding", te);
1664 }
1665
1666 apr_table_unset(r->headers_out,"Transfer-Encoding");
1667
1668 ap_log_rerror(APLOG_MARK, APLOG_TRACE3, 0, r, "start body send");
1669
1670 /* read the body, pass it to the output filters */
1671
1672 /* Handle the case where the error document is itself reverse
1673 * proxied and was successful. We must maintain any previous
1674 * error status so that an underlying error (eg HTTP_NOT_FOUND)
1675 * doesn't become an HTTP_OK.
1676 */
1680 }
1681
1683 finish = FALSE;
1684 do {
1686 apr_status_t rv;
1687
1688 rv = ap_get_brigade(backend->r->input_filters, bb,
1690 req->sconf->io_buffer_size);
1691
1692 /* ap_get_brigade will return success with an empty brigade
1693 * for a non-blocking read which would block: */
1694 if (mode == APR_NONBLOCK_READ
1695 && (APR_STATUS_IS_EAGAIN(rv)
1696 || (rv == APR_SUCCESS && APR_BRIGADE_EMPTY(bb)))) {
1697 /* flush to the client and switch to blocking mode */
1698 e = apr_bucket_flush_create(c->bucket_alloc);
1701 || c->aborted) {
1702 backend->close = 1;
1703 break;
1704 }
1707 continue;
1708 }
1709 else if (rv == APR_EOF) {
1710 backend->close = 1;
1711 break;
1712 }
1713 else if (rv != APR_SUCCESS) {
1714 /* In this case, we are in real trouble because
1715 * our backend bailed on us. Pass along a 502 error
1716 * error bucket
1717 */
1719 "error reading response");
1723 backend_broke = 1;
1724 backend->close = 1;
1725 break;
1726 }
1727 /* next time try a non-blocking read */
1729
1730 if (!apr_is_empty_table(backend->r->trailers_in)) {
1732 backend->r->trailers_in, NULL);
1733 apr_table_clear(backend->r->trailers_in);
1734 }
1735
1737 backend->worker->s->read += readbytes;
1738#if DEBUGGING
1739 {
1741 "readbytes: %#x", readbytes);
1742 }
1743#endif
1744 /* sanity check */
1745 if (APR_BRIGADE_EMPTY(bb)) {
1746 break;
1747 }
1748
1749 /* Switch the allocator lifetime of the buckets */
1751
1752 /* found the last brigade? */
1754
1755 /* signal that we must leave */
1756 finish = TRUE;
1757
1758 /* the brigade may contain transient buckets that contain
1759 * data that lives only as long as the backend connection.
1760 * Force a setaside so these transient buckets become heap
1761 * buckets that live as long as the request.
1762 */
1763 for (e = APR_BRIGADE_FIRST(pass_bb); e
1765 = APR_BUCKET_NEXT(e)) {
1767 }
1768
1769 /* finally it is safe to clean up the brigade from the
1770 * connection pool, as we have forced a setaside on all
1771 * buckets.
1772 */
1774
1775 /* make sure we release the backend connection as soon
1776 * as we know we are done, so that the backend isn't
1777 * left waiting for a slow client to eventually
1778 * acknowledge the data.
1779 */
1781 backend, r->server);
1782 /* Ensure that the backend is not reused */
1783 req->backend = NULL;
1784
1785 }
1786
1787 /* try send what we read */
1789 || c->aborted) {
1790 /* Ack! Phbtt! Die! User aborted! */
1791 /* Only close backend if we haven't got all from the
1792 * backend. Furthermore if req->backend is NULL it is no
1793 * longer safe to fiddle around with backend as it might
1794 * be already in use by another thread.
1795 */
1796 if (req->backend) {
1797 /* this causes socket close below */
1798 req->backend->close = 1;
1799 }
1800 finish = TRUE;
1801 }
1802
1803 /* make sure we always clean up after ourselves */
1806
1807 } while (!finish);
1808
1809 ap_log_rerror(APLOG_MARK, APLOG_TRACE2, 0, r, "end body send");
1810 }
1811 else {
1812 ap_log_rerror(APLOG_MARK, APLOG_TRACE2, 0, r, "header only");
1813
1814 /* make sure we release the backend connection as soon
1815 * as we know we are done, so that the backend isn't
1816 * left waiting for a slow client to eventually
1817 * acknowledge the data.
1818 */
1820 backend, r->server);
1821 /* Ensure that the backend is not reused */
1822 req->backend = NULL;
1823
1824 /* Pass EOS bucket down the filter chain. */
1825 e = apr_bucket_eos_create(c->bucket_alloc);
1828
1830 }
1832
1833 /* We have to cleanup bb brigade, because buckets inserted to it could be
1834 * created from scpool and this pool can be freed before this brigade. */
1836
1837 /* See define of AP_MAX_INTERIM_RESPONSES for why */
1841 "Too many (%d) interim responses from origin server",
1843 }
1844
1845 /* If our connection with the client is to be aborted, return DONE. */
1846 if (c->aborted || backend_broke) {
1847 return DONE;
1848 }
1849
1850 return OK;
1851}
1852
1853static
1855 proxy_conn_rec *backend)
1856{
1857 ap_proxy_release_connection(scheme, backend, r->server);
1858 return OK;
1859}
1860
1861/*
1862 * This handles http:// URLs, and other URLs using a remote proxy over http
1863 * If proxyhost is NULL, then contact the server directly, otherwise
1864 * go via the proxy.
1865 * Note that if a proxy is used, then URLs other than http: can be accessed,
1866 * also, if we have trouble which is clearly specific to the proxy, then
1867 * we return DECLINED so that we can try another proxy. (Or the direct
1868 * route.)
1869 */
1871 proxy_server_conf *conf,
1872 char *url, const char *proxyname,
1874{
1875 int status;
1876 const char *scheme;
1877 const char *u = url;
1878 proxy_http_req_t *req = NULL;
1879 proxy_conn_rec *backend = NULL;
1880 apr_bucket_brigade *input_brigade = NULL;
1881 int is_ssl = 0;
1882 conn_rec *c = r->connection;
1883 proxy_dir_conf *dconf;
1884 int retry = 0;
1885 char *locurl = url;
1886 int toclose = 0;
1887 /*
1888 * Use a shorter-lived pool to reduce memory usage
1889 * and avoid a memory leak
1890 */
1891 apr_pool_t *p = r->pool;
1892 apr_uri_t *uri;
1893
1894 scheme = get_url_scheme(&u, &is_ssl);
1895 if (!scheme && proxyname && strncasecmp(url, "ftp:", 4) == 0) {
1896 u = url + 4;
1897 scheme = "ftp";
1898 is_ssl = 0;
1899 }
1900 if (!scheme || u[0] != '/' || u[1] != '/' || u[2] == '\0') {
1902 "HTTP: declining URL %s", url);
1903 return DECLINED; /* only interested in HTTP, WS or FTP via proxy */
1904 }
1905 if (is_ssl && !ap_ssl_has_outgoing_handlers()) {
1907 "HTTP: declining URL %s (mod_ssl not configured?)", url);
1908 return DECLINED;
1909 }
1910 ap_log_rerror(APLOG_MARK, APLOG_TRACE1, 0, r, "HTTP: serving URL %s", url);
1911
1912 /* create space for state information */
1913 if ((status = ap_proxy_acquire_connection(scheme, &backend,
1914 worker, r->server)) != OK) {
1915 return status;
1916 }
1917
1918 backend->is_ssl = is_ssl;
1919
1920 req = apr_pcalloc(p, sizeof(*req));
1921 req->p = p;
1922 req->r = r;
1923 req->sconf = conf;
1924 req->worker = worker;
1925 req->backend = backend;
1926 req->proto = scheme;
1927 req->bucket_alloc = c->bucket_alloc;
1928 req->rb_method = RB_INIT;
1929
1930 dconf = ap_get_module_config(r->per_dir_config, &proxy_module);
1931
1932 if (apr_table_get(r->subprocess_env, "force-proxy-request-1.0")) {
1933 req->force10 = 1;
1934 }
1935 else if (*worker->s->upgrade || *req->proto == 'w') {
1936 /* Forward Upgrade header if it matches the configured one(s),
1937 * the default being "WebSocket" for ws[s] schemes.
1938 */
1939 const char *upgrade = apr_table_get(r->headers_in, "Upgrade");
1940 if (upgrade && ap_proxy_worker_can_upgrade(p, worker, upgrade,
1941 (*req->proto == 'w')
1942 ? "WebSocket" : NULL)) {
1943 req->upgrade = upgrade;
1944 }
1945 }
1946
1947 /* We possibly reuse input data prefetched in previous call(s), e.g. for a
1948 * balancer fallback scenario, and in this case the 100 continue settings
1949 * should be consistent between balancer members. If not, we need to ignore
1950 * Proxy100Continue on=>off once we tried to prefetch already, otherwise
1951 * the HTTP_IN filter won't send 100 Continue for us anymore, and we might
1952 * deadlock with the client waiting for each other. Note that off=>on is
1953 * not an issue because in this case r->expecting_100 is false (the 100
1954 * Continue is out already), but we make sure that prefetch will be
1955 * nonblocking to avoid passing more time there.
1956 */
1957 apr_pool_userdata_get((void **)&input_brigade, "proxy-req-input", p);
1958
1959 /* Should we handle end-to-end or ping 100-continue? */
1960 if (!req->force10
1961 && ((r->expecting_100 && (dconf->forward_100_continue || input_brigade))
1962 || PROXY_SHOULD_PING_100_CONTINUE(worker, r))) {
1963 /* Tell ap_proxy_create_hdrbrgd() to preserve/add the Expect header */
1964 apr_table_setn(r->notes, "proxy-100-continue", "1");
1965 req->do_100_continue = 1;
1966 }
1967
1968 /* Should we block while prefetching the body or try nonblocking and flush
1969 * data to the backend ASAP?
1970 */
1971 if (input_brigade
1972 || req->do_100_continue
1974 "proxy-prefetch-nonblocking")) {
1975 req->prefetch_nonblocking = 1;
1976 }
1977
1978 /*
1979 * In the case that we are handling a reverse proxy connection and this
1980 * is not a request that is coming over an already kept alive connection
1981 * with the client, do NOT reuse the connection to the backend, because
1982 * we cannot forward a failure to the client in this case as the client
1983 * does NOT expect this in this situation.
1984 * Yes, this creates a performance penalty.
1985 */
1986 if ((r->proxyreq == PROXYREQ_REVERSE) && (!c->keepalives)
1987 && (apr_table_get(r->subprocess_env, "proxy-initial-not-pooled"))) {
1988 backend->close = 1;
1989 }
1990
1991 /* Step One: Determine Who To Connect To */
1992 uri = apr_palloc(p, sizeof(*uri));
1993 if ((status = ap_proxy_determine_connection(p, r, conf, worker, backend,
1994 uri, &locurl, proxyname,
1996 sizeof(req->server_portstr))))
1997 goto cleanup;
1998
1999 /* The header is always (re-)built since it depends on worker settings,
2000 * but the body can be fetched only once (even partially), so it's saved
2001 * in between proxy_http_handler() calls should we come back here.
2002 */
2004 if (input_brigade == NULL) {
2005 input_brigade = apr_brigade_create(p, req->bucket_alloc);
2006 apr_pool_userdata_setn(input_brigade, "proxy-req-input", NULL, p);
2007 }
2008 req->input_brigade = input_brigade;
2009
2010 /* Prefetch (nonlocking) the request body so to increase the chance to get
2011 * the whole (or enough) body and determine Content-Length vs chunked or
2012 * spooled. By doing this before connecting or reusing the backend, we want
2013 * to minimize the delay between this connection is considered alive and
2014 * the first bytes sent (should the client's link be slow or some input
2015 * filter retain the data). This is a best effort to prevent the backend
2016 * from closing (from under us) what it thinks is an idle connection, hence
2017 * to reduce to the minimum the unavoidable local is_socket_connected() vs
2018 * remote keepalive race condition.
2019 */
2020 if ((status = ap_proxy_http_prefetch(req, uri, locurl)) != OK)
2021 goto cleanup;
2022
2023 /* We need to reset backend->close now, since ap_proxy_http_prefetch() set
2024 * it to disable the reuse of the connection *after* this request (no keep-
2025 * alive), not to close any reusable connection before this request. However
2026 * assure what is expected later by using a local flag and do the right thing
2027 * when ap_proxy_connect_backend() below provides the connection to close.
2028 */
2029 toclose = backend->close;
2030 backend->close = 0;
2031
2032 while (retry < 2) {
2033 if (retry) {
2034 char *newurl = url;
2035
2036 /* Step One (again): (Re)Determine Who To Connect To */
2037 if ((status = ap_proxy_determine_connection(p, r, conf, worker,
2038 backend, uri, &newurl, proxyname, proxyport,
2039 req->server_portstr, sizeof(req->server_portstr))))
2040 break;
2041
2042 /* The code assumes locurl is not changed during the loop, or
2043 * ap_proxy_http_prefetch() would have to be called every time,
2044 * and header_brigade be changed accordingly...
2045 */
2047 }
2048
2049 /* Step Two: Make the Connection */
2050 if (ap_proxy_check_connection(scheme, backend, r->server, 1,
2052 && ap_proxy_connect_backend(scheme, backend, worker,
2053 r->server)) {
2055 "HTTP: failed to make connection to backend: %s",
2056 backend->hostname);
2058 break;
2059 }
2060
2061 /* Step Three: Create conn_rec */
2062 if ((status = ap_proxy_connection_create_ex(scheme, backend, r)) != OK)
2063 break;
2064 req->origin = backend->connection;
2065
2066 /* Don't recycle the connection if prefetch (above) told not to do so */
2067 if (toclose) {
2068 backend->close = 1;
2070 }
2071
2072 /* Step Four: Send the Request
2073 * On the off-chance that we forced a 100-Continue as a
2074 * kinda HTTP ping test, allow for retries
2075 */
2077 if (status != OK) {
2080 "HTTP: 100-Continue failed to %pI (%s:%d)",
2081 backend->addr, backend->hostname, backend->port);
2082 backend->close = 1;
2083 retry++;
2084 continue;
2085 }
2086 break;
2087 }
2088
2089 /* Step Five: Receive the Response... Fall thru to cleanup */
2091
2092 break;
2093 }
2094
2095 /* Step Six: Clean Up */
2096cleanup:
2097 if (req->backend) {
2098 if (status != OK)
2099 req->backend->close = 1;
2100 ap_proxy_http_cleanup(scheme, r, req->backend);
2101 }
2102 return status;
2103}
2104
2105/* post_config hook: */
2107 apr_pool_t *ptemp, server_rec *s)
2108{
2109
2110 /* proxy_http_post_config() will be called twice during startup. So, don't
2111 * set up the static data the 1st time through. */
2113 return OK;
2114 }
2115
2120 "mod_proxy must be loaded for mod_proxy_http");
2121 return !OK;
2122 }
2123
2124 return OK;
2125}
2126
2134
2137 NULL, /* create per-directory config structure */
2138 NULL, /* merge per-directory config structures */
2139 NULL, /* create per-server config structure */
2140 NULL, /* merge per-server config structures */
2141 NULL, /* command apr_table_t */
2142 ap_proxy_http_register_hook/* register hooks */
2143};
2144
Apache Regex defines.
int n
Definition ap_regex.h:278
const char apr_size_t ap_regmatch_t * pmatch
Definition ap_regex.h:172
const char apr_size_t len
Definition ap_regex.h:187
const char apr_size_t nmatch
Definition ap_regex.h:172
#define AP_SERVER_BASEVERSION
Definition ap_release.h:75
#define TRUE
Definition abts.h:38
#define FALSE
Definition abts.h:35
apr_size_t const unsigned char unsigned int unsigned int d
Definition apr_siphash.h:72
apr_bucket * ap_bucket_eoc_create(apr_bucket_alloc_t *list)
Definition eoc_bucket.c:38
static apr_pool_t * pconf
Definition event.c:441
#define ap_get_module_config(v, m)
ap_conf_vector_t * ap_create_request_config(apr_pool_t *p)
Definition config.c:356
void ap_hook_post_config(ap_HOOK_post_config_t *pf, const char *const *aszPre, const char *const *aszSucc, int nOrder)
Definition config.c:105
#define AP_DECLARE_MODULE(foo)
request_rec int int apr_table_t const char * path
request_rec * r
#define HUGE_STRING_LEN
Definition httpd.h:303
#define HTTP_VERSION_MAJOR(number)
Definition httpd.h:271
#define HTTP_VERSION_MINOR(number)
Definition httpd.h:273
#define DEFAULT_HTTPS_PORT
Definition httpd.h:280
#define DECLINED
Definition httpd.h:457
#define OK
Definition httpd.h:456
#define DEFAULT_HTTP_PORT
Definition httpd.h:278
#define DONE
Definition httpd.h:458
#define ap_xlate_proto_to_ascii(x, y)
Definition util_ebcdic.h:80
apr_status_t ap_pass_brigade(ap_filter_t *filter, apr_bucket_brigade *bucket)
apr_status_t ap_get_brigade(ap_filter_t *filter, apr_bucket_brigade *bucket, ap_input_mode_t mode, apr_read_type_e block, apr_off_t readbytes)
#define AP_SQ_MS_CREATE_PRE_CONFIG
Definition http_core.h:1047
int ap_state_query(int query_code)
Definition core.c:5378
const char * ap_get_server_name(request_rec *r)
Definition core.c:1145
#define AP_SQ_MAIN_STATE
Definition http_core.h:1030
#define ap_get_core_module_config(v)
Definition http_core.h:383
apr_socket_t * ap_get_conn_socket(conn_rec *c)
Definition core.c:5202
#define APLOGNO(n)
Definition http_log.h:117
#define APLOG_TRACE4
Definition http_log.h:75
#define APLOG_INFO
Definition http_log.h:70
#define ap_log_rerror
Definition http_log.h:454
#define APLOG_ERR
Definition http_log.h:67
#define APLOG_TRACE3
Definition http_log.h:74
#define ap_log_error
Definition http_log.h:370
#define APLOG_MARK
Definition http_log.h:283
#define APLOG_WARNING
Definition http_log.h:68
#define APLOG_EMERG
Definition http_log.h:64
#define APLOG_TRACE2
Definition http_log.h:73
#define APLOG_TRACE1
Definition http_log.h:72
#define APLOG_DEBUG
Definition http_log.h:71
const unsigned char * buf
Definition util_md5.h:50
#define AP_GETLINE_FOLD
void ap_send_interim_response(request_rec *r, int send_headers)
Definition protocol.c:2316
#define AP_GETLINE_NOSPC_EOL
#define ap_rgetline(s, n, read, r, fold, bb)
void ap_set_content_type(request_rec *r, const char *ct)
int ap_discard_request_body(request_rec *r)
int ap_ssl_has_outgoing_handlers(void)
Definition ssl.c:164
const char apr_port_t port
Definition http_vhost.h:125
const char * host
Definition http_vhost.h:124
#define CRLF_ASCII
Definition httpd.h:737
#define CRLF
Definition httpd.h:724
#define APR_EOF
Definition apr_errno.h:461
#define APR_EINVAL
Definition apr_errno.h:711
#define APR_STATUS_IS_ENOSPC(s)
Definition apr_errno.h:1255
#define APR_STATUS_IS_TIMEUP(s)
Definition apr_errno.h:534
#define APR_STATUS_IS_EAGAIN(s)
Definition apr_errno.h:1272
#define APR_BRIGADE_LAST(b)
#define APR_BRIGADE_PREPEND(a, b)
#define APR_BRIGADE_INSERT_TAIL(b, e)
#define APR_BRIGADE_INSERT_HEAD(b, e)
#define APR_BUCKET_NEXT(e)
apr_read_type_e
Definition apr_buckets.h:57
apr_bucket * e
#define APR_BRIGADE_EMPTY(b)
#define APR_BRIGADE_SENTINEL(b)
#define apr_bucket_delete(e)
#define APR_BUCKET_IS_EOS(e)
#define apr_bucket_setaside(e, p)
#define APR_BRIGADE_FIRST(b)
#define APR_BUCKET_INSERT_BEFORE(a, b)
#define APR_BUCKET_PREV(e)
@ APR_BLOCK_READ
Definition apr_buckets.h:58
@ APR_NONBLOCK_READ
Definition apr_buckets.h:59
apr_dbd_transaction_t int mode
Definition apr_dbd.h:261
const char apr_ssize_t int flags
Definition apr_encode.h:168
const char * url
Definition apr_escape.h:120
#define APR_HOOK_FIRST
Definition apr_hooks.h:301
#define APR_HOOK_MIDDLE
Definition apr_hooks.h:303
#define APR_RETRIEVE_OPTIONAL_FN(name)
apr_redis_t * rc
Definition apr_redis.h:173
const char * uri
Definition apr_uri.h:159
#define HTTP_OK
Definition httpd.h:490
#define HTTP_BAD_REQUEST
Definition httpd.h:508
#define HTTP_SERVICE_UNAVAILABLE
Definition httpd.h:538
#define HTTP_CONTINUE
Definition httpd.h:487
#define AP_STATUS_IS_HEADER_ONLY(x)
Definition httpd.h:574
#define HTTP_NOT_MODIFIED
Definition httpd.h:504
#define HTTP_BAD_GATEWAY
Definition httpd.h:537
#define HTTP_INTERNAL_SERVER_ERROR
Definition httpd.h:535
#define ap_is_HTTP_INFO(x)
Definition httpd.h:548
#define HTTP_FORBIDDEN
Definition httpd.h:511
#define HTTP_SWITCHING_PROTOCOLS
Definition httpd.h:488
#define HTTP_UNAUTHORIZED
Definition httpd.h:509
#define HTTP_NO_CONTENT
Definition httpd.h:494
#define PROXY_CHECK_CONN_EMPTY
Definition mod_proxy.h:1135
int ap_proxy_connect_backend(const char *proxy_function, proxy_conn_rec *conn, proxy_worker *worker, server_rec *s)
int ap_proxy_should_override(proxy_dir_conf *conf, int code)
int ap_proxy_read_input(request_rec *r, proxy_conn_rec *backend, apr_bucket_brigade *input_brigade, apr_off_t max_read)
char * ap_proxy_canon_netloc(apr_pool_t *p, char **const urlp, char **userp, char **passwordp, char **hostp, apr_port_t *port)
Definition proxy_util.c:342
int ap_proxy_release_connection(const char *proxy_function, proxy_conn_rec *conn, server_rec *s)
int ap_proxy_tunnel_run(proxy_tunnel_rec *tunnel)
int ap_proxy_acquire_connection(const char *proxy_function, proxy_conn_rec **conn, proxy_worker *worker, server_rec *s)
int ap_proxy_spool_input(request_rec *r, proxy_conn_rec *backend, apr_bucket_brigade *input_brigade, apr_off_t *bytes_spooled, apr_off_t max_mem_spool)
char * ap_proxy_canonenc_ex(apr_pool_t *p, const char *x, int len, enum enctype t, int flags, int proxyreq)
Definition proxy_util.c:220
void ap_proxy_backend_broke(request_rec *r, apr_bucket_brigade *brigade)
const char * ap_proxy_location_reverse_map(request_rec *r, proxy_dir_conf *conf, const char *url)
Definition proxy_util.c:882
apr_status_t ap_proxy_tunnel_create(proxy_tunnel_rec **tunnel, request_rec *r, conn_rec *c_o, const char *scheme)
apr_status_t ap_proxy_check_connection(const char *scheme, proxy_conn_rec *conn, server_rec *server, unsigned max_blank_lines, int flags)
int ap_proxy_connection_reusable(proxy_conn_rec *conn)
#define PROXY_CANONENC_NOENCODEDSLASHENCODING
Definition mod_proxy.h:81
int ap_proxy_pre_http_request(conn_rec *c, request_rec *r)
Definition proxy_util.c:876
int ap_proxy_worker_can_upgrade(apr_pool_t *p, const proxy_worker *worker, const char *upgrade, const char *dflt)
apr_status_t ap_proxy_buckets_lifetime_transform(request_rec *r, apr_bucket_brigade *from, apr_bucket_brigade *to)
const char *(* ap_proxy_header_reverse_map_fn)(request_rec *, proxy_dir_conf *, const char *)
Definition mod_proxy.h:732
void proxy_hook_canon_handler(proxy_HOOK_canon_handler_t *pf, const char *const *aszPre, const char *const *aszSucc, int nOrder)
Definition mod_proxy.c:3412
int ap_proxy_connection_create_ex(const char *proxy_function, proxy_conn_rec *conn, request_rec *r)
#define PROXY_SHOULD_PING_100_CONTINUE(w, r)
Definition mod_proxy.h:407
int ap_proxy_create_hdrbrgd(apr_pool_t *p, apr_bucket_brigade *header_brigade, request_rec *r, proxy_conn_rec *p_conn, proxy_worker *worker, proxy_server_conf *conf, apr_uri_t *uri, char *url, char *server_portstr, char **old_cl_val, char **old_te_val)
int ap_proxyerror(request_rec *r, int statuscode, const char *message)
Definition proxy_util.c:432
int ap_proxy_determine_connection(apr_pool_t *p, request_rec *r, proxy_server_conf *conf, proxy_worker *worker, proxy_conn_rec *conn, apr_uri_t *uri, char **url, const char *proxyname, apr_port_t proxyport, char *server_portstr, int server_portstr_size)
void proxy_hook_scheme_handler(proxy_HOOK_scheme_handler_t *pf, const char *const *aszPre, const char *const *aszSucc, int nOrder)
Definition mod_proxy.c:3406
const char * ap_proxy_cookie_reverse_map(request_rec *r, proxy_dir_conf *conf, const char *str)
int proxy_run_create_req(request_rec *r, request_rec *pr)
Definition proxy_util.c:92
int ap_proxy_prefetch_input(request_rec *r, proxy_conn_rec *backend, apr_bucket_brigade *input_brigade, apr_read_type_e block, apr_off_t *bytes_read, apr_off_t max_read)
int ap_proxy_pass_brigade(apr_bucket_alloc_t *bucket_alloc, request_rec *r, proxy_conn_rec *p_conn, conn_rec *origin, apr_bucket_brigade *bb, int flush)
@ enc_path
Definition mod_proxy.h:76
#define STANDARD20_MODULE_STUFF
int ap_cstr_casecmp(const char *s1, const char *s2)
Definition util.c:3542
#define ap_strchr_c(s, c)
Definition httpd.h:2353
#define AP_DEBUG_ASSERT(exp)
Definition httpd.h:2283
const char * ap_scan_vchar_obstext(const char *ptr)
Definition util.c:1674
#define PROXYREQ_PROXY
Definition httpd.h:1134
int ap_parse_strict_length(apr_off_t *len, const char *str)
Definition util.c:2683
#define PROXYREQ_REVERSE
Definition httpd.h:1135
ap_regex_t * ap_pregcomp(apr_pool_t *p, const char *pattern, int cflags)
Definition util.c:262
#define PROXYREQ_RESPONSE
Definition httpd.h:1136
@ AP_CONN_CLOSE
Definition httpd.h:1145
apr_size_t size
apr_uint32_t val
Definition apr_atomic.h:66
const char int apr_pool_t * pool
Definition apr_cstr.h:84
#define apr_isspace(c)
Definition apr_lib.h:225
#define apr_tolower(c)
Definition apr_lib.h:231
const char * value
Definition apr_env.h:51
#define APR_SUCCESS
Definition apr_errno.h:225
int apr_status_t
Definition apr_errno.h:44
const char * key
void apr_size_t apr_size_t * bytes_read
void * data
void const char apr_status_t(* cleanup)(void *))
char * buffer
int strcasecmp(const char *a, const char *b)
int strncasecmp(const char *a, const char *b, size_t n)
apr_vformatter_buff_t * c
Definition apr_lib.h:175
apr_uint16_t apr_port_t
apr_interval_time_t apr_pollcb_cb_t func
Definition apr_poll.h:422
#define apr_pool_create(newpool, parent)
Definition apr_pools.h:322
#define apr_pcalloc(p, size)
Definition apr_pools.h:465
const void apr_size_t bytes
Definition apr_random.h:91
const char char ** end
const char * s
Definition apr_strings.h:95
apr_int32_t apr_int32_t apr_int32_t err
int int status
#define APR_RFC822_DATE_LEN
Definition apr_time.h:186
apr_int64_t apr_interval_time_t
Definition apr_time.h:55
apr_int64_t apr_time_t
Definition apr_time.h:45
apr_pool_t * p
Definition md_event.c:32
Proxy Extension Module for Apache.
static void add_te_chunked(apr_pool_t *p, apr_bucket_alloc_t *bucket_alloc, apr_bucket_brigade *header_brigade)
static const char * date_canon(apr_pool_t *p, const char *date)
static int add_trailers(void *data, const char *key, const char *val)
rb_methods
@ RB_SPOOL_CL
@ RB_STREAM_CL
@ RB_INIT
@ RB_STREAM_CHUNKED
static apr_status_t ap_proxy_http_cleanup(const char *scheme, request_rec *r, proxy_conn_rec *backend)
static int ap_proxy_http_prefetch(proxy_http_req_t *req, apr_uri_t *uri, char *url)
static void process_proxy_header(request_rec *r, proxy_dir_conf *c, const char *key, const char *value)
static int proxy_http_post_config(apr_pool_t *pconf, apr_pool_t *plog, apr_pool_t *ptemp, server_rec *s)
static int send_continue_body(proxy_http_req_t *req)
static const char * get_url_scheme(const char **url, int *is_ssl)
#define ZERO_ASCII
static void terminate_headers(proxy_http_req_t *req)
static int stream_reqbody(proxy_http_req_t *req)
static apr_status_t ap_proxy_read_headers(request_rec *r, request_rec *rr, char *buffer, int size, conn_rec *c, int *pread_len)
static int proxy_http_handler(request_rec *r, proxy_worker *worker, proxy_server_conf *conf, char *url, const char *proxyname, apr_port_t proxyport)
static apr_table_t * ap_proxy_clean_warnings(apr_pool_t *p, apr_table_t *headers)
#define MAX_MEM_SPOOL
static int(* ap_proxy_clear_connection_fn)(request_rec *r, apr_table_t *headers)
static int proxy_http_canon(request_rec *r, char *url)
static void ap_proxy_http_register_hook(apr_pool_t *p)
static int ap_proxy_http_process_response(proxy_http_req_t *req)
static apr_status_t ap_proxygetline(apr_bucket_brigade *bb, char *s, int n, request_rec *r, int flags, int *read)
#define AP_MAX_INTERIM_RESPONSES
static int clean_warning_headers(void *data, const char *key, const char *val)
static request_rec * make_fake_req(conn_rec *c, request_rec *r)
static int ap_proxy_http_request(proxy_http_req_t *req)
static ap_regex_t * warn_rx
static void add_cl(apr_pool_t *p, apr_bucket_alloc_t *bucket_alloc, apr_bucket_brigade *header_brigade, const char *cl_val)
static int addit_dammit(void *v, const char *key, const char *val)
return NULL
Definition mod_so.c:359
int i
Definition mod_so.c:347
sconf
Definition mod_so.c:349
static int ap_proxy_clear_connection(request_rec *r, apr_table_t *headers)
static sed_label_t * search(sed_commands_t *commands)
Definition sed0.c:907
char * name
apr_bucket_alloc_t * bucket_alloc
Structure to store things which are per connection.
Definition httpd.h:1152
ap_conn_keepalive_e keepalive
Definition httpd.h:1223
apr_sockaddr_t * local_addr
Definition httpd.h:1162
int keepalives
Definition httpd.h:1226
Per-directory configuration.
Definition http_core.h:527
apr_pool_t * pool
apr_time_t time
apr_table_t * table
const char * hostname
Definition mod_proxy.h:275
apr_bucket_brigade * tmp_bb
Definition mod_proxy.h:290
request_rec * r
Definition mod_proxy.h:271
apr_sockaddr_t * addr
Definition mod_proxy.h:276
apr_socket_t * sock
Definition mod_proxy.h:278
proxy_worker * worker
Definition mod_proxy.h:273
apr_port_t port
Definition mod_proxy.h:282
unsigned int is_ssl
Definition mod_proxy.h:283
unsigned int close
Definition mod_proxy.h:284
conn_rec * connection
Definition mod_proxy.h:270
unsigned int forward_100_continue
Definition mod_proxy.h:252
unsigned int force10
apr_bucket_brigade * header_brigade
unsigned int prefetch_nonblocking
apr_bucket_brigade * input_brigade
const char * proto
request_rec * r
unsigned int do_100_continue
proxy_worker * worker
const char * upgrade
apr_bucket_alloc_t * bucket_alloc
proxy_server_conf * sconf
proxy_conn_rec * backend
enum proxy_server_conf::@32 viaopt
apr_size_t io_buffer_size
Definition mod_proxy.h:177
unsigned int ping_timeout_set
Definition mod_proxy.h:471
apr_size_t response_field_size
Definition mod_proxy.h:489
unsigned int response_field_size_set
Definition mod_proxy.h:490
apr_interval_time_t ping_timeout
Definition mod_proxy.h:455
proxy_worker_shared * s
Definition mod_proxy.h:505
A structure that represents the current request.
Definition httpd.h:845
int status
Definition httpd.h:891
char * uri
Definition httpd.h:1016
apr_table_t * trailers_in
Definition httpd.h:1104
struct ap_filter_t * output_filters
Definition httpd.h:1070
int header_only
Definition httpd.h:875
struct ap_filter_t * proto_input_filters
Definition httpd.h:1079
apr_table_t * notes
Definition httpd.h:985
unsigned expecting_100
Definition httpd.h:951
const char * hostname
Definition httpd.h:883
apr_pool_t * pool
Definition httpd.h:847
char * filename
Definition httpd.h:1018
apr_time_t request_time
Definition httpd.h:886
int proxyreq
Definition httpd.h:873
conn_rec * connection
Definition httpd.h:849
apr_bucket_brigade * kept_body
Definition httpd.h:953
apr_table_t * err_headers_out
Definition httpd.h:981
const struct ap_logconf * log
Definition httpd.h:1054
int proto_num
Definition httpd.h:877
struct ap_filter_t * input_filters
Definition httpd.h:1072
apr_table_t * headers_in
Definition httpd.h:976
request_rec * main
Definition httpd.h:860
apr_table_t * subprocess_env
Definition httpd.h:983
apr_off_t sent_bodyct
Definition httpd.h:929
server_rec * server
Definition httpd.h:851
struct ap_conf_vector_t * per_dir_config
Definition httpd.h:1047
const char * status_line
Definition httpd.h:889
const char * method
Definition httpd.h:900
char * args
Definition httpd.h:1026
apr_table_t * trailers_out
Definition httpd.h:1106
apr_table_t * headers_out
Definition httpd.h:978
A structure to store information for each virtual server.
Definition httpd.h:1322
char * server_hostname
Definition httpd.h:1365
struct ap_conf_vector_t * module_config
Definition httpd.h:1341
struct ap_logconf log
Definition httpd.h:1335
apr_status_t apr_socket_timeout_set(apr_socket_t *sock, apr_interval_time_t t)
Definition sockopt.c:75
apr_status_t apr_socket_timeout_get(apr_socket_t *sock, apr_interval_time_t *t)
Definition sockopt.c:355
apr_status_t apr_rfc822_date(char *date_str, apr_time_t t)
Definition timestr.c:42
@ AP_MODE_READBYTES
Definition util_filter.h:43
typedef int(WSAAPI *apr_winapi_fpt_WSAPoll)(IN OUT LPWSAPOLLFD fdArray