varnish-cache/bin/varnishd/http2/cache_http2_deliver.c
1
/*-
2
 * Copyright (c) 2016 Varnish Software AS
3
 * All rights reserved.
4
 *
5
 * Author: Poul-Henning Kamp <phk@phk.freebsd.dk>
6
 *
7
 * Redistribution and use in source and binary forms, with or without
8
 * modification, are permitted provided that the following conditions
9
 * are met:
10
 * 1. Redistributions of source code must retain the above copyright
11
 *    notice, this list of conditions and the following disclaimer.
12
 * 2. Redistributions in binary form must reproduce the above copyright
13
 *    notice, this list of conditions and the following disclaimer in the
14
 *    documentation and/or other materials provided with the distribution.
15
 *
16
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19
 * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
20
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26
 * SUCH DAMAGE.
27
 *
28
 */
29
30
#include "config.h"
31
32
#include "cache/cache_varnishd.h"
33
34
#include <netinet/in.h>
35
36
#include <ctype.h>
37
#include <stdio.h>
38
39
#include "cache/cache_filter.h"
40
#include "cache/cache_transport.h"
41
42
#include "http2/cache_http2.h"
43
44
#include "vct.h"
45
46
/**********************************************************************/
47
48
struct hpack_static {
49
        uint8_t                 idx;
50
        const char *            name;
51
        const char *            val;
52
};
53
54
static const struct hpack_static hp_static[] = {
55
#define HPS(I,N,V) [I] = { I, N ":", V },
56
#include "tbl/vhp_static.h"
57
        { 0, "\377:", ""}               // Terminator
58
};
59
60
static const struct hpack_static *hp_idx[256];
61
62
void
63 1228
V2D_Init(void)
64
{
65
        int i;
66
#define HPS(I,N,V)                                              \
67
        i = hp_static[I].name[0];                               \
68
        if (hp_idx[i] == NULL) hp_idx[i] = &hp_static[I];
69
#include "tbl/vhp_static.h"
70
#undef HPS
71 1228
}
72
73
/**********************************************************************/
74
75
static int v_matchproto_(vdp_bytes)
76 110
h2_bytes(struct req *req, enum vdp_action act, void **priv,
77
    const void *ptr, ssize_t len)
78
{
79
        struct h2_req *r2;
80
81 110
        CHECK_OBJ_NOTNULL(req, REQ_MAGIC);
82 110
        CAST_OBJ_NOTNULL(r2, req->transport_priv, H2_REQ_MAGIC);
83
        (void)priv;
84
85 110
        if (act == VDP_INIT)
86 34
                return (0);
87 76
        if (r2->error && act != VDP_FINI)
88 4
                return (-1);
89 72
        H2_Send_Get(req->wrk, r2->h2sess, r2);
90 72
        H2_Send(req->wrk, r2,
91
            H2_F_DATA,
92
            act == VDP_FINI ? H2FF_DATA_END_STREAM : H2FF_NONE,
93
            len, ptr);
94 72
        req->acct.resp_bodybytes += len;
95 72
        H2_Send_Rel(r2->h2sess, r2);
96 72
        return (0);
97
}
98
99
static const struct vdp h2_vdp = {
100
        .name =         "H2B",
101
        .func =         h2_bytes,
102
};
103
104
static inline size_t
105 74
h2_status(uint8_t *p, uint16_t status) {
106 74
        size_t l = 1;
107
108 74
        switch (status) {
109 52
        case 200: *p = 0x80 |  8; break;
110 0
        case 204: *p = 0x80 |  9; break;
111 2
        case 206: *p = 0x80 | 10; break;
112 6
        case 304: *p = 0x80 | 11; break;
113 0
        case 400: *p = 0x80 | 12; break;
114 2
        case 404: *p = 0x80 | 13; break;
115 2
        case 500: *p = 0x80 | 14; break;
116
        default:
117 10
                *p++ = 0x18;
118 10
                *p++ = 0x03;
119 10
                l = 2;
120
121 10
                l += snprintf((char*)p, 4, "%03d", status);
122 10
                assert(l == 5);
123 10
                break;
124
        }
125
126 74
        return (l);
127
}
128
129
int v_matchproto_(vtr_minimal_response_f)
130 4
h2_minimal_response(struct req *req, uint16_t status)
131
{
132
        struct h2_req *r2;
133
        size_t l;
134
        uint8_t buf[6];
135
136 4
        CHECK_OBJ_NOTNULL(req, REQ_MAGIC);
137 4
        CAST_OBJ_NOTNULL(r2, req->transport_priv, H2_REQ_MAGIC);
138
139 4
        assert(status >= 100);
140 4
        assert(status < 1000);
141
142 4
        l = h2_status(buf, status);
143 4
        assert(l < sizeof(buf));
144
145 4
        VSLb(req->vsl, SLT_RespProtocol, "HTTP/2.0");
146 4
        VSLb(req->vsl, SLT_RespStatus, "%03d", status);
147 4
        VSLb(req->vsl, SLT_RespReason, "%s", http_Status2Reason(status, NULL));
148
149 4
        if (status >= 400)
150 0
                req->err_code = status;
151
152
        /* XXX return code checking once H2_Send returns anything but 0 */
153 4
        H2_Send_Get(req->wrk, r2->h2sess, r2);
154 4
        H2_Send(req->wrk, r2,
155
            H2_F_HEADERS,
156
            H2FF_HEADERS_END_HEADERS |
157
                (status < 200 ? 0 : H2FF_HEADERS_END_STREAM),
158
            l, buf);
159 4
        H2_Send_Rel(r2->h2sess, r2);
160 4
        return (0);
161
}
162
163
static uint8_t *
164 908
h2_enc_len(uint8_t *p, unsigned bits, unsigned val)
165
{
166 908
        assert(bits < 8);
167 908
        unsigned mask = (1U << bits) - 1U;
168
169 908
        if (val >= mask) {
170 382
                *p++ |= (uint8_t)mask;
171 382
                val -= mask;
172 768
                while (val >= 128) {
173 4
                        *p++ = 0x80 | ((uint8_t)val & 0x7f);
174 4
                        val >>= 7;
175
                }
176
        }
177 908
        *p++ = (uint8_t)val;
178 908
        return (p);
179
}
180
181
void v_matchproto_(vtr_deliver_f)
182 70
h2_deliver(struct req *req, struct boc *boc, int sendbody)
183
{
184
        ssize_t sz, sz1;
185
        uint8_t *p;
186
        unsigned u;
187
        const char *r;
188
        struct http *hp;
189
        struct sess *sp;
190
        struct h2_req *r2;
191
        int i, err;
192
        const struct hpack_static *hps;
193
194 70
        CHECK_OBJ_NOTNULL(req, REQ_MAGIC);
195 70
        CHECK_OBJ_ORNULL(boc, BOC_MAGIC);
196 70
        CHECK_OBJ_NOTNULL(req->objcore, OBJCORE_MAGIC);
197 70
        CAST_OBJ_NOTNULL(r2, req->transport_priv, H2_REQ_MAGIC);
198 70
        sp = req->sp;
199 70
        CHECK_OBJ_NOTNULL(sp, SESS_MAGIC);
200
201 70
        (void)WS_Reserve(req->ws, 0);
202 70
        p = (void*)req->ws->f;
203
204 70
        p += h2_status(p, req->resp->status);
205
206 70
        hp = req->resp;
207 524
        for (u = HTTP_HDR_FIRST; u < hp->nhd; u++) {
208 454
                assert(WS_Inside(req->ws, p, NULL));
209
210 454
                r = strchr(hp->hd[u].b, ':');
211 454
                AN(r);
212
213 454
                hps = hp_idx[tolower(*hp->hd[u].b)];
214 454
                sz = 1 + r - hp->hd[u].b;
215 454
                assert(sz > 0);
216 1958
                while (hps != NULL && hps->idx > 0) {
217 1434
                        i = strncasecmp(hps->name, hp->hd[u].b, sz);
218 1434
                        if (i < 0) {
219 1050
                                hps++;
220 1050
                                continue;
221
                        }
222 384
                        if (i > 0)
223 6
                                hps = NULL;
224 384
                        break;
225
                }
226 454
                if (hps != NULL) {
227 756
                        VSLb(req->vsl, SLT_Debug,
228
                            "HP {%d, \"%s\", \"%s\"} <%s>",
229 756
                            hps->idx, hps->name, hps->val, hp->hd[u].b);
230 378
                        *p = 0x10;
231 378
                        p = h2_enc_len(p, 4, hps->idx);
232
                } else {
233
234 76
                        *p++ = 0x10;
235 76
                        sz--;
236 76
                        p = h2_enc_len(p, 7, sz);
237 1238
                        for (sz1 = 0; sz1 < sz; sz1++)
238 1162
                                *p++ = (uint8_t)tolower(hp->hd[u].b[sz1]);
239
240
                }
241
242 1362
                while (vct_islws(*++r))
243 454
                        continue;
244 454
                sz = hp->hd[u].e - r;
245 454
                p = h2_enc_len(p, 7, sz);
246 454
                memcpy(p, r, sz);
247 454
                p += sz;
248 454
                assert(WS_Inside(req->ws, p, NULL));
249
        }
250 70
        sz = (char*)p - req->ws->f;
251
252 70
        AZ(req->wrk->v1l);
253
254
        /* XXX: Optimize !sendbody case */
255
256 70
        if (sendbody && req->resp_len == 0)
257 30
                sendbody = 0;
258
259 70
        H2_Send_Get(req->wrk, r2->h2sess, r2);
260 70
        H2_Send(req->wrk, r2, H2_F_HEADERS,
261
            (sendbody ? 0 : H2FF_HEADERS_END_STREAM) | H2FF_HEADERS_END_HEADERS,
262 70
            sz, req->ws->f);
263 70
        req->acct.resp_hdrbytes += sz;
264 70
        H2_Send_Rel(r2->h2sess, r2);
265
266 70
        WS_Release(req->ws, 0);
267
268 70
        if (sendbody) {
269 34
                VDP_push(req, &h2_vdp, NULL, 1);
270 34
                err = VDP_DeliverObj(req);
271
                /*XXX*/(void)err;
272
        }
273
274 70
        AZ(req->wrk->v1l);
275 70
        VDP_close(req);
276 70
}