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