varnish-cache/bin/varnishd/cache/cache_req_fsm.c
0
/*-
1
 * Copyright (c) 2006 Verdens Gang AS
2
 * Copyright (c) 2006-2017 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
 * This file contains the request-handling state engine, which is intended to
31
 * (over time) be(come) protocol agnostic.
32
 * We already use this now with ESI:includes, which are for all relevant
33
 * purposes a different "protocol"
34
 *
35
 * A special complication is the fact that we can suspend processing of
36
 * a request when hash-lookup finds a busy objhdr.
37
 *
38
 */
39
40
#include "config.h"
41
42
#include "cache_varnishd.h"
43
#include "cache_filter.h"
44
#include "cache_objhead.h"
45
#include "cache_transport.h"
46
#include "vcc_interface.h"
47
48
#include "http1/cache_http1.h"
49
#include "storage/storage.h"
50
#include "vcl.h"
51
#include "vct.h"
52
#include "vsha256.h"
53
#include "vtim.h"
54
55
#define REQ_STEPS \
56
  REQ_STEP(transport,           TRANSPORT,      ) \
57
  REQ_STEP(restart,             RESTART,        static) \
58
  REQ_STEP(recv,                RECV,           ) \
59
  REQ_STEP(pipe,                PIPE,           static) \
60
  REQ_STEP(pass,                PASS,           static) \
61
  REQ_STEP(lookup,              LOOKUP,         static) \
62
  REQ_STEP(purge,               PURGE,          static) \
63
  REQ_STEP(miss,                MISS,           static) \
64
  REQ_STEP(fetch,               FETCH,          static) \
65
  REQ_STEP(deliver,             DELIVER,        static) \
66
  REQ_STEP(vclfail,             VCLFAIL,        static) \
67
  REQ_STEP(synth,               SYNTH,          static) \
68
  REQ_STEP(transmit,            TRANSMIT,       static) \
69
  REQ_STEP(finish,              FINISH,         static)
70
71
#define REQ_STEP(l, U, priv) \
72
    static req_state_f cnt_##l; \
73
    priv const struct req_step R_STP_##U[1] = {{ \
74
        .name = "Req Step " #l, \
75
        .func = cnt_##l, \
76
    }};
77
REQ_STEPS
78
#undef REQ_STEP
79
80
/*--------------------------------------------------------------------
81
 * Handle "Expect:" and "Connection:" on incoming request
82
 */
83
84
static enum req_fsm_nxt v_matchproto_(req_state_f)
85 14621
cnt_transport(struct worker *wrk, struct req *req)
86
{
87
        const char *p;
88
89 14621
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
90 14621
        CHECK_OBJ_NOTNULL(req, REQ_MAGIC);
91 14621
        CHECK_OBJ_NOTNULL(req->http, HTTP_MAGIC);
92 14621
        CHECK_OBJ_NOTNULL(req->transport, TRANSPORT_MAGIC);
93 14621
        AN(req->req_body_status);
94
95 14621
        if (http_GetHdr(req->http, H_Expect, &p)) {
96 48
                if (!http_expect_eq(p, 100-continue)) {
97 8
                        req->doclose = SC_RX_JUNK;
98 8
                        (void)req->transport->minimal_response(req, 417);
99 8
                        wrk->stats->client_req_417++;
100 8
                        return (REQ_FSM_DONE);
101
                }
102 40
                if (req->http->protover >= 11 &&
103 40
                    req->htc->pipeline_b == NULL)       // XXX: HTTP1 vs 2 ?
104 36
                        req->want100cont = 1;
105 40
                http_Unset(req->http, H_Expect);
106 40
        }
107
108 14613
        AZ(req->err_code);
109
110 14613
        req->doclose = http_DoConnection(req->http, SC_REQ_CLOSE);
111 14613
        if (req->doclose == SC_RX_BAD) {
112 12
                wrk->stats->client_req_400++;
113 12
                (void)req->transport->minimal_response(req, 400);
114 12
                return (REQ_FSM_DONE);
115
        }
116
117 14601
        if (req->req_body_status->avail == 1) {
118 492
                AN(req->transport->req_body != NULL);
119 492
                VFP_Setup(req->vfc, wrk);
120 492
                req->vfc->resp = req->http;             // XXX
121 492
                req->transport->req_body(req);
122 492
        }
123
124 14601
        req->ws_req = WS_Snapshot(req->ws);
125 14601
        HTTP_Clone(req->http0, req->http);      // For ESI & restart
126 14601
        req->req_step = R_STP_RECV;
127 14601
        return (REQ_FSM_MORE);
128 14621
}
129
130
/*--------------------------------------------------------------------
131
 * Deliver an object to client
132
 */
133
134
int
135 12856
Resp_Setup_Deliver(struct req *req)
136
{
137
        struct http *h;
138
        struct objcore *oc;
139
        const void *hdrs;
140
141 12856
        CHECK_OBJ_NOTNULL(req, REQ_MAGIC);
142 12856
        oc = req->objcore;
143 12856
        CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC);
144
145 12856
        h = req->resp;
146
147 12856
        HTTP_Setup(h, req->ws, req->vsl, SLT_RespMethod);
148
149 12856
        hdrs = ObjGetAttr(req->wrk, oc, OA_HEADERS, NULL);
150 12856
        if (hdrs == NULL || HTTP_Decode(h, hdrs))
151 10
                return (-1);
152
153 12848
        http_ForceField(h, HTTP_HDR_PROTO, "HTTP/1.1");
154
155 12848
        if (req->is_hit)
156 8666
                http_PrintfHeader(h, "X-Varnish: %ju %ju", VXID(req->vsl->wid),
157 4333
                    VXID(ObjGetXID(req->wrk, oc)));
158
        else
159 8515
                http_PrintfHeader(h, "X-Varnish: %ju", VXID(req->vsl->wid));
160
161
        /*
162
         * We base Age calculation upon the last timestamp taken during client
163
         * request processing. This gives some inaccuracy, but since Age is only
164
         * full second resolution that shouldn't matter. (Last request timestamp
165
         * could be a Start timestamp taken before the object entered into cache
166
         * leading to negative age. Truncate to zero in that case).
167
         */
168 25696
        http_PrintfHeader(h, "Age: %.0f",
169 12848
            floor(fmax(0., req->t_prev - oc->t_origin)));
170
171 12848
        http_AppendHeader(h, H_Via, http_ViaHeader());
172
173 13788
        if (cache_param->http_gzip_support &&
174 12827
            ObjCheckFlag(req->wrk, oc, OF_GZIPED) &&
175 940
            !RFC2616_Req_Gzip(req->http))
176 304
                RFC2616_Weaken_Etag(h);
177 12848
        return (0);
178 12858
}
179
180
void
181 2212
Resp_Setup_Synth(struct req *req)
182
{
183
        struct http *h;
184
185 2212
        CHECK_OBJ_NOTNULL(req, REQ_MAGIC);
186
187 2212
        h = req->resp;
188
189 2212
        HTTP_Setup(h, req->ws, req->vsl, SLT_RespMethod);
190
191 2212
        AZ(req->objcore);
192 2212
        http_PutResponse(h, "HTTP/1.1", req->err_code, req->err_reason);
193
194 2212
        http_TimeHeader(h, "Date: ", W_TIM_real(req->wrk));
195 2212
        http_SetHeader(h, "Server: Varnish");
196 2212
        http_PrintfHeader(h, "X-Varnish: %ju", VXID(req->vsl->wid));
197
198
        /*
199
         * For late 100-continue, we suggest to VCL to close the connection to
200
         * neither send a 100-continue nor drain-read the request. But VCL has
201
         * the option to veto by removing Connection: close
202
         */
203 2212
        if (req->want100cont)
204 12
                http_SetHeader(h, "Connection: close");
205 2212
}
206
207
static enum req_fsm_nxt v_matchproto_(req_state_f)
208 12812
cnt_deliver(struct worker *wrk, struct req *req)
209
{
210
        unsigned status;
211
212 12812
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
213 12812
        CHECK_OBJ_NOTNULL(req, REQ_MAGIC);
214 12812
        CHECK_OBJ_NOTNULL(req->objcore, OBJCORE_MAGIC);
215 12812
        CHECK_OBJ_NOTNULL(req->objcore->objhead, OBJHEAD_MAGIC);
216 12812
        AZ(req->stale_oc);
217 12812
        AN(req->vcl);
218
219 12812
        assert(req->objcore->refcnt > 0);
220
221 12812
        ObjTouch(req->wrk, req->objcore, req->t_prev);
222
223 12812
        if (Resp_Setup_Deliver(req)) {
224 8
                (void)HSH_DerefObjCore(wrk, &req->objcore, HSH_RUSH_POLICY);
225 8
                req->err_code = 500;
226 8
                req->req_step = R_STP_SYNTH;
227 8
                return (REQ_FSM_MORE);
228
        }
229
230 12804
        status = http_GetStatus(req->resp);
231 12804
        if (cache_param->http_range_support && status == 200 &&
232 11408
            !(req->objcore->flags & OC_F_PRIVATE))
233 9209
                http_ForceHeader(req->resp, H_Accept_Ranges, "bytes");
234
235 12804
        req->t_resp = W_TIM_real(wrk);
236 12804
        VCL_deliver_method(req->vcl, wrk, req, NULL, NULL);
237
238 12804
        assert(req->restarts <= req->max_restarts);
239
240 12804
        if (wrk->vpi->handling != VCL_RET_DELIVER) {
241 520
                HSH_Cancel(wrk, req->objcore, NULL);
242 520
                (void)HSH_DerefObjCore(wrk, &req->objcore, HSH_RUSH_POLICY);
243 520
                http_Teardown(req->resp);
244
245 520
                switch (wrk->vpi->handling) {
246
                case VCL_RET_RESTART:
247 124
                        req->req_step = R_STP_RESTART;
248 124
                        break;
249
                case VCL_RET_FAIL:
250 364
                        req->req_step = R_STP_VCLFAIL;
251 364
                        break;
252
                case VCL_RET_SYNTH:
253 32
                        req->req_step = R_STP_SYNTH;
254 32
                        break;
255
                default:
256 0
                        WRONG("Illegal return from vcl_deliver{}");
257 0
                }
258
259 520
                return (REQ_FSM_MORE);
260
        }
261
262 12284
        VSLb_ts_req(req, "Process", W_TIM_real(wrk));
263
264 12284
        assert(wrk->vpi->handling == VCL_RET_DELIVER);
265
266 12284
        if (IS_TOPREQ(req) && RFC2616_Do_Cond(req))
267 120
                http_PutResponse(req->resp, "HTTP/1.1", 304, NULL);
268
269 12284
        req->req_step = R_STP_TRANSMIT;
270 12284
        return (REQ_FSM_MORE);
271 12812
}
272
273
/*--------------------------------------------------------------------
274
 * VCL failed, die horribly
275
 */
276
277
static enum req_fsm_nxt v_matchproto_(req_state_f)
278 632
cnt_vclfail(struct worker *wrk, struct req *req)
279
{
280
        struct vrt_ctx ctx[1];
281
282 632
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
283 632
        CHECK_OBJ_NOTNULL(req, REQ_MAGIC);
284
285 632
        AZ(req->objcore);
286 632
        AZ(req->stale_oc);
287
288 632
        INIT_OBJ(ctx, VRT_CTX_MAGIC);
289 632
        VCL_Req2Ctx(ctx, req);
290
291 632
        Req_Rollback(ctx);
292
293 632
        if (req->req_reset) {
294 92
                req->err_code = 408;
295 92
                req->err_reason = "Client disconnected";
296 92
        } else {
297 540
                req->err_code = 503;
298 540
                req->err_reason = "VCL failed";
299
        }
300 632
        req->req_step = R_STP_SYNTH;
301 632
        req->doclose = SC_VCL_FAILURE;
302 632
        req->vdp_filter_list = NULL;
303 632
        return (REQ_FSM_MORE);
304
}
305
306
/*--------------------------------------------------------------------
307
 * Emit a synthetic response
308
 */
309
310
static enum req_fsm_nxt v_matchproto_(req_state_f)
311 2204
cnt_synth(struct worker *wrk, struct req *req)
312
{
313
        struct vsb *synth_body;
314
        ssize_t sz, szl;
315
        uint16_t status;
316
        uint8_t *ptr;
317
        const char *body;
318
319 2204
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
320 2204
        CHECK_OBJ_NOTNULL(req, REQ_MAGIC);
321
322 2204
        AZ(req->objcore);
323 2204
        AZ(req->stale_oc);
324
325 2204
        wrk->stats->s_synth++;
326
327 2204
        if (req->err_code < 100)
328 0
                req->err_code = 501;
329
330 2204
        Resp_Setup_Synth(req);
331
332 2204
        req->vdp_filter_list = NULL;
333 2204
        synth_body = VSB_new_auto();
334 2204
        AN(synth_body);
335
336 2204
        req->t_resp = W_TIM_real(wrk);
337 2204
        VCL_synth_method(req->vcl, wrk, req, NULL, synth_body);
338
339 2204
        AZ(VSB_finish(synth_body));
340
341 2204
        VSLb_ts_req(req, "Process", W_TIM_real(wrk));
342
343 2204
        while (wrk->vpi->handling == VCL_RET_FAIL) {
344 108
                if (req->esi_level > 0) {
345 4
                        wrk->vpi->handling = VCL_RET_DELIVER;
346 4
                        break;
347
                }
348 104
                VSB_destroy(&synth_body);
349 104
                (void)VRB_Ignore(req);
350 104
                status = req->req_reset ? 408 : 500;
351 104
                (void)req->transport->minimal_response(req, status);
352 104
                req->doclose = SC_VCL_FAILURE; // XXX: Not necessary any more ?
353 104
                VSLb_ts_req(req, "Resp", W_TIM_real(wrk));
354 104
                http_Teardown(req->resp);
355 104
                return (REQ_FSM_DONE);
356
        }
357
358 2100
        if (wrk->vpi->handling == VCL_RET_RESTART && req->restarts > req->max_restarts)
359 4
                wrk->vpi->handling = VCL_RET_DELIVER;
360
361 2100
        if (wrk->vpi->handling == VCL_RET_RESTART) {
362
                /*
363
                 * XXX: Should we reset req->doclose = SC_VCL_FAILURE
364
                 * XXX: If so, to what ?
365
                 */
366 44
                HTTP_Setup(req->resp, req->ws, req->vsl, SLT_RespMethod);
367 44
                VSB_destroy(&synth_body);
368 44
                req->req_step = R_STP_RESTART;
369 44
                return (REQ_FSM_MORE);
370
        }
371 2056
        assert(wrk->vpi->handling == VCL_RET_DELIVER);
372
373 2056
        http_Unset(req->resp, H_Content_Length);
374 4112
        http_PrintfHeader(req->resp, "Content-Length: %zd",
375 2056
            VSB_len(synth_body));
376
377 2056
        if (req->doclose == SC_NULL &&
378 1499
            http_HdrIs(req->resp, H_Connection, "close"))
379 12
                req->doclose = SC_RESP_CLOSE;
380
381
        /* Discard any lingering request body before delivery */
382 2056
        (void)VRB_Ignore(req);
383
384 2056
        req->objcore = HSH_Private(wrk);
385 2056
        CHECK_OBJ_NOTNULL(req->objcore, OBJCORE_MAGIC);
386 2056
        szl = -1;
387 2056
        if (STV_NewObject(wrk, req->objcore, stv_transient, 0)) {
388 2048
                body = VSB_data(synth_body);
389 2048
                szl = VSB_len(synth_body);
390 2048
                assert(szl >= 0);
391 3972
                while (szl > 0) {
392 1924
                        sz = szl;
393 1924
                        if (! ObjGetSpace(wrk, req->objcore, &sz, &ptr)) {
394 0
                                szl = -1;
395 0
                                break;
396
                        }
397 1924
                        if (sz > szl)
398 0
                                sz = szl;
399 1924
                        szl -= sz;
400 1924
                        memcpy(ptr, body, sz);
401 1924
                        ObjExtend(wrk, req->objcore, sz, szl == 0 ? 1 : 0);
402 1924
                        body += sz;
403
                }
404 2048
        }
405
406 2056
        if (szl >= 0)
407 2048
                AZ(ObjSetU64(wrk, req->objcore, OA_LEN, VSB_len(synth_body)));
408 2056
        HSH_DerefBoc(wrk, req->objcore);
409 2056
        VSB_destroy(&synth_body);
410
411 2056
        if (szl < 0) {
412 8
                VSLb(req->vsl, SLT_Error, "Could not get storage");
413 8
                req->doclose = SC_OVERLOAD;
414 8
                VSLb_ts_req(req, "Resp", W_TIM_real(wrk));
415 8
                (void)HSH_DerefObjCore(wrk, &req->objcore, 1);
416 8
                http_Teardown(req->resp);
417 8
                return (REQ_FSM_DONE);
418
        }
419
420 2048
        req->req_step = R_STP_TRANSMIT;
421 2048
        return (REQ_FSM_MORE);
422 2204
}
423
424
/*--------------------------------------------------------------------
425
 * The mechanics of sending a response (from deliver or synth)
426
 */
427
428
static enum req_fsm_nxt v_matchproto_(req_state_f)
429 14335
cnt_transmit(struct worker *wrk, struct req *req)
430
{
431 14335
        enum req_fsm_nxt nxt = REQ_FSM_MORE;
432
        enum vtr_deliver_e dnxt;
433
        uint16_t status;
434
        int sendbody, head;
435
        intmax_t clval;
436
437 14335
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
438 14335
        CHECK_OBJ_NOTNULL(req, REQ_MAGIC);
439 14335
        CHECK_OBJ_NOTNULL(req->transport, TRANSPORT_MAGIC);
440 14335
        CHECK_OBJ_NOTNULL(req->objcore, OBJCORE_MAGIC);
441 14335
        AZ(req->stale_oc);
442 14335
        AZ(req->res_pipe | req->res_esi);
443 14335
        AZ(req->boc);
444 14335
        req->req_step = R_STP_FINISH;
445
446
        /* Grab a ref to the bo if there is one (=streaming) */
447 14335
        req->boc = HSH_RefBoc(req->objcore);
448 14335
        if (req->boc && req->boc->state < BOS_STREAM)
449 0
                ObjWaitState(req->objcore, BOS_STREAM);
450 14335
        clval = http_GetContentLength(req->resp);
451
        /* RFC 7230, 3.3.3 */
452 14335
        status = http_GetStatus(req->resp);
453 14335
        head = http_method_eq(req->http0->hd[HTTP_HDR_METHOD].b, HEAD);
454
455 14335
        if (req->boc != NULL || (req->objcore->flags & (OC_F_FAILED)))
456 3384
                req->resp_len = clval;
457
        else
458 10951
                req->resp_len = ObjGetLen(req->wrk, req->objcore);
459
460 14335
        if (head || status < 200 || status == 204 || status == 304) {
461
                // rfc7230,l,1748,1752
462 651
                sendbody = 0;
463 651
        } else {
464 13684
                sendbody = 1;
465
        }
466
467 14335
        VDP_Init(req->vdc, req->wrk, req->vsl, req, NULL, &req->resp_len);
468 14335
        if (req->vdp_filter_list == NULL)
469 13267
                req->vdp_filter_list = resp_Get_Filter_List(req);
470 14335
        if (req->vdp_filter_list == NULL ||
471 14336
            VCL_StackVDP(req->vdc, req->vcl, req->vdp_filter_list, req, NULL)) {
472 254
                VSLb(req->vsl, SLT_Error, "Failure to push processors");
473 254
                req->doclose = SC_OVERLOAD;
474 254
                req->acct.resp_bodybytes +=
475 254
                        VDP_Close(req->vdc, req->objcore, req->boc);
476 254
        } else {
477 14085
                if (status < 200 || status == 204) {
478
                        // rfc7230,l,1691,1695
479 81
                        http_Unset(req->resp, H_Content_Length);
480 14085
                } else if (status == 304) {
481
                        // rfc7230,l,1675,1677
482 136
                        http_Unset(req->resp, H_Content_Length);
483 14004
                } else if (clval >= 0 && clval == req->resp_len) {
484
                        /* Reuse C-L header */
485 13868
                } else if (head && req->objcore->flags & OC_F_HFM) {
486
                        /*
487
                         * Don't touch C-L header (debatable)
488
                         *
489
                         * The only way to do it correctly would be to GET
490
                         * to the backend, and discard the body once the
491
                         * filters have had a chance to chew on it, but that
492
                         * would negate the "pass for huge objects" use case.
493
                         */
494 4
                } else {
495 3511
                        http_Unset(req->resp, H_Content_Length);
496 3511
                        if (req->resp_len >= 0)
497 4134
                                http_PrintfHeader(req->resp,
498 2067
                                    "Content-Length: %jd", req->resp_len);
499
                }
500 14085
                if (req->resp_len == 0)
501 4065
                        sendbody = 0;
502 14085
                dnxt = req->transport->deliver(req, sendbody);
503 14085
                if (dnxt == VTR_D_DISEMBARK)
504 128
                        nxt = REQ_FSM_DISEMBARK;
505
                else
506 13957
                        assert(dnxt == VTR_D_DONE);
507
        }
508 14339
        return (nxt);
509
}
510
511
static enum req_fsm_nxt v_matchproto_(req_state_f)
512 14333
cnt_finish(struct worker *wrk, struct req *req)
513
{
514
515 14333
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
516 14333
        CHECK_OBJ_NOTNULL(req, REQ_MAGIC);
517
518 14333
        VSLb_ts_req(req, "Resp", W_TIM_real(wrk));
519
520 14333
        if (req->doclose == SC_NULL && (req->objcore->flags & OC_F_FAILED)) {
521
                /* The object we delivered failed due to a streaming error.
522
                 * Fail the request. */
523 56
                req->doclose = SC_TX_ERROR;
524 56
        }
525
526 14333
        if (req->boc != NULL) {
527 3382
                HSH_DerefBoc(wrk, req->objcore);
528 3382
                req->boc = NULL;
529 3382
        }
530
531 14333
        (void)HSH_DerefObjCore(wrk, &req->objcore, HSH_RUSH_POLICY);
532 14333
        http_Teardown(req->resp);
533
534 14333
        req->vdp_filter_list = NULL;
535 14333
        req->res_pipe = 0;
536 14333
        req->res_esi = 0;
537 14333
        return (REQ_FSM_DONE);
538
}
539
540
/*--------------------------------------------------------------------
541
 * Initiated a fetch (pass/miss) which we intend to deliver
542
 */
543
544
static enum req_fsm_nxt v_matchproto_(req_state_f)
545 8596
cnt_fetch(struct worker *wrk, struct req *req)
546
{
547
548 8596
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
549 8596
        CHECK_OBJ_NOTNULL(req, REQ_MAGIC);
550 8596
        CHECK_OBJ_NOTNULL(req->objcore, OBJCORE_MAGIC);
551 8596
        AZ(req->stale_oc);
552
553 8596
        wrk->stats->s_fetch++;
554 8596
        (void)VRB_Ignore(req);
555
556 8596
        if (req->objcore->flags & OC_F_FAILED) {
557 104
                req->err_code = 503;
558 104
                req->req_step = R_STP_SYNTH;
559 104
                (void)HSH_DerefObjCore(wrk, &req->objcore, 1);
560 104
                AZ(req->objcore);
561 104
                return (REQ_FSM_MORE);
562
        }
563
564 8492
        req->req_step = R_STP_DELIVER;
565 8492
        return (REQ_FSM_MORE);
566 8596
}
567
568
/*--------------------------------------------------------------------
569
 * Attempt to lookup objhdr from hash.  We disembark and reenter
570
 * this state if we get suspended on a busy objhdr.
571
 */
572
573
static enum req_fsm_nxt v_matchproto_(req_state_f)
574 10252
cnt_lookup(struct worker *wrk, struct req *req)
575
{
576
        struct objcore *oc, *busy;
577
        enum lookup_e lr;
578 10252
        int had_objhead = 0;
579
580 10252
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
581 10252
        CHECK_OBJ_NOTNULL(req, REQ_MAGIC);
582 10252
        AZ(req->objcore);
583 10252
        AZ(req->stale_oc);
584
585 10252
        AN(req->vcl);
586
587 10252
        VRY_Prep(req);
588
589 10252
        AZ(req->objcore);
590 10252
        if (req->hash_objhead)
591 192
                had_objhead = 1;
592 10252
        wrk->strangelove = 0;
593 10252
        lr = HSH_Lookup(req, &oc, &busy);
594 10252
        if (lr == HSH_BUSY) {
595
                /*
596
                 * We lost the session to a busy object, disembark the
597
                 * worker thread.   We return to STP_LOOKUP when the busy
598
                 * object has been unbusied, and still have the objhead
599
                 * around to restart the lookup with.
600
                 */
601 192
                return (REQ_FSM_DISEMBARK);
602
        }
603 10060
        assert(wrk->strangelove >= 0);
604 10060
        if ((unsigned)wrk->strangelove >= cache_param->vary_notice)
605 496
                VSLb(req->vsl, SLT_Notice, "vsl: High number of variants (%d)",
606 248
                    wrk->strangelove);
607 10060
        if (had_objhead)
608 182
                VSLb_ts_req(req, "Waitinglist", W_TIM_real(wrk));
609
610 10060
        if (req->vcf != NULL) {
611 24
                (void)req->vcf->func(req, NULL, NULL, 2);
612 24
                req->vcf = NULL;
613 24
        }
614
615 10060
        if (busy == NULL) {
616 4125
                VRY_Finish(req, DISCARD);
617 4125
        } else {
618 5935
                AN(busy->flags & OC_F_BUSY);
619 5935
                VRY_Finish(req, KEEP);
620
        }
621
622 10060
        AZ(req->objcore);
623 10060
        if (lr == HSH_MISS || lr == HSH_HITMISS) {
624 5567
                AN(busy);
625 5567
                AN(busy->flags & OC_F_BUSY);
626 5567
                req->objcore = busy;
627 5567
                req->stale_oc = oc;
628 5567
                req->req_step = R_STP_MISS;
629 5567
                if (lr == HSH_HITMISS)
630 148
                        req->is_hitmiss = 1;
631 5567
                return (REQ_FSM_MORE);
632
        }
633 4493
        if (lr == HSH_HITPASS) {
634 44
                AZ(busy);
635 44
                AZ(oc);
636 44
                req->req_step = R_STP_PASS;
637 44
                req->is_hitpass = 1;
638 44
                return (REQ_FSM_MORE);
639
        }
640
641 4449
        assert(lr == HSH_HIT || lr == HSH_GRACE);
642
643 4449
        CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC);
644 4449
        AZ(oc->flags & OC_F_BUSY);
645 4449
        req->objcore = oc;
646 4449
        AZ(oc->flags & OC_F_HFM);
647
648 4449
        VCL_hit_method(req->vcl, wrk, req, NULL, NULL);
649
650 4449
        switch (wrk->vpi->handling) {
651
        case VCL_RET_DELIVER:
652 4329
                if (busy != NULL) {
653 340
                        AZ(oc->flags & OC_F_HFM);
654 340
                        CHECK_OBJ_NOTNULL(busy->boc, BOC_MAGIC);
655
                        // XXX: shouldn't we go to miss?
656 340
                        VBF_Fetch(wrk, req, busy, oc, VBF_BACKGROUND);
657 340
                        wrk->stats->s_fetch++;
658 340
                        wrk->stats->s_bgfetch++;
659 340
                } else {
660 3989
                        (void)VRB_Ignore(req);// XXX: handle err
661
                }
662 4329
                wrk->stats->cache_hit++;
663 4329
                req->is_hit = 1;
664 4329
                if (lr == HSH_GRACE)
665 352
                        wrk->stats->cache_hit_grace++;
666 4329
                req->req_step = R_STP_DELIVER;
667 4329
                return (REQ_FSM_MORE);
668
        case VCL_RET_RESTART:
669 56
                req->req_step = R_STP_RESTART;
670 56
                break;
671
        case VCL_RET_FAIL:
672 20
                req->req_step = R_STP_VCLFAIL;
673 20
                break;
674
        case VCL_RET_SYNTH:
675 36
                req->req_step = R_STP_SYNTH;
676 36
                break;
677
        case VCL_RET_PASS:
678 8
                wrk->stats->cache_hit++;
679 8
                req->is_hit = 1;
680 8
                req->req_step = R_STP_PASS;
681 8
                break;
682
        default:
683 0
                WRONG("Illegal return from vcl_hit{}");
684 0
        }
685
686
        /* Drop our object, we won't need it */
687 120
        (void)HSH_DerefObjCore(wrk, &req->objcore, HSH_RUSH_POLICY);
688
689 120
        if (busy != NULL) {
690 28
                (void)HSH_DerefObjCore(wrk, &busy, 0);
691 28
                VRY_Clear(req);
692 28
        }
693
694 120
        return (REQ_FSM_MORE);
695 10252
}
696
697
/*--------------------------------------------------------------------
698
 * Cache miss.
699
 */
700
701
static enum req_fsm_nxt v_matchproto_(req_state_f)
702 5562
cnt_miss(struct worker *wrk, struct req *req)
703
{
704
705 5562
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
706 5562
        CHECK_OBJ_NOTNULL(req, REQ_MAGIC);
707 5562
        AN(req->vcl);
708 5562
        CHECK_OBJ_NOTNULL(req->objcore, OBJCORE_MAGIC);
709 5562
        CHECK_OBJ_ORNULL(req->stale_oc, OBJCORE_MAGIC);
710
711 5562
        VCL_miss_method(req->vcl, wrk, req, NULL, NULL);
712 5562
        switch (wrk->vpi->handling) {
713
        case VCL_RET_FETCH:
714 5487
                wrk->stats->cache_miss++;
715 5487
                VBF_Fetch(wrk, req, req->objcore, req->stale_oc, VBF_NORMAL);
716 5487
                if (req->stale_oc != NULL)
717 240
                        (void)HSH_DerefObjCore(wrk, &req->stale_oc, 0);
718 5487
                req->req_step = R_STP_FETCH;
719 5487
                return (REQ_FSM_MORE);
720
        case VCL_RET_FAIL:
721 15
                req->req_step = R_STP_VCLFAIL;
722 15
                break;
723
        case VCL_RET_SYNTH:
724 32
                req->req_step = R_STP_SYNTH;
725 32
                break;
726
        case VCL_RET_RESTART:
727 16
                req->req_step = R_STP_RESTART;
728 16
                break;
729
        case VCL_RET_PASS:
730 12
                req->req_step = R_STP_PASS;
731 12
                break;
732
        default:
733 0
                WRONG("Illegal return from vcl_miss{}");
734 0
        }
735 75
        VRY_Clear(req);
736 75
        if (req->stale_oc != NULL)
737 4
                (void)HSH_DerefObjCore(wrk, &req->stale_oc, 0);
738 75
        AZ(HSH_DerefObjCore(wrk, &req->objcore, 1));
739 75
        return (REQ_FSM_MORE);
740 5562
}
741
742
/*--------------------------------------------------------------------
743
 * Pass processing
744
 */
745
746
static enum req_fsm_nxt v_matchproto_(req_state_f)
747 3116
cnt_pass(struct worker *wrk, struct req *req)
748
{
749
750 3116
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
751 3116
        CHECK_OBJ_NOTNULL(req, REQ_MAGIC);
752 3116
        AN(req->vcl);
753 3116
        AZ(req->objcore);
754 3116
        AZ(req->stale_oc);
755
756 3116
        VCL_pass_method(req->vcl, wrk, req, NULL, NULL);
757 3116
        switch (wrk->vpi->handling) {
758
        case VCL_RET_FAIL:
759 4
                req->req_step = R_STP_VCLFAIL;
760 4
                break;
761
        case VCL_RET_SYNTH:
762 4
                req->req_step = R_STP_SYNTH;
763 4
                break;
764
        case VCL_RET_RESTART:
765 0
                req->req_step = R_STP_RESTART;
766 0
                break;
767
        case VCL_RET_FETCH:
768 3108
                wrk->stats->s_pass++;
769 3108
                req->objcore = HSH_Private(wrk);
770 3108
                CHECK_OBJ_NOTNULL(req->objcore, OBJCORE_MAGIC);
771 3108
                VBF_Fetch(wrk, req, req->objcore, NULL, VBF_PASS);
772 3108
                req->req_step = R_STP_FETCH;
773 3108
                break;
774
        default:
775 0
                WRONG("Illegal return from cnt_pass{}");
776 0
        }
777 3116
        return (REQ_FSM_MORE);
778
}
779
780
/*--------------------------------------------------------------------
781
 * Pipe mode
782
 */
783
784
static enum req_fsm_nxt v_matchproto_(req_state_f)
785 124
cnt_pipe(struct worker *wrk, struct req *req)
786
{
787
        struct busyobj *bo;
788
        enum req_fsm_nxt nxt;
789
790 124
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
791 124
        CHECK_OBJ_NOTNULL(req, REQ_MAGIC);
792 124
        AZ(req->objcore);
793 124
        AZ(req->stale_oc);
794 124
        AN(req->vcl);
795
796 124
        wrk->stats->s_pipe++;
797 124
        bo = VBO_GetBusyObj(wrk, req);
798 124
        CHECK_OBJ_NOTNULL(bo, BUSYOBJ_MAGIC);
799 124
        VSLb(bo->vsl, SLT_Begin, "bereq %ju pipe", VXID(req->vsl->wid));
800 124
        VSLb(req->vsl, SLT_Link, "bereq %ju pipe", VXID(bo->vsl->wid));
801 124
        VSLb_ts_busyobj(bo, "Start", W_TIM_real(wrk));
802 124
        THR_SetBusyobj(bo);
803 124
        bo->sp = req->sp;
804 124
        SES_Ref(bo->sp);
805
806 124
        HTTP_Setup(bo->bereq, req->ws, bo->vsl, SLT_BereqMethod);
807 124
        http_FilterReq(bo->bereq, req->http, 0);        // XXX: 0 ?
808 124
        http_PrintfHeader(bo->bereq, "X-Varnish: %ju", VXID(req->vsl->wid));
809 124
        http_ForceHeader(bo->bereq, H_Connection, "close");
810
811 124
        if (req->want100cont) {
812 0
                http_SetHeader(bo->bereq, "Expect: 100-continue");
813 0
                req->want100cont = 0;
814 0
        }
815
816 124
        bo->wrk = wrk;
817 124
        bo->task_deadline = NAN; /* XXX: copy req->task_deadline */
818 124
        if (WS_Overflowed(req->ws))
819 4
                wrk->vpi->handling = VCL_RET_FAIL;
820
        else
821 120
                VCL_pipe_method(req->vcl, wrk, req, bo, NULL);
822
823 124
        switch (wrk->vpi->handling) {
824
        case VCL_RET_SYNTH:
825 12
                req->req_step = R_STP_SYNTH;
826 12
                nxt = REQ_FSM_MORE;
827 12
                break;
828
        case VCL_RET_PIPE:
829 104
                VSLb_ts_req(req, "Process", W_TIM_real(wrk));
830 104
                VSLb_ts_busyobj(bo, "Process", wrk->lastused);
831 104
                if (V1P_Enter() == 0) {
832 100
                        AZ(bo->req);
833 100
                        bo->req = req;
834 100
                        bo->wrk = wrk;
835
                        /* Unless cached, reqbody is not our job */
836 100
                        if (req->req_body_status != BS_CACHED)
837 96
                                req->req_body_status = BS_NONE;
838 100
                        SES_Close(req->sp, VDI_Http1Pipe(req, bo));
839 100
                        nxt = REQ_FSM_DONE;
840 100
                        V1P_Leave();
841 100
                        break;
842
                }
843 4
                wrk->stats->pipe_limited++;
844
                /* fall through */
845
        case VCL_RET_FAIL:
846 12
                req->req_step = R_STP_VCLFAIL;
847 12
                nxt = REQ_FSM_MORE;
848 12
                break;
849
        default:
850 0
                WRONG("Illegal return from vcl_pipe{}");
851 0
        }
852 124
        http_Teardown(bo->bereq);
853 124
        SES_Rel(bo->sp);
854 124
        VBO_ReleaseBusyObj(wrk, &bo);
855 124
        THR_SetBusyobj(NULL);
856 124
        return (nxt);
857
}
858
859
/*--------------------------------------------------------------------
860
 * Handle restart events
861
 */
862
863
static enum req_fsm_nxt v_matchproto_(req_state_f)
864 288
cnt_restart(struct worker *wrk, struct req *req)
865
{
866
867 288
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
868 288
        CHECK_OBJ_NOTNULL(req, REQ_MAGIC);
869 288
        AZ(req->objcore);
870 288
        AZ(req->stale_oc);
871
872 288
        if (++req->restarts > req->max_restarts) {
873 40
                VSLb(req->vsl, SLT_VCL_Error, "Too many restarts");
874 40
                req->err_code = 503;
875 40
                req->req_step = R_STP_SYNTH;
876 40
        } else {
877
                // XXX: ReqEnd + ReqAcct ?
878 248
                VSLb_ts_req(req, "Restart", W_TIM_real(wrk));
879 496
                VSL_ChgId(req->vsl, "req", "restart",
880 248
                    VXID_Get(wrk, VSL_CLIENTMARKER));
881 248
                VSLb_ts_req(req, "Start", req->t_prev);
882 248
                req->err_code = 0;
883 248
                req->req_step = R_STP_RECV;
884
        }
885 288
        return (REQ_FSM_MORE);
886
}
887
888
/*
889
 * prepare the request for vcl_recv, either initially or after a reset
890
 * e.g. due to vcl switching
891
 *
892
 * TODO
893
 * - make restarts == 0 bit re-usable for rollback
894
 * - remove duplication with Req_Cleanup()
895
 */
896
897
static void v_matchproto_(req_state_f)
898 14889
cnt_recv_prep(struct req *req, const char *ci)
899
{
900
901 14889
        if (req->restarts == 0) {
902
                /*
903
                 * This really should be done earlier, but we want to capture
904
                 * it in the VSL log.
905
                 */
906 14641
                http_AppendHeader(req->http, H_X_Forwarded_For, ci);
907 14641
                http_AppendHeader(req->http, H_Via, http_ViaHeader());
908 14641
                http_CollectHdr(req->http, H_Cache_Control);
909
910
                /* By default we use the first backend */
911 29282
                VRT_Assign_Backend(&req->director_hint,
912 14641
                    VCL_DefaultDirector(req->vcl));
913
914 14641
                req->d_ttl = -1;
915 14641
                req->d_grace = -1;
916 14641
                req->disable_esi = 0;
917 14641
                req->hash_always_miss = 0;
918 14641
                req->hash_ignore_busy = 0;
919 14641
                req->hash_ignore_vary = 0;
920 14641
                req->client_identity = NULL;
921 14641
                req->storage = NULL;
922 14641
                req->trace = FEATURE(FEATURE_TRACE);
923 14641
        }
924
925 14889
        req->is_hit = 0;
926 14889
        req->is_hitmiss = 0;
927 14889
        req->is_hitpass = 0;
928 14889
        req->err_code = 0;
929 14889
        req->err_reason = NULL;
930
931 14889
        req->vfp_filter_list = NULL;
932 14889
}
933
934
/*--------------------------------------------------------------------
935
 * We have a complete request, set everything up and start it.
936
 * We can come here both with a request from the client and with
937
 * a interior request during ESI delivery.
938
 */
939
940
static enum req_fsm_nxt v_matchproto_(req_state_f)
941 14830
cnt_recv(struct worker *wrk, struct req *req)
942
{
943
        unsigned recv_handling;
944
        struct VSHA256Context sha256ctx;
945
        const char *ci;
946
947 14830
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
948 14830
        CHECK_OBJ_NOTNULL(req, REQ_MAGIC);
949 14830
        AN(req->vcl);
950 14830
        AZ(req->objcore);
951 14830
        AZ(req->stale_oc);
952 14830
        AZ(req->err_code);
953
954 14830
        AZ(isnan(req->t_first));
955 14830
        AZ(isnan(req->t_prev));
956 14830
        AZ(isnan(req->t_req));
957
958 14830
        ci = Req_LogStart(wrk, req);
959 14830
        http_VSL_log(req->http);
960
961 14830
        if (http_CountHdr(req->http0, H_Host) > 1) {
962 8
                VSLb(req->vsl, SLT_BogoHeader, "Multiple Host: headers");
963 8
                wrk->stats->client_req_400++;
964 8
                (void)req->transport->minimal_response(req, 400);
965 8
                return (REQ_FSM_DONE);
966
        }
967
968 14822
        if (http_CountHdr(req->http0, H_Content_Length) > 1) {
969 4
                VSLb(req->vsl, SLT_BogoHeader, "Multiple Content-Length: headers");
970 4
                wrk->stats->client_req_400++;
971 4
                (void)req->transport->minimal_response(req, 400);
972 4
                return (REQ_FSM_DONE);
973
        }
974
975 14818
        cnt_recv_prep(req, ci);
976
977 14818
        if (req->req_body_status == BS_ERROR) {
978 0
                req->doclose = SC_OVERLOAD;
979 0
                return (REQ_FSM_DONE);
980
        }
981
982 14818
        VCL_recv_method(req->vcl, wrk, req, NULL, NULL);
983
984 14818
        if (wrk->vpi->handling == VCL_RET_FAIL) {
985 189
                req->req_step = R_STP_VCLFAIL;
986 189
                return (REQ_FSM_MORE);
987
        }
988
989 14629
        if (wrk->vpi->handling == VCL_RET_VCL && req->restarts == 0) {
990
                // Req_Rollback has happened in VPI_vcl_select
991 52
                assert(WS_Snapshot(req->ws) == req->ws_req);
992 52
                cnt_recv_prep(req, ci);
993 52
                VCL_recv_method(req->vcl, wrk, req, NULL, NULL);
994 52
        }
995
996 14629
        if (req->want100cont && !req->late100cont) {
997 16
                req->want100cont = 0;
998 16
                if (req->transport->minimal_response(req, 100)) {
999 0
                        req->doclose = SC_REM_CLOSE;
1000 0
                        return (REQ_FSM_DONE);
1001
                }
1002 16
        }
1003
1004
        /* Attempts to cache req.body may fail */
1005 14629
        if (req->req_body_status == BS_ERROR) {
1006 16
                req->doclose = SC_RX_BODY;
1007 16
                return (REQ_FSM_DONE);
1008
        }
1009
1010 14613
        recv_handling = wrk->vpi->handling;
1011
1012
        /* We wash the A-E header here for the sake of VRY */
1013 29077
        if (cache_param->http_gzip_support &&
1014 14596
             (recv_handling != VCL_RET_PIPE) &&
1015 14464
             (recv_handling != VCL_RET_PASS)) {
1016 11420
                if (RFC2616_Req_Gzip(req->http)) {
1017 816
                        http_ForceHeader(req->http, H_Accept_Encoding, "gzip");
1018 816
                } else {
1019 10604
                        http_Unset(req->http, H_Accept_Encoding);
1020
                }
1021 11420
        }
1022
1023 14613
        VSHA256_Init(&sha256ctx);
1024 14613
        VCL_hash_method(req->vcl, wrk, req, NULL, &sha256ctx);
1025 14613
        if (wrk->vpi->handling == VCL_RET_FAIL)
1026 22
                recv_handling = wrk->vpi->handling;
1027
        else
1028 14591
                assert(wrk->vpi->handling == VCL_RET_LOOKUP);
1029 14613
        VSHA256_Final(req->digest, &sha256ctx);
1030
1031 14613
        switch (recv_handling) {
1032
        case VCL_RET_VCL:
1033 32
                VSLb(req->vsl, SLT_VCL_Error,
1034
                    "Illegal return(vcl): %s",
1035 16
                    req->restarts ? "Not after restarts" :
1036
                    "Only from active VCL");
1037 16
                req->err_code = 503;
1038 16
                req->req_step = R_STP_SYNTH;
1039 16
                break;
1040
        case VCL_RET_PURGE:
1041 28
                req->req_step = R_STP_PURGE;
1042 28
                break;
1043
        case VCL_RET_HASH:
1044 10058
                req->req_step = R_STP_LOOKUP;
1045 10058
                break;
1046
        case VCL_RET_PIPE:
1047 132
                if (!IS_TOPREQ(req)) {
1048 0
                        VSLb(req->vsl, SLT_VCL_Error,
1049
                            "vcl_recv{} returns pipe for ESI included object."
1050
                            "  Doing pass.");
1051 0
                        req->req_step = R_STP_PASS;
1052 132
                } else if (req->http0->protover > 11) {
1053 8
                        VSLb(req->vsl, SLT_VCL_Error,
1054
                            "vcl_recv{} returns pipe for HTTP/2 request."
1055
                            "  Doing pass.");
1056 8
                        req->req_step = R_STP_PASS;
1057 8
                } else {
1058 124
                        req->req_step = R_STP_PIPE;
1059
                }
1060 132
                break;
1061
        case VCL_RET_PASS:
1062 3044
                req->req_step = R_STP_PASS;
1063 3044
                break;
1064
        case VCL_RET_SYNTH:
1065 1275
                req->req_step = R_STP_SYNTH;
1066 1275
                break;
1067
        case VCL_RET_RESTART:
1068 36
                req->req_step = R_STP_RESTART;
1069 36
                break;
1070
        case VCL_RET_FAIL:
1071 24
                req->req_step = R_STP_VCLFAIL;
1072 24
                break;
1073
        default:
1074 0
                WRONG("Illegal return from vcl_recv{}");
1075 0
        }
1076 14613
        return (REQ_FSM_MORE);
1077 14830
}
1078
1079
/*--------------------------------------------------------------------
1080
 * Find the objhead, purge it.
1081
 *
1082
 * In VCL, a restart is necessary to get a new object
1083
 */
1084
1085
static enum req_fsm_nxt v_matchproto_(req_state_f)
1086 28
cnt_purge(struct worker *wrk, struct req *req)
1087
{
1088
        struct objcore *oc, *boc;
1089
        enum lookup_e lr;
1090
1091 28
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
1092 28
        CHECK_OBJ_NOTNULL(req, REQ_MAGIC);
1093 28
        AZ(req->objcore);
1094 28
        AZ(req->stale_oc);
1095
1096 28
        AN(req->vcl);
1097
1098 28
        VRY_Prep(req);
1099
1100 28
        AZ(req->objcore);
1101 28
        req->hash_always_miss = 1;
1102 28
        lr = HSH_Lookup(req, &oc, &boc);
1103 28
        assert (lr == HSH_MISS);
1104 28
        AZ(oc);
1105 28
        CHECK_OBJ_NOTNULL(boc, OBJCORE_MAGIC);
1106 28
        VRY_Finish(req, DISCARD);
1107
1108 28
        (void)HSH_Purge(wrk, boc->objhead, req->t_req, 0, 0, 0);
1109
1110 28
        AZ(HSH_DerefObjCore(wrk, &boc, 1));
1111
1112 28
        VCL_purge_method(req->vcl, wrk, req, NULL, NULL);
1113 28
        switch (wrk->vpi->handling) {
1114
        case VCL_RET_RESTART:
1115 12
                req->req_step = R_STP_RESTART;
1116 12
                break;
1117
        case VCL_RET_FAIL:
1118 4
                req->req_step = R_STP_VCLFAIL;
1119 4
                break;
1120
        case VCL_RET_SYNTH:
1121 12
                req->req_step = R_STP_SYNTH;
1122 12
                break;
1123
        default:
1124 0
                WRONG("Illegal return from vcl_purge{}");
1125 0
        }
1126 28
        return (REQ_FSM_MORE);
1127
}
1128
1129
/*--------------------------------------------------------------------
1130
 * Central state engine dispatcher.
1131
 *
1132
 * Kick the session around until it has had enough.
1133
 *
1134
 */
1135
1136
static void v_matchproto_(req_state_f)
1137 812
cnt_diag(struct req *req, const char *state)
1138
{
1139
1140 812
        CHECK_OBJ_NOTNULL(req, REQ_MAGIC);
1141
1142 1624
        VSLb(req->vsl,  SLT_Debug, "vxid %ju STP_%s sp %p vcl %p",
1143 812
            VXID(req->vsl->wid), state, req->sp, req->vcl);
1144 812
        VSL_Flush(req->vsl, 0);
1145 812
}
1146
1147
void
1148 15069
CNT_Embark(struct worker *wrk, struct req *req)
1149
{
1150
1151 15069
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
1152 15069
        CHECK_OBJ_NOTNULL(req, REQ_MAGIC);
1153
1154
        /* wrk can have changed for restarts */
1155 15069
        req->vfc->wrk = req->wrk = wrk;
1156 15069
        wrk->vsl = req->vsl;
1157 15069
        if (req->req_step == R_STP_TRANSPORT && req->vcl == NULL) {
1158 13151
                VCL_Refresh(&wrk->wpriv->vcl);
1159 13151
                req->vcl = wrk->wpriv->vcl;
1160 13151
                wrk->wpriv->vcl = NULL;
1161 13151
                VSLbs(req->vsl, SLT_VCL_use, TOSTRAND(VCL_Name(req->vcl)));
1162 13151
        }
1163
1164 15069
        AN(req->vcl);
1165 15069
}
1166
1167
enum req_fsm_nxt
1168 14941
CNT_Request(struct req *req)
1169
{
1170
        struct vrt_ctx ctx[1];
1171
        struct worker *wrk;
1172
        enum req_fsm_nxt nxt;
1173
1174 14941
        CHECK_OBJ_NOTNULL(req, REQ_MAGIC);
1175
1176 14941
        wrk = req->wrk;
1177 14941
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
1178
1179 14941
        CHECK_OBJ_NOTNULL(req->transport, TRANSPORT_MAGIC);
1180 14941
        AN(req->transport->deliver);
1181 14941
        AN(req->transport->minimal_response);
1182
1183
        /*
1184
         * Possible entrance states
1185
         */
1186 14941
        assert(
1187
            req->req_step == R_STP_LOOKUP ||
1188
            req->req_step == R_STP_FINISH ||
1189
            req->req_step == R_STP_TRANSPORT);
1190
1191 14941
        AN(VXID_TAG(req->vsl->wid) & VSL_CLIENTMARKER);
1192 14941
        AN(req->vcl);
1193
1194 116672
        for (nxt = REQ_FSM_MORE; nxt == REQ_FSM_MORE; ) {
1195
                /*
1196
                 * This is a good place to be paranoid about the various
1197
                 * pointers still pointing to the things we expect.
1198
                 */
1199 101731
                CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
1200 101731
                CHECK_OBJ_NOTNULL(wrk->wpriv, WORKER_PRIV_MAGIC);
1201 101731
                CHECK_OBJ_ORNULL(wrk->wpriv->nobjhead, OBJHEAD_MAGIC);
1202 101731
                CHECK_OBJ_NOTNULL(req, REQ_MAGIC);
1203 101731
                CHECK_OBJ_NOTNULL(req->doclose, STREAM_CLOSE_MAGIC);
1204
1205 101731
                AN(req->req_step);
1206 101731
                AN(req->req_step->name);
1207 101731
                AN(req->req_step->func);
1208 101731
                if (DO_DEBUG(DBG_REQ_STATE))
1209 812
                        cnt_diag(req, req->req_step->name);
1210 101731
                nxt = req->req_step->func(wrk, req);
1211 101731
                CHECK_OBJ_ORNULL(wrk->wpriv->nobjhead, OBJHEAD_MAGIC);
1212
        }
1213 14941
        wrk->vsl = NULL;
1214 14941
        if (nxt == REQ_FSM_DONE) {
1215 14592
                INIT_OBJ(ctx, VRT_CTX_MAGIC);
1216 14592
                VCL_Req2Ctx(ctx, req);
1217 14592
                if (IS_TOPREQ(req)) {
1218 13128
                        VCL_TaskLeave(ctx, req->top->privs);
1219 13128
                        if (req->top->vcl0 != NULL)
1220 48
                                VCL_Recache(wrk, &req->top->vcl0);
1221 13128
                }
1222 14592
                VCL_TaskLeave(ctx, req->privs);
1223 14592
                assert(!IS_NO_VXID(req->vsl->wid));
1224 14592
                VRB_Free(req);
1225 14592
                VRT_Assign_Backend(&req->director_hint, NULL);
1226 14592
                req->wrk = NULL;
1227 14592
        }
1228 14941
        assert(nxt == REQ_FSM_DISEMBARK || !WS_IsReserved(req->ws));
1229 14941
        return (nxt);
1230
}