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 * chunk_filter.c --- HTTP/1.1 chunked transfer encoding filter.
19 */
20
21#include "apr_strings.h"
22#include "apr_thread_proc.h"    /* for RLIMIT stuff */
23
24#define APR_WANT_STRFUNC
25#include "apr_want.h"
26
27#include "httpd.h"
28#include "http_config.h"
29#include "http_connection.h"
30#include "http_core.h"
31#include "http_protocol.h"  /* For index_of_response().  Grump. */
32#include "http_request.h"
33
34#include "util_filter.h"
35#include "util_ebcdic.h"
36#include "ap_mpm.h"
37#include "scoreboard.h"
38
39#include "mod_core.h"
40
41/*
42 * A pointer to this is used to memorize in the filter context that a bad
43 * gateway error bucket had been seen. It is used as an invented unique pointer.
44 */
45static char bad_gateway_seen;
46
47apr_status_t ap_http_chunk_filter(ap_filter_t *f, apr_bucket_brigade *b)
48{
49#define ASCII_CRLF  "\015\012"
50#define ASCII_ZERO  "\060"
51    conn_rec *c = f->r->connection;
52    apr_bucket_brigade *more, *tmp;
53    apr_bucket *e;
54    apr_status_t rv;
55
56    for (more = tmp = NULL; b; b = more, more = NULL) {
57        apr_off_t bytes = 0;
58        apr_bucket *eos = NULL;
59        apr_bucket *flush = NULL;
60        /* XXX: chunk_hdr must remain at this scope since it is used in a
61         *      transient bucket.
62         */
63        char chunk_hdr[20]; /* enough space for the snprintf below */
64
65
66        for (e = APR_BRIGADE_FIRST(b);
67             e != APR_BRIGADE_SENTINEL(b);
68             e = APR_BUCKET_NEXT(e))
69        {
70            if (APR_BUCKET_IS_EOS(e)) {
71                /* there shouldn't be anything after the eos */
72                eos = e;
73                break;
74            }
75            if (AP_BUCKET_IS_ERROR(e)
76                && (((ap_bucket_error *)(e->data))->status
77                    == HTTP_BAD_GATEWAY)) {
78                /*
79                 * We had a broken backend. Memorize this in the filter
80                 * context.
81                 */
82                f->ctx = &bad_gateway_seen;
83                continue;
84            }
85            if (APR_BUCKET_IS_FLUSH(e)) {
86                flush = e;
87                if (e != APR_BRIGADE_LAST(b)) {
88                    more = apr_brigade_split_ex(b, APR_BUCKET_NEXT(e), tmp);
89                }
90                break;
91            }
92            else if (e->length == (apr_size_t)-1) {
93                /* unknown amount of data (e.g. a pipe) */
94                const char *data;
95                apr_size_t len;
96
97                rv = apr_bucket_read(e, &data, &len, APR_BLOCK_READ);
98                if (rv != APR_SUCCESS) {
99                    return rv;
100                }
101                if (len > 0) {
102                    /*
103                     * There may be a new next bucket representing the
104                     * rest of the data stream on which a read() may
105                     * block so we pass down what we have so far.
106                     */
107                    bytes += len;
108                    more = apr_brigade_split_ex(b, APR_BUCKET_NEXT(e), tmp);
109                    break;
110                }
111                else {
112                    /* If there was nothing in this bucket then we can
113                     * safely move on to the next one without pausing
114                     * to pass down what we have counted up so far.
115                     */
116                    continue;
117                }
118            }
119            else {
120                bytes += e->length;
121            }
122        }
123
124        /*
125         * XXX: if there aren't very many bytes at this point it may
126         * be a good idea to set them aside and return for more,
127         * unless we haven't finished counting this brigade yet.
128         */
129        /* if there are content bytes, then wrap them in a chunk */
130        if (bytes > 0) {
131            apr_size_t hdr_len;
132            /*
133             * Insert the chunk header, specifying the number of bytes in
134             * the chunk.
135             */
136            hdr_len = apr_snprintf(chunk_hdr, sizeof(chunk_hdr),
137                                   "%" APR_UINT64_T_HEX_FMT CRLF, (apr_uint64_t)bytes);
138            ap_xlate_proto_to_ascii(chunk_hdr, hdr_len);
139            e = apr_bucket_transient_create(chunk_hdr, hdr_len,
140                                            c->bucket_alloc);
141            APR_BRIGADE_INSERT_HEAD(b, e);
142
143            /*
144             * Insert the end-of-chunk CRLF before an EOS or
145             * FLUSH bucket, or appended to the brigade
146             */
147            e = apr_bucket_immortal_create(ASCII_CRLF, 2, c->bucket_alloc);
148            if (eos != NULL) {
149                APR_BUCKET_INSERT_BEFORE(eos, e);
150            }
151            else if (flush != NULL) {
152                APR_BUCKET_INSERT_BEFORE(flush, e);
153            }
154            else {
155                APR_BRIGADE_INSERT_TAIL(b, e);
156            }
157        }
158
159        /* RFC 2616, Section 3.6.1
160         *
161         * If there is an EOS bucket, then prefix it with:
162         *   1) the last-chunk marker ("0" CRLF)
163         *   2) the trailer
164         *   3) the end-of-chunked body CRLF
165         *
166         * We only do this if we have not seen an error bucket with
167         * status HTTP_BAD_GATEWAY. We have memorized an
168         * error bucket that we had seen in the filter context.
169         * The error bucket with status HTTP_BAD_GATEWAY indicates that the
170         * connection to the backend (mod_proxy) broke in the middle of the
171         * response. In order to signal the client that something went wrong
172         * we do not create the last-chunk marker and set c->keepalive to
173         * AP_CONN_CLOSE in the core output filter.
174         *
175         * XXX: it would be nice to combine this with the end-of-chunk
176         * marker above, but this is a bit more straight-forward for
177         * now.
178         */
179        if (eos && !f->ctx) {
180            /* XXX: (2) trailers ... does not yet exist */
181            e = apr_bucket_immortal_create(ASCII_ZERO ASCII_CRLF
182                                           /* <trailers> */
183                                           ASCII_CRLF, 5, c->bucket_alloc);
184            APR_BUCKET_INSERT_BEFORE(eos, e);
185        }
186
187        /* pass the brigade to the next filter. */
188        rv = ap_pass_brigade(f->next, b);
189        if (rv != APR_SUCCESS || eos != NULL) {
190            return rv;
191        }
192        tmp = b;
193        apr_brigade_cleanup(tmp);
194    }
195    return APR_SUCCESS;
196}
197