Code Coverage: vinyl-cache/bin/vinyld/cache/cache_esi_deliver.c
 
82217aa4fc7094b3610340078afaf78cd95a8ec9
 
0
/*-
1
 * Copyright (c) 2011 Varnish Software AS
2
 * All rights reserved.
3
 *
4
 * Author: Poul-Henning Kamp <phk@phk.freebsd.dk>
5
 *
6
 * SPDX-License-Identifier: BSD-2-Clause
7
 *
8
 * Redistribution and use in source and binary forms, with or without
9
 * modification, are permitted provided that the following conditions
10
 * are met:
11
 * 1. Redistributions of source code must retain the above copyright
12
 *    notice, this list of conditions and the following disclaimer.
13
 * 2. Redistributions in binary form must reproduce the above copyright
14
 *    notice, this list of conditions and the following disclaimer in the
15
 *    documentation and/or other materials provided with the distribution.
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20
 * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
21
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27
 * SUCH DAMAGE.
28
 *
29
 * VED - Vinyl Esi Delivery
30
 */
31
32
#include "config.h"
33
34
#include "cache_int.h"
35
36
#include <stdlib.h>
37
38
#include "cache_transport.h"
39
#include "cache_filter.h"
40
#include "cache_vgz.h"
41
42
#include "vct.h"
43
#include "vtim.h"
44
#include "cache_esi.h"
45
#include "vend.h"
46
#include "vgz.h"
47
48
static vtr_deliver_f ved_deliver;
49
static vtr_reembark_f ved_reembark;
50
51
static const uint8_t gzip_hdr[] = {
52
        0x1f, 0x8b, 0x08,
53
        0x00, 0x00, 0x00, 0x00,
54
        0x00,
55
        0x02, 0x03
56
};
57
58
struct ecx {
59
        unsigned        magic;
60
#define ECX_MAGIC       0x0b0f9163
61
        const uint8_t   *p;
62
        const uint8_t   *e;
63
        int             state;
64
        ssize_t         l;
65
        int             isgzip;
66
        int             woken;
67
        int             abrt;
68
69
        struct req      *preq;
70
        struct ecx      *pecx;
71
        ssize_t         l_crc;
72
        uint32_t        crc;
73
};
74
75
static int v_matchproto_(vtr_minimal_response_f)
76
ved_minimal_response(struct req *req, uint16_t status)
77 0
{
78 0
        (void)req;
79 0
        (void)status;
80 0
        WRONG("esi:includes should not try minimal responses");
81 0
}
82
83
static const struct transport VED_transport = {
84
        .magic =                TRANSPORT_MAGIC,
85
        .name =                 "ESI_INCLUDE",
86
        .deliver =              ved_deliver,
87
        .reembark =             ved_reembark,
88
        .minimal_response =     ved_minimal_response,
89
};
90
91
/*--------------------------------------------------------------------*/
92
93
static void v_matchproto_(vtr_reembark_f)
94
ved_reembark(struct worker *wrk, struct req *req)
95 1
{
96 1
        struct ecx *ecx;
97
98 1
        (void)wrk;
99 1
        CHECK_OBJ_NOTNULL(req, REQ_MAGIC);
100 1
        CAST_OBJ_NOTNULL(ecx, req->transport_priv, ECX_MAGIC);
101 1
        Lck_Lock(&req->sp->mtx);
102 1
        ecx->woken = 1;
103 1
        PTOK(pthread_cond_signal(&ecx->preq->wrk->cond));
104 1
        Lck_Unlock(&req->sp->mtx);
105 1
}
106
107
/*--------------------------------------------------------------------*/
108
109
static void ved_run(struct worker *wrk, struct req *req);
110
111
static struct req *
112
ved_prep(struct req *preq, const char *src, const char *host,
113
    int isgzip)
114 4473
{
115 4473
        struct worker *wrk;
116 4473
        struct req *req;
117
118 4473
        CHECK_OBJ_NOTNULL(preq, REQ_MAGIC);
119 4473
        CHECK_OBJ_NOTNULL(preq->top, REQTOP_MAGIC);
120 4473
        wrk = preq->wrk;
121
122 4473
        if (preq->esi_level >= cache_param->max_esi_depth) {
123 66
                VSLb(preq->vsl, SLT_VCL_Error,
124 66
                    "ESI depth limit reached (param max_esi_depth = %u)",
125 66
                    cache_param->max_esi_depth);
126 66
                return (NULL);
127 66
        }
128
129 4407
        req = Req_New(preq->sp, preq);
130 4407
        AN(req);
131 4407
        assert(IS_NO_VXID(req->vsl->wid));
132 4407
        req->vsl->wid = VXID_Get(wrk, VSL_CLIENTMARKER);
133
134 4407
        wrk->stats->esi_req++;
135 4407
        req->esi_level = preq->esi_level + 1;
136
137 4407
        VSLb(req->vsl, SLT_Begin, "req %ju esi %u",
138 4407
            (uintmax_t)VXID(preq->vsl->wid), req->esi_level);
139 4407
        VSLb(preq->vsl, SLT_Link, "req %ju esi %u",
140 4407
            (uintmax_t)VXID(req->vsl->wid), req->esi_level);
141
142 4407
        VSLb_ts_req(req, "Start", W_TIM_real(wrk));
143
144 4407
        HTTP_Setup(req->http, req->ws, req->vsl, SLT_ReqMethod);
145 4407
        HTTP_Dup(req->http, preq->http0);
146
147 4407
        http_SetH(req->http, HTTP_HDR_URL, src);
148 4407
        if (host != NULL && *host != '\0')  {
149 2
                http_Unset(req->http, H_Host);
150 2
                http_SetHeader(req->http, host);
151 2
        }
152
153 4407
        http_ForceField(req->http, HTTP_HDR_METHOD, "GET");
154 4407
        http_ForceField(req->http, HTTP_HDR_PROTO, "HTTP/1.1");
155
156
        /* Don't allow conditionals, we can't use a 304 */
157 4407
        http_Unset(req->http, H_If_Modified_Since);
158 4407
        http_Unset(req->http, H_If_None_Match);
159
160
        /* Don't allow Range */
161 4407
        http_Unset(req->http, H_Range);
162
163
        /* Set Accept-Encoding according to what we want */
164 4407
        if (isgzip)
165 1502
                http_ForceHeader(req->http, H_Accept_Encoding, "gzip");
166 2905
        else
167 2905
                http_Unset(req->http, H_Accept_Encoding);
168
169
        /* Client content already taken care of */
170 4407
        http_Unset(req->http, H_Content_Length);
171 4407
        http_Unset(req->http, H_Transfer_Encoding);
172 4407
        req->req_body_status = BS_NONE;
173
174 4407
        AZ(req->vcl);
175 4407
        assert(req->top == preq->top);
176 4407
        if (req->top->vcl0)
177 2
                req->vcl = req->top->vcl0;
178 4405
        else
179 4405
                req->vcl = preq->vcl;
180 4407
        VCL_Ref(req->vcl);
181
182 4407
        assert(req->req_step == R_STP_TRANSPORT);
183 4407
        req->t_req = preq->t_req;
184
185 4407
        return (req);
186 4407
}
187
188
static void
189
ved_include(struct req *preq, const char *src, const char *host,
190
    struct ecx *ecx)
191 4473
{
192 4473
        struct req *req;
193
194 4473
        CHECK_OBJ_NOTNULL(ecx, ECX_MAGIC);
195 4473
        req = ved_prep(preq, src, host, ecx->isgzip);
196 4473
        if (req == NULL && ecx->abrt)
197 1
                preq->top->topreq->vdc->retval = -1;
198 4473
        if (req == NULL)
199 66
                return;
200 4407
        req->transport = &VED_transport;
201 4407
        req->transport_priv = ecx;
202 4407
        ved_run(preq->wrk, req);
203 4407
}
204
205
static void
206
ved_run(struct worker *wrk, struct req *req)
207 4407
{
208 4407
        struct req *preq = THR_GetRequest();
209 4407
        struct ecx *ecx;
210 4407
        struct sess *sp;
211 4407
        enum req_fsm_nxt s;
212
213 4407
        CHECK_OBJ_NOTNULL(req, REQ_MAGIC);
214 4407
        CAST_OBJ_NOTNULL(ecx, req->transport_priv, ECX_MAGIC);
215 4407
        sp = req->sp;
216 4407
        CHECK_OBJ_NOTNULL(sp, SESS_MAGIC);
217
218 4407
        THR_SetRequest(req);
219 4407
        VCL_TaskEnter(req->privs);
220
221 4408
        while (1) {
222 4408
                CNT_Embark(wrk, req);
223 4408
                ecx->woken = 0;
224 4408
                s = CNT_Request(req);
225 4408
                if (s == REQ_FSM_DONE)
226 4407
                        break;
227 1
                DSL(DBG_WAITINGLIST, req->vsl->wid,
228 1
                    "waiting for ESI (%d)", (int)s);
229 1
                assert(s == REQ_FSM_DISEMBARK);
230 1
                Lck_Lock(&sp->mtx);
231 1
                if (!ecx->woken)
232 1
                        (void)Lck_CondWait(&ecx->preq->wrk->cond, &sp->mtx);
233 1
                Lck_Unlock(&sp->mtx);
234 1
                AZ(req->wrk);
235 1
        }
236
237 4407
        VCL_Rel(&req->vcl);
238
239 4407
        req->wrk = NULL;
240 4407
        THR_SetRequest(preq);
241
242 4407
        Req_Cleanup(sp, wrk, req);
243 4407
        Req_Release(req);
244 4407
}
245
246
/*--------------------------------------------------------------------*/
247
248
//#define Debug(fmt, ...) printf(fmt, __VA_ARGS__)
249
#define Debug(fmt, ...) /**/
250
251
static ssize_t
252
ved_decode_len(struct vsl_log *vsl, const uint8_t **pp)
253 8410
{
254 8410
        const uint8_t *p;
255 8410
        ssize_t l;
256
257 8410
        p = *pp;
258 8410
        switch (*p & 15) {
259 8217
        case 1:
260 8217
                l = p[1];
261 8217
                p += 2;
262 8217
                break;
263 191
        case 2:
264 191
                l = vbe16dec(p + 1);
265 191
                p += 3;
266 191
                break;
267 2
        case 8:
268 2
                l = vbe64dec(p + 1);
269 2
                p += 9;
270 2
                break;
271 0
        default:
272 0
                VSLb(vsl, SLT_Error,
273 0
                    "ESI-corruption: Illegal Length %d %d\n", *p, (*p & 15));
274 0
                WRONG("ESI-codes: illegal length");
275 8410
        }
276 8410
        *pp = p;
277 8410
        assert(l > 0);
278 8410
        return (l);
279 8410
}
280
281
/*---------------------------------------------------------------------
282
 */
283
284
static int v_matchproto_(vdp_init_f)
285
ved_vdp_esi_init(VRT_CTX, struct vdp_ctx *vdc, void **priv)
286 555
{
287 555
        struct ecx *ecx;
288
289 555
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
290 555
        CHECK_OBJ_ORNULL(ctx->req, REQ_MAGIC);
291 555
        CHECK_OBJ_NOTNULL(vdc, VDP_CTX_MAGIC);
292 555
        CHECK_OBJ_ORNULL(vdc->oc, OBJCORE_MAGIC);
293 555
        CHECK_OBJ_NOTNULL(vdc->hp, HTTP_MAGIC);
294 555
        AN(vdc->clen);
295 555
        AN(priv);
296
297 555
        AZ(*priv);
298 555
        if (vdc->oc == NULL || !ObjHasAttr(vdc->wrk, vdc->oc, OA_ESIDATA))
299 0
                return (1);
300
301 555
        if (ctx->req == NULL) {
302 0
                VSLb(vdc->vsl, SLT_Error,
303 0
                     "esi can only be used on the client side");
304 0
                return (1);
305 0
        }
306
307 555
        ALLOC_OBJ(ecx, ECX_MAGIC);
308 555
        AN(ecx);
309 555
        ecx->preq = ctx->req;
310 555
        *priv = ecx;
311 555
        RFC2616_Weaken_Etag(vdc->hp);
312
313 555
        ctx->req->res_esi = 1;
314 555
        if (*vdc->clen != 0)
315 555
                *vdc->clen = -1;
316 555
        if (ctx->req->esi_level > 0) {
317 410
                assert(ctx->req->transport == &VED_transport);
318 410
                CAST_OBJ_NOTNULL(ecx->pecx, ctx->req->transport_priv, ECX_MAGIC);
319 410
                if (!ecx->pecx->isgzip)
320 226
                        ecx->pecx = NULL;
321 410
        }
322
323 555
        return (0);
324 555
}
325
326
static int v_matchproto_(vdp_fini_f)
327
ved_vdp_esi_fini(struct vdp_ctx *vdc, void **priv)
328 555
{
329 555
        struct ecx *ecx;
330
331 555
        (void)vdc;
332 555
        TAKE_OBJ_NOTNULL(ecx, priv, ECX_MAGIC);
333 555
        FREE_OBJ(ecx);
334 555
        return (0);
335 555
}
336
337
static int v_matchproto_(vdp_bytes_f)
338
ved_vdp_esi_bytes(struct vdp_ctx *vdc, enum vdp_action act, void **priv,
339
    const void *ptr, ssize_t len)
340 700
{
341 700
        const uint8_t *q, *r;
342 700
        ssize_t l = 0;
343 700
        uint32_t icrc = 0;
344 700
        uint8_t tailbuf[8 + 5];
345 700
        const uint8_t *pp;
346 700
        struct ecx *ecx;
347 700
        int retval = 0;
348
349 700
        if (act == VDP_END)
350 554
                act = VDP_FLUSH;
351
352 700
        AN(priv);
353 700
        CHECK_OBJ_NOTNULL(vdc, VDP_CTX_MAGIC);
354 700
        CAST_OBJ_NOTNULL(ecx, *priv, ECX_MAGIC);
355 700
        pp = ptr;
356
357 21144
        while (1) {
358 21144
                switch (ecx->state) {
359 554
                case 0:
360 554
                        ecx->p = ObjGetAttr(vdc->wrk, ecx->preq->objcore,
361 554
                            OA_ESIDATA, &l);
362 554
                        AN(ecx->p);
363 554
                        assert(l > 0);
364 554
                        ecx->e = ecx->p + l;
365
366 554
                        if (*ecx->p == VEC_GZ) {
367 226
                                if (ecx->pecx == NULL)
368 100
                                        retval = VDP_bytes(vdc, VDP_NULL,
369 100
                                            gzip_hdr, sizeof gzip_hdr);
370 226
                                ecx->l_crc = 0;
371 226
                                ecx->crc = crc32(0L, Z_NULL, 0);
372 226
                                ecx->isgzip = 1;
373 226
                                ecx->p++;
374 226
                        }
375 554
                        ecx->state = 1;
376 554
                        break;
377 12459
                case 1:
378 12459
                        if (ecx->p >= ecx->e) {
379 544
                                ecx->state = 2;
380 544
                                break;
381 544
                        }
382 11915
                        switch (*ecx->p) {
383 2617
                        case VEC_V1:
384 2618
                        case VEC_V2:
385 2619
                        case VEC_V8:
386 2619
                                ecx->l = ved_decode_len(vdc->vsl, &ecx->p);
387 2619
                                if (ecx->l < 0)
388 0
                                        return (-1);
389 2619
                                if (ecx->isgzip) {
390 969
                                        assert(*ecx->p == VEC_C1 ||
391 969
                                            *ecx->p == VEC_C2 ||
392 969
                                            *ecx->p == VEC_C8);
393 969
                                        l = ved_decode_len(vdc->vsl, &ecx->p);
394 969
                                        if (l < 0)
395 0
                                                return (-1);
396 969
                                        icrc = vbe32dec(ecx->p);
397 969
                                        ecx->p += 4;
398 969
                                        ecx->crc = crc32_combine(
399 969
                                            ecx->crc, icrc, l);
400 969
                                        ecx->l_crc += l;
401 969
                                }
402 2619
                                ecx->state = 3;
403 2619
                                break;
404 4820
                        case VEC_S1:
405 4821
                        case VEC_S2:
406 4822
                        case VEC_S8:
407 4822
                                ecx->l = ved_decode_len(vdc->vsl, &ecx->p);
408 4822
                                if (ecx->l < 0)
409 0
                                        return (-1);
410 4822
                                Debug("SKIP1(%d)\n", (int)ecx->l);
411 4822
                                ecx->state = 4;
412 4822
                                break;
413 4341
                        case VEC_IA:
414 4341
                                ecx->abrt =
415 4341
                                    ecx->preq->esi_include_onerror;
416
                                /* FALLTHROUGH */
417 4474
                        case VEC_IC:
418 4474
                                if (VDP_bytes(vdc, VDP_FLUSH, NULL, 0)) {
419 1
                                        ecx->p = ecx->e;
420 1
                                        break;
421 1
                                }
422 4473
                                ecx->p++;
423 4473
                                q = (const void*)strchr((const char*)ecx->p, '\0');
424 4473
                                AN(q);
425 4473
                                q++;
426 4473
                                r = (const void*)strchr((const char*)q, '\0');
427 4473
                                AN(r);
428 4473
                                Debug("INCL [%s][%s] BEGIN\n", q, ecx->p);
429 4473
                                ved_include(ecx->preq,
430 4473
                                    (const char*)q, (const char*)ecx->p, ecx);
431 4473
                                Debug("INCL [%s][%s] END\n", q, ecx->p);
432 4473
                                ecx->p = r + 1;
433 4473
                                break;
434 0
                        default:
435 0
                                VSLb(vdc->vsl, SLT_Error,
436 0
                                    "ESI corruption line %d 0x%02x [%s]\n",
437 0
                                    __LINE__, *ecx->p, ecx->p);
438 0
                                WRONG("ESI-codes: Illegal code");
439 11915
                        }
440 11915
                        break;
441 11915
                case 2:
442 544
                        ptr = NULL;
443 544
                        len = 0;
444 544
                        if (ecx->isgzip && ecx->pecx == NULL) {
445
                                /*
446
                                 * We are bytealigned here, so simply emit
447
                                 * a gzip literal block with finish bit set.
448
                                 */
449 100
                                tailbuf[0] = 0x01;
450 100
                                tailbuf[1] = 0x00;
451 100
                                tailbuf[2] = 0x00;
452 100
                                tailbuf[3] = 0xff;
453 100
                                tailbuf[4] = 0xff;
454
455
                                /* Emit CRC32 */
456 100
                                vle32enc(tailbuf + 5, ecx->crc);
457
458
                                /* MOD(2^32) length */
459 100
                                vle32enc(tailbuf + 9, ecx->l_crc);
460
461 100
                                ptr = tailbuf;
462 100
                                len = 13;
463 444
                        } else if (ecx->pecx != NULL) {
464 184
                                ecx->pecx->crc = crc32_combine(ecx->pecx->crc,
465 184
                                    ecx->crc, ecx->l_crc);
466 184
                                ecx->pecx->l_crc += ecx->l_crc;
467 184
                        }
468 544
                        retval = VDP_bytes(vdc, VDP_END, ptr, len);
469 544
                        ecx->state = 99;
470 544
                        return (retval);
471 2641
                case 3:
472 7577
                case 4:
473
                        /*
474
                         * There is no guarantee that the 'l' bytes are all
475
                         * in the same storage segment, so loop over storage
476
                         * until we have processed them all.
477
                         */
478 7577
                        if (ecx->l <= len) {
479 7441
                                if (ecx->state == 3)
480 2619
                                        retval = VDP_bytes(vdc, act,
481 2619
                                            pp, ecx->l);
482 7441
                                len -= ecx->l;
483 7441
                                pp += ecx->l;
484 7441
                                ecx->state = 1;
485 7441
                                break;
486 7441
                        }
487 136
                        if (ecx->state == 3 && len > 0)
488 22
                                retval = VDP_bytes(vdc, act, pp, len);
489 136
                        ecx->l -= len;
490 136
                        return (retval);
491 10
                case 99:
492
                        /*
493
                         * VEP does not account for the PAD+CRC+LEN
494
                         * so we can see up to approx 15 bytes here.
495
                         */
496 10
                        return (retval);
497 0
                default:
498 0
                        WRONG("FOO");
499 0
                        break;
500 21144
                }
501 20453
                if (retval)
502 10
                        return (retval);
503 20453
        }
504 700
}
505
506
const struct vdp VDP_esi = {
507
        .name =         "esi",
508
        .init =         ved_vdp_esi_init,
509
        .bytes =        ved_vdp_esi_bytes,
510
        .fini =         ved_vdp_esi_fini,
511
};
512
513
/*
514
 * Account body bytes on req
515
 * Push bytes to preq
516
 */
517
static inline int
518
ved_bytes(struct ecx *ecx, enum vdp_action act,
519
    const void *ptr, ssize_t len)
520 47205
{
521 47205
        if (act == VDP_END)
522 3515
                act = VDP_FLUSH;
523 47205
        return (VDP_bytes(ecx->preq->vdc, act, ptr, len));
524 47205
}
525
526
/*---------------------------------------------------------------------
527
 * If a gzipped ESI object includes a ungzipped object, we need to make
528
 * it looked like a gzipped data stream.  The official way to do so would
529
 * be to fire up libvgz and gzip it, but we don't, we fake it.
530
 *
531
 * First, we cannot know if it is ungzipped on purpose, the admin may
532
 * know something we don't.
533
 *
534
 * What do you mean "BS ?"
535
 *
536
 * All right then...
537
 *
538
 * The matter of the fact is that we simply will not fire up a gzip in
539
 * the output path because it costs too much memory and CPU, so we simply
540
 * wrap the data in very convenient "gzip copy-blocks" and send it down
541
 * the stream with a bit more overhead.
542
 */
543
544
static int v_matchproto_(vdp_fini_f)
545
ved_pretend_gzip_fini(struct vdp_ctx *vdc, void **priv)
546 1218
{
547 1218
        (void)vdc;
548 1218
        *priv = NULL;
549 1218
        return (0);
550 1218
}
551
552
static int v_matchproto_(vdp_bytes_f)
553
ved_pretend_gzip_bytes(struct vdp_ctx *vdc, enum vdp_action act, void **priv,
554
    const void *pv, ssize_t l)
555 6485
{
556 6485
        uint8_t buf1[5], buf2[5];
557 6485
        const uint8_t *p;
558 6485
        uint16_t lx;
559 6485
        struct ecx *ecx;
560
561 6485
        CHECK_OBJ_NOTNULL(vdc, VDP_CTX_MAGIC);
562 6485
        CAST_OBJ_NOTNULL(ecx, *priv, ECX_MAGIC);
563
564 6485
        (void)priv;
565 6485
        if (l == 0)
566 1690
                return (ved_bytes(ecx, act, pv, l));
567
568 4795
        p = pv;
569
570 4795
        AN (ecx->isgzip);
571 4795
        ecx->crc = crc32(ecx->crc, p, l);
572 4795
        ecx->l_crc += l;
573
574
        /*
575
         * buf1 can safely be emitted multiple times for objects longer
576
         * than 64K-1 bytes.
577
         */
578 4795
        lx = 65535;
579 4795
        buf1[0] = 0;
580 4795
        vle16enc(buf1 + 1, lx);
581 4795
        vle16enc(buf1 + 3, ~lx);
582
583 9591
        while (l > 0) {
584 4796
                if (l >= 65535) {
585 0
                        lx = 65535;
586 0
                        if (ved_bytes(ecx, VDP_NULL, buf1, sizeof buf1))
587 0
                                return (-1);
588 4796
                } else {
589 4796
                        lx = (uint16_t)l;
590 4796
                        buf2[0] = 0;
591 4796
                        vle16enc(buf2 + 1, lx);
592 4796
                        vle16enc(buf2 + 3, ~lx);
593 4796
                        if (ved_bytes(ecx, VDP_NULL, buf2, sizeof buf2))
594 0
                                return (-1);
595 4796
                }
596 4796
                if (ved_bytes(ecx, VDP_NULL, p, lx))
597 0
                        return (-1);
598 4796
                l -= lx;
599 4796
                p += lx;
600 4796
        }
601
        /* buf1 & buf2 are local, so we have to flush */
602 4795
        return (ved_bytes(ecx, VDP_FLUSH, NULL, 0));
603 4795
}
604
605
static const struct vdp ved_pretend_gz = {
606
        .name =         "PGZ",
607
        .bytes =        ved_pretend_gzip_bytes,
608
        .fini =         ved_pretend_gzip_fini,
609
};
610
611
/*---------------------------------------------------------------------
612
 * Include a gzipped object in a gzipped ESI object delivery
613
 *
614
 * This is the interesting case: Deliver all the deflate blocks, stripping
615
 * the "LAST" bit of the last one and padding it, as necessary, to a byte
616
 * boundary.
617
 *
618
 */
619
620
struct ved_foo {
621
        unsigned                magic;
622
#define VED_FOO_MAGIC           0x6a5a262d
623
        struct ecx              *ecx;
624
        struct objcore          *objcore;
625
        uint64_t                start, last, stop, lpad;
626
        ssize_t                 ll;
627
        uint64_t                olen;
628
        uint8_t                 dbits[8];
629
        uint8_t                 tailbuf[8];
630
};
631
632
static int v_matchproto_(vdp_init_f)
633
ved_gzgz_init(VRT_CTX, struct vdp_ctx *vdc, void **priv)
634 154
{
635 154
        ssize_t l;
636 154
        const char *p;
637 154
        struct ved_foo *foo;
638
639 154
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
640 154
        CHECK_OBJ_NOTNULL(vdc, VDP_CTX_MAGIC);
641 154
        AN(priv);
642
643 154
        CAST_OBJ_NOTNULL(foo, *priv, VED_FOO_MAGIC);
644 154
        CHECK_OBJ_NOTNULL(foo->objcore, OBJCORE_MAGIC);
645
646 154
        memset(foo->tailbuf, 0xdd, sizeof foo->tailbuf);
647
648 154
        AN(ObjCheckFlag(vdc->wrk, foo->objcore, OF_GZIPED));
649
650 154
        p = ObjGetAttr(vdc->wrk, foo->objcore, OA_GZIPBITS, &l);
651 154
        AN(p);
652 154
        assert(l == 32);
653 154
        foo->start = vbe64dec(p);
654 154
        foo->last = vbe64dec(p + 8);
655 154
        foo->stop = vbe64dec(p + 16);
656 154
        foo->olen = ObjGetLen(vdc->wrk, foo->objcore);
657 154
        assert(foo->start > 0 && foo->start < foo->olen * 8);
658 154
        assert(foo->last > 0 && foo->last < foo->olen * 8);
659 154
        assert(foo->stop > 0 && foo->stop < foo->olen * 8);
660 154
        assert(foo->last >= foo->start);
661 154
        assert(foo->last < foo->stop);
662
663
        /* The start bit must be byte aligned. */
664 154
        AZ(foo->start & 7);
665 154
        return (0);
666 154
}
667
668
/*
669
 * XXX: for act == VDP_END || act == VDP_FLUSH, we send a flush more often than
670
 * we need. The VDP_END case would trip our "at most one VDP_END call" assertion
671
 * in VDP_bytes(), but ved_bytes() covers it.
672
 *
673
 * To avoid unnecessary chunks downstream, it would be nice to re-structure the
674
 * code to identify the last block, send VDP_END/VDP_FLUSH for that one and
675
 * VDP_NULL for anything before it.
676
 */
677
678
static int v_matchproto_(vdp_bytes_f)
679
ved_gzgz_bytes(struct vdp_ctx *vdc, enum vdp_action act, void **priv,
680
    const void *ptr, ssize_t len)
681 202
{
682 202
        struct ved_foo *foo;
683 202
        const uint8_t *pp;
684 202
        ssize_t dl;
685 202
        ssize_t l;
686
687 202
        (void)vdc;
688 202
        CAST_OBJ_NOTNULL(foo, *priv, VED_FOO_MAGIC);
689 202
        pp = ptr;
690 202
        if (len > 0) {
691
                /* Skip over the GZIP header */
692 202
                dl = foo->start / 8 - foo->ll;
693 202
                if (dl > 0) {
694
                        /* Before foo.start, skip */
695 157
                        if (dl > len)
696 3
                                dl = len;
697 157
                        foo->ll += dl;
698 157
                        len -= dl;
699 157
                        pp += dl;
700 157
                }
701 202
        }
702 202
        if (len > 0) {
703
                /* The main body of the object */
704 199
                dl = foo->last / 8 - foo->ll;
705 199
                if (dl > 0) {
706 103
                        dl = vmin(dl, len);
707 103
                        if (ved_bytes(foo->ecx, act, pp, dl))
708 0
                                return (-1);
709 103
                        foo->ll += dl;
710 103
                        len -= dl;
711 103
                        pp += dl;
712 103
                }
713 199
        }
714 202
        if (len > 0 && foo->ll == foo->last / 8) {
715
                /* Remove the "LAST" bit */
716 154
                foo->dbits[0] = *pp;
717 154
                foo->dbits[0] &= ~(1U << (foo->last & 7));
718 154
                if (ved_bytes(foo->ecx, act, foo->dbits, 1))
719 0
                        return (-1);
720 154
                foo->ll++;
721 154
                len--;
722 154
                pp++;
723 154
        }
724 202
        if (len > 0) {
725
                /* Last block */
726 169
                dl = foo->stop / 8 - foo->ll;
727 169
                if (dl > 0) {
728 83
                        dl = vmin(dl, len);
729 83
                        if (ved_bytes(foo->ecx, act, pp, dl))
730 0
                                return (-1);
731 83
                        foo->ll += dl;
732 83
                        len -= dl;
733 83
                        pp += dl;
734 83
                }
735 169
        }
736 202
        if (len > 0 && (foo->stop & 7) && foo->ll == foo->stop / 8) {
737
                /* Add alignment to byte boundary */
738 146
                foo->dbits[1] = *pp;
739 146
                foo->ll++;
740 146
                len--;
741 146
                pp++;
742 146
                switch ((int)(foo->stop & 7)) {
743 2
                case 1: /*
744
                         * x000....
745
                         * 00000000 00000000 11111111 11111111
746
                         */
747 4
                case 3: /*
748
                         * xxx000..
749
                         * 00000000 00000000 11111111 11111111
750
                         */
751 6
                case 5: /*
752
                         * xxxxx000
753
                         * 00000000 00000000 11111111 11111111
754
                         */
755 6
                        foo->dbits[2] = 0x00; foo->dbits[3] = 0x00;
756 6
                        foo->dbits[4] = 0xff; foo->dbits[5] = 0xff;
757 6
                        foo->lpad = 5;
758 6
                        break;
759 76
                case 2: /* xx010000 00000100 00000001 00000000 */
760 76
                        foo->dbits[1] |= 0x08;
761 76
                        foo->dbits[2] = 0x20;
762 76
                        foo->dbits[3] = 0x80;
763 76
                        foo->dbits[4] = 0x00;
764 76
                        foo->lpad = 4;
765 76
                        break;
766 2
                case 4: /* xxxx0100 00000001 00000000 */
767 2
                        foo->dbits[1] |= 0x20;
768 2
                        foo->dbits[2] = 0x80;
769 2
                        foo->dbits[3] = 0x00;
770 2
                        foo->lpad = 3;
771 2
                        break;
772 4
                case 6: /* xxxxxx01 00000000 */
773 4
                        foo->dbits[1] |= 0x80;
774 4
                        foo->dbits[2] = 0x00;
775 4
                        foo->lpad = 2;
776 4
                        break;
777 58
                case 7: /*
778
                         * xxxxxxx0
779
                         * 00......
780
                         * 00000000 00000000 11111111 11111111
781
                         */
782 58
                        foo->dbits[2] = 0x00;
783 58
                        foo->dbits[3] = 0x00; foo->dbits[4] = 0x00;
784 58
                        foo->dbits[5] = 0xff; foo->dbits[6] = 0xff;
785 58
                        foo->lpad = 6;
786 58
                        break;
787 0
                case 0: /* xxxxxxxx */
788 0
                default:
789 0
                        WRONG("compiler must be broken");
790 146
                }
791 146
                if (ved_bytes(foo->ecx, act, foo->dbits + 1, foo->lpad))
792 0
                        return (-1);
793 146
        }
794 202
        if (len > 0) {
795
                /* Recover GZIP tail */
796 163
                dl = foo->olen - foo->ll;
797 163
                assert(dl >= 0);
798 163
                if (dl > len)
799 9
                        dl = len;
800 163
                if (dl > 0) {
801 163
                        assert(dl <= 8);
802 163
                        l = foo->ll - (foo->olen - 8);
803 163
                        assert(l >= 0);
804 163
                        assert(l <= 8);
805 163
                        assert(l + dl <= 8);
806 163
                        vmemcpy(foo->tailbuf + l, pp, dl);
807 163
                        foo->ll += dl;
808 163
                        len -= dl;
809 163
                }
810 163
        }
811 202
        assert(len == 0);
812 202
        return (0);
813 202
}
814
815
static int v_matchproto_(vdp_fini_f)
816
ved_gzgz_fini(struct vdp_ctx *vdc, void **priv)
817 154
{
818 154
        uint32_t icrc;
819 154
        uint32_t ilen;
820 154
        struct ved_foo *foo;
821
822 154
        (void)vdc;
823 154
        TAKE_OBJ_NOTNULL(foo, priv, VED_FOO_MAGIC);
824
825
        /* XXX
826
         * this works due to the esi layering, a VDP pushing bytes from _fini
827
         * will otherwise have its own _bytes method called.
828
         *
829
         * Could rewrite use VDP_END
830
         */
831 154
        (void)ved_bytes(foo->ecx, VDP_FLUSH, NULL, 0);
832
833 154
        icrc = vle32dec(foo->tailbuf);
834 154
        ilen = vle32dec(foo->tailbuf + 4);
835 154
        foo->ecx->crc = crc32_combine(foo->ecx->crc, icrc, ilen);
836 154
        foo->ecx->l_crc += ilen;
837
838 154
        return (0);
839 154
}
840
841
static const struct vdp ved_gzgz = {
842
        .name =         "VZZ",
843
        .init =         ved_gzgz_init,
844
        .bytes =        ved_gzgz_bytes,
845
        .fini =         ved_gzgz_fini,
846
};
847
848
/*--------------------------------------------------------------------
849
 * Straight through without processing.
850
 */
851
852
static int v_matchproto_(vdp_fini_f)
853
ved_vdp_fini(struct vdp_ctx *vdc, void **priv)
854 3015
{
855 3015
        (void)vdc;
856 3015
        *priv = NULL;
857 3015
        return (0);
858 3015
}
859
860
static int v_matchproto_(vdp_bytes_f)
861
ved_vdp_bytes(struct vdp_ctx *vdc, enum vdp_action act, void **priv,
862
    const void *ptr, ssize_t len)
863 30489
{
864 30489
        struct ecx *ecx;
865
866 30489
        (void)vdc;
867 30489
        CAST_OBJ_NOTNULL(ecx, *priv, ECX_MAGIC);
868 30489
        return (ved_bytes(ecx, act, ptr, len));
869 30489
}
870
871
static const struct vdp ved_ved = {
872
        .name =         "VED",
873
        .bytes =        ved_vdp_bytes,
874
        .fini =         ved_vdp_fini,
875
};
876
877
static void
878
ved_close(struct req *req, int error)
879 4407
{
880 4407
        req->acct.resp_bodybytes += VDP_Close(req->vdc, req->objcore, req->boc);
881
882 4407
        if (! error)
883 4403
                return;
884 4
        req->top->topreq->vdc->retval = -1;
885 4
        req->top->topreq->doclose = req->doclose;
886 4
}
887
888
/*--------------------------------------------------------------------*/
889
890
static enum vtr_deliver_e v_matchproto_(vtr_deliver_f)
891
ved_deliver(struct req *req, int wantbody)
892 4406
{
893 4406
        int i = 0;
894 4406
        const char *p;
895 4406
        uint16_t status;
896 4406
        struct ecx *ecx;
897 4406
        struct ved_foo foo[1];
898 4406
        struct vrt_ctx ctx[1];
899
900 4406
        CHECK_OBJ_NOTNULL(req, REQ_MAGIC);
901 4406
        CHECK_OBJ_ORNULL(req->boc, BOC_MAGIC);
902 4406
        CHECK_OBJ_NOTNULL(req->objcore, OBJCORE_MAGIC);
903
904 4406
        CAST_OBJ_NOTNULL(ecx, req->transport_priv, ECX_MAGIC);
905
906 4406
        status = req->resp->status % 1000;
907
908 4406
        if (FEATURE(FEATURE_ESI_INCLUDE_ONERROR) &&
909 4406
            status != 200 && status != 204) {
910 5
                ved_close(req, ecx->abrt);
911 5
                return (VTR_D_DONE);
912 5
        }
913
914 4401
        if (wantbody == 0) {
915 13
                ved_close(req, 0);
916 13
                return (VTR_D_DONE);
917 13
        }
918
919 4388
        if (req->boc == NULL && ObjGetLen(req->wrk, req->objcore) == 0) {
920 0
                ved_close(req, 0);
921 0
                return (VTR_D_DONE);
922 0
        }
923
924 4388
        if (http_GetHdr(req->resp, H_Content_Encoding, &p))
925 281
                i = http_coding_eq(p, gzip);
926 4388
        if (i)
927 281
                i = ObjCheckFlag(req->wrk, req->objcore, OF_GZIPED);
928
929 4388
        INIT_OBJ(ctx, VRT_CTX_MAGIC);
930 4388
        VCL_Req2Ctx(ctx, req);
931
932 4388
        if (ecx->isgzip && i && !req->res_esi) {
933
                /* A gzipped include which is not ESI processed */
934
935
                /* OA_GZIPBITS are not valid until BOS_FINISHED */
936 155
                if (req->boc != NULL)
937 42
                        (void)ObjWaitState(req->objcore, BOS_FINISHED);
938
939 155
                if (req->objcore->flags & OC_F_FAILED) {
940
                        /* No way of signalling errors in the middle of
941
                         * the ESI body. Omit this ESI fragment.
942
                         * XXX change error argument to 1
943
                         */
944 1
                        ved_close(req, 0);
945 1
                        return (VTR_D_DONE);
946 1
                }
947
948 154
                INIT_OBJ(foo, VED_FOO_MAGIC);
949 154
                foo->ecx = ecx;
950 154
                foo->objcore = req->objcore;
951 154
                i = VDP_Push(ctx, req->vdc, req->ws, &ved_gzgz, foo);
952 4233
        } else if (ecx->isgzip && !i) {
953
                /* Non-Gzip'ed include in gzipped parent */
954 1218
                i = VDP_Push(ctx, req->vdc, req->ws, &ved_pretend_gz, ecx);
955 3015
        } else {
956
                /* Anything else goes straight through */
957 3015
                i = VDP_Push(ctx, req->vdc, req->ws, &ved_ved, ecx);
958 3015
        }
959
960 4387
        if (i == 0) {
961 4387
                i = VDP_DeliverObj(req->vdc, req->objcore);
962 4387
        } else {
963 0
                VSLb(req->vsl, SLT_Error, "Failure to push ESI processors");
964 0
                req->doclose = SC_OVERLOAD;
965 0
        }
966
967 4387
        if (i && req->doclose == SC_NULL)
968 6
                req->doclose = SC_REM_CLOSE;
969
970 4387
        ved_close(req, i && ecx->abrt ? 1 : 0);
971 4387
        return (VTR_D_DONE);
972 4388
}