1 /* Licensed to the Apache Software Foundation (ASF) under one or more
2 * contributor license agreements. See the NOTICE file distributed with
3 * this work for additional information regarding copyright ownership.
4 * The ASF licenses this file to You under the Apache License, Version 2.0
5 * (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 * chunk_filter.c --- HTTP/1.1 chunked transfer encoding filter.
21 #include "apr_strings.h"
22 #include "apr_thread_proc.h" /* for RLIMIT stuff */
24 #define APR_WANT_STRFUNC
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"
34 #include "util_filter.h"
35 #include "util_ebcdic.h"
37 #include "scoreboard.h"
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.
45 static char bad_gateway_seen;
47 apr_status_t ap_http_chunk_filter(ap_filter_t *f, apr_bucket_brigade *b)
49 #define ASCII_CRLF "\015\012"
50 #define ASCII_ZERO "\060"
51 conn_rec *c = f->r->connection;
52 apr_bucket_brigade *more, *tmp;
56 for (more = tmp = NULL; b; b = more, more = NULL) {
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
63 char chunk_hdr[20]; /* enough space for the snprintf below */
66 for (e = APR_BRIGADE_FIRST(b);
67 e != APR_BRIGADE_SENTINEL(b);
68 e = APR_BUCKET_NEXT(e))
70 if (APR_BUCKET_IS_EOS(e)) {
71 /* there shouldn't be anything after the eos */
75 if (AP_BUCKET_IS_ERROR(e) &&
76 (((ap_bucket_error *)(e->data))->status == HTTP_BAD_GATEWAY ||
77 ((ap_bucket_error *)(e->data))->status == HTTP_GATEWAY_TIME_OUT)) {
79 * We had a broken backend. Memorize this in the filter
82 f->ctx = &bad_gateway_seen;
85 if (APR_BUCKET_IS_FLUSH(e)) {
87 if (e != APR_BRIGADE_LAST(b)) {
88 more = apr_brigade_split_ex(b, APR_BUCKET_NEXT(e), tmp);
92 else if (e->length == (apr_size_t)-1) {
93 /* unknown amount of data (e.g. a pipe) */
97 rv = apr_bucket_read(e, &data, &len, APR_BLOCK_READ);
98 if (rv != APR_SUCCESS) {
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.
108 more = apr_brigade_split_ex(b, APR_BUCKET_NEXT(e), tmp);
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.
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.
129 /* if there are content bytes, then wrap them in a chunk */
133 * Insert the chunk header, specifying the number of bytes in
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,
141 APR_BRIGADE_INSERT_HEAD(b, e);
144 * Insert the end-of-chunk CRLF before an EOS or
145 * FLUSH bucket, or appended to the brigade
147 e = apr_bucket_immortal_create(ASCII_CRLF, 2, c->bucket_alloc);
149 APR_BUCKET_INSERT_BEFORE(eos, e);
151 else if (flush != NULL) {
152 APR_BUCKET_INSERT_BEFORE(flush, e);
155 APR_BRIGADE_INSERT_TAIL(b, e);
159 /* RFC 2616, Section 3.6.1
161 * If there is an EOS bucket, then prefix it with:
162 * 1) the last-chunk marker ("0" CRLF)
164 * 3) the end-of-chunked body CRLF
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.
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
179 if (eos && !f->ctx) {
180 /* XXX: (2) trailers ... does not yet exist */
181 e = apr_bucket_immortal_create(ASCII_ZERO ASCII_CRLF
183 ASCII_CRLF, 5, c->bucket_alloc);
184 APR_BUCKET_INSERT_BEFORE(eos, e);
187 /* pass the brigade to the next filter. */
188 rv = ap_pass_brigade(f->next, b);
189 if (rv != APR_SUCCESS || eos != NULL) {
193 apr_brigade_cleanup(tmp);