| | 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 |
58265 |
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 |
58265 |
ssize_t i = 0; |
58 |
|
|
59 |
58265 |
CHECK_OBJ_NOTNULL(vc, VFP_CTX_MAGIC); |
60 |
58265 |
CHECK_OBJ_NOTNULL(htc, HTTP_CONN_MAGIC); |
61 |
58265 |
assert(len > 0); |
62 |
58265 |
l = 0; |
63 |
58265 |
p = d; |
64 |
58265 |
if (htc->pipeline_b) { |
65 |
2936 |
l = htc->pipeline_e - htc->pipeline_b; |
66 |
2936 |
assert(l > 0); |
67 |
2936 |
l = vmin(l, len); |
68 |
2936 |
memcpy(p, htc->pipeline_b, l); |
69 |
2936 |
p += l; |
70 |
2936 |
len -= l; |
71 |
2936 |
htc->pipeline_b += l; |
72 |
2936 |
if (htc->pipeline_b == htc->pipeline_e) |
73 |
1045 |
htc->pipeline_b = htc->pipeline_e = NULL; |
74 |
2936 |
} |
75 |
58265 |
if (len > 0) { |
76 |
55401 |
i = read(*htc->rfd, p, len); |
77 |
55401 |
if (i < 0) { |
78 |
9 |
VTCP_Assert(i); |
79 |
18 |
VSLbs(vc->wrk->vsl, SLT_FetchError, |
80 |
9 |
TOSTRAND(VAS_errtxt(errno))); |
81 |
9 |
return (i); |
82 |
|
} |
83 |
55392 |
assert(i <= len); |
84 |
55392 |
if (i == 0) |
85 |
59 |
htc->doclose = SC_RESP_CLOSE; |
86 |
55392 |
} |
87 |
58256 |
assert(i >= 0); |
88 |
58256 |
assert(l >= 0); |
89 |
58256 |
assert(i < SSIZE_MAX / 2); |
90 |
58256 |
assert(l < SSIZE_MAX / 2); |
91 |
58256 |
return (i + l); |
92 |
58265 |
} |
93 |
|
|
94 |
|
|
95 |
|
/*-------------------------------------------------------------------- |
96 |
|
* read (CR)?LF at the end of a chunk |
97 |
|
*/ |
98 |
|
static enum vfp_status |
99 |
379 |
v1f_chunk_end(struct vfp_ctx *vc, struct http_conn *htc) |
100 |
|
{ |
101 |
|
char c; |
102 |
|
|
103 |
379 |
if (v1f_read(vc, htc, &c, 1) <= 0) |
104 |
0 |
return (VFP_Error(vc, "chunked read err")); |
105 |
379 |
if (c == '\r' && v1f_read(vc, htc, &c, 1) <= 0) |
106 |
0 |
return (VFP_Error(vc, "chunked read err")); |
107 |
379 |
if (c != '\n') |
108 |
2 |
return (VFP_Error(vc, "chunked tail no NL")); |
109 |
377 |
return (VFP_OK); |
110 |
379 |
} |
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 |
390 |
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 |
390 |
CHECK_OBJ_NOTNULL(vc, VFP_CTX_MAGIC); |
129 |
390 |
CHECK_OBJ_NOTNULL(htc, HTTP_CONN_MAGIC); |
130 |
390 |
AN(szp); |
131 |
390 |
assert(*szp == -1); |
132 |
|
|
133 |
|
/* Skip leading whitespace */ |
134 |
390 |
do { |
135 |
390 |
lr = v1f_read(vc, htc, buf, 1); |
136 |
390 |
if (lr <= 0) |
137 |
5 |
return (VFP_Error(vc, "chunked read err")); |
138 |
385 |
} while (vct_isows(buf[0])); |
139 |
|
|
140 |
385 |
if (!vct_ishex(buf[0])) |
141 |
2 |
return (VFP_Error(vc, "chunked header non-hex")); |
142 |
|
|
143 |
|
/* Collect hex digits, skipping leading zeros */ |
144 |
691 |
for (u = 1; u < sizeof buf; u++) { |
145 |
690 |
do { |
146 |
1360 |
lr = v1f_read(vc, htc, buf + u, 1); |
147 |
1360 |
if (lr <= 0) |
148 |
0 |
return (VFP_Error(vc, "chunked read err")); |
149 |
1360 |
} while (u == 1 && buf[0] == '0' && buf[u] == '0'); |
150 |
690 |
if (!vct_ishex(buf[u])) |
151 |
382 |
break; |
152 |
308 |
} |
153 |
|
|
154 |
383 |
if (u >= sizeof buf) |
155 |
1 |
return (VFP_Error(vc, "chunked header too long")); |
156 |
|
|
157 |
|
/* Skip trailing white space */ |
158 |
382 |
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 |
382 |
if (buf[u] == '\r' && v1f_read(vc, htc, buf + u, 1) <= 0) |
165 |
0 |
return (VFP_Error(vc, "chunked read err")); |
166 |
382 |
if (buf[u] != '\n') |
167 |
0 |
return (VFP_Error(vc, "chunked header no NL")); |
168 |
|
|
169 |
382 |
buf[u] = '\0'; |
170 |
|
|
171 |
382 |
cll = strtoumax(buf, &q, 16); |
172 |
382 |
if (q == NULL || *q != '\0') |
173 |
0 |
return (VFP_Error(vc, "chunked header number syntax")); |
174 |
382 |
cl = (ssize_t)cll; |
175 |
382 |
if (cl < 0 || (uintmax_t)cl != cll) |
176 |
1 |
return (VFP_Error(vc, "bogusly large chunk size")); |
177 |
|
|
178 |
381 |
*szp = cl; |
179 |
381 |
return (VFP_OK); |
180 |
390 |
} |
181 |
|
|
182 |
|
|
183 |
|
/*-------------------------------------------------------------------- |
184 |
|
* Read a chunked HTTP object. |
185 |
|
* |
186 |
|
*/ |
187 |
|
|
188 |
|
static enum vfp_status v_matchproto_(vfp_pull_f) |
189 |
53127 |
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 |
53127 |
CHECK_OBJ_NOTNULL(vc, VFP_CTX_MAGIC); |
197 |
53127 |
CHECK_OBJ_NOTNULL(vfe, VFP_ENTRY_MAGIC); |
198 |
53127 |
CAST_OBJ_NOTNULL(htc, vfe->priv1, HTTP_CONN_MAGIC); |
199 |
53127 |
AN(ptr); |
200 |
53127 |
AN(lp); |
201 |
|
|
202 |
53127 |
l = *lp; |
203 |
53127 |
*lp = 0; |
204 |
53127 |
if (vfe->priv2 == -1) { |
205 |
390 |
vfps = v1f_chunked_hdr(vc, htc, &vfe->priv2); |
206 |
390 |
if (vfps != VFP_OK) |
207 |
9 |
return (vfps); |
208 |
381 |
} |
209 |
53118 |
if (vfe->priv2 > 0) { |
210 |
53000 |
if (vfe->priv2 < l) |
211 |
242 |
l = vfe->priv2; |
212 |
53000 |
lr = v1f_read(vc, htc, ptr, l); |
213 |
53000 |
if (lr <= 0) |
214 |
1 |
return (VFP_Error(vc, "chunked insufficient bytes")); |
215 |
52999 |
*lp = lr; |
216 |
52999 |
vfe->priv2 -= lr; |
217 |
52999 |
if (vfe->priv2 != 0) |
218 |
52738 |
return (VFP_OK); |
219 |
|
|
220 |
261 |
vfe->priv2 = -1; |
221 |
261 |
return (v1f_chunk_end(vc, htc)); |
222 |
|
} |
223 |
118 |
AZ(vfe->priv2); |
224 |
118 |
vfps = v1f_chunk_end(vc, htc); |
225 |
118 |
return (vfps == VFP_OK ? VFP_END : vfps); |
226 |
53127 |
} |
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 |
2351 |
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 |
2351 |
CHECK_OBJ_NOTNULL(vc, VFP_CTX_MAGIC); |
244 |
2351 |
CHECK_OBJ_NOTNULL(vfe, VFP_ENTRY_MAGIC); |
245 |
2351 |
CAST_OBJ_NOTNULL(htc, vfe->priv1, HTTP_CONN_MAGIC); |
246 |
2351 |
AN(p); |
247 |
2351 |
AN(lp); |
248 |
|
|
249 |
2351 |
l = *lp; |
250 |
2351 |
*lp = 0; |
251 |
|
|
252 |
2351 |
if (vfe->priv2 == 0) // XXX: Optimize Content-Len: 0 out earlier |
253 |
0 |
return (VFP_END); |
254 |
2351 |
l = vmin(l, vfe->priv2); |
255 |
2351 |
lr = v1f_read(vc, htc, p, l); |
256 |
2351 |
if (lr <= 0) |
257 |
23 |
return (VFP_Error(vc, "straight insufficient bytes")); |
258 |
2328 |
*lp = lr; |
259 |
2328 |
vfe->priv2 -= lr; |
260 |
2328 |
if (vfe->priv2 == 0) |
261 |
981 |
return (VFP_END); |
262 |
1347 |
return (VFP_OK); |
263 |
2351 |
} |
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 |
69 |
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 |
69 |
CHECK_OBJ_NOTNULL(vc, VFP_CTX_MAGIC); |
279 |
69 |
CHECK_OBJ_NOTNULL(vfe, VFP_ENTRY_MAGIC); |
280 |
69 |
CAST_OBJ_NOTNULL(htc, vfe->priv1, HTTP_CONN_MAGIC); |
281 |
69 |
AN(p); |
282 |
|
|
283 |
69 |
AN(lp); |
284 |
|
|
285 |
69 |
l = *lp; |
286 |
69 |
*lp = 0; |
287 |
69 |
lr = v1f_read(vc, htc, p, l); |
288 |
69 |
if (lr < 0) |
289 |
3 |
return (VFP_Error(vc, "eof socket fail")); |
290 |
66 |
if (lr == 0) |
291 |
20 |
return (VFP_END); |
292 |
46 |
*lp = lr; |
293 |
46 |
return (VFP_OK); |
294 |
69 |
} |
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 |
1293 |
V1F_Setup_Fetch(struct vfp_ctx *vfc, struct http_conn *htc) |
306 |
|
{ |
307 |
|
struct vfp_entry *vfe; |
308 |
|
|
309 |
1293 |
CHECK_OBJ_NOTNULL(vfc, VFP_CTX_MAGIC); |
310 |
1293 |
CHECK_OBJ_NOTNULL(htc, HTTP_CONN_MAGIC); |
311 |
|
|
312 |
1293 |
if (htc->body_status == BS_EOF) { |
313 |
23 |
assert(htc->content_length == -1); |
314 |
23 |
vfe = VFP_Push(vfc, &v1f_eof); |
315 |
23 |
if (vfe == NULL) |
316 |
0 |
return (ENOSPC); |
317 |
23 |
vfe->priv2 = 0; |
318 |
1293 |
} else if (htc->body_status == BS_LENGTH) { |
319 |
1113 |
assert(htc->content_length > 0); |
320 |
1113 |
vfe = VFP_Push(vfc, &v1f_straight); |
321 |
1113 |
if (vfe == NULL) |
322 |
0 |
return (ENOSPC); |
323 |
1113 |
vfe->priv2 = htc->content_length; |
324 |
1270 |
} else if (htc->body_status == BS_CHUNKED) { |
325 |
157 |
assert(htc->content_length == -1); |
326 |
157 |
vfe = VFP_Push(vfc, &v1f_chunked); |
327 |
157 |
if (vfe == NULL) |
328 |
16 |
return (ENOSPC); |
329 |
141 |
vfe->priv2 = -1; |
330 |
141 |
} else { |
331 |
0 |
WRONG("Wrong body_status"); |
332 |
|
} |
333 |
1277 |
vfe->priv1 = htc; |
334 |
1277 |
return (0); |
335 |
1293 |
} |