Code Coverage: vinyl-cache/bin/vinyld/cache/cache_fetch.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
#include "config.h"
32
33
#include "cache_int.h"
34
#include "cache_filter.h"
35
#include "cache_objhead.h"
36
#include "storage/storage.h"
37
#include "vcl.h"
38
#include "vtim.h"
39
#include "vcc_interface.h"
40
41
#define FETCH_STEPS \
42
        FETCH_STEP(mkbereq,           MKBEREQ) \
43
        FETCH_STEP(retry,             RETRY) \
44
        FETCH_STEP(startfetch,        STARTFETCH) \
45
        FETCH_STEP(condfetch,         CONDFETCH) \
46
        FETCH_STEP(fetch,             FETCH) \
47
        FETCH_STEP(fetchbody,         FETCHBODY) \
48
        FETCH_STEP(fetchend,          FETCHEND) \
49
        FETCH_STEP(error,             ERROR) \
50
        FETCH_STEP(fail,              FAIL) \
51
        FETCH_STEP(done,              DONE)
52
53
typedef const struct fetch_step *vbf_state_f(struct worker *, struct busyobj *);
54
55
struct fetch_step {
56
        const char      *name;
57
        vbf_state_f     *func;
58
};
59
60
#define FETCH_STEP(l, U) \
61
    static vbf_state_f vbf_stp_##l; \
62
    static const struct fetch_step F_STP_##U[1] = {{ .name = "Fetch Step " #l, .func = vbf_stp_##l, }};
63
FETCH_STEPS
64
#undef FETCH_STEP
65
66
static hdr_t const H_X_Vinyl = HDR("X-Vinyl");
67
68
static void
69 3007
init_esi_flags(struct busyobj *bo) {
70 3007
        CHECK_OBJ_NOTNULL(bo, BUSYOBJ_MAGIC);
71 3007
#define BERESP_ESI_FLAG(lower, feature, doc) \
72 12028
        bo->lower = FEATURE(feature);
73 3007
#include "tbl/beresp_esi_flags.h"
74 3007
}
75
76
/*--------------------------------------------------------------------
77
 * Allocate an object, with fall-back to Transient.
78
 * XXX: This somewhat overlaps the stuff in stevedore.c
79
 * XXX: Should this be merged over there ?
80
 */
81
82
static int
83
vbf_allocobj(struct busyobj *bo, unsigned l)
84 2949
{
85 2949
        struct objcore *oc;
86 2949
        const struct stevedore *stv;
87 2949
        uint64_t *counter = NULL;
88 2949
        vtim_dur lifetime;
89
90 2949
        CHECK_OBJ_NOTNULL(bo, BUSYOBJ_MAGIC);
91 2949
        oc = bo->fetch_objcore;
92 2949
        CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC);
93
94 2949
        lifetime = oc->ttl + oc->grace + oc->keep;
95
96 2949
        if (bo->uncacheable) {
97 1557
                stv = stv_transient;
98 1557
                counter = &bo->wrk->stats->beresp_uncacheable;
99 1557
        }
100 1392
        else if (lifetime < cache_param->shortlived) {
101 137
                stv = stv_transient;
102 137
                counter = &bo->wrk->stats->beresp_shortlived;
103 137
        }
104 1255
        else
105 1255
                stv = bo->storage;
106
107 2949
        bo->storage = NULL;
108
109 2949
        if (stv == NULL)
110 1
                return (0);
111
112 2948
        if (STV_NewObject(bo->wrk, oc, stv, l)) {
113 2940
                if (counter != NULL)
114 1691
                        (*counter)++;
115 2940
                return (1);
116 2940
        }
117
118 8
        if (stv == stv_transient)
119 4
                return (0);
120
121
        /*
122
         * Try to salvage the transaction by allocating a shortlived object
123
         * on Transient storage.
124
         */
125
126 4
        oc->ttl = vmin_t(float, oc->ttl, cache_param->shortlived);
127 4
        oc->grace = 0.0;
128 4
        oc->keep = 0.0;
129 4
        return (STV_NewObject(bo->wrk, oc, stv_transient, l));
130 8
}
131
132
static void
133
vbf_cleanup(struct busyobj *bo)
134 2720
{
135 2720
        struct vfp_ctx *vfc;
136
137 2720
        CHECK_OBJ_NOTNULL(bo, BUSYOBJ_MAGIC);
138 2720
        vfc = bo->vfc;
139 2720
        CHECK_OBJ_NOTNULL(vfc, VFP_CTX_MAGIC);
140
141 2720
        bo->acct.beresp_bodybytes += VFP_Close(vfc);
142 2720
        bo->vfp_filter_list = NULL;
143
144 2720
        if (bo->director_state != DIR_S_NULL)
145 2710
                VDI_Finish(bo);
146 2720
}
147
148
void
149
Bereq_Rollback(VRT_CTX)
150 10
{
151 10
        struct busyobj *bo;
152
153 10
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
154 10
        bo = ctx->bo;
155 10
        CHECK_OBJ_NOTNULL(bo, BUSYOBJ_MAGIC);
156
157 10
        if (bo->htc != NULL &&
158 10
            bo->htc->body_status != BS_NONE &&
159 10
            bo->htc->body_status != BS_TAKEN)
160 1
                bo->htc->doclose = SC_RESP_CLOSE;
161
162 10
        vbf_cleanup(bo);
163 10
        VCL_TaskLeave(ctx, bo->privs);
164 10
        VCL_TaskEnter(bo->privs);
165 10
        HTTP_Clone(bo->bereq, bo->bereq0);
166 10
        bo->vfp_filter_list = NULL;
167 10
        bo->err_reason = NULL;
168 10
        AN(bo->ws_bo);
169 10
        WS_Rollback(bo->ws, bo->ws_bo);
170 10
}
171
172
/*--------------------------------------------------------------------
173
 * Turn the beresp into a obj
174
 */
175
176
static int
177
vbf_vary(struct busyobj *bo, struct vsb **varyp)
178 2947
{
179 2947
        struct vsb *vary = NULL;
180 2947
        int varyl;
181 2947
        struct objcore *oc;
182
183 2947
        CHECK_OBJ_NOTNULL(bo, BUSYOBJ_MAGIC);
184 2947
        oc = bo->fetch_objcore;
185 2947
        CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC);
186
187 2947
        AN(varyp);
188
189 2947
        if (oc->flags & OC_F_PRIVATE)
190 1441
                return (0);
191
192 1506
        varyl = VRY_Create(bo, &vary);
193 1506
        if (varyl > 0) {
194 221
                AN(vary);
195 221
                *varyp = vary;
196 221
                assert(varyl == VSB_len(vary));
197 221
                varyl = PRNDUP((intptr_t)varyl);
198 1285
        } else if (varyl < 0) {
199
                /*
200
                 * Vary parse error
201
                 * Complain about it, and make this a pass.
202
                 */
203 5
                VSLb(bo->vsl, SLT_Error,
204 5
                    "Illegal 'Vary' header from backend, "
205 5
                    "making this a pass.");
206 5
                bo->uncacheable = 1;
207 5
                AZ(vary);
208 5
                varyl = 0;
209 5
        } else
210
                /* No vary */
211 1280
                AZ(vary);
212 1506
        assert(varyl >= 0);
213 1506
        return (varyl);
214 1506
}
215
216
static int
217
vbf_beresp2obj(struct busyobj *bo)
218 2948
{
219 2948
        unsigned l, l2;
220 2948
        const char *b;
221 2948
        uint8_t *bp;
222 2948
        struct vsb *vary = NULL;
223 2948
        struct objcore *oc;
224
225 2948
        CHECK_OBJ_NOTNULL(bo, BUSYOBJ_MAGIC);
226 2948
        oc = bo->fetch_objcore;
227 2948
        CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC);
228
229
        /* Create Vary instructions */
230 2948
        l = vbf_vary(bo, &vary);
231
232 2948
        l2 = http_EstimateWS(bo->beresp,
233 2948
            bo->uncacheable ? HTTPH_A_PASS : HTTPH_A_INS);
234 2948
        l += l2;
235
236 2948
        if (bo->uncacheable)
237 1557
                oc->flags |= OC_F_HFM;
238
239 2948
        if (!vbf_allocobj(bo, l)) {
240 6
                if (vary != NULL)
241 0
                        VSB_destroy(&vary);
242 6
                AZ(vary);
243 6
                return (VFP_Error(bo->vfc, "Could not get storage"));
244 6
        }
245
246 2942
        if (vary != NULL) {
247 221
                AN(ObjSetAttr(bo->wrk, oc, OA_VARY, VSB_len(vary), VSB_data(vary)));
248 221
                VSB_destroy(&vary);
249 221
        }
250
251 2942
        AZ(ObjSetXID(bo->wrk, oc, bo->vsl->wid));
252
253
        /* for HTTP_Encode() VSLH call */
254 2942
        bo->beresp->logtag = SLT_ObjMethod;
255
256
        /* Filter into object */
257 2942
        bp = ObjSetAttr(bo->wrk, oc, OA_HEADERS, l2, NULL);
258 2942
        AN(bp);
259 2942
        HTTP_Encode(bo->beresp, bp, l2,
260 2942
            bo->uncacheable ? HTTPH_A_PASS : HTTPH_A_INS);
261
262 2942
        if (http_GetHdr(bo->beresp, H_Last_Modified, &b))
263 45
                AZ(ObjSetDouble(bo->wrk, oc, OA_LASTMODIFIED, VTIM_parse(b)));
264 2897
        else
265 2897
                AZ(ObjSetDouble(bo->wrk, oc, OA_LASTMODIFIED,
266 2942
                    floor(oc->t_origin)));
267
268 2942
        return (0);
269 2942
}
270
271
/*--------------------------------------------------------------------
272
 * Copy req->bereq and release req if no body
273
 */
274
275
static const struct fetch_step * v_matchproto_(vbf_state_f)
276
vbf_stp_mkbereq(struct worker *wrk, struct busyobj *bo)
277 2969
{
278 2969
        const char *q;
279 2969
        struct objcore *oc;
280
281 2969
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
282 2969
        CHECK_OBJ_NOTNULL(bo, BUSYOBJ_MAGIC);
283 2969
        CHECK_OBJ_NOTNULL(bo->req, REQ_MAGIC);
284 2969
        oc = bo->fetch_objcore;
285 2969
        CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC);
286
287 2969
        assert(oc->boc->state == BOS_INVALID);
288 2969
        AZ(bo->storage);
289
290 2969
        HTTP_Setup(bo->bereq0, bo->ws, bo->vsl, SLT_BereqMethod);
291 2969
        http_FilterReq(bo->bereq0, bo->req->http,
292 2969
            bo->uncacheable ? HTTPH_R_PASS : HTTPH_R_FETCH);
293
294 2969
        if (bo->uncacheable)
295 1441
                AZ(bo->stale_oc);
296 1528
        else {
297 1528
                http_ForceField(bo->bereq0, HTTP_HDR_METHOD, "GET");
298 1528
                if (cache_param->http_gzip_support)
299 1523
                        http_ForceHeader(bo->bereq0, H_Accept_Encoding, "gzip");
300 1528
        }
301 2969
        http_ForceField(bo->bereq0, HTTP_HDR_PROTO, "HTTP/1.1");
302
303 2969
        if (bo->stale_oc != NULL && !(bo->stale_oc->flags & OC_F_DYING) &&
304 2969
            ObjCheckFlag(bo->wrk, bo->stale_oc, OF_IMSCAND) &&
305 2969
            (bo->stale_oc->boc != NULL || ObjGetLen(wrk, bo->stale_oc) != 0)) {
306 41
                AZ(bo->stale_oc->flags & (OC_F_HFM|OC_F_PRIVATE));
307 41
                q = RFC2616_Strong_LM(NULL, wrk, bo->stale_oc);
308 41
                if (q != NULL)
309 14
                        http_PrintfHeader(bo->bereq0,
310 14
                            "If-Modified-Since: %s", q);
311 41
                q = HTTP_GetHdrPack(bo->wrk, bo->stale_oc, H_ETag);
312 41
                if (q != NULL)
313 28
                        http_PrintfHeader(bo->bereq0,
314 28
                            "If-None-Match: %s", q);
315 41
        }
316
317 2969
        http_CopyHome(bo->bereq0);
318 2969
        HTTP_Setup(bo->bereq, bo->ws, bo->vsl, SLT_BereqMethod);
319 2969
        bo->ws_bo = WS_Snapshot(bo->ws);
320 2969
        HTTP_Clone(bo->bereq, bo->bereq0);
321
322 2969
        if (bo->req->req_body_status->avail == 0) {
323 2736
                VBO_SetState(bo->wrk, bo, BOS_REQ_DONE);
324 2736
        } else if (bo->req->req_body_status == BS_CACHED) {
325 29
                AN(bo->req->body_oc);
326 29
                bo->bereq_body = bo->req->body_oc;
327 29
                HSH_Ref(bo->bereq_body);
328 29
                VBO_SetState(bo->wrk, bo, BOS_REQ_DONE);
329 29
        }
330 2969
        return (F_STP_STARTFETCH);
331 2969
}
332
333
/*--------------------------------------------------------------------
334
 * Start a new VSL transaction and try again
335
 * Prepare the busyobj and fetch processors
336
 */
337
338
static const struct fetch_step * v_matchproto_(vbf_state_f)
339
vbf_stp_retry(struct worker *wrk, struct busyobj *bo)
340 39
{
341 39
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
342 39
        CHECK_OBJ_NOTNULL(bo, BUSYOBJ_MAGIC);
343
344 39
        assert(bo->fetch_objcore->boc->state <= BOS_REQ_DONE);
345
346 39
        if (bo->no_retry != NULL && bo->no_retry != retry_disabled) {
347 2
                VSLb(bo->vsl, SLT_Error,
348 2
                    "Retry not possible, %s", bo->no_retry);
349 2
                return (F_STP_FAIL);
350 2
        }
351
352 37
        VSLb_ts_busyobj(bo, "Retry", W_TIM_real(wrk));
353
354
        /* VDI_Finish (via vbf_cleanup) must have been called before */
355 37
        assert(bo->director_state == DIR_S_NULL);
356
357
        /* reset other bo attributes - See VBO_GetBusyObj */
358 37
        bo->storage = NULL;
359 37
        bo->do_esi = 0;
360 37
        bo->do_stream = 1;
361 37
        bo->was_304 = 0;
362 37
        bo->err_code = 0;
363 37
        bo->err_reason = NULL;
364 37
        bo->connect_timeout = NAN;
365 37
        bo->first_byte_timeout = NAN;
366 37
        bo->between_bytes_timeout = NAN;
367 37
        if (bo->htc != NULL)
368 0
                bo->htc->doclose = SC_NULL;
369 37
        init_esi_flags(bo);
370
371
        // XXX: BereqEnd + BereqAcct ?
372 37
        VSL_ChgId(bo->vsl, "bereq", "retry", VXID_Get(wrk, VSL_BACKENDMARKER));
373 37
        VSLb_ts_busyobj(bo, "Start", bo->t_prev);
374 37
        http_VSL_log(bo->bereq);
375
376 37
        return (F_STP_STARTFETCH);
377 37
}
378
379
/*--------------------------------------------------------------------
380
 * 304 setup logic
381
 */
382
383
static void
384
vbf_304_logic(struct busyobj *bo)
385 32
{
386
387 32
        AZ(bo->stale_oc->flags & (OC_F_HFM|OC_F_PRIVATE));
388 32
        if (ObjCheckFlag(bo->wrk, bo->stale_oc, OF_CHGCE)) {
389
                /*
390
                 * If a VFP changed C-E in the stored
391
                 * object, then don't overwrite C-E from
392
                 * the IMS fetch, and we must weaken any
393
                 * new ETag we get.
394
                 */
395 2
                RFC2616_Weaken_Etag(bo->beresp);
396 2
        }
397 32
        http_Unset(bo->beresp, H_Content_Encoding);
398 32
        http_Unset(bo->beresp, H_Content_Length);
399 32
        HTTP_Merge(bo->wrk, bo->stale_oc, bo->beresp);
400 32
}
401
402
/*--------------------------------------------------------------------
403
 * Setup bereq from bereq0, run vcl_backend_fetch
404
 */
405
406
static const struct fetch_step * v_matchproto_(vbf_state_f)
407
vbf_stp_startfetch(struct worker *wrk, struct busyobj *bo)
408 3006
{
409 3006
        int i;
410 3006
        const char *q;
411 3006
        vtim_real now;
412 3006
        unsigned handling, skip_vbr = 0;
413 3006
        struct objcore *oc;
414
415 3006
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
416 3006
        CHECK_OBJ_NOTNULL(bo, BUSYOBJ_MAGIC);
417 3006
        oc = bo->fetch_objcore;
418 3006
        CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC);
419
420
        // this complements the stale_oc handling in vbf_stp_mkbereq():
421
        // Conditions might have changed since we made the bereq (retry)
422 3006
        if (! bo->uncacheable && bo->stale_oc != NULL &&
423 3006
            bo->stale_oc->flags & OC_F_DYING) {
424 2
                http_Unset(bo->bereq, H_If_Modified_Since);
425 2
                http_Unset(bo->bereq, H_If_None_Match);
426 2
        }
427
428 3006
        AZ(bo->storage);
429 3006
        bo->storage = bo->uncacheable ? stv_transient : STV_next();
430
431 3006
        if (bo->retries > 0)
432 37
                http_Unset(bo->bereq, H_X_Vinyl);
433
434 3006
        http_PrintfHeader(bo->bereq, "X-Vinyl: %ju", VXID(bo->vsl->wid));
435
436 3006
        if (bo->bereq_body == NULL && bo->req == NULL) {
437 2773
                if (http_method_among(bo->bereq->wkm, (WKM_GET | WKM_HEAD | WKM_DELETE |
438 2773
                    WKM_OPTIONS | WKM_TRACE)))
439 2753
                        http_Unset(bo->bereq, H_Content_Length);
440 20
                else
441 20
                        http_ForceHeader(bo->bereq, H_Content_Length, "0");
442 2773
        }
443
444 3006
        VCL_backend_fetch_method(bo->vcl, wrk, NULL, bo, NULL);
445
446 3006
        if (wrk->vpi->handling == VCL_RET_ABANDON ||
447 3006
            wrk->vpi->handling == VCL_RET_FAIL)
448 8
                return (F_STP_FAIL);
449
450 2998
        assert (wrk->vpi->handling == VCL_RET_FETCH ||
451 2998
            wrk->vpi->handling == VCL_RET_ERROR);
452
453 2998
        HTTP_Setup(bo->beresp, bo->ws, bo->vsl, SLT_BerespMethod);
454
455 2998
        assert(oc->boc->state <= BOS_REQ_DONE);
456
457 2998
        AZ(bo->htc);
458
459 2998
        VFP_Setup(bo->vfc, wrk);
460 2998
        bo->vfc->oc = oc;
461 2998
        bo->vfc->resp = bo->beresp;
462 2998
        bo->vfc->req = bo->bereq;
463
464 2998
        if (wrk->vpi->handling == VCL_RET_ERROR)
465 13
                return (F_STP_ERROR);
466
467 2985
        VSLb_ts_busyobj(bo, "Fetch", W_TIM_real(wrk));
468 2985
        i = VDI_GetHdr(bo);
469 2985
        if (bo->htc != NULL)
470 2709
                CHECK_OBJ_NOTNULL(bo->htc->doclose, STREAM_CLOSE_MAGIC);
471
472 2985
        bo->t_resp = now = W_TIM_real(wrk);
473 2985
        VSLb_ts_busyobj(bo, "Beresp", now);
474
475 2985
        if (i) {
476 274
                assert(bo->director_state == DIR_S_NULL);
477 274
                return (F_STP_ERROR);
478 274
        }
479
480 2711
        if (bo->htc != NULL && bo->htc->body_status == BS_ERROR) {
481 2
                bo->htc->doclose = SC_RX_BODY;
482 2
                vbf_cleanup(bo);
483 2
                VSLb(bo->vsl, SLT_Error, "Body cannot be fetched");
484 2
                assert(bo->director_state == DIR_S_NULL);
485 2
                return (F_STP_ERROR);
486 2
        }
487
488 2709
        if (!http_GetHdr(bo->beresp, H_Date, NULL)) {
489
                /*
490
                 * RFC 2616 14.18 Date: The Date general-header field
491
                 * represents the date and time at which the message was
492
                 * originated, having the same semantics as orig-date in
493
                 * RFC 822. ... A received message that does not have a
494
                 * Date header field MUST be assigned one by the recipient
495
                 * if the message will be cached by that recipient or
496
                 * gatewayed via a protocol which requires a Date.
497
                 *
498
                 * If we didn't get a Date header, we assign one here.
499
                 */
500 95
                http_TimeHeader(bo->beresp, "Date: ", now);
501 95
        }
502
503
        /*
504
         * These two headers can be spread over multiple actual headers
505
         * and we rely on their content outside of VCL, so collect them
506
         * into one line here.
507
         */
508 2709
        http_CollectHdr(bo->beresp, H_Cache_Control);
509 2709
        http_CollectHdr(bo->beresp, H_Vary);
510
511
        /* What does RFC2616 think about TTL ? */
512 2709
        RFC2616_Ttl(bo, now,
513 2709
            &oc->t_origin,
514 2709
            &oc->ttl,
515 2709
            &oc->grace,
516 2709
            &oc->keep);
517
518 2709
        AZ(bo->do_esi);
519 2709
        AZ(bo->was_304);
520
521 2709
        if (http_IsStatus(bo->beresp, 304) && !bo->uncacheable) {
522 41
                if (bo->stale_oc == NULL){
523 0
                        VSLb(bo->vsl, SLT_Error,
524 0
                            "304 response but not conditional fetch");
525 0
                        bo->htc->doclose = SC_RX_BAD;
526 0
                        vbf_cleanup(bo);
527 0
                        return (F_STP_ERROR);
528 0
                }
529 41
                bo->was_304 = 1;
530 41
                VCL_backend_refresh_method(bo->vcl, wrk, NULL, bo, NULL);
531 41
                switch (wrk->vpi->handling) {
532 32
                case VCL_RET_MERGE:
533 32
                        vbf_304_logic(bo);
534 32
                        break;
535 2
                case VCL_RET_BERESP:
536 2
                        http_SetStatus(bo->beresp, 200, NULL);
537 2
                        http_Unset(bo->beresp, H_Content_Length);
538 2
                        http_Unset(bo->beresp, H_Content_Encoding);
539 2
                        q = HTTP_GetHdrPack(wrk, bo->stale_oc,
540 2
                            H_Content_Length);
541 2
                        if (q != NULL) {
542 1
                                http_ForceHeader(bo->beresp,
543 1
                                    H_Content_Length, q);
544 1
                        }
545 2
                        q = HTTP_GetHdrPack(wrk, bo->stale_oc,
546 2
                            H_Content_Encoding);
547 2
                        if (q != NULL) {
548 0
                                http_ForceHeader(bo->beresp,
549 0
                                    H_Content_Encoding, q);
550 0
                        }
551 2
                        q = HTTP_GetHdrPack(wrk, bo->stale_oc,
552 2
                            H_Last_Modified);
553 2
                        if (q != NULL) {
554 0
                                http_ForceHeader(bo->beresp,
555 0
                                    H_Last_Modified, q);
556 0
                        }
557 2
                        q = HTTP_GetHdrPack(wrk, bo->stale_oc, H_ETag);
558 2
                        if (q != NULL)
559 2
                                http_ForceHeader(bo->beresp, H_ETag, q);
560 2
                        break;
561 1
                case VCL_RET_OBJ_STALE:
562 1
                        if (HTTP_Decode(bo->beresp, ObjGetAttr(bo->wrk,
563 1
                                bo->stale_oc, OA_HEADERS, NULL))) {
564 0
                                bo->htc->doclose = SC_RX_OVERFLOW;
565 0
                                vbf_cleanup(bo);
566 0
                                return (F_STP_ERROR);
567 0
                        }
568 1
                        break;
569 1
                case VCL_RET_RETRY:
570 6
                case VCL_RET_ERROR:
571 6
                case VCL_RET_ABANDON:
572 6
                case VCL_RET_FAIL:
573 6
                        skip_vbr = 1;
574 6
                        break;
575 0
                default:
576 0
                        WRONG("Illegal return from vcl_backend_refresh{}");
577 41
                }
578 41
        }
579
580 2709
        if (bo->htc != NULL && bo->htc->doclose == SC_NULL &&
581 2709
            http_GetHdrField(bo->bereq, H_Connection, "close", NULL))
582 13
                bo->htc->doclose = SC_REQ_CLOSE;
583 2709
        if (!skip_vbr)
584 2703
                VCL_backend_response_method(bo->vcl, wrk, NULL, bo, NULL);
585
586 2709
        if (bo->htc != NULL && bo->htc->doclose == SC_NULL &&
587 2709
            http_GetHdrField(bo->beresp, H_Connection, "close", NULL))
588 1
                bo->htc->doclose = SC_RESP_CLOSE;
589
590 2709
        if (VRG_CheckBo(bo) < 0) {
591
                /* the body is never read, so we cannot reuse it */
592 8
                if (bo->htc != NULL && bo->htc->body_status != BS_NONE)
593 7
                        bo->htc->doclose = SC_RX_BAD;
594 8
                vbf_cleanup(bo);
595 8
                return (F_STP_ERROR);
596 8
        }
597
598 2701
        if (wrk->vpi->handling == VCL_RET_ABANDON ||
599 2701
            wrk->vpi->handling == VCL_RET_FAIL ||
600 2701
            wrk->vpi->handling == VCL_RET_ERROR) {
601
                /* do not count deliberately ending the backend connection as
602
                 * fetch failure
603
                 */
604 16
                handling = wrk->vpi->handling;
605 16
                if (bo->htc)
606 16
                        bo->htc->doclose = SC_RESP_CLOSE;
607 16
                vbf_cleanup(bo);
608 16
                wrk->vpi->handling = handling;
609
610 16
                if (wrk->vpi->handling == VCL_RET_ERROR)
611 10
                        return (F_STP_ERROR);
612 6
                else
613 6
                        return (F_STP_FAIL);
614 16
        }
615
616 2685
        if (wrk->vpi->handling == VCL_RET_RETRY) {
617 29
                if (bo->htc && bo->htc->body_status != BS_NONE)
618 7
                        bo->htc->doclose = SC_RESP_CLOSE;
619 29
                vbf_cleanup(bo);
620
621 29
                if (bo->retries++ < bo->max_retries)
622 27
                        return (F_STP_RETRY);
623
624 2
                VSLb(bo->vsl, SLT_VCL_Error,
625 2
                    "Too many retries, delivering 503");
626 2
                assert(bo->director_state == DIR_S_NULL);
627 2
                return (F_STP_ERROR);
628 2
        }
629
630 2656
        VSLb_ts_busyobj(bo, "Process", W_TIM_real(wrk));
631 2656
        assert(oc->boc->state <= BOS_REQ_DONE);
632 2656
        if (oc->boc->state != BOS_REQ_DONE)
633 174
                VBO_SetState(wrk, bo, BOS_REQ_DONE);
634
635 2656
        if (bo->do_esi)
636 651
                bo->do_stream = 0;
637 2656
        if (wrk->vpi->handling == VCL_RET_PASS) {
638 30
                oc->flags |= OC_F_HFP;
639 30
                bo->uncacheable = 1;
640 30
                wrk->vpi->handling = VCL_RET_DELIVER;
641 30
        }
642 2656
        if (!bo->uncacheable || !bo->do_stream || bo->is_bgfetch)
643 1720
                oc->boc->transit_buffer = 0;
644 2656
        if (bo->uncacheable)
645 1409
                oc->flags |= OC_F_HFM;
646
647 2656
        assert(wrk->vpi->handling == VCL_RET_DELIVER);
648
649 2656
        return (bo->was_304 ? F_STP_CONDFETCH : F_STP_FETCH);
650 2656
}
651
652
/*--------------------------------------------------------------------
653
 */
654
655
static const struct fetch_step * v_matchproto_(vbf_state_f)
656
vbf_stp_fetchbody(struct worker *wrk, struct busyobj *bo)
657 1685
{
658 1685
        ssize_t l;
659 1685
        uint8_t *ptr;
660 1685
        enum vfp_status vfps = VFP_ERROR;
661 1685
        ssize_t est;
662 1685
        struct vfp_ctx *vfc;
663 1685
        struct objcore *oc;
664
665 1685
        CHECK_OBJ_NOTNULL(bo, BUSYOBJ_MAGIC);
666 1685
        vfc = bo->vfc;
667 1685
        CHECK_OBJ_NOTNULL(vfc, VFP_CTX_MAGIC);
668 1685
        oc = bo->fetch_objcore;
669 1685
        CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC);
670
671 1685
        AN(vfc->vfp_nxt);
672
673 1685
        est = bo->htc->content_length;
674 1685
        if (est < 0)
675 170
                est = 0;
676
677 58400
        do {
678 58400
                if (oc->flags & OC_F_CANCEL) {
679
                        /*
680
                         * A pass object and delivery was terminated
681
                         * We don't fail the fetch, in order for HitMiss
682
                         * objects to be created.
683
                         */
684 1
                        AN(oc->flags & OC_F_HFM);
685 1
                        VSLb(wrk->vsl, SLT_Debug,
686 1
                            "Fetch: Pass delivery abandoned");
687 1
                        bo->htc->doclose = SC_RX_BODY;
688 1
                        break;
689 1
                }
690 58399
                AZ(vfc->failed);
691 58399
                l = est;
692 58399
                assert(l >= 0);
693 58399
                if (VFP_GetStorage(vfc, &l, &ptr) != VFP_OK) {
694 5
                        bo->htc->doclose = SC_RX_BODY;
695 5
                        break;
696 5
                }
697
698 58394
                AZ(vfc->failed);
699 58394
                vfps = VFP_Suck(vfc, ptr, &l);
700 58398
                if (l >= 0 && vfps != VFP_ERROR) {
701 58357
                        VFP_Extend(vfc, l, vfps);
702 58357
                        if (est >= l)
703 3308
                                est -= l;
704 55049
                        else
705 55049
                                est = 0;
706 58357
                }
707 58394
        } while (vfps == VFP_OK);
708
709 1685
        if (vfc->failed) {
710 46
                (void)VFP_Error(vfc, "Fetch pipeline failed to process");
711 46
                bo->htc->doclose = SC_RX_BODY;
712 46
                vbf_cleanup(bo);
713 46
                if (!bo->do_stream) {
714 25
                        assert(oc->boc->state < BOS_STREAM);
715
                        // XXX: doclose = ?
716 25
                        return (F_STP_ERROR);
717 25
                } else {
718 21
                        wrk->stats->fetch_failed++;
719 21
                        return (F_STP_FAIL);
720 21
                }
721 46
        }
722
723 1639
        return (F_STP_FETCHEND);
724 1685
}
725
726
static const struct fetch_step * v_matchproto_(vbf_state_f)
727
vbf_stp_fetch(struct worker *wrk, struct busyobj *bo)
728 2618
{
729 2618
        struct vrt_ctx ctx[1];
730 2618
        struct objcore *oc;
731
732 2618
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
733 2618
        CHECK_OBJ_NOTNULL(bo, BUSYOBJ_MAGIC);
734 2618
        oc = bo->fetch_objcore;
735 2618
        CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC);
736
737 2618
        assert(wrk->vpi->handling == VCL_RET_DELIVER);
738
739 2618
        if (bo->htc == NULL) {
740 2
                (void)VFP_Error(bo->vfc, "No backend connection (rollback?)");
741 2
                vbf_cleanup(bo);
742 2
                return (F_STP_ERROR);
743 2
        }
744
745
        /* No body -> done */
746 2616
        if (bo->htc->body_status == BS_NONE || bo->htc->content_length == 0) {
747 855
                http_Unset(bo->beresp, H_Content_Encoding);
748 855
                bo->do_gzip = bo->do_gunzip = 0;
749 855
                bo->do_stream = 0;
750 855
                bo->vfp_filter_list = "";
751 1761
        } else if (bo->vfp_filter_list == NULL) {
752 1755
                bo->vfp_filter_list = VBF_Get_Filter_List(bo);
753 1755
        }
754
755 2616
        if (bo->vfp_filter_list == NULL ||
756 2618
            VCL_StackVFP(bo->vfc, bo->vcl, bo->vfp_filter_list)) {
757 24
                (bo)->htc->doclose = SC_OVERLOAD;
758 24
                vbf_cleanup(bo);
759 24
                return (F_STP_ERROR);
760 24
        }
761
762 2592
        if (oc->flags & OC_F_PRIVATE)
763 1281
                AN(bo->uncacheable);
764
765 2592
        oc->boc->fetched_so_far = 0;
766
767 2592
        INIT_OBJ(ctx, VRT_CTX_MAGIC);
768 2592
        VCL_Bo2Ctx(ctx, bo);
769
770 2592
        if (VFP_Open(ctx, bo->vfc)) {
771 53
                (void)VFP_Error(bo->vfc, "Fetch pipeline failed to open");
772 53
                bo->htc->doclose = SC_RX_BODY;
773 53
                vbf_cleanup(bo);
774 53
                return (F_STP_ERROR);
775 53
        }
776
777 2539
        if (vbf_beresp2obj(bo)) {
778 3
                bo->htc->doclose = SC_RX_BODY;
779 3
                vbf_cleanup(bo);
780 3
                return (F_STP_ERROR);
781 3
        }
782
783 2536
#define OBJ_FLAG(U, l, v)                                               \
784 10147
        if (bo->vfc->obj_flags & OF_##U)                                \
785 10147
                ObjSetFlag(bo->wrk, oc, OF_##U, 1);
786 2539
#include "tbl/obj_attr.h"
787
788 2536
        if (!(oc->flags & OC_F_HFM) &&
789 2536
            http_IsStatus(bo->beresp, 200) && (
790 1182
              RFC2616_Strong_LM(bo->beresp, NULL, NULL) != NULL ||
791 1182
              http_GetHdr(bo->beresp, H_ETag, NULL)))
792 62
                ObjSetFlag(bo->wrk, oc, OF_IMSCAND, 1);
793
794 2536
        assert(oc->boc->refcount >= 1);
795
796 2536
        assert(oc->boc->state == BOS_REQ_DONE);
797
798 2536
        if (bo->do_stream)
799 1046
                VBO_SetState(wrk, bo, BOS_STREAM);
800
801 2536
        VSLb(bo->vsl, SLT_Fetch_Body, "%u %s %s",
802 2536
            bo->htc->body_status->nbr, bo->htc->body_status->name,
803 2536
            bo->do_stream ? "stream" : "-");
804
805 2536
        if (bo->htc->body_status != BS_NONE) {
806 1685
                assert(bo->htc->body_status != BS_ERROR);
807 1685
                return (F_STP_FETCHBODY);
808 1685
        }
809 851
        AZ(bo->vfc->failed);
810 851
        return (F_STP_FETCHEND);
811 851
}
812
813
static const struct fetch_step * v_matchproto_(vbf_state_f)
814
vbf_stp_fetchend(struct worker *wrk, struct busyobj *bo)
815 2523
{
816
817 2523
        struct objcore *oc;
818
819 2523
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
820 2523
        CHECK_OBJ_NOTNULL(bo, BUSYOBJ_MAGIC);
821 2523
        oc = bo->fetch_objcore;
822 2523
        CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC);
823
824 2523
        AZ(bo->vfc->failed);
825
826
        /* Recycle the backend connection before setting BOS_FINISHED to
827
           give predictable backend reuse behavior for vinyltest */
828 2523
        vbf_cleanup(bo);
829
830 2523
        AZ(ObjSetU64(wrk, oc, OA_LEN, oc->boc->fetched_so_far));
831
832 2523
        if (bo->do_stream)
833 1054
                assert(oc->boc->state == BOS_STREAM);
834 1469
        else
835 1469
                assert(oc->boc->state == BOS_REQ_DONE);
836
837 2523
        VBO_SetState(wrk, bo, BOS_FINISHED);
838 2523
        VSLb_ts_busyobj(bo, "BerespBody", W_TIM_real(wrk));
839 2523
        if (bo->stale_oc != NULL) {
840 81
                VSL(SLT_ExpKill, NO_VXID, "VBF_Superseded x=%ju n=%ju",
841 81
                    VXID(ObjGetXID(wrk, bo->stale_oc)),
842 81
                    VXID(ObjGetXID(wrk, bo->fetch_objcore)));
843 81
                HSH_Replace(bo->stale_oc, bo->fetch_objcore);
844 81
        }
845 2523
        return (F_STP_DONE);
846 2523
}
847
848
/*--------------------------------------------------------------------
849
 */
850
851
struct vbf_objiter_priv {
852
        unsigned                magic;
853
#define VBF_OBITER_PRIV_MAGIC   0x3c272a17
854
        struct vfp_ctx          *vfc;
855
        // not yet allocated
856
        ssize_t         l;
857
        // current allocation
858
        uint8_t         *p;
859
        ssize_t         pl;
860
};
861
862
static int v_matchproto_(objiterate_f)
863
vbf_objiterate(void *priv, unsigned flush, const void *ptr, ssize_t len)
864 34
{
865 34
        struct vbf_objiter_priv *vop;
866 34
        ssize_t l;
867 34
        const uint8_t *ps = ptr;
868
869 34
        CAST_OBJ_NOTNULL(vop, priv, VBF_OBITER_PRIV_MAGIC);
870 34
        CHECK_OBJ_NOTNULL(vop->vfc, VFP_CTX_MAGIC);
871
872 34
        flush &= OBJ_ITER_END;
873
874 67
        while (len > 0) {
875 34
                if (vop->pl == 0) {
876 33
                        vop->p = NULL;
877 33
                        AN(vop->l);
878 33
                        vop->pl = vop->l;
879 33
                        if (VFP_GetStorage(vop->vfc, &vop->pl, &vop->p)
880 33
                            != VFP_OK)
881 1
                                return (1);
882 32
                        if (vop->pl < vop->l)
883 2
                                vop->l -= vop->pl;
884 30
                        else
885 30
                                vop->l = 0;
886 32
                }
887 33
                AN(vop->pl);
888 33
                AN(vop->p);
889
890 33
                l = vmin(vop->pl, len);
891 33
                vmemcpy(vop->p, ps, l);
892 33
                VFP_Extend(vop->vfc, l,
893 33
                           flush && l == len ? VFP_END : VFP_OK);
894 33
                ps += l;
895 33
                vop->p += l;
896 33
                len -= l;
897 33
                vop->pl -= l;
898 33
        }
899 33
        if (flush && vop->vfc->failed == 0)
900 30
                AZ(vop->l);
901 33
        return (0);
902 33
}
903
904
// Fetch oc into vfc->oc
905
// soon to be exported
906
static enum vfp_status
907
VBF_Obj(struct worker *wrk, struct objcore *oc, struct vfp_ctx *vfc)
908 31
{
909 31
        struct vbf_objiter_priv vop[1];
910
911 31
        INIT_OBJ(vop, VBF_OBITER_PRIV_MAGIC);
912 31
        vop->vfc = vfc;
913 31
        vop->l = ObjGetLen(wrk, oc);
914 31
        if (ObjIterate(wrk, oc, vop, vbf_objiterate, 0))
915 1
                return (VFP_Error(vfc, "Template object failed"));
916 30
        return (VFP_OK);
917 31
}
918
919
static const struct fetch_step * v_matchproto_(vbf_state_f)
920
vbf_stp_condfetch(struct worker *wrk, struct busyobj *bo)
921 34
{
922 34
        struct boc *stale_boc;
923 34
        enum boc_state_e stale_state;
924 34
        struct objcore *oc, *stale_oc;
925
926 34
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
927 34
        CHECK_OBJ_NOTNULL(bo, BUSYOBJ_MAGIC);
928 34
        oc = bo->fetch_objcore;
929 34
        CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC);
930 34
        stale_oc = bo->stale_oc;
931 34
        CHECK_OBJ_NOTNULL(stale_oc, OBJCORE_MAGIC);
932
933 34
        stale_boc = HSH_RefBoc(stale_oc);
934 34
        CHECK_OBJ_ORNULL(stale_boc, BOC_MAGIC);
935 34
        if (stale_boc) {
936
                /* Wait for the stale object to become fully fetched, so
937
                 * that we can catch fetch errors, before we unbusy the
938
                 * new object. This serves two purposes. First it helps
939
                 * with request coalescing, and stops long chains of
940
                 * IMS-updated short-TTL objects all streaming from a
941
                 * single slow body fetch. Second it makes sure that all
942
                 * the object attributes are complete when we copy them
943
                 * (this would be an issue for ie OA_GZIPBITS). */
944 4
                VSLb(bo->vsl, SLT_Notice,
945 4
                    "vsl: Conditional fetch wait for streaming object");
946
                /* XXX: We should have a VCL controlled timeout here */
947 4
                stale_state = ObjWaitState(stale_oc, BOS_FINISHED);
948 4
                HSH_DerefBoc(bo->wrk, stale_oc);
949 4
                stale_boc = NULL;
950 4
                if (stale_state != BOS_FINISHED) {
951 2
                        assert(stale_state == BOS_FAILED);
952 2
                        AN(stale_oc->flags & OC_F_FAILED);
953 2
                }
954 4
        }
955
956 34
        AZ(stale_boc);
957 34
        if (stale_oc->flags & OC_F_FAILED) {
958 2
                (void)VFP_Error(bo->vfc, "Template object failed");
959 2
                vbf_cleanup(bo);
960 2
                wrk->stats->fetch_failed++;
961 2
                return (F_STP_FAIL);
962 2
        }
963
964 32
        if (vbf_beresp2obj(bo)) {
965 1
                vbf_cleanup(bo);
966 1
                wrk->stats->fetch_failed++;
967 1
                return (F_STP_FAIL);
968 1
        }
969
970 31
        if (ObjHasAttr(bo->wrk, stale_oc, OA_ESIDATA))
971 1
                AZ(ObjCopyAttr(bo->wrk, oc, stale_oc, OA_ESIDATA));
972
973 31
        AZ(ObjCopyAttr(bo->wrk, oc, stale_oc, OA_FLAGS));
974 31
        if (oc->flags & OC_F_HFM)
975 2
                ObjSetFlag(bo->wrk, oc, OF_IMSCAND, 0);
976 31
        AZ(ObjCopyAttr(bo->wrk, oc, stale_oc, OA_GZIPBITS));
977
978 31
        if (bo->do_stream)
979 30
                VBO_SetState(wrk, bo, BOS_STREAM);
980
981 31
        (void)VBF_Obj(wrk, stale_oc, bo->vfc);
982
983 31
        if (bo->vfc->failed) {
984 1
                vbf_cleanup(bo);
985 1
                wrk->stats->fetch_failed++;
986 1
                return (F_STP_FAIL);
987 1
        }
988 30
        return (F_STP_FETCHEND);
989 31
}
990
991
/*--------------------------------------------------------------------
992
 * Create synth object
993
 *
994
 * replaces a stale object unless
995
 * - abandoning the bereq or
996
 * - leaving vcl_backend_error with return (deliver)
997
 *
998
 * We do want the stale replacement to avoid an object pileup with short ttl and
999
 * long grace/keep, yet there could exist cases where a cache object is
1000
 * deliberately created to momentarily override a stale object.
1001
 *
1002
 * If this case exists, we should add a vcl veto (e.g. beresp.replace_stale with
1003
 * default true)
1004
 */
1005
1006
static const struct fetch_step * v_matchproto_(vbf_state_f)
1007
vbf_stp_error(struct worker *wrk, struct busyobj *bo)
1008 416
{
1009 416
        ssize_t l, ll, o;
1010 416
        vtim_real now;
1011 416
        uint8_t *ptr;
1012 416
        struct vsb *synth_body;
1013 416
        struct objcore *stale, *oc;
1014
1015 416
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
1016 416
        CHECK_OBJ_NOTNULL(bo, BUSYOBJ_MAGIC);
1017 416
        oc = bo->fetch_objcore;
1018 416
        CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC);
1019 416
        CHECK_OBJ_NOTNULL(oc->boc, BOC_MAGIC);
1020 416
        assert(oc->boc->state < BOS_STREAM);
1021 416
        assert(bo->director_state == DIR_S_NULL);
1022
1023 416
        if (wrk->vpi->handling != VCL_RET_ERROR)
1024 393
                wrk->stats->fetch_failed++;
1025
1026 416
        now = W_TIM_real(wrk);
1027 416
        VSLb_ts_busyobj(bo, "Error", now);
1028
1029 416
        if (oc->stobj->stevedore != NULL) {
1030
                // replacing an already fetched object with a "synth" one
1031 25
                assert(oc->boc->state < BOS_STREAM);
1032 25
                oc->boc->fetched_so_far = 0;
1033 25
                ObjFreeObj(bo->wrk, oc);
1034 25
        }
1035
1036 416
        if (bo->storage == NULL)
1037 28
                bo->storage = STV_next();
1038
1039
        // XXX: reset all beresp flags ?
1040
1041 416
        HTTP_Setup(bo->beresp, bo->ws, bo->vsl, SLT_BerespMethod);
1042 416
        if (bo->err_code > 0)
1043 35
                http_PutResponse(bo->beresp, "HTTP/1.1", bo->err_code,
1044 35
                    bo->err_reason);
1045 381
        else
1046 381
                http_PutResponse(bo->beresp, "HTTP/1.1", 503,
1047 381
                    "Backend fetch failed");
1048
1049 416
        http_TimeHeader(bo->beresp, "Date: ", now);
1050 416
        http_SetHeader(bo->beresp, "Server: Vinyl-Cache");
1051
1052 416
        stale = bo->stale_oc;
1053 416
        oc->t_origin = now;
1054 416
        oc->ttl = 0;
1055 416
        oc->grace = 0;
1056 416
        oc->keep = 0;
1057
1058 416
        synth_body = VSB_new_auto();
1059 416
        AN(synth_body);
1060
1061 416
        VCL_backend_error_method(bo->vcl, wrk, NULL, bo, synth_body);
1062
1063 416
        AZ(VSB_finish(synth_body));
1064
1065 416
        if (wrk->vpi->handling == VCL_RET_ABANDON || wrk->vpi->handling == VCL_RET_FAIL) {
1066 28
                VSB_destroy(&synth_body);
1067 28
                return (F_STP_FAIL);
1068 28
        }
1069
1070 388
        if (wrk->vpi->handling == VCL_RET_RETRY) {
1071 13
                VSB_destroy(&synth_body);
1072 13
                if (bo->retries++ < bo->max_retries)
1073 12
                        return (F_STP_RETRY);
1074 1
                VSLb(bo->vsl, SLT_VCL_Error, "Too many retries, failing");
1075 1
                return (F_STP_FAIL);
1076 13
        }
1077
1078 375
        assert(wrk->vpi->handling == VCL_RET_DELIVER);
1079
1080 375
        assert(bo->vfc->wrk == bo->wrk);
1081 375
        assert(bo->vfc->oc == oc);
1082 375
        assert(bo->vfc->resp == bo->beresp);
1083 375
        assert(bo->vfc->req == bo->bereq);
1084
1085 375
        if (vbf_beresp2obj(bo)) {
1086 2
                VSB_destroy(&synth_body);
1087 2
                return (F_STP_FAIL);
1088 2
        }
1089
1090 373
        oc->boc->transit_buffer = 0;
1091
1092 373
        ll = VSB_len(synth_body);
1093 373
        o = 0;
1094 698
        while (ll > 0) {
1095 326
                l = ll;
1096 326
                if (VFP_GetStorage(bo->vfc, &l, &ptr) != VFP_OK) {
1097 1
                        VSB_destroy(&synth_body);
1098 1
                        return (F_STP_FAIL);
1099 1
                }
1100 325
                l = vmin(l, ll);
1101 325
                vmemcpy(ptr, VSB_data(synth_body) + o, l);
1102 325
                VFP_Extend(bo->vfc, l, l == ll ? VFP_END : VFP_OK);
1103 325
                ll -= l;
1104 325
                o += l;
1105 325
        }
1106 372
        assert(o == VSB_len(synth_body));
1107 372
        AZ(ObjSetU64(wrk, oc, OA_LEN, o));
1108 372
        VSB_destroy(&synth_body);
1109 372
        if (stale != NULL && oc->ttl > 0)
1110 22
                HSH_Kill(stale);
1111 372
        VBO_SetState(wrk, bo, BOS_FINISHED);
1112 372
        return (F_STP_DONE);
1113 372
}
1114
1115
/*--------------------------------------------------------------------
1116
 */
1117
1118
static const struct fetch_step * v_matchproto_(vbf_state_f)
1119
vbf_stp_fail(struct worker *wrk, struct busyobj *bo)
1120 74
{
1121 74
        struct objcore *oc;
1122
1123 74
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
1124 74
        CHECK_OBJ_NOTNULL(bo, BUSYOBJ_MAGIC);
1125 74
        oc = bo->fetch_objcore;
1126 74
        CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC);
1127
1128 74
        assert(oc->boc->state < BOS_FINISHED);
1129 74
        VBO_SetState(wrk, bo, BOS_FAILED);
1130 74
        HSH_Kill(oc);
1131 74
        return (F_STP_DONE);
1132 74
}
1133
1134
/*--------------------------------------------------------------------
1135
 */
1136
1137
static const struct fetch_step * v_matchproto_(vbf_state_f)
1138
vbf_stp_done(struct worker *wrk, struct busyobj *bo)
1139 0
{
1140
1141 0
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
1142 0
        CHECK_OBJ_NOTNULL(bo, BUSYOBJ_MAGIC);
1143 0
        WRONG("Just plain wrong");
1144 0
        NEEDLESS(return (F_STP_DONE));
1145 0
}
1146
1147
static void v_matchproto_(task_func_t)
1148
vbf_fetch_thread(struct worker *wrk, void *priv)
1149 2969
{
1150 2969
        struct vrt_ctx ctx[1];
1151 2969
        struct busyobj *bo;
1152 2969
        struct objcore *oc;
1153 2969
        const struct fetch_step *stp;
1154
1155 2969
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
1156 2969
        CAST_OBJ_NOTNULL(bo, priv, BUSYOBJ_MAGIC);
1157 2969
        CHECK_OBJ_NOTNULL(bo->req, REQ_MAGIC);
1158 2969
        oc = bo->fetch_objcore;
1159 2969
        CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC);
1160
1161 2969
        THR_SetBusyobj(bo);
1162 2969
        stp = F_STP_MKBEREQ;
1163 2969
        assert(isnan(bo->t_first));
1164 2969
        assert(isnan(bo->t_prev));
1165 2969
        VSLb_ts_busyobj(bo, "Start", W_TIM_real(wrk));
1166
1167 2969
        bo->wrk = wrk;
1168 2969
        wrk->vsl = bo->vsl;
1169
1170
#if 0
1171
        if (bo->stale_oc != NULL) {
1172
                CHECK_OBJ_NOTNULL(bo->stale_oc, OBJCORE_MAGIC);
1173
                /* We don't want the oc/stevedore ops in fetching thread */
1174
                if (!ObjCheckFlag(wrk, bo->stale_oc, OF_IMSCAND))
1175
                        (void)HSH_DerefObjCore(wrk, &bo->stale_oc, 0);
1176
        }
1177
#endif
1178
1179 2969
        VCL_TaskEnter(bo->privs);
1180 16330
        while (stp != F_STP_DONE) {
1181 13361
                CHECK_OBJ_NOTNULL(bo, BUSYOBJ_MAGIC);
1182 13361
                assert(oc->boc->refcount >= 1);
1183 13361
                if (oc->boc->state < BOS_REQ_DONE)
1184 3197
                        AN(bo->req);
1185 10164
                else
1186 10164
                        AZ(bo->req);
1187 13361
                AN(stp);
1188 13361
                AN(stp->name);
1189 13361
                AN(stp->func);
1190 13361
                stp = stp->func(wrk, bo);
1191 13361
        }
1192
1193 2969
        assert(bo->director_state == DIR_S_NULL);
1194
1195 2969
        INIT_OBJ(ctx, VRT_CTX_MAGIC);
1196 2969
        VCL_Bo2Ctx(ctx, bo);
1197 2969
        VCL_TaskLeave(ctx, bo->privs);
1198 2969
        http_Teardown(bo->bereq);
1199 2969
        http_Teardown(bo->beresp);
1200
        // cannot make assumptions about the number of references here #3434
1201 2969
        if (bo->bereq_body != NULL)
1202 24
                (void)HSH_DerefObjCore(bo->wrk, &bo->bereq_body);
1203
1204 2969
        if (oc->boc->state == BOS_FINISHED) {
1205 2895
                AZ(oc->flags & OC_F_FAILED);
1206 2895
                VSLb(bo->vsl, SLT_Length, "%ju",
1207 2895
                    (uintmax_t)ObjGetLen(bo->wrk, oc));
1208 2895
        }
1209
        // AZ(oc->boc); // XXX
1210
1211 2969
        if (bo->stale_oc != NULL)
1212 159
                (void)HSH_DerefObjCore(wrk, &bo->stale_oc);
1213
1214 2969
        wrk->vsl = NULL;
1215 2969
        HSH_DerefBoc(wrk, oc);
1216 2969
        SES_Rel(bo->sp);
1217 2969
        VBO_ReleaseBusyObj(wrk, &bo);
1218 2969
        THR_SetBusyobj(NULL);
1219 2969
}
1220
1221
/*--------------------------------------------------------------------
1222
 */
1223
1224
void
1225
VBF_Fetch(struct worker *wrk, struct req *req, struct objcore *oc,
1226
    struct objcore *oldoc, enum vbf_fetch_mode_e mode)
1227 2970
{
1228 2970
        enum boc_state_e state;
1229 2970
        struct boc *boc;
1230 2970
        struct busyobj *bo;
1231 2970
        enum task_prio prio;
1232 2970
        const char *how;
1233
1234 2970
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
1235 2970
        CHECK_OBJ_NOTNULL(req, REQ_MAGIC);
1236 2970
        CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC);
1237 2970
        CHECK_OBJ_ORNULL(oldoc, OBJCORE_MAGIC);
1238
1239 2970
        bo = VBO_GetBusyObj(wrk, req);
1240 2970
        CHECK_OBJ_NOTNULL(bo, BUSYOBJ_MAGIC);
1241 2970
        AN(bo->vcl);
1242
1243 2970
        init_esi_flags(bo);
1244
1245 2970
        boc = HSH_RefBoc(oc);
1246 2970
        CHECK_OBJ_NOTNULL(boc, BOC_MAGIC);
1247 2970
        assert(boc->state < BOS_STREAM);
1248 2970
        boc->transit_buffer = cache_param->transit_buffer;
1249
1250 2970
        switch (mode) {
1251 1441
        case VBF_PASS:
1252 1441
                prio = TASK_QUEUE_BO;
1253 1441
                how = "pass";
1254 1441
                bo->uncacheable = 1;
1255 1441
                break;
1256 1440
        case VBF_NORMAL:
1257 1440
                prio = TASK_QUEUE_BO;
1258 1440
                how = "fetch";
1259 1440
                break;
1260 89
        case VBF_BACKGROUND:
1261 89
                prio = TASK_QUEUE_BG;
1262 89
                how = "bgfetch";
1263 89
                bo->is_bgfetch = 1;
1264 89
                break;
1265 0
        default:
1266 0
                WRONG("Wrong fetch mode");
1267 2970
        }
1268
1269 8910
#define REQ_BEREQ_FLAG(l, r, w, d) bo->l = req->l;
1270 2970
#include "tbl/req_bereq_flags.h"
1271
1272 2970
        VSLb(bo->vsl, SLT_Begin, "bereq %ju %s", VXID(req->vsl->wid), how);
1273 2970
        VSLbs(bo->vsl, SLT_VCL_use, TOSTRAND(VCL_Name(bo->vcl)));
1274 2970
        VSLb(req->vsl, SLT_Link, "bereq %ju %s", VXID(bo->vsl->wid), how);
1275
1276 2970
        THR_SetBusyobj(bo);
1277
1278 2970
        bo->sp = req->sp;
1279 2970
        SES_Ref(bo->sp);
1280
1281 2970
        oc->boc->vary = req->vary_b;
1282 2970
        req->vary_b = NULL;
1283
1284 2970
        HSH_Ref(oc);
1285 2970
        AZ(bo->fetch_objcore);
1286 2970
        bo->fetch_objcore = oc;
1287
1288 2970
        AZ(bo->stale_oc);
1289 2970
        if (oldoc != NULL) {
1290 160
                assert(oldoc->refcnt > 0);
1291 160
                HSH_Ref(oldoc);
1292 160
                bo->stale_oc = oldoc;
1293 160
        }
1294
1295 2970
        AZ(bo->req);
1296 2970
        bo->req = req;
1297
1298 2970
        bo->fetch_task->priv = bo;
1299 2970
        bo->fetch_task->func = vbf_fetch_thread;
1300
1301 2970
        if (Pool_Task(wrk->pool, bo->fetch_task, prio)) {
1302 1
                wrk->stats->bgfetch_no_thread++;
1303 1
                VSLb(bo->vsl, SLT_FetchError,
1304 1
                    "No thread available for bgfetch");
1305 1
                (void)vbf_stp_fail(req->wrk, bo);
1306 1
                if (bo->stale_oc != NULL)
1307 1
                        (void)HSH_DerefObjCore(wrk, &bo->stale_oc);
1308 1
                HSH_DerefBoc(wrk, oc);
1309 1
                SES_Rel(bo->sp);
1310 1
                THR_SetBusyobj(NULL);
1311 1
                VBO_ReleaseBusyObj(wrk, &bo);
1312 2969
        } else {
1313 2969
                THR_SetBusyobj(NULL);
1314 2969
                bo = NULL; /* ref transferred to fetch thread */
1315 2969
                if (mode == VBF_BACKGROUND) {
1316 88
                        (void)ObjWaitState(oc, BOS_REQ_DONE);
1317 88
                        (void)VRB_Ignore(req);
1318 2881
                } else {
1319 2881
                        state = ObjWaitState(oc, BOS_STREAM);
1320 2881
                        AZ(oc->flags & OC_F_BUSY);
1321 2881
                        if (state == BOS_FAILED)
1322 28
                                AN(oc->flags & OC_F_FAILED);
1323 2881
                }
1324 2969
        }
1325 2970
        AZ(bo);
1326 2970
        VSLb_ts_req(req, "Fetch", W_TIM_real(wrk));
1327 2970
        assert(oc->boc == boc);
1328 2970
        HSH_DerefBoc(wrk, oc);
1329 2970
        if (mode == VBF_BACKGROUND)
1330 89
                (void)HSH_DerefObjCore(wrk, &oc);
1331 2970
}