Apache HTTPD
http_filters.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/*
18 * http_filter.c --- HTTP routines which either filters or deal with filters.
19 */
20
21#include "apr.h"
22#include "apr_strings.h"
23#include "apr_buckets.h"
24#include "apr_lib.h"
25#include "apr_signal.h"
26
27#define APR_WANT_STDIO /* for sscanf */
28#define APR_WANT_STRFUNC
29#define APR_WANT_MEMFUNC
30#include "apr_want.h"
31
32#include "util_filter.h"
33#include "ap_config.h"
34#include "httpd.h"
35#include "http_config.h"
36#include "http_core.h"
37#include "http_protocol.h"
38#include "http_main.h"
39#include "http_request.h"
40#include "http_vhost.h"
41#include "http_connection.h"
42#include "http_log.h" /* For errors detected in basic auth common
43 * support code... */
44#include "apr_date.h" /* For apr_date_parse_http and APR_DATE_BAD */
45#include "util_charset.h"
46#include "util_ebcdic.h"
47#include "util_time.h"
48
49#include "mod_core.h"
50
51#if APR_HAVE_STDARG_H
52#include <stdarg.h>
53#endif
54#if APR_HAVE_UNISTD_H
55#include <unistd.h>
56#endif
57
59
60typedef struct http_filter_ctx
61{
68 enum
69 {
70 BODY_NONE, /* streamed data */
71 BODY_LENGTH, /* data constrained by content length */
72 BODY_CHUNK, /* chunk expected */
73 BODY_CHUNK_PART, /* chunk digits */
74 BODY_CHUNK_EXT, /* chunk extension */
75 BODY_CHUNK_CR, /* got space(s) after digits, expect [CR]LF or ext */
76 BODY_CHUNK_LF, /* got CR after digits or ext, expect LF */
77 BODY_CHUNK_DATA, /* data constrained by chunked encoding */
78 BODY_CHUNK_END, /* chunked data terminating CRLF */
79 BODY_CHUNK_END_LF, /* got CR after data, expect LF */
80 BODY_CHUNK_TRAILER /* trailers */
82 unsigned int eos_sent :1,
86
87/* bail out if some error in the HTTP input filter happens */
90 int http_error)
91{
93 apr_bucket_brigade *bb = ctx->bb;
94
96
97 if (f->r->proxyreq == PROXYREQ_RESPONSE) {
98 switch (http_error) {
100 return APR_ENOSPC;
101
103 return APR_INCOMPLETE;
104
106 return APR_ENOTIMPL;
107
108 default:
109 return APR_EGENERAL;
110 }
111 }
112
114 NULL, f->r->pool,
115 f->c->bucket_alloc);
117 e = apr_bucket_eos_create(f->c->bucket_alloc);
119 ctx->eos_sent = 1;
120 /* If chunked encoding / content-length are corrupt, we may treat parts
121 * of this request's body as the next one's headers.
122 * To be safe, disable keep-alive.
123 */
124 f->r->connection->keepalive = AP_CONN_CLOSE;
125 return ap_pass_brigade(f->r->output_filters, bb);
126}
127
138 apr_size_t len, int linelimit, int strict)
139{
140 apr_size_t i = 0;
141
142 while (i < len) {
143 char c = buffer[i];
144
146
147 /* handle CRLF after the chunk */
148 if (ctx->state == BODY_CHUNK_END
149 || ctx->state == BODY_CHUNK_END_LF) {
150 if (c == LF) {
151 if (strict && (ctx->state != BODY_CHUNK_END_LF)) {
152 /*
153 * CR missing before LF.
154 */
155 return APR_EINVAL;
156 }
157 ctx->state = BODY_CHUNK;
158 }
159 else if (c == CR && ctx->state == BODY_CHUNK_END) {
160 ctx->state = BODY_CHUNK_END_LF;
161 }
162 else {
163 /*
164 * CRLF expected.
165 */
166 return APR_EINVAL;
167 }
168 i++;
169 continue;
170 }
171
172 /* handle start of the chunk */
173 if (ctx->state == BODY_CHUNK) {
174 if (!apr_isxdigit(c)) {
175 /*
176 * Detect invalid character at beginning. This also works for
177 * empty chunk size lines.
178 */
179 return APR_EINVAL;
180 }
181 else {
182 ctx->state = BODY_CHUNK_PART;
183 }
184 ctx->remaining = 0;
185 ctx->chunkbits = sizeof(apr_off_t) * 8;
186 ctx->chunk_used = 0;
187 ctx->chunk_bws = 0;
188 }
189
190 if (c == LF) {
191 if (strict && (ctx->state != BODY_CHUNK_LF)) {
192 /*
193 * CR missing before LF.
194 */
195 return APR_EINVAL;
196 }
197 if (ctx->remaining) {
198 ctx->state = BODY_CHUNK_DATA;
199 }
200 else {
201 ctx->state = BODY_CHUNK_TRAILER;
202 }
203 }
204 else if (ctx->state == BODY_CHUNK_LF) {
205 /*
206 * LF expected.
207 */
208 return APR_EINVAL;
209 }
210 else if (c == CR) {
211 ctx->state = BODY_CHUNK_LF;
212 }
213 else if (c == ';') {
214 ctx->state = BODY_CHUNK_EXT;
215 }
216 else if (ctx->state == BODY_CHUNK_EXT) {
217 /*
218 * Control chars (excluding tabs) are invalid.
219 * TODO: more precisely limit input
220 */
221 if (c != '\t' && apr_iscntrl(c)) {
222 return APR_EINVAL;
223 }
224 }
225 else if (c == ' ' || c == '\t') {
226 /* Be lenient up to 10 implied *LWS, a legacy of RFC 2616,
227 * and noted as errata to RFC7230;
228 * https://www.rfc-editor.org/errata_search.php?rfc=7230&eid=4667
229 */
230 ctx->state = BODY_CHUNK_CR;
231 if (++ctx->chunk_bws > 10) {
232 return APR_EINVAL;
233 }
234 }
235 else if (ctx->state == BODY_CHUNK_CR) {
236 /*
237 * ';', CR or LF expected.
238 */
239 return APR_EINVAL;
240 }
241 else if (ctx->state == BODY_CHUNK_PART) {
242 int xvalue;
243
244 /* ignore leading zeros */
245 if (!ctx->remaining && c == '0') {
246 i++;
247 continue;
248 }
249
250 ctx->chunkbits -= 4;
251 if (ctx->chunkbits < 0) {
252 /* overflow */
253 return APR_ENOSPC;
254 }
255
256 if (c >= '0' && c <= '9') {
257 xvalue = c - '0';
258 }
259 else if (c >= 'A' && c <= 'F') {
260 xvalue = c - 'A' + 0xa;
261 }
262 else if (c >= 'a' && c <= 'f') {
263 xvalue = c - 'a' + 0xa;
264 }
265 else {
266 /* bogus character */
267 return APR_EINVAL;
268 }
269
270 ctx->remaining = (ctx->remaining << 4) | xvalue;
271 if (ctx->remaining < 0) {
272 /* overflow */
273 return APR_ENOSPC;
274 }
275 }
276 else {
277 /* Should not happen */
278 return APR_EGENERAL;
279 }
280
281 i++;
282 }
283
284 /* sanity check */
285 ctx->chunk_used += len;
286 if (ctx->chunk_used < 0 || ctx->chunk_used > linelimit) {
287 return APR_ENOSPC;
288 }
289
290 return APR_SUCCESS;
291}
292
294 apr_bucket_brigade *b, int merge)
295{
296 int rv;
297 apr_bucket *e;
298 request_rec *r = f->r;
300 int saved_status = r->status;
301
302 r->status = HTTP_OK;
306
307 if(r->status == HTTP_OK) {
309 e = apr_bucket_eos_create(f->c->bucket_alloc);
311 ctx->eos_sent = 1;
312 rv = APR_SUCCESS;
313 }
314 else {
315 const char *error_notes = apr_table_get(r->notes,
316 "error-notes");
318 "Error while reading HTTP trailer: %i%s%s",
319 r->status, error_notes ? ": " : "",
320 error_notes ? error_notes : "");
321 rv = APR_EINVAL;
322 }
323
324 if(!merge) {
326 }
327 else {
329 r->trailers_in);
330 }
331
332 return rv;
333}
334
335/* This is the HTTP_INPUT filter for HTTP requests and responses from
336 * proxied servers (mod_proxy). It handles chunked and content-length
337 * bodies. This can only be inserted/used after the headers
338 * are successfully parsed.
339 */
343{
344 core_server_config *conf =
345 (core_server_config *) ap_get_module_config(f->r->server->module_config,
346 &core_module);
347 int strict = (conf->http_conformance != AP_HTTP_CONFORMANCE_UNSAFE);
348 apr_bucket *e;
349 http_ctx_t *ctx = f->ctx;
350 apr_status_t rv;
352 int again;
353
354 /* just get out of the way of things we don't want. */
356 return ap_get_brigade(f->next, b, mode, block, readbytes);
357 }
358
359 if (!ctx) {
360 const char *tenc, *lenp;
361 f->ctx = ctx = apr_pcalloc(f->r->pool, sizeof(*ctx));
362 ctx->state = BODY_NONE;
363 ctx->bb = apr_brigade_create(f->r->pool, f->c->bucket_alloc);
364
365 /* LimitRequestBody does not apply to proxied responses.
366 * Consider implementing this check in its own filter.
367 * Would adding a directive to limit the size of proxied
368 * responses be useful?
369 */
370 if (!f->r->proxyreq) {
371 ctx->limit = ap_get_limit_req_body(f->r);
372 }
373 else {
374 ctx->limit = 0;
375 }
376
377 tenc = apr_table_get(f->r->headers_in, "Transfer-Encoding");
378 lenp = apr_table_get(f->r->headers_in, "Content-Length");
379
380 if (tenc) {
381 if (ap_is_chunked(f->r->pool, tenc)) {
382 ctx->state = BODY_CHUNK;
383 }
384 else if (f->r->proxyreq == PROXYREQ_RESPONSE) {
385 /* http://tools.ietf.org/html/draft-ietf-httpbis-p1-messaging-23
386 * Section 3.3.3.3: "If a Transfer-Encoding header field is
387 * present in a response and the chunked transfer coding is not
388 * the final encoding, the message body length is determined by
389 * reading the connection until it is closed by the server."
390 */
392 "Unknown Transfer-Encoding: %s; "
393 "using read-until-close", tenc);
394 tenc = NULL;
395 }
396 else {
397 /* Something that isn't a HTTP request, unless some future
398 * edition defines new transfer encodings, is unsupported.
399 */
401 "Unknown Transfer-Encoding: %s", tenc);
403 }
404 lenp = NULL;
405 }
406 if (lenp) {
407 ctx->state = BODY_LENGTH;
408
409 /* Protects against over/underflow, non-digit chars in the
410 * string, leading plus/minus signs, trailing characters and
411 * a negative number.
412 */
413 if (!ap_parse_strict_length(&ctx->remaining, lenp)) {
414 ctx->remaining = 0;
416 "Invalid Content-Length");
417
419 }
420
421 /* If we have a limit in effect and we know the C-L ahead of
422 * time, stop it here if it is invalid.
423 */
424 if (ctx->limit && ctx->limit < ctx->remaining) {
426 "Requested content-length of %" APR_OFF_T_FMT
427 " is larger than the configured limit"
428 " of %" APR_OFF_T_FMT, ctx->remaining, ctx->limit);
430 }
431 }
432
433 /* If we don't have a request entity indicated by the headers, EOS.
434 * (BODY_NONE is a valid intermediate state due to trailers,
435 * but it isn't a valid starting state.)
436 *
437 * RFC 2616 Section 4.4 note 5 states that connection-close
438 * is invalid for a request entity - request bodies must be
439 * denoted by C-L or T-E: chunked.
440 *
441 * Note that since the proxy uses this filter to handle the
442 * proxied *response*, proxy responses MUST be exempt.
443 */
444 if (ctx->state == BODY_NONE && f->r->proxyreq != PROXYREQ_RESPONSE) {
445 e = apr_bucket_eos_create(f->c->bucket_alloc);
447 ctx->eos_sent = 1;
448 return APR_SUCCESS;
449 }
450 }
451
452 /* Since we're about to read data, send 100-Continue if needed.
453 * Only valid on chunked and C-L bodies where the C-L is > 0.
454 *
455 * If the read is to be nonblocking though, the caller may not want to
456 * handle this just now (e.g. mod_proxy_http), and is prepared to read
457 * nothing if the client really waits for 100 continue, so we don't
458 * send it now and wait for later blocking read.
459 *
460 * In any case, even if r->expecting remains set at the end of the
461 * request handling, ap_set_keepalive() will finally do the right
462 * thing (i.e. "Connection: close" the connection).
463 */
464 if (block == APR_BLOCK_READ
465 && (ctx->state == BODY_CHUNK
466 || (ctx->state == BODY_LENGTH && ctx->remaining > 0))
467 && f->r->expecting_100 && f->r->proto_num >= HTTP_VERSION(1,1)
468 && !(ctx->eos_sent || f->r->eos_sent || f->r->bytes_sent)) {
469 if (!ap_is_HTTP_SUCCESS(f->r->status)) {
470 ctx->state = BODY_NONE;
471 ctx->eos_sent = 1; /* send EOS below */
472 }
473 else if (!ctx->seen_data) {
474 int saved_status = f->r->status;
475 const char *saved_status_line = f->r->status_line;
476 f->r->status = HTTP_CONTINUE;
477 f->r->status_line = NULL;
479 AP_DEBUG_ASSERT(!f->r->expecting_100);
480 f->r->status_line = saved_status_line;
481 f->r->status = saved_status;
482 }
483 else {
484 /* https://tools.ietf.org/html/rfc7231#section-5.1.1
485 * A server MAY omit sending a 100 (Continue) response if it
486 * has already received some or all of the message body for
487 * the corresponding request [...]
488 */
489 f->r->expecting_100 = 0;
490 }
491 }
492
493 /* sanity check in case we're read twice */
494 if (ctx->eos_sent) {
495 e = apr_bucket_eos_create(f->c->bucket_alloc);
497 return APR_SUCCESS;
498 }
499
500 do {
502 again = 0; /* until further notice */
503
504 /* read and handle the brigade */
505 switch (ctx->state) {
506 case BODY_CHUNK:
507 case BODY_CHUNK_PART:
508 case BODY_CHUNK_EXT:
509 case BODY_CHUNK_CR:
510 case BODY_CHUNK_LF:
511 case BODY_CHUNK_END:
512 case BODY_CHUNK_END_LF: {
513
514 rv = ap_get_brigade(f->next, b, AP_MODE_GETLINE, block, 0);
515
516 /* for timeout */
518 && ((rv == APR_SUCCESS && APR_BRIGADE_EMPTY(b))
519 || (APR_STATUS_IS_EAGAIN(rv)))) {
520 return APR_EAGAIN;
521 }
522
523 if (rv == APR_EOF) {
524 return APR_INCOMPLETE;
525 }
526
527 if (rv != APR_SUCCESS) {
528 return rv;
529 }
530
532 while (e != APR_BRIGADE_SENTINEL(b)) {
533 const char *buffer;
535
537 int parsing = 0;
538
540 if (rv == APR_SUCCESS) {
541 parsing = 1;
542 if (len > 0) {
543 ctx->seen_data = 1;
544 }
546 f->r->server->limit_req_fieldsize, strict);
547 }
548 if (rv != APR_SUCCESS) {
550 "Error reading/parsing chunk %s ",
551 (APR_ENOSPC == rv) ? "(overflow)" : "");
552 if (parsing) {
553 if (rv != APR_ENOSPC) {
555 }
557 }
558 return rv;
559 }
560 }
561
564 }
565 again = 1; /* come around again */
566
567 if (ctx->state == BODY_CHUNK_TRAILER) {
568 /* Treat UNSET as DISABLE - trailers aren't merged by default */
569 return read_chunked_trailers(ctx, f, b,
571 }
572
573 break;
574 }
575 case BODY_NONE:
576 case BODY_LENGTH:
577 case BODY_CHUNK_DATA: {
578
579 /* Ensure that the caller can not go over our boundary point. */
580 if (ctx->state != BODY_NONE && ctx->remaining < readbytes) {
581 readbytes = ctx->remaining;
582 }
583 if (readbytes > 0) {
585
586 rv = ap_get_brigade(f->next, b, mode, block, readbytes);
587
588 /* for timeout */
590 && ((rv == APR_SUCCESS && APR_BRIGADE_EMPTY(b))
591 || (APR_STATUS_IS_EAGAIN(rv)))) {
592 return APR_EAGAIN;
593 }
594
595 if (rv == APR_EOF && ctx->state != BODY_NONE
596 && ctx->remaining > 0) {
597 return APR_INCOMPLETE;
598 }
599
600 if (rv != APR_SUCCESS) {
601 return rv;
602 }
603
604 /* How many bytes did we just read? */
606 if (totalread > 0) {
607 ctx->seen_data = 1;
608 }
609
610 /* If this happens, we have a bucket of unknown length. Die because
611 * it means our assumptions have changed. */
613
614 if (ctx->state != BODY_NONE) {
615 ctx->remaining -= totalread;
616 if (ctx->remaining > 0) {
618 if (APR_BUCKET_IS_EOS(e)) {
620 return APR_INCOMPLETE;
621 }
622 }
623 else if (ctx->state == BODY_CHUNK_DATA) {
624 /* next chunk please */
625 ctx->state = BODY_CHUNK_END;
626 ctx->chunk_used = 0;
627 }
628 }
629
630 /* We have a limit in effect. */
631 if (ctx->limit) {
632 /* FIXME: Note that we might get slightly confused on
633 * chunked inputs as we'd need to compensate for the chunk
634 * lengths which may not really count. This seems to be up
635 * for interpretation.
636 */
637 ctx->limit_used += totalread;
638 if (ctx->limit < ctx->limit_used) {
640 APLOGNO(01591) "Read content length of "
641 "%" APR_OFF_T_FMT " is larger than the "
642 "configured limit of %" APR_OFF_T_FMT,
643 ctx->limit_used, ctx->limit);
644 return bail_out_on_error(ctx, f,
646 }
647 }
648 }
649
650 /* If we have no more bytes remaining on a C-L request,
651 * save the caller a round trip to discover EOS.
652 */
653 if (ctx->state == BODY_LENGTH && ctx->remaining == 0) {
654 e = apr_bucket_eos_create(f->c->bucket_alloc);
656 ctx->eos_sent = 1;
657 }
658
659 break;
660 }
661 case BODY_CHUNK_TRAILER: {
662
663 rv = ap_get_brigade(f->next, b, mode, block, readbytes);
664
665 /* for timeout */
667 && ((rv == APR_SUCCESS && APR_BRIGADE_EMPTY(b))
668 || (APR_STATUS_IS_EAGAIN(rv)))) {
669 return APR_EAGAIN;
670 }
671
672 if (rv != APR_SUCCESS) {
673 return rv;
674 }
675
676 break;
677 }
678 default: {
679 /* Should not happen */
681 "Unexpected body state (%i)", (int)ctx->state);
682 return APR_EGENERAL;
683 }
684 }
685
686 } while (again);
687
688 return APR_SUCCESS;
689}
690
695
696/* check a single header, to be used with apr_table_do() */
698 const char *name, const char **val)
699{
700 const char *pos, *end;
701 char *dst = NULL;
702
703 if (name[0] == '\0') {
705 "Empty response header name, aborting request");
706 return 0;
707 }
708
709 if (ctx->strict) {
711 }
712 else {
714 }
715 if (*end) {
717 "Response header name '%s' contains invalid "
718 "characters, aborting request",
719 name);
720 return 0;
721 }
722
723 for (pos = *val; *pos; pos = end) {
725 if (*end) {
726 if (end[0] != CR || end[1] != LF || (end[2] != ' ' &&
727 end[2] != '\t')) {
729 "Response header '%s' value of '%s' contains "
730 "invalid characters, aborting request",
731 name, pos);
732 return 0;
733 }
734 if (!dst) {
735 *val = dst = apr_palloc(ctx->r->pool, strlen(*val) + 1);
736 }
737 }
738 if (dst) {
739 memcpy(dst, pos, end - pos);
740 dst += end - pos;
741 if (*end) {
742 /* skip folding and replace with a single space */
743 end += 3 + strspn(end + 3, "\t ");
744 *dst++ = ' ';
745 }
746 }
747 }
748 if (dst) {
749 *dst = '\0';
750 }
751 return 1;
752}
753
755{
756 const apr_array_header_t *headers = apr_table_elts(t);
757 apr_table_entry_t *header;
758 int i;
759
760 for (i = 0; i < headers->nelts; ++i) {
761 header = &APR_ARRAY_IDX(headers, i, apr_table_entry_t);
762 if (!header->key) {
763 continue;
764 }
765 if (!check_header(ctx, header->key, (const char **)&header->val)) {
766 return 0;
767 }
768 }
769 return 1;
770}
771
777{
778 struct check_header_ctx ctx;
779 core_server_config *conf =
781 const char *val;
782
783 if ((val = apr_table_get(r->headers_out, "Transfer-Encoding"))) {
784 if (apr_table_get(r->headers_out, "Content-Length")) {
785 apr_table_unset(r->headers_out, "Content-Length");
787 }
788 if (!ap_is_chunked(r->pool, val)) {
790 return 0;
791 }
792 }
793
794 ctx.r = r;
795 ctx.strict = (conf->http_conformance != AP_HTTP_CONFORMANCE_UNSAFE);
798}
799
801{
802 void *check = NULL;
803 apr_pool_userdata_get(&check, "check_headers_recursion", r->pool);
804 if (check) {
805 return 1;
806 }
807 apr_pool_userdata_setn("true", "check_headers_recursion", NULL, r->pool);
808 return 0;
809}
810
815
816/* Send a single HTTP header field to the client. Note that this function
817 * is used in calls to apr_table_do(), so don't change its interface.
818 * It returns true unless there was a write error of some kind.
819 */
821 const char *fieldname, const char *fieldval)
822{
823#if APR_CHARSET_EBCDIC
824 char *headfield;
826
828 len = strlen(headfield);
829
832#else
833 struct iovec vec[4];
834 struct iovec *v = vec;
835 v->iov_base = (void *)fieldname;
836 v->iov_len = strlen(fieldname);
837 v++;
838 v->iov_base = ": ";
839 v->iov_len = sizeof(": ") - 1;
840 v++;
841 v->iov_base = (void *)fieldval;
842 v->iov_len = strlen(fieldval);
843 v++;
844 v->iov_base = CRLF;
845 v->iov_len = sizeof(CRLF) - 1;
846 apr_brigade_writev(h->bb, NULL, NULL, vec, 4);
847#endif /* !APR_CHARSET_EBCDIC */
848 return 1;
849}
850
851/* This routine is called by apr_table_do and merges all instances of
852 * the passed field values into a single array that will be further
853 * processed by some later routine. Originally intended to help split
854 * and recombine multiple Vary fields, though it is generic to any field
855 * consisting of comma/space-separated tokens.
856 */
857static int uniq_field_values(void *d, const char *key, const char *val)
858{
860 char *start;
861 char *e;
862 char **strpp;
863 int i;
864
866
868
869 do {
870 /* Find a non-empty fieldname */
871
872 while (*e == ',' || apr_isspace(*e)) {
873 ++e;
874 }
875 if (*e == '\0') {
876 break;
877 }
878 start = e;
879 while (*e != '\0' && *e != ',' && !apr_isspace(*e)) {
880 ++e;
881 }
882 if (*e != '\0') {
883 *e++ = '\0';
884 }
885
886 /* Now add it to values if it isn't already represented.
887 * Could be replaced by a ap_array_strcasecmp() if we had one.
888 */
889 for (i = 0, strpp = (char **) values->elts; i < values->nelts;
890 ++i, ++strpp) {
891 if (*strpp && ap_cstr_casecmp(*strpp, start) == 0) {
892 break;
893 }
894 }
895 if (i == values->nelts) { /* if not found */
896 *(char **)apr_array_push(values) = start;
897 }
898 } while (*e != '\0');
899
900 return 1;
901}
902
903/*
904 * Since some clients choke violently on multiple Vary fields, or
905 * Vary fields with duplicate tokens, combine any multiples and remove
906 * any duplicates.
907 */
909{
911
912 varies = apr_array_make(r->pool, 5, sizeof(char *));
913
914 /* Extract all Vary fields from the headers_out, separate each into
915 * its comma-separated fieldname values, and then add them to varies
916 * if not already present in the array.
917 */
919
920 /* If we found any, replace old Vary fields with unique-ified value */
921
922 if (varies->nelts > 0) {
925 }
926}
927
928/* Send a request's HTTP response headers to the client.
929 */
931 const request_rec *r)
932{
933 const apr_array_header_t *elts;
936 struct iovec *vec;
937 struct iovec *vec_next;
938
940 if (elts->nelts == 0) {
941 return APR_SUCCESS;
942 }
943 t_elt = (const apr_table_entry_t *)(elts->elts);
944 t_end = t_elt + elts->nelts;
945 vec = (struct iovec *)apr_palloc(h->pool, 4 * elts->nelts *
946 sizeof(struct iovec));
947 vec_next = vec;
948
949 /* For each field, generate
950 * name ": " value CRLF
951 */
952 do {
953 vec_next->iov_base = (void*)(t_elt->key);
954 vec_next->iov_len = strlen(t_elt->key);
955 vec_next++;
956 vec_next->iov_base = ": ";
957 vec_next->iov_len = sizeof(": ") - 1;
958 vec_next++;
959 vec_next->iov_base = (void*)(t_elt->val);
960 vec_next->iov_len = strlen(t_elt->val);
961 vec_next++;
962 vec_next->iov_base = CRLF;
963 vec_next->iov_len = sizeof(CRLF) - 1;
964 vec_next++;
965 t_elt++;
966 } while (t_elt < t_end);
967
968 if (APLOGrtrace4(r)) {
969 t_elt = (const apr_table_entry_t *)(elts->elts);
970 do {
971 ap_log_rerror(APLOG_MARK, APLOG_TRACE4, 0, r, " %s: %s",
973 ap_escape_logitem(r->pool, t_elt->val));
974 t_elt++;
975 } while (t_elt < t_end);
976 }
977
978#if APR_CHARSET_EBCDIC
979 {
981 char *tmp = apr_pstrcatv(r->pool, vec, vec_next - vec, &len);
983 return apr_brigade_write(h->bb, NULL, NULL, tmp, len);
984 }
985#else
986 return apr_brigade_writev(h->bb, NULL, NULL, vec, vec_next - vec);
987#endif
988}
989
990/* Confirm that the status line is well-formed and matches r->status.
991 * If they don't match, a filter may have negated the status line set by a
992 * handler.
993 * Zap r->status_line if bad.
994 */
996{
997 char *end;
998
999 if (r->status_line) {
1000 int len = strlen(r->status_line);
1001 if (len < 3
1002 || apr_strtoi64(r->status_line, &end, 10) != r->status
1003 || (end - 3) != r->status_line
1004 || (len >= 4 && ! apr_isspace(r->status_line[3]))) {
1005 r->status_line = NULL;
1006 return APR_EGENERAL;
1007 }
1008 /* Since we passed the above check, we know that length three
1009 * is equivalent to only a 3 digit numeric http status.
1010 * RFC2616 mandates a trailing space, let's add it.
1011 */
1012 if (len == 3) {
1014 return APR_EGENERAL;
1015 }
1016 return APR_SUCCESS;
1017 }
1018 return APR_EGENERAL;
1019}
1020
1021/*
1022 * Determine the protocol to use for the response. Potentially downgrade
1023 * to HTTP/1.0 in some situations and/or turn off keepalives.
1024 *
1025 * also prepare r->status_line.
1026 */
1028 const char **protocol)
1029{
1030 apr_status_t rv;
1031
1032 if (r->assbackwards) {
1033 /* no such thing as a response protocol */
1034 return;
1035 }
1036
1037 rv = validate_status_line(r);
1038
1039 if (!r->status_line) {
1041 } else if (rv != APR_SUCCESS) {
1042 /* Status line is OK but our own reason phrase
1043 * would be preferred if defined
1044 */
1045 const char *tmp = ap_get_status_line(r->status);
1046 if (!strncmp(tmp, r->status_line, 3)) {
1047 r->status_line = tmp;
1048 }
1049 }
1050
1051 /* Note that we must downgrade before checking for force responses. */
1052 if (r->proto_num > HTTP_VERSION(1,0)
1053 && apr_table_get(r->subprocess_env, "downgrade-1.0")) {
1054 r->proto_num = HTTP_VERSION(1,0);
1055 }
1056
1057 /* kludge around broken browsers when indicated by force-response-1.0
1058 */
1059 if (r->proto_num == HTTP_VERSION(1,0)
1060 && apr_table_get(r->subprocess_env, "force-response-1.0")) {
1061 *protocol = "HTTP/1.0";
1063 }
1064 else {
1066 }
1067
1068}
1069
1070/* fill "bb" with a barebones/initial HTTP response header */
1072 const char *protocol)
1073{
1074 char *date = NULL;
1075 const char *proxy_date = NULL;
1076 const char *server = NULL;
1077 const char *us = ap_get_server_banner();
1079 struct iovec vec[4];
1080
1081 if (r->assbackwards) {
1082 /* there are no headers to send */
1083 return;
1084 }
1085
1086 /* Output the HTTP/1.x Status-Line and the Date and Server fields */
1087
1088 vec[0].iov_base = (void *)protocol;
1089 vec[0].iov_len = strlen(protocol);
1090 vec[1].iov_base = (void *)" ";
1091 vec[1].iov_len = sizeof(" ") - 1;
1092 vec[2].iov_base = (void *)(r->status_line);
1093 vec[2].iov_len = strlen(r->status_line);
1094 vec[3].iov_base = (void *)CRLF;
1095 vec[3].iov_len = sizeof(CRLF) - 1;
1096#if APR_CHARSET_EBCDIC
1097 {
1098 char *tmp;
1100 tmp = apr_pstrcatv(r->pool, vec, 4, &len);
1102 apr_brigade_write(bb, NULL, NULL, tmp, len);
1103 }
1104#else
1105 apr_brigade_writev(bb, NULL, NULL, vec, 4);
1106#endif
1107
1108 h.pool = r->pool;
1109 h.bb = bb;
1110
1111 /*
1112 * keep the set-by-proxy server and date headers, otherwise
1113 * generate a new server header / date header
1114 */
1115 if (r->proxyreq != PROXYREQ_NONE) {
1117 if (!proxy_date) {
1118 /*
1119 * proxy_date needs to be const. So use date for the creation of
1120 * our own Date header and pass it over to proxy_date later to
1121 * avoid a compiler warning.
1122 */
1125 }
1126 server = apr_table_get(r->headers_out, "Server");
1127 }
1128 else {
1131 }
1132
1133 form_header_field(&h, "Date", proxy_date ? proxy_date : date );
1134
1135 if (!server && *us)
1136 server = us;
1137 if (server)
1138 form_header_field(&h, "Server", server);
1139
1140 if (APLOGrtrace3(r)) {
1142 "Response sent with status %d%s",
1143 r->status,
1144 APLOGrtrace4(r) ? ", headers:" : "");
1145
1146 /*
1147 * Date and Server are less interesting, use TRACE5 for them while
1148 * using TRACE4 for the other headers.
1149 */
1150 ap_log_rerror(APLOG_MARK, APLOG_TRACE5, 0, r, " Date: %s",
1151 proxy_date ? proxy_date : date );
1152 if (server)
1153 ap_log_rerror(APLOG_MARK, APLOG_TRACE5, 0, r, " Server: %s",
1154 server);
1155 }
1156
1157
1158 /* unset so we don't send them again */
1159 apr_table_unset(r->headers_out, "Date"); /* Avoid bogosity */
1160 if (server) {
1161 apr_table_unset(r->headers_out, "Server");
1162 }
1163}
1164
1172
1174{
1175 char crlf[] = CRLF;
1177
1178 buflen = strlen(crlf);
1181}
1182
1184{
1185 core_server_config *conf;
1186 int rv;
1189 apr_bucket *b;
1190 int body;
1191 char *bodyread = NULL, *bodyoff;
1192 apr_size_t bodylen = 0;
1194 long res = -1; /* init to avoid gcc -Wall warning */
1195
1196 if (r->method_number != M_TRACE) {
1197 return DECLINED;
1198 }
1199
1200 /* Get the original request */
1201 while (r->prev) {
1202 r = r->prev;
1203 }
1205
1206 if (conf->trace_enable == AP_TRACE_DISABLE) {
1207 apr_table_setn(r->notes, "error-notes",
1208 "TRACE denied by server configuration");
1210 }
1211
1212 if (conf->trace_enable == AP_TRACE_EXTENDED)
1213 /* XXX: should be = REQUEST_CHUNKED_PASS */
1215 else
1216 body = REQUEST_NO_BODY;
1217
1218 if ((rv = ap_setup_client_block(r, body))) {
1220 apr_table_setn(r->notes, "error-notes",
1221 "TRACE with a request body is not allowed");
1222 return rv;
1223 }
1224
1226
1227 if (r->remaining > 0) {
1228 if (r->remaining > 65536) {
1229 apr_table_setn(r->notes, "error-notes",
1230 "Extended TRACE request bodies cannot exceed 64k\n");
1232 }
1233 /* always 32 extra bytes to catch chunk header exceptions */
1234 bodybuf = (apr_size_t)r->remaining + 32;
1235 }
1236 else {
1237 /* Add an extra 8192 for chunk headers */
1238 bodybuf = 73730;
1239 }
1240
1242
1243 /* only while we have enough for a chunked header */
1244 while ((!bodylen || bodybuf >= 32) &&
1246 bodylen += res;
1247 bodybuf -= res;
1248 bodyoff += res;
1249 }
1250 if (res > 0 && bodybuf < 32) {
1251 /* discard_rest_of_request_body into our buffer */
1252 while (ap_get_client_block(r, bodyread, bodylen) > 0)
1253 ;
1254 apr_table_setn(r->notes, "error-notes",
1255 "Extended TRACE request bodies cannot exceed 64k\n");
1257 }
1258
1259 if (res < 0) {
1260 return HTTP_BAD_REQUEST;
1261 }
1262 }
1263
1264 ap_set_content_type_ex(r, "message/http", 1);
1265
1266 /* Now we recreate the request, and echo it back */
1267
1269#if APR_CHARSET_EBCDIC
1270 {
1271 char *tmp;
1273 len = strlen(r->the_request);
1274 tmp = apr_pmemdup(r->pool, r->the_request, len);
1277 }
1278#else
1280#endif
1281 h.pool = r->pool;
1282 h.bb = bb;
1283 apr_table_do((int (*) (void *, const char *, const char *))
1284 form_header_field, (void *) &h, r->headers_in, NULL);
1286
1287 /* If configured to accept a body, echo the body */
1288 if (bodylen) {
1290 r->pool, bb->bucket_alloc);
1292 }
1293
1295
1296 return DONE;
1297}
1298
1302
1305{
1306 request_rec *r = f->r;
1307 conn_rec *c = r->connection;
1308 const char *clheader;
1309 int header_only = (r->header_only || AP_STATUS_IS_HEADER_ONLY(r->status));
1310 const char *protocol = NULL;
1311 apr_bucket *e;
1314 header_filter_ctx *ctx = f->ctx;
1315 const char *ctype;
1318 int recursive_error = 0;
1319
1321
1322 if (!ctx) {
1323 ctx = f->ctx = apr_pcalloc(r->pool, sizeof(header_filter_ctx));
1324 }
1325 else if (ctx->headers_sent) {
1326 /* Eat body if response must not have one. */
1327 if (header_only) {
1328 /* Still next filters may be waiting for EOS, so pass it (alone)
1329 * when encountered and be done with this filter.
1330 */
1331 e = APR_BRIGADE_LAST(b);
1337 rv = ap_pass_brigade(f->next, b);
1338 }
1340 return rv;
1341 }
1342 }
1343
1344 for (e = APR_BRIGADE_FIRST(b);
1346 e = APR_BUCKET_NEXT(e))
1347 {
1348 if (AP_BUCKET_IS_ERROR(e) && !eb) {
1349 eb = e->data;
1350 continue;
1351 }
1352 /*
1353 * If we see an EOC bucket it is a signal that we should get out
1354 * of the way doing nothing.
1355 */
1356 if (AP_BUCKET_IS_EOC(e)) {
1358 return ap_pass_brigade(f->next, b);
1359 }
1360 }
1361
1362 if (!ctx->headers_sent && !check_headers(r)) {
1363 /* We may come back here from ap_die() below,
1364 * so clear anything from this response.
1365 */
1370 r->clength = r->chunked = 0;
1372
1373 /* Don't recall ap_die() if we come back here (from its own internal
1374 * redirect or error response), otherwise we can end up in infinite
1375 * recursion; better fall through with 500, minimal headers and an
1376 * empty body (EOS only).
1377 */
1378 if (!check_headers_recursion(r)) {
1380 return AP_FILTER_ERROR;
1381 }
1383 e = ap_bucket_eoc_create(c->bucket_alloc);
1385 e = apr_bucket_eos_create(c->bucket_alloc);
1388 recursive_error = 1;
1389 }
1390 else if (eb) {
1391 int status;
1392 status = eb->status;
1394 ap_die(status, r);
1395 return AP_FILTER_ERROR;
1396 }
1397
1398 if (r->assbackwards) {
1399 r->sent_bodyct = 1;
1401 rv = ap_pass_brigade(f->next, b);
1402 goto out;
1403 }
1404
1405 /*
1406 * Now that we are ready to send a response, we need to combine the two
1407 * header field tables into a single table. If we don't do this, our
1408 * later attempts to set or unset a given fieldname might be bypassed.
1409 */
1412 r->headers_out);
1414 }
1415
1416 /*
1417 * Remove the 'Vary' header field if the client can't handle it.
1418 * Since this will have nasty effects on HTTP/1.1 caches, force
1419 * the response into HTTP/1.0 mode.
1420 *
1421 * Note: the force-response-1.0 should come before the call to
1422 * basic_http_header_check()
1423 */
1424 if (apr_table_get(r->subprocess_env, "force-no-vary") != NULL) {
1425 apr_table_unset(r->headers_out, "Vary");
1426 r->proto_num = HTTP_VERSION(1,0);
1427 apr_table_setn(r->subprocess_env, "force-response-1.0", "1");
1428 }
1429 else {
1430 fixup_vary(r);
1431 }
1432
1433
1434 /*
1435 * Control cachability for non-cacheable responses if not already set by
1436 * some other part of the server configuration.
1437 */
1438 if (r->no_cache && !apr_table_get(r->headers_out, "Expires")) {
1439 char *date = apr_palloc(r->pool, APR_RFC822_DATE_LEN);
1441 apr_table_addn(r->headers_out, "Expires", date);
1442 }
1443
1444 /*
1445 * Now remove any ETag response header field if earlier processing
1446 * says so (such as a 'FileETag None' directive).
1447 */
1448 if (apr_table_get(r->notes, "no-etag") != NULL) {
1449 apr_table_unset(r->headers_out, "ETag");
1450 }
1451
1452 /* determine the protocol and whether we should use keepalives. */
1455
1456 /* 204/304 responses don't have content related headers */
1458 apr_table_unset(r->headers_out, "Transfer-Encoding");
1459 apr_table_unset(r->headers_out, "Content-Length");
1462 r->clength = r->chunked = 0;
1463 }
1464 else if (r->chunked) {
1465 apr_table_mergen(r->headers_out, "Transfer-Encoding", "chunked");
1466 apr_table_unset(r->headers_out, "Content-Length");
1467 }
1468
1470 if (ctype) {
1471 apr_table_setn(r->headers_out, "Content-Type", ctype);
1472 }
1473
1474 if (r->content_encoding) {
1475 apr_table_setn(r->headers_out, "Content-Encoding",
1477 }
1478
1480 int i;
1481 char *token;
1482 char **languages = (char **)(r->content_languages->elts);
1483 const char *field = apr_table_get(r->headers_out, "Content-Language");
1484
1485 while (field && (token = ap_get_list_item(r->pool, &field)) != NULL) {
1486 for (i = 0; i < r->content_languages->nelts; ++i) {
1487 if (!ap_cstr_casecmp(token, languages[i]))
1488 break;
1489 }
1490 if (i == r->content_languages->nelts) {
1491 *((char **) apr_array_push(r->content_languages)) = token;
1492 }
1493 }
1494
1496 apr_table_setn(r->headers_out, "Content-Language", field);
1497 }
1498
1499 /* This is a hack, but I can't find anyway around it. The idea is that
1500 * we don't want to send out 0 Content-Lengths if it is a head request.
1501 * This happens when modules try to outsmart the server, and return
1502 * if they see a HEAD request. Apache 1.3 handlers were supposed to
1503 * just return in that situation, and the core handled the HEAD. In
1504 * 2.0, if a handler returns, then the core sends an EOS bucket down
1505 * the filter stack, and the content-length filter computes a C-L of
1506 * zero and that gets put in the headers, and we end up sending a
1507 * zero C-L to the client. We can't just remove the C-L filter,
1508 * because well behaved 2.0 handlers will send their data down the stack,
1509 * and we will compute a real C-L for the head request. RBB
1510 */
1511 if (r->header_only
1512 && (clheader = apr_table_get(r->headers_out, "Content-Length"))
1513 && !strcmp(clheader, "0")) {
1514 apr_table_unset(r->headers_out, "Content-Length");
1515 }
1516
1517 b2 = apr_brigade_create(r->pool, c->bucket_alloc);
1519
1520 h.pool = r->pool;
1521 h.bb = b2;
1522
1524
1526
1527 if (header_only) {
1528 e = APR_BRIGADE_LAST(b);
1533 }
1535 }
1536
1537 rv = ap_pass_brigade(f->next, b2);
1539 ctx->headers_sent = 1;
1540
1541 if (rv != APR_SUCCESS || header_only) {
1542 goto out;
1543 }
1544
1545 r->sent_bodyct = 1; /* Whatever follows is real body stuff... */
1546
1547 if (r->chunked) {
1548 /* We can't add this filter until we have already sent the headers.
1549 * If we add it before this point, then the headers will be chunked
1550 * as well, and that is just wrong.
1551 */
1552 ap_add_output_filter("CHUNK", NULL, r, r->connection);
1553 }
1554
1555 /* Don't remove this filter until after we have added the CHUNK filter.
1556 * Otherwise, f->next won't be the CHUNK filter and thus the first
1557 * brigade won't be chunked properly.
1558 */
1560 rv = ap_pass_brigade(f->next, b);
1561out:
1562 if (recursive_error) {
1563 return AP_FILTER_ERROR;
1564 }
1565 return rv;
1566}
1567
1568/*
1569 * Map specific APR codes returned by the filter stack to HTTP error
1570 * codes, or the default status code provided. Use it as follows:
1571 *
1572 * return ap_map_http_request_error(rv, HTTP_BAD_REQUEST);
1573 *
1574 * If the filter has already handled the error, AP_FILTER_ERROR will
1575 * be returned, which is cleanly passed through.
1576 *
1577 * These mappings imply that the filter stack is reading from the
1578 * downstream client, the proxy will map these codes differently.
1579 */
1581{
1582 switch (rv) {
1583 case AP_FILTER_ERROR:
1584 return AP_FILTER_ERROR;
1585
1586 case APR_ENOSPC:
1588
1589 case APR_ENOTIMPL:
1590 return HTTP_NOT_IMPLEMENTED;
1591
1592 case APR_TIMEUP:
1593 case APR_ETIMEDOUT:
1594 return HTTP_REQUEST_TIME_OUT;
1595
1596 default:
1597 return status;
1598 }
1599}
1600
1601/* In HTTP/1.1, any method can have a body. However, most GET handlers
1602 * wouldn't know what to do with a request body if they received one.
1603 * This helper routine tests for and reads any message body in the request,
1604 * simply discarding whatever it receives. We need to do this because
1605 * failing to read the request body would cause it to be interpreted
1606 * as the next request on a persistent connection.
1607 *
1608 * Since we return an error status if the request is malformed, this
1609 * routine should be called at the beginning of a no-body handler, e.g.,
1610 *
1611 * if ((retval = ap_discard_request_body(r)) != OK) {
1612 * return retval;
1613 * }
1614 */
1616{
1617 int rc = OK;
1618 conn_rec *c = r->connection;
1620
1621 /* Sometimes we'll get in a state where the input handling has
1622 * detected an error where we want to drop the connection, so if
1623 * that's the case, don't read the data as that is what we're trying
1624 * to avoid.
1625 *
1626 * This function is also a no-op on a subrequest.
1627 */
1628 if (r->main || c->keepalive == AP_CONN_CLOSE) {
1629 return OK;
1630 }
1632 c->keepalive = AP_CONN_CLOSE;
1633 return OK;
1634 }
1635
1637 for (;;) {
1638 apr_status_t rv;
1639
1642 if (rv != APR_SUCCESS) {
1644 goto cleanup;
1645 }
1646
1647 while (!APR_BRIGADE_EMPTY(bb)) {
1649
1650 if (APR_BUCKET_IS_EOS(b)) {
1651 goto cleanup;
1652 }
1653
1654 /* There is no need to read empty or metadata buckets or
1655 * buckets of known length, but we MUST read buckets of
1656 * unknown length in order to exhaust them.
1657 */
1658 if (b->length == (apr_size_t)-1) {
1660 const char *data;
1661
1663 if (rv != APR_SUCCESS) {
1665 goto cleanup;
1666 }
1667 }
1668
1670 }
1671 }
1672
1673cleanup:
1675 if (rc != OK) {
1676 c->keepalive = AP_CONN_CLOSE;
1677 }
1678 return rc;
1679}
1680
1681/* Here we deal with getting the request message body from the client.
1682 * Whether or not the request contains a body is signaled by the presence
1683 * of a non-zero Content-Length or by a Transfer-Encoding: chunked.
1684 *
1685 * Note that this is more complicated than it was in Apache 1.1 and prior
1686 * versions, because chunked support means that the module does less.
1687 *
1688 * The proper procedure is this:
1689 *
1690 * 1. Call ap_setup_client_block() near the beginning of the request
1691 * handler. This will set up all the necessary properties, and will
1692 * return either OK, or an error code. If the latter, the module should
1693 * return that error code. The second parameter selects the policy to
1694 * apply if the request message indicates a body, and how a chunked
1695 * transfer-coding should be interpreted. Choose one of
1696 *
1697 * REQUEST_NO_BODY Send 413 error if message has any body
1698 * REQUEST_CHUNKED_ERROR Send 411 error if body without Content-Length
1699 * REQUEST_CHUNKED_DECHUNK If chunked, remove the chunks for me.
1700 * REQUEST_CHUNKED_PASS If chunked, pass the chunk headers with body.
1701 *
1702 * In order to use the last two options, the caller MUST provide a buffer
1703 * large enough to hold a chunk-size line, including any extensions.
1704 *
1705 * 2. When you are ready to read a body (if any), call ap_should_client_block().
1706 * This will tell the module whether or not to read input. If it is 0,
1707 * the module should assume that there is no message body to read.
1708 *
1709 * 3. Finally, call ap_get_client_block in a loop. Pass it a buffer and its size.
1710 * It will put data into the buffer (not necessarily a full buffer), and
1711 * return the length of the input block. When it is done reading, it will
1712 * return 0 if EOF, or -1 if there was an error.
1713 * If an error occurs on input, we force an end to keepalive.
1714 *
1715 * This step also sends a 100 Continue response to HTTP/1.1 clients if appropriate.
1716 */
1717
1719{
1720 const char *tenc = apr_table_get(r->headers_in, "Transfer-Encoding");
1721 const char *lenp = apr_table_get(r->headers_in, "Content-Length");
1722 apr_off_t limit_req_body = ap_get_limit_req_body(r);
1723
1725 r->read_chunked = 0;
1726 r->remaining = 0;
1727
1728 if (tenc) {
1729 if (ap_cstr_casecmp(tenc, "chunked")) {
1731 "Unknown Transfer-Encoding %s", tenc);
1732 return HTTP_NOT_IMPLEMENTED;
1733 }
1736 "chunked Transfer-Encoding forbidden: %s", r->uri);
1738 }
1739
1740 r->read_chunked = 1;
1741 }
1742 else if (lenp) {
1744 r->remaining = 0;
1746 "Invalid Content-Length '%s'", lenp);
1747 return HTTP_BAD_REQUEST;
1748 }
1749 }
1750
1751 if ((r->read_body == REQUEST_NO_BODY)
1752 && (r->read_chunked || (r->remaining > 0))) {
1754 "%s with body is not allowed for %s", r->method, r->uri);
1756 }
1757
1758 if (limit_req_body > 0 && (r->remaining > limit_req_body)) {
1759 /* will be logged when the body is discarded */
1761 }
1762
1763#ifdef AP_DEBUG
1764 {
1765 /* Make sure ap_getline() didn't leave any droppings. */
1769 }
1770#endif
1771
1772 return OK;
1773}
1774
1776{
1777 /* First check if we have already read the request body */
1778
1779 if (r->read_length || (!r->read_chunked && (r->remaining <= 0))) {
1780 return 0;
1781 }
1782
1783 return 1;
1784}
1785
1786/* get_client_block is called in a loop to get the request message body.
1787 * This is quite simple if the client includes a content-length
1788 * (the normal case), but gets messy if the body is chunked. Note that
1789 * r->remaining is used to maintain state across calls and that
1790 * r->read_length is the total number of bytes given to the caller
1791 * across all invocations. It is messy because we have to be careful not
1792 * to read past the data provided by the client, since these reads block.
1793 * Returns 0 on End-of-body, -1 on error or premature chunk end.
1794 *
1795 */
1798{
1799 apr_status_t rv;
1801
1802 if (r->remaining < 0 || (!r->read_chunked && r->remaining == 0)) {
1803 return 0;
1804 }
1805
1807 if (bb == NULL) {
1809 return -1;
1810 }
1811
1814
1815 /* We lose the failure code here. This is why ap_get_client_block should
1816 * not be used.
1817 */
1818 if (rv == AP_FILTER_ERROR) {
1819 /* AP_FILTER_ERROR means a filter has responded already,
1820 * we are DONE.
1821 */
1823 return -1;
1824 }
1825 if (rv != APR_SUCCESS) {
1826 /* if we actually fail here, we want to just return and
1827 * stop trying to read data from the client.
1828 */
1831 return -1;
1832 }
1833
1834 /* If this fails, it means that a filter is written incorrectly and that
1835 * it needs to learn how to properly handle APR_BLOCK_READ requests by
1836 * returning data when requested.
1837 */
1839
1840 /* Check to see if EOS in the brigade.
1841 *
1842 * If so, we have to leave a nugget for the *next* ap_get_client_block
1843 * call to return 0.
1844 */
1846 if (r->read_chunked) {
1847 r->remaining = -1;
1848 }
1849 else {
1850 r->remaining = 0;
1851 }
1852 }
1853
1854 rv = apr_brigade_flatten(bb, buffer, &bufsiz);
1855 if (rv != APR_SUCCESS) {
1857 return -1;
1858 }
1859
1860 /* XXX yank me? */
1861 r->read_length += bufsiz;
1862
1864 return bufsiz;
1865}
1866
1867/* Context struct for ap_http_outerror_filter */
1868typedef struct {
1872
1873/* Filter to handle any error buckets on output */
1876{
1877 request_rec *r = f->r;
1879 apr_bucket *e;
1880
1881 /* Create context if none is present */
1882 if (!ctx) {
1884 f->ctx = ctx;
1885 }
1886 for (e = APR_BRIGADE_FIRST(b);
1888 e = APR_BUCKET_NEXT(e))
1889 {
1890 if (AP_BUCKET_IS_ERROR(e)) {
1891 /*
1892 * Start of error handling state tree. Just one condition
1893 * right now :)
1894 */
1895 if (((ap_bucket_error *)(e->data))->status == HTTP_BAD_GATEWAY) {
1896 /* stream aborted and we have not ended it yet */
1898 }
1899 /*
1900 * Memorize the status code of the first error bucket for possible
1901 * later use.
1902 */
1903 if (!ctx->first_error) {
1904 ctx->first_error = ((ap_bucket_error *)(e->data))->status;
1905 }
1906 continue;
1907 }
1908 /* Detect EOC buckets and memorize this in the context. */
1909 if (AP_BUCKET_IS_EOC(e)) {
1911 ctx->seen_eoc = 1;
1912 }
1913 }
1914 /*
1915 * Remove all data buckets that are in a brigade after an EOC bucket
1916 * was seen, as an EOC bucket tells us that no (further) resource
1917 * and protocol data should go out to the client. OTOH meta buckets
1918 * are still welcome as they might trigger needed actions down in
1919 * the chain (e.g. in network filters like SSL).
1920 * Remark 1: It is needed to dump ALL data buckets in the brigade
1921 * since an filter in between might have inserted data
1922 * buckets BEFORE the EOC bucket sent by the original
1923 * sender and we do NOT want this data to be sent.
1924 * Remark 2: Dumping all data buckets here does not necessarily mean
1925 * that no further data is send to the client as:
1926 * 1. Network filters like SSL can still be triggered via
1927 * meta buckets to talk with the client e.g. for a
1928 * clean shutdown.
1929 * 2. There could be still data that was buffered before
1930 * down in the chain that gets flushed by a FLUSH or an
1931 * EOS bucket.
1932 */
1933 if (ctx->seen_eoc) {
1934 /*
1935 * Set the request status to the status of the first error bucket.
1936 * This should ensure that we log an appropriate status code in
1937 * the access log.
1938 * We need to set r->status on each call after we noticed an EOC as
1939 * data bucket generators like ap_die might have changed the status
1940 * code. But we know better in this case and insist on the status
1941 * code that we have seen in the error bucket.
1942 */
1943 if (ctx->first_error) {
1944 r->status = ctx->first_error;
1945 }
1946 for (e = APR_BRIGADE_FIRST(b);
1948 e = APR_BUCKET_NEXT(e))
1949 {
1950 if (!APR_BUCKET_IS_METADATA(e)) {
1952 }
1953 }
1954 }
1955
1956 return ap_pass_brigade(f->next, b);
1957}
Symbol export macros and hook functions.
#define AP_DECLARE_NONSTD(type)
Definition ap_config.h:77
#define AP_DECLARE(type)
Definition ap_config.h:67
const char apr_size_t len
Definition ap_regex.h:187
APR-UTIL Buckets/Bucket Brigades.
APR-UTIL date routines.
APR general purpose library routines.
APR Signal Handling.
apr_size_t const unsigned char unsigned int unsigned int d
Definition apr_siphash.h:72
APR Strings library.
APR Standard Headers Support.
apr_bucket * ap_bucket_eoc_create(apr_bucket_alloc_t *list)
Definition eoc_bucket.c:38
#define APLOG_USE_MODULE(foo)
#define ap_get_module_config(v, m)
request_rec * r
#define AP_BUCKET_IS_EOC(e)
#define HUGE_STRING_LEN
Definition httpd.h:303
#define AP_FILTER_ERROR
Definition httpd.h:473
#define AP_SERVER_PROTOCOL
Definition httpd.h:216
#define DECLINED
Definition httpd.h:457
#define HTTP_VERSION(major, minor)
Definition httpd.h:269
#define OK
Definition httpd.h:456
#define DONE
Definition httpd.h:458
const char * ap_get_server_banner(void)
Definition core.c:3593
#define AP_CORE_DECLARE_NONSTD
Definition httpd.h:390
#define ap_xlate_proto_to_ascii(x, y)
Definition util_ebcdic.h:80
#define ap_xlate_proto_from_ascii(x, y)
Definition util_ebcdic.h:81
apr_status_t ap_pass_brigade(ap_filter_t *filter, apr_bucket_brigade *bucket)
ap_filter_t * ap_add_output_filter(const char *name, void *ctx, request_rec *r, conn_rec *c)
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)
void ap_remove_output_filter(ap_filter_t *f)
apr_off_t ap_get_limit_req_body(const request_rec *r)
Definition core.c:1259
#define ap_get_core_module_config(v)
Definition http_core.h:383
#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 APLOGrtrace4(r)
Definition http_log.h:249
#define APLOG_TRACE3
Definition http_log.h:74
#define APLOG_MARK
Definition http_log.h:283
#define APLOG_TRACE5
Definition http_log.h:76
#define APLOGrtrace3(r)
Definition http_log.h:248
int ap_map_http_request_error(apr_status_t rv, int status)
void ap_send_interim_response(request_rec *r, int send_headers)
Definition protocol.c:2316
apr_status_t ap_http_header_filter(ap_filter_t *f, apr_bucket_brigade *b)
int ap_set_keepalive(request_rec *r)
int ap_should_client_block(request_rec *r)
void ap_set_content_length(request_rec *r, apr_off_t length)
Definition protocol.c:160
const char * ap_make_content_type(request_rec *r, const char *type)
Definition protocol.c:110
void ap_set_content_type_ex(request_rec *r, const char *ct, int trusted)
#define AP_BUCKET_IS_ERROR(e)
long ap_get_client_block(request_rec *r, char *buffer, apr_size_t bufsiz)
const char * ap_get_status_line(int status)
int ap_setup_client_block(request_rec *r, int read_policy)
int ap_discard_request_body(request_rec *r)
void ap_get_mime_headers(request_rec *r)
Definition protocol.c:1348
apr_bucket * ap_bucket_error_create(int error, const char *buf, apr_pool_t *p, apr_bucket_alloc_t *list)
void ap_die(int type, request_rec *r)
apr_status_t ap_recent_rfc822_date(char *date_str, apr_time_t t)
Definition util_time.c:282
#define CRLF_ASCII
Definition httpd.h:737
#define LF
Definition httpd.h:720
#define CRLF
Definition httpd.h:724
#define CR
Definition httpd.h:722
#define APR_EAGAIN
Definition apr_errno.h:730
#define APR_EGENERAL
Definition apr_errno.h:313
#define APR_ENOSPC
Definition apr_errno.h:676
#define APR_EOF
Definition apr_errno.h:461
#define APR_INCOMPLETE
Definition apr_errno.h:452
#define APR_ETIMEDOUT
Definition apr_errno.h:784
#define APR_ENOTIMPL
Definition apr_errno.h:476
#define APR_TIMEUP
Definition apr_errno.h:450
#define APR_EINVAL
Definition apr_errno.h:711
#define APR_STATUS_IS_EAGAIN(s)
Definition apr_errno.h:1272
apr_file_t * f
#define APR_BUCKET_REMOVE(e)
#define APR_BRIGADE_LAST(b)
#define APR_BUCKET_IS_METADATA(e)
#define APR_BRIGADE_INSERT_TAIL(b, e)
apr_file_t apr_off_t start
#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)
apr_brigade_flush void * ctx
#define APR_BRIGADE_FIRST(b)
#define apr_bucket_read(e, str, len, block)
@ APR_BLOCK_READ
Definition apr_buckets.h:58
@ APR_NONBLOCK_READ
Definition apr_buckets.h:59
apr_pool_t apr_dbd_t apr_dbd_results_t ** res
Definition apr_dbd.h:287
apr_dbd_transaction_t int mode
Definition apr_dbd.h:261
const char apr_hash_t ** values
apr_memcache_server_t * server
apr_redis_t * rc
Definition apr_redis.h:173
#define HTTP_OK
Definition httpd.h:490
#define HTTP_BAD_REQUEST
Definition httpd.h:508
#define HTTP_LENGTH_REQUIRED
Definition httpd.h:519
#define HTTP_REQUEST_TIME_OUT
Definition httpd.h:516
#define HTTP_CONTINUE
Definition httpd.h:487
#define AP_STATUS_IS_HEADER_ONLY(x)
Definition httpd.h:574
#define HTTP_BAD_GATEWAY
Definition httpd.h:537
#define HTTP_INTERNAL_SERVER_ERROR
Definition httpd.h:535
#define HTTP_METHOD_NOT_ALLOWED
Definition httpd.h:513
#define HTTP_REQUEST_ENTITY_TOO_LARGE
Definition httpd.h:521
#define HTTP_NOT_IMPLEMENTED
Definition httpd.h:536
#define ap_is_HTTP_SUCCESS(x)
Definition httpd.h:550
#define ap_status_drops_connection(x)
Definition httpd.h:563
int ap_send_http_trace(request_rec *r)
apr_status_t ap_http_filter(ap_filter_t *f, apr_bucket_brigade *b, ap_input_mode_t mode, apr_read_type_e block, apr_off_t readbytes)
void ap_basic_http_header(request_rec *r, apr_bucket_brigade *bb)
apr_status_t ap_http_outerror_filter(ap_filter_t *f, apr_bucket_brigade *b)
#define M_TRACE
Definition httpd.h:598
const char const char int ap_is_chunked(apr_pool_t *p, const char *line)
Definition util.c:1786
const char * ap_scan_http_token(const char *ptr)
Definition util.c:1664
int ap_cstr_casecmp(const char *s1, const char *s2)
Definition util.c:3542
char * ap_escape_logitem(apr_pool_t *p, const char *str)
Definition util.c:2183
char * ap_get_list_item(apr_pool_t *p, const char **field)
Definition util.c:1314
#define PROXYREQ_NONE
Definition httpd.h:1133
#define AP_DEBUG_ASSERT(exp)
Definition httpd.h:2283
const char * ap_scan_vchar_obstext(const char *ptr)
Definition util.c:1674
const char * ap_scan_http_field_content(const char *ptr)
Definition util.c:1654
int ap_parse_strict_length(apr_off_t *len, const char *str)
Definition util.c:2683
#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
#define apr_isspace(c)
Definition apr_lib.h:225
#define apr_isxdigit(c)
Definition apr_lib.h:229
#define apr_iscntrl(c)
Definition apr_lib.h:207
#define APR_SUCCESS
Definition apr_errno.h:225
int apr_status_t
Definition apr_errno.h:44
const char * key
const struct iovec * vec
void * data
void const char apr_status_t(* cleanup)(void *))
char * buffer
const apr_hash_t * h
Definition apr_hash.h:97
apr_vformatter_buff_t * c
Definition apr_lib.h:175
apr_interval_time_t t
apr_size_t buflen
int int int protocol
apr_pool_t * b
Definition apr_pools.h:529
#define apr_pcalloc(p, size)
Definition apr_pools.h:465
const char char ** end
#define APR_ARRAY_IDX(ary, i, type)
Definition apr_tables.h:141
int int status
#define APR_RFC822_DATE_LEN
Definition apr_time.h:186
#define REQUEST_CHUNKED_ERROR
Definition httpd.h:748
#define REQUEST_CHUNKED_DECHUNK
Definition httpd.h:750
#define REQUEST_NO_BODY
Definition httpd.h:746
Apache Configuration.
Apache connection library.
CORE HTTP Daemon.
#define AP_TRACE_EXTENDED
Definition http_core.h:730
#define AP_TRACE_DISABLE
Definition http_core.h:728
#define AP_MERGE_TRAILERS_ENABLE
Definition http_core.h:733
#define AP_HTTP_CONFORMANCE_UNSAFE
Definition http_core.h:746
static apr_status_t parse_chunk_size(http_ctx_t *ctx, const char *buffer, apr_size_t len, int linelimit, int strict)
static int uniq_field_values(void *d, const char *key, const char *val)
static apr_status_t send_all_header_fields(header_struct *h, const request_rec *r)
static int check_headers_table(apr_table_t *t, struct check_header_ctx *ctx)
static void terminate_header(apr_bucket_brigade *bb)
static int check_headers_recursion(request_rec *r)
struct http_filter_ctx http_ctx_t
static int check_header(struct check_header_ctx *ctx, const char *name, const char **val)
static void basic_http_header_check(request_rec *r, const char **protocol)
static apr_status_t bail_out_on_error(http_ctx_t *ctx, ap_filter_t *f, int http_error)
static int form_header_field(header_struct *h, const char *fieldname, const char *fieldval)
static void fixup_vary(request_rec *r)
static void basic_http_header(request_rec *r, apr_bucket_brigade *bb, const char *protocol)
static apr_status_t read_chunked_trailers(http_ctx_t *ctx, ap_filter_t *f, apr_bucket_brigade *b, int merge)
static APR_INLINE int check_headers(request_rec *r)
static apr_status_t validate_status_line(request_rec *r)
Apache Logging library.
Command line options.
HTTP protocol handling.
Apache Request library.
Virtual Host package.
HTTP Daemon routines.
mod_core private header file
static apr_file_t * out
Definition mod_info.c:85
return NULL
Definition mod_so.c:359
int i
Definition mod_so.c:347
char * name
A bucket referring to an HTTP error.
The representation of a filter chain.
apr_bucket_alloc_t * bucket_alloc
void * data
apr_pool_t * pool
apr_pool_t * pool
Definition apr_hash.c:76
Definition apr_tables.h:81
char * val
Definition apr_tables.h:87
char * key
Definition apr_tables.h:83
request_rec * r
Structure to store things which are per connection.
Definition httpd.h:1152
ap_conn_keepalive_e keepalive
Definition httpd.h:1223
struct apr_bucket_alloc_t * bucket_alloc
Definition httpd.h:1201
Per-request configuration.
Definition http_core.h:394
apr_bucket_brigade * bb
apr_pool_t * pool
apr_off_t limit_used
apr_bucket_brigade * bb
apr_off_t remaining
apr_int32_t chunk_bws
enum http_filter_ctx::@17 state
apr_int32_t chunkbits
unsigned int eos_sent
apr_off_t limit
apr_int32_t chunk_used
unsigned int seen_data
A structure that represents the current request.
Definition httpd.h:845
apr_array_header_t * content_languages
Definition httpd.h:999
int status
Definition httpd.h:891
char * uri
Definition httpd.h:1016
const char * content_type
Definition httpd.h:992
apr_table_t * trailers_in
Definition httpd.h:1104
struct ap_filter_t * output_filters
Definition httpd.h:1070
request_rec * prev
Definition httpd.h:856
int assbackwards
Definition httpd.h:868
int read_body
Definition httpd.h:947
int header_only
Definition httpd.h:875
apr_table_t * notes
Definition httpd.h:985
char * the_request
Definition httpd.h:866
int method_number
Definition httpd.h:898
apr_pool_t * pool
Definition httpd.h:847
apr_time_t request_time
Definition httpd.h:886
apr_off_t remaining
Definition httpd.h:959
int proxyreq
Definition httpd.h:873
conn_rec * connection
Definition httpd.h:849
int no_cache
Definition httpd.h:1082
apr_table_t * err_headers_out
Definition httpd.h:981
int proto_num
Definition httpd.h:877
int chunked
Definition httpd.h:942
struct ap_filter_t * input_filters
Definition httpd.h:1072
struct ap_conf_vector_t * request_config
Definition httpd.h:1049
apr_table_t * headers_in
Definition httpd.h:976
int read_chunked
Definition httpd.h:949
apr_off_t read_length
Definition httpd.h:961
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
apr_off_t clength
Definition httpd.h:940
const char * status_line
Definition httpd.h:889
const char * method
Definition httpd.h:900
const char * content_encoding
Definition httpd.h:997
apr_table_t * headers_out
Definition httpd.h:978
struct ap_conf_vector_t * module_config
Definition httpd.h:1341
charset conversion
Utilities for EBCDIC conversion.
Apache filter library.
ap_input_mode_t
input filtering modes
Definition util_filter.h:41
@ AP_MODE_READBYTES
Definition util_filter.h:43
@ AP_MODE_GETLINE
Definition util_filter.h:48
Apache date-time handling functions.