Code Coverage: vinyl-cache/bin/vinyld/cache/cache_req_body.c
 
82217aa4fc7094b3610340078afaf78cd95a8ec9
 
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
 */
31
32
#include "config.h"
33
34
#include <stdlib.h>
35
36
#include "cache_int.h"
37
#include "cache_filter.h"
38
#include "cache_objhead.h"
39
#include "cache_transport.h"
40
41
#include "vtim.h"
42
#include "storage/storage.h"
43
44
/*----------------------------------------------------------------------
45
 * Pull the req.body in via/into a objcore
46
 *
47
 * This can be called only once per request
48
 *
49
 */
50
51
static ssize_t
52
vrb_pull(struct req *req, ssize_t maxsize, objiterate_f *func, void *priv)
53 257
{
54 257
        ssize_t l, r = 0, yet;
55 257
        struct vrt_ctx ctx[1];
56 257
        struct vfp_ctx *vfc;
57 257
        uint8_t *ptr;
58 257
        enum vfp_status vfps = VFP_ERROR;
59 257
        const struct stevedore *stv;
60 257
        ssize_t req_bodybytes = 0;
61 257
        unsigned flush = OBJ_ITER_FLUSH;
62
63 257
        CHECK_OBJ_NOTNULL(req, REQ_MAGIC);
64
65 257
        CHECK_OBJ_NOTNULL(req->htc, HTTP_CONN_MAGIC);
66 257
        CHECK_OBJ_NOTNULL(req->vfc, VFP_CTX_MAGIC);
67 257
        vfc = req->vfc;
68
69 257
        req->body_oc = HSH_Private(req->wrk);
70 257
        AN(req->body_oc);
71
72 257
        if (req->storage != NULL)
73 6
                stv = req->storage;
74 251
        else
75 251
                stv = stv_transient;
76
77 257
        req->storage = NULL;
78
79 257
        if (STV_NewObject(req->wrk, req->body_oc, stv, 0) == 0) {
80 2
                req->req_body_status = BS_ERROR;
81 2
                HSH_DerefBoc(req->wrk, req->body_oc);
82 2
                AZ(HSH_DerefObjCore(req->wrk, &req->body_oc));
83 2
                (void)VFP_Error(vfc, "Object allocation failed:"
84 2
                    " Ran out of space in %s", stv->vclname);
85 2
                return (-1);
86 2
        }
87
88 255
        vfc->oc = req->body_oc;
89
90 255
        INIT_OBJ(ctx, VRT_CTX_MAGIC);
91 255
        VCL_Req2Ctx(ctx, req);
92
93 255
        if (req->vfp_filter_list != NULL &&
94 255
            VCL_StackVFP(vfc, req->vcl, req->vfp_filter_list)) {
95 0
                (void)VFP_Error(vfc, "req.body filters failed");
96 0
                req->req_body_status = BS_ERROR;
97 0
                HSH_DerefBoc(req->wrk, req->body_oc);
98 0
                AZ(HSH_DerefObjCore(req->wrk, &req->body_oc));
99 0
                return (-1);
100 0
        }
101
102
103 255
        if (VFP_Open(ctx, vfc) < 0) {
104 0
                req->req_body_status = BS_ERROR;
105 0
                HSH_DerefBoc(req->wrk, req->body_oc);
106 0
                AZ(HSH_DerefObjCore(req->wrk, &req->body_oc));
107 0
                return (-1);
108 0
        }
109
110 255
        AN(req->htc);
111 255
        yet = req->htc->content_length;
112 255
        if (yet != 0 && req->want100cont) {
113 3
                req->want100cont = 0;
114 3
                (void)req->transport->minimal_response(req, 100);
115 3
        }
116 255
        yet = vmax_t(ssize_t, yet, 0);
117 433
        do {
118 433
                AZ(vfc->failed);
119 433
                if (maxsize >= 0 && req_bodybytes > maxsize) {
120 1
                        (void)VFP_Error(vfc, "Request body too big to cache");
121 1
                        break;
122 1
                }
123
                /* NB: only attempt a full allocation when caching. */
124 432
                l = maxsize > 0 ? yet : 0;
125 432
                if (VFP_GetStorage(vfc, &l, &ptr) != VFP_OK)
126 0
                        break;
127 432
                AZ(vfc->failed);
128 432
                AN(ptr);
129 432
                AN(l);
130 432
                vfps = VFP_Suck(vfc, ptr, &l);
131 432
                if (l > 0 && vfps != VFP_ERROR) {
132 395
                        req_bodybytes += l;
133 395
                        if (yet >= l)
134 169
                                yet -= l;
135 226
                        else if (yet > 0)
136 0
                                yet = 0;
137 395
                        if (func != NULL) {
138 334
                                if (vfps == VFP_END)
139 192
                                        flush |= OBJ_ITER_END;
140 334
                                r = func(priv, flush, ptr, l);
141
                                // ending delivery early is not an error,
142
                                // continue sucking
143 334
                                if (r > 0)
144 3
                                        func = NULL;
145 331
                                else if (r)
146 0
                                        break;
147 334
                        } else {
148 61
                                ObjExtend(req->wrk, req->body_oc, l,
149 61
                                    vfps == VFP_END ? 1 : 0);
150 61
                        }
151 395
                }
152
153 432
        } while (vfps == VFP_OK);
154 255
        req->acct.req_bodybytes += VFP_Close(vfc);
155 255
        VSLb_ts_req(req, "ReqBody", VTIM_real());
156 255
        if (func != NULL || r > 0) {
157 222
                HSH_DerefBoc(req->wrk, req->body_oc);
158 222
                AZ(HSH_DerefObjCore(req->wrk, &req->body_oc));
159 222
                if (r > 0)
160 3
                        return (r);
161 219
                AN(func);
162 219
                if (vfps == VFP_END && r == 0 && (flush & OBJ_ITER_END) == 0)
163 8
                        r = func(priv, flush | OBJ_ITER_END, NULL, 0);
164 219
                if (vfps != VFP_END) {
165 20
                        req->req_body_status = BS_ERROR;
166 20
                        if (r == 0)
167 20
                                r = -1;
168 20
                }
169 219
                return (r);
170 219
        }
171
172 33
        AZ(ObjSetU64(req->wrk, req->body_oc, OA_LEN, req_bodybytes));
173 33
        HSH_DerefBoc(req->wrk, req->body_oc);
174
175 33
        if (vfps != VFP_END) {
176 3
                req->req_body_status = BS_ERROR;
177 3
                AZ(HSH_DerefObjCore(req->wrk, &req->body_oc));
178 3
                return (-1);
179 3
        }
180
181 30
        assert(req_bodybytes >= 0);
182 30
        if (req_bodybytes != req->htc->content_length) {
183
                // XXX wrong for rollback
184
                /* We must update also the "pristine" req.* copy */
185 8
                http_Unset(req->http0, H_Content_Length);
186 8
                http_Unset(req->http0, H_Transfer_Encoding);
187 8
                http_PrintfHeader(req->http0, "Content-Length: %ju",
188 8
                    (uintmax_t)req_bodybytes);
189
190 8
                http_Unset(req->http, H_Content_Length);
191 8
                http_Unset(req->http, H_Transfer_Encoding);
192 8
                http_PrintfHeader(req->http, "Content-Length: %ju",
193 8
                    (uintmax_t)req_bodybytes);
194 8
        }
195
196 30
        req->req_body_status = BS_CACHED;
197 30
        return (req_bodybytes);
198 30
}
199
200
/*----------------------------------------------------------------------
201
 * Iterate over the req.body.
202
 *
203
 * This can be done exactly once if uncached, and multiple times if the
204
 * req.body is cached.
205
 *
206
 * return length or -1 on error
207
 */
208
209
ssize_t
210
VRB_Iterate(struct worker *wrk, struct vsl_log *vsl,
211
    struct req *req, objiterate_f *func, void *priv)
212 253
{
213 253
        int i;
214
215 253
        CHECK_OBJ_NOTNULL(req, REQ_MAGIC);
216 253
        AN(func);
217
218 253
        if (req->req_body_status == BS_CACHED) {
219 31
                AN(req->body_oc);
220 31
                if (ObjIterate(wrk, req->body_oc, priv, func, 0))
221 0
                        return (-1);
222 31
                return (0);
223 31
        }
224 222
        if (req->req_body_status == BS_NONE)
225 0
                return (0);
226 222
        if (req->req_body_status == BS_TAKEN) {
227 0
                VSLb(vsl, SLT_VCL_Error,
228 0
                    "Uncached req.body can only be consumed once.");
229 0
                return (-1);
230 0
        }
231 222
        if (req->req_body_status == BS_ERROR) {
232 0
                VSLb(vsl, SLT_FetchError,
233 0
                    "Had failed reading req.body before.");
234 0
                return (-1);
235 0
        }
236 222
        Lck_Lock(&req->sp->mtx);
237 222
        if (req->req_body_status->avail > 0) {
238 222
                req->req_body_status = BS_TAKEN;
239 222
                i = 0;
240 222
        } else
241 0
                i = -1;
242 222
        Lck_Unlock(&req->sp->mtx);
243 222
        if (i) {
244 0
                VSLb(vsl, SLT_VCL_Error,
245 0
                    "Multiple attempts to access non-cached req.body");
246 0
                return (i);
247 0
        }
248 222
        return (vrb_pull(req, -1, func, priv));
249 222
}
250
251
/*----------------------------------------------------------------------
252
 * VRB_Ignore() is a dedicated function, because we might
253
 * be able to dissuade or terminate its transmission in some protocols.
254
 *
255
 * For HTTP1, we do nothing if we are going to close the connection anyway or
256
 * just iterate it into oblivion.
257
 */
258
259
static int v_matchproto_(objiterate_f)
260
httpq_req_body_discard(void *priv, unsigned flush, const void *ptr, ssize_t len)
261 81
{
262
263 81
        (void)priv;
264 81
        (void)flush;
265 81
        (void)ptr;
266 81
        (void)len;
267 81
        return (0);
268 81
}
269
270
int
271
VRB_Ignore(struct req *req)
272 8762
{
273
274 8762
        CHECK_OBJ_NOTNULL(req, REQ_MAGIC);
275
276 8762
        if (req->doclose != SC_NULL)
277 217
                return (0);
278 8545
        if (req->req_body_status->avail > 0)
279 59
                (void)VRB_Iterate(req->wrk, req->vsl, req,
280 59
                    httpq_req_body_discard, NULL);
281 8545
        if (req->req_body_status == BS_ERROR)
282 5
                req->doclose = SC_RX_BODY;
283 8545
        return (0);
284 8762
}
285
286
/*----------------------------------------------------------------------
287
 */
288
289
void
290
VRB_Free(struct req *req)
291 8687
{
292 8687
        int r;
293
294 8687
        CHECK_OBJ_NOTNULL(req, REQ_MAGIC);
295
296 8687
        if (req->body_oc == NULL)
297 8657
                return;
298
299 30
        r = HSH_DerefObjCore(req->wrk, &req->body_oc);
300
301
        // each busyobj may have gained a reference
302 30
        assert (r >= 0);
303 30
        assert ((unsigned)r <= req->restarts + 1);
304 30
}
305
306
/*----------------------------------------------------------------------
307
 * Cache the req.body if it is smaller than the given size
308
 *
309
 * This function must be called before any backend fetches are kicked
310
 * off to prevent parallelism.
311
 */
312
313
ssize_t
314
VRB_Cache(struct req *req, ssize_t maxsize)
315 42
{
316 42
        uint64_t u;
317
318 42
        CHECK_OBJ_NOTNULL(req, REQ_MAGIC);
319 42
        assert (req->req_step == R_STP_RECV);
320 42
        assert(maxsize >= 0);
321
322
        /*
323
         * We only allow caching to happen the first time through vcl_recv{}
324
         * where we know we will have no competition or conflicts for the
325
         * updates to req.http.* etc.
326
         */
327 42
        if (req->restarts > 0 && req->req_body_status != BS_CACHED) {
328 0
                VSLb(req->vsl, SLT_VCL_Error,
329 0
                    "req.body must be cached before restarts");
330 0
                return (-1);
331 0
        }
332
333 42
        if (req->req_body_status == BS_CACHED) {
334 3
                AZ(ObjGetU64(req->wrk, req->body_oc, OA_LEN, &u));
335 3
                return (u);
336 3
        }
337
338 39
        if (req->req_body_status->avail <= 0)
339 3
                return (req->req_body_status->avail);
340
341 36
        if (req->htc->content_length > maxsize) {
342 1
                req->req_body_status = BS_ERROR;
343 1
                (void)VFP_Error(req->vfc, "Request body too big to cache");
344 1
                return (-1);
345 1
        }
346
347 35
        return (vrb_pull(req, maxsize, NULL, NULL));
348 36
}