]> granicus.if.org Git - apache/blob - modules/http/http_filters.c
Extend the socket callbacks in event to allow a timeout on the I/O callback.
[apache] / modules / http / http_filters.c
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
58 APLOG_USE_MODULE(http);
59
60 #define INVALID_CHAR -2
61
62 typedef struct http_filter_ctx
63 {
64     apr_off_t remaining;
65     apr_off_t limit;
66     apr_off_t limit_used;
67     apr_int32_t chunk_used;
68     apr_int16_t chunkbits;
69     enum
70     {
71         BODY_NONE, /* streamed data */
72         BODY_LENGTH, /* data constrained by content length */
73         BODY_CHUNK, /* chunk expected */
74         BODY_CHUNK_PART, /* chunk digits */
75         BODY_CHUNK_EXT, /* chunk extension */
76         BODY_CHUNK_DATA, /* data constrained by chunked encoding */
77         BODY_CHUNK_END, /* chunk terminating CRLF */
78         BODY_CHUNK_TRAILER /* trailers */
79     } state :3;
80     int eos_sent :1;
81 } http_ctx_t;
82
83 /**
84  * Parse a chunk line with optional extension, detect overflow.
85  * There are two error cases:
86  *  1) If the conversion would require too many bits, APR_EGENERAL is returned.
87  *  2) If the conversion used the correct number of bits, but an overflow
88  *     caused only the sign bit to flip, then APR_ENOSPC is returned.
89  * In general, any negative number can be considered an overflow error.
90  */
91 static apr_status_t parse_chunk_size(http_ctx_t *ctx, const char *buffer,
92         apr_size_t len, int linelimit)
93 {
94     apr_size_t i = 0;
95
96     while (i < len) {
97         char c = buffer[i];
98
99         ap_xlate_proto_from_ascii(&c, 1);
100
101         /* handle CRLF after the chunk */
102         if (ctx->state == BODY_CHUNK_END) {
103             if (c == LF) {
104                 ctx->state = BODY_CHUNK;
105             }
106             i++;
107             continue;
108         }
109
110         /* handle start of the chunk */
111         if (ctx->state == BODY_CHUNK) {
112             if (!apr_isxdigit(c)) {
113                 /*
114                  * Detect invalid character at beginning. This also works for empty
115                  * chunk size lines.
116                  */
117                 return APR_EGENERAL;
118             }
119             else {
120                 ctx->state = BODY_CHUNK_PART;
121             }
122             ctx->remaining = 0;
123             ctx->chunkbits = sizeof(long) * 8;
124             ctx->chunk_used = 0;
125         }
126
127         /* handle a chunk part, or a chunk extension */
128         /*
129          * In theory, we are supposed to expect CRLF only, but our
130          * test suite sends LF only. Tolerate a missing CR.
131          */
132         if (c == ';' || c == CR) {
133             ctx->state = BODY_CHUNK_EXT;
134         }
135         else if (c == LF) {
136             if (ctx->remaining) {
137                 ctx->state = BODY_CHUNK_DATA;
138             }
139             else {
140                 ctx->state = BODY_CHUNK_TRAILER;
141             }
142         }
143         else if (ctx->state != BODY_CHUNK_EXT) {
144             int xvalue = 0;
145
146             /* ignore leading zeros */
147             if (!ctx->remaining && c == '0') {
148                 i++;
149                 continue;
150             }
151
152             if (c >= '0' && c <= '9') {
153                 xvalue = c - '0';
154             }
155             else if (c >= 'A' && c <= 'F') {
156                 xvalue = c - 'A' + 0xa;
157             }
158             else if (c >= 'a' && c <= 'f') {
159                 xvalue = c - 'a' + 0xa;
160             }
161             else {
162                 /* bogus character */
163                 return APR_EGENERAL;
164             }
165
166             ctx->remaining = (ctx->remaining << 4) | xvalue;
167             ctx->chunkbits -= 4;
168             if (ctx->chunkbits <= 0 || ctx->remaining < 0) {
169                 /* overflow */
170                 return APR_ENOSPC;
171             }
172
173         }
174
175         i++;
176     }
177
178     /* sanity check */
179     ctx->chunk_used += len;
180     if (ctx->chunk_used < 0 || ctx->chunk_used > linelimit) {
181         return APR_ENOSPC;
182     }
183
184     return APR_SUCCESS;
185 }
186
187 /* This is the HTTP_INPUT filter for HTTP requests and responses from
188  * proxied servers (mod_proxy).  It handles chunked and content-length
189  * bodies.  This can only be inserted/used after the headers
190  * are successfully parsed.
191  */
192 apr_status_t ap_http_filter(ap_filter_t *f, apr_bucket_brigade *b,
193         ap_input_mode_t mode, apr_read_type_e block, apr_off_t readbytes)
194 {
195     apr_bucket *e;
196     http_ctx_t *ctx = f->ctx;
197     apr_status_t rv;
198     apr_off_t totalread;
199     int again;
200
201     /* just get out of the way of things we don't want. */
202     if (mode != AP_MODE_READBYTES && mode != AP_MODE_GETLINE) {
203         return ap_get_brigade(f->next, b, mode, block, readbytes);
204     }
205
206     if (!ctx) {
207         const char *tenc, *lenp;
208         f->ctx = ctx = apr_pcalloc(f->r->pool, sizeof(*ctx));
209         ctx->state = BODY_NONE;
210
211         /* LimitRequestBody does not apply to proxied responses.
212          * Consider implementing this check in its own filter.
213          * Would adding a directive to limit the size of proxied
214          * responses be useful?
215          */
216         if (!f->r->proxyreq) {
217             ctx->limit = ap_get_limit_req_body(f->r);
218         }
219         else {
220             ctx->limit = 0;
221         }
222
223         tenc = apr_table_get(f->r->headers_in, "Transfer-Encoding");
224         lenp = apr_table_get(f->r->headers_in, "Content-Length");
225
226         if (tenc) {
227             if (strcasecmp(tenc, "chunked") == 0 /* fast path */
228                     || ap_find_last_token(f->r->pool, tenc, "chunked")) {
229                 ctx->state = BODY_CHUNK;
230             }
231             else if (f->r->proxyreq == PROXYREQ_RESPONSE) {
232                 /* http://tools.ietf.org/html/draft-ietf-httpbis-p1-messaging-23
233                  * Section 3.3.3.3: "If a Transfer-Encoding header field is
234                  * present in a response and the chunked transfer coding is not
235                  * the final encoding, the message body length is determined by
236                  * reading the connection until it is closed by the server."
237                  */
238                 ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, f->r, APLOGNO(02555)
239                               "Unknown Transfer-Encoding: %s;"
240                               " using read-until-close", tenc);
241                 tenc = NULL;
242             }
243             else {
244                 /* Something that isn't a HTTP request, unless some future
245                  * edition defines new transfer encodings, is unsupported.
246                  */
247                 ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, f->r, APLOGNO(01585)
248                               "Unknown Transfer-Encoding: %s", tenc);
249                 return APR_EGENERAL;
250             }
251             lenp = NULL;
252         }
253         if (lenp) {
254             char *endstr;
255
256             ctx->state = BODY_LENGTH;
257
258             /* Protects against over/underflow, non-digit chars in the
259              * string (excluding leading space) (the endstr checks)
260              * and a negative number. */
261             if (apr_strtoff(&ctx->remaining, lenp, &endstr, 10)
262                     || endstr == lenp || *endstr || ctx->remaining < 0) {
263
264                 ctx->remaining = 0;
265                 ap_log_rerror(
266                         APLOG_MARK, APLOG_INFO, 0, f->r, APLOGNO(01587)
267                         "Invalid Content-Length");
268
269                 return APR_ENOSPC;
270             }
271
272             /* If we have a limit in effect and we know the C-L ahead of
273              * time, stop it here if it is invalid.
274              */
275             if (ctx->limit && ctx->limit < ctx->remaining) {
276                 ap_log_rerror(
277                         APLOG_MARK, APLOG_INFO, 0, f->r, APLOGNO(01588)
278                         "Requested content-length of %" APR_OFF_T_FMT
279                         " is larger than the configured limit"
280                         " of %" APR_OFF_T_FMT, ctx->remaining, ctx->limit);
281                 return APR_ENOSPC;
282             }
283         }
284
285         /* If we don't have a request entity indicated by the headers, EOS.
286          * (BODY_NONE is a valid intermediate state due to trailers,
287          *  but it isn't a valid starting state.)
288          *
289          * RFC 2616 Section 4.4 note 5 states that connection-close
290          * is invalid for a request entity - request bodies must be
291          * denoted by C-L or T-E: chunked.
292          *
293          * Note that since the proxy uses this filter to handle the
294          * proxied *response*, proxy responses MUST be exempt.
295          */
296         if (ctx->state == BODY_NONE && f->r->proxyreq != PROXYREQ_RESPONSE) {
297             e = apr_bucket_eos_create(f->c->bucket_alloc);
298             APR_BRIGADE_INSERT_TAIL(b, e);
299             ctx->eos_sent = 1;
300             return APR_SUCCESS;
301         }
302
303         /* Since we're about to read data, send 100-Continue if needed.
304          * Only valid on chunked and C-L bodies where the C-L is > 0. */
305         if ((ctx->state == BODY_CHUNK
306                 || (ctx->state == BODY_LENGTH && ctx->remaining > 0))
307                 && f->r->expecting_100 && f->r->proto_num >= HTTP_VERSION(1,1)
308                 && !(f->r->eos_sent || f->r->bytes_sent)) {
309             if (!ap_is_HTTP_SUCCESS(f->r->status)) {
310                 ctx->state = BODY_NONE;
311                 ctx->eos_sent = 1;
312             }
313             else {
314                 char *tmp;
315                 int len;
316                 apr_bucket_brigade *bb;
317
318                 bb = apr_brigade_create(f->r->pool, f->c->bucket_alloc);
319
320                 /* if we send an interim response, we're no longer
321                  * in a state of expecting one.
322                  */
323                 f->r->expecting_100 = 0;
324                 tmp = apr_pstrcat(f->r->pool, AP_SERVER_PROTOCOL, " ",
325                         ap_get_status_line(HTTP_CONTINUE), CRLF CRLF, NULL);
326                 len = strlen(tmp);
327                 ap_xlate_proto_to_ascii(tmp, len);
328                 e = apr_bucket_pool_create(tmp, len, f->r->pool,
329                         f->c->bucket_alloc);
330                 APR_BRIGADE_INSERT_HEAD(bb, e);
331                 e = apr_bucket_flush_create(f->c->bucket_alloc);
332                 APR_BRIGADE_INSERT_TAIL(bb, e);
333
334                 ap_pass_brigade(f->c->output_filters, bb);
335             }
336         }
337     }
338
339     /* sanity check in case we're read twice */
340     if (ctx->eos_sent) {
341         e = apr_bucket_eos_create(f->c->bucket_alloc);
342         APR_BRIGADE_INSERT_TAIL(b, e);
343         return APR_SUCCESS;
344     }
345
346     do {
347         apr_brigade_cleanup(b);
348         again = 0; /* until further notice */
349
350         /* read and handle the brigade */
351         switch (ctx->state) {
352         case BODY_CHUNK:
353         case BODY_CHUNK_PART:
354         case BODY_CHUNK_EXT:
355         case BODY_CHUNK_END: {
356
357             rv = ap_get_brigade(f->next, b, AP_MODE_GETLINE, block, 0);
358
359             /* for timeout */
360             if (block == APR_NONBLOCK_READ
361                     && ((rv == APR_SUCCESS && APR_BRIGADE_EMPTY(b))
362                             || (APR_STATUS_IS_EAGAIN(rv)))) {
363                 return APR_EAGAIN;
364             }
365
366             if (rv == APR_EOF) {
367                 return APR_INCOMPLETE;
368             }
369
370             if (rv != APR_SUCCESS) {
371                 return rv;
372             }
373
374             e = APR_BRIGADE_FIRST(b);
375             while (e != APR_BRIGADE_SENTINEL(b)) {
376                 const char *buffer;
377                 apr_size_t len;
378
379                 if (!APR_BUCKET_IS_METADATA(e)) {
380                     rv = apr_bucket_read(e, &buffer, &len, APR_BLOCK_READ);
381
382                     if (rv == APR_SUCCESS) {
383                         rv = parse_chunk_size(ctx, buffer, len,
384                                 f->r->server->limit_req_fieldsize);
385                     }
386                     if (rv != APR_SUCCESS) {
387                         ap_log_rerror(
388                                 APLOG_MARK, APLOG_INFO, rv, f->r, APLOGNO(01590) "Error reading chunk %s ", (APR_ENOSPC == rv) ? "(overflow)" : "");
389                         return rv;
390                     }
391                 }
392
393                 apr_bucket_delete(e);
394                 e = APR_BRIGADE_FIRST(b);
395             }
396             again = 1; /* come around again */
397
398             if (ctx->state == BODY_CHUNK_TRAILER) {
399                 ap_get_mime_headers(f->r);
400                 e = apr_bucket_eos_create(f->c->bucket_alloc);
401                 APR_BRIGADE_INSERT_TAIL(b, e);
402                 ctx->eos_sent = 1;
403                 return APR_SUCCESS;
404             }
405
406             break;
407         }
408         case BODY_NONE:
409         case BODY_LENGTH:
410         case BODY_CHUNK_DATA: {
411
412             /* Ensure that the caller can not go over our boundary point. */
413             if (ctx->state != BODY_NONE && ctx->remaining < readbytes) {
414                 readbytes = ctx->remaining;
415             }
416             if (readbytes > 0) {
417
418                 rv = ap_get_brigade(f->next, b, mode, block, readbytes);
419
420                 /* for timeout */
421                 if (block == APR_NONBLOCK_READ
422                         && ((rv == APR_SUCCESS && APR_BRIGADE_EMPTY(b))
423                                 || (APR_STATUS_IS_EAGAIN(rv)))) {
424                     return APR_EAGAIN;
425                 }
426
427                 if (rv == APR_EOF && ctx->state != BODY_NONE
428                         && ctx->remaining > 0) {
429                     return APR_INCOMPLETE;
430                 }
431
432                 if (rv != APR_SUCCESS) {
433                     return rv;
434                 }
435
436                 /* How many bytes did we just read? */
437                 apr_brigade_length(b, 0, &totalread);
438
439                 /* If this happens, we have a bucket of unknown length.  Die because
440                  * it means our assumptions have changed. */
441                 AP_DEBUG_ASSERT(totalread >= 0);
442
443                 if (ctx->state != BODY_NONE) {
444                     ctx->remaining -= totalread;
445                     if (ctx->remaining > 0) {
446                         e = APR_BRIGADE_LAST(b);
447                         if (APR_BUCKET_IS_EOS(e)) {
448                             apr_bucket_delete(e);
449                             return APR_INCOMPLETE;
450                         }
451                     }
452                     else if (ctx->state == BODY_CHUNK_DATA) {
453                         /* next chunk please */
454                         ctx->state = BODY_CHUNK_END;
455                         ctx->chunk_used = 0;
456                     }
457                 }
458
459             }
460
461             /* If we have no more bytes remaining on a C-L request,
462              * save the caller a round trip to discover EOS.
463              */
464             if (ctx->state == BODY_LENGTH && ctx->remaining == 0) {
465                 e = apr_bucket_eos_create(f->c->bucket_alloc);
466                 APR_BRIGADE_INSERT_TAIL(b, e);
467                 ctx->eos_sent = 1;
468             }
469
470             /* We have a limit in effect. */
471             if (ctx->limit) {
472                 /* FIXME: Note that we might get slightly confused on chunked inputs
473                  * as we'd need to compensate for the chunk lengths which may not
474                  * really count.  This seems to be up for interpretation.  */
475                 ctx->limit_used += totalread;
476                 if (ctx->limit < ctx->limit_used) {
477                     ap_log_rerror(
478                             APLOG_MARK, APLOG_INFO, 0, f->r, APLOGNO(01591) "Read content-length of %" APR_OFF_T_FMT " is larger than the configured limit"
479                             " of %" APR_OFF_T_FMT, ctx->limit_used, ctx->limit);
480                     return APR_ENOSPC;
481                 }
482             }
483
484             break;
485         }
486         case BODY_CHUNK_TRAILER: {
487
488             rv = ap_get_brigade(f->next, b, mode, block, readbytes);
489
490             /* for timeout */
491             if (block == APR_NONBLOCK_READ
492                     && ((rv == APR_SUCCESS && APR_BRIGADE_EMPTY(b))
493                             || (APR_STATUS_IS_EAGAIN(rv)))) {
494                 return APR_EAGAIN;
495             }
496
497             if (rv != APR_SUCCESS) {
498                 return rv;
499             }
500
501             break;
502         }
503         default: {
504             break;
505         }
506         }
507
508     } while (again);
509
510     return APR_SUCCESS;
511 }
512
513 struct check_header_ctx {
514     request_rec *r;
515     int error;
516 };
517
518 /* check a single header, to be used with apr_table_do() */
519 static int check_header(void *arg, const char *name, const char *val)
520 {
521     struct check_header_ctx *ctx = arg;
522     if (name[0] == '\0') {
523         ctx->error = 1;
524         ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, ctx->r, APLOGNO(02428)
525                       "Empty response header name, aborting request");
526         return 0;
527     }
528     if (ap_has_cntrl(name)) {
529         ctx->error = 1;
530         ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, ctx->r, APLOGNO(02429)
531                       "Response header name '%s' contains control "
532                       "characters, aborting request",
533                       name);
534         return 0;
535     }
536     if (ap_has_cntrl(val)) {
537         ctx->error = 1;
538         ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, ctx->r, APLOGNO(02430)
539                       "Response header '%s' contains control characters, "
540                       "aborting request: %s",
541                       name, val);
542         return 0;
543     }
544     return 1;
545 }
546
547 /**
548  * Check headers for HTTP conformance
549  * @return 1 if ok, 0 if bad
550  */
551 static APR_INLINE int check_headers(request_rec *r)
552 {
553     const char *loc;
554     struct check_header_ctx ctx = { 0, 0 };
555
556     ctx.r = r;
557     apr_table_do(check_header, &ctx, r->headers_out, NULL);
558     if (ctx.error)
559         return 0; /* problem has been logged by check_header() */
560
561     if ((loc = apr_table_get(r->headers_out, "Location")) != NULL) {
562         const char *scheme_end = ap_strchr_c(loc, ':');
563         const char *s = loc;
564
565         /*
566          * Check that the URI has a valid scheme and is absolute
567          * XXX Should we do a full uri parse here?
568          */
569         if (scheme_end == NULL || scheme_end == loc)
570             goto bad;
571
572         do {
573             if ((!apr_isalnum(*s) && *s != '.' && *s != '+' && *s != '-')
574                 || !apr_isascii(*s) ) {
575                 goto bad;
576             }
577         } while (++s < scheme_end);
578
579         if (scheme_end[1] != '/' || scheme_end[2] != '/')
580             goto bad;
581     }
582
583     return 1;
584
585 bad:
586     ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(02431)
587                   "Bad Location header in response: '%s', aborting request",
588                   loc);
589     return 0;
590 }
591
592 typedef struct header_struct {
593     apr_pool_t *pool;
594     apr_bucket_brigade *bb;
595 } header_struct;
596
597 /* Send a single HTTP header field to the client.  Note that this function
598  * is used in calls to apr_table_do(), so don't change its interface.
599  * It returns true unless there was a write error of some kind.
600  */
601 static int form_header_field(header_struct *h,
602                              const char *fieldname, const char *fieldval)
603 {
604 #if APR_CHARSET_EBCDIC
605     char *headfield;
606     apr_size_t len;
607
608     headfield = apr_pstrcat(h->pool, fieldname, ": ", fieldval, CRLF, NULL);
609     len = strlen(headfield);
610
611     ap_xlate_proto_to_ascii(headfield, len);
612     apr_brigade_write(h->bb, NULL, NULL, headfield, len);
613 #else
614     struct iovec vec[4];
615     struct iovec *v = vec;
616     v->iov_base = (void *)fieldname;
617     v->iov_len = strlen(fieldname);
618     v++;
619     v->iov_base = ": ";
620     v->iov_len = sizeof(": ") - 1;
621     v++;
622     v->iov_base = (void *)fieldval;
623     v->iov_len = strlen(fieldval);
624     v++;
625     v->iov_base = CRLF;
626     v->iov_len = sizeof(CRLF) - 1;
627     apr_brigade_writev(h->bb, NULL, NULL, vec, 4);
628 #endif /* !APR_CHARSET_EBCDIC */
629     return 1;
630 }
631
632 /* This routine is called by apr_table_do and merges all instances of
633  * the passed field values into a single array that will be further
634  * processed by some later routine.  Originally intended to help split
635  * and recombine multiple Vary fields, though it is generic to any field
636  * consisting of comma/space-separated tokens.
637  */
638 static int uniq_field_values(void *d, const char *key, const char *val)
639 {
640     apr_array_header_t *values;
641     char *start;
642     char *e;
643     char **strpp;
644     int  i;
645
646     values = (apr_array_header_t *)d;
647
648     e = apr_pstrdup(values->pool, val);
649
650     do {
651         /* Find a non-empty fieldname */
652
653         while (*e == ',' || apr_isspace(*e)) {
654             ++e;
655         }
656         if (*e == '\0') {
657             break;
658         }
659         start = e;
660         while (*e != '\0' && *e != ',' && !apr_isspace(*e)) {
661             ++e;
662         }
663         if (*e != '\0') {
664             *e++ = '\0';
665         }
666
667         /* Now add it to values if it isn't already represented.
668          * Could be replaced by a ap_array_strcasecmp() if we had one.
669          */
670         for (i = 0, strpp = (char **) values->elts; i < values->nelts;
671              ++i, ++strpp) {
672             if (*strpp && strcasecmp(*strpp, start) == 0) {
673                 break;
674             }
675         }
676         if (i == values->nelts) {  /* if not found */
677             *(char **)apr_array_push(values) = start;
678         }
679     } while (*e != '\0');
680
681     return 1;
682 }
683
684 /*
685  * Since some clients choke violently on multiple Vary fields, or
686  * Vary fields with duplicate tokens, combine any multiples and remove
687  * any duplicates.
688  */
689 static void fixup_vary(request_rec *r)
690 {
691     apr_array_header_t *varies;
692
693     varies = apr_array_make(r->pool, 5, sizeof(char *));
694
695     /* Extract all Vary fields from the headers_out, separate each into
696      * its comma-separated fieldname values, and then add them to varies
697      * if not already present in the array.
698      */
699     apr_table_do((int (*)(void *, const char *, const char *))uniq_field_values,
700                  (void *) varies, r->headers_out, "Vary", NULL);
701
702     /* If we found any, replace old Vary fields with unique-ified value */
703
704     if (varies->nelts > 0) {
705         apr_table_setn(r->headers_out, "Vary",
706                        apr_array_pstrcat(r->pool, varies, ','));
707     }
708 }
709
710 /* Send a request's HTTP response headers to the client.
711  */
712 static apr_status_t send_all_header_fields(header_struct *h,
713                                            const request_rec *r)
714 {
715     const apr_array_header_t *elts;
716     const apr_table_entry_t *t_elt;
717     const apr_table_entry_t *t_end;
718     struct iovec *vec;
719     struct iovec *vec_next;
720
721     elts = apr_table_elts(r->headers_out);
722     if (elts->nelts == 0) {
723         return APR_SUCCESS;
724     }
725     t_elt = (const apr_table_entry_t *)(elts->elts);
726     t_end = t_elt + elts->nelts;
727     vec = (struct iovec *)apr_palloc(h->pool, 4 * elts->nelts *
728                                      sizeof(struct iovec));
729     vec_next = vec;
730
731     /* For each field, generate
732      *    name ": " value CRLF
733      */
734     do {
735         vec_next->iov_base = (void*)(t_elt->key);
736         vec_next->iov_len = strlen(t_elt->key);
737         vec_next++;
738         vec_next->iov_base = ": ";
739         vec_next->iov_len = sizeof(": ") - 1;
740         vec_next++;
741         vec_next->iov_base = (void*)(t_elt->val);
742         vec_next->iov_len = strlen(t_elt->val);
743         vec_next++;
744         vec_next->iov_base = CRLF;
745         vec_next->iov_len = sizeof(CRLF) - 1;
746         vec_next++;
747         t_elt++;
748     } while (t_elt < t_end);
749
750     if (APLOGrtrace4(r)) {
751         t_elt = (const apr_table_entry_t *)(elts->elts);
752         do {
753             ap_log_rerror(APLOG_MARK, APLOG_TRACE4, 0, r, "  %s: %s",
754                           ap_escape_logitem(r->pool, t_elt->key),
755                           ap_escape_logitem(r->pool, t_elt->val));
756             t_elt++;
757         } while (t_elt < t_end);
758     }
759
760 #if APR_CHARSET_EBCDIC
761     {
762         apr_size_t len;
763         char *tmp = apr_pstrcatv(r->pool, vec, vec_next - vec, &len);
764         ap_xlate_proto_to_ascii(tmp, len);
765         return apr_brigade_write(h->bb, NULL, NULL, tmp, len);
766     }
767 #else
768     return apr_brigade_writev(h->bb, NULL, NULL, vec, vec_next - vec);
769 #endif
770 }
771
772 /* Confirm that the status line is well-formed and matches r->status.
773  * If they don't match, a filter may have negated the status line set by a
774  * handler.
775  * Zap r->status_line if bad.
776  */
777 static apr_status_t validate_status_line(request_rec *r)
778 {
779     char *end;
780
781     if (r->status_line) {
782         int len = strlen(r->status_line);
783         if (len < 3
784             || apr_strtoi64(r->status_line, &end, 10) != r->status
785             || (end - 3) != r->status_line
786             || (len >= 4 && ! apr_isspace(r->status_line[3]))) {
787             r->status_line = NULL;
788             return APR_EGENERAL;
789         }
790         /* Since we passed the above check, we know that length three
791          * is equivalent to only a 3 digit numeric http status.
792          * RFC2616 mandates a trailing space, let's add it.
793          */
794         if (len == 3) {
795             r->status_line = apr_pstrcat(r->pool, r->status_line, " ", NULL);
796             return APR_EGENERAL;
797         }
798         return APR_SUCCESS;
799     }
800     return APR_EGENERAL;
801 }
802
803 /*
804  * Determine the protocol to use for the response. Potentially downgrade
805  * to HTTP/1.0 in some situations and/or turn off keepalives.
806  *
807  * also prepare r->status_line.
808  */
809 static void basic_http_header_check(request_rec *r,
810                                     const char **protocol)
811 {
812     apr_status_t rv;
813
814     if (r->assbackwards) {
815         /* no such thing as a response protocol */
816         return;
817     }
818
819     rv = validate_status_line(r);
820
821     if (!r->status_line) {
822         r->status_line = ap_get_status_line(r->status);
823     } else if (rv != APR_SUCCESS) {
824         /* Status line is OK but our own reason phrase
825          * would be preferred if defined
826          */
827         const char *tmp = ap_get_status_line(r->status);
828         if (!strncmp(tmp, r->status_line, 3)) {
829             r->status_line = tmp;
830         }
831     }
832
833     /* Note that we must downgrade before checking for force responses. */
834     if (r->proto_num > HTTP_VERSION(1,0)
835         && apr_table_get(r->subprocess_env, "downgrade-1.0")) {
836         r->proto_num = HTTP_VERSION(1,0);
837     }
838
839     /* kludge around broken browsers when indicated by force-response-1.0
840      */
841     if (r->proto_num == HTTP_VERSION(1,0)
842         && apr_table_get(r->subprocess_env, "force-response-1.0")) {
843         *protocol = "HTTP/1.0";
844         r->connection->keepalive = AP_CONN_CLOSE;
845     }
846     else {
847         *protocol = AP_SERVER_PROTOCOL;
848     }
849
850 }
851
852 /* fill "bb" with a barebones/initial HTTP response header */
853 static void basic_http_header(request_rec *r, apr_bucket_brigade *bb,
854                               const char *protocol)
855 {
856     char *date = NULL;
857     const char *proxy_date = NULL;
858     const char *server = NULL;
859     const char *us = ap_get_server_banner();
860     header_struct h;
861     struct iovec vec[4];
862
863     if (r->assbackwards) {
864         /* there are no headers to send */
865         return;
866     }
867
868     /* Output the HTTP/1.x Status-Line and the Date and Server fields */
869
870     vec[0].iov_base = (void *)protocol;
871     vec[0].iov_len  = strlen(protocol);
872     vec[1].iov_base = (void *)" ";
873     vec[1].iov_len  = sizeof(" ") - 1;
874     vec[2].iov_base = (void *)(r->status_line);
875     vec[2].iov_len  = strlen(r->status_line);
876     vec[3].iov_base = (void *)CRLF;
877     vec[3].iov_len  = sizeof(CRLF) - 1;
878 #if APR_CHARSET_EBCDIC
879     {
880         char *tmp;
881         apr_size_t len;
882         tmp = apr_pstrcatv(r->pool, vec, 4, &len);
883         ap_xlate_proto_to_ascii(tmp, len);
884         apr_brigade_write(bb, NULL, NULL, tmp, len);
885     }
886 #else
887     apr_brigade_writev(bb, NULL, NULL, vec, 4);
888 #endif
889
890     h.pool = r->pool;
891     h.bb = bb;
892
893     /*
894      * keep the set-by-proxy server and date headers, otherwise
895      * generate a new server header / date header
896      */
897     if (r->proxyreq != PROXYREQ_NONE) {
898         proxy_date = apr_table_get(r->headers_out, "Date");
899         if (!proxy_date) {
900             /*
901              * proxy_date needs to be const. So use date for the creation of
902              * our own Date header and pass it over to proxy_date later to
903              * avoid a compiler warning.
904              */
905             date = apr_palloc(r->pool, APR_RFC822_DATE_LEN);
906             ap_recent_rfc822_date(date, r->request_time);
907         }
908         server = apr_table_get(r->headers_out, "Server");
909     }
910     else {
911         date = apr_palloc(r->pool, APR_RFC822_DATE_LEN);
912         ap_recent_rfc822_date(date, r->request_time);
913     }
914
915     form_header_field(&h, "Date", proxy_date ? proxy_date : date );
916
917     if (!server && *us)
918         server = us;
919     if (server)
920         form_header_field(&h, "Server", server);
921
922     if (APLOGrtrace3(r)) {
923         ap_log_rerror(APLOG_MARK, APLOG_TRACE3, 0, r,
924                       "Response sent with status %d%s",
925                       r->status,
926                       APLOGrtrace4(r) ? ", headers:" : "");
927
928         /*
929          * Date and Server are less interesting, use TRACE5 for them while
930          * using TRACE4 for the other headers.
931          */
932         ap_log_rerror(APLOG_MARK, APLOG_TRACE5, 0, r, "  Date: %s",
933                       proxy_date ? proxy_date : date );
934         if (server)
935             ap_log_rerror(APLOG_MARK, APLOG_TRACE5, 0, r, "  Server: %s",
936                           server);
937     }
938
939
940     /* unset so we don't send them again */
941     apr_table_unset(r->headers_out, "Date");        /* Avoid bogosity */
942     if (server) {
943         apr_table_unset(r->headers_out, "Server");
944     }
945 }
946
947 AP_DECLARE(void) ap_basic_http_header(request_rec *r, apr_bucket_brigade *bb)
948 {
949     const char *protocol = NULL;
950
951     basic_http_header_check(r, &protocol);
952     basic_http_header(r, bb, protocol);
953 }
954
955 static void terminate_header(apr_bucket_brigade *bb)
956 {
957     char crlf[] = CRLF;
958     apr_size_t buflen;
959
960     buflen = strlen(crlf);
961     ap_xlate_proto_to_ascii(crlf, buflen);
962     apr_brigade_write(bb, NULL, NULL, crlf, buflen);
963 }
964
965 AP_DECLARE_NONSTD(int) ap_send_http_trace(request_rec *r)
966 {
967     core_server_config *conf;
968     int rv;
969     apr_bucket_brigade *bb;
970     header_struct h;
971     apr_bucket *b;
972     int body;
973     char *bodyread = NULL, *bodyoff;
974     apr_size_t bodylen = 0;
975     apr_size_t bodybuf;
976     long res = -1; /* init to avoid gcc -Wall warning */
977
978     if (r->method_number != M_TRACE) {
979         return DECLINED;
980     }
981
982     /* Get the original request */
983     while (r->prev) {
984         r = r->prev;
985     }
986     conf = ap_get_core_module_config(r->server->module_config);
987
988     if (conf->trace_enable == AP_TRACE_DISABLE) {
989         apr_table_setn(r->notes, "error-notes",
990                       "TRACE denied by server configuration");
991         return HTTP_METHOD_NOT_ALLOWED;
992     }
993
994     if (conf->trace_enable == AP_TRACE_EXTENDED)
995         /* XXX: should be = REQUEST_CHUNKED_PASS */
996         body = REQUEST_CHUNKED_DECHUNK;
997     else
998         body = REQUEST_NO_BODY;
999
1000     if ((rv = ap_setup_client_block(r, body))) {
1001         if (rv == HTTP_REQUEST_ENTITY_TOO_LARGE)
1002             apr_table_setn(r->notes, "error-notes",
1003                           "TRACE with a request body is not allowed");
1004         return rv;
1005     }
1006
1007     if (ap_should_client_block(r)) {
1008
1009         if (r->remaining > 0) {
1010             if (r->remaining > 65536) {
1011                 apr_table_setn(r->notes, "error-notes",
1012                        "Extended TRACE request bodies cannot exceed 64k\n");
1013                 return HTTP_REQUEST_ENTITY_TOO_LARGE;
1014             }
1015             /* always 32 extra bytes to catch chunk header exceptions */
1016             bodybuf = (apr_size_t)r->remaining + 32;
1017         }
1018         else {
1019             /* Add an extra 8192 for chunk headers */
1020             bodybuf = 73730;
1021         }
1022
1023         bodyoff = bodyread = apr_palloc(r->pool, bodybuf);
1024
1025         /* only while we have enough for a chunked header */
1026         while ((!bodylen || bodybuf >= 32) &&
1027                (res = ap_get_client_block(r, bodyoff, bodybuf)) > 0) {
1028             bodylen += res;
1029             bodybuf -= res;
1030             bodyoff += res;
1031         }
1032         if (res > 0 && bodybuf < 32) {
1033             /* discard_rest_of_request_body into our buffer */
1034             while (ap_get_client_block(r, bodyread, bodylen) > 0)
1035                 ;
1036             apr_table_setn(r->notes, "error-notes",
1037                    "Extended TRACE request bodies cannot exceed 64k\n");
1038             return HTTP_REQUEST_ENTITY_TOO_LARGE;
1039         }
1040
1041         if (res < 0) {
1042             return HTTP_BAD_REQUEST;
1043         }
1044     }
1045
1046     ap_set_content_type(r, "message/http");
1047
1048     /* Now we recreate the request, and echo it back */
1049
1050     bb = apr_brigade_create(r->pool, r->connection->bucket_alloc);
1051 #if APR_CHARSET_EBCDIC
1052     {
1053         char *tmp;
1054         apr_size_t len;
1055         len = strlen(r->the_request);
1056         tmp = apr_pmemdup(r->pool, r->the_request, len);
1057         ap_xlate_proto_to_ascii(tmp, len);
1058         apr_brigade_putstrs(bb, NULL, NULL, tmp, CRLF_ASCII, NULL);
1059     }
1060 #else
1061     apr_brigade_putstrs(bb, NULL, NULL, r->the_request, CRLF, NULL);
1062 #endif
1063     h.pool = r->pool;
1064     h.bb = bb;
1065     apr_table_do((int (*) (void *, const char *, const char *))
1066                  form_header_field, (void *) &h, r->headers_in, NULL);
1067     apr_brigade_puts(bb, NULL, NULL, CRLF_ASCII);
1068
1069     /* If configured to accept a body, echo the body */
1070     if (bodylen) {
1071         b = apr_bucket_pool_create(bodyread, bodylen,
1072                                    r->pool, bb->bucket_alloc);
1073         APR_BRIGADE_INSERT_TAIL(bb, b);
1074     }
1075
1076     ap_pass_brigade(r->output_filters,  bb);
1077
1078     return DONE;
1079 }
1080
1081 typedef struct header_filter_ctx {
1082     int headers_sent;
1083 } header_filter_ctx;
1084
1085 AP_CORE_DECLARE_NONSTD(apr_status_t) ap_http_header_filter(ap_filter_t *f,
1086                                                            apr_bucket_brigade *b)
1087 {
1088     request_rec *r = f->r;
1089     conn_rec *c = r->connection;
1090     const char *clheader;
1091     const char *protocol = NULL;
1092     apr_bucket *e;
1093     apr_bucket_brigade *b2;
1094     header_struct h;
1095     header_filter_ctx *ctx = f->ctx;
1096     const char *ctype;
1097     ap_bucket_error *eb = NULL;
1098     core_server_config *conf;
1099
1100     AP_DEBUG_ASSERT(!r->main);
1101
1102     if (r->header_only) {
1103         if (!ctx) {
1104             ctx = f->ctx = apr_pcalloc(r->pool, sizeof(header_filter_ctx));
1105         }
1106         else if (ctx->headers_sent) {
1107             apr_brigade_cleanup(b);
1108             return OK;
1109         }
1110     }
1111
1112     for (e = APR_BRIGADE_FIRST(b);
1113          e != APR_BRIGADE_SENTINEL(b);
1114          e = APR_BUCKET_NEXT(e))
1115     {
1116         if (AP_BUCKET_IS_ERROR(e) && !eb) {
1117             eb = e->data;
1118             continue;
1119         }
1120         /*
1121          * If we see an EOC bucket it is a signal that we should get out
1122          * of the way doing nothing.
1123          */
1124         if (AP_BUCKET_IS_EOC(e)) {
1125             ap_remove_output_filter(f);
1126             return ap_pass_brigade(f->next, b);
1127         }
1128     }
1129     if (eb) {
1130         int status;
1131
1132         status = eb->status;
1133         apr_brigade_cleanup(b);
1134         ap_die(status, r);
1135         return AP_FILTER_ERROR;
1136     }
1137
1138     if (r->assbackwards) {
1139         r->sent_bodyct = 1;
1140         ap_remove_output_filter(f);
1141         return ap_pass_brigade(f->next, b);
1142     }
1143
1144     /*
1145      * Now that we are ready to send a response, we need to combine the two
1146      * header field tables into a single table.  If we don't do this, our
1147      * later attempts to set or unset a given fieldname might be bypassed.
1148      */
1149     if (!apr_is_empty_table(r->err_headers_out)) {
1150         r->headers_out = apr_table_overlay(r->pool, r->err_headers_out,
1151                                            r->headers_out);
1152     }
1153
1154     conf = ap_get_core_module_config(r->server->module_config);
1155     if (conf->http_conformance & AP_HTTP_CONFORMANCE_STRICT) {
1156         int ok = check_headers(r);
1157         if (!ok && !(conf->http_conformance & AP_HTTP_CONFORMANCE_LOGONLY)) {
1158             ap_die(HTTP_INTERNAL_SERVER_ERROR, r);
1159             return AP_FILTER_ERROR;
1160         }
1161     }
1162
1163     /*
1164      * Remove the 'Vary' header field if the client can't handle it.
1165      * Since this will have nasty effects on HTTP/1.1 caches, force
1166      * the response into HTTP/1.0 mode.
1167      *
1168      * Note: the force-response-1.0 should come before the call to
1169      *       basic_http_header_check()
1170      */
1171     if (apr_table_get(r->subprocess_env, "force-no-vary") != NULL) {
1172         apr_table_unset(r->headers_out, "Vary");
1173         r->proto_num = HTTP_VERSION(1,0);
1174         apr_table_setn(r->subprocess_env, "force-response-1.0", "1");
1175     }
1176     else {
1177         fixup_vary(r);
1178     }
1179
1180     /*
1181      * Now remove any ETag response header field if earlier processing
1182      * says so (such as a 'FileETag None' directive).
1183      */
1184     if (apr_table_get(r->notes, "no-etag") != NULL) {
1185         apr_table_unset(r->headers_out, "ETag");
1186     }
1187
1188     /* determine the protocol and whether we should use keepalives. */
1189     basic_http_header_check(r, &protocol);
1190     ap_set_keepalive(r);
1191
1192     if (r->chunked) {
1193         apr_table_mergen(r->headers_out, "Transfer-Encoding", "chunked");
1194         apr_table_unset(r->headers_out, "Content-Length");
1195     }
1196
1197     ctype = ap_make_content_type(r, r->content_type);
1198     if (ctype) {
1199         apr_table_setn(r->headers_out, "Content-Type", ctype);
1200     }
1201
1202     if (r->content_encoding) {
1203         apr_table_setn(r->headers_out, "Content-Encoding",
1204                        r->content_encoding);
1205     }
1206
1207     if (!apr_is_empty_array(r->content_languages)) {
1208         int i;
1209         char *token;
1210         char **languages = (char **)(r->content_languages->elts);
1211         const char *field = apr_table_get(r->headers_out, "Content-Language");
1212
1213         while (field && (token = ap_get_list_item(r->pool, &field)) != NULL) {
1214             for (i = 0; i < r->content_languages->nelts; ++i) {
1215                 if (!strcasecmp(token, languages[i]))
1216                     break;
1217             }
1218             if (i == r->content_languages->nelts) {
1219                 *((char **) apr_array_push(r->content_languages)) = token;
1220             }
1221         }
1222
1223         field = apr_array_pstrcat(r->pool, r->content_languages, ',');
1224         apr_table_setn(r->headers_out, "Content-Language", field);
1225     }
1226
1227     /*
1228      * Control cachability for non-cachable responses if not already set by
1229      * some other part of the server configuration.
1230      */
1231     if (r->no_cache && !apr_table_get(r->headers_out, "Expires")) {
1232         char *date = apr_palloc(r->pool, APR_RFC822_DATE_LEN);
1233         ap_recent_rfc822_date(date, r->request_time);
1234         apr_table_addn(r->headers_out, "Expires", date);
1235     }
1236
1237     /* This is a hack, but I can't find anyway around it.  The idea is that
1238      * we don't want to send out 0 Content-Lengths if it is a head request.
1239      * This happens when modules try to outsmart the server, and return
1240      * if they see a HEAD request.  Apache 1.3 handlers were supposed to
1241      * just return in that situation, and the core handled the HEAD.  In
1242      * 2.0, if a handler returns, then the core sends an EOS bucket down
1243      * the filter stack, and the content-length filter computes a C-L of
1244      * zero and that gets put in the headers, and we end up sending a
1245      * zero C-L to the client.  We can't just remove the C-L filter,
1246      * because well behaved 2.0 handlers will send their data down the stack,
1247      * and we will compute a real C-L for the head request. RBB
1248      *
1249      * Allow modification of this behavior through the
1250      * HttpContentLengthHeadZero directive.
1251      *
1252      * The default (unset) behavior is to squelch the C-L in this case.
1253      */
1254     if (r->header_only
1255         && (clheader = apr_table_get(r->headers_out, "Content-Length"))
1256         && !strcmp(clheader, "0")
1257         && conf->http_cl_head_zero != AP_HTTP_CL_HEAD_ZERO_ENABLE) {
1258         apr_table_unset(r->headers_out, "Content-Length");
1259     }
1260
1261     b2 = apr_brigade_create(r->pool, c->bucket_alloc);
1262     basic_http_header(r, b2, protocol);
1263
1264     h.pool = r->pool;
1265     h.bb = b2;
1266
1267     if (r->status == HTTP_NOT_MODIFIED) {
1268         apr_table_do((int (*)(void *, const char *, const char *)) form_header_field,
1269                      (void *) &h, r->headers_out,
1270                      "Connection",
1271                      "Keep-Alive",
1272                      "ETag",
1273                      "Content-Location",
1274                      "Expires",
1275                      "Cache-Control",
1276                      "Vary",
1277                      "Warning",
1278                      "WWW-Authenticate",
1279                      "Proxy-Authenticate",
1280                      "Set-Cookie",
1281                      "Set-Cookie2",
1282                      NULL);
1283     }
1284     else {
1285         send_all_header_fields(&h, r);
1286     }
1287
1288     terminate_header(b2);
1289
1290     ap_pass_brigade(f->next, b2);
1291
1292     if (r->header_only) {
1293         apr_brigade_cleanup(b);
1294         ctx->headers_sent = 1;
1295         return OK;
1296     }
1297
1298     r->sent_bodyct = 1;         /* Whatever follows is real body stuff... */
1299
1300     if (r->chunked) {
1301         /* We can't add this filter until we have already sent the headers.
1302          * If we add it before this point, then the headers will be chunked
1303          * as well, and that is just wrong.
1304          */
1305         ap_add_output_filter("CHUNK", NULL, r, r->connection);
1306     }
1307
1308     /* Don't remove this filter until after we have added the CHUNK filter.
1309      * Otherwise, f->next won't be the CHUNK filter and thus the first
1310      * brigade won't be chunked properly.
1311      */
1312     ap_remove_output_filter(f);
1313     return ap_pass_brigade(f->next, b);
1314 }
1315
1316 /*
1317  * Map specific APR codes returned by the filter stack to HTTP error
1318  * codes, or the default status code provided. Use it as follows:
1319  *
1320  * return ap_map_http_request_error(rv, HTTP_BAD_REQUEST);
1321  *
1322  * If the filter has already handled the error, AP_FILTER_ERROR will
1323  * be returned, which is cleanly passed through.
1324  *
1325  * These mappings imply that the filter stack is reading from the
1326  * downstream client, the proxy will map these codes differently.
1327  */
1328 AP_DECLARE(int) ap_map_http_request_error(apr_status_t rv, int status)
1329 {
1330     switch (rv) {
1331     case AP_FILTER_ERROR: {
1332         return AP_FILTER_ERROR;
1333     }
1334     case APR_EGENERAL: {
1335         return HTTP_BAD_REQUEST;
1336     }
1337     case APR_ENOSPC: {
1338         return HTTP_REQUEST_ENTITY_TOO_LARGE;
1339     }
1340     case APR_ENOTIMPL: {
1341         return HTTP_NOT_IMPLEMENTED;
1342     }
1343     case APR_ETIMEDOUT: {
1344         return HTTP_REQUEST_TIME_OUT;
1345     }
1346     default: {
1347         return status;
1348     }
1349     }
1350 }
1351
1352 /* In HTTP/1.1, any method can have a body.  However, most GET handlers
1353  * wouldn't know what to do with a request body if they received one.
1354  * This helper routine tests for and reads any message body in the request,
1355  * simply discarding whatever it receives.  We need to do this because
1356  * failing to read the request body would cause it to be interpreted
1357  * as the next request on a persistent connection.
1358  *
1359  * Since we return an error status if the request is malformed, this
1360  * routine should be called at the beginning of a no-body handler, e.g.,
1361  *
1362  *    if ((retval = ap_discard_request_body(r)) != OK) {
1363  *        return retval;
1364  *    }
1365  */
1366 AP_DECLARE(int) ap_discard_request_body(request_rec *r)
1367 {
1368     apr_bucket_brigade *bb;
1369     int seen_eos;
1370     apr_status_t rv;
1371
1372     /* Sometimes we'll get in a state where the input handling has
1373      * detected an error where we want to drop the connection, so if
1374      * that's the case, don't read the data as that is what we're trying
1375      * to avoid.
1376      *
1377      * This function is also a no-op on a subrequest.
1378      */
1379     if (r->main || r->connection->keepalive == AP_CONN_CLOSE ||
1380         ap_status_drops_connection(r->status)) {
1381         return OK;
1382     }
1383
1384     bb = apr_brigade_create(r->pool, r->connection->bucket_alloc);
1385     seen_eos = 0;
1386     do {
1387         apr_bucket *bucket;
1388
1389         rv = ap_get_brigade(r->input_filters, bb, AP_MODE_READBYTES,
1390                             APR_BLOCK_READ, HUGE_STRING_LEN);
1391
1392         if (rv != APR_SUCCESS) {
1393             apr_brigade_destroy(bb);
1394             return ap_map_http_request_error(rv, HTTP_BAD_REQUEST);
1395         }
1396
1397         for (bucket = APR_BRIGADE_FIRST(bb);
1398              bucket != APR_BRIGADE_SENTINEL(bb);
1399              bucket = APR_BUCKET_NEXT(bucket))
1400         {
1401             const char *data;
1402             apr_size_t len;
1403
1404             if (APR_BUCKET_IS_EOS(bucket)) {
1405                 seen_eos = 1;
1406                 break;
1407             }
1408
1409             /* These are metadata buckets. */
1410             if (bucket->length == 0) {
1411                 continue;
1412             }
1413
1414             /* We MUST read because in case we have an unknown-length
1415              * bucket or one that morphs, we want to exhaust it.
1416              */
1417             rv = apr_bucket_read(bucket, &data, &len, APR_BLOCK_READ);
1418             if (rv != APR_SUCCESS) {
1419                 apr_brigade_destroy(bb);
1420                 return HTTP_BAD_REQUEST;
1421             }
1422         }
1423         apr_brigade_cleanup(bb);
1424     } while (!seen_eos);
1425
1426     return OK;
1427 }
1428
1429 /* Here we deal with getting the request message body from the client.
1430  * Whether or not the request contains a body is signaled by the presence
1431  * of a non-zero Content-Length or by a Transfer-Encoding: chunked.
1432  *
1433  * Note that this is more complicated than it was in Apache 1.1 and prior
1434  * versions, because chunked support means that the module does less.
1435  *
1436  * The proper procedure is this:
1437  *
1438  * 1. Call ap_setup_client_block() near the beginning of the request
1439  *    handler. This will set up all the necessary properties, and will
1440  *    return either OK, or an error code. If the latter, the module should
1441  *    return that error code. The second parameter selects the policy to
1442  *    apply if the request message indicates a body, and how a chunked
1443  *    transfer-coding should be interpreted. Choose one of
1444  *
1445  *    REQUEST_NO_BODY          Send 413 error if message has any body
1446  *    REQUEST_CHUNKED_ERROR    Send 411 error if body without Content-Length
1447  *    REQUEST_CHUNKED_DECHUNK  If chunked, remove the chunks for me.
1448  *    REQUEST_CHUNKED_PASS     If chunked, pass the chunk headers with body.
1449  *
1450  *    In order to use the last two options, the caller MUST provide a buffer
1451  *    large enough to hold a chunk-size line, including any extensions.
1452  *
1453  * 2. When you are ready to read a body (if any), call ap_should_client_block().
1454  *    This will tell the module whether or not to read input. If it is 0,
1455  *    the module should assume that there is no message body to read.
1456  *
1457  * 3. Finally, call ap_get_client_block in a loop. Pass it a buffer and its size.
1458  *    It will put data into the buffer (not necessarily a full buffer), and
1459  *    return the length of the input block. When it is done reading, it will
1460  *    return 0 if EOF, or -1 if there was an error.
1461  *    If an error occurs on input, we force an end to keepalive.
1462  *
1463  *    This step also sends a 100 Continue response to HTTP/1.1 clients if appropriate.
1464  */
1465
1466 AP_DECLARE(int) ap_setup_client_block(request_rec *r, int read_policy)
1467 {
1468     const char *tenc = apr_table_get(r->headers_in, "Transfer-Encoding");
1469     const char *lenp = apr_table_get(r->headers_in, "Content-Length");
1470
1471     r->read_body = read_policy;
1472     r->read_chunked = 0;
1473     r->remaining = 0;
1474
1475     if (tenc) {
1476         if (strcasecmp(tenc, "chunked")) {
1477             ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, APLOGNO(01592)
1478                           "Unknown Transfer-Encoding %s", tenc);
1479             return HTTP_NOT_IMPLEMENTED;
1480         }
1481         if (r->read_body == REQUEST_CHUNKED_ERROR) {
1482             ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, APLOGNO(01593)
1483                           "chunked Transfer-Encoding forbidden: %s", r->uri);
1484             return (lenp) ? HTTP_BAD_REQUEST : HTTP_LENGTH_REQUIRED;
1485         }
1486
1487         r->read_chunked = 1;
1488     }
1489     else if (lenp) {
1490         char *endstr;
1491
1492         if (apr_strtoff(&r->remaining, lenp, &endstr, 10)
1493             || *endstr || r->remaining < 0) {
1494             r->remaining = 0;
1495             ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, APLOGNO(01594)
1496                           "Invalid Content-Length");
1497             return HTTP_BAD_REQUEST;
1498         }
1499     }
1500
1501     if ((r->read_body == REQUEST_NO_BODY)
1502         && (r->read_chunked || (r->remaining > 0))) {
1503         ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, APLOGNO(01595)
1504                       "%s with body is not allowed for %s", r->method, r->uri);
1505         return HTTP_REQUEST_ENTITY_TOO_LARGE;
1506     }
1507
1508 #ifdef AP_DEBUG
1509     {
1510         /* Make sure ap_getline() didn't leave any droppings. */
1511         core_request_config *req_cfg =
1512             (core_request_config *)ap_get_core_module_config(r->request_config);
1513         AP_DEBUG_ASSERT(APR_BRIGADE_EMPTY(req_cfg->bb));
1514     }
1515 #endif
1516
1517     return OK;
1518 }
1519
1520 AP_DECLARE(int) ap_should_client_block(request_rec *r)
1521 {
1522     /* First check if we have already read the request body */
1523
1524     if (r->read_length || (!r->read_chunked && (r->remaining <= 0))) {
1525         return 0;
1526     }
1527
1528     return 1;
1529 }
1530
1531 /* get_client_block is called in a loop to get the request message body.
1532  * This is quite simple if the client includes a content-length
1533  * (the normal case), but gets messy if the body is chunked. Note that
1534  * r->remaining is used to maintain state across calls and that
1535  * r->read_length is the total number of bytes given to the caller
1536  * across all invocations.  It is messy because we have to be careful not
1537  * to read past the data provided by the client, since these reads block.
1538  * Returns 0 on End-of-body, -1 on error or premature chunk end.
1539  *
1540  */
1541 AP_DECLARE(long) ap_get_client_block(request_rec *r, char *buffer,
1542                                      apr_size_t bufsiz)
1543 {
1544     apr_status_t rv;
1545     apr_bucket_brigade *bb;
1546
1547     if (r->remaining < 0 || (!r->read_chunked && r->remaining == 0)) {
1548         return 0;
1549     }
1550
1551     bb = apr_brigade_create(r->pool, r->connection->bucket_alloc);
1552     if (bb == NULL) {
1553         r->connection->keepalive = AP_CONN_CLOSE;
1554         return -1;
1555     }
1556
1557     rv = ap_get_brigade(r->input_filters, bb, AP_MODE_READBYTES,
1558                         APR_BLOCK_READ, bufsiz);
1559
1560     /* We lose the failure code here.  This is why ap_get_client_block should
1561      * not be used.
1562      */
1563     if (rv != APR_SUCCESS) {
1564         apr_bucket *e;
1565
1566         /* work around our silent swallowing of error messages by mapping
1567          * error codes at this point, and sending an error bucket back
1568          * upstream.
1569          */
1570         apr_brigade_cleanup(bb);
1571
1572         e = ap_bucket_error_create(
1573                 ap_map_http_request_error(rv, HTTP_BAD_REQUEST), NULL, r->pool,
1574                 r->connection->bucket_alloc);
1575         APR_BRIGADE_INSERT_TAIL(bb, e);
1576
1577         e = apr_bucket_eos_create(r->connection->bucket_alloc);
1578         APR_BRIGADE_INSERT_TAIL(bb, e);
1579
1580         rv = ap_pass_brigade(r->output_filters, bb);
1581         if (APR_SUCCESS != rv) {
1582             ap_log_rerror(APLOG_MARK, APLOG_INFO, rv, r, APLOGNO(02484)
1583                           "Error while writing error response");
1584         }
1585
1586         /* if we actually fail here, we want to just return and
1587          * stop trying to read data from the client.
1588          */
1589         r->connection->keepalive = AP_CONN_CLOSE;
1590         apr_brigade_destroy(bb);
1591         return -1;
1592     }
1593
1594     /* If this fails, it means that a filter is written incorrectly and that
1595      * it needs to learn how to properly handle APR_BLOCK_READ requests by
1596      * returning data when requested.
1597      */
1598     AP_DEBUG_ASSERT(!APR_BRIGADE_EMPTY(bb));
1599
1600     /* Check to see if EOS in the brigade.
1601      *
1602      * If so, we have to leave a nugget for the *next* ap_get_client_block
1603      * call to return 0.
1604      */
1605     if (APR_BUCKET_IS_EOS(APR_BRIGADE_LAST(bb))) {
1606         if (r->read_chunked) {
1607             r->remaining = -1;
1608         }
1609         else {
1610             r->remaining = 0;
1611         }
1612     }
1613
1614     rv = apr_brigade_flatten(bb, buffer, &bufsiz);
1615     if (rv != APR_SUCCESS) {
1616         apr_brigade_destroy(bb);
1617         return -1;
1618     }
1619
1620     /* XXX yank me? */
1621     r->read_length += bufsiz;
1622
1623     apr_brigade_destroy(bb);
1624     return bufsiz;
1625 }
1626
1627 /* Context struct for ap_http_outerror_filter */
1628 typedef struct {
1629     int seen_eoc;
1630 } outerror_filter_ctx_t;
1631
1632 /* Filter to handle any error buckets on output */
1633 apr_status_t ap_http_outerror_filter(ap_filter_t *f,
1634                                      apr_bucket_brigade *b)
1635 {
1636     request_rec *r = f->r;
1637     outerror_filter_ctx_t *ctx = (outerror_filter_ctx_t *)(f->ctx);
1638     apr_bucket *e;
1639
1640     /* Create context if none is present */
1641     if (!ctx) {
1642         ctx = apr_pcalloc(r->pool, sizeof(outerror_filter_ctx_t));
1643         f->ctx = ctx;
1644     }
1645     for (e = APR_BRIGADE_FIRST(b);
1646          e != APR_BRIGADE_SENTINEL(b);
1647          e = APR_BUCKET_NEXT(e))
1648     {
1649         if (AP_BUCKET_IS_ERROR(e)) {
1650             /*
1651              * Start of error handling state tree. Just one condition
1652              * right now :)
1653              */
1654             if (((ap_bucket_error *)(e->data))->status == HTTP_BAD_GATEWAY ||
1655                 ((ap_bucket_error *)(e->data))->status == HTTP_GATEWAY_TIME_OUT) {
1656                 /* stream aborted and we have not ended it yet */
1657                 r->connection->keepalive = AP_CONN_CLOSE;
1658             }
1659             continue;
1660         }
1661         /* Detect EOC buckets and memorize this in the context. */
1662         if (AP_BUCKET_IS_EOC(e)) {
1663             ctx->seen_eoc = 1;
1664         }
1665     }
1666     /*
1667      * Remove all data buckets that are in a brigade after an EOC bucket
1668      * was seen, as an EOC bucket tells us that no (further) resource
1669      * and protocol data should go out to the client. OTOH meta buckets
1670      * are still welcome as they might trigger needed actions down in
1671      * the chain (e.g. in network filters like SSL).
1672      * Remark 1: It is needed to dump ALL data buckets in the brigade
1673      *           since an filter in between might have inserted data
1674      *           buckets BEFORE the EOC bucket sent by the original
1675      *           sender and we do NOT want this data to be sent.
1676      * Remark 2: Dumping all data buckets here does not necessarily mean
1677      *           that no further data is send to the client as:
1678      *           1. Network filters like SSL can still be triggered via
1679      *              meta buckets to talk with the client e.g. for a
1680      *              clean shutdown.
1681      *           2. There could be still data that was buffered before
1682      *              down in the chain that gets flushed by a FLUSH or an
1683      *              EOS bucket.
1684      */
1685     if (ctx->seen_eoc) {
1686         for (e = APR_BRIGADE_FIRST(b);
1687              e != APR_BRIGADE_SENTINEL(b);
1688              e = APR_BUCKET_NEXT(e))
1689         {
1690             if (!APR_BUCKET_IS_METADATA(e)) {
1691                 APR_BUCKET_REMOVE(e);
1692             }
1693         }
1694     }
1695
1696     return ap_pass_brigade(f->next,  b);
1697 }