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