]> granicus.if.org Git - apache/blob - modules/http/http_filters.c
Deal with unrecognised Transfer-Encoding headers.
[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 #define CORE_PRIVATE
33 #include "util_filter.h"
34 #include "ap_config.h"
35 #include "httpd.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
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 extern module AP_MODULE_DECLARE_DATA http_module;
59
60 static long get_chunk_size(char *);
61
62 typedef struct http_filter_ctx {
63     apr_off_t remaining;
64     apr_off_t limit;
65     apr_off_t limit_used;
66     enum {
67         BODY_NONE,
68         BODY_LENGTH,
69         BODY_CHUNK,
70         BODY_CHUNK_PART
71     } state;
72     int eos_sent;
73 } http_ctx_t;
74
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.
79  */
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,
82                             apr_off_t readbytes)
83 {
84     apr_bucket *e;
85     http_ctx_t *ctx = f->ctx;
86     apr_status_t rv;
87     apr_off_t totalread;
88
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);
92     }
93
94     if (!ctx) {
95         const char *tenc, *lenp;
96         f->ctx = ctx = apr_palloc(f->r->pool, sizeof(*ctx));
97         ctx->state = BODY_NONE;
98         ctx->remaining = 0;
99         ctx->limit_used = 0;
100         ctx->eos_sent = 0;
101
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?
106          */
107         if (!f->r->proxyreq) {
108             ctx->limit = ap_get_limit_req_body(f->r);
109         }
110         else {
111             ctx->limit = 0;
112         }
113
114         tenc = apr_table_get(f->r->headers_in, "Transfer-Encoding");
115         lenp = apr_table_get(f->r->headers_in, "Content-Length");
116
117         if (tenc) {
118             /* RFC2616 allows qualifiers, so use strncasecmp */
119             if (!strncasecmp(tenc, "chunked", 7)) {
120                 ctx->state = BODY_CHUNK;
121             }
122             else {
123                 /* Something that isn't in HTTP, unless some future
124                  * edition defines new transfer ecodings, is unsupported.
125                  */
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);
135                 ctx->eos_sent = 1;
136                 return ap_pass_brigade(f->r->output_filters, bb);
137             }
138         }
139         else if (lenp) {
140             char *endstr;
141
142             ctx->state = BODY_LENGTH;
143             errno = 0;
144
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;
151
152                 ctx->remaining = 0;
153                 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, f->r,
154                               "Invalid Content-Length");
155
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);
162                 ctx->eos_sent = 1;
163                 return ap_pass_brigade(f->r->output_filters, bb);
164             }
165
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.
168              */
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);
181                 ctx->eos_sent = 1;
182                 return ap_pass_brigade(f->r->output_filters, bb);
183             }
184         }
185
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.)
189          *
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.
193          *
194          * Note that since the proxy uses this filter to handle the
195          * proxied *response*, proxy responses MUST be exempt.
196          */
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);
200             ctx->eos_sent = 1;
201             return APR_SUCCESS;
202         }
203
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)) {
210             char *tmp;
211             apr_bucket_brigade *bb;
212
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,
217                                        f->c->bucket_alloc);
218             APR_BRIGADE_INSERT_HEAD(bb, e);
219             e = apr_bucket_flush_create(f->c->bucket_alloc);
220             APR_BRIGADE_INSERT_TAIL(bb, e);
221
222             ap_pass_brigade(f->c->output_filters, bb);
223         }
224
225         /* We can't read the chunk until after sending 100 if required. */
226         if (ctx->state == BODY_CHUNK) {
227             char line[30];
228             apr_bucket_brigade *bb;
229             apr_size_t len = 30;
230             apr_off_t brigade_length;
231
232             bb = apr_brigade_create(f->r->pool, f->c->bucket_alloc);
233
234             rv = ap_get_brigade(f->next, bb, AP_MODE_GETLINE,
235                                 block, 0);
236
237             /* for timeout */
238             if (block == APR_NONBLOCK_READ &&
239                 ( (rv == APR_SUCCESS && APR_BRIGADE_EMPTY(bb)) ||
240                   (APR_STATUS_IS_EAGAIN(rv)) )) {
241                 return APR_EAGAIN;
242             }
243
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.
247                  */
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)) {
252                     rv = APR_ENOSPC;
253                 }
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);
258                     }
259                 }
260             }
261             apr_brigade_cleanup(bb);
262
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,
268                                            f->r->pool,
269                                            f->c->bucket_alloc);
270                 APR_BRIGADE_INSERT_TAIL(bb, e);
271                 e = apr_bucket_eos_create(f->c->bucket_alloc);
272                 APR_BRIGADE_INSERT_TAIL(bb, e);
273                 ctx->eos_sent = 1;
274                 return ap_pass_brigade(f->r->output_filters, bb);
275             }
276
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);
283                 ctx->eos_sent = 1;
284                 return APR_SUCCESS;
285             }
286         }
287     }
288
289     if (ctx->eos_sent) {
290         e = apr_bucket_eos_create(f->c->bucket_alloc);
291         APR_BRIGADE_INSERT_TAIL(b, e);
292         return APR_SUCCESS;
293     }
294
295     if (!ctx->remaining) {
296         switch (ctx->state) {
297         case BODY_NONE:
298             break;
299         case BODY_LENGTH:
300             e = apr_bucket_eos_create(f->c->bucket_alloc);
301             APR_BRIGADE_INSERT_TAIL(b, e);
302             ctx->eos_sent = 1;
303             return APR_SUCCESS;
304         case BODY_CHUNK: case BODY_CHUNK_PART:
305             {
306                 char line[30];
307                 apr_bucket_brigade *bb;
308                 apr_size_t len = 30;
309                 apr_status_t http_error = HTTP_REQUEST_ENTITY_TOO_LARGE;
310
311                 bb = apr_brigade_create(f->r->pool, f->c->bucket_alloc);
312
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,
316                                         block, 0);
317                     apr_brigade_cleanup(bb);
318                     if (block == APR_NONBLOCK_READ &&
319                         (APR_STATUS_IS_EAGAIN(rv))) {
320                         return APR_EAGAIN;
321                     }
322                 } else {
323                     rv = APR_SUCCESS;
324                 }
325
326                 if (rv == APR_SUCCESS) {
327                     /* Read the real chunk line. */
328                     rv = ap_get_brigade(f->next, bb, AP_MODE_GETLINE,
329                                         block, 0);
330                     /* Test timeout */
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;
335                         return APR_EAGAIN;
336                     }
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. */
342                             if (!len) {
343                                 rv = APR_EGENERAL;
344                                 http_error = HTTP_SERVICE_UNAVAILABLE;
345                             }
346                             else {
347                                 ctx->remaining = get_chunk_size(line);
348                             }
349                         }
350                     }
351                     apr_brigade_cleanup(bb);
352                 }
353
354                 /* Detect chunksize error (such as overflow) */
355                 if (rv != APR_SUCCESS || ctx->remaining < 0) {
356                     apr_status_t out_error;
357
358                     ctx->remaining = 0; /* Reset it in case we have to
359                                          * come back here later */
360                     e = ap_bucket_error_create(http_error,
361                                                NULL, f->r->pool,
362                                                f->c->bucket_alloc);
363                     APR_BRIGADE_INSERT_TAIL(bb, e);
364                     e = apr_bucket_eos_create(f->c->bucket_alloc);
365                     APR_BRIGADE_INSERT_TAIL(bb, e);
366                     ctx->eos_sent = 1;
367                     out_error = ap_pass_brigade(f->r->output_filters, bb);
368                     return rv;
369                 }
370
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);
377                     ctx->eos_sent = 1;
378                     return APR_SUCCESS;
379                 }
380             }
381             break;
382         }
383     }
384
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;
389         }
390         AP_DEBUG_ASSERT(readbytes > 0);
391     }
392
393     rv = ap_get_brigade(f->next, b, mode, block, readbytes);
394
395     if (rv != APR_SUCCESS) {
396         return rv;
397     }
398
399     /* How many bytes did we just read? */
400     apr_brigade_length(b, 0, &totalread);
401
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);
405
406     if (ctx->state != BODY_NONE) {
407         ctx->remaining -= totalread;
408     }
409
410     /* If we have no more bytes remaining on a C-L request,
411      * save the callter a roundtrip to discover EOS.
412      */
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);
416     }
417
418     /* We have a limit in effect. */
419     if (ctx->limit) {
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,
432                                        f->r->pool,
433                                        f->c->bucket_alloc);
434             APR_BRIGADE_INSERT_TAIL(bb, e);
435             e = apr_bucket_eos_create(f->c->bucket_alloc);
436             APR_BRIGADE_INSERT_TAIL(bb, e);
437             ctx->eos_sent = 1;
438             return ap_pass_brigade(f->r->output_filters, bb);
439         }
440     }
441
442     return APR_SUCCESS;
443 }
444
445 /**
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
451  *     returned.
452  * In general, any negative number can be considered an overflow error.
453  */
454 static long get_chunk_size(char *b)
455 {
456     long chunksize = 0;
457     size_t chunkbits = sizeof(long) * 8;
458
459     ap_xlate_proto_from_ascii(b, strlen(b));
460
461     /* Skip leading zeros */
462     while (*b == '0') {
463         ++b;
464     }
465
466     while (apr_isxdigit(*b) && (chunkbits > 0)) {
467         int xvalue = 0;
468
469         if (*b >= '0' && *b <= '9') {
470             xvalue = *b - '0';
471         }
472         else if (*b >= 'A' && *b <= 'F') {
473             xvalue = *b - 'A' + 0xa;
474         }
475         else if (*b >= 'a' && *b <= 'f') {
476             xvalue = *b - 'a' + 0xa;
477         }
478
479         chunksize = (chunksize << 4) | xvalue;
480         chunkbits -= 4;
481         ++b;
482     }
483     if (apr_isxdigit(*b) && (chunkbits <= 0)) {
484         /* overflow */
485         return -1;
486     }
487
488     return chunksize;
489 }
490
491 typedef struct header_struct {
492     apr_pool_t *pool;
493     apr_bucket_brigade *bb;
494 } header_struct;
495
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.
500  */
501 static int form_header_field(header_struct *h,
502                              const char *fieldname, const char *fieldval)
503 {
504 #if APR_CHARSET_EBCDIC
505     char *headfield;
506     apr_size_t len;
507     apr_size_t name_len;
508     apr_size_t val_len;
509     char *next;
510
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;
517     *next++ = ':';
518     *next++ = ' ';
519     memcpy(next, fieldval, val_len);
520     next += val_len;
521     *next++ = CR;
522     *next++ = LF;
523     *next = 0;
524     ap_xlate_proto_to_ascii(headfield, len);
525     apr_brigade_write(h->bb, NULL, NULL, headfield, len);
526 #else
527     struct iovec vec[4];
528     struct iovec *v = vec;
529     v->iov_base = (void *)fieldname;
530     v->iov_len = strlen(fieldname);
531     v++;
532     v->iov_base = ": ";
533     v->iov_len = sizeof(": ") - 1;
534     v++;
535     v->iov_base = (void *)fieldval;
536     v->iov_len = strlen(fieldval);
537     v++;
538     v->iov_base = CRLF;
539     v->iov_len = sizeof(CRLF) - 1;
540     apr_brigade_writev(h->bb, NULL, NULL, vec, 4);
541 #endif /* !APR_CHARSET_EBCDIC */
542     return 1;
543 }
544
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.
550  */
551 static int uniq_field_values(void *d, const char *key, const char *val)
552 {
553     apr_array_header_t *values;
554     char *start;
555     char *e;
556     char **strpp;
557     int  i;
558
559     values = (apr_array_header_t *)d;
560
561     e = apr_pstrdup(values->pool, val);
562
563     do {
564         /* Find a non-empty fieldname */
565
566         while (*e == ',' || apr_isspace(*e)) {
567             ++e;
568         }
569         if (*e == '\0') {
570             break;
571         }
572         start = e;
573         while (*e != '\0' && *e != ',' && !apr_isspace(*e)) {
574             ++e;
575         }
576         if (*e != '\0') {
577             *e++ = '\0';
578         }
579
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.
582          */
583         for (i = 0, strpp = (char **) values->elts; i < values->nelts;
584              ++i, ++strpp) {
585             if (*strpp && strcasecmp(*strpp, start) == 0) {
586                 break;
587             }
588         }
589         if (i == values->nelts) {  /* if not found */
590             *(char **)apr_array_push(values) = start;
591         }
592     } while (*e != '\0');
593
594     return 1;
595 }
596
597 /*
598  * Since some clients choke violently on multiple Vary fields, or
599  * Vary fields with duplicate tokens, combine any multiples and remove
600  * any duplicates.
601  */
602 static void fixup_vary(request_rec *r)
603 {
604     apr_array_header_t *varies;
605
606     varies = apr_array_make(r->pool, 5, sizeof(char *));
607
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.
611      */
612     apr_table_do((int (*)(void *, const char *, const char *))uniq_field_values,
613                  (void *) varies, r->headers_out, "Vary", NULL);
614
615     /* If we found any, replace old Vary fields with unique-ified value */
616
617     if (varies->nelts > 0) {
618         apr_table_setn(r->headers_out, "Vary",
619                        apr_array_pstrcat(r->pool, varies, ','));
620     }
621 }
622
623 /* Send a request's HTTP response headers to the client.
624  */
625 static apr_status_t send_all_header_fields(header_struct *h,
626                                            const request_rec *r)
627 {
628     const apr_array_header_t *elts;
629     const apr_table_entry_t *t_elt;
630     const apr_table_entry_t *t_end;
631     struct iovec *vec;
632     struct iovec *vec_next;
633
634     elts = apr_table_elts(r->headers_out);
635     if (elts->nelts == 0) {
636         return APR_SUCCESS;
637     }
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));
642     vec_next = vec;
643
644     /* For each field, generate
645      *    name ": " value CRLF
646      */
647     do {
648         vec_next->iov_base = (void*)(t_elt->key);
649         vec_next->iov_len = strlen(t_elt->key);
650         vec_next++;
651         vec_next->iov_base = ": ";
652         vec_next->iov_len = sizeof(": ") - 1;
653         vec_next++;
654         vec_next->iov_base = (void*)(t_elt->val);
655         vec_next->iov_len = strlen(t_elt->val);
656         vec_next++;
657         vec_next->iov_base = CRLF;
658         vec_next->iov_len = sizeof(CRLF) - 1;
659         vec_next++;
660         t_elt++;
661     } while (t_elt < t_end);
662
663 #if APR_CHARSET_EBCDIC
664     {
665         apr_size_t len;
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);
669     }
670 #else
671     return apr_brigade_writev(h->bb, NULL, NULL, vec, vec_next - vec);
672 #endif
673 }
674
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
677  * handler.
678  * Zap r->status_line if bad.
679  */
680 static void validate_status_line(request_rec *r)
681 {
682     char *end;
683
684     if (r->status_line
685         && (strlen(r->status_line) <= 4
686             || apr_strtoi64(r->status_line, &end, 10) != r->status
687             || *end != ' '
688             || (end - 3) != r->status_line)) {
689         r->status_line = NULL;
690     }
691 }
692
693 /*
694  * Determine the protocol to use for the response. Potentially downgrade
695  * to HTTP/1.0 in some situations and/or turn off keepalives.
696  *
697  * also prepare r->status_line.
698  */
699 static void basic_http_header_check(request_rec *r,
700                                     const char **protocol)
701 {
702     if (r->assbackwards) {
703         /* no such thing as a response protocol */
704         return;
705     }
706
707     validate_status_line(r);
708
709     if (!r->status_line) {
710         r->status_line = ap_get_status_line(r->status);
711     }
712
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);
717     }
718
719     /* kludge around broken browsers when indicated by force-response-1.0
720      */
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;
725     }
726     else {
727         *protocol = AP_SERVER_PROTOCOL;
728     }
729
730 }
731
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)
735 {
736     char *date;
737     const char *server;
738     header_struct h;
739     struct iovec vec[4];
740
741     if (r->assbackwards) {
742         /* there are no headers to send */
743         return;
744     }
745
746     /* Output the HTTP/1.x Status-Line and the Date and Server fields */
747
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
757     {
758         char *tmp;
759         apr_size_t len;
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);
763     }
764 #else
765     apr_brigade_writev(bb, NULL, NULL, vec, 4);
766 #endif
767
768     h.pool = r->pool;
769     h.bb = bb;
770
771     /*
772      * keep the set-by-proxy server and date headers, otherwise
773      * generate a new server header / date header
774      */
775     if (r->proxyreq != PROXYREQ_NONE) {
776         const char *proxy_date;
777
778         proxy_date = apr_table_get(r->headers_out, "Date");
779         if (!proxy_date) {
780             /*
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.
784              */
785             date = apr_palloc(r->pool, APR_RFC822_DATE_LEN);
786             ap_recent_rfc822_date(date, r->request_time);
787             proxy_date = date;
788         }
789         form_header_field(&h, "Date", proxy_date);
790         server = apr_table_get(r->headers_out, "Server");
791         if (server) {
792             form_header_field(&h, "Server", server);
793         } else {
794             form_header_field(&h, "Server", ap_get_server_banner());
795         }
796     }
797     else {
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());
802     }
803
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");
807 }
808
809 AP_DECLARE(void) ap_basic_http_header(request_rec *r, apr_bucket_brigade *bb)
810 {
811     const char *protocol;
812
813     basic_http_header_check(r, &protocol);
814     basic_http_header(r, bb, protocol);
815 }
816
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.
824  *
825  * This should also work on keepalive connections assuming they use the
826  * same small buffer for the first read of each new request.
827  *
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.
833  */
834 static void terminate_header(apr_bucket_brigade *bb)
835 {
836     char tmp[] = "X-Pad: avoid browser bug" CRLF;
837     char crlf[] = CRLF;
838     apr_off_t len;
839     apr_size_t buflen;
840
841     (void) apr_brigade_length(bb, 1, &len);
842
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);
847     }
848     buflen = strlen(crlf);
849     ap_xlate_proto_to_ascii(crlf, buflen);
850     apr_brigade_write(bb, NULL, NULL, crlf, buflen);
851 }
852
853 AP_DECLARE_NONSTD(int) ap_send_http_trace(request_rec *r)
854 {
855     core_server_config *conf;
856     int rv;
857     apr_bucket_brigade *bb;
858     header_struct h;
859     apr_bucket *b;
860     int body;
861     char *bodyread = NULL, *bodyoff;
862     apr_size_t bodylen = 0;
863     apr_size_t bodybuf;
864     long res = -1; /* init to avoid gcc -Wall warning */
865
866     if (r->method_number != M_TRACE) {
867         return DECLINED;
868     }
869
870     /* Get the original request */
871     while (r->prev) {
872         r = r->prev;
873     }
874     conf = (core_server_config *)ap_get_module_config(r->server->module_config,
875                                                       &core_module);
876
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;
881     }
882
883     if (conf->trace_enable == AP_TRACE_EXTENDED)
884         /* XX should be = REQUEST_CHUNKED_PASS */
885         body = REQUEST_CHUNKED_DECHUNK;
886     else
887         body = REQUEST_NO_BODY;
888
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");
893         return rv;
894     }
895
896     if (ap_should_client_block(r)) {
897
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;
903             }
904             /* always 32 extra bytes to catch chunk header exceptions */
905             bodybuf = (apr_size_t)r->remaining + 32;
906         }
907         else {
908             /* Add an extra 8192 for chunk headers */
909             bodybuf = 73730;
910         }
911
912         bodyoff = bodyread = apr_palloc(r->pool, bodybuf);
913
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) {
917             bodylen += res;
918             bodybuf -= res;
919             bodyoff += res;
920         }
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)
924                 ;
925             apr_table_setn(r->notes, "error-notes",
926                    "Extended TRACE request bodies cannot exceed 64k\n");
927             return HTTP_REQUEST_ENTITY_TOO_LARGE;
928         }
929
930         if (res < 0) {
931             return HTTP_BAD_REQUEST;
932         }
933     }
934
935     ap_set_content_type(r, "message/http");
936
937     /* Now we recreate the request, and echo it back */
938
939     bb = apr_brigade_create(r->pool, r->connection->bucket_alloc);
940     apr_brigade_putstrs(bb, NULL, NULL, r->the_request, CRLF, NULL);
941     h.pool = r->pool;
942     h.bb = bb;
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);
946
947     /* If configured to accept a body, echo the body */
948     if (bodylen) {
949         b = apr_bucket_pool_create(bodyread, bodylen,
950                                    r->pool, bb->bucket_alloc);
951         APR_BRIGADE_INSERT_TAIL(bb, b);
952     }
953
954     ap_pass_brigade(r->output_filters,  bb);
955
956     return DONE;
957 }
958
959 typedef struct header_filter_ctx {
960     int headers_sent;
961 } header_filter_ctx;
962
963 AP_CORE_DECLARE_NONSTD(apr_status_t) ap_http_header_filter(ap_filter_t *f,
964                                                            apr_bucket_brigade *b)
965 {
966     request_rec *r = f->r;
967     conn_rec *c = r->connection;
968     const char *clheader;
969     const char *protocol;
970     apr_bucket *e;
971     apr_bucket_brigade *b2;
972     header_struct h;
973     header_filter_ctx *ctx = f->ctx;
974     const char *ctype;
975
976     AP_DEBUG_ASSERT(!r->main);
977
978     if (r->header_only) {
979         if (!ctx) {
980             ctx = f->ctx = apr_pcalloc(r->pool, sizeof(header_filter_ctx));
981         }
982         else if (ctx->headers_sent) {
983             apr_brigade_destroy(b);
984             return OK;
985         }
986     }
987
988     for (e = APR_BRIGADE_FIRST(b);
989          e != APR_BRIGADE_SENTINEL(b);
990          e = APR_BUCKET_NEXT(e))
991     {
992         if (AP_BUCKET_IS_ERROR(e)) {
993             ap_bucket_error *eb = e->data;
994
995             ap_die(eb->status, r);
996             return AP_FILTER_ERROR;
997         }
998     }
999
1000     if (r->assbackwards) {
1001         r->sent_bodyct = 1;
1002         ap_remove_output_filter(f);
1003         return ap_pass_brigade(f->next, b);
1004     }
1005
1006     /*
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.
1010      */
1011     if (!apr_is_empty_table(r->err_headers_out)) {
1012         r->headers_out = apr_table_overlay(r->pool, r->err_headers_out,
1013                                            r->headers_out);
1014     }
1015
1016     /*
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.
1020      *
1021      * Note: the force-response-1.0 should come before the call to
1022      *       basic_http_header_check()
1023      */
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");
1028     }
1029     else {
1030         fixup_vary(r);
1031     }
1032
1033     /*
1034      * Now remove any ETag response header field if earlier processing
1035      * says so (such as a 'FileETag None' directive).
1036      */
1037     if (apr_table_get(r->notes, "no-etag") != NULL) {
1038         apr_table_unset(r->headers_out, "ETag");
1039     }
1040
1041     /* determine the protocol and whether we should use keepalives. */
1042     basic_http_header_check(r, &protocol);
1043     ap_set_keepalive(r);
1044
1045     if (r->chunked) {
1046         apr_table_mergen(r->headers_out, "Transfer-Encoding", "chunked");
1047         apr_table_unset(r->headers_out, "Content-Length");
1048     }
1049
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);
1053     }
1054
1055     if (r->content_encoding) {
1056         apr_table_setn(r->headers_out, "Content-Encoding",
1057                        r->content_encoding);
1058     }
1059
1060     if (!apr_is_empty_array(r->content_languages)) {
1061         int i;
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]);
1065         }
1066     }
1067
1068     /*
1069      * Control cachability for non-cachable responses if not already set by
1070      * some other part of the server configuration.
1071      */
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);
1076     }
1077
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
1089      */
1090     if (r->header_only
1091         && (clheader = apr_table_get(r->headers_out, "Content-Length"))
1092         && !strcmp(clheader, "0")) {
1093         apr_table_unset(r->headers_out, "Content-Length");
1094     }
1095
1096     b2 = apr_brigade_create(r->pool, c->bucket_alloc);
1097     basic_http_header(r, b2, protocol);
1098
1099     h.pool = r->pool;
1100     h.bb = b2;
1101
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,
1105                      "Connection",
1106                      "Keep-Alive",
1107                      "ETag",
1108                      "Content-Location",
1109                      "Expires",
1110                      "Cache-Control",
1111                      "Vary",
1112                      "Warning",
1113                      "WWW-Authenticate",
1114                      "Proxy-Authenticate",
1115                      "Set-Cookie",
1116                      "Set-Cookie2",
1117                      NULL);
1118     }
1119     else {
1120         send_all_header_fields(&h, r);
1121     }
1122
1123     terminate_header(b2);
1124
1125     ap_pass_brigade(f->next, b2);
1126
1127     if (r->header_only) {
1128         apr_brigade_destroy(b);
1129         ctx->headers_sent = 1;
1130         return OK;
1131     }
1132
1133     r->sent_bodyct = 1;         /* Whatever follows is real body stuff... */
1134
1135     if (r->chunked) {
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.
1139          */
1140         ap_add_output_filter("CHUNK", NULL, r, r->connection);
1141     }
1142
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.
1146      */
1147     ap_remove_output_filter(f);
1148     return ap_pass_brigade(f->next, b);
1149 }
1150
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.
1157  *
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.,
1160  *
1161  *    if ((retval = ap_discard_request_body(r)) != OK) {
1162  *        return retval;
1163  *    }
1164  */
1165 AP_DECLARE(int) ap_discard_request_body(request_rec *r)
1166 {
1167     apr_bucket_brigade *bb, *kept_body = NULL;
1168     apr_bucket *e;
1169     int rv, seen_eos;
1170     core_dir_conf *dconf;
1171     apr_size_t left = 0;
1172
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
1176      * to avoid.
1177      *
1178      * This function is also a no-op on a subrequest.
1179      */
1180     if (r->main || r->connection->keepalive == AP_CONN_CLOSE ||
1181         ap_status_drops_connection(r->status)) {
1182         return OK;
1183     }
1184
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.
1189      */
1190     if (!r->kept_body) {
1191         dconf = ap_get_module_config(r->per_dir_config,
1192                                      &http_module);
1193         if (dconf->keep_body > 0) {
1194             left = dconf->keep_body;
1195             kept_body = apr_brigade_create(r->pool, r->connection->bucket_alloc);
1196         }
1197     }
1198
1199     bb = apr_brigade_create(r->pool, r->connection->bucket_alloc);
1200     seen_eos = 0;
1201     do {
1202         apr_bucket *bucket;
1203
1204         rv = ap_get_brigade(r->input_filters, bb, AP_MODE_READBYTES,
1205                             APR_BLOCK_READ, HUGE_STRING_LEN);
1206
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.
1210              *
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.
1214              */
1215             if (rv == AP_FILTER_ERROR) {
1216                 apr_brigade_destroy(bb);
1217                 return rv;
1218             }
1219             else {
1220                 apr_brigade_destroy(bb);
1221                 return HTTP_BAD_REQUEST;
1222             }
1223         }
1224
1225         for (bucket = APR_BRIGADE_FIRST(bb);
1226              bucket != APR_BRIGADE_SENTINEL(bb);
1227              bucket = APR_BUCKET_NEXT(bucket))
1228         {
1229             const char *data;
1230             apr_size_t len;
1231
1232             if (APR_BUCKET_IS_EOS(bucket)) {
1233                 seen_eos = 1;
1234                 break;
1235             }
1236
1237             /* These are metadata buckets. */
1238             if (bucket->length == 0) {
1239                 continue;
1240             }
1241
1242             /* We MUST read because in case we have an unknown-length
1243              * bucket or one that morphs, we want to exhaust it.
1244              */
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;
1249             }
1250
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.
1255              */
1256             if (kept_body) {
1257                 if (len <= left) {
1258                     apr_bucket_copy(bucket, &e);
1259                     APR_BRIGADE_INSERT_TAIL(kept_body, e);
1260                     left -= len;
1261                 }
1262                 else {
1263                     apr_brigade_destroy(bb);
1264                     apr_brigade_destroy(kept_body);
1265                     return HTTP_REQUEST_ENTITY_TOO_LARGE;
1266                 }
1267             }
1268             
1269         }
1270         apr_brigade_cleanup(bb);
1271     } while (!seen_eos);
1272
1273     if (kept_body) {
1274         r->kept_body = kept_body;
1275     }
1276
1277     return OK;
1278 }
1279
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.
1283  *
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.
1286  *
1287  * The proper procedure is this:
1288  *
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
1295  *
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.
1300  *
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.
1303  *
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.
1307  *
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.
1313  *
1314  *    This step also sends a 100 Continue response to HTTP/1.1 clients if appropriate.
1315  */
1316
1317 AP_DECLARE(int) ap_setup_client_block(request_rec *r, int read_policy)
1318 {
1319     const char *tenc = apr_table_get(r->headers_in, "Transfer-Encoding");
1320     const char *lenp = apr_table_get(r->headers_in, "Content-Length");
1321
1322     r->read_body = read_policy;
1323     r->read_chunked = 0;
1324     r->remaining = 0;
1325
1326     if (tenc) {
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;
1331         }
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;
1336         }
1337
1338         r->read_chunked = 1;
1339     }
1340     else if (lenp) {
1341         char *endstr;
1342
1343         if (apr_strtoff(&r->remaining, lenp, &endstr, 10)
1344             || *endstr || r->remaining < 0) {
1345             r->remaining = 0;
1346             ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
1347                           "Invalid Content-Length");
1348             return HTTP_BAD_REQUEST;
1349         }
1350     }
1351
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;
1357     }
1358
1359 #ifdef AP_DEBUG
1360     {
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,
1364                                                         &core_module);
1365         AP_DEBUG_ASSERT(APR_BRIGADE_EMPTY(req_cfg->bb));
1366     }
1367 #endif
1368
1369     return OK;
1370 }
1371
1372 AP_DECLARE(int) ap_should_client_block(request_rec *r)
1373 {
1374     /* First check if we have already read the request body */
1375
1376     if (r->read_length || (!r->read_chunked && (r->remaining <= 0))) {
1377         return 0;
1378     }
1379
1380     return 1;
1381 }
1382
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.
1391  *
1392  */
1393 AP_DECLARE(long) ap_get_client_block(request_rec *r, char *buffer,
1394                                      apr_size_t bufsiz)
1395 {
1396     apr_status_t rv;
1397     apr_bucket_brigade *bb;
1398
1399     if (r->remaining < 0 || (!r->read_chunked && r->remaining == 0)) {
1400         return 0;
1401     }
1402
1403     bb = apr_brigade_create(r->pool, r->connection->bucket_alloc);
1404     if (bb == NULL) {
1405         r->connection->keepalive = AP_CONN_CLOSE;
1406         return -1;
1407     }
1408
1409     rv = ap_get_brigade(r->input_filters, bb, AP_MODE_READBYTES,
1410                         APR_BLOCK_READ, bufsiz);
1411
1412     /* We lose the failure code here.  This is why ap_get_client_block should
1413      * not be used.
1414      */
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.
1418          */
1419         r->connection->keepalive = AP_CONN_CLOSE;
1420         apr_brigade_destroy(bb);
1421         return -1;
1422     }
1423
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.
1427      */
1428     AP_DEBUG_ASSERT(!APR_BRIGADE_EMPTY(bb));
1429
1430     /* Check to see if EOS in the brigade.
1431      *
1432      * If so, we have to leave a nugget for the *next* ap_get_client_block
1433      * call to return 0.
1434      */
1435     if (APR_BUCKET_IS_EOS(APR_BRIGADE_LAST(bb))) {
1436         if (r->read_chunked) {
1437             r->remaining = -1;
1438         }
1439         else {
1440             r->remaining = 0;
1441         }
1442     }
1443
1444     rv = apr_brigade_flatten(bb, buffer, &bufsiz);
1445     if (rv != APR_SUCCESS) {
1446         apr_brigade_destroy(bb);
1447         return -1;
1448     }
1449
1450     /* XXX yank me? */
1451     r->read_length += bufsiz;
1452
1453     apr_brigade_destroy(bb);
1454     return bufsiz;
1455 }
1456
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)
1460 {
1461     request_rec *r = f->r;
1462     apr_bucket *e;
1463
1464     for (e = APR_BRIGADE_FIRST(b);
1465          e != APR_BRIGADE_SENTINEL(b);
1466          e = APR_BUCKET_NEXT(e))
1467     {
1468         if (AP_BUCKET_IS_ERROR(e)) {
1469             /*
1470              * Start of error handling state tree. Just one condition
1471              * right now :)
1472              */
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;
1476             }
1477         }
1478     }
1479
1480     return ap_pass_brigade(f->next,  b);
1481 }
1482
1483 typedef struct kept_body_filter_ctx {
1484     apr_off_t offset;
1485     apr_off_t remaining;
1486 } kept_body_ctx_t;
1487
1488 /**
1489  * Initialisation of filter to handle a kept body on subrequests.
1490  * 
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.
1494  */
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;
1499
1500     if (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));
1504     }
1505
1506     return OK;
1507 }
1508
1509 /**
1510  * Filter to handle a kept body on subrequests.
1511  * 
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.
1514  */
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;
1522     apr_status_t rv;
1523
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);
1527     }
1528
1529     /* set up the context if it does not already exist */
1530     if (!ctx) {
1531         f->ctx = ctx = apr_palloc(f->r->pool, sizeof(*ctx));
1532         ctx->offset = 0;
1533         apr_brigade_length(kept_body, 1, &ctx->remaining);
1534     }
1535
1536     /* kept_body is finished, send next filter */
1537     if (ctx->remaining <= 0) {
1538         return ap_get_brigade(f->next, b, mode, block, readbytes);
1539     }
1540
1541     /* send all of the kept_body, but no more */
1542     if (readbytes > ctx->remaining) {
1543         readbytes = ctx->remaining;
1544     }
1545
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);
1550         return rv;
1551     }
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);
1555         return rv;
1556     }
1557  
1558     do {
1559         apr_bucket *foo;
1560         const char *str;
1561         apr_size_t len;
1562
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);
1571         }
1572         APR_BRIGADE_INSERT_TAIL(b, foo);
1573         ec = APR_BUCKET_NEXT(ec);
1574     } while (ec != e2);
1575
1576     ctx->remaining -= readbytes;
1577     ctx->offset += readbytes;
1578     return APR_SUCCESS;
1579
1580 }