Apache HTTPD
chunk_filter.c
Go to the documentation of this file.
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
48{
49#define ASCII_CRLF "\015\012"
50#define ASCII_ZERO "\060"
51 conn_rec *c = f->r->connection;
54 apr_status_t rv;
55
56 for (more = tmp = NULL; b; b = more, more = NULL) {
57 apr_off_t bytes = 0;
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);
69 {
70 if (APR_BUCKET_IS_EOS(e)) {
71 /* there shouldn't be anything after the eos */
73 eos = e;
74 break;
75 }
77 && (((ap_bucket_error *)(e->data))->status
78 == HTTP_BAD_GATEWAY)) {
79 /*
80 * We had a broken backend. Memorize this in the filter
81 * context.
82 */
83 f->ctx = &bad_gateway_seen;
84 continue;
85 }
87 flush = e;
88 if (e != APR_BRIGADE_LAST(b)) {
90 }
91 break;
92 }
93 else if (e->length == (apr_size_t)-1) {
94 /* unknown amount of data (e.g. a pipe) */
95 const char *data;
97
99 if (rv != APR_SUCCESS) {
100 return rv;
101 }
102 if (len > 0) {
103 /*
104 * There may be a new next bucket representing the
105 * rest of the data stream on which a read() may
106 * block so we pass down what we have so far.
107 */
108 bytes += len;
110 break;
111 }
112 else {
113 /* If there was nothing in this bucket then we can
114 * safely move on to the next one without pausing
115 * to pass down what we have counted up so far.
116 */
117 continue;
118 }
119 }
120 else {
121 bytes += e->length;
122 }
123 }
124
125 /*
126 * XXX: if there aren't very many bytes at this point it may
127 * be a good idea to set them aside and return for more,
128 * unless we haven't finished counting this brigade yet.
129 */
130 /* if there are content bytes, then wrap them in a chunk */
131 if (bytes > 0) {
133 /*
134 * Insert the chunk header, specifying the number of bytes in
135 * the chunk.
136 */
141 c->bucket_alloc);
143
144 /*
145 * Insert the end-of-chunk CRLF before an EOS or
146 * FLUSH bucket, or appended to the brigade
147 */
148 e = apr_bucket_immortal_create(ASCII_CRLF, 2, c->bucket_alloc);
149 if (eos != NULL) {
151 }
152 else if (flush != NULL) {
154 }
155 else {
157 }
158 }
159
160 /* RFC 2616, Section 3.6.1
161 *
162 * If there is an EOS bucket, then prefix it with:
163 * 1) the last-chunk marker ("0" CRLF)
164 * 2) the trailer
165 * 3) the end-of-chunked body CRLF
166 *
167 * We only do this if we have not seen an error bucket with
168 * status HTTP_BAD_GATEWAY. We have memorized an
169 * error bucket that we had seen in the filter context.
170 * The error bucket with status HTTP_BAD_GATEWAY indicates that the
171 * connection to the backend (mod_proxy) broke in the middle of the
172 * response. In order to signal the client that something went wrong
173 * we do not create the last-chunk marker and set c->keepalive to
174 * AP_CONN_CLOSE in the core output filter.
175 *
176 * XXX: it would be nice to combine this with the end-of-chunk
177 * marker above, but this is a bit more straight-forward for
178 * now.
179 */
180 if (eos && !f->ctx) {
181 /* XXX: (2) trailers ... does not yet exist */
183 /* <trailers> */
184 ASCII_CRLF, 5, c->bucket_alloc);
186 }
187
188 /* pass the brigade to the next filter. */
189 rv = ap_pass_brigade(f->next, b);
191 if (rv != APR_SUCCESS || eos != NULL) {
192 return rv;
193 }
194 tmp = b;
195 }
196 return APR_SUCCESS;
197}
Apache Multi-Processing Module library.
const char apr_size_t len
Definition ap_regex.h:187
APR Strings library.
APR Thread and Process Library.
APR Standard Headers Support.
#define ASCII_CRLF
#define ASCII_ZERO
static char bad_gateway_seen
int flush
#define ap_xlate_proto_to_ascii(x, y)
Definition util_ebcdic.h:80
apr_status_t ap_pass_brigade(ap_filter_t *filter, apr_bucket_brigade *bucket)
void ap_remove_output_filter(ap_filter_t *f)
#define AP_BUCKET_IS_ERROR(e)
#define CRLF
Definition httpd.h:724
apr_file_t * f
#define APR_BUCKET_IS_FLUSH(e)
#define APR_BRIGADE_LAST(b)
#define APR_BRIGADE_INSERT_TAIL(b, e)
#define APR_BRIGADE_INSERT_HEAD(b, e)
#define APR_BUCKET_NEXT(e)
apr_bucket * e
#define APR_BRIGADE_SENTINEL(b)
#define APR_BUCKET_IS_EOS(e)
#define APR_BRIGADE_FIRST(b)
#define APR_BUCKET_INSERT_BEFORE(a, b)
#define apr_bucket_read(e, str, len, block)
@ APR_BLOCK_READ
Definition apr_buckets.h:58
#define HTTP_BAD_GATEWAY
Definition httpd.h:537
apr_status_t ap_http_chunk_filter(ap_filter_t *f, apr_bucket_brigade *b)
apr_size_t size
#define APR_SUCCESS
Definition apr_errno.h:225
int apr_status_t
Definition apr_errno.h:44
void * data
apr_vformatter_buff_t * c
Definition apr_lib.h:175
apr_pool_t * b
Definition apr_pools.h:529
const void apr_size_t bytes
Definition apr_random.h:91
Apache Configuration.
Apache connection library.
CORE HTTP Daemon.
HTTP protocol handling.
Apache Request library.
HTTP Daemon routines.
mod_core private header file
return NULL
Definition mod_so.c:359
Apache scoreboard library.
A bucket referring to an HTTP error.
The representation of a filter chain.
apr_size_t length
void * data
Structure to store things which are per connection.
Definition httpd.h:1152
Utilities for EBCDIC conversion.
Apache filter library.