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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * http_filter.c --- HTTP routines which either filters or deal with filters.
22 #include "apr_strings.h"
23 #include "apr_buckets.h"
25 #include "apr_signal.h"
27 #define APR_WANT_STDIO /* for sscanf */
28 #define APR_WANT_STRFUNC
29 #define APR_WANT_MEMFUNC
33 #include "util_filter.h"
34 #include "ap_config.h"
36 #include "http_config.h"
37 #include "http_core.h"
38 #include "http_protocol.h"
39 #include "http_main.h"
40 #include "http_request.h"
41 #include "http_vhost.h"
42 #include "http_log.h" /* For errors detected in basic auth common
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"
58 extern module AP_MODULE_DECLARE_DATA http_module;
60 static long get_chunk_size(char *);
62 typedef struct http_filter_ctx {
75 /* This is the HTTP_INPUT filter for HTTP requests and responses from
76 * proxied servers (mod_proxy). It handles chunked and content-length
77 * bodies. This can only be inserted/used after the headers
78 * are successfully parsed.
80 apr_status_t ap_http_filter(ap_filter_t *f, apr_bucket_brigade *b,
81 ap_input_mode_t mode, apr_read_type_e block,
85 http_ctx_t *ctx = f->ctx;
89 /* just get out of the way of things we don't want. */
90 if (mode != AP_MODE_READBYTES && mode != AP_MODE_GETLINE) {
91 return ap_get_brigade(f->next, b, mode, block, readbytes);
95 const char *tenc, *lenp;
96 f->ctx = ctx = apr_palloc(f->r->pool, sizeof(*ctx));
97 ctx->state = BODY_NONE;
102 /* LimitRequestBody does not apply to proxied responses.
103 * Consider implementing this check in its own filter.
104 * Would adding a directive to limit the size of proxied
105 * responses be useful?
107 if (!f->r->proxyreq) {
108 ctx->limit = ap_get_limit_req_body(f->r);
114 tenc = apr_table_get(f->r->headers_in, "Transfer-Encoding");
115 lenp = apr_table_get(f->r->headers_in, "Content-Length");
118 /* RFC2616 allows qualifiers, so use strncasecmp */
119 if (!strncasecmp(tenc, "chunked", 7)) {
120 ctx->state = BODY_CHUNK;
123 /* Something that isn't in HTTP, unless some future
124 * edition defines new transfer ecodings, is unsupported.
126 apr_bucket_brigade *bb;
127 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, f->r,
128 "Unknown Transfer-Encoding: %s", tenc);
129 bb = apr_brigade_create(f->r->pool, f->c->bucket_alloc);
130 e = ap_bucket_error_create(HTTP_NOT_IMPLEMENTED, NULL,
131 f->r->pool, f->c->bucket_alloc);
132 APR_BRIGADE_INSERT_TAIL(bb, e);
133 e = apr_bucket_eos_create(f->c->bucket_alloc);
134 APR_BRIGADE_INSERT_TAIL(bb, e);
136 return ap_pass_brigade(f->r->output_filters, bb);
142 ctx->state = BODY_LENGTH;
145 /* Protects against over/underflow, non-digit chars in the
146 * string (excluding leading space) (the endstr checks)
147 * and a negative number. */
148 if (apr_strtoff(&ctx->remaining, lenp, &endstr, 10)
149 || endstr == lenp || *endstr || ctx->remaining < 0) {
150 apr_bucket_brigade *bb;
153 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, f->r,
154 "Invalid Content-Length");
156 bb = apr_brigade_create(f->r->pool, f->c->bucket_alloc);
157 e = ap_bucket_error_create(HTTP_REQUEST_ENTITY_TOO_LARGE, NULL,
158 f->r->pool, f->c->bucket_alloc);
159 APR_BRIGADE_INSERT_TAIL(bb, e);
160 e = apr_bucket_eos_create(f->c->bucket_alloc);
161 APR_BRIGADE_INSERT_TAIL(bb, e);
163 return ap_pass_brigade(f->r->output_filters, bb);
166 /* If we have a limit in effect and we know the C-L ahead of
167 * time, stop it here if it is invalid.
169 if (ctx->limit && ctx->limit < ctx->remaining) {
170 apr_bucket_brigade *bb;
171 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, f->r,
172 "Requested content-length of %" APR_OFF_T_FMT
173 " is larger than the configured limit"
174 " of %" APR_OFF_T_FMT, ctx->remaining, ctx->limit);
175 bb = apr_brigade_create(f->r->pool, f->c->bucket_alloc);
176 e = ap_bucket_error_create(HTTP_REQUEST_ENTITY_TOO_LARGE, NULL,
177 f->r->pool, f->c->bucket_alloc);
178 APR_BRIGADE_INSERT_TAIL(bb, e);
179 e = apr_bucket_eos_create(f->c->bucket_alloc);
180 APR_BRIGADE_INSERT_TAIL(bb, e);
182 return ap_pass_brigade(f->r->output_filters, bb);
186 /* If we don't have a request entity indicated by the headers, EOS.
187 * (BODY_NONE is a valid intermediate state due to trailers,
188 * but it isn't a valid starting state.)
190 * RFC 2616 Section 4.4 note 5 states that connection-close
191 * is invalid for a request entity - request bodies must be
192 * denoted by C-L or T-E: chunked.
194 * Note that since the proxy uses this filter to handle the
195 * proxied *response*, proxy responses MUST be exempt.
197 if (ctx->state == BODY_NONE && f->r->proxyreq != PROXYREQ_RESPONSE) {
198 e = apr_bucket_eos_create(f->c->bucket_alloc);
199 APR_BRIGADE_INSERT_TAIL(b, e);
204 /* Since we're about to read data, send 100-Continue if needed.
205 * Only valid on chunked and C-L bodies where the C-L is > 0. */
206 if ((ctx->state == BODY_CHUNK ||
207 (ctx->state == BODY_LENGTH && ctx->remaining > 0)) &&
208 f->r->expecting_100 && f->r->proto_num >= HTTP_VERSION(1,1) &&
209 !(f->r->eos_sent || f->r->bytes_sent)) {
211 apr_bucket_brigade *bb;
213 tmp = apr_pstrcat(f->r->pool, AP_SERVER_PROTOCOL, " ",
214 ap_get_status_line(100), CRLF CRLF, NULL);
215 bb = apr_brigade_create(f->r->pool, f->c->bucket_alloc);
216 e = apr_bucket_pool_create(tmp, strlen(tmp), f->r->pool,
218 APR_BRIGADE_INSERT_HEAD(bb, e);
219 e = apr_bucket_flush_create(f->c->bucket_alloc);
220 APR_BRIGADE_INSERT_TAIL(bb, e);
222 ap_pass_brigade(f->c->output_filters, bb);
225 /* We can't read the chunk until after sending 100 if required. */
226 if (ctx->state == BODY_CHUNK) {
228 apr_bucket_brigade *bb;
230 apr_off_t brigade_length;
232 bb = apr_brigade_create(f->r->pool, f->c->bucket_alloc);
234 rv = ap_get_brigade(f->next, bb, AP_MODE_GETLINE,
238 if (block == APR_NONBLOCK_READ &&
239 ( (rv == APR_SUCCESS && APR_BRIGADE_EMPTY(bb)) ||
240 (APR_STATUS_IS_EAGAIN(rv)) )) {
244 if (rv == APR_SUCCESS) {
245 /* We have to check the length of the brigade we got back.
246 * We will not accept partial or blank lines.
248 rv = apr_brigade_length(bb, 1, &brigade_length);
249 if (rv == APR_SUCCESS
250 && (!brigade_length ||
251 brigade_length > f->r->server->limit_req_line)) {
254 if (rv == APR_SUCCESS) {
255 rv = apr_brigade_flatten(bb, line, &len);
256 if (rv == APR_SUCCESS) {
257 ctx->remaining = get_chunk_size(line);
261 apr_brigade_cleanup(bb);
263 /* Detect chunksize error (such as overflow) */
264 if (rv != APR_SUCCESS || ctx->remaining < 0) {
265 ctx->remaining = 0; /* Reset it in case we have to
266 * come back here later */
267 e = ap_bucket_error_create(HTTP_REQUEST_ENTITY_TOO_LARGE, NULL,
270 APR_BRIGADE_INSERT_TAIL(bb, e);
271 e = apr_bucket_eos_create(f->c->bucket_alloc);
272 APR_BRIGADE_INSERT_TAIL(bb, e);
274 return ap_pass_brigade(f->r->output_filters, bb);
277 if (!ctx->remaining) {
278 /* Handle trailers by calling ap_get_mime_headers again! */
279 ctx->state = BODY_NONE;
280 ap_get_mime_headers(f->r);
281 e = apr_bucket_eos_create(f->c->bucket_alloc);
282 APR_BRIGADE_INSERT_TAIL(b, e);
290 e = apr_bucket_eos_create(f->c->bucket_alloc);
291 APR_BRIGADE_INSERT_TAIL(b, e);
295 if (!ctx->remaining) {
296 switch (ctx->state) {
300 e = apr_bucket_eos_create(f->c->bucket_alloc);
301 APR_BRIGADE_INSERT_TAIL(b, e);
304 case BODY_CHUNK: case BODY_CHUNK_PART:
307 apr_bucket_brigade *bb;
309 apr_status_t http_error = HTTP_REQUEST_ENTITY_TOO_LARGE;
311 bb = apr_brigade_create(f->r->pool, f->c->bucket_alloc);
313 /* We need to read the CRLF after the chunk. */
314 if (ctx->state == BODY_CHUNK) {
315 rv = ap_get_brigade(f->next, bb, AP_MODE_GETLINE,
317 apr_brigade_cleanup(bb);
318 if (block == APR_NONBLOCK_READ &&
319 (APR_STATUS_IS_EAGAIN(rv))) {
326 if (rv == APR_SUCCESS) {
327 /* Read the real chunk line. */
328 rv = ap_get_brigade(f->next, bb, AP_MODE_GETLINE,
331 if (block == APR_NONBLOCK_READ &&
332 ( (rv == APR_SUCCESS && APR_BRIGADE_EMPTY(bb)) ||
333 (APR_STATUS_IS_EAGAIN(rv)) )) {
334 ctx->state = BODY_CHUNK_PART;
337 ctx->state = BODY_CHUNK;
338 if (rv == APR_SUCCESS) {
339 rv = apr_brigade_flatten(bb, line, &len);
340 if (rv == APR_SUCCESS) {
341 /* Wait a sec, that's a blank line! Oh no. */
344 http_error = HTTP_SERVICE_UNAVAILABLE;
347 ctx->remaining = get_chunk_size(line);
351 apr_brigade_cleanup(bb);
354 /* Detect chunksize error (such as overflow) */
355 if (rv != APR_SUCCESS || ctx->remaining < 0) {
356 apr_status_t out_error;
358 ctx->remaining = 0; /* Reset it in case we have to
359 * come back here later */
360 e = ap_bucket_error_create(http_error,
363 APR_BRIGADE_INSERT_TAIL(bb, e);
364 e = apr_bucket_eos_create(f->c->bucket_alloc);
365 APR_BRIGADE_INSERT_TAIL(bb, e);
367 out_error = ap_pass_brigade(f->r->output_filters, bb);
371 if (!ctx->remaining) {
372 /* Handle trailers by calling ap_get_mime_headers again! */
373 ctx->state = BODY_NONE;
374 ap_get_mime_headers(f->r);
375 e = apr_bucket_eos_create(f->c->bucket_alloc);
376 APR_BRIGADE_INSERT_TAIL(b, e);
385 /* Ensure that the caller can not go over our boundary point. */
386 if (ctx->state == BODY_LENGTH || ctx->state == BODY_CHUNK) {
387 if (ctx->remaining < readbytes) {
388 readbytes = ctx->remaining;
390 AP_DEBUG_ASSERT(readbytes > 0);
393 rv = ap_get_brigade(f->next, b, mode, block, readbytes);
395 if (rv != APR_SUCCESS) {
399 /* How many bytes did we just read? */
400 apr_brigade_length(b, 0, &totalread);
402 /* If this happens, we have a bucket of unknown length. Die because
403 * it means our assumptions have changed. */
404 AP_DEBUG_ASSERT(totalread >= 0);
406 if (ctx->state != BODY_NONE) {
407 ctx->remaining -= totalread;
410 /* If we have no more bytes remaining on a C-L request,
411 * save the callter a roundtrip to discover EOS.
413 if (ctx->state == BODY_LENGTH && ctx->remaining == 0) {
414 e = apr_bucket_eos_create(f->c->bucket_alloc);
415 APR_BRIGADE_INSERT_TAIL(b, e);
418 /* We have a limit in effect. */
420 /* FIXME: Note that we might get slightly confused on chunked inputs
421 * as we'd need to compensate for the chunk lengths which may not
422 * really count. This seems to be up for interpretation. */
423 ctx->limit_used += totalread;
424 if (ctx->limit < ctx->limit_used) {
425 apr_bucket_brigade *bb;
426 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, f->r,
427 "Read content-length of %" APR_OFF_T_FMT
428 " is larger than the configured limit"
429 " of %" APR_OFF_T_FMT, ctx->limit_used, ctx->limit);
430 bb = apr_brigade_create(f->r->pool, f->c->bucket_alloc);
431 e = ap_bucket_error_create(HTTP_REQUEST_ENTITY_TOO_LARGE, NULL,
434 APR_BRIGADE_INSERT_TAIL(bb, e);
435 e = apr_bucket_eos_create(f->c->bucket_alloc);
436 APR_BRIGADE_INSERT_TAIL(bb, e);
438 return ap_pass_brigade(f->r->output_filters, bb);
446 * Parse a chunk extension, detect overflow.
447 * There are two error cases:
448 * 1) If the conversion would require too many bits, a -1 is returned.
449 * 2) If the conversion used the correct number of bits, but an overflow
450 * caused only the sign bit to flip, then that negative number is
452 * In general, any negative number can be considered an overflow error.
454 static long get_chunk_size(char *b)
457 size_t chunkbits = sizeof(long) * 8;
459 ap_xlate_proto_from_ascii(b, strlen(b));
461 /* Skip leading zeros */
466 while (apr_isxdigit(*b) && (chunkbits > 0)) {
469 if (*b >= '0' && *b <= '9') {
472 else if (*b >= 'A' && *b <= 'F') {
473 xvalue = *b - 'A' + 0xa;
475 else if (*b >= 'a' && *b <= 'f') {
476 xvalue = *b - 'a' + 0xa;
479 chunksize = (chunksize << 4) | xvalue;
483 if (apr_isxdigit(*b) && (chunkbits <= 0)) {
491 typedef struct header_struct {
493 apr_bucket_brigade *bb;
496 /* Send a single HTTP header field to the client. Note that this function
497 * is used in calls to table_do(), so their interfaces are co-dependent.
498 * In other words, don't change this one without checking table_do in alloc.c.
499 * It returns true unless there was a write error of some kind.
501 static int form_header_field(header_struct *h,
502 const char *fieldname, const char *fieldval)
504 #if APR_CHARSET_EBCDIC
511 name_len = strlen(fieldname);
512 val_len = strlen(fieldval);
513 len = name_len + val_len + 4; /* 4 for ": " plus CRLF */
514 headfield = (char *)apr_palloc(h->pool, len + 1);
515 memcpy(headfield, fieldname, name_len);
516 next = headfield + name_len;
519 memcpy(next, fieldval, val_len);
524 ap_xlate_proto_to_ascii(headfield, len);
525 apr_brigade_write(h->bb, NULL, NULL, headfield, len);
528 struct iovec *v = vec;
529 v->iov_base = (void *)fieldname;
530 v->iov_len = strlen(fieldname);
533 v->iov_len = sizeof(": ") - 1;
535 v->iov_base = (void *)fieldval;
536 v->iov_len = strlen(fieldval);
539 v->iov_len = sizeof(CRLF) - 1;
540 apr_brigade_writev(h->bb, NULL, NULL, vec, 4);
541 #endif /* !APR_CHARSET_EBCDIC */
545 /* This routine is called by apr_table_do and merges all instances of
546 * the passed field values into a single array that will be further
547 * processed by some later routine. Originally intended to help split
548 * and recombine multiple Vary fields, though it is generic to any field
549 * consisting of comma/space-separated tokens.
551 static int uniq_field_values(void *d, const char *key, const char *val)
553 apr_array_header_t *values;
559 values = (apr_array_header_t *)d;
561 e = apr_pstrdup(values->pool, val);
564 /* Find a non-empty fieldname */
566 while (*e == ',' || apr_isspace(*e)) {
573 while (*e != '\0' && *e != ',' && !apr_isspace(*e)) {
580 /* Now add it to values if it isn't already represented.
581 * Could be replaced by a ap_array_strcasecmp() if we had one.
583 for (i = 0, strpp = (char **) values->elts; i < values->nelts;
585 if (*strpp && strcasecmp(*strpp, start) == 0) {
589 if (i == values->nelts) { /* if not found */
590 *(char **)apr_array_push(values) = start;
592 } while (*e != '\0');
598 * Since some clients choke violently on multiple Vary fields, or
599 * Vary fields with duplicate tokens, combine any multiples and remove
602 static void fixup_vary(request_rec *r)
604 apr_array_header_t *varies;
606 varies = apr_array_make(r->pool, 5, sizeof(char *));
608 /* Extract all Vary fields from the headers_out, separate each into
609 * its comma-separated fieldname values, and then add them to varies
610 * if not already present in the array.
612 apr_table_do((int (*)(void *, const char *, const char *))uniq_field_values,
613 (void *) varies, r->headers_out, "Vary", NULL);
615 /* If we found any, replace old Vary fields with unique-ified value */
617 if (varies->nelts > 0) {
618 apr_table_setn(r->headers_out, "Vary",
619 apr_array_pstrcat(r->pool, varies, ','));
623 /* Send a request's HTTP response headers to the client.
625 static apr_status_t send_all_header_fields(header_struct *h,
626 const request_rec *r)
628 const apr_array_header_t *elts;
629 const apr_table_entry_t *t_elt;
630 const apr_table_entry_t *t_end;
632 struct iovec *vec_next;
634 elts = apr_table_elts(r->headers_out);
635 if (elts->nelts == 0) {
638 t_elt = (const apr_table_entry_t *)(elts->elts);
639 t_end = t_elt + elts->nelts;
640 vec = (struct iovec *)apr_palloc(h->pool, 4 * elts->nelts *
641 sizeof(struct iovec));
644 /* For each field, generate
645 * name ": " value CRLF
648 vec_next->iov_base = (void*)(t_elt->key);
649 vec_next->iov_len = strlen(t_elt->key);
651 vec_next->iov_base = ": ";
652 vec_next->iov_len = sizeof(": ") - 1;
654 vec_next->iov_base = (void*)(t_elt->val);
655 vec_next->iov_len = strlen(t_elt->val);
657 vec_next->iov_base = CRLF;
658 vec_next->iov_len = sizeof(CRLF) - 1;
661 } while (t_elt < t_end);
663 #if APR_CHARSET_EBCDIC
666 char *tmp = apr_pstrcatv(r->pool, vec, vec_next - vec, &len);
667 ap_xlate_proto_to_ascii(tmp, len);
668 return apr_brigade_write(h->bb, NULL, NULL, tmp, len);
671 return apr_brigade_writev(h->bb, NULL, NULL, vec, vec_next - vec);
675 /* Confirm that the status line is well-formed and matches r->status.
676 * If they don't match, a filter may have negated the status line set by a
678 * Zap r->status_line if bad.
680 static void validate_status_line(request_rec *r)
685 && (strlen(r->status_line) <= 4
686 || apr_strtoi64(r->status_line, &end, 10) != r->status
688 || (end - 3) != r->status_line)) {
689 r->status_line = NULL;
694 * Determine the protocol to use for the response. Potentially downgrade
695 * to HTTP/1.0 in some situations and/or turn off keepalives.
697 * also prepare r->status_line.
699 static void basic_http_header_check(request_rec *r,
700 const char **protocol)
702 if (r->assbackwards) {
703 /* no such thing as a response protocol */
707 validate_status_line(r);
709 if (!r->status_line) {
710 r->status_line = ap_get_status_line(r->status);
713 /* Note that we must downgrade before checking for force responses. */
714 if (r->proto_num > HTTP_VERSION(1,0)
715 && apr_table_get(r->subprocess_env, "downgrade-1.0")) {
716 r->proto_num = HTTP_VERSION(1,0);
719 /* kludge around broken browsers when indicated by force-response-1.0
721 if (r->proto_num == HTTP_VERSION(1,0)
722 && apr_table_get(r->subprocess_env, "force-response-1.0")) {
723 *protocol = "HTTP/1.0";
724 r->connection->keepalive = AP_CONN_CLOSE;
727 *protocol = AP_SERVER_PROTOCOL;
732 /* fill "bb" with a barebones/initial HTTP response header */
733 static void basic_http_header(request_rec *r, apr_bucket_brigade *bb,
734 const char *protocol)
741 if (r->assbackwards) {
742 /* there are no headers to send */
746 /* Output the HTTP/1.x Status-Line and the Date and Server fields */
748 vec[0].iov_base = (void *)protocol;
749 vec[0].iov_len = strlen(protocol);
750 vec[1].iov_base = (void *)" ";
751 vec[1].iov_len = sizeof(" ") - 1;
752 vec[2].iov_base = (void *)(r->status_line);
753 vec[2].iov_len = strlen(r->status_line);
754 vec[3].iov_base = (void *)CRLF;
755 vec[3].iov_len = sizeof(CRLF) - 1;
756 #if APR_CHARSET_EBCDIC
760 tmp = apr_pstrcatv(r->pool, vec, 4, &len);
761 ap_xlate_proto_to_ascii(tmp, len);
762 apr_brigade_write(bb, NULL, NULL, tmp, len);
765 apr_brigade_writev(bb, NULL, NULL, vec, 4);
772 * keep the set-by-proxy server and date headers, otherwise
773 * generate a new server header / date header
775 if (r->proxyreq != PROXYREQ_NONE) {
776 const char *proxy_date;
778 proxy_date = apr_table_get(r->headers_out, "Date");
781 * proxy_date needs to be const. So use date for the creation of
782 * our own Date header and pass it over to proxy_date later to
783 * avoid a compiler warning.
785 date = apr_palloc(r->pool, APR_RFC822_DATE_LEN);
786 ap_recent_rfc822_date(date, r->request_time);
789 form_header_field(&h, "Date", proxy_date);
790 server = apr_table_get(r->headers_out, "Server");
792 form_header_field(&h, "Server", server);
794 form_header_field(&h, "Server", ap_get_server_banner());
798 date = apr_palloc(r->pool, APR_RFC822_DATE_LEN);
799 ap_recent_rfc822_date(date, r->request_time);
800 form_header_field(&h, "Date", date);
801 form_header_field(&h, "Server", ap_get_server_banner());
804 /* unset so we don't send them again */
805 apr_table_unset(r->headers_out, "Date"); /* Avoid bogosity */
806 apr_table_unset(r->headers_out, "Server");
809 AP_DECLARE(void) ap_basic_http_header(request_rec *r, apr_bucket_brigade *bb)
811 const char *protocol;
813 basic_http_header_check(r, &protocol);
814 basic_http_header(r, bb, protocol);
817 /* Navigator versions 2.x, 3.x and 4.0 betas up to and including 4.0b2
818 * have a header parsing bug. If the terminating \r\n occur starting
819 * at offset 256, 257 or 258 of output then it will not properly parse
820 * the headers. Curiously it doesn't exhibit this problem at 512, 513.
821 * We are guessing that this is because their initial read of a new request
822 * uses a 256 byte buffer, and subsequent reads use a larger buffer.
823 * So the problem might exist at different offsets as well.
825 * This should also work on keepalive connections assuming they use the
826 * same small buffer for the first read of each new request.
828 * At any rate, we check the bytes written so far and, if we are about to
829 * tickle the bug, we instead insert a bogus padding header. Since the bug
830 * manifests as a broken image in Navigator, users blame the server. :(
831 * It is more expensive to check the User-Agent than it is to just add the
832 * bytes, so we haven't used the BrowserMatch feature here.
834 static void terminate_header(apr_bucket_brigade *bb)
836 char tmp[] = "X-Pad: avoid browser bug" CRLF;
841 (void) apr_brigade_length(bb, 1, &len);
843 if (len >= 255 && len <= 257) {
844 buflen = strlen(tmp);
845 ap_xlate_proto_to_ascii(tmp, buflen);
846 apr_brigade_write(bb, NULL, NULL, tmp, buflen);
848 buflen = strlen(crlf);
849 ap_xlate_proto_to_ascii(crlf, buflen);
850 apr_brigade_write(bb, NULL, NULL, crlf, buflen);
853 AP_DECLARE_NONSTD(int) ap_send_http_trace(request_rec *r)
855 core_server_config *conf;
857 apr_bucket_brigade *bb;
861 char *bodyread = NULL, *bodyoff;
862 apr_size_t bodylen = 0;
864 long res = -1; /* init to avoid gcc -Wall warning */
866 if (r->method_number != M_TRACE) {
870 /* Get the original request */
874 conf = (core_server_config *)ap_get_module_config(r->server->module_config,
877 if (conf->trace_enable == AP_TRACE_DISABLE) {
878 apr_table_setn(r->notes, "error-notes",
879 "TRACE denied by server configuration");
880 return HTTP_METHOD_NOT_ALLOWED;
883 if (conf->trace_enable == AP_TRACE_EXTENDED)
884 /* XX should be = REQUEST_CHUNKED_PASS */
885 body = REQUEST_CHUNKED_DECHUNK;
887 body = REQUEST_NO_BODY;
889 if ((rv = ap_setup_client_block(r, body))) {
890 if (rv == HTTP_REQUEST_ENTITY_TOO_LARGE)
891 apr_table_setn(r->notes, "error-notes",
892 "TRACE with a request body is not allowed");
896 if (ap_should_client_block(r)) {
898 if (r->remaining > 0) {
899 if (r->remaining > 65536) {
900 apr_table_setn(r->notes, "error-notes",
901 "Extended TRACE request bodies cannot exceed 64k\n");
902 return HTTP_REQUEST_ENTITY_TOO_LARGE;
904 /* always 32 extra bytes to catch chunk header exceptions */
905 bodybuf = (apr_size_t)r->remaining + 32;
908 /* Add an extra 8192 for chunk headers */
912 bodyoff = bodyread = apr_palloc(r->pool, bodybuf);
914 /* only while we have enough for a chunked header */
915 while ((!bodylen || bodybuf >= 32) &&
916 (res = ap_get_client_block(r, bodyoff, bodybuf)) > 0) {
921 if (res > 0 && bodybuf < 32) {
922 /* discard_rest_of_request_body into our buffer */
923 while (ap_get_client_block(r, bodyread, bodylen) > 0)
925 apr_table_setn(r->notes, "error-notes",
926 "Extended TRACE request bodies cannot exceed 64k\n");
927 return HTTP_REQUEST_ENTITY_TOO_LARGE;
931 return HTTP_BAD_REQUEST;
935 ap_set_content_type(r, "message/http");
937 /* Now we recreate the request, and echo it back */
939 bb = apr_brigade_create(r->pool, r->connection->bucket_alloc);
940 apr_brigade_putstrs(bb, NULL, NULL, r->the_request, CRLF, NULL);
943 apr_table_do((int (*) (void *, const char *, const char *))
944 form_header_field, (void *) &h, r->headers_in, NULL);
945 apr_brigade_puts(bb, NULL, NULL, CRLF);
947 /* If configured to accept a body, echo the body */
949 b = apr_bucket_pool_create(bodyread, bodylen,
950 r->pool, bb->bucket_alloc);
951 APR_BRIGADE_INSERT_TAIL(bb, b);
954 ap_pass_brigade(r->output_filters, bb);
959 typedef struct header_filter_ctx {
963 AP_CORE_DECLARE_NONSTD(apr_status_t) ap_http_header_filter(ap_filter_t *f,
964 apr_bucket_brigade *b)
966 request_rec *r = f->r;
967 conn_rec *c = r->connection;
968 const char *clheader;
969 const char *protocol;
971 apr_bucket_brigade *b2;
973 header_filter_ctx *ctx = f->ctx;
976 AP_DEBUG_ASSERT(!r->main);
978 if (r->header_only) {
980 ctx = f->ctx = apr_pcalloc(r->pool, sizeof(header_filter_ctx));
982 else if (ctx->headers_sent) {
983 apr_brigade_destroy(b);
988 for (e = APR_BRIGADE_FIRST(b);
989 e != APR_BRIGADE_SENTINEL(b);
990 e = APR_BUCKET_NEXT(e))
992 if (AP_BUCKET_IS_ERROR(e)) {
993 ap_bucket_error *eb = e->data;
995 ap_die(eb->status, r);
996 return AP_FILTER_ERROR;
1000 if (r->assbackwards) {
1002 ap_remove_output_filter(f);
1003 return ap_pass_brigade(f->next, b);
1007 * Now that we are ready to send a response, we need to combine the two
1008 * header field tables into a single table. If we don't do this, our
1009 * later attempts to set or unset a given fieldname might be bypassed.
1011 if (!apr_is_empty_table(r->err_headers_out)) {
1012 r->headers_out = apr_table_overlay(r->pool, r->err_headers_out,
1017 * Remove the 'Vary' header field if the client can't handle it.
1018 * Since this will have nasty effects on HTTP/1.1 caches, force
1019 * the response into HTTP/1.0 mode.
1021 * Note: the force-response-1.0 should come before the call to
1022 * basic_http_header_check()
1024 if (apr_table_get(r->subprocess_env, "force-no-vary") != NULL) {
1025 apr_table_unset(r->headers_out, "Vary");
1026 r->proto_num = HTTP_VERSION(1,0);
1027 apr_table_set(r->subprocess_env, "force-response-1.0", "1");
1034 * Now remove any ETag response header field if earlier processing
1035 * says so (such as a 'FileETag None' directive).
1037 if (apr_table_get(r->notes, "no-etag") != NULL) {
1038 apr_table_unset(r->headers_out, "ETag");
1041 /* determine the protocol and whether we should use keepalives. */
1042 basic_http_header_check(r, &protocol);
1043 ap_set_keepalive(r);
1046 apr_table_mergen(r->headers_out, "Transfer-Encoding", "chunked");
1047 apr_table_unset(r->headers_out, "Content-Length");
1050 ctype = ap_make_content_type(r, r->content_type);
1051 if (strcasecmp(ctype, NO_CONTENT_TYPE)) {
1052 apr_table_setn(r->headers_out, "Content-Type", ctype);
1055 if (r->content_encoding) {
1056 apr_table_setn(r->headers_out, "Content-Encoding",
1057 r->content_encoding);
1060 if (!apr_is_empty_array(r->content_languages)) {
1062 char **languages = (char **)(r->content_languages->elts);
1063 for (i = 0; i < r->content_languages->nelts; ++i) {
1064 apr_table_mergen(r->headers_out, "Content-Language", languages[i]);
1069 * Control cachability for non-cachable responses if not already set by
1070 * some other part of the server configuration.
1072 if (r->no_cache && !apr_table_get(r->headers_out, "Expires")) {
1073 char *date = apr_palloc(r->pool, APR_RFC822_DATE_LEN);
1074 ap_recent_rfc822_date(date, r->request_time);
1075 apr_table_addn(r->headers_out, "Expires", date);
1078 /* This is a hack, but I can't find anyway around it. The idea is that
1079 * we don't want to send out 0 Content-Lengths if it is a head request.
1080 * This happens when modules try to outsmart the server, and return
1081 * if they see a HEAD request. Apache 1.3 handlers were supposed to
1082 * just return in that situation, and the core handled the HEAD. In
1083 * 2.0, if a handler returns, then the core sends an EOS bucket down
1084 * the filter stack, and the content-length filter computes a C-L of
1085 * zero and that gets put in the headers, and we end up sending a
1086 * zero C-L to the client. We can't just remove the C-L filter,
1087 * because well behaved 2.0 handlers will send their data down the stack,
1088 * and we will compute a real C-L for the head request. RBB
1091 && (clheader = apr_table_get(r->headers_out, "Content-Length"))
1092 && !strcmp(clheader, "0")) {
1093 apr_table_unset(r->headers_out, "Content-Length");
1096 b2 = apr_brigade_create(r->pool, c->bucket_alloc);
1097 basic_http_header(r, b2, protocol);
1102 if (r->status == HTTP_NOT_MODIFIED) {
1103 apr_table_do((int (*)(void *, const char *, const char *)) form_header_field,
1104 (void *) &h, r->headers_out,
1114 "Proxy-Authenticate",
1120 send_all_header_fields(&h, r);
1123 terminate_header(b2);
1125 ap_pass_brigade(f->next, b2);
1127 if (r->header_only) {
1128 apr_brigade_destroy(b);
1129 ctx->headers_sent = 1;
1133 r->sent_bodyct = 1; /* Whatever follows is real body stuff... */
1136 /* We can't add this filter until we have already sent the headers.
1137 * If we add it before this point, then the headers will be chunked
1138 * as well, and that is just wrong.
1140 ap_add_output_filter("CHUNK", NULL, r, r->connection);
1143 /* Don't remove this filter until after we have added the CHUNK filter.
1144 * Otherwise, f->next won't be the CHUNK filter and thus the first
1145 * brigade won't be chunked properly.
1147 ap_remove_output_filter(f);
1148 return ap_pass_brigade(f->next, b);
1151 /* In HTTP/1.1, any method can have a body. However, most GET handlers
1152 * wouldn't know what to do with a request body if they received one.
1153 * This helper routine tests for and reads any message body in the request,
1154 * simply discarding whatever it receives. We need to do this because
1155 * failing to read the request body would cause it to be interpreted
1156 * as the next request on a persistent connection.
1158 * Since we return an error status if the request is malformed, this
1159 * routine should be called at the beginning of a no-body handler, e.g.,
1161 * if ((retval = ap_discard_request_body(r)) != OK) {
1165 AP_DECLARE(int) ap_discard_request_body(request_rec *r)
1167 apr_bucket_brigade *bb, *kept_body = NULL;
1170 core_dir_conf *dconf;
1171 apr_size_t left = 0;
1173 /* Sometimes we'll get in a state where the input handling has
1174 * detected an error where we want to drop the connection, so if
1175 * that's the case, don't read the data as that is what we're trying
1178 * This function is also a no-op on a subrequest.
1180 if (r->main || r->connection->keepalive == AP_CONN_CLOSE ||
1181 ap_status_drops_connection(r->status)) {
1185 /* We may want to save this body away if the administrator has
1186 * asked us to do so for this directory. This allows the body
1187 * to be reexamined by filters such as mod_include, even though
1188 * the main request has no need for this body.
1190 if (!r->kept_body) {
1191 dconf = ap_get_module_config(r->per_dir_config,
1193 if (dconf->keep_body > 0) {
1194 left = dconf->keep_body;
1195 kept_body = apr_brigade_create(r->pool, r->connection->bucket_alloc);
1199 bb = apr_brigade_create(r->pool, r->connection->bucket_alloc);
1204 rv = ap_get_brigade(r->input_filters, bb, AP_MODE_READBYTES,
1205 APR_BLOCK_READ, HUGE_STRING_LEN);
1207 if (rv != APR_SUCCESS) {
1208 /* FIXME: If we ever have a mapping from filters (apr_status_t)
1209 * to HTTP error codes, this would be a good place for them.
1211 * If we received the special case AP_FILTER_ERROR, it means
1212 * that the filters have already handled this error.
1213 * Otherwise, we should assume we have a bad request.
1215 if (rv == AP_FILTER_ERROR) {
1216 apr_brigade_destroy(bb);
1220 apr_brigade_destroy(bb);
1221 return HTTP_BAD_REQUEST;
1225 for (bucket = APR_BRIGADE_FIRST(bb);
1226 bucket != APR_BRIGADE_SENTINEL(bb);
1227 bucket = APR_BUCKET_NEXT(bucket))
1232 if (APR_BUCKET_IS_EOS(bucket)) {
1237 /* These are metadata buckets. */
1238 if (bucket->length == 0) {
1242 /* We MUST read because in case we have an unknown-length
1243 * bucket or one that morphs, we want to exhaust it.
1245 rv = apr_bucket_read(bucket, &data, &len, APR_BLOCK_READ);
1246 if (rv != APR_SUCCESS) {
1247 apr_brigade_destroy(bb);
1248 return HTTP_BAD_REQUEST;
1251 /* If we have been asked to, keep the data up until the
1252 * configured limit. If the limit is exceeded, we return an
1253 * HTTP_REQUEST_ENTITY_TOO_LARGE response so the caller is
1254 * clear the server couldn't handle their request.
1258 apr_bucket_copy(bucket, &e);
1259 APR_BRIGADE_INSERT_TAIL(kept_body, e);
1263 apr_brigade_destroy(bb);
1264 apr_brigade_destroy(kept_body);
1265 return HTTP_REQUEST_ENTITY_TOO_LARGE;
1270 apr_brigade_cleanup(bb);
1271 } while (!seen_eos);
1274 r->kept_body = kept_body;
1280 /* Here we deal with getting the request message body from the client.
1281 * Whether or not the request contains a body is signaled by the presence
1282 * of a non-zero Content-Length or by a Transfer-Encoding: chunked.
1284 * Note that this is more complicated than it was in Apache 1.1 and prior
1285 * versions, because chunked support means that the module does less.
1287 * The proper procedure is this:
1289 * 1. Call ap_setup_client_block() near the beginning of the request
1290 * handler. This will set up all the necessary properties, and will
1291 * return either OK, or an error code. If the latter, the module should
1292 * return that error code. The second parameter selects the policy to
1293 * apply if the request message indicates a body, and how a chunked
1294 * transfer-coding should be interpreted. Choose one of
1296 * REQUEST_NO_BODY Send 413 error if message has any body
1297 * REQUEST_CHUNKED_ERROR Send 411 error if body without Content-Length
1298 * REQUEST_CHUNKED_DECHUNK If chunked, remove the chunks for me.
1299 * REQUEST_CHUNKED_PASS If chunked, pass the chunk headers with body.
1301 * In order to use the last two options, the caller MUST provide a buffer
1302 * large enough to hold a chunk-size line, including any extensions.
1304 * 2. When you are ready to read a body (if any), call ap_should_client_block().
1305 * This will tell the module whether or not to read input. If it is 0,
1306 * the module should assume that there is no message body to read.
1308 * 3. Finally, call ap_get_client_block in a loop. Pass it a buffer and its size.
1309 * It will put data into the buffer (not necessarily a full buffer), and
1310 * return the length of the input block. When it is done reading, it will
1311 * return 0 if EOF, or -1 if there was an error.
1312 * If an error occurs on input, we force an end to keepalive.
1314 * This step also sends a 100 Continue response to HTTP/1.1 clients if appropriate.
1317 AP_DECLARE(int) ap_setup_client_block(request_rec *r, int read_policy)
1319 const char *tenc = apr_table_get(r->headers_in, "Transfer-Encoding");
1320 const char *lenp = apr_table_get(r->headers_in, "Content-Length");
1322 r->read_body = read_policy;
1323 r->read_chunked = 0;
1327 if (strcasecmp(tenc, "chunked")) {
1328 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
1329 "Unknown Transfer-Encoding %s", tenc);
1330 return HTTP_NOT_IMPLEMENTED;
1332 if (r->read_body == REQUEST_CHUNKED_ERROR) {
1333 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
1334 "chunked Transfer-Encoding forbidden: %s", r->uri);
1335 return (lenp) ? HTTP_BAD_REQUEST : HTTP_LENGTH_REQUIRED;
1338 r->read_chunked = 1;
1343 if (apr_strtoff(&r->remaining, lenp, &endstr, 10)
1344 || *endstr || r->remaining < 0) {
1346 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
1347 "Invalid Content-Length");
1348 return HTTP_BAD_REQUEST;
1352 if ((r->read_body == REQUEST_NO_BODY)
1353 && (r->read_chunked || (r->remaining > 0))) {
1354 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
1355 "%s with body is not allowed for %s", r->method, r->uri);
1356 return HTTP_REQUEST_ENTITY_TOO_LARGE;
1361 /* Make sure ap_getline() didn't leave any droppings. */
1362 core_request_config *req_cfg =
1363 (core_request_config *)ap_get_module_config(r->request_config,
1365 AP_DEBUG_ASSERT(APR_BRIGADE_EMPTY(req_cfg->bb));
1372 AP_DECLARE(int) ap_should_client_block(request_rec *r)
1374 /* First check if we have already read the request body */
1376 if (r->read_length || (!r->read_chunked && (r->remaining <= 0))) {
1383 /* get_client_block is called in a loop to get the request message body.
1384 * This is quite simple if the client includes a content-length
1385 * (the normal case), but gets messy if the body is chunked. Note that
1386 * r->remaining is used to maintain state across calls and that
1387 * r->read_length is the total number of bytes given to the caller
1388 * across all invocations. It is messy because we have to be careful not
1389 * to read past the data provided by the client, since these reads block.
1390 * Returns 0 on End-of-body, -1 on error or premature chunk end.
1393 AP_DECLARE(long) ap_get_client_block(request_rec *r, char *buffer,
1397 apr_bucket_brigade *bb;
1399 if (r->remaining < 0 || (!r->read_chunked && r->remaining == 0)) {
1403 bb = apr_brigade_create(r->pool, r->connection->bucket_alloc);
1405 r->connection->keepalive = AP_CONN_CLOSE;
1409 rv = ap_get_brigade(r->input_filters, bb, AP_MODE_READBYTES,
1410 APR_BLOCK_READ, bufsiz);
1412 /* We lose the failure code here. This is why ap_get_client_block should
1415 if (rv != APR_SUCCESS) {
1416 /* if we actually fail here, we want to just return and
1417 * stop trying to read data from the client.
1419 r->connection->keepalive = AP_CONN_CLOSE;
1420 apr_brigade_destroy(bb);
1424 /* If this fails, it means that a filter is written incorrectly and that
1425 * it needs to learn how to properly handle APR_BLOCK_READ requests by
1426 * returning data when requested.
1428 AP_DEBUG_ASSERT(!APR_BRIGADE_EMPTY(bb));
1430 /* Check to see if EOS in the brigade.
1432 * If so, we have to leave a nugget for the *next* ap_get_client_block
1435 if (APR_BUCKET_IS_EOS(APR_BRIGADE_LAST(bb))) {
1436 if (r->read_chunked) {
1444 rv = apr_brigade_flatten(bb, buffer, &bufsiz);
1445 if (rv != APR_SUCCESS) {
1446 apr_brigade_destroy(bb);
1451 r->read_length += bufsiz;
1453 apr_brigade_destroy(bb);
1457 /* Filter to handle any error buckets on output */
1458 apr_status_t ap_http_outerror_filter(ap_filter_t *f,
1459 apr_bucket_brigade *b)
1461 request_rec *r = f->r;
1464 for (e = APR_BRIGADE_FIRST(b);
1465 e != APR_BRIGADE_SENTINEL(b);
1466 e = APR_BUCKET_NEXT(e))
1468 if (AP_BUCKET_IS_ERROR(e)) {
1470 * Start of error handling state tree. Just one condition
1473 if (((ap_bucket_error *)(e->data))->status == HTTP_BAD_GATEWAY) {
1474 /* stream aborted and we have not ended it yet */
1475 r->connection->keepalive = AP_CONN_CLOSE;
1480 return ap_pass_brigade(f->next, b);
1483 typedef struct kept_body_filter_ctx {
1485 apr_off_t remaining;
1489 * Initialisation of filter to handle a kept body on subrequests.
1491 * If a body is to be reinserted into a subrequest, any chunking will have
1492 * been removed from the body during storage. We need to change the request
1493 * from Transfer-Encoding: chunked to an explicit Content-Length.
1495 int ap_kept_body_filter_init(ap_filter_t *f) {
1496 apr_off_t length = 0;
1497 request_rec *r = f->r;
1498 apr_bucket_brigade *kept_body = r->kept_body;
1501 apr_table_unset(r->headers_in, "Transfer-Encoding");
1502 apr_brigade_length(kept_body, 1, &length);
1503 apr_table_set(r->headers_in, "Content-Length", apr_off_t_toa(r->pool, length));
1510 * Filter to handle a kept body on subrequests.
1512 * If a body has been previously kept by the request, and if a subrequest wants
1513 * to re-insert the body into the request, this input filter makes it happen.
1515 apr_status_t ap_kept_body_filter(ap_filter_t *f, apr_bucket_brigade *b,
1516 ap_input_mode_t mode, apr_read_type_e block,
1517 apr_off_t readbytes) {
1518 request_rec *r = f->r;
1519 apr_bucket_brigade *kept_body = r->kept_body;
1520 kept_body_ctx_t *ctx = f->ctx;
1521 apr_bucket *ec, *e2;
1524 /* just get out of the way of things we don't want. */
1525 if (!kept_body || (mode != AP_MODE_READBYTES && mode != AP_MODE_GETLINE)) {
1526 return ap_get_brigade(f->next, b, mode, block, readbytes);
1529 /* set up the context if it does not already exist */
1531 f->ctx = ctx = apr_palloc(f->r->pool, sizeof(*ctx));
1533 apr_brigade_length(kept_body, 1, &ctx->remaining);
1536 /* kept_body is finished, send next filter */
1537 if (ctx->remaining <= 0) {
1538 return ap_get_brigade(f->next, b, mode, block, readbytes);
1541 /* send all of the kept_body, but no more */
1542 if (readbytes > ctx->remaining) {
1543 readbytes = ctx->remaining;
1546 /* send part of the kept_body */
1547 if ((rv = apr_brigade_partition(kept_body, ctx->offset, &ec)) != APR_SUCCESS) {
1548 ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
1549 "apr_brigade_partition() failed on kept_body at %" APR_OFF_T_FMT, ctx->offset);
1552 if ((rv = apr_brigade_partition(kept_body, ctx->offset + readbytes, &e2)) != APR_SUCCESS) {
1553 ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
1554 "apr_brigade_partition() failed on kept_body at %" APR_OFF_T_FMT, ctx->offset + readbytes);
1563 if (apr_bucket_copy(ec, &foo) != APR_SUCCESS) {
1564 /* As above; this should not fail since the bucket has
1565 * a known length, but just to be sure, this takes
1566 * care of uncopyable buckets that do somehow manage
1567 * to slip through. */
1568 /* XXX: check for failure? */
1569 apr_bucket_read(ec, &str, &len, APR_BLOCK_READ);
1570 apr_bucket_copy(ec, &foo);
1572 APR_BRIGADE_INSERT_TAIL(b, foo);
1573 ec = APR_BUCKET_NEXT(ec);
1576 ctx->remaining -= readbytes;
1577 ctx->offset += readbytes;