]> granicus.if.org Git - apache/blob - modules/http/chunk_filter.c
this appears to be a "mv" rather than a "swap", so we should be able to
[apache] / modules / http / chunk_filter.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  * 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 #define CORE_PRIVATE
28 #include "httpd.h"
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"
34
35 #include "util_filter.h"
36 #include "util_ebcdic.h"
37 #include "ap_mpm.h"
38 #include "scoreboard.h"
39
40 #include "mod_core.h"
41
42 /*
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.
45  */
46 static char bad_gateway_seen;
47
48 apr_status_t ap_http_chunk_filter(ap_filter_t *f, apr_bucket_brigade *b)
49 {
50 #define ASCII_CRLF  "\015\012"
51 #define ASCII_ZERO  "\060"
52     conn_rec *c = f->r->connection;
53     apr_bucket_brigade *more;
54     apr_bucket *e;
55     apr_status_t rv;
56
57     for (more = NULL; b; b = more, more = NULL) {
58         apr_off_t bytes = 0;
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
62          *      transient bucket.
63          */
64         char chunk_hdr[20]; /* enough space for the snprintf below */
65
66
67         for (e = APR_BRIGADE_FIRST(b);
68              e != APR_BRIGADE_SENTINEL(b);
69              e = APR_BUCKET_NEXT(e))
70         {
71             if (APR_BUCKET_IS_EOS(e)) {
72                 /* there shouldn't be anything after the eos */
73                 eos = e;
74                 break;
75             }
76             if (AP_BUCKET_IS_ERROR(e)
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             }
86             if (APR_BUCKET_IS_FLUSH(e)) {
87                 flush = e;
88                 more = apr_brigade_split(b, APR_BUCKET_NEXT(e));
89                 break;
90             }
91             else if (e->length == (apr_size_t)-1) {
92                 /* unknown amount of data (e.g. a pipe) */
93                 const char *data;
94                 apr_size_t len;
95
96                 rv = apr_bucket_read(e, &data, &len, APR_BLOCK_READ);
97                 if (rv != APR_SUCCESS) {
98                     return rv;
99                 }
100                 if (len > 0) {
101                     /*
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.
105                      */
106                     bytes += len;
107                     more = apr_brigade_split(b, APR_BUCKET_NEXT(e));
108                     break;
109                 }
110                 else {
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.
114                      */
115                     continue;
116                 }
117             }
118             else {
119                 bytes += e->length;
120             }
121         }
122
123         /*
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.
127          */
128         /* if there are content bytes, then wrap them in a chunk */
129         if (bytes > 0) {
130             apr_size_t hdr_len;
131             /*
132              * Insert the chunk header, specifying the number of bytes in
133              * the chunk.
134              */
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,
139                                             c->bucket_alloc);
140             APR_BRIGADE_INSERT_HEAD(b, e);
141
142             /*
143              * Insert the end-of-chunk CRLF before an EOS or
144              * FLUSH bucket, or appended to the brigade
145              */
146             e = apr_bucket_immortal_create(ASCII_CRLF, 2, c->bucket_alloc);
147             if (eos != NULL) {
148                 APR_BUCKET_INSERT_BEFORE(eos, e);
149             }
150             else if (flush != NULL) {
151                 APR_BUCKET_INSERT_BEFORE(flush, e);
152             }
153             else {
154                 APR_BRIGADE_INSERT_TAIL(b, e);
155             }
156         }
157
158         /* RFC 2616, Section 3.6.1
159          *
160          * If there is an EOS bucket, then prefix it with:
161          *   1) the last-chunk marker ("0" CRLF)
162          *   2) the trailer
163          *   3) the end-of-chunked body CRLF
164          *
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.
173          *
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
176          * now.
177          */
178         if (eos && !f->ctx) {
179             /* XXX: (2) trailers ... does not yet exist */
180             e = apr_bucket_immortal_create(ASCII_ZERO ASCII_CRLF
181                                            /* <trailers> */
182                                            ASCII_CRLF, 5, c->bucket_alloc);
183             APR_BUCKET_INSERT_BEFORE(eos, e);
184         }
185
186         /* pass the brigade to the next filter. */
187         rv = ap_pass_brigade(f->next, b);
188         if (rv != APR_SUCCESS || eos != NULL) {
189             return rv;
190         }
191     }
192     return APR_SUCCESS;
193 }