Code Coverage: vinyl-cache/bin/vinyld/http2/cache_http2_deliver.c
 
82217aa4fc7094b3610340078afaf78cd95a8ec9
 
0
/*-
1
 * Copyright (c) 2016 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
 */
30
31
#include "config.h"
32
33
#include "cache/cache_int.h"
34
35
#include <ctype.h>
36
#include <stdio.h>
37
38
#include "cache/cache_filter.h"
39
#include "cache/cache_transport.h"
40
41
#include "http2/cache_http2.h"
42
43
#include "vct.h"
44
45
/**********************************************************************/
46
47
struct hpack_static {
48
        uint8_t                 idx;
49
        const char *            name;
50
        const char *            val;
51
};
52
53
static const struct hpack_static hp_static[] = {
54
#define HPS(I,N,V) [I] = { I, N ":", V },
55
#include "tbl/vhp_static.h"
56
        { 0, "\377:", ""}               // Terminator
57
};
58
59
static const struct hpack_static *hp_idx[256];
60
61
void
62
V2D_Init(void)
63 1026
{
64 1026
        int i;
65 1026
#define HPS(I,N,V)                                              \
66 62586
        i = hp_static[I].name[0];                               \
67 62586
        if (hp_idx[i] == NULL) hp_idx[i] = &hp_static[I];
68 1026
#include "tbl/vhp_static.h"
69 1026
#undef HPS
70 1026
}
71
72
/**********************************************************************/
73
74
static int v_matchproto_(vdp_init_f)
75
h2_init(VRT_CTX, struct vdp_ctx *vdc, void **priv)
76 41
{
77 41
        struct h2_req *r2;
78
79 41
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
80 41
        CHECK_OBJ_NOTNULL(vdc, VDP_CTX_MAGIC);
81 41
        AN(priv);
82 41
        CAST_OBJ_NOTNULL(r2, *priv, H2_REQ_MAGIC);
83 41
        (void)r2;
84 41
        return (0);
85 41
}
86
87
static int v_matchproto_(vdp_fini_f)
88
h2_fini(struct vdp_ctx *vdc, void **priv)
89 41
{
90 41
        struct h2_req *r2;
91
92 41
        CHECK_OBJ_NOTNULL(vdc, VDP_CTX_MAGIC);
93 41
        CHECK_OBJ_NOTNULL(vdc->wrk, WORKER_MAGIC);
94 41
        TAKE_OBJ_NOTNULL(r2, priv, H2_REQ_MAGIC);
95
96 41
        if (r2->error)
97 8
                return (0);
98
99 33
        if (vdc->retval < 0) {
100 4
                r2->error = H2SE_INTERNAL_ERROR; /* XXX: proper error? */
101 4
                H2_Send_Get(vdc->wrk, r2->h2sess, r2);
102 4
                H2_Send_RST(vdc->wrk, r2->h2sess, r2, r2->stream, r2->error);
103 4
                H2_Send_Rel(r2->h2sess, r2);
104 4
                return (0);
105 4
        }
106
107 29
        H2_Send_Get(vdc->wrk, r2->h2sess, r2);
108 29
        H2_Send(vdc->wrk, r2, H2_F_DATA, H2FF_DATA_END_STREAM, 0, "", NULL);
109 29
        H2_Send_Rel(r2->h2sess, r2);
110 29
        return (0);
111 33
}
112
113
static int v_matchproto_(vdp_bytes_f)
114
h2_bytes(struct vdp_ctx *vdc, enum vdp_action act, void **priv,
115
    const void *ptr, ssize_t len)
116 172
{
117 172
        struct h2_req *r2;
118
119 172
        CHECK_OBJ_NOTNULL(vdc, VDP_CTX_MAGIC);
120 172
        CAST_OBJ_NOTNULL(r2, *priv, H2_REQ_MAGIC);
121 172
        (void)act;
122
123 172
        if ((r2->h2sess->error || r2->error))
124 7
                return (-1);
125 165
        if (len == 0)
126 23
                return (0);
127 142
        H2_Send_Get(vdc->wrk, r2->h2sess, r2);
128 142
        vdc->bytes_done = 0;
129 142
        H2_Send(vdc->wrk, r2, H2_F_DATA, H2FF_NONE, len, ptr, &vdc->bytes_done);
130 142
        H2_Send_Rel(r2->h2sess, r2);
131 142
        return (0);
132 165
}
133
134
static const struct vdp h2_vdp = {
135
        .name =         "H2B",
136
        .init =         h2_init,
137
        .bytes =        h2_bytes,
138
        .fini =         h2_fini,
139
};
140
141
static inline size_t
142 142
h2_status(uint8_t *p, uint16_t status) {
143 142
        size_t l = 1;
144
145 142
        switch (status) {
146 98
        case 200: *p = 0x80 |  8; break;
147 0
        case 204: *p = 0x80 |  9; break;
148 2
        case 206: *p = 0x80 | 10; break;
149 3
        case 304: *p = 0x80 | 11; break;
150 1
        case 400: *p = 0x80 | 12; break;
151 1
        case 404: *p = 0x80 | 13; break;
152 1
        case 500: *p = 0x80 | 14; break;
153 36
        default:
154 36
                *p++ = 0x18;
155 36
                *p++ = 0x03;
156 36
                l = 2;
157
158 36
                l += snprintf((char*)p, 4, "%03d", status);
159 36
                assert(l == 5);
160 36
                break;
161 142
        }
162
163 142
        return (l);
164 142
}
165
166
int v_matchproto_(vtr_minimal_response_f)
167
h2_minimal_response(struct req *req, uint16_t status)
168 30
{
169 30
        struct h2_req *r2;
170 30
        size_t l;
171 30
        uint8_t buf[6];
172
173 30
        CHECK_OBJ_NOTNULL(req, REQ_MAGIC);
174 30
        CAST_OBJ_NOTNULL(r2, req->transport_priv, H2_REQ_MAGIC);
175
176 30
        assert(status >= 100);
177 30
        assert(status < 1000);
178
179 30
        l = h2_status(buf, status);
180 30
        assert(l < sizeof(buf));
181
182 30
        VSLb(req->vsl, SLT_RespProtocol, "HTTP/2.0");
183 30
        VSLb(req->vsl, SLT_RespStatus, "%03d", status);
184 30
        VSLbs(req->vsl, SLT_RespReason,
185 30
              TOSTRAND(http_Status2Reason(status, NULL)));
186
187 30
        if (status >= 400)
188 28
                req->err_code = status;
189
190
        /* XXX return code checking once H2_Send returns anything but 0 */
191 30
        H2_Send_Get(req->wrk, r2->h2sess, r2);
192 30
        H2_Send(req->wrk, r2,
193 30
            H2_F_HEADERS,
194 30
            H2FF_HEADERS_END_HEADERS |
195 30
                (status < 200 ? 0 : H2FF_HEADERS_END_STREAM),
196 30
            l, buf, NULL);
197 30
        H2_Send_Rel(r2->h2sess, r2);
198 30
        return (0);
199 30
}
200
201
static void
202
h2_enc_len(struct vsb *vsb, unsigned bits, unsigned val, uint8_t b0)
203 1708
{
204 1708
        assert(bits < 8);
205 1708
        unsigned mask = (1U << bits) - 1U;
206
207 1708
        if (val >= mask) {
208 644
                VSB_putc(vsb, b0 | (uint8_t)mask);
209 644
                val -= mask;
210 649
                while (val >= 128) {
211 5
                        VSB_putc(vsb, 0x80 | ((uint8_t)val & 0x7f));
212 5
                        val >>= 7;
213 5
                }
214 644
        }
215 1708
        VSB_putc(vsb, (uint8_t)val);
216 1708
}
217
218
/*
219
 * Hand-crafted-H2-HEADERS-R-Us:
220
 *
221
 * This is a handbuilt HEADERS frame for when we run out of workspace
222
 * during delivery.
223
 */
224
225
static const uint8_t h2_500_resp[] = {
226
        // :status 500
227
        0x8e,
228
229
        // content-length 0
230
        0x1f, 0x0d, 0x01, 0x30,
231
232
        // server Vinyl-Cache
233
        0x1f, 0x27, 0x0b, 'V', 'i', 'n', 'y', 'l', '-', 'C', 'a', 'c', 'h', 'e'
234
};
235
236
static void
237
h2_build_headers(struct vsb *resp, struct req *req)
238 112
{
239 112
        unsigned u, l;
240 112
        int i;
241 112
        struct http *hp;
242 112
        const char *r;
243 112
        const struct hpack_static *hps;
244 112
        uint8_t buf[6];
245 112
        ssize_t sz, sz1;
246
247 112
        assert(req->resp->status % 1000 >= 100);
248 112
        l = h2_status(buf, req->resp->status % 1000);
249 112
        VSB_bcat(resp, buf, l);
250
251 112
        hp = req->resp;
252 970
        for (u = HTTP_HDR_FIRST; u < hp->nhd && !VSB_error(resp); u++) {
253 858
                r = strchr(hp->hd[u].b, ':');
254 858
                AN(r);
255
256 858
                if (http_IsFiltered(hp, u, HTTPH_C_SPECIFIC))
257 3
                        continue; //rfc7540,l,2999,3006
258
259 855
                hps = hp_idx[tolower(*hp->hd[u].b)];
260 855
                sz = 1 + r - hp->hd[u].b;
261 855
                assert(sz > 0);
262 2620
                while (hps != NULL && hps->idx > 0) {
263 2494
                        i = strncasecmp(hps->name, hp->hd[u].b, sz);
264 2494
                        if (i < 0) {
265 1765
                                hps++;
266 1765
                                continue;
267 1765
                        }
268 729
                        if (i > 0)
269 94
                                hps = NULL;
270 729
                        break;
271 2494
                }
272 855
                if (hps != NULL) {
273 639
                        VSLb(req->vsl, SLT_Debug,
274 639
                            "HP {%d, \"%s\", \"%s\"} <%s>",
275 639
                            hps->idx, hps->name, hps->val, hp->hd[u].b);
276 639
                        h2_enc_len(resp, 4, hps->idx, 0x10);
277 639
                } else {
278 216
                        VSB_putc(resp, 0x10);
279 216
                        sz--;
280 216
                        h2_enc_len(resp, 7, sz, 0);
281 1911
                        for (sz1 = 0; sz1 < sz; sz1++)
282 1695
                                VSB_putc(resp, tolower(hp->hd[u].b[sz1]));
283
284 216
                }
285
286 1710
                while (vct_islws(*++r))
287 855
                        continue;
288 855
                sz = hp->hd[u].e - r;
289 855
                h2_enc_len(resp, 7, sz, 0);
290 855
                VSB_bcat(resp, r, sz);
291 855
        }
292 112
}
293
294
enum vtr_deliver_e v_matchproto_(vtr_deliver_f)
295
h2_deliver(struct req *req, int sendbody)
296 112
{
297 112
        size_t sz;
298 112
        const char *r;
299 112
        struct h2_req *r2;
300 112
        struct vsb resp[1];
301 112
        struct vrt_ctx ctx[1];
302 112
        uintptr_t ss;
303
304 112
        CHECK_OBJ_NOTNULL(req, REQ_MAGIC);
305 112
        CHECK_OBJ_NOTNULL(req->objcore, OBJCORE_MAGIC);
306 112
        CAST_OBJ_NOTNULL(r2, req->transport_priv, H2_REQ_MAGIC);
307 112
        CHECK_OBJ_NOTNULL(req->sp, SESS_MAGIC);
308
309 112
        VSLb(req->vsl, SLT_RespProtocol, "HTTP/2.0");
310
311 112
        (void)http_DoConnection(req->resp, SC_RESP_CLOSE);
312
313 112
        ss = WS_Snapshot(req->ws);
314
315 112
        WS_VSB_new(resp, req->ws);
316 112
        h2_build_headers(resp, req);
317 112
        r = WS_VSB_finish(resp, req->ws, &sz);
318
319 112
        if (r == NULL) {
320 1
                VSLb(req->vsl, SLT_Error, "workspace_client overflow");
321 1
                VSLb(req->vsl, SLT_RespStatus, "500");
322 1
                VSLb(req->vsl, SLT_RespReason, "Internal Server Error");
323 1
                req->wrk->stats->client_resp_500++;
324
325 1
                r = (const char*)h2_500_resp;
326 1
                sz = sizeof h2_500_resp;
327 1
                sendbody = 0;
328 1
        }
329
330 112
        r2->t_send = req->t_prev;
331
332 112
        H2_Send_Get(req->wrk, r2->h2sess, r2);
333 112
        H2_Send(req->wrk, r2, H2_F_HEADERS,
334 112
            (sendbody ? 0 : H2FF_HEADERS_END_STREAM) | H2FF_HEADERS_END_HEADERS,
335 112
            sz, r, &req->acct.resp_hdrbytes);
336 112
        H2_Send_Rel(r2->h2sess, r2);
337
338 112
        WS_Reset(req->ws, ss);
339
340 112
        if (sendbody) {
341 41
                INIT_OBJ(ctx, VRT_CTX_MAGIC);
342 41
                VCL_Req2Ctx(ctx, req);
343 41
                if (! VDP_Push(ctx, req->vdc, req->ws, &h2_vdp, r2) &&
344 41
                    VDP_DeliverObj(req->vdc, req->objcore) < 0)
345 10
                        req->vdc->retval = -1;
346
347 41
        }
348
349 112
        req->acct.resp_bodybytes += VDP_Close(req->vdc, req->objcore, req->boc);
350 112
        return (VTR_D_DONE);
351 112
}