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 143853
cnt_transport(struct worker *wrk, struct req *req)
86
{
87
        const char *p;
88
89 143853
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
90 143853
        CHECK_OBJ_NOTNULL(req, REQ_MAGIC);
91 143853
        CHECK_OBJ_NOTNULL(req->http, HTTP_MAGIC);
92 143853
        CHECK_OBJ_NOTNULL(req->transport, TRANSPORT_MAGIC);
93 143853
        AN(req->req_body_status);
94
95 143853
        if (http_GetHdr(req->http, H_Expect, &p)) {
96 480
                if (!http_expect_eq(p, 100-continue)) {
97 80
                        req->doclose = SC_RX_JUNK;
98 80
                        (void)req->transport->minimal_response(req, 417);
99 80
                        wrk->stats->client_req_417++;
100 80
                        return (REQ_FSM_DONE);
101
                }
102 400
                if (req->http->protover >= 11 &&
103 400
                    req->htc->pipeline_b == NULL)       // XXX: HTTP1 vs 2 ?
104 360
                        req->want100cont = 1;
105 400
                http_Unset(req->http, H_Expect);
106 400
        }
107
108 143773
        AZ(req->err_code);
109
110 143773
        req->doclose = http_DoConnection(req->http, SC_REQ_CLOSE);
111 143773
        if (req->doclose == SC_RX_BAD) {
112 120
                wrk->stats->client_req_400++;
113 120
                (void)req->transport->minimal_response(req, 400);
114 120
                return (REQ_FSM_DONE);
115
        }
116
117 143653
        if (req->req_body_status->avail == 1) {
118 4800
                AN(req->transport->req_body != NULL);
119 4800
                VFP_Setup(req->vfc, wrk);
120 4800
                req->vfc->resp = req->http;             // XXX
121 4800
                req->transport->req_body(req);
122 4800
        }
123
124 143653
        req->ws_req = WS_Snapshot(req->ws);
125 143653
        HTTP_Clone(req->http0, req->http);      // For ESI & restart
126 143653
        req->req_step = R_STP_RECV;
127 143653
        return (REQ_FSM_MORE);
128 143853
}
129
130
/*--------------------------------------------------------------------
131
 * Deliver an object to client
132
 */
133
134
int
135 126144
Resp_Setup_Deliver(struct req *req)
136
{
137
        struct http *h;
138
        struct objcore *oc;
139
        const void *hdrs;
140
141 126144
        CHECK_OBJ_NOTNULL(req, REQ_MAGIC);
142 126144
        oc = req->objcore;
143 126144
        CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC);
144
145 126144
        h = req->resp;
146
147 126144
        HTTP_Setup(h, req->ws, req->vsl, SLT_RespMethod);
148
149 126144
        hdrs = ObjGetAttr(req->wrk, oc, OA_HEADERS, NULL);
150 126144
        if (hdrs == NULL || HTTP_Decode(h, hdrs))
151 98
                return (-1);
152
153 126052
        http_ForceField(h, HTTP_HDR_PROTO, "HTTP/1.1");
154
155 126052
        if (req->is_hit)
156 84700
                http_PrintfHeader(h, "X-Varnish: %ju %ju", VXID(req->vsl->wid),
157 42350
                    VXID(ObjGetXID(req->wrk, oc)));
158
        else
159 83702
                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 252104
        http_PrintfHeader(h, "Age: %.0f",
169 126052
            floor(fmax(0., req->t_prev - oc->t_origin)));
170
171 126052
        http_AppendHeader(h, H_Via, http_ViaHeader());
172
173 135452
        if (cache_param->http_gzip_support &&
174 125850
            ObjCheckFlag(req->wrk, oc, OF_GZIPED) &&
175 9400
            !RFC2616_Req_Gzip(req->http))
176 3040
                RFC2616_Weaken_Etag(h);
177 126052
        return (0);
178 126150
}
179
180
void
181 22038
Resp_Setup_Synth(struct req *req)
182
{
183
        struct http *h;
184
185 22038
        CHECK_OBJ_NOTNULL(req, REQ_MAGIC);
186
187 22038
        h = req->resp;
188
189 22038
        HTTP_Setup(h, req->ws, req->vsl, SLT_RespMethod);
190
191 22038
        AZ(req->objcore);
192 22038
        http_PutResponse(h, "HTTP/1.1", req->err_code, req->err_reason);
193
194 22038
        http_TimeHeader(h, "Date: ", W_TIM_real(req->wrk));
195 22038
        http_SetHeader(h, "Server: Varnish");
196 22038
        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 22038
        if (req->want100cont)
204 120
                http_SetHeader(h, "Connection: close");
205 22038
}
206
207
static enum req_fsm_nxt v_matchproto_(req_state_f)
208 125736
cnt_deliver(struct worker *wrk, struct req *req)
209
{
210
        unsigned status;
211
212 125736
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
213 125736
        CHECK_OBJ_NOTNULL(req, REQ_MAGIC);
214 125736
        CHECK_OBJ_NOTNULL(req->objcore, OBJCORE_MAGIC);
215 125736
        CHECK_OBJ_NOTNULL(req->objcore->objhead, OBJHEAD_MAGIC);
216 125736
        AZ(req->stale_oc);
217 125736
        AN(req->vcl);
218
219 125736
        assert(req->objcore->refcnt > 0);
220
221 125736
        ObjTouch(req->wrk, req->objcore, req->t_prev);
222
223 125736
        if (Resp_Setup_Deliver(req)) {
224 80
                (void)HSH_DerefObjCore(wrk, &req->objcore, HSH_RUSH_POLICY);
225 80
                req->err_code = 500;
226 80
                req->req_step = R_STP_SYNTH;
227 80
                return (REQ_FSM_MORE);
228
        }
229
230 125656
        status = http_GetStatus(req->resp);
231 125656
        if (cache_param->http_range_support && status == 200 &&
232 111692
            !(req->objcore->flags & OC_F_PRIVATE))
233 90027
                http_ForceHeader(req->resp, H_Accept_Ranges, "bytes");
234
235 125656
        req->t_resp = W_TIM_real(wrk);
236 125656
        VCL_deliver_method(req->vcl, wrk, req, NULL, NULL);
237
238 125656
        assert(req->restarts <= cache_param->max_restarts);
239
240 125656
        if (wrk->vpi->handling != VCL_RET_DELIVER) {
241 5048
                HSH_Cancel(wrk, req->objcore, NULL);
242 5048
                (void)HSH_DerefObjCore(wrk, &req->objcore, HSH_RUSH_POLICY);
243 5048
                http_Teardown(req->resp);
244
245 5048
                switch (wrk->vpi->handling) {
246
                case VCL_RET_RESTART:
247 1080
                        req->req_step = R_STP_RESTART;
248 1080
                        break;
249
                case VCL_RET_FAIL:
250 3648
                        req->req_step = R_STP_VCLFAIL;
251 3648
                        break;
252
                case VCL_RET_SYNTH:
253 320
                        req->req_step = R_STP_SYNTH;
254 320
                        break;
255
                default:
256 0
                        WRONG("Illegal return from vcl_deliver{}");
257 0
                }
258
259 5048
                return (REQ_FSM_MORE);
260
        }
261
262 120608
        VSLb_ts_req(req, "Process", W_TIM_real(wrk));
263
264 120608
        assert(wrk->vpi->handling == VCL_RET_DELIVER);
265
266 120608
        if (IS_TOPREQ(req) && RFC2616_Do_Cond(req))
267 1200
                http_PutResponse(req->resp, "HTTP/1.1", 304, NULL);
268
269 120608
        req->req_step = R_STP_TRANSMIT;
270 120608
        return (REQ_FSM_MORE);
271 125736
}
272
273
/*--------------------------------------------------------------------
274
 * VCL failed, die horribly
275
 */
276
277
static enum req_fsm_nxt v_matchproto_(req_state_f)
278 6279
cnt_vclfail(struct worker *wrk, struct req *req)
279
{
280
        struct vrt_ctx ctx[1];
281
282 6279
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
283 6279
        CHECK_OBJ_NOTNULL(req, REQ_MAGIC);
284
285 6279
        AZ(req->objcore);
286 6279
        AZ(req->stale_oc);
287
288 6279
        INIT_OBJ(ctx, VRT_CTX_MAGIC);
289 6279
        VCL_Req2Ctx(ctx, req);
290
291 6279
        Req_Rollback(ctx);
292
293 6279
        if (req->req_reset) {
294 879
                req->err_code = 408;
295 879
                req->err_reason = "Client disconnected";
296 879
        } else {
297 5400
                req->err_code = 503;
298 5400
                req->err_reason = "VCL failed";
299
        }
300 6279
        req->req_step = R_STP_SYNTH;
301 6279
        req->doclose = SC_VCL_FAILURE;
302 6279
        req->vdp_filter_list = NULL;
303 6279
        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 21960
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 21960
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
320 21960
        CHECK_OBJ_NOTNULL(req, REQ_MAGIC);
321
322 21960
        AZ(req->objcore);
323 21960
        AZ(req->stale_oc);
324
325 21960
        wrk->stats->s_synth++;
326
327 21960
        if (req->err_code < 100)
328 0
                req->err_code = 501;
329
330 21960
        Resp_Setup_Synth(req);
331
332 21960
        req->vdp_filter_list = NULL;
333 21960
        synth_body = VSB_new_auto();
334 21960
        AN(synth_body);
335
336 21960
        req->t_resp = W_TIM_real(wrk);
337 21960
        VCL_synth_method(req->vcl, wrk, req, NULL, synth_body);
338
339 21960
        AZ(VSB_finish(synth_body));
340
341 21960
        VSLb_ts_req(req, "Process", W_TIM_real(wrk));
342
343 21960
        while (wrk->vpi->handling == VCL_RET_FAIL) {
344 1040
                if (req->esi_level > 0) {
345 40
                        wrk->vpi->handling = VCL_RET_DELIVER;
346 40
                        break;
347
                }
348 1000
                VSB_destroy(&synth_body);
349 1000
                (void)VRB_Ignore(req);
350 1000
                status = req->req_reset ? 408 : 500;
351 1000
                (void)req->transport->minimal_response(req, status);
352 1000
                req->doclose = SC_VCL_FAILURE; // XXX: Not necessary any more ?
353 1000
                VSLb_ts_req(req, "Resp", W_TIM_real(wrk));
354 1000
                http_Teardown(req->resp);
355 1000
                return (REQ_FSM_DONE);
356
        }
357
358 20960
        if (wrk->vpi->handling == VCL_RET_RESTART &&
359 480
            req->restarts > cache_param->max_restarts)
360 40
                wrk->vpi->handling = VCL_RET_DELIVER;
361
362 20960
        if (wrk->vpi->handling == VCL_RET_RESTART) {
363
                /*
364
                 * XXX: Should we reset req->doclose = SC_VCL_FAILURE
365
                 * XXX: If so, to what ?
366
                 */
367 440
                HTTP_Setup(req->resp, req->ws, req->vsl, SLT_RespMethod);
368 440
                VSB_destroy(&synth_body);
369 440
                req->req_step = R_STP_RESTART;
370 440
                return (REQ_FSM_MORE);
371
        }
372 20520
        assert(wrk->vpi->handling == VCL_RET_DELIVER);
373
374 20520
        http_Unset(req->resp, H_Content_Length);
375 41040
        http_PrintfHeader(req->resp, "Content-Length: %zd",
376 20520
            VSB_len(synth_body));
377
378 20520
        if (req->doclose == SC_NULL &&
379 14960
            http_HdrIs(req->resp, H_Connection, "close"))
380 120
                req->doclose = SC_RESP_CLOSE;
381
382
        /* Discard any lingering request body before delivery */
383 20520
        (void)VRB_Ignore(req);
384
385 20520
        req->objcore = HSH_Private(wrk);
386 20520
        CHECK_OBJ_NOTNULL(req->objcore, OBJCORE_MAGIC);
387 20520
        szl = -1;
388 20520
        if (STV_NewObject(wrk, req->objcore, stv_transient, 0)) {
389 20440
                body = VSB_data(synth_body);
390 20440
                szl = VSB_len(synth_body);
391 20440
                assert(szl >= 0);
392 39640
                while (szl > 0) {
393 19200
                        sz = szl;
394 19200
                        if (! ObjGetSpace(wrk, req->objcore, &sz, &ptr)) {
395 0
                                szl = -1;
396 0
                                break;
397
                        }
398 19200
                        if (sz > szl)
399 0
                                sz = szl;
400 19200
                        szl -= sz;
401 19200
                        memcpy(ptr, body, sz);
402 19200
                        ObjExtend(wrk, req->objcore, sz, szl == 0 ? 1 : 0);
403 19200
                        body += sz;
404
                }
405 20440
        }
406
407 20520
        if (szl >= 0)
408 20440
                AZ(ObjSetU64(wrk, req->objcore, OA_LEN, VSB_len(synth_body)));
409 20520
        HSH_DerefBoc(wrk, req->objcore);
410 20520
        VSB_destroy(&synth_body);
411
412 20520
        if (szl < 0) {
413 80
                VSLb(req->vsl, SLT_Error, "Could not get storage");
414 80
                req->doclose = SC_OVERLOAD;
415 80
                VSLb_ts_req(req, "Resp", W_TIM_real(wrk));
416 80
                (void)HSH_DerefObjCore(wrk, &req->objcore, 1);
417 80
                http_Teardown(req->resp);
418 80
                return (REQ_FSM_DONE);
419
        }
420
421 20440
        req->req_step = R_STP_TRANSMIT;
422 20440
        return (REQ_FSM_MORE);
423 21960
}
424
425
/*--------------------------------------------------------------------
426
 * The mechanics of sending a response (from deliver or synth)
427
 */
428
429
static enum req_fsm_nxt v_matchproto_(req_state_f)
430 141038
cnt_transmit(struct worker *wrk, struct req *req)
431
{
432 141038
        enum req_fsm_nxt nxt = REQ_FSM_MORE;
433
        enum vtr_deliver_e dnxt;
434
        uint16_t status;
435
        int sendbody, head;
436
        intmax_t clval;
437
438 141038
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
439 141038
        CHECK_OBJ_NOTNULL(req, REQ_MAGIC);
440 141038
        CHECK_OBJ_NOTNULL(req->transport, TRANSPORT_MAGIC);
441 141038
        CHECK_OBJ_NOTNULL(req->objcore, OBJCORE_MAGIC);
442 141038
        AZ(req->stale_oc);
443 141038
        AZ(req->res_mode);
444 141038
        AZ(req->boc);
445 141038
        req->req_step = R_STP_FINISH;
446
447
        /* Grab a ref to the bo if there is one (=streaming) */
448 141038
        req->boc = HSH_RefBoc(req->objcore);
449 141038
        if (req->boc && req->boc->state < BOS_STREAM)
450 0
                ObjWaitState(req->objcore, BOS_STREAM);
451 141038
        clval = http_GetContentLength(req->resp);
452
        /* RFC 7230, 3.3.3 */
453 141038
        status = http_GetStatus(req->resp);
454 141038
        head = http_method_eq(req->http0->hd[HTTP_HDR_METHOD].b, HEAD);
455
456 141038
        if (req->boc != NULL || (req->objcore->flags & (OC_F_FAILED)))
457 33528
                req->resp_len = clval;
458
        else
459 107510
                req->resp_len = ObjGetLen(req->wrk, req->objcore);
460
461 141038
        if (head || status < 200 || status == 204 || status == 304) {
462
                // rfc7230,l,1748,1752
463 6461
                sendbody = 0;
464 6461
        } else {
465 134577
                sendbody = 1;
466
        }
467
468 141038
        VDP_Init(req->vdc, req->wrk, req->vsl, req, NULL, &req->resp_len);
469 141038
        if (req->vdp_filter_list == NULL)
470 130378
                req->vdp_filter_list = resp_Get_Filter_List(req);
471 141038
        if (req->vdp_filter_list == NULL ||
472 141054
            VCL_StackVDP(req->vdc, req->vcl, req->vdp_filter_list, req, NULL)) {
473 2546
                VSLb(req->vsl, SLT_Error, "Failure to push processors");
474 2546
                req->doclose = SC_OVERLOAD;
475 2546
                req->acct.resp_bodybytes +=
476 2546
                        VDP_Close(req->vdc, req->objcore, req->boc);
477 2546
        } else {
478 138532
                if (status < 200 || status == 204) {
479
                        // rfc7230,l,1691,1695
480 764
                        http_Unset(req->resp, H_Content_Length);
481 138532
                } else if (status == 304) {
482
                        // rfc7230,l,1675,1677
483 1360
                        http_Unset(req->resp, H_Content_Length);
484 137768
                } else if (clval >= 0 && clval == req->resp_len) {
485
                        /* Reuse C-L header */
486 136408
                } else if (head && req->objcore->flags & OC_F_HFM) {
487
                        /*
488
                         * Don't touch C-L header (debatable)
489
                         *
490
                         * The only way to do it correctly would be to GET
491
                         * to the backend, and discard the body once the
492
                         * filters have had a chance to chew on it, but that
493
                         * would negate the "pass for huge objects" use case.
494
                         */
495 40
                } else {
496 34833
                        http_Unset(req->resp, H_Content_Length);
497 34833
                        if (req->resp_len >= 0)
498 40790
                                http_PrintfHeader(req->resp,
499 20395
                                    "Content-Length: %jd", req->resp_len);
500
                }
501 138530
                if (req->resp_len == 0)
502 39064
                        sendbody = 0;
503 138530
                dnxt = req->transport->deliver(req, sendbody);
504 138530
                if (dnxt == VTR_D_DISEMBARK)
505 1280
                        nxt = REQ_FSM_DISEMBARK;
506
                else
507 137250
                        assert(dnxt == VTR_D_DONE);
508
        }
509 141076
        return (nxt);
510
}
511
512
static enum req_fsm_nxt v_matchproto_(req_state_f)
513 141016
cnt_finish(struct worker *wrk, struct req *req)
514
{
515
516 141016
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
517 141016
        CHECK_OBJ_NOTNULL(req, REQ_MAGIC);
518
519 141016
        VSLb_ts_req(req, "Resp", W_TIM_real(wrk));
520
521 141016
        if (req->doclose == SC_NULL && (req->objcore->flags & OC_F_FAILED)) {
522
                /* The object we delivered failed due to a streaming error.
523
                 * Fail the request. */
524 560
                req->doclose = SC_TX_ERROR;
525 560
        }
526
527 141016
        if (req->boc != NULL) {
528 33504
                HSH_DerefBoc(wrk, req->objcore);
529 33504
                req->boc = NULL;
530 33504
        }
531
532 141016
        (void)HSH_DerefObjCore(wrk, &req->objcore, HSH_RUSH_POLICY);
533 141016
        http_Teardown(req->resp);
534
535 141016
        req->vdp_filter_list = NULL;
536 141016
        req->res_mode = 0;
537 141016
        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 84509
cnt_fetch(struct worker *wrk, struct req *req)
546
{
547
548 84509
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
549 84509
        CHECK_OBJ_NOTNULL(req, REQ_MAGIC);
550 84509
        CHECK_OBJ_NOTNULL(req->objcore, OBJCORE_MAGIC);
551 84509
        AZ(req->stale_oc);
552
553 84509
        wrk->stats->s_fetch++;
554 84509
        (void)VRB_Ignore(req);
555
556 84509
        if (req->objcore->flags & OC_F_FAILED) {
557 1040
                req->err_code = 503;
558 1040
                req->req_step = R_STP_SYNTH;
559 1040
                (void)HSH_DerefObjCore(wrk, &req->objcore, 1);
560 1040
                AZ(req->objcore);
561 1040
                return (REQ_FSM_MORE);
562
        }
563
564 83469
        req->req_step = R_STP_DELIVER;
565 83469
        return (REQ_FSM_MORE);
566 84509
}
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 100356
cnt_lookup(struct worker *wrk, struct req *req)
575
{
576
        struct objcore *oc, *busy;
577
        enum lookup_e lr;
578 100356
        int had_objhead = 0;
579
580 100356
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
581 100356
        CHECK_OBJ_NOTNULL(req, REQ_MAGIC);
582 100356
        AZ(req->objcore);
583 100356
        AZ(req->stale_oc);
584
585 100356
        AN(req->vcl);
586
587 100356
        VRY_Prep(req);
588
589 100356
        AZ(req->objcore);
590 100356
        if (req->hash_objhead)
591 1897
                had_objhead = 1;
592 100356
        wrk->strangelove = 0;
593 100356
        lr = HSH_Lookup(req, &oc, &busy);
594 100356
        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 1902
                return (REQ_FSM_DISEMBARK);
602
        }
603 98454
        assert(wrk->strangelove >= 0);
604 98454
        if ((unsigned)wrk->strangelove >= cache_param->vary_notice)
605 4960
                VSLb(req->vsl, SLT_Notice, "vsl: High number of variants (%d)",
606 2480
                    wrk->strangelove);
607 98454
        if (had_objhead)
608 1781
                VSLb_ts_req(req, "Waitinglist", W_TIM_real(wrk));
609
610 98454
        if (req->vcf != NULL) {
611 240
                (void)req->vcf->func(req, NULL, NULL, 2);
612 240
                req->vcf = NULL;
613 240
        }
614
615 98454
        if (busy == NULL) {
616 40233
                VRY_Finish(req, DISCARD);
617 40233
        } else {
618 58221
                AN(busy->flags & OC_F_BUSY);
619 58221
                VRY_Finish(req, KEEP);
620
        }
621
622 98454
        AZ(req->objcore);
623 98454
        if (lr == HSH_MISS || lr == HSH_HITMISS) {
624 54546
                AN(busy);
625 54546
                AN(busy->flags & OC_F_BUSY);
626 54546
                req->objcore = busy;
627 54546
                req->stale_oc = oc;
628 54546
                req->req_step = R_STP_MISS;
629 54546
                if (lr == HSH_HITMISS)
630 1400
                        req->is_hitmiss = 1;
631 54546
                return (REQ_FSM_MORE);
632
        }
633 43908
        if (lr == HSH_HITPASS) {
634 398
                AZ(busy);
635 398
                AZ(oc);
636 398
                req->req_step = R_STP_PASS;
637 398
                req->is_hitpass = 1;
638 398
                return (REQ_FSM_MORE);
639
        }
640
641 43510
        assert(lr == HSH_HIT || lr == HSH_GRACE);
642
643 43510
        CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC);
644 43510
        AZ(oc->flags & OC_F_BUSY);
645 43510
        req->objcore = oc;
646 43510
        AZ(oc->flags & OC_F_HFM);
647
648 43510
        VCL_hit_method(req->vcl, wrk, req, NULL, NULL);
649
650 43510
        switch (wrk->vpi->handling) {
651
        case VCL_RET_DELIVER:
652 42309
                if (busy != NULL) {
653 3400
                        AZ(oc->flags & OC_F_HFM);
654 3400
                        CHECK_OBJ_NOTNULL(busy->boc, BOC_MAGIC);
655
                        // XXX: shouldn't we go to miss?
656 3400
                        VBF_Fetch(wrk, req, busy, oc, VBF_BACKGROUND);
657 3400
                        wrk->stats->s_fetch++;
658 3400
                        wrk->stats->s_bgfetch++;
659 3400
                } else {
660 38909
                        (void)VRB_Ignore(req);// XXX: handle err
661
                }
662 42309
                wrk->stats->cache_hit++;
663 42309
                req->is_hit = 1;
664 42309
                if (lr == HSH_GRACE)
665 3520
                        wrk->stats->cache_hit_grace++;
666 42309
                req->req_step = R_STP_DELIVER;
667 42309
                return (REQ_FSM_MORE);
668
        case VCL_RET_RESTART:
669 560
                req->req_step = R_STP_RESTART;
670 560
                break;
671
        case VCL_RET_FAIL:
672 201
                req->req_step = R_STP_VCLFAIL;
673 201
                break;
674
        case VCL_RET_SYNTH:
675 360
                req->req_step = R_STP_SYNTH;
676 360
                break;
677
        case VCL_RET_PASS:
678 80
                wrk->stats->cache_hit++;
679 80
                req->is_hit = 1;
680 80
                req->req_step = R_STP_PASS;
681 80
                break;
682
        default:
683 0
                WRONG("Illegal return from vcl_hit{}");
684 0
        }
685
686
        /* Drop our object, we won't need it */
687 1201
        (void)HSH_DerefObjCore(wrk, &req->objcore, HSH_RUSH_POLICY);
688
689 1201
        if (busy != NULL) {
690 280
                (void)HSH_DerefObjCore(wrk, &busy, 0);
691 280
                VRY_Clear(req);
692 280
        }
693
694 1201
        return (REQ_FSM_MORE);
695 100356
}
696
697
/*--------------------------------------------------------------------
698
 * Cache miss.
699
 */
700
701
static enum req_fsm_nxt v_matchproto_(req_state_f)
702 54500
cnt_miss(struct worker *wrk, struct req *req)
703
{
704
705 54500
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
706 54500
        CHECK_OBJ_NOTNULL(req, REQ_MAGIC);
707 54500
        AN(req->vcl);
708 54500
        CHECK_OBJ_NOTNULL(req->objcore, OBJCORE_MAGIC);
709 54500
        CHECK_OBJ_ORNULL(req->stale_oc, OBJCORE_MAGIC);
710
711 54500
        VCL_miss_method(req->vcl, wrk, req, NULL, NULL);
712 54500
        switch (wrk->vpi->handling) {
713
        case VCL_RET_FETCH:
714 53799
                wrk->stats->cache_miss++;
715 53799
                VBF_Fetch(wrk, req, req->objcore, req->stale_oc, VBF_NORMAL);
716 53799
                if (req->stale_oc != NULL)
717 2360
                        (void)HSH_DerefObjCore(wrk, &req->stale_oc, 0);
718 53799
                req->req_step = R_STP_FETCH;
719 53799
                return (REQ_FSM_MORE);
720
        case VCL_RET_FAIL:
721 141
                req->req_step = R_STP_VCLFAIL;
722 141
                break;
723
        case VCL_RET_SYNTH:
724 320
                req->req_step = R_STP_SYNTH;
725 320
                break;
726
        case VCL_RET_RESTART:
727 160
                req->req_step = R_STP_RESTART;
728 160
                break;
729
        case VCL_RET_PASS:
730 80
                req->req_step = R_STP_PASS;
731 80
                break;
732
        default:
733 0
                WRONG("Illegal return from vcl_miss{}");
734 0
        }
735 701
        VRY_Clear(req);
736 701
        if (req->stale_oc != NULL)
737 0
                (void)HSH_DerefObjCore(wrk, &req->stale_oc, 0);
738 701
        AZ(HSH_DerefObjCore(wrk, &req->objcore, 1));
739 701
        return (REQ_FSM_MORE);
740 54500
}
741
742
/*--------------------------------------------------------------------
743
 * Pass processing
744
 */
745
746
static enum req_fsm_nxt v_matchproto_(req_state_f)
747 30800
cnt_pass(struct worker *wrk, struct req *req)
748
{
749
750 30800
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
751 30800
        CHECK_OBJ_NOTNULL(req, REQ_MAGIC);
752 30800
        AN(req->vcl);
753 30800
        AZ(req->objcore);
754 30800
        AZ(req->stale_oc);
755
756 30800
        VCL_pass_method(req->vcl, wrk, req, NULL, NULL);
757 30800
        switch (wrk->vpi->handling) {
758
        case VCL_RET_FAIL:
759 40
                req->req_step = R_STP_VCLFAIL;
760 40
                break;
761
        case VCL_RET_SYNTH:
762 40
                req->req_step = R_STP_SYNTH;
763 40
                break;
764
        case VCL_RET_RESTART:
765 0
                req->req_step = R_STP_RESTART;
766 0
                break;
767
        case VCL_RET_FETCH:
768 30720
                wrk->stats->s_pass++;
769 30720
                req->objcore = HSH_Private(wrk);
770 30720
                CHECK_OBJ_NOTNULL(req->objcore, OBJCORE_MAGIC);
771 30720
                VBF_Fetch(wrk, req, req->objcore, NULL, VBF_PASS);
772 30720
                req->req_step = R_STP_FETCH;
773 30720
                break;
774
        default:
775 0
                WRONG("Illegal return from cnt_pass{}");
776 0
        }
777 30800
        return (REQ_FSM_MORE);
778
}
779
780
/*--------------------------------------------------------------------
781
 * Pipe mode
782
 */
783
784
static enum req_fsm_nxt v_matchproto_(req_state_f)
785 1240
cnt_pipe(struct worker *wrk, struct req *req)
786
{
787
        struct busyobj *bo;
788
        enum req_fsm_nxt nxt;
789
790 1240
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
791 1240
        CHECK_OBJ_NOTNULL(req, REQ_MAGIC);
792 1240
        AZ(req->objcore);
793 1240
        AZ(req->stale_oc);
794 1240
        AN(req->vcl);
795
796 1240
        wrk->stats->s_pipe++;
797 1240
        bo = VBO_GetBusyObj(wrk, req);
798 1240
        CHECK_OBJ_NOTNULL(bo, BUSYOBJ_MAGIC);
799 1240
        VSLb(bo->vsl, SLT_Begin, "bereq %ju pipe", VXID(req->vsl->wid));
800 1240
        VSLb(req->vsl, SLT_Link, "bereq %ju pipe", VXID(bo->vsl->wid));
801 1240
        VSLb_ts_busyobj(bo, "Start", W_TIM_real(wrk));
802 1240
        THR_SetBusyobj(bo);
803 1240
        bo->sp = req->sp;
804 1240
        SES_Ref(bo->sp);
805
806 1240
        HTTP_Setup(bo->bereq, req->ws, bo->vsl, SLT_BereqMethod);
807 1240
        http_FilterReq(bo->bereq, req->http, 0);        // XXX: 0 ?
808 1240
        http_PrintfHeader(bo->bereq, "X-Varnish: %ju", VXID(req->vsl->wid));
809 1240
        http_ForceHeader(bo->bereq, H_Connection, "close");
810
811 1240
        if (req->want100cont) {
812 0
                http_SetHeader(bo->bereq, "Expect: 100-continue");
813 0
                req->want100cont = 0;
814 0
        }
815
816 1240
        bo->wrk = wrk;
817 1240
        bo->task_deadline = NAN; /* XXX: copy req->task_deadline */
818 1240
        if (WS_Overflowed(req->ws))
819 40
                wrk->vpi->handling = VCL_RET_FAIL;
820
        else
821 1200
                VCL_pipe_method(req->vcl, wrk, req, bo, NULL);
822
823 1240
        switch (wrk->vpi->handling) {
824
        case VCL_RET_SYNTH:
825 120
                req->req_step = R_STP_SYNTH;
826 120
                nxt = REQ_FSM_MORE;
827 120
                break;
828
        case VCL_RET_PIPE:
829 1040
                VSLb_ts_req(req, "Process", W_TIM_real(wrk));
830 1040
                VSLb_ts_busyobj(bo, "Process", wrk->lastused);
831 1040
                if (V1P_Enter() == 0) {
832 1000
                        AZ(bo->req);
833 1000
                        bo->req = req;
834 1000
                        bo->wrk = wrk;
835
                        /* Unless cached, reqbody is not our job */
836 1000
                        if (req->req_body_status != BS_CACHED)
837 960
                                req->req_body_status = BS_NONE;
838 1000
                        SES_Close(req->sp, VDI_Http1Pipe(req, bo));
839 1000
                        nxt = REQ_FSM_DONE;
840 1000
                        V1P_Leave();
841 1000
                        break;
842
                }
843 40
                wrk->stats->pipe_limited++;
844
                /* fall through */
845
        case VCL_RET_FAIL:
846 120
                req->req_step = R_STP_VCLFAIL;
847 120
                nxt = REQ_FSM_MORE;
848 120
                break;
849
        default:
850 0
                WRONG("Illegal return from vcl_pipe{}");
851 0
        }
852 1240
        http_Teardown(bo->bereq);
853 1240
        SES_Rel(bo->sp);
854 1240
        VBO_ReleaseBusyObj(wrk, &bo);
855 1240
        THR_SetBusyobj(NULL);
856 1240
        return (nxt);
857
}
858
859
/*--------------------------------------------------------------------
860
 * Handle restart events
861
 */
862
863
static enum req_fsm_nxt v_matchproto_(req_state_f)
864 2720
cnt_restart(struct worker *wrk, struct req *req)
865
{
866
867 2720
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
868 2720
        CHECK_OBJ_NOTNULL(req, REQ_MAGIC);
869 2720
        AZ(req->objcore);
870 2720
        AZ(req->stale_oc);
871
872 2720
        if (++req->restarts > cache_param->max_restarts) {
873 400
                VSLb(req->vsl, SLT_VCL_Error, "Too many restarts");
874 400
                req->err_code = 503;
875 400
                req->req_step = R_STP_SYNTH;
876 400
        } else {
877
                // XXX: ReqEnd + ReqAcct ?
878 2320
                VSLb_ts_req(req, "Restart", W_TIM_real(wrk));
879 4640
                VSL_ChgId(req->vsl, "req", "restart",
880 2320
                    VXID_Get(wrk, VSL_CLIENTMARKER));
881 2320
                VSLb_ts_req(req, "Start", req->t_prev);
882 2320
                req->err_code = 0;
883 2320
                req->req_step = R_STP_RECV;
884
        }
885 2720
        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 146370
cnt_recv_prep(struct req *req, const char *ci)
899
{
900
901 146370
        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 144050
                http_AppendHeader(req->http, H_X_Forwarded_For, ci);
907 144050
                http_AppendHeader(req->http, H_Via, http_ViaHeader());
908 144050
                http_CollectHdr(req->http, H_Cache_Control);
909
910
                /* By default we use the first backend */
911 288100
                VRT_Assign_Backend(&req->director_hint,
912 144050
                    VCL_DefaultDirector(req->vcl));
913
914 144050
                req->d_ttl = -1;
915 144050
                req->d_grace = -1;
916 144050
                req->disable_esi = 0;
917 144050
                req->hash_always_miss = 0;
918 144050
                req->hash_ignore_busy = 0;
919 144050
                req->hash_ignore_vary = 0;
920 144050
                req->client_identity = NULL;
921 144050
                req->storage = NULL;
922 144050
                req->trace = FEATURE(FEATURE_TRACE);
923 144050
        }
924
925 146370
        req->is_hit = 0;
926 146370
        req->is_hitmiss = 0;
927 146370
        req->is_hitpass = 0;
928 146370
        req->err_code = 0;
929 146370
        req->err_reason = NULL;
930
931 146370
        req->vfp_filter_list = NULL;
932 146370
}
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 145804
cnt_recv(struct worker *wrk, struct req *req)
942
{
943
        unsigned recv_handling;
944
        struct VSHA256Context sha256ctx;
945
        const char *ci;
946
947 145804
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
948 145804
        CHECK_OBJ_NOTNULL(req, REQ_MAGIC);
949 145804
        AN(req->vcl);
950 145804
        AZ(req->objcore);
951 145804
        AZ(req->stale_oc);
952 145804
        AZ(req->err_code);
953
954 145804
        AZ(isnan(req->t_first));
955 145804
        AZ(isnan(req->t_prev));
956 145804
        AZ(isnan(req->t_req));
957
958 145804
        ci = Req_LogStart(wrk, req);
959 145804
        http_VSL_log(req->http);
960
961 145804
        if (http_CountHdr(req->http0, H_Host) > 1) {
962 80
                VSLb(req->vsl, SLT_BogoHeader, "Multiple Host: headers");
963 80
                wrk->stats->client_req_400++;
964 80
                (void)req->transport->minimal_response(req, 400);
965 80
                return (REQ_FSM_DONE);
966
        }
967
968 145724
        if (http_CountHdr(req->http0, H_Content_Length) > 1) {
969 40
                VSLb(req->vsl, SLT_BogoHeader, "Multiple Content-Length: headers");
970 40
                wrk->stats->client_req_400++;
971 40
                (void)req->transport->minimal_response(req, 400);
972 40
                return (REQ_FSM_DONE);
973
        }
974
975 145684
        cnt_recv_prep(req, ci);
976
977 145684
        if (req->req_body_status == BS_ERROR) {
978 0
                req->doclose = SC_OVERLOAD;
979 0
                return (REQ_FSM_DONE);
980
        }
981
982 145684
        VCL_recv_method(req->vcl, wrk, req, NULL, NULL);
983
984 145684
        if (wrk->vpi->handling == VCL_RET_FAIL) {
985 1840
                req->req_step = R_STP_VCLFAIL;
986 1840
                return (REQ_FSM_MORE);
987
        }
988
989 143844
        if (wrk->vpi->handling == VCL_RET_VCL && req->restarts == 0) {
990
                // Req_Rollback has happened in VPI_vcl_select
991 520
                assert(WS_Snapshot(req->ws) == req->ws_req);
992 520
                cnt_recv_prep(req, ci);
993 520
                VCL_recv_method(req->vcl, wrk, req, NULL, NULL);
994 520
        }
995
996 143844
        if (req->want100cont && !req->late100cont) {
997 160
                req->want100cont = 0;
998 160
                if (req->transport->minimal_response(req, 100)) {
999 0
                        req->doclose = SC_REM_CLOSE;
1000 0
                        return (REQ_FSM_DONE);
1001
                }
1002 160
        }
1003
1004
        /* Attempts to cache req.body may fail */
1005 143844
        if (req->req_body_status == BS_ERROR) {
1006 160
                req->doclose = SC_RX_BODY;
1007 160
                return (REQ_FSM_DONE);
1008
        }
1009
1010 143684
        recv_handling = wrk->vpi->handling;
1011
1012
        /* We wash the A-E header here for the sake of VRY */
1013 285846
        if (cache_param->http_gzip_support &&
1014 143479
             (recv_handling != VCL_RET_PIPE) &&
1015 142162
             (recv_handling != VCL_RET_PASS)) {
1016 112002
                if (RFC2616_Req_Gzip(req->http)) {
1017 8160
                        http_ForceHeader(req->http, H_Accept_Encoding, "gzip");
1018 8160
                } else {
1019 103842
                        http_Unset(req->http, H_Accept_Encoding);
1020
                }
1021 112002
        }
1022
1023 143684
        VSHA256_Init(&sha256ctx);
1024 143684
        VCL_hash_method(req->vcl, wrk, req, NULL, &sha256ctx);
1025 143684
        if (wrk->vpi->handling == VCL_RET_FAIL)
1026 249
                recv_handling = wrk->vpi->handling;
1027
        else
1028 143435
                assert(wrk->vpi->handling == VCL_RET_LOOKUP);
1029 143684
        VSHA256_Final(req->digest, &sha256ctx);
1030
1031 143684
        switch (recv_handling) {
1032
        case VCL_RET_VCL:
1033 320
                VSLb(req->vsl, SLT_VCL_Error,
1034
                    "Illegal return(vcl): %s",
1035 160
                    req->restarts ? "Not after restarts" :
1036
                    "Only from active VCL");
1037 160
                req->err_code = 503;
1038 160
                req->req_step = R_STP_SYNTH;
1039 160
                break;
1040
        case VCL_RET_PURGE:
1041 280
                req->req_step = R_STP_PURGE;
1042 280
                break;
1043
        case VCL_RET_HASH:
1044 98438
                req->req_step = R_STP_LOOKUP;
1045 98438
                break;
1046
        case VCL_RET_PIPE:
1047 1320
                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 1320
                } else if (req->http0->protover > 11) {
1053 80
                        VSLb(req->vsl, SLT_VCL_Error,
1054
                            "vcl_recv{} returns pipe for HTTP/2 request."
1055
                            "  Doing pass.");
1056 80
                        req->req_step = R_STP_PASS;
1057 80
                } else {
1058 1240
                        req->req_step = R_STP_PIPE;
1059
                }
1060 1320
                break;
1061
        case VCL_RET_PASS:
1062 30157
                req->req_step = R_STP_PASS;
1063 30157
                break;
1064
        case VCL_RET_SYNTH:
1065 12720
                req->req_step = R_STP_SYNTH;
1066 12720
                break;
1067
        case VCL_RET_RESTART:
1068 360
                req->req_step = R_STP_RESTART;
1069 360
                break;
1070
        case VCL_RET_FAIL:
1071 249
                req->req_step = R_STP_VCLFAIL;
1072 249
                break;
1073
        default:
1074 0
                WRONG("Illegal return from vcl_recv{}");
1075 0
        }
1076 143684
        return (REQ_FSM_MORE);
1077 145804
}
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 280
cnt_purge(struct worker *wrk, struct req *req)
1087
{
1088
        struct objcore *oc, *boc;
1089
        enum lookup_e lr;
1090
1091 280
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
1092 280
        CHECK_OBJ_NOTNULL(req, REQ_MAGIC);
1093 280
        AZ(req->objcore);
1094 280
        AZ(req->stale_oc);
1095
1096 280
        AN(req->vcl);
1097
1098 280
        VRY_Prep(req);
1099
1100 280
        AZ(req->objcore);
1101 280
        req->hash_always_miss = 1;
1102 280
        lr = HSH_Lookup(req, &oc, &boc);
1103 280
        assert (lr == HSH_MISS);
1104 280
        AZ(oc);
1105 280
        CHECK_OBJ_NOTNULL(boc, OBJCORE_MAGIC);
1106 280
        VRY_Finish(req, DISCARD);
1107
1108 280
        (void)HSH_Purge(wrk, boc->objhead, req->t_req, 0, 0, 0);
1109
1110 280
        AZ(HSH_DerefObjCore(wrk, &boc, 1));
1111
1112 280
        VCL_purge_method(req->vcl, wrk, req, NULL, NULL);
1113 280
        switch (wrk->vpi->handling) {
1114
        case VCL_RET_RESTART:
1115 120
                req->req_step = R_STP_RESTART;
1116 120
                break;
1117
        case VCL_RET_FAIL:
1118 40
                req->req_step = R_STP_VCLFAIL;
1119 40
                break;
1120
        case VCL_RET_SYNTH:
1121 120
                req->req_step = R_STP_SYNTH;
1122 120
                break;
1123
        default:
1124 0
                WRONG("Illegal return from vcl_purge{}");
1125 0
        }
1126 280
        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 8117
cnt_diag(struct req *req, const char *state)
1138
{
1139
1140 8117
        CHECK_OBJ_NOTNULL(req, REQ_MAGIC);
1141
1142 16234
        VSLb(req->vsl,  SLT_Debug, "vxid %ju STP_%s sp %p vcl %p",
1143 8117
            VXID(req->vsl->wid), state, req->sp, req->vcl);
1144 8117
        VSL_Flush(req->vsl, 0);
1145 8117
}
1146
1147
void
1148 148318
CNT_Embark(struct worker *wrk, struct req *req)
1149
{
1150
1151 148318
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
1152 148318
        CHECK_OBJ_NOTNULL(req, REQ_MAGIC);
1153
1154
        /* wrk can have changed for restarts */
1155 148318
        req->vfc->wrk = req->wrk = wrk;
1156 148318
        wrk->vsl = req->vsl;
1157 148318
        if (req->req_step == R_STP_TRANSPORT && req->vcl == NULL) {
1158 129195
                VCL_Refresh(&wrk->wpriv->vcl);
1159 129195
                req->vcl = wrk->wpriv->vcl;
1160 129195
                wrk->wpriv->vcl = NULL;
1161 129195
                VSLbs(req->vsl, SLT_VCL_use, TOSTRAND(VCL_Name(req->vcl)));
1162 129195
        }
1163
1164 148318
        AN(req->vcl);
1165 148318
}
1166
1167
enum req_fsm_nxt
1168 147040
CNT_Request(struct req *req)
1169
{
1170
        struct vrt_ctx ctx[1];
1171
        struct worker *wrk;
1172
        enum req_fsm_nxt nxt;
1173
1174 147040
        CHECK_OBJ_NOTNULL(req, REQ_MAGIC);
1175
1176 147040
        wrk = req->wrk;
1177 147040
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
1178
1179 147040
        CHECK_OBJ_NOTNULL(req->transport, TRANSPORT_MAGIC);
1180 147040
        AN(req->transport->deliver);
1181 147040
        AN(req->transport->minimal_response);
1182
1183
        /*
1184
         * Possible entrance states
1185
         */
1186 147040
        assert(
1187
            req->req_step == R_STP_LOOKUP ||
1188
            req->req_step == R_STP_FINISH ||
1189
            req->req_step == R_STP_TRANSPORT);
1190
1191 147040
        AN(VXID_TAG(req->vsl->wid) & VSL_CLIENTMARKER);
1192 147040
        AN(req->vcl);
1193
1194 1147010
        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 999970
                CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
1200 999970
                CHECK_OBJ_NOTNULL(wrk->wpriv, WORKER_PRIV_MAGIC);
1201 999970
                CHECK_OBJ_ORNULL(wrk->wpriv->nobjhead, OBJHEAD_MAGIC);
1202 999970
                CHECK_OBJ_NOTNULL(req, REQ_MAGIC);
1203 999970
                CHECK_OBJ_NOTNULL(req->doclose, STREAM_CLOSE_MAGIC);
1204
1205 999970
                AN(req->req_step);
1206 999970
                AN(req->req_step->name);
1207 999970
                AN(req->req_step->func);
1208 999970
                if (DO_DEBUG(DBG_REQ_STATE))
1209 8118
                        cnt_diag(req, req->req_step->name);
1210 999970
                nxt = req->req_step->func(wrk, req);
1211 999970
                CHECK_OBJ_ORNULL(wrk->wpriv->nobjhead, OBJHEAD_MAGIC);
1212
        }
1213 147040
        wrk->vsl = NULL;
1214 147040
        if (nxt == REQ_FSM_DONE) {
1215 143575
                INIT_OBJ(ctx, VRT_CTX_MAGIC);
1216 143575
                VCL_Req2Ctx(ctx, req);
1217 143575
                if (IS_TOPREQ(req)) {
1218 128936
                        VCL_TaskLeave(ctx, req->top->privs);
1219 128936
                        if (req->top->vcl0 != NULL)
1220 480
                                VCL_Recache(wrk, &req->top->vcl0);
1221 128936
                }
1222 143575
                VCL_TaskLeave(ctx, req->privs);
1223 143575
                assert(!IS_NO_VXID(req->vsl->wid));
1224 143575
                VRB_Free(req);
1225 143575
                VRT_Assign_Backend(&req->director_hint, NULL);
1226 143575
                req->wrk = NULL;
1227 143575
        }
1228 147040
        assert(nxt == REQ_FSM_DISEMBARK || !WS_IsReserved(req->ws));
1229 147040
        return (nxt);
1230
}