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
29
#include "http_config.h"
30
#include "http_connection.h"
31
#include "http_core.h"
32
#include "http_protocol.h" /* For index_of_response(). Grump. */
33
#include "http_request.h"
35
#include "util_filter.h"
36
#include "util_ebcdic.h"
38
#include "scoreboard.h"
43
* A pointer to this is used to memorize in the filter context that a bad
44
* gateway error bucket had been seen. It is used as an invented unique pointer.
46
static char bad_gateway_seen;
48
apr_status_t ap_http_chunk_filter(ap_filter_t *f, apr_bucket_brigade *b)
50
#define ASCII_CRLF "\015\012"
51
#define ASCII_ZERO "\060"
52
conn_rec *c = f->r->connection;
53
apr_bucket_brigade *more;
57
for (more = NULL; b; b = more, more = NULL) {
59
apr_bucket *eos = NULL;
60
apr_bucket *flush = NULL;
61
/* XXX: chunk_hdr must remain at this scope since it is used in a
64
char chunk_hdr[20]; /* enough space for the snprintf below */
67
for (e = APR_BRIGADE_FIRST(b);
68
e != APR_BRIGADE_SENTINEL(b);
69
e = APR_BUCKET_NEXT(e))
71
if (APR_BUCKET_IS_EOS(e)) {
72
/* there shouldn't be anything after the eos */
76
if (AP_BUCKET_IS_ERROR(e)
77
&& (((ap_bucket_error *)(e->data))->status
78
== HTTP_BAD_GATEWAY)) {
80
* We had a broken backend. Memorize this in the filter
83
f->ctx = &bad_gateway_seen;
86
if (APR_BUCKET_IS_FLUSH(e)) {
88
more = apr_brigade_split(b, APR_BUCKET_NEXT(e));
91
else if (e->length == (apr_size_t)-1) {
92
/* unknown amount of data (e.g. a pipe) */
96
rv = apr_bucket_read(e, &data, &len, APR_BLOCK_READ);
97
if (rv != APR_SUCCESS) {
102
* There may be a new next bucket representing the
103
* rest of the data stream on which a read() may
104
* block so we pass down what we have so far.
107
more = apr_brigade_split(b, APR_BUCKET_NEXT(e));
111
/* If there was nothing in this bucket then we can
112
* safely move on to the next one without pausing
113
* to pass down what we have counted up so far.
124
* XXX: if there aren't very many bytes at this point it may
125
* be a good idea to set them aside and return for more,
126
* unless we haven't finished counting this brigade yet.
128
/* if there are content bytes, then wrap them in a chunk */
132
* Insert the chunk header, specifying the number of bytes in
135
hdr_len = apr_snprintf(chunk_hdr, sizeof(chunk_hdr),
136
"%" APR_UINT64_T_HEX_FMT CRLF, (apr_uint64_t)bytes);
137
ap_xlate_proto_to_ascii(chunk_hdr, hdr_len);
138
e = apr_bucket_transient_create(chunk_hdr, hdr_len,
140
APR_BRIGADE_INSERT_HEAD(b, e);
143
* Insert the end-of-chunk CRLF before an EOS or
144
* FLUSH bucket, or appended to the brigade
146
e = apr_bucket_immortal_create(ASCII_CRLF, 2, c->bucket_alloc);
148
APR_BUCKET_INSERT_BEFORE(eos, e);
150
else if (flush != NULL) {
151
APR_BUCKET_INSERT_BEFORE(flush, e);
154
APR_BRIGADE_INSERT_TAIL(b, e);
158
/* RFC 2616, Section 3.6.1
160
* If there is an EOS bucket, then prefix it with:
161
* 1) the last-chunk marker ("0" CRLF)
163
* 3) the end-of-chunked body CRLF
165
* We only do this if we have not seen an error bucket with
166
* status HTTP_BAD_GATEWAY. We have memorized an
167
* error bucket that we had seen in the filter context.
168
* The error bucket with status HTTP_BAD_GATEWAY indicates that the
169
* connection to the backend (mod_proxy) broke in the middle of the
170
* response. In order to signal the client that something went wrong
171
* we do not create the last-chunk marker and set c->keepalive to
172
* AP_CONN_CLOSE in the core output filter.
174
* XXX: it would be nice to combine this with the end-of-chunk
175
* marker above, but this is a bit more straight-forward for
178
if (eos && !f->ctx) {
179
/* XXX: (2) trailers ... does not yet exist */
180
e = apr_bucket_immortal_create(ASCII_ZERO ASCII_CRLF
182
ASCII_CRLF, 5, c->bucket_alloc);
183
APR_BUCKET_INSERT_BEFORE(eos, e);
186
/* pass the brigade to the next filter. */
187
rv = ap_pass_brigade(f->next, b);
188
if (rv != APR_SUCCESS || eos != NULL) {