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