varnish-cache/bin/varnishd/http1/cache_http1_vfp.c
0
/*-
1
 * Copyright (c) 2006 Verdens Gang AS
2
 * Copyright (c) 2006-2015 Varnish Software AS
3
 * All rights reserved.
4
 *
5
 * Author: Poul-Henning Kamp <phk@phk.freebsd.dk>
6
 *
7
 * SPDX-License-Identifier: BSD-2-Clause
8
 *
9
 * Redistribution and use in source and binary forms, with or without
10
 * modification, are permitted provided that the following conditions
11
 * are met:
12
 * 1. Redistributions of source code must retain the above copyright
13
 *    notice, this list of conditions and the following disclaimer.
14
 * 2. Redistributions in binary form must reproduce the above copyright
15
 *    notice, this list of conditions and the following disclaimer in the
16
 *    documentation and/or other materials provided with the distribution.
17
 *
18
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21
 * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
22
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28
 * SUCH DAMAGE.
29
 *
30
 * HTTP1 Fetch Filters
31
 *
32
 * These filters are used for both req.body and beresp.body to handle
33
 * the HTTP/1 aspects (C-L/Chunked/EOF)
34
 *
35
 */
36
37
#include "config.h"
38
39
#include <inttypes.h>
40
41
#include "cache/cache_varnishd.h"
42
#include "cache/cache_filter.h"
43
#include "cache_http1.h"
44
45
#include "vct.h"
46
#include "vtcp.h"
47
48
/*--------------------------------------------------------------------
49
 * Read up to len bytes, returning pipelined data first.
50
 */
51
52
static ssize_t
53 760409
v1f_read(const struct vfp_ctx *vc, struct http_conn *htc, void *d, ssize_t len)
54
{
55
        ssize_t l;
56
        unsigned char *p;
57 760409
        ssize_t i = 0;
58
59 760409
        CHECK_OBJ_NOTNULL(vc, VFP_CTX_MAGIC);
60 760409
        CHECK_OBJ_NOTNULL(htc, HTTP_CONN_MAGIC);
61 760409
        assert(len > 0);
62 760409
        l = 0;
63 760409
        p = d;
64 760409
        if (htc->pipeline_b) {
65 38664
                l = htc->pipeline_e - htc->pipeline_b;
66 38664
                assert(l > 0);
67 38664
                l = vmin(l, len);
68 38664
                memcpy(p, htc->pipeline_b, l);
69 38664
                p += l;
70 38664
                len -= l;
71 38664
                htc->pipeline_b += l;
72 38664
                if (htc->pipeline_b == htc->pipeline_e)
73 13918
                        htc->pipeline_b = htc->pipeline_e = NULL;
74 38664
        }
75 760409
        if (len > 0) {
76 722695
                i = read(*htc->rfd, p, len);
77 722695
                if (i < 0) {
78 117
                        VTCP_Assert(i);
79 234
                        VSLbs(vc->wrk->vsl, SLT_FetchError,
80 117
                            TOSTRAND(VAS_errtxt(errno)));
81 117
                        return (i);
82
                }
83 722578
                assert(i <= len);
84 722578
                if (i == 0)
85 767
                        htc->doclose = SC_RESP_CLOSE;
86 722578
        }
87 760292
        assert(i >= 0);
88 760292
        assert(l >= 0);
89 760292
        assert(i < SSIZE_MAX / 2);
90 760292
        assert(l < SSIZE_MAX / 2);
91 760292
        return (i + l);
92 760409
}
93
94
95
/*--------------------------------------------------------------------
96
 * read (CR)?LF at the end of a chunk
97
 */
98
static enum vfp_status
99 5021
v1f_chunk_end(struct vfp_ctx *vc, struct http_conn *htc)
100
{
101
        char c;
102
103 5021
        if (v1f_read(vc, htc, &c, 1) <= 0)
104 0
                return (VFP_Error(vc, "chunked read err"));
105 5021
        if (c == '\r' && v1f_read(vc, htc, &c, 1) <= 0)
106 0
                return (VFP_Error(vc, "chunked read err"));
107 5021
        if (c != '\n')
108 39
                return (VFP_Error(vc, "chunked tail no NL"));
109 4982
        return (VFP_OK);
110 5021
}
111
112
113
/*--------------------------------------------------------------------
114
 * Parse a chunk header and, for VFP_OK, return size in a pointer
115
 *
116
 * XXX: Reading one byte at a time is pretty pessimal.
117
 */
118
119
static enum vfp_status
120 5162
v1f_chunked_hdr(struct vfp_ctx *vc, struct http_conn *htc, ssize_t *szp)
121
{
122
        char buf[20];           /* XXX: 20 is arbitrary */
123
        unsigned u;
124
        uintmax_t cll;
125
        ssize_t cl, lr;
126
        char *q;
127
128 5162
        CHECK_OBJ_NOTNULL(vc, VFP_CTX_MAGIC);
129 5162
        CHECK_OBJ_NOTNULL(htc, HTTP_CONN_MAGIC);
130 5162
        AN(szp);
131 5162
        assert(*szp == -1);
132
133
        /* Skip leading whitespace */
134 5162
        do {
135 5163
                lr = v1f_read(vc, htc, buf, 1);
136 5163
                if (lr <= 0)
137 65
                        return (VFP_Error(vc, "chunked read err"));
138 5098
        } while (vct_isows(buf[0]));
139
140 5099
        if (!vct_ishex(buf[0]))
141 26
                return (VFP_Error(vc, "chunked header non-hex"));
142
143
        /* Collect hex digits, skipping leading zeros */
144 9077
        for (u = 1; u < sizeof buf; u++) {
145 9063
                do {
146 17774
                        lr = v1f_read(vc, htc, buf + u, 1);
147 17774
                        if (lr <= 0)
148 0
                                return (VFP_Error(vc, "chunked read err"));
149 17774
                } while (u == 1 && buf[0] == '0' && buf[u] == '0');
150 9063
                if (!vct_ishex(buf[u]))
151 5059
                        break;
152 4004
        }
153
154 5073
        if (u >= sizeof buf)
155 13
                return (VFP_Error(vc, "chunked header too long"));
156
157
        /* Skip trailing white space */
158 5060
        while (vct_isows(buf[u])) {
159 0
                lr = v1f_read(vc, htc, buf + u, 1);
160 0
                if (lr <= 0)
161 0
                        return (VFP_Error(vc, "chunked read err"));
162
        }
163
164 5060
        if (buf[u] == '\r' && v1f_read(vc, htc, buf + u, 1) <= 0)
165 0
                return (VFP_Error(vc, "chunked read err"));
166 5060
        if (buf[u] != '\n')
167 0
                return (VFP_Error(vc, "chunked header no NL"));
168
169 5058
        buf[u] = '\0';
170
171 5058
        cll = strtoumax(buf, &q, 16);
172 5058
        if (q == NULL || *q != '\0')
173 4
                return (VFP_Error(vc, "chunked header number syntax"));
174 5054
        cl = (ssize_t)cll;
175 5054
        if (cl < 0 || (uintmax_t)cl != cll)
176 15
                return (VFP_Error(vc, "bogusly large chunk size"));
177
178 5047
        *szp = cl;
179 5047
        return (VFP_OK);
180 5164
}
181
182
183
/*--------------------------------------------------------------------
184
 * Read a chunked HTTP object.
185
 *
186
 */
187
188
static enum vfp_status v_matchproto_(vfp_pull_f)
189 692547
v1f_chunked_pull(struct vfp_ctx *vc, struct vfp_entry *vfe, void *ptr,
190
    ssize_t *lp)
191
{
192
        static enum vfp_status vfps;
193
        struct http_conn *htc;
194
        ssize_t l, lr;
195
196 692547
        CHECK_OBJ_NOTNULL(vc, VFP_CTX_MAGIC);
197 692547
        CHECK_OBJ_NOTNULL(vfe, VFP_ENTRY_MAGIC);
198 692547
        CAST_OBJ_NOTNULL(htc, vfe->priv1, HTTP_CONN_MAGIC);
199 692547
        AN(ptr);
200 692547
        AN(lp);
201
202 692547
        l = *lp;
203 692547
        *lp = 0;
204 692547
        if (vfe->priv2 == -1) {
205 5162
                vfps = v1f_chunked_hdr(vc, htc, &vfe->priv2);
206 5162
                if (vfps != VFP_OK)
207 117
                        return (vfps);
208 5045
        }
209 692430
        if (vfe->priv2 > 0) {
210 690867
                if (vfe->priv2 < l)
211 3217
                        l = vfe->priv2;
212 690867
                lr = v1f_read(vc, htc, ptr, l);
213 690867
                if (lr <= 0)
214 13
                        return (VFP_Error(vc, "chunked insufficient bytes"));
215 690854
                *lp = lr;
216 690854
                vfe->priv2 -= lr;
217 690854
                if (vfe->priv2 != 0)
218 687396
                        return (VFP_OK);
219
220 3458
                vfe->priv2 = -1;
221 3458
                return (v1f_chunk_end(vc, htc));
222
        }
223 1563
        AZ(vfe->priv2);
224 1563
        vfps = v1f_chunk_end(vc, htc);
225 1563
        return (vfps == VFP_OK ? VFP_END : vfps);
226 692547
}
227
228
static const struct vfp v1f_chunked = {
229
        .name = "V1F_CHUNKED",
230
        .pull = v1f_chunked_pull,
231
};
232
233
234
/*--------------------------------------------------------------------*/
235
236
static enum vfp_status v_matchproto_(vfp_pull_f)
237 31204
v1f_straight_pull(struct vfp_ctx *vc, struct vfp_entry *vfe, void *p,
238
    ssize_t *lp)
239
{
240
        ssize_t l, lr;
241
        struct http_conn *htc;
242
243 31204
        CHECK_OBJ_NOTNULL(vc, VFP_CTX_MAGIC);
244 31204
        CHECK_OBJ_NOTNULL(vfe, VFP_ENTRY_MAGIC);
245 31204
        CAST_OBJ_NOTNULL(htc, vfe->priv1, HTTP_CONN_MAGIC);
246 31204
        AN(p);
247 31204
        AN(lp);
248
249 31204
        l = *lp;
250 31204
        *lp = 0;
251
252 31204
        if (vfe->priv2 == 0) // XXX: Optimize Content-Len: 0 out earlier
253 0
                return (VFP_END);
254 31204
        l = vmin(l, vfe->priv2);
255 31204
        lr = v1f_read(vc, htc, p, l);
256 31204
        if (lr <= 0)
257 299
                return (VFP_Error(vc, "straight insufficient bytes"));
258 30905
        *lp = lr;
259 30905
        vfe->priv2 -= lr;
260 30905
        if (vfe->priv2 == 0)
261 13060
                return (VFP_END);
262 17845
        return (VFP_OK);
263 31204
}
264
265
static const struct vfp v1f_straight = {
266
        .name = "V1F_STRAIGHT",
267
        .pull = v1f_straight_pull,
268
};
269
270
/*--------------------------------------------------------------------*/
271
272
static enum vfp_status v_matchproto_(vfp_pull_f)
273 897
v1f_eof_pull(struct vfp_ctx *vc, struct vfp_entry *vfe, void *p, ssize_t *lp)
274
{
275
        ssize_t l, lr;
276
        struct http_conn *htc;
277
278 897
        CHECK_OBJ_NOTNULL(vc, VFP_CTX_MAGIC);
279 897
        CHECK_OBJ_NOTNULL(vfe, VFP_ENTRY_MAGIC);
280 897
        CAST_OBJ_NOTNULL(htc, vfe->priv1, HTTP_CONN_MAGIC);
281 897
        AN(p);
282
283 897
        AN(lp);
284
285 897
        l = *lp;
286 897
        *lp = 0;
287 897
        lr = v1f_read(vc, htc, p, l);
288 897
        if (lr < 0)
289 39
                return (VFP_Error(vc, "eof socket fail"));
290 858
        if (lr == 0)
291 260
                return (VFP_END);
292 598
        *lp = lr;
293 598
        return (VFP_OK);
294 897
}
295
296
static const struct vfp v1f_eof = {
297
        .name = "V1F_EOF",
298
        .pull = v1f_eof_pull,
299
};
300
301
/*--------------------------------------------------------------------
302
 */
303
304
int
305 17158
V1F_Setup_Fetch(struct vfp_ctx *vfc, struct http_conn *htc)
306
{
307
        struct vfp_entry *vfe;
308
309 17158
        CHECK_OBJ_NOTNULL(vfc, VFP_CTX_MAGIC);
310 17158
        CHECK_OBJ_NOTNULL(htc, HTTP_CONN_MAGIC);
311
312 17158
        if (htc->body_status == BS_EOF) {
313 299
                assert(htc->content_length == -1);
314 299
                vfe = VFP_Push(vfc, &v1f_eof);
315 299
                if (vfe == NULL)
316 0
                        return (ENOSPC);
317 299
                vfe->priv2 = 0;
318 17158
        } else if (htc->body_status == BS_LENGTH) {
319 14779
                assert(htc->content_length > 0);
320 14779
                vfe = VFP_Push(vfc, &v1f_straight);
321 14779
                if (vfe == NULL)
322 0
                        return (ENOSPC);
323 14779
                vfe->priv2 = htc->content_length;
324 16859
        } else if (htc->body_status == BS_CHUNKED) {
325 2080
                assert(htc->content_length == -1);
326 2080
                vfe = VFP_Push(vfc, &v1f_chunked);
327 2080
                if (vfe == NULL)
328 208
                        return (ENOSPC);
329 1872
                vfe->priv2 = -1;
330 1872
        } else {
331 0
                WRONG("Wrong body_status");
332
        }
333 16950
        vfe->priv1 = htc;
334 16950
        return (0);
335 17158
}