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