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 86920
cnt_transport(struct worker *wrk, struct req *req)
86
{
87
        const char *p;
88
89 86920
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
90 86920
        CHECK_OBJ_NOTNULL(req, REQ_MAGIC);
91 86920
        CHECK_OBJ_NOTNULL(req->http, HTTP_MAGIC);
92 86920
        CHECK_OBJ_NOTNULL(req->transport, TRANSPORT_MAGIC);
93 86920
        AN(req->req_body_status);
94
95 86920
        if (http_GetHdr(req->http, H_Expect, &p)) {
96 276
                if (!http_expect_eq(p, 100-continue)) {
97 46
                        req->doclose = SC_RX_JUNK;
98 46
                        (void)req->transport->minimal_response(req, 417);
99 46
                        wrk->stats->client_req_417++;
100 46
                        return (REQ_FSM_DONE);
101
                }
102 230
                if (req->http->protover >= 11 &&
103 230
                    req->htc->pipeline_b == NULL)       // XXX: HTTP1 vs 2 ?
104 207
                        req->want100cont = 1;
105 230
                http_Unset(req->http, H_Expect);
106 230
        }
107
108 86874
        AZ(req->err_code);
109
110 86874
        req->doclose = http_DoConnection(req->http, SC_REQ_CLOSE);
111 86874
        if (req->doclose == SC_RX_BAD) {
112 69
                wrk->stats->client_req_400++;
113 69
                (void)req->transport->minimal_response(req, 400);
114 69
                return (REQ_FSM_DONE);
115
        }
116
117 86805
        if (req->req_body_status->avail == 1) {
118 3036
                AN(req->transport->req_body != NULL);
119 3036
                VFP_Setup(req->vfc, wrk);
120 3036
                req->vfc->resp = req->http;             // XXX
121 3036
                req->transport->req_body(req);
122 3036
        }
123
124 86805
        req->ws_req = WS_Snapshot(req->ws);
125 86805
        HTTP_Clone(req->http0, req->http);      // For ESI & restart
126 86805
        req->req_step = R_STP_RECV;
127 86805
        return (REQ_FSM_MORE);
128 86920
}
129
130
/*--------------------------------------------------------------------
131
 * Deliver an object to client
132
 */
133
134
int
135 76452
Resp_Setup_Deliver(struct req *req)
136
{
137
        struct http *h;
138
        struct objcore *oc;
139
        const void *hdrs;
140
141 76452
        CHECK_OBJ_NOTNULL(req, REQ_MAGIC);
142 76452
        oc = req->objcore;
143 76452
        CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC);
144
145 76452
        h = req->resp;
146
147 76452
        HTTP_Setup(h, req->ws, req->vsl, SLT_RespMethod);
148
149 76452
        hdrs = ObjGetAttr(req->wrk, oc, OA_HEADERS, NULL);
150 76452
        if (hdrs == NULL || HTTP_Decode(h, hdrs))
151 49
                return (-1);
152
153 76409
        http_ForceField(h, HTTP_HDR_PROTO, "HTTP/1.1");
154
155 76409
        if (req->is_hit)
156 52372
                http_PrintfHeader(h, "X-Varnish: %ju %ju", VXID(req->vsl->wid),
157 26186
                    VXID(ObjGetXID(req->wrk, oc)));
158
        else
159 50223
                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 152818
        http_PrintfHeader(h, "Age: %.0f",
169 76409
            floor(fmax(0., req->t_prev - oc->t_origin)));
170
171 76409
        http_AppendHeader(h, H_Via, http_ViaHeader());
172
173 82734
        if (cache_param->http_gzip_support &&
174 76294
            ObjCheckFlag(req->wrk, oc, OF_GZIPED) &&
175 6325
            !RFC2616_Req_Gzip(req->http))
176 2300
                RFC2616_Weaken_Etag(h);
177 76409
        return (0);
178 76458
}
179
180
void
181 13040
Resp_Setup_Synth(struct req *req)
182
{
183
        struct http *h;
184
185 13040
        CHECK_OBJ_NOTNULL(req, REQ_MAGIC);
186
187 13040
        h = req->resp;
188
189 13040
        HTTP_Setup(h, req->ws, req->vsl, SLT_RespMethod);
190
191 13040
        AZ(req->objcore);
192 13040
        http_PutResponse(h, "HTTP/1.1", req->err_code, req->err_reason);
193
194 13040
        http_TimeHeader(h, "Date: ", W_TIM_real(req->wrk));
195 13040
        http_SetHeader(h, "Server: Varnish");
196 13040
        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 13040
        if (req->want100cont)
204 69
                http_SetHeader(h, "Connection: close");
205 13040
}
206
207
static enum req_fsm_nxt v_matchproto_(req_state_f)
208 76229
cnt_deliver(struct worker *wrk, struct req *req)
209
{
210
        unsigned status;
211
212 76229
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
213 76229
        CHECK_OBJ_NOTNULL(req, REQ_MAGIC);
214 76229
        CHECK_OBJ_NOTNULL(req->objcore, OBJCORE_MAGIC);
215 76229
        CHECK_OBJ_NOTNULL(req->objcore->objhead, OBJHEAD_MAGIC);
216 76229
        AZ(req->stale_oc);
217 76229
        AN(req->vcl);
218
219 76229
        assert(req->objcore->refcnt > 0);
220
221 76229
        ObjTouch(req->wrk, req->objcore, req->t_prev);
222
223 76229
        if (Resp_Setup_Deliver(req)) {
224 46
                (void)HSH_DerefObjCore(wrk, &req->objcore);
225 46
                req->err_code = 500;
226 46
                req->req_step = R_STP_SYNTH;
227 46
                return (REQ_FSM_MORE);
228
        }
229
230 76183
        status = http_GetStatus(req->resp);
231 76183
        if (cache_param->http_range_support && status == 200 &&
232 67604
            !(req->objcore->flags & OC_F_PRIVATE))
233 54729
                http_ForceHeader(req->resp, H_Accept_Ranges, "bytes");
234
235 76183
        req->t_resp = W_TIM_real(wrk);
236 76183
        VCL_deliver_method(req->vcl, wrk, req, NULL, NULL);
237
238 76183
        assert(req->restarts <= req->max_restarts);
239
240 76183
        if (wrk->vpi->handling != VCL_RET_DELIVER) {
241 2991
                HSH_Cancel(wrk, req->objcore, NULL);
242 2991
                (void)HSH_DerefObjCore(wrk, &req->objcore);
243 2991
                http_Teardown(req->resp);
244
245 2991
                switch (wrk->vpi->handling) {
246
                case VCL_RET_RESTART:
247 713
                        req->req_step = R_STP_RESTART;
248 713
                        break;
249
                case VCL_RET_FAIL:
250 2094
                        req->req_step = R_STP_VCLFAIL;
251 2094
                        break;
252
                case VCL_RET_SYNTH:
253 184
                        req->req_step = R_STP_SYNTH;
254 184
                        break;
255
                default:
256 0
                        WRONG("Illegal return from vcl_deliver{}");
257 0
                }
258
259 2991
                return (REQ_FSM_MORE);
260
        }
261
262 73192
        VSLb_ts_req(req, "Process", W_TIM_real(wrk));
263
264 73192
        assert(wrk->vpi->handling == VCL_RET_DELIVER);
265
266 73192
        if (IS_TOPREQ(req) && RFC2616_Do_Cond(req))
267 690
                http_PutResponse(req->resp, "HTTP/1.1", 304, NULL);
268
269 73192
        req->req_step = R_STP_TRANSMIT;
270 73192
        return (REQ_FSM_MORE);
271 76229
}
272
273
/*--------------------------------------------------------------------
274
 * VCL failed, die horribly
275
 */
276
277
static enum req_fsm_nxt v_matchproto_(req_state_f)
278 3723
cnt_vclfail(struct worker *wrk, struct req *req)
279
{
280
        struct vrt_ctx ctx[1];
281
282 3723
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
283 3723
        CHECK_OBJ_NOTNULL(req, REQ_MAGIC);
284
285 3723
        AZ(req->objcore);
286 3723
        AZ(req->stale_oc);
287
288 3723
        INIT_OBJ(ctx, VRT_CTX_MAGIC);
289 3723
        VCL_Req2Ctx(ctx, req);
290
291 3723
        Req_Rollback(ctx);
292
293 3723
        if (req->req_reset) {
294 618
                req->err_code = 408;
295 618
                req->err_reason = "Client disconnected";
296 618
        } else {
297 3105
                req->err_code = 503;
298 3105
                req->err_reason = "VCL failed";
299
        }
300 3723
        req->req_step = R_STP_SYNTH;
301 3723
        req->doclose = SC_VCL_FAILURE;
302 3723
        req->vdp_filter_list = NULL;
303 3723
        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 12995
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 12995
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
320 12995
        CHECK_OBJ_NOTNULL(req, REQ_MAGIC);
321
322 12995
        AZ(req->objcore);
323 12995
        AZ(req->stale_oc);
324
325 12995
        wrk->stats->s_synth++;
326
327 12995
        if (req->err_code < 100)
328 0
                req->err_code = 501;
329
330 12995
        Resp_Setup_Synth(req);
331
332 12995
        req->vdp_filter_list = NULL;
333 12995
        synth_body = VSB_new_auto();
334 12995
        AN(synth_body);
335
336 12995
        req->t_resp = W_TIM_real(wrk);
337 12995
        VCL_synth_method(req->vcl, wrk, req, NULL, synth_body);
338
339 12995
        AZ(VSB_finish(synth_body));
340
341 12995
        VSLb_ts_req(req, "Process", W_TIM_real(wrk));
342
343 12995
        while (wrk->vpi->handling == VCL_RET_FAIL) {
344 713
                if (req->esi_level > 0) {
345 23
                        wrk->vpi->handling = VCL_RET_DELIVER;
346 23
                        break;
347
                }
348 690
                VSB_destroy(&synth_body);
349 690
                (void)VRB_Ignore(req);
350 690
                status = req->req_reset ? 408 : 500;
351 690
                (void)req->transport->minimal_response(req, status);
352 690
                req->doclose = SC_VCL_FAILURE; // XXX: Not necessary any more ?
353 690
                VSLb_ts_req(req, "Resp", W_TIM_real(wrk));
354 690
                http_Teardown(req->resp);
355 690
                return (REQ_FSM_DONE);
356
        }
357
358 12305
        if (wrk->vpi->handling == VCL_RET_RESTART && req->restarts > req->max_restarts)
359 23
                wrk->vpi->handling = VCL_RET_DELIVER;
360
361 12305
        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 253
                HTTP_Setup(req->resp, req->ws, req->vsl, SLT_RespMethod);
367 253
                VSB_destroy(&synth_body);
368 253
                req->req_step = R_STP_RESTART;
369 253
                return (REQ_FSM_MORE);
370
        }
371 12052
        assert(wrk->vpi->handling == VCL_RET_DELIVER);
372
373 12052
        http_Unset(req->resp, H_Content_Length);
374 24104
        http_PrintfHeader(req->resp, "Content-Length: %zd",
375 12052
            VSB_len(synth_body));
376
377 20838
        if (req->doclose == SC_NULL &&
378 8855
            (http_HdrIs(req->resp, H_Connection, "close") ||
379 8786
            http_HdrIs(req->http, H_Connection, "close")))
380 92
                req->doclose = SC_RESP_CLOSE;
381
382
        /* Discard any lingering request body before delivery */
383 12052
        (void)VRB_Ignore(req);
384
385 12052
        req->objcore = HSH_Private(wrk);
386 12052
        CHECK_OBJ_NOTNULL(req->objcore, OBJCORE_MAGIC);
387 12052
        szl = -1;
388 12052
        if (STV_NewObject(wrk, req->objcore, stv_transient, 0)) {
389 12006
                body = VSB_data(synth_body);
390 12006
                szl = VSB_len(synth_body);
391 12006
                assert(szl >= 0);
392 23299
                while (szl > 0) {
393 11293
                        sz = szl;
394 11293
                        if (! ObjGetSpace(wrk, req->objcore, &sz, &ptr)) {
395 0
                                szl = -1;
396 0
                                break;
397
                        }
398 11293
                        if (sz > szl)
399 0
                                sz = szl;
400 11293
                        szl -= sz;
401 11293
                        memcpy(ptr, body, sz);
402 11293
                        ObjExtend(wrk, req->objcore, sz, szl == 0 ? 1 : 0);
403 11293
                        body += sz;
404
                }
405 12006
        }
406
407 12052
        if (szl >= 0)
408 12005
                AZ(ObjSetU64(wrk, req->objcore, OA_LEN, VSB_len(synth_body)));
409 12052
        HSH_DerefBoc(wrk, req->objcore);
410 12052
        VSB_destroy(&synth_body);
411
412 12052
        if (szl < 0) {
413 46
                VSLb(req->vsl, SLT_Error, "Could not get storage");
414 46
                req->doclose = SC_OVERLOAD;
415 46
                VSLb_ts_req(req, "Resp", W_TIM_real(wrk));
416 46
                (void)HSH_DerefObjCore(wrk, &req->objcore);
417 46
                http_Teardown(req->resp);
418 46
                return (REQ_FSM_DONE);
419
        }
420
421 12006
        req->req_step = R_STP_TRANSMIT;
422 12006
        return (REQ_FSM_MORE);
423 12995
}
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 85191
cnt_transmit(struct worker *wrk, struct req *req)
431
{
432 85191
        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 85191
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
439 85191
        CHECK_OBJ_NOTNULL(req, REQ_MAGIC);
440 85191
        CHECK_OBJ_NOTNULL(req->transport, TRANSPORT_MAGIC);
441 85191
        CHECK_OBJ_NOTNULL(req->objcore, OBJCORE_MAGIC);
442 85191
        AZ(req->stale_oc);
443 85191
        AZ(req->res_pipe | req->res_esi);
444 85191
        AZ(req->boc);
445 85191
        req->req_step = R_STP_FINISH;
446
447
        /* Grab a ref to the bo if there is one (=streaming) */
448 85191
        req->boc = HSH_RefBoc(req->objcore);
449 85191
        if (req->boc && req->boc->state < BOS_STREAM)
450 0
                (void)ObjWaitState(req->objcore, BOS_STREAM);
451 85191
        clval = http_GetContentLength(req->resp);
452
        /* RFC 7230, 3.3.3 */
453 85191
        status = http_GetStatus(req->resp);
454 85191
        head = http_method_eq(req->http0->hd[HTTP_HDR_METHOD].b, HEAD);
455
456 85191
        if (req->boc != NULL || (req->objcore->flags & (OC_F_FAILED)))
457 20211
                req->resp_len = clval;
458
        else
459 64980
                req->resp_len = ObjGetLen(req->wrk, req->objcore);
460
461 85191
        if (head || status < 200 || status == 204 || status == 304) {
462
                // rfc7230,l,1748,1752
463 3889
                sendbody = 0;
464 3889
        } else {
465 81302
                sendbody = 1;
466
        }
467
468 85191
        VDP_Init(req->vdc, req->wrk, req->vsl, req, NULL, &req->resp_len);
469 85191
        if (req->vdp_filter_list == NULL)
470 78632
                req->vdp_filter_list = resp_Get_Filter_List(req);
471 85191
        if (req->vdp_filter_list == NULL ||
472 85193
            VCL_StackVDP(req->vdc, req->vcl, req->vdp_filter_list, req, NULL)) {
473 1461
                VSLb(req->vsl, SLT_Error, "Failure to push processors");
474 1461
                req->doclose = SC_OVERLOAD;
475 1461
                req->acct.resp_bodybytes +=
476 1461
                        VDP_Close(req->vdc, req->objcore, req->boc);
477 1461
        } else {
478 83750
                if (status < 200 || status == 204) {
479
                        // rfc7230,l,1691,1695
480 533
                        http_Unset(req->resp, H_Content_Length);
481 83750
                } else if (status == 304) {
482
                        // rfc7230,l,1675,1677
483 782
                        http_Unset(req->resp, H_Content_Length);
484 83217
                } else if (clval >= 0 && clval == req->resp_len) {
485
                        /* Reuse C-L header */
486 82435
                } else if (head) {
487
                        /* rfc9110,l,3226,3227
488
                         * "MAY send Content-Length ... [for] HEAD"
489
                         * do not touch to support cached HEAD #4245
490
                         */
491 92
                        req->resp_len = 0;
492 92
                } else {
493 21157
                        http_Unset(req->resp, H_Content_Length);
494 21157
                        if (req->resp_len >= 0)
495 25176
                                http_PrintfHeader(req->resp,
496 12588
                                    "Content-Length: %jd", req->resp_len);
497
                }
498 83750
                if (req->resp_len == 0)
499 23863
                        sendbody = 0;
500 83750
                dnxt = req->transport->deliver(req, sendbody);
501 83750
                if (dnxt == VTR_D_DISEMBARK)
502 1656
                        nxt = REQ_FSM_DISEMBARK;
503
                else
504 82094
                        assert(dnxt == VTR_D_DONE);
505
        }
506 85211
        return (nxt);
507
}
508
509
static enum req_fsm_nxt v_matchproto_(req_state_f)
510 85176
cnt_finish(struct worker *wrk, struct req *req)
511
{
512
513 85176
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
514 85176
        CHECK_OBJ_NOTNULL(req, REQ_MAGIC);
515
516 85176
        VSLb_ts_req(req, "Resp", W_TIM_real(wrk));
517
518 85176
        if (req->doclose == SC_NULL && (req->objcore->flags & OC_F_FAILED)) {
519
                /* The object we delivered failed due to a streaming error.
520
                 * Fail the request. */
521 368
                req->doclose = SC_TX_ERROR;
522 368
        }
523
524 85176
        if (req->boc != NULL) {
525 20172
                HSH_DerefBoc(wrk, req->objcore);
526 20172
                req->boc = NULL;
527 20172
        }
528
529 85176
        (void)HSH_DerefObjCore(wrk, &req->objcore);
530 85176
        http_Teardown(req->resp);
531
532 85176
        req->vdp_filter_list = NULL;
533 85176
        req->res_pipe = 0;
534 85176
        req->res_esi = 0;
535 85176
        return (REQ_FSM_DONE);
536
}
537
538
/*--------------------------------------------------------------------
539
 * Initiated a fetch (pass/miss) which we intend to deliver
540
 */
541
542
static enum req_fsm_nxt v_matchproto_(req_state_f)
543 50709
cnt_fetch(struct worker *wrk, struct req *req)
544
{
545
546 50709
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
547 50709
        CHECK_OBJ_NOTNULL(req, REQ_MAGIC);
548 50709
        CHECK_OBJ_NOTNULL(req->objcore, OBJCORE_MAGIC);
549 50709
        AZ(req->stale_oc);
550
551 50709
        wrk->stats->s_fetch++;
552 50709
        (void)VRB_Ignore(req);
553
554 50709
        if (req->objcore->flags & OC_F_FAILED) {
555 621
                req->err_code = 503;
556 621
                req->req_step = R_STP_SYNTH;
557 621
                (void)HSH_DerefObjCore(wrk, &req->objcore);
558 621
                AZ(req->objcore);
559 621
                return (REQ_FSM_MORE);
560
        }
561
562 50088
        req->req_step = R_STP_DELIVER;
563 50088
        return (REQ_FSM_MORE);
564 50709
}
565
566
/*--------------------------------------------------------------------
567
 * Attempt to lookup objhdr from hash.  We disembark and reenter
568
 * this state if we get suspended on a busy objhdr.
569
 */
570
571
static enum req_fsm_nxt v_matchproto_(req_state_f)
572 61264
cnt_lookup(struct worker *wrk, struct req *req)
573
{
574
        struct objcore *oc, *busy;
575
        enum lookup_e lr;
576
        int waitinglist_gen;
577
578 61264
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
579 61264
        CHECK_OBJ_NOTNULL(req, REQ_MAGIC);
580 61264
        AZ(req->stale_oc);
581
582 61264
        AN(req->vcl);
583
584 61264
        VRY_Prep(req);
585 61264
        waitinglist_gen = req->waitinglist_gen;
586
587 61264
        if (req->waitinglist_gen) {
588 1273
                CHECK_OBJ_NOTNULL(req->objcore, OBJCORE_MAGIC);
589 1273
                req->waitinglist_gen = 0;
590 1273
        } else
591 59991
                AZ(req->objcore);
592
593 61264
        wrk->strangelove = 0;
594 61264
        lr = HSH_Lookup(req, &oc, &busy);
595 61264
        if (lr == HSH_BUSY) {
596
                /*
597
                 * We lost the session to a busy object, disembark the
598
                 * worker thread.   We return to STP_LOOKUP when the busy
599
                 * object has been unbusied, and still have the objhead
600
                 * around to restart the lookup with.
601
                 */
602 1270
                return (REQ_FSM_DISEMBARK);
603
        }
604 59994
        assert(wrk->strangelove >= 0);
605 59994
        if ((unsigned)wrk->strangelove >= cache_param->vary_notice)
606 2852
                VSLb(req->vsl, SLT_Notice, "vsl: High number of variants (%d)",
607 1426
                    wrk->strangelove);
608 59994
        if (waitinglist_gen)
609 1271
                VSLb_ts_req(req, "Waitinglist", W_TIM_real(wrk));
610
611 59994
        if (req->vcf != NULL) {
612 138
                (void)req->vcf->func(req, NULL, NULL, 2);
613 138
                req->vcf = NULL;
614 138
        }
615
616 59994
        if (busy == NULL) {
617 24952
                VRY_Finish(req, DISCARD);
618 24952
        } else {
619 35042
                AN(busy->flags & OC_F_BUSY);
620 35042
                VRY_Finish(req, KEEP);
621
        }
622
623 59994
        AZ(req->objcore);
624 59994
        if (lr == HSH_MISS || lr == HSH_HITMISS) {
625 32878
                AN(busy);
626 32878
                AN(busy->flags & OC_F_BUSY);
627 32878
                req->objcore = busy;
628 32878
                req->stale_oc = oc;
629 32878
                req->req_step = R_STP_MISS;
630 32878
                if (lr == HSH_HITMISS)
631 828
                        req->is_hitmiss = 1;
632 32878
                return (REQ_FSM_MORE);
633
        }
634 27116
        if (lr == HSH_HITPASS) {
635 253
                AZ(busy);
636 253
                AZ(oc);
637 253
                req->req_step = R_STP_PASS;
638 253
                req->is_hitpass = 1;
639 253
                return (REQ_FSM_MORE);
640
        }
641
642 26863
        assert(lr == HSH_HIT || lr == HSH_GRACE);
643
644 26863
        CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC);
645 26863
        AZ(oc->flags & OC_F_BUSY);
646 26863
        req->objcore = oc;
647 26863
        AZ(oc->flags & OC_F_HFM);
648
649 26863
        VCL_hit_method(req->vcl, wrk, req, NULL, NULL);
650
651 26863
        switch (wrk->vpi->handling) {
652
        case VCL_RET_DELIVER:
653 26165
                if (busy != NULL) {
654 2001
                        AZ(oc->flags & OC_F_HFM);
655 2001
                        CHECK_OBJ_NOTNULL(busy->boc, BOC_MAGIC);
656
                        // XXX: shouldn't we go to miss?
657 2001
                        VBF_Fetch(wrk, req, busy, oc, VBF_BACKGROUND);
658 2001
                        wrk->stats->s_fetch++;
659 2001
                        wrk->stats->s_bgfetch++;
660 2001
                } else {
661 24164
                        (void)VRB_Ignore(req);// XXX: handle err
662
                }
663 26165
                wrk->stats->cache_hit++;
664 26165
                req->is_hit = 1;
665 26165
                if (lr == HSH_GRACE)
666 2070
                        wrk->stats->cache_hit_grace++;
667 26165
                req->req_step = R_STP_DELIVER;
668 26165
                return (REQ_FSM_MORE);
669
        case VCL_RET_RESTART:
670 329
                req->req_step = R_STP_RESTART;
671 329
                break;
672
        case VCL_RET_FAIL:
673 116
                req->req_step = R_STP_VCLFAIL;
674 116
                break;
675
        case VCL_RET_SYNTH:
676 207
                req->req_step = R_STP_SYNTH;
677 207
                break;
678
        case VCL_RET_PASS:
679 46
                wrk->stats->cache_hit++;
680 46
                req->is_hit = 1;
681 46
                req->req_step = R_STP_PASS;
682 46
                break;
683
        default:
684 0
                WRONG("Illegal return from vcl_hit{}");
685 0
        }
686
687
        /* Drop our object, we won't need it */
688 698
        (void)HSH_DerefObjCore(wrk, &req->objcore);
689
690 698
        if (busy != NULL) {
691 161
                HSH_Withdraw(wrk, &busy);
692 161
                VRY_Clear(req);
693 161
        }
694
695 698
        return (REQ_FSM_MORE);
696 61264
}
697
698
/*--------------------------------------------------------------------
699
 * Cache miss.
700
 */
701
702
static enum req_fsm_nxt v_matchproto_(req_state_f)
703 32857
cnt_miss(struct worker *wrk, struct req *req)
704
{
705
706 32857
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
707 32857
        CHECK_OBJ_NOTNULL(req, REQ_MAGIC);
708 32857
        AN(req->vcl);
709 32857
        CHECK_OBJ_NOTNULL(req->objcore, OBJCORE_MAGIC);
710 32857
        CHECK_OBJ_ORNULL(req->stale_oc, OBJCORE_MAGIC);
711
712 32857
        VCL_miss_method(req->vcl, wrk, req, NULL, NULL);
713 32857
        switch (wrk->vpi->handling) {
714
        case VCL_RET_FETCH:
715 32430
                wrk->stats->cache_miss++;
716 32430
                VBF_Fetch(wrk, req, req->objcore, req->stale_oc, VBF_NORMAL);
717 32430
                if (req->stale_oc != NULL)
718 1633
                        (void)HSH_DerefObjCore(wrk, &req->stale_oc);
719 32430
                req->req_step = R_STP_FETCH;
720 32430
                return (REQ_FSM_MORE);
721
        case VCL_RET_FAIL:
722 82
                req->req_step = R_STP_VCLFAIL;
723 82
                break;
724
        case VCL_RET_SYNTH:
725 184
                req->req_step = R_STP_SYNTH;
726 184
                break;
727
        case VCL_RET_RESTART:
728 92
                req->req_step = R_STP_RESTART;
729 92
                break;
730
        case VCL_RET_PASS:
731 69
                req->req_step = R_STP_PASS;
732 69
                break;
733
        default:
734 0
                WRONG("Illegal return from vcl_miss{}");
735 0
        }
736 427
        VRY_Clear(req);
737 427
        if (req->stale_oc != NULL)
738 23
                (void)HSH_DerefObjCore(wrk, &req->stale_oc);
739 427
        HSH_Withdraw(wrk, &req->objcore);
740 427
        return (REQ_FSM_MORE);
741 32857
}
742
743
/*--------------------------------------------------------------------
744
 * Pass processing
745
 */
746
747
static enum req_fsm_nxt v_matchproto_(req_state_f)
748 18331
cnt_pass(struct worker *wrk, struct req *req)
749
{
750
751 18331
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
752 18331
        CHECK_OBJ_NOTNULL(req, REQ_MAGIC);
753 18331
        AN(req->vcl);
754 18331
        AZ(req->objcore);
755 18331
        AZ(req->stale_oc);
756
757 18331
        VCL_pass_method(req->vcl, wrk, req, NULL, NULL);
758 18331
        switch (wrk->vpi->handling) {
759
        case VCL_RET_FAIL:
760 23
                req->req_step = R_STP_VCLFAIL;
761 23
                break;
762
        case VCL_RET_SYNTH:
763 23
                req->req_step = R_STP_SYNTH;
764 23
                break;
765
        case VCL_RET_RESTART:
766 0
                req->req_step = R_STP_RESTART;
767 0
                break;
768
        case VCL_RET_FETCH:
769 18285
                wrk->stats->s_pass++;
770 18285
                req->objcore = HSH_Private(wrk);
771 18285
                CHECK_OBJ_NOTNULL(req->objcore, OBJCORE_MAGIC);
772 18285
                VBF_Fetch(wrk, req, req->objcore, NULL, VBF_PASS);
773 18285
                req->req_step = R_STP_FETCH;
774 18285
                break;
775
        default:
776 0
                WRONG("Illegal return from cnt_pass{}");
777 0
        }
778 18331
        return (REQ_FSM_MORE);
779
}
780
781
/*--------------------------------------------------------------------
782
 * Pipe mode
783
 */
784
785
static enum req_fsm_nxt v_matchproto_(req_state_f)
786 690
cnt_pipe(struct worker *wrk, struct req *req)
787
{
788
        struct busyobj *bo;
789
        enum req_fsm_nxt nxt;
790
791 690
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
792 690
        CHECK_OBJ_NOTNULL(req, REQ_MAGIC);
793 690
        AZ(req->objcore);
794 690
        AZ(req->stale_oc);
795 690
        AN(req->vcl);
796
797 690
        wrk->stats->s_pipe++;
798 690
        bo = VBO_GetBusyObj(wrk, req);
799 690
        CHECK_OBJ_NOTNULL(bo, BUSYOBJ_MAGIC);
800 690
        VSLb(bo->vsl, SLT_Begin, "bereq %ju pipe", VXID(req->vsl->wid));
801 690
        VSLb(req->vsl, SLT_Link, "bereq %ju pipe", VXID(bo->vsl->wid));
802 690
        VSLb_ts_busyobj(bo, "Start", W_TIM_real(wrk));
803 690
        THR_SetBusyobj(bo);
804 690
        bo->sp = req->sp;
805 690
        SES_Ref(bo->sp);
806
807 690
        HTTP_Setup(bo->bereq, req->ws, bo->vsl, SLT_BereqMethod);
808 690
        http_FilterReq(bo->bereq, req->http, 0);        // XXX: 0 ?
809 690
        http_PrintfHeader(bo->bereq, "X-Varnish: %ju", VXID(req->vsl->wid));
810 690
        http_ForceHeader(bo->bereq, H_Connection, "close");
811
812 690
        if (req->want100cont) {
813 0
                http_SetHeader(bo->bereq, "Expect: 100-continue");
814 0
                req->want100cont = 0;
815 0
        }
816
817 690
        bo->wrk = wrk;
818 690
        bo->task_deadline = NAN; /* XXX: copy req->task_deadline */
819 690
        if (WS_Overflowed(req->ws))
820 23
                wrk->vpi->handling = VCL_RET_FAIL;
821
        else
822 667
                VCL_pipe_method(req->vcl, wrk, req, bo, NULL);
823
824 690
        switch (wrk->vpi->handling) {
825
        case VCL_RET_SYNTH:
826 69
                req->req_step = R_STP_SYNTH;
827 69
                nxt = REQ_FSM_MORE;
828 69
                break;
829
        case VCL_RET_PIPE:
830 575
                VSLb_ts_req(req, "Process", W_TIM_real(wrk));
831 575
                VSLb_ts_busyobj(bo, "Process", wrk->lastused);
832 575
                if (V1P_Enter() == 0) {
833 552
                        AZ(bo->req);
834 552
                        bo->req = req;
835 552
                        bo->wrk = wrk;
836
                        /* Unless cached, reqbody is not our job */
837 552
                        if (req->req_body_status != BS_CACHED)
838 529
                                req->req_body_status = BS_NONE;
839 552
                        SES_Close(req->sp, VDI_Http1Pipe(req, bo));
840 552
                        nxt = REQ_FSM_DONE;
841 552
                        V1P_Leave();
842 552
                        break;
843
                }
844 23
                wrk->stats->pipe_limited++;
845
                /* fall through */
846
        case VCL_RET_FAIL:
847 69
                req->req_step = R_STP_VCLFAIL;
848 69
                nxt = REQ_FSM_MORE;
849 69
                break;
850
        default:
851 0
                WRONG("Illegal return from vcl_pipe{}");
852 0
        }
853 690
        http_Teardown(bo->bereq);
854 690
        SES_Rel(bo->sp);
855 690
        VBO_ReleaseBusyObj(wrk, &bo);
856 690
        THR_SetBusyobj(NULL);
857 690
        return (nxt);
858
}
859
860
/*--------------------------------------------------------------------
861
 * Handle restart events
862
 */
863
864
static enum req_fsm_nxt v_matchproto_(req_state_f)
865 1663
cnt_restart(struct worker *wrk, struct req *req)
866
{
867
868 1663
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
869 1663
        CHECK_OBJ_NOTNULL(req, REQ_MAGIC);
870 1663
        AZ(req->objcore);
871 1663
        AZ(req->stale_oc);
872
873 1663
        if (++req->restarts > req->max_restarts) {
874 230
                VSLb(req->vsl, SLT_VCL_Error, "Too many restarts");
875 230
                req->err_code = 503;
876 230
                req->req_step = R_STP_SYNTH;
877 230
        } else {
878
                // XXX: ReqEnd + ReqAcct ?
879 1433
                VSLb_ts_req(req, "Restart", W_TIM_real(wrk));
880 2866
                VSL_ChgId(req->vsl, "req", "restart",
881 1433
                    VXID_Get(wrk, VSL_CLIENTMARKER));
882 1433
                VSLb_ts_req(req, "Start", req->t_prev);
883 1433
                req->err_code = 0;
884 1433
                req->req_step = R_STP_RECV;
885
        }
886 1663
        return (REQ_FSM_MORE);
887
}
888
889
/*
890
 * prepare the request for vcl_recv, either initially or after a reset
891
 * e.g. due to vcl switching
892
 *
893
 * TODO
894
 * - make restarts == 0 bit re-usable for rollback
895
 * - remove duplication with Req_Cleanup()
896
 */
897
898
static void v_matchproto_(req_state_f)
899 88464
cnt_recv_prep(struct req *req, const char *ci)
900
{
901
902 88464
        if (req->restarts == 0) {
903
                /*
904
                 * This really should be done earlier, but we want to capture
905
                 * it in the VSL log.
906
                 */
907 87031
                http_AppendHeader(req->http, H_X_Forwarded_For, ci);
908 87031
                http_AppendHeader(req->http, H_Via, http_ViaHeader());
909 87031
                http_CollectHdr(req->http, H_Cache_Control);
910
911
                /* By default we use the first backend */
912 174062
                VRT_Assign_Backend(&req->director_hint,
913 87031
                    VCL_DefaultDirector(req->vcl));
914
915 87031
                req->d_ttl = -1;
916 87031
                req->d_grace = -1;
917 87031
                req->disable_esi = 0;
918 87031
                req->hash_always_miss = 0;
919 87031
                req->hash_ignore_busy = 0;
920 87031
                req->hash_ignore_vary = 0;
921 87031
                req->client_identity = NULL;
922 87031
                req->storage = NULL;
923 87031
                req->trace = FEATURE(FEATURE_TRACE);
924 87031
        }
925
926 88464
        req->is_hit = 0;
927 88464
        req->is_hitmiss = 0;
928 88464
        req->is_hitpass = 0;
929 88464
        req->err_code = 0;
930 88464
        req->err_reason = NULL;
931
932 88464
        req->vfp_filter_list = NULL;
933 88464
}
934
935
/*--------------------------------------------------------------------
936
 * We have a complete request, set everything up and start it.
937
 * We can come here both with a request from the client and with
938
 * a interior request during ESI delivery.
939
 */
940
941
static enum req_fsm_nxt v_matchproto_(req_state_f)
942 88140
cnt_recv(struct worker *wrk, struct req *req)
943
{
944
        unsigned recv_handling;
945
        struct VSHA256Context sha256ctx;
946
        const char *ci;
947
948 88140
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
949 88140
        CHECK_OBJ_NOTNULL(req, REQ_MAGIC);
950 88140
        AN(req->vcl);
951 88140
        AZ(req->objcore);
952 88140
        AZ(req->stale_oc);
953 88140
        AZ(req->err_code);
954
955 88140
        AZ(isnan(req->t_first));
956 88140
        AZ(isnan(req->t_prev));
957 88140
        AZ(isnan(req->t_req));
958
959 88140
        ci = Req_LogStart(wrk, req);
960 88140
        http_VSL_log(req->http);
961
962 88140
        if (http_CountHdr(req->http0, H_Host) > 1) {
963 46
                VSLb(req->vsl, SLT_BogoHeader, "Multiple Host: headers");
964 46
                wrk->stats->client_req_400++;
965 46
                req->doclose = SC_RX_BAD;
966 46
                (void)req->transport->minimal_response(req, 400);
967 46
                return (REQ_FSM_DONE);
968
        }
969
970 88094
        if (http_CountHdr(req->http0, H_Content_Length) > 1) {
971 23
                VSLb(req->vsl, SLT_BogoHeader, "Multiple Content-Length: headers");
972 23
                wrk->stats->client_req_400++;
973 23
                req->doclose = SC_RX_BAD;
974 23
                (void)req->transport->minimal_response(req, 400);
975 23
                return (REQ_FSM_DONE);
976
        }
977
978 88071
        cnt_recv_prep(req, ci);
979
980 88071
        if (req->req_body_status == BS_ERROR) {
981 0
                req->doclose = SC_OVERLOAD;
982 0
                return (REQ_FSM_DONE);
983
        }
984
985 88071
        VCL_recv_method(req->vcl, wrk, req, NULL, NULL);
986
987 88071
        if (wrk->vpi->handling == VCL_RET_FAIL) {
988 1082
                req->req_step = R_STP_VCLFAIL;
989 1082
                return (REQ_FSM_MORE);
990
        }
991
992 86989
        if (wrk->vpi->handling == VCL_RET_VCL && req->restarts == 0) {
993
                // Req_Rollback has happened in VPI_vcl_select
994 299
                assert(WS_Snapshot(req->ws) == req->ws_req);
995 299
                cnt_recv_prep(req, ci);
996 299
                VCL_recv_method(req->vcl, wrk, req, NULL, NULL);
997 299
        }
998
999 86989
        if (req->want100cont && !req->late100cont) {
1000 92
                req->want100cont = 0;
1001 92
                if (req->transport->minimal_response(req, 100)) {
1002 0
                        req->doclose = SC_REM_CLOSE;
1003 0
                        return (REQ_FSM_DONE);
1004
                }
1005 92
        }
1006
1007
        /* Attempts to cache req.body may fail */
1008 86989
        if (req->req_body_status == BS_ERROR) {
1009 115
                req->doclose = SC_RX_BODY;
1010 115
                (void)req->transport->minimal_response(req, 400);
1011 115
                return (REQ_FSM_DONE);
1012
        }
1013
1014 86874
        recv_handling = wrk->vpi->handling;
1015
1016
        /* We wash the A-E header here for the sake of VRY */
1017 172901
        if (cache_param->http_gzip_support &&
1018 86763
             (recv_handling != VCL_RET_PIPE) &&
1019 86027
             (recv_handling != VCL_RET_PASS)) {
1020 68113
                if (RFC2616_Req_Gzip(req->http)) {
1021 5060
                        http_ForceHeader(req->http, H_Accept_Encoding, "gzip");
1022 5060
                } else {
1023 63053
                        http_Unset(req->http, H_Accept_Encoding);
1024
                }
1025 68113
        }
1026
1027 86874
        VSHA256_Init(&sha256ctx);
1028 86874
        VCL_hash_method(req->vcl, wrk, req, NULL, &sha256ctx);
1029 86874
        if (wrk->vpi->handling == VCL_RET_FAIL)
1030 238
                recv_handling = wrk->vpi->handling;
1031
        else
1032 86636
                assert(wrk->vpi->handling == VCL_RET_LOOKUP);
1033 86874
        VSHA256_Final(req->digest, &sha256ctx);
1034
1035 86874
        switch (recv_handling) {
1036
        case VCL_RET_VCL:
1037 184
                VSLb(req->vsl, SLT_VCL_Error,
1038
                    "Illegal return(vcl): %s",
1039 92
                    req->restarts ? "Not after restarts" :
1040
                    "Only from active VCL");
1041 92
                req->err_code = 503;
1042 92
                req->req_step = R_STP_SYNTH;
1043 92
                break;
1044
        case VCL_RET_PURGE:
1045 230
                req->req_step = R_STP_PURGE;
1046 230
                break;
1047
        case VCL_RET_HASH:
1048 59988
                req->req_step = R_STP_LOOKUP;
1049 59988
                break;
1050
        case VCL_RET_PIPE:
1051 736
                if (!IS_TOPREQ(req)) {
1052 0
                        VSLb(req->vsl, SLT_VCL_Error,
1053
                            "vcl_recv{} returns pipe for ESI included object."
1054
                            "  Doing pass.");
1055 0
                        req->req_step = R_STP_PASS;
1056 736
                } else if (req->http0->protover > 11) {
1057 46
                        VSLb(req->vsl, SLT_VCL_Error,
1058
                            "vcl_recv{} returns pipe for HTTP/2 request."
1059
                            "  Doing pass.");
1060 46
                        req->req_step = R_STP_PASS;
1061 46
                } else {
1062 690
                        req->req_step = R_STP_PIPE;
1063
                }
1064 736
                break;
1065
        case VCL_RET_PASS:
1066 17913
                req->req_step = R_STP_PASS;
1067 17913
                break;
1068
        case VCL_RET_SYNTH:
1069 7474
                req->req_step = R_STP_SYNTH;
1070 7474
                break;
1071
        case VCL_RET_RESTART:
1072 207
                req->req_step = R_STP_RESTART;
1073 207
                break;
1074
        case VCL_RET_FAIL:
1075 234
                req->req_step = R_STP_VCLFAIL;
1076 234
                break;
1077
        default:
1078 0
                WRONG("Illegal return from vcl_recv{}");
1079 0
        }
1080 86874
        return (REQ_FSM_MORE);
1081 88140
}
1082
1083
/*--------------------------------------------------------------------
1084
 * Find the objhead, purge it.
1085
 *
1086
 * In VCL, a restart is necessary to get a new object
1087
 */
1088
1089
static enum req_fsm_nxt v_matchproto_(req_state_f)
1090 230
cnt_purge(struct worker *wrk, struct req *req)
1091
{
1092
        struct objcore *oc, *boc;
1093
        enum lookup_e lr;
1094
1095 230
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
1096 230
        CHECK_OBJ_NOTNULL(req, REQ_MAGIC);
1097 230
        AZ(req->objcore);
1098 230
        AZ(req->stale_oc);
1099
1100 230
        AN(req->vcl);
1101
1102 230
        VRY_Prep(req);
1103
1104 230
        AZ(req->objcore);
1105 230
        req->hash_always_miss = 1;
1106 230
        lr = HSH_Lookup(req, &oc, &boc);
1107 230
        assert (lr == HSH_MISS);
1108 230
        AZ(oc);
1109 230
        CHECK_OBJ_NOTNULL(boc, OBJCORE_MAGIC);
1110 230
        VRY_Finish(req, DISCARD);
1111
1112 230
        (void)HSH_Purge(wrk, boc->objhead, req->t_req, 0, 0, 0);
1113
1114 230
        HSH_Withdraw(wrk, &boc);
1115
1116 230
        VCL_purge_method(req->vcl, wrk, req, NULL, NULL);
1117 230
        switch (wrk->vpi->handling) {
1118
        case VCL_RET_RESTART:
1119 69
                req->req_step = R_STP_RESTART;
1120 69
                break;
1121
        case VCL_RET_FAIL:
1122 23
                req->req_step = R_STP_VCLFAIL;
1123 23
                break;
1124
        case VCL_RET_SYNTH:
1125 138
                req->req_step = R_STP_SYNTH;
1126 138
                break;
1127
        default:
1128 0
                WRONG("Illegal return from vcl_purge{}");
1129 0
        }
1130 230
        return (REQ_FSM_MORE);
1131
}
1132
1133
/*--------------------------------------------------------------------
1134
 * Central state engine dispatcher.
1135
 *
1136
 * Kick the session around until it has had enough.
1137
 *
1138
 */
1139
1140
static void v_matchproto_(req_state_f)
1141 10276
cnt_diag(struct req *req, const char *state)
1142
{
1143
1144 10276
        CHECK_OBJ_NOTNULL(req, REQ_MAGIC);
1145
1146 20552
        VSLb(req->vsl,  SLT_Debug, "vxid %ju STP_%s sp %p vcl %p",
1147 10276
            VXID(req->vsl->wid), state, req->sp, req->vcl);
1148 10276
        VSL_Flush(req->vsl, 0);
1149 10276
}
1150
1151
void
1152 91511
CNT_Embark(struct worker *wrk, struct req *req)
1153
{
1154
1155 91511
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
1156 91511
        CHECK_OBJ_NOTNULL(req, REQ_MAGIC);
1157
1158
        /* wrk can have changed for restarts */
1159 91511
        req->vfc->wrk = req->wrk = wrk;
1160 91511
        wrk->vsl = req->vsl;
1161 91511
        if (req->req_step == R_STP_TRANSPORT && req->vcl == NULL) {
1162 78494
                VCL_Refresh(&wrk->wpriv->vcl);
1163 78494
                req->vcl = wrk->wpriv->vcl;
1164 78494
                wrk->wpriv->vcl = NULL;
1165 78494
                VSLbs(req->vsl, SLT_VCL_use, TOSTRAND(VCL_Name(req->vcl)));
1166 78494
        }
1167
1168 91511
        AN(req->vcl);
1169 91511
}
1170
1171
enum req_fsm_nxt
1172 89855
CNT_Request(struct req *req)
1173
{
1174
        struct vrt_ctx ctx[1];
1175
        struct worker *wrk;
1176
        enum req_fsm_nxt nxt;
1177
1178 89855
        CHECK_OBJ_NOTNULL(req, REQ_MAGIC);
1179
1180 89855
        wrk = req->wrk;
1181 89855
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
1182
1183 89855
        CHECK_OBJ_NOTNULL(req->transport, TRANSPORT_MAGIC);
1184 89855
        AN(req->transport->deliver);
1185 89855
        AN(req->transport->minimal_response);
1186
1187
        /*
1188
         * Possible entrance states
1189
         */
1190 89855
        assert(
1191
            req->req_step == R_STP_LOOKUP ||
1192
            req->req_step == R_STP_FINISH ||
1193
            req->req_step == R_STP_TRANSPORT);
1194
1195 89855
        AN(VXID_TAG(req->vsl->wid) & VSL_CLIENTMARKER);
1196 89855
        AN(req->vcl);
1197
1198 693902
        for (nxt = REQ_FSM_MORE; nxt == REQ_FSM_MORE; ) {
1199
                /*
1200
                 * This is a good place to be paranoid about the various
1201
                 * pointers still pointing to the things we expect.
1202
                 */
1203 604047
                CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
1204 604047
                CHECK_OBJ_NOTNULL(wrk->wpriv, WORKER_PRIV_MAGIC);
1205 604047
                CHECK_OBJ_ORNULL(wrk->wpriv->nobjhead, OBJHEAD_MAGIC);
1206 604047
                CHECK_OBJ_NOTNULL(req, REQ_MAGIC);
1207 604047
                CHECK_OBJ_NOTNULL(req->doclose, STREAM_CLOSE_MAGIC);
1208
1209 604047
                AN(req->req_step);
1210 604047
                AN(req->req_step->name);
1211 604047
                AN(req->req_step->func);
1212 604047
                if (DO_DEBUG(DBG_REQ_STATE))
1213 10276
                        cnt_diag(req, req->req_step->name);
1214 604047
                nxt = req->req_step->func(wrk, req);
1215 604047
                CHECK_OBJ_ORNULL(wrk->wpriv->nobjhead, OBJHEAD_MAGIC);
1216
        }
1217 89855
        wrk->vsl = NULL;
1218 89855
        if (nxt == REQ_FSM_DONE) {
1219 86765
                INIT_OBJ(ctx, VRT_CTX_MAGIC);
1220 86765
                VCL_Req2Ctx(ctx, req);
1221 86765
                if (IS_TOPREQ(req)) {
1222 78348
                        VCL_TaskLeave(ctx, req->top->privs);
1223 78348
                        if (req->top->vcl0 != NULL)
1224 276
                                VCL_Recache(wrk, &req->top->vcl0);
1225 78348
                }
1226 86765
                VCL_TaskLeave(ctx, req->privs);
1227 86765
                assert(!IS_NO_VXID(req->vsl->wid));
1228 86765
                VRB_Free(req);
1229 86765
                VRT_Assign_Backend(&req->director_hint, NULL);
1230 86765
                req->wrk = NULL;
1231 86765
        }
1232 89855
        assert(nxt == REQ_FSM_DISEMBARK || !WS_IsReserved(req->ws));
1233 89855
        return (nxt);
1234
}