Code Coverage: vinyl-cache/bin/vinyld/cache/cache_http.c
 
82217aa4fc7094b3610340078afaf78cd95a8ec9
 
0
/*-
1
 * Copyright (c) 2006 Verdens Gang AS
2
 * Copyright (c) 2006-2017 Varnish Software AS
3
 * Copyright 2026 UPLEX - Nils Goroll Systemoptimierung
4
 * All rights reserved.
5
 *
6
 * Author: Poul-Henning Kamp <phk@phk.freebsd.dk>
7
 *
8
 * SPDX-License-Identifier: BSD-2-Clause
9
 *
10
 * Redistribution and use in source and binary forms, with or without
11
 * modification, are permitted provided that the following conditions
12
 * are met:
13
 * 1. Redistributions of source code must retain the above copyright
14
 *    notice, this list of conditions and the following disclaimer.
15
 * 2. Redistributions in binary form must reproduce the above copyright
16
 *    notice, this list of conditions and the following disclaimer in the
17
 *    documentation and/or other materials provided with the distribution.
18
 *
19
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22
 * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
23
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29
 * SUCH DAMAGE.
30
 *
31
 * HTTP request storage and manipulation
32
 */
33
34
#include "config.h"
35
36
#include "cache_int.h"
37
#include <stdio.h>
38
#include <stdlib.h>
39
40
#include "common/heritage.h"
41
42
#include "vct.h"
43
#include "vend.h"
44
#include "vnum.h"
45
#include "vtim.h"
46
47
#define BODYSTATUS(U, l, n, a, k)                               \
48
        const struct body_status BS_##U[1] = {{                 \
49
                .name = #l,                                     \
50
                .nbr = n,                                       \
51
                .avail = a,                                     \
52
                .length_known = k                               \
53
        }};
54
#include "tbl/body_status.h"
55
56
57
#define HTTPH(a, b, c) hdr_t b = HDR(a);
58
#include "tbl/http_headers.h"
59
60
hdr_t H__Status = HDR(":status");
61
hdr_t H__Proto  = HDR(":proto");
62
hdr_t H__Reason = HDR(":reason");
63
64
static char * via_hdr;
65
66
/*--------------------------------------------------------------------
67
 * Perfect hash to rapidly recognize headers from tbl/http_headers.h
68
 * which have non-zero flags.
69
 *
70
 * A suitable algorithm can be found with `gperf`:
71
 *
72
 *      tr '" ,' '   ' < include/tbl/http_headers.h |
73
 *              awk '$1 == "H(" {print $2}' |
74
 *              gperf --ignore-case
75
 *
76
 */
77
78 188250
#define GPERF_MIN_WORD_LENGTH 2
79 93994
#define GPERF_MAX_WORD_LENGTH 19
80 94046
#define GPERF_MAX_HASH_VALUE 79
81
82
static const unsigned char http_asso_values[256] = {
83
        80, 80, 80, 80, 80, 80, 80, 80, 80, 80,
84
        80, 80, 80, 80, 80, 80, 80, 80, 80, 80,
85
        80, 80, 80, 80, 80, 80, 80, 80, 80, 80,
86
        80, 80, 80, 80, 80, 80, 80, 80, 80, 80,
87
        80, 80, 80, 80, 80, 80, 80, 80, 80, 80,
88
        80, 80, 80,  0, 80, 80, 80, 80, 80, 80,
89
        80, 80, 80, 80, 80,  5, 80, 20,  0,  0,
90
        5, 10,  5,  5, 80,  0, 15,  0, 20, 80,
91
        40, 80,  0, 35, 10, 20, 55, 45,  0,  0,
92
        80, 80, 80, 80, 80, 80, 80,  5, 80, 20,
93
        0,  0,  5, 10,  5,  5, 80,  0, 15,  0,
94
        20, 80, 40, 80,  0, 35, 10, 20, 55, 45,
95
        0,  0, 80, 80, 80, 80, 80, 80, 80, 80,
96
        80, 80, 80, 80, 80, 80, 80, 80, 80, 80,
97
        80, 80, 80, 80, 80, 80, 80, 80, 80, 80,
98
        80, 80, 80, 80, 80, 80, 80, 80, 80, 80,
99
        80, 80, 80, 80, 80, 80, 80, 80, 80, 80,
100
        80, 80, 80, 80, 80, 80, 80, 80, 80, 80,
101
        80, 80, 80, 80, 80, 80, 80, 80, 80, 80,
102
        80, 80, 80, 80, 80, 80, 80, 80, 80, 80,
103
        80, 80, 80, 80, 80, 80, 80, 80, 80, 80,
104
        80, 80, 80, 80, 80, 80, 80, 80, 80, 80,
105
        80, 80, 80, 80, 80, 80, 80, 80, 80, 80,
106
        80, 80, 80, 80, 80, 80, 80, 80, 80, 80,
107
        80, 80, 80, 80, 80, 80, 80, 80, 80, 80,
108
        80, 80, 80, 80, 80, 80
109
};
110
111
static struct http_hdrflg {
112
        hdr_t           *hdr;
113
        unsigned        flag;
114
} http_hdrflg[GPERF_MAX_HASH_VALUE + 1] = {
115
        { NULL }, { NULL }, { NULL }, { NULL },
116
        { &H_Date },
117
        { &H_Range },
118
        { NULL },
119
        { &H_Referer },
120
        { &H_Age },
121
        { &H_From },
122
        { &H_Keep_Alive },
123
        { &H_Retry_After },
124
        { &H_TE },
125
        { &H_If_Range },
126
        { &H_ETag },
127
        { &H_X_Forwarded_For },
128
        { &H_Expect },
129
        { &H_Trailer },
130
        { &H_If_Match },
131
        { &H_Host },
132
        { &H_Accept_Language },
133
        { &H_Accept },
134
        { &H_If_Modified_Since },
135
        { &H_If_None_Match },
136
        { &H_If_Unmodified_Since },
137
        { NULL },
138
        { &H_Cookie },
139
        { &H_Upgrade },
140
        { &H_Last_Modified },
141
        { &H_Accept_Charset },
142
        { &H_Accept_Encoding },
143
        { &H_Content_MD5 },
144
        { &H_Content_Type },
145
        { &H_Content_Range },
146
        { NULL }, { NULL },
147
        { &H_Content_Language },
148
        { &H_Transfer_Encoding },
149
        { &H_Authorization },
150
        { &H_Content_Length },
151
        { &H_User_Agent },
152
        { &H_Server },
153
        { &H_Expires },
154
        { &H_Location },
155
        { NULL },
156
        { &H_Set_Cookie },
157
        { &H_Content_Encoding },
158
        { &H_Max_Forwards },
159
        { &H_Cache_Control },
160
        { NULL },
161
        { &H_Connection },
162
        { &H_Pragma },
163
        { NULL },
164
        { &H_Accept_Ranges },
165
        { &H_HTTP2_Settings },
166
        { &H_Allow },
167
        { &H_Content_Location },
168
        { NULL },
169
        { &H_Proxy_Authenticate },
170
        { &H_Vary },
171
        { NULL },
172
        { &H_WWW_Authenticate },
173
        { &H_Warning },
174
        { &H_Via },
175
        { NULL }, { NULL }, { NULL }, { NULL },
176
        { NULL }, { NULL }, { NULL }, { NULL },
177
        { NULL }, { NULL }, { NULL }, { NULL },
178
        { NULL }, { NULL }, { NULL },
179
        { &H_Proxy_Authorization }
180
};
181
182
static struct http_hdrflg *
183
http_hdr_flags(const char *b, const char *e)
184 94125
{
185 94125
        unsigned u;
186 94125
        struct http_hdrflg *retval;
187
188 94125
        if (b == NULL || e == NULL)
189 0
                return (NULL);
190 94125
        u = pdiff(b, e);
191 94125
        if (u < GPERF_MIN_WORD_LENGTH || u > GPERF_MAX_WORD_LENGTH)
192 79
                return (NULL);
193 94046
        u += http_asso_values[(uint8_t)(e[-1])] +
194 94046
             http_asso_values[(uint8_t)(b[0])];
195 94046
        if (u > GPERF_MAX_HASH_VALUE)
196 1219
                return (NULL);
197 92827
        retval = &http_hdrflg[u];
198 92827
        if (retval->hdr == NULL)
199 319
                return (NULL);
200 92508
        AN(*retval->hdr);
201 92508
        if (!http_hdr_at(*retval->hdr + 1, b, e - b))
202 2590
                return (NULL);
203 89918
        return (retval);
204 92508
}
205
206
/*--------------------------------------------------------------------*/
207
208
static void
209
http_init_hdr(hdr_t hdr, int flg)
210 53352
{
211 53352
        struct http_hdrflg *f;
212
213 53352
        f = http_hdr_flags(hdr->str, hdr->str + hdr->len - 1);
214 53352
        AN(f);
215 53352
        assert(*f->hdr == hdr);
216 53352
        f->flag = flg;
217 53352
}
218
219
void
220
HTTP_Init(void)
221 1026
{
222 1026
        struct vsb *vsb;
223
224 53352
#define HTTPH(a, b, c) http_init_hdr(b, c);
225 1026
#include "tbl/http_headers.h"
226
227 1026
        vsb = VSB_new_auto();
228 1026
        AN(vsb);
229 1026
        VSB_printf(vsb, "1.1 %s (Vinyl-Cache/" PACKAGE_BRANCH ")",
230 1026
            heritage.identity);
231 1026
        AZ(VSB_finish(vsb));
232 1026
        REPLACE(via_hdr, VSB_data(vsb));
233 1026
        VSB_destroy(&vsb);
234 1026
}
235
236
/*--------------------------------------------------------------------
237
 * These two functions are in an incestuous relationship with the
238
 * order of macros in include/tbl/vsl_tags_http.h
239
 *
240
 * The http->logtag is the SLT_*Method enum, and we add to that, to
241
 * get the SLT_ to use.
242
 */
243
244
static void
245
http_VSLH(const struct http *hp, unsigned hdr)
246 239579
{
247 239579
        int i;
248
249 239607
        if (hp->vsl != NULL) {
250 239607
                assert(VXID_TAG(hp->vsl->wid));
251 239607
                i = hdr;
252 239607
                if (i > HTTP_HDR_FIRST)
253 124186
                        i = HTTP_HDR_FIRST;
254 239607
                i += hp->logtag;
255 239607
                VSLbt(hp->vsl, (enum VSL_tag_e)i, hp->hd[hdr]);
256 239607
        }
257 239579
}
258
259
static void
260
http_VSLH_del(const struct http *hp, unsigned hdr)
261 4477
{
262 4477
        int i;
263
264 4478
        if (hp->vsl != NULL) {
265
                /* We don't support unsetting stuff in the first line */
266 4478
                assert (hdr >= HTTP_HDR_FIRST);
267 4478
                assert(VXID_TAG(hp->vsl->wid));
268 4478
                i = (HTTP_HDR_UNSET - HTTP_HDR_METHOD);
269 4478
                i += hp->logtag;
270 4478
                VSLbt(hp->vsl, (enum VSL_tag_e)i, hp->hd[hdr]);
271 4478
        }
272 4477
}
273
274
/*--------------------------------------------------------------------*/
275
276
void
277
http_VSL_log(const struct http *hp)
278 11503
{
279 11503
        unsigned u;
280
281 103455
        for (u = 0; u < hp->nhd; u++)
282 91952
                if (hp->hd[u].b != NULL)
283 68974
                        http_VSLH(hp, u);
284 11503
}
285
286
/*--------------------------------------------------------------------*/
287
288
static void
289
http_fail(const struct http *hp)
290 109
{
291 109
        char id[WS_ID_SIZE];
292
293 109
        VSC_C_main->losthdr++;
294 109
        WS_Id(hp->ws, id);
295 109
        VSLb(hp->vsl, SLT_Error, "out of workspace (%s)", id);
296 109
        WS_MarkOverflow(hp->ws);
297 109
}
298
299
/*--------------------------------------------------------------------
300
 * List of canonical HTTP response code names from RFC2616
301
 */
302
303
static struct http_msg {
304
        unsigned        nbr;
305
        const char      *status;
306
        const char      *txt;
307
} http_msg[] = {
308
#define HTTP_RESP(n, t) { n, #n, t},
309
#include "tbl/http_response.h"
310
        { 0, "0", NULL }
311
};
312
313
const char *
314
http_Status2Reason(unsigned status, const char **sstr)
315 8021
{
316 8021
        struct http_msg *mp;
317
318 8021
        status %= 1000;
319 8021
        assert(status >= 100);
320 80193
        for (mp = http_msg; mp->nbr != 0 && mp->nbr <= status; mp++)
321 80146
                if (mp->nbr == status) {
322 7974
                        if (sstr)
323 4317
                                *sstr = mp->status;
324 7974
                        return (mp->txt);
325 7974
                }
326 47
        return ("Unknown HTTP Status");
327 8021
}
328
329
/*--------------------------------------------------------------------*/
330
331
unsigned
332
HTTP_estimate(unsigned nhttp)
333 10375
{
334
335
        /* XXX: We trust the structs to size-aligned as necessary */
336 10375
        return (PRNDUP(sizeof(struct http) + sizeof(txt) * nhttp + nhttp));
337 10375
}
338
339
struct http *
340
HTTP_create(void *p, uint16_t nhttp, unsigned len)
341 31125
{
342 31125
        struct http *hp;
343
344 31125
        hp = p;
345 31125
        hp->magic = HTTP_MAGIC;
346 31125
        hp->hd = (void*)(hp + 1);
347 31125
        hp->shd = nhttp;
348 31125
        hp->hdf = (void*)(hp->hd + nhttp);
349 31125
        assert((unsigned char*)p + len == hp->hdf + PRNDUP(nhttp));
350 31125
        return (hp);
351 31125
}
352
353
/*--------------------------------------------------------------------*/
354
355
void
356
HTTP_Setup(struct http *hp, struct ws *ws, struct vsl_log *vsl,
357
    enum VSL_tag_e  whence)
358 26950
{
359 26950
        http_Teardown(hp);
360 26950
        hp->nhd = HTTP_HDR_FIRST;
361 26950
        hp->logtag = whence;
362 26950
        hp->ws = ws;
363 26950
        hp->vsl = vsl;
364 26950
        hp->wkm = WKM_UNKNOWN;
365 26950
}
366
367
/*--------------------------------------------------------------------
368
 * http_Teardown() is a safety feature, we use it to zap all http
369
 * structs once we're done with them, to minimize the risk that
370
 * old stale pointers exist to no longer valid stuff.
371
 */
372
373
void
374
http_Teardown(struct http *hp)
375 41645
{
376
377 41645
        CHECK_OBJ_NOTNULL(hp, HTTP_MAGIC);
378 41645
        AN(hp->shd);
379 41645
        memset(&hp->nhd, 0, sizeof *hp - offsetof(struct http, nhd));
380 41645
        memset(hp->hd, 0, sizeof *hp->hd * hp->shd);
381 41645
        memset(hp->hdf, 0, sizeof *hp->hdf * hp->shd);
382 41645
}
383
384
/*--------------------------------------------------------------------
385
 * Duplicate the http content into another http
386
 * We cannot just memcpy the struct because the hd & hdf are private
387
 * storage to the struct http.
388
 */
389
390
void
391
HTTP_Dup(struct http *to, const struct http * fm)
392 16212
{
393
394 16212
        assert(fm->nhd <= to->shd);
395 16212
        vmemcpy(to->hd, fm->hd, fm->nhd * sizeof *to->hd);
396 16212
        vmemcpy(to->hdf, fm->hdf, fm->nhd * sizeof *to->hdf);
397 16212
        to->nhd = fm->nhd;
398 16212
        to->logtag = fm->logtag;
399 16212
        to->status = fm->status;
400 16212
        to->protover = fm->protover;
401 16212
        to->wkm = fm->wkm;
402 16212
}
403
404
405
/*--------------------------------------------------------------------
406
 * Clone the entire http structure, including vsl & ws
407
 */
408
409
void
410
HTTP_Clone(struct http *to, const struct http * const fm)
411 11805
{
412
413 11805
        HTTP_Dup(to, fm);
414 11805
        to->vsl = fm->vsl;
415 11805
        to->ws = fm->ws;
416 11805
}
417
418
/*--------------------------------------------------------------------*/
419
420
void
421
http_Proto(struct http *to)
422 11531
{
423 11531
        const char *fm;
424
425 11531
        fm = to->hd[HTTP_HDR_PROTO].b;
426
427 11531
        if (fm != NULL &&
428 11531
            (fm[0] == 'H' || fm[0] == 'h') &&
429 11531
            (fm[1] == 'T' || fm[1] == 't') &&
430 11531
            (fm[2] == 'T' || fm[2] == 't') &&
431 11531
            (fm[3] == 'P' || fm[3] == 'p') &&
432 11531
            fm[4] == '/' &&
433 11531
            vct_isdigit(fm[5]) &&
434 11531
            fm[6] == '.' &&
435 11531
            vct_isdigit(fm[7]) &&
436 11531
            fm[8] == '\0') {
437 11523
                to->protover = 10 * (fm[5] - '0') + (fm[7] - '0');
438 11523
        } else {
439 8
                to->protover = 0;
440 8
        }
441 11531
}
442
443
/*--------------------------------------------------------------------*/
444
445
void
446
http_SetH(struct http *to, unsigned n, const char *header)
447 94329
{
448
449 94329
        assert(n < to->nhd);
450 94329
        AN(header);
451 94329
        to->hd[n].b = TRUST_ME(header);
452 94329
        to->hd[n].e = strchr(to->hd[n].b, '\0');
453 94329
        to->hdf[n] = 0;
454 94329
        http_VSLH(to, n);
455 94329
        if (n == HTTP_HDR_PROTO)
456 4590
                http_Proto(to);
457 94329
        if (n == HTTP_HDR_METHOD)
458 39
                http_SetWellKnownMethod(to);
459 94329
}
460
461
/*--------------------------------------------------------------------*/
462
463
static void
464
http_PutField(struct http *to, int field, const char *string)
465 35
{
466 35
        const char *p;
467
468 35
        CHECK_OBJ_NOTNULL(to, HTTP_MAGIC);
469 35
        p = WS_Copy(to->ws, string, -1);
470 35
        if (p == NULL) {
471 1
                http_fail(to);
472 1
                VSLbs(to->vsl, SLT_LostHeader, TOSTRAND(string));
473 1
                return;
474 1
        }
475 34
        http_SetH(to, field, p);
476 34
}
477
478
/*--------------------------------------------------------------------*/
479
480
int
481
http_IsHdr(const txt *hh, hdr_t hdr)
482 357543
{
483
484 357543
        Tcheck(*hh);
485 357543
        CHECK_HDR(hdr);
486 357543
        return (http_hdr_at(hdr->str, hh->b, hdr->len));
487 357543
}
488
489
/*--------------------------------------------------------------------*/
490
491
static unsigned
492
http_findhdr(const struct http *hp, unsigned l, const char *hdr)
493 271526
{
494 271526
        unsigned u;
495
496 1308863
        for (u = HTTP_HDR_FIRST; u < hp->nhd; u++) {
497 1077149
                Tcheck(hp->hd[u]);
498 1077149
                if (hp->hd[u].e < hp->hd[u].b + l + 1)
499 226401
                        continue;
500 850748
                if (hp->hd[u].b[l] != ':')
501 721989
                        continue;
502 128759
                if (!http_hdr_at(hdr, hp->hd[u].b, l))
503 88947
                        continue;
504 39812
                return (u);
505 128759
        }
506 231714
        return (0);
507 271526
}
508
509
/*--------------------------------------------------------------------
510
 * Count how many instances we have of this header
511
 */
512
513
unsigned
514
http_CountHdr(const struct http *hp, hdr_t hdr)
515 9025
{
516 9025
        unsigned retval = 0;
517 9025
        unsigned u;
518
519 9025
        CHECK_OBJ_NOTNULL(hp, HTTP_MAGIC);
520
521 32059
        for (u = HTTP_HDR_FIRST; u < hp->nhd; u++) {
522 23034
                Tcheck(hp->hd[u]);
523 23034
                if (http_IsHdr(&hp->hd[u], hdr))
524 8751
                        retval++;
525 23034
        }
526 9025
        return (retval);
527 9025
}
528
529
/*--------------------------------------------------------------------
530
 * This function collapses multiple header lines of the same name.
531
 * The lines are joined with a comma, according to [rfc2616, 4.2bot, p32]
532
 *
533
 * XXX Collection of issues / open questions with the CollectHdr family of
534
 * functions
535
 * - should know which header is list-type, attempting collect on singleton
536
 *   should fail (in particular Host)
537
 *   - For list type, consider "value, containing, comma" vs v1, v2
538
 *   - but no complete list exists
539
 * - should offer to keep uniqe values only (Content-Length)
540
 * - should skip h2 pseudo headers (starting with :)
541
 * - check for empty elements (",   ,")
542
 *
543
 * Worthwile historic reading: 653d3912245c4010a3e0fb4c3c376fb2cd816c04
544
 */
545
546
void
547
http_CollectHdr(struct http *hp, hdr_t hdr)
548 75744
{
549
550 75744
        http_CollectHdrSep(hp, hdr, NULL);
551 75744
}
552
553
/*--------------------------------------------------------------------
554
 * You may prefer to collapse header fields using a different separator.
555
 * For Cookie headers, the separator is "; " for example. That's probably
556
 * the only example too.
557
 */
558
559
static int
560
http_CollectHdrSepFrom(struct http *hp, unsigned char hdl, const char *hds,
561
    const char *sep, unsigned lsep, unsigned f);
562
563
void
564
http_CollectHdrSep(struct http *hp, hdr_t hdr, const char *sep)
565 75898
{
566 75898
        unsigned lsep, f;
567
568 75898
        CHECK_OBJ_NOTNULL(hp, HTTP_MAGIC);
569 75898
        CHECK_HDR(hdr);
570
571 75898
        if (WS_Overflowed(hp->ws))
572 120
                return;
573
574 75778
        if (sep == NULL || *sep == '\0')
575 75611
                sep = ", ";
576 75778
        lsep = vstrlen(sep);
577
578 75778
        f = http_findhdr(hp, hdr->len - 1, hdr->str);
579 75778
        if (f == 0)
580 73110
                return;
581
582 2668
        (void)http_CollectHdrSepFrom(hp, hdr->len, hdr->str, sep, lsep, f);
583 2668
}
584
585
/*
586
 * implements the actual collection from header index f
587
 *
588
 * returns 1 if collected, 0 if not and -1 for error
589
 */
590
591
static int
592
http_CollectHdrSepFrom(struct http *hp, unsigned char hdl, const char *hds,
593
    const char *sep, unsigned lsep, unsigned f)
594 2741
{
595 2741
        unsigned u, ml, x, d;
596 2741
        char *b = NULL, *e = NULL;
597 2741
        const char *v;
598 10362
        for (d = u = f + 1; u < hp->nhd; u++) {
599 7621
                Tcheck(hp->hd[u]);
600 7621
                if (! http_hdr_at(hds, hp->hd[u].b, hdl)) {
601 7584
                        if (d != u) {
602 114
                                hp->hd[d] = hp->hd[u];
603 114
                                hp->hdf[d] = hp->hdf[u];
604 114
                        }
605 7584
                        d++;
606 7584
                        continue;
607 7584
                }
608 37
                if (b == NULL) {
609 21
                        http_VSLH_del(hp, f);
610
                        /* Found second header, start our collection */
611 21
                        ml = WS_ReserveAll(hp->ws);
612 42
                        b = WS_Reservation(hp->ws);
613 0
                        e = b + ml;
614 42
                        x = Tlen(hp->hd[f]);
615 42
                        if (b + x >= e) {
616 0
                                http_fail(hp);
617 0
                                VSLbs(hp->vsl, SLT_LostHeader,
618 0
                                    TOSTRAND(hds));
619 0
                                WS_Release(hp->ws, 0);
620 0
                                return (-1);
621 0
                        }
622 21
                        vmemcpy(b, hp->hd[f].b, x);
623 21
                        b += x;
624 21
                }
625 37
                http_VSLH_del(hp, u);
626
627 37
                AN(b);
628 37
                AN(e);
629
630
                /* Append the Nth header we found */
631 37
                x = Tlen(hp->hd[u]) - hdl;
632
633 37
                v = hp->hd[u].b + hdl;
634 67
                while (vct_issp(*v)) {
635 30
                        v++;
636 30
                        x--;
637 30
                }
638
639 37
                if (b + lsep + x >= e) {
640 0
                        http_fail(hp);
641 0
                        VSLbs(hp->vsl, SLT_LostHeader, TOSTRAND(hds));
642 0
                        WS_Release(hp->ws, 0);
643 0
                        return (-1);
644 0
                }
645 37
                vmemcpy(b, sep, lsep);
646 37
                b += lsep;
647 37
                vmemcpy(b, v, x);
648 37
                b += x;
649 37
        }
650 2741
        if (b == NULL)
651 2713
                return (0);
652 28
        hp->nhd = (uint16_t)d;
653 28
        AN(e);
654 28
        *b = '\0';
655 28
        hp->hd[f].b = WS_Reservation(hp->ws);
656 0
        hp->hd[f].e = b;
657 28
        http_VSLH(hp, f);
658 28
        WS_ReleaseP(hp->ws, b + 1);
659 28
        return (1);
660 28
}
661
662
/*
663
 * this respects the two exceptions for combining field values: Cookie, which
664
 * uses "; ", and Set-Cookie, which can not be combined.
665
 *
666
 * All other cases need to be catered for in VCL (e.g. by copying again from
667
 * req0)
668
 */
669
void
670
http_CollectAllHdrs(struct http *hp)
671 2
{
672 2
        const unsigned lsep = 2;
673 2
        unsigned f;
674
675 2
        CHECK_OBJ_NOTNULL(hp, HTTP_MAGIC);
676
677 20
        for (f = HTTP_HDR_FIRST; f < hp->nhd; f++) {
678 18
                Tcheck(hp->hd[f]);
679 18
                if (http_IsHdr(&hp->hd[f], H_Set_Cookie))
680 2
                        continue;
681 16
                const char *sep = http_IsHdr(&hp->hd[f], H_Cookie) ?
682 15
                        "; " : ", ";
683 16
                assert(vstrlen(sep) == lsep);
684
685 16
                const char *e = strchr(hp->hd[f].b, ':');
686
                // XXX bad, how to handle?
687 16
                if (e == NULL)
688 0
                        continue;
689
690 16
                e++;
691 16
                size_t l = e - hp->hd[f].b;
692 16
                assert(l <= UCHAR_MAX);
693
694 16
                int i = http_CollectHdrSepFrom(hp, (unsigned char)l,
695 16
                    hp->hd[f].b, sep, lsep, f);
696 16
                if (i < 0)
697 0
                        return;
698 16
        }
699 2
}
700
701
/*--------------------------------------------------------------------*/
702
703
int
704
http_GetHdr(const struct http *hp, hdr_t hdr, const char **ptr)
705 195847
{
706 195847
        unsigned u;
707 195847
        const char *p;
708
709 195847
        CHECK_HDR(hdr);
710 195847
        u = http_findhdr(hp, hdr->len - 1, hdr->str);
711 195847
        if (u == 0) {
712 156300
                if (ptr != NULL)
713 146620
                        *ptr = NULL;
714 156300
                return (0);
715 156300
        }
716 39547
        if (ptr != NULL) {
717 31800
                p = hp->hd[u].b + hdr->len;
718 63513
                while (vct_issp(*p))
719 31713
                        p++;
720 31800
                *ptr = p;
721 31800
        }
722 39547
        return (1);
723 195847
}
724
725
/*-----------------------------------------------------------------------------
726
 * Split source string at any of the separators, return pointer to first
727
 * and last+1 char of substrings, with whitespace trimmed at both ends.
728
 * If sep being an empty string is shorthand for VCT::SP
729
 * If stop is NULL, src is NUL terminated.
730
 */
731
732
static int
733
http_split(const char **src, const char *stop, const char *sep,
734
    const char **b, const char **e)
735 10553
{
736 10553
        const char *p, *q;
737
738 10553
        AN(src);
739 10553
        AN(*src);
740 10553
        AN(sep);
741 10553
        AN(b);
742 10553
        AN(e);
743
744 10553
        if (stop == NULL)
745 10548
                stop = strchr(*src, '\0');
746
747 10710
        for (p = *src; p < stop && (vct_issp(*p) || strchr(sep, *p)); p++)
748 157
                continue;
749
750 10553
        if (p >= stop) {
751 3896
                *b = NULL;
752 3896
                *e = NULL;
753 3896
                return (0);
754 3896
        }
755
756 6657
        *b = p;
757 6657
        if (*sep == '\0') {
758 0
                for (q = p + 1; q < stop && !vct_issp(*q); q++)
759 0
                        continue;
760 0
                *e = q;
761 0
                *src = q;
762 0
                return (1);
763 0
        }
764 35341
        for (q = p + 1; q < stop && !strchr(sep, *q); q++)
765 28684
                continue;
766 6657
        *src = q;
767 6667
        while (q > p && vct_issp(q[-1]))
768 0
                q--;
769 6657
        *e = q;
770 6657
        return (1);
771 6657
}
772
773
/*-----------------------------------------------------------------------------
774
 * Comparison rule for tokens:
775
 *      if target string starts with '"', we use memcmp() and expect closing
776
 *      double quote as well
777
 *      otherwise we use http_tok_at()
778
 *
779
 * On match we increment *bp past the token name.
780
 */
781
782
static int
783
http_istoken(const char **bp, const char *e, const char *token)
784 5872
{
785 5872
        int fl = vstrlen(token);
786 5872
        const char *b;
787
788 5872
        AN(bp);
789 5872
        AN(e);
790 5872
        AN(token);
791
792 5872
        b = *bp;
793
794 5872
        if (b + fl + 2 <= e && *b == '"' &&
795 5872
            !vmemcmp(b + 1, token, fl) && b[fl + 1] == '"') {
796 0
                *bp += fl + 2;
797 0
                return (1);
798 0
        }
799 5872
        if (b + fl <= e && http_tok_at(b, token, fl) &&
800 5872
            (b + fl == e || !vct_istchar(b[fl]))) {
801 2682
                *bp += fl;
802 2682
                return (1);
803 2682
        }
804 3190
        return (0);
805 5872
}
806
807
/*-----------------------------------------------------------------------------
808
 * Find a given data element (token) in a header according to RFC2616's #rule
809
 * (section 2.1, p15)
810
 *
811
 * On case sensitivity:
812
 *
813
 * Section 4.2 (Messages Headers) defines field (header) name as case
814
 * insensitive, but the field (header) value/content may be case-sensitive.
815
 *
816
 * http_GetHdrToken looks up a token in a header value and the rfc does not say
817
 * explicitly if tokens are to be compared with or without respect to case.
818
 *
819
 * But all examples and specific statements regarding tokens follow the rule
820
 * that unquoted tokens are to be matched case-insensitively and quoted tokens
821
 * case-sensitively.
822
 *
823
 * The optional pb and pe arguments will point to the token content start and
824
 * end+1, white space trimmed on both sides.
825
 */
826
827
int
828
http_GetHdrToken(const struct http *hp, hdr_t hdr,
829
    const char *token, const char **pb, const char **pe)
830 27469
{
831 27469
        const char *h, *b, *e;
832
833 27469
        if (pb != NULL)
834 18246
                *pb = NULL;
835 27469
        if (pe != NULL)
836 9001
                *pe = NULL;
837 27469
        if (!http_GetHdr(hp, hdr, &h))
838 21658
                return (0);
839 5811
        AN(h);
840
841 9000
        while (http_split(&h, NULL, ",", &b, &e))
842 5872
                if (http_istoken(&b, e, token))
843 2683
                        break;
844 5811
        if (b == NULL)
845 3141
                return (0);
846 2670
        if (pb != NULL) {
847 2587
                for (; vct_islws(*b); b++)
848 5
                        continue;
849 2582
                if (b == e) {
850 2532
                        b = NULL;
851 2532
                        e = NULL;
852 2532
                }
853 2582
                *pb = b;
854 2582
                if (pe != NULL)
855 2525
                        *pe = e;
856 2582
        }
857 2670
        return (1);
858 5811
}
859
860
/*--------------------------------------------------------------------
861
 * Find a given header field's quality value (qvalue).
862
 */
863
864
double
865
http_GetHdrQ(const struct http *hp, hdr_t hdr, const char *field)
866 9000
{
867 9000
        const char *hb, *he, *b, *e;
868 9000
        int i;
869 9000
        double a, f;
870
871 9000
        i = http_GetHdrToken(hp, hdr, field, &hb, &he);
872 9000
        if (!i)
873 6479
                return (0.);
874
875 2521
        if (hb == NULL)
876 2518
                return (1.);
877 7
        while (http_split(&hb, he, ";", &b, &e)) {
878 7
                if (*b != 'q')
879 0
                        continue;
880 7
                for (b++; b < e && vct_issp(*b); b++)
881 0
                        continue;
882 7
                if (b == e || *b != '=')
883 0
                        continue;
884 7
                break;
885 7
        }
886 3
        if (b == NULL)
887 0
                return (1.);
888 7
        for (b++; b < e && vct_issp(*b); b++)
889 0
                continue;
890 7
        if (b == e || (*b != '.' && !vct_isdigit(*b)))
891 1
                return (0.);
892 2
        a = 0;
893 11
        while (b < e && vct_isdigit(*b)) {
894 6
                a *= 10.;
895 6
                a += *b - '0';
896 6
                b++;
897 6
        }
898 5
        if (b == e || *b++ != '.')
899 1
                return (a);
900 1
        f = .1;
901 12
        while (b < e && vct_isdigit(*b)) {
902 11
                a += f * (*b - '0');
903 11
                f *= .1;
904 11
                b++;
905 11
        }
906 1
        return (a);
907 2
}
908
909
/*--------------------------------------------------------------------
910
 * Find a given header field's value.
911
 */
912
913
int
914
http_GetHdrField(const struct http *hp, hdr_t hdr,
915
    const char *field, const char **ptr)
916 9249
{
917 9249
        const char *h;
918 9249
        int i;
919
920 9249
        if (ptr != NULL)
921 4075
                *ptr = NULL;
922
923 9249
        h = NULL;
924 9249
        i = http_GetHdrToken(hp, hdr, field, &h, NULL);
925 9249
        if (!i)
926 9202
                return (i);
927
928 47
        if (ptr != NULL && h != NULL) {
929
                /* Skip whitespace, looking for '=' */
930 41
                while (*h && vct_issp(*h))
931 0
                        h++;
932 41
                if (*h == '=') {
933 41
                        h++;
934 45
                        while (*h && vct_issp(*h))
935 4
                                h++;
936 41
                        *ptr = h;
937 41
                }
938 41
        }
939 47
        return (i);
940 9249
}
941
942
/*--------------------------------------------------------------------
943
 * Return the content length if all Content-Length headers are well formed and
944
 * agree. Remove agreeing duplicates. Normalize to no leading zeroes.
945
 *
946
 * Returns -1 for no content length, -2 for malformed.
947
 *
948
 * The surviving C-L header is normalized to not contain leading zeroes in order
949
 * to support vcl like if (req.http.Content-Length ~ "^\d{3}"). If all headers
950
 * had leading zeroes or list values, we generate a new one
951
 */
952
953
ssize_t
954
http_GetContentLength(struct http *hp)
955 15732
{
956 15732
        txt first = { NULL, NULL };
957 15732
        ssize_t cl = -1;        // default no length
958 15732
        uint16_t u, v;
959 15732
        int need = 1;
960
961 15732
        CHECK_OBJ_NOTNULL(hp, HTTP_MAGIC);
962
963
        // basically the same outer loop as http_Unset
964 90042
        for (v = u = HTTP_HDR_FIRST; u < hp->nhd; u++) {
965 74317
                Tcheck(hp->hd[u]);
966 74317
                if (http_IsHdr(&hp->hd[u], H_Content_Length)) {
967 6251
                        const char *b = hp->hd[u].b + H_Content_Length->len;
968
969 12526
                        while (vct_issp(*b))
970 6275
                                b++;
971
972
                        // leading zeroes are not canonical
973 6251
                        int noncanon = (pdiff(b, hp->hd[u].e) > 1 && b[0] == '0');
974
975 6251
                        ssize_t l = VNUM_uint(b, NULL, &b);
976 6251
                        ssize_t ll = l;
977
978 6253
                        for (;;) {
979
                                // malformed numbers and mismatches to previous
980
                                // are hard errors
981 6253
                                if (l < 0 || ll != l) {
982 7
                                        l = -2;
983 7
                                        break;
984 7
                                }
985
986 6246
                                while (vct_isows(*b))
987 0
                                        b++;
988 6246
                                if (*b == '\0')
989 6242
                                        break;
990 4
                                if (*b != ',') {
991 0
                                        l = -2;
992 0
                                        break;
993 0
                                }
994 4
                                b++;
995
996
                                // list values are not canonical
997 4
                                noncanon = 1;
998 7
                                while (vct_issp(*b))
999 3
                                        b++;
1000 4
                                if (*b == '\0')
1001 1
                                        break;
1002 3
                                ll = l;
1003 3
                                l = VNUM_uint(b, NULL, &b);
1004 3
                        }
1005
1006 6251
                        assert(l == -2 || l >= 0);
1007 6251
                        assert(cl >= -2);
1008
1009
                        // note good cl or log errors
1010 6251
                        if (l == -2) {
1011 7
                                cl = -2;
1012 7
                                VSLb(hp->vsl, SLT_HttpGarbage, "%.*s",
1013 7
                                    (int)pdiff(hp->hd[u].b, hp->hd[u].e), hp->hd[u].b);
1014 6244
                        } else if (cl == -1)
1015 6232
                                cl = l;
1016 12
                        else if (l != cl) {
1017 3
                                cl = -2;
1018
1019 3
                                AN(first.b);
1020 3
                                VSLb(hp->vsl, SLT_HttpGarbage, "mismatch: %.*s",
1021 3
                                    (int)pdiff(hp->hd[u].b, hp->hd[u].e), hp->hd[u].b);
1022 3
                                VSLb(hp->vsl, SLT_HttpGarbage, "first   : %.*s",
1023 3
                                    (int)pdiff(first.b, first.e), first.b);
1024 3
                        }
1025
1026
                        // Return early if wrong and all headers kept
1027 6251
                        if (cl == -2 && v == u)
1028 7
                                return (cl);
1029
1030
                        // track for logging, by value because the
1031
                        // compaction below can move this slot
1032 6244
                        if (first.b == NULL)
1033 6232
                                first = hp->hd[u];
1034
1035
                        // decide if to keep header
1036 6244
                        if (cl >= 0 && need && !noncanon) {
1037 6229
                                need = 0;
1038 6229
                        } else if (cl >= 0) {
1039 11
                                http_VSLH_del(hp, u);
1040 11
                                continue;
1041 11
                        }
1042 6244
                }
1043
1044 74299
                if (v != u) {
1045 26
                        vmemcpy(&hp->hd[v], &hp->hd[u], sizeof *hp->hd);
1046 26
                        vmemcpy(&hp->hdf[v], &hp->hdf[u], sizeof *hp->hdf);
1047 26
                }
1048 74299
                v++;
1049 74299
        }
1050 15725
        hp->nhd = v;
1051
1052 15725
        if (cl >= 0 && need) {
1053
                // we have deleted all headers above
1054 2
                http_PrintfHeader(hp, "Content-Length: %zd", cl);
1055 2
        }
1056
1057 15725
        return (cl);
1058 15732
}
1059
1060
/*--------------------------------------------------------------------
1061
 */
1062
1063
stream_close_t
1064
http_DoConnection(struct http *hp, stream_close_t sc_close)
1065 35342
{
1066 35342
        const char *h, *b, *e;
1067 35342
        stream_close_t retval;
1068 35342
        unsigned u, v;
1069 35342
        struct http_hdrflg *f;
1070
1071 35342
        CHECK_OBJ_NOTNULL(hp, HTTP_MAGIC);
1072 35342
        assert(sc_close == SC_REQ_CLOSE || sc_close == SC_RESP_CLOSE);
1073
1074 35342
        if (hp->protover == 10)
1075 52
                retval = SC_REQ_HTTP10;
1076 35290
        else
1077 35290
                retval = SC_NULL;
1078
1079 35342
        http_CollectHdr(hp, H_Connection);
1080 35342
        if (!http_GetHdr(hp, H_Connection, &h))
1081 34605
                return (retval);
1082 737
        AN(h);
1083 1498
        while (http_split(&h, NULL, ",", &b, &e)) {
1084 764
                u = pdiff(b, e);
1085 764
                if (u == 5 && http_hdr_at(b, "close", u))
1086 181
                        retval = sc_close;
1087 764
                if (u == 10 && http_hdr_at(b, "keep-alive", u))
1088 551
                        retval = SC_NULL;
1089
1090
                /* Refuse removal of well-known-headers if they would pass. */
1091 764
                f = http_hdr_flags(b, e);
1092 764
                if (f != NULL && !(f->flag & HTTPH_R_PASS))
1093 3
                        return (SC_RX_BAD);
1094
1095 6254
                for (v = HTTP_HDR_FIRST; v < hp->nhd; v++) {
1096 5493
                        Tcheck(hp->hd[v]);
1097 5493
                        if (hp->hd[v].e < hp->hd[v].b + u + 1)
1098 1036
                                continue;
1099 4457
                        if (hp->hd[v].b[u] != ':')
1100 3835
                                continue;
1101 622
                        if (!http_hdr_at(b, hp->hd[v].b, u))
1102 603
                                continue;
1103 19
                        hp->hdf[v] |= HDF_FILTER;
1104 19
                }
1105 761
        }
1106 734
        CHECK_OBJ_NOTNULL(retval, STREAM_CLOSE_MAGIC);
1107 734
        return (retval);
1108 734
}
1109
1110
// Set the correct Connection header for closing or not
1111
// Existing Connection: close has precedence
1112
stream_close_t
1113
http_EnsureConnection(struct http *hp, stream_close_t sc_close)
1114 3972
{
1115 3972
        const char *h, *b, *e;
1116 3972
        struct vsb vsb[1];
1117 3972
        unsigned has_keep = 0, has_close = 0;
1118 3972
        unsigned u, n = 0;
1119
1120 3972
        CHECK_OBJ_NOTNULL(hp, HTTP_MAGIC);
1121 3972
        http_CollectHdr(hp, H_Connection);
1122 3972
        if (!http_GetHdr(hp, H_Connection, &h)) {
1123 3956
                if (sc_close == SC_NULL)
1124 3775
                        http_SetHeader(hp, "Connection: keep-alive");
1125 181
                else
1126 181
                        http_SetHeader(hp, "Connection: close");
1127 3956
                return (sc_close);
1128 3956
        }
1129
1130 16
        WS_VSB_new(vsb, hp->ws);
1131 16
        AN(h);
1132 42
        while (http_split(&h, NULL, ",", &b, &e)) {
1133 26
                u = pdiff(b, e);
1134 26
                if (u == 5 && http_hdr_at(b, "close", u)) {
1135 15
                        has_close = 1;
1136 15
                        continue;
1137 15
                }
1138 11
                if (u == 10 && http_hdr_at(b, "keep-alive", u)) {
1139 2
                        has_keep = 1;
1140 2
                        continue;
1141 2
                }
1142 9
                if (n++ == 0)
1143 6
                        VSB_cat(vsb, "Connection: ");
1144 3
                else
1145 3
                        VSB_cat(vsb, ", ");
1146 9
                VSB_bcat(vsb, b, u);
1147 9
        }
1148 16
        if (sc_close == SC_NULL && has_close)
1149 1
                sc_close = SC_RESP_CLOSE;
1150
        //lint -e{731} Boolean argument to equal/not equal
1151 16
        if (has_keep ^ has_close && (sc_close == SC_NULL) == (has_keep == 1)) {
1152
                // already correct
1153 15
                WS_Release(hp->ws, 0);
1154 15
                return (sc_close);
1155 15
        }
1156 1
        http_Unset(hp, H_Connection);
1157 1
        if (n == 0 && sc_close == SC_NULL) {
1158 0
                WS_Release(hp->ws, 0);
1159 0
                http_SetHeader(hp, "Connection: keep-alive");
1160 0
                return (sc_close);
1161 0
        }
1162 1
        if (n == 0) {
1163 1
                WS_Release(hp->ws, 0);
1164 1
                http_SetHeader(hp, "Connection: close");
1165 1
                return (sc_close);
1166 1
        }
1167 0
        if (sc_close == SC_NULL)
1168 1
                VSB_cat(vsb, ", keep-alive");
1169 18446744073709551615
        else
1170 18446744073709551615
                VSB_cat(vsb, ", close");
1171 0
        h = WS_VSB_finish(vsb, hp->ws, NULL);
1172 0
        if (h == NULL)
1173 0
                return (SC_OVERLOAD);
1174 0
        http_SetHeader(hp, h);
1175 0
        return (sc_close);
1176 0
}
1177
1178
/*--------------------------------------------------------------------*/
1179
1180
int
1181
http_HdrIs(const struct http *hp, hdr_t hdr, const char *val)
1182 12638
{
1183 12638
        const char *p;
1184
1185 12638
        if (!http_GetHdr(hp, hdr, &p))
1186 9844
                return (0);
1187 2794
        AN(p);
1188 2794
        if (http_tok_eq(p, val))
1189 2771
                return (1);
1190 23
        return (0);
1191 2794
}
1192
1193
/*--------------------------------------------------------------------*/
1194
1195
uint16_t
1196
http_GetStatus(const struct http *hp)
1197 27127
{
1198
1199 27127
        CHECK_OBJ_NOTNULL(hp, HTTP_MAGIC);
1200 27127
        return (hp->status);
1201 27127
}
1202
1203
int
1204
http_IsStatus(const struct http *hp, int val)
1205 12924
{
1206
1207 12924
        CHECK_OBJ_NOTNULL(hp, HTTP_MAGIC);
1208 12924
        assert(val >= 100 && val <= 999);
1209 12924
        return (val == (hp->status % 1000));
1210 12924
}
1211
1212
/*--------------------------------------------------------------------
1213
 * Setting the status will also set the Reason appropriately
1214
 */
1215
1216
void
1217
http_SetStatus(struct http *to, uint16_t status, const char *reason)
1218 4352
{
1219 4352
        char buf[4];
1220 4352
        const char *sstr = NULL;
1221
1222 4352
        CHECK_OBJ_NOTNULL(to, HTTP_MAGIC);
1223
        /*
1224
         * We allow people to use top digits for internal VCL
1225
         * signalling, but strip them from the ASCII version.
1226
         */
1227 4352
        to->status = status;
1228 4352
        status %= 1000;
1229 4352
        assert(status >= 100);
1230
1231 4352
        if (reason == NULL)
1232 241
                reason = http_Status2Reason(status, &sstr);
1233 4111
        else
1234 4111
                (void)http_Status2Reason(status, &sstr);
1235
1236 4352
        if (sstr) {
1237 4317
                http_SetH(to, HTTP_HDR_STATUS, sstr);
1238 4317
        } else {
1239 35
                bprintf(buf, "%03d", status);
1240 35
                http_PutField(to, HTTP_HDR_STATUS, buf);
1241 35
        }
1242 4352
        http_SetH(to, HTTP_HDR_REASON, reason);
1243 4352
}
1244
1245
/*--------------------------------------------------------------------*/
1246
1247
const char *
1248
http_GetMethod(const struct http *hp)
1249 7352
{
1250
1251 7352
        CHECK_OBJ_NOTNULL(hp, HTTP_MAGIC);
1252 7352
        Tcheck(hp->hd[HTTP_HDR_METHOD]);
1253 7352
        return (hp->hd[HTTP_HDR_METHOD].b);
1254 7352
}
1255
1256
/*--------------------------------------------------------------------
1257
 * Force a particular header field to a particular value
1258
 */
1259
1260
void
1261
http_ForceField(struct http *to, unsigned n, const char *t)
1262 18313
{
1263 18313
        int i;
1264
1265 18313
        CHECK_OBJ_NOTNULL(to, HTTP_MAGIC);
1266 18313
        assert(n < HTTP_HDR_FIRST);
1267 18313
        assert(n == HTTP_HDR_METHOD || n == HTTP_HDR_PROTO);
1268 18313
        AN(t);
1269
1270
        /* NB: method names and protocol versions are case-sensitive. */
1271 18313
        if (to->hd[n].b == NULL || vstrcmp(to->hd[n].b, t)) {
1272 128
                i = (HTTP_HDR_UNSET - HTTP_HDR_METHOD);
1273 128
                i += to->logtag;
1274
                /* XXX: this is a dead branch */
1275 128
                if (n >= HTTP_HDR_FIRST)
1276 0
                        VSLbt(to->vsl, (enum VSL_tag_e)i, to->hd[n]);
1277 128
                http_SetH(to, n, t);
1278 128
        }
1279 18313
}
1280
1281
/*--------------------------------------------------------------------*/
1282
1283
void
1284
http_PutResponse(struct http *to, const char *proto, uint16_t status,
1285
    const char *reason)
1286 4245
{
1287
1288 4245
        CHECK_OBJ_NOTNULL(to, HTTP_MAGIC);
1289 4245
        if (proto != NULL)
1290 4245
                http_SetH(to, HTTP_HDR_PROTO, proto);
1291 4245
        http_SetStatus(to, status, reason);
1292 4245
}
1293
1294
/*--------------------------------------------------------------------
1295
 * check if header is filtered by the dynamic marker or the static
1296
 * definitions in http_headers.h
1297
 */
1298
1299
static inline int
1300
http_isfiltered(const struct http *fm, unsigned u, unsigned how)
1301 57571
{
1302 57571
        const char *e;
1303 57571
        const struct http_hdrflg *f;
1304
1305 57571
        if (fm->hdf[u] & HDF_FILTER)
1306 17
                return (1);
1307 57554
        if (u < HTTP_HDR_FIRST)
1308 17648
                return (0);
1309 39906
        e = strchr(fm->hd[u].b, ':');
1310 39906
        if (e == NULL)
1311 0
                return (0);
1312 39906
        f = http_hdr_flags(fm->hd[u].b, e);
1313 39906
        return (f != NULL && f->flag & how);
1314 39906
}
1315
1316
int
1317
http_IsFiltered(const struct http *fm, unsigned u, unsigned how)
1318 858
{
1319
1320 858
        return (http_isfiltered(fm, u, how));
1321 858
}
1322
1323
/*--------------------------------------------------------------------
1324
 * Estimate how much workspace we need to Filter this header according
1325
 * to 'how'.
1326
 */
1327
1328
unsigned
1329
http_EstimateWS(const struct http *fm, unsigned how)
1330 2948
{
1331 2948
        unsigned u, l;
1332
1333 2948
        l = 4;
1334 2948
        CHECK_OBJ_NOTNULL(fm, HTTP_MAGIC);
1335 30555
        for (u = 0; u < fm->nhd; u++) {
1336 27607
                if (u == HTTP_HDR_METHOD || u == HTTP_HDR_URL)
1337 5894
                        continue;
1338 21713
                Tcheck(fm->hd[u]);
1339 21713
                if (http_isfiltered(fm, u, how))
1340 1284
                        continue;
1341 20429
                l += Tlen(fm->hd[u]) + 1L;
1342 20429
        }
1343 2948
        return (PRNDUP(l + 1L));
1344 2948
}
1345
1346
/*--------------------------------------------------------------------
1347
 * Encode http struct as byte string.
1348
 *
1349
 * XXX: We could save considerable special-casing below by encoding also
1350
 * XXX: H__Status, H__Reason and H__Proto into the string, but it would
1351
 * XXX: add 26-30 bytes to all encoded objects to save a little code.
1352
 * XXX: It could possibly be a good idea for later HTTP versions.
1353
 */
1354
1355
void
1356
HTTP_Encode(const struct http *fm, uint8_t *p0, unsigned l, unsigned how)
1357 2942
{
1358 2942
        unsigned u, w;
1359 2942
        uint16_t n;
1360 2942
        uint8_t *p, *e;
1361
1362 2942
        AN(p0);
1363 2942
        AN(l);
1364 2942
        p = p0;
1365 2942
        e = p + l;
1366 2942
        assert(p + 5 <= e);
1367 2942
        assert(fm->nhd <= fm->shd);
1368 2942
        n = HTTP_HDR_FIRST - 3;
1369 2942
        vbe16enc(p + 2, fm->status);
1370 2942
        p += 4;
1371 2942
        CHECK_OBJ_NOTNULL(fm, HTTP_MAGIC);
1372 30517
        for (u = 0; u < fm->nhd; u++) {
1373 27575
                if (u == HTTP_HDR_METHOD || u == HTTP_HDR_URL)
1374 5884
                        continue;
1375 21691
                Tcheck(fm->hd[u]);
1376 21691
                if (http_isfiltered(fm, u, how))
1377 1284
                        continue;
1378 20407
                http_VSLH(fm, u);
1379 20407
                w = Tlen(fm->hd[u]) + 1L;
1380 20407
                assert(p + w + 1 <= e);
1381 20407
                vmemcpy(p, fm->hd[u].b, w);
1382 20407
                p += w;
1383 20407
                n++;
1384 20407
        }
1385 2942
        *p++ = '\0';
1386 2942
        assert(p <= e);
1387 2942
        vbe16enc(p0, n + 1);
1388 2942
}
1389
1390
/*--------------------------------------------------------------------
1391
 * Decode byte string into http struct
1392
 */
1393
1394
int
1395
HTTP_Decode(struct http *to, const uint8_t *fm)
1396 5006
{
1397
1398 5006
        CHECK_OBJ_NOTNULL(to, HTTP_MAGIC);
1399 5006
        AN(to->vsl);
1400 5006
        AN(fm);
1401 5006
        if (vbe16dec(fm) <= to->shd) {
1402 5004
                to->status = vbe16dec(fm + 2);
1403 5004
                fm += 4;
1404 48748
                for (to->nhd = 0; to->nhd < to->shd; to->nhd++) {
1405 48748
                        if (to->nhd == HTTP_HDR_METHOD ||
1406 48748
                            to->nhd == HTTP_HDR_URL) {
1407 10008
                                to->hd[to->nhd].b = NULL;
1408 10008
                                to->hd[to->nhd].e = NULL;
1409 10008
                                continue;
1410 10008
                        }
1411 38740
                        if (*fm == '\0')
1412 5005
                                return (0);
1413 33735
                        to->hd[to->nhd].b = (const void*)fm;
1414 33735
                        fm = (const void*)strchr((const void*)fm, '\0');
1415 33735
                        to->hd[to->nhd].e = (const void*)fm;
1416 33735
                        fm++;
1417 33735
                        http_VSLH(to, to->nhd);
1418 33735
                }
1419 5004
        }
1420 1
        VSLb(to->vsl, SLT_Error,
1421 1
            "Too many headers to Decode object (%u vs. %u)",
1422 1
            vbe16dec(fm), to->shd);
1423 1
        return (-1);
1424 5006
}
1425
1426
/*--------------------------------------------------------------------*/
1427
1428
uint16_t
1429
HTTP_GetStatusPack(struct worker *wrk, struct objcore *oc)
1430 34
{
1431 34
        const char *ptr;
1432 34
        ptr = ObjGetAttr(wrk, oc, OA_HEADERS, NULL);
1433 34
        AN(ptr);
1434
1435 34
        return (vbe16dec(ptr + 2));
1436 34
}
1437
1438
/*--------------------------------------------------------------------*/
1439
1440
/* Get the first packed header */
1441
int
1442
HTTP_IterHdrPack(struct worker *wrk, struct objcore *oc, const char **p)
1443 924
{
1444 924
        const char *ptr;
1445
1446 924
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
1447 924
        CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC);
1448 924
        AN(p);
1449
1450 924
        if (*p == NULL) {
1451 288
                ptr = ObjGetAttr(wrk, oc, OA_HEADERS, NULL);
1452 288
                AN(ptr);
1453 288
                ptr += 4;       /* Skip nhd and status */
1454 288
                ptr = strchr(ptr, '\0') + 1;    /* Skip :proto: */
1455 288
                ptr = strchr(ptr, '\0') + 1;    /* Skip :status: */
1456 288
                ptr = strchr(ptr, '\0') + 1;    /* Skip :reason: */
1457 288
                *p = ptr;
1458 636
        } else {
1459 636
                *p = strchr(*p, '\0') + 1;      /* Skip to next header */
1460 636
        }
1461 924
        if (**p == '\0')
1462 58
                return (0);
1463 866
        return (1);
1464 924
}
1465
1466
const char *
1467
HTTP_GetHdrPack(struct worker *wrk, struct objcore *oc, hdr_t hdr)
1468 306
{
1469 306
        const char *ptr;
1470
1471 306
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
1472 306
        CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC);
1473 306
        CHECK_HDR(hdr);
1474
1475 306
        if (hdr->str[0] == ':') {
1476
                /* Special cases */
1477 18
                ptr = ObjGetAttr(wrk, oc, OA_HEADERS, NULL);
1478 18
                AN(ptr);
1479 18
                ptr += 4;       /* Skip nhd and status */
1480
1481
                /* XXX: should we also have h2_hdr_eq() ? */
1482 18
                if (!vstrcmp(hdr->str, ":proto:"))
1483 2
                        return (ptr);
1484 16
                ptr = strchr(ptr, '\0') + 1;
1485 16
                if (!vstrcmp(hdr->str, ":status:"))
1486 14
                        return (ptr);
1487 2
                ptr = strchr(ptr, '\0') + 1;
1488 2
                if (!vstrcmp(hdr->str, ":reason:"))
1489 2
                        return (ptr);
1490 0
                WRONG("Unknown magic packed header");
1491 0
        }
1492
1493 866
        HTTP_FOREACH_PACK(wrk, oc, ptr) {
1494 866
                if (http_hdr_at(ptr, hdr->str, hdr->len)) {
1495 230
                        ptr += hdr->len;
1496 459
                        while (vct_islws(*ptr))
1497 229
                                ptr++;
1498 230
                        return (ptr);
1499 230
                }
1500 866
        }
1501
1502 58
        return (NULL);
1503 288
}
1504
1505
/*--------------------------------------------------------------------
1506
 * Merge any headers in the oc->OA_HEADER into the struct http if they
1507
 * are not there already.
1508
 */
1509
1510
void
1511
HTTP_Merge(struct worker *wrk, struct objcore *oc, struct http *to)
1512 32
{
1513 32
        const char *ptr;
1514 32
        unsigned u;
1515 32
        const char *p;
1516 32
        unsigned nhd_before_merge;
1517
1518 32
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
1519 32
        CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC);
1520 32
        CHECK_OBJ_NOTNULL(to, HTTP_MAGIC);
1521
1522 32
        ptr = ObjGetAttr(wrk, oc, OA_HEADERS, NULL);
1523 32
        AN(ptr);
1524
1525 32
        to->status = vbe16dec(ptr + 2);
1526 32
        ptr += 4;
1527
1528 192
        for (u = 0; u < HTTP_HDR_FIRST; u++) {
1529 160
                if (u == HTTP_HDR_METHOD || u == HTTP_HDR_URL)
1530 64
                        continue;
1531 96
                http_SetH(to, u, ptr);
1532 96
                ptr = strchr(ptr, '\0') + 1;
1533 96
        }
1534 32
        nhd_before_merge = to->nhd;
1535 191
        while (*ptr != '\0') {
1536 159
                p = strchr(ptr, ':');
1537 159
                AN(p);
1538 159
                u = http_findhdr(to, p - ptr, ptr);
1539 159
                if (u == 0 || u >= nhd_before_merge)
1540 75
                        http_SetHeader(to, ptr);
1541 159
                ptr = strchr(ptr, '\0') + 1;
1542 159
        }
1543 32
}
1544
1545
/*--------------------------------------------------------------------*/
1546
1547
static void
1548
http_linkh(const struct http *to, const struct http *fm, unsigned n)
1549 9000
{
1550
1551 9000
        assert(n < HTTP_HDR_FIRST);
1552 9000
        Tcheck(fm->hd[n]);
1553 9000
        to->hd[n] = fm->hd[n];
1554 9000
        to->hdf[n] = fm->hdf[n];
1555 9000
        http_VSLH(to, n);
1556 9000
}
1557
1558
/*--------------------------------------------------------------------*/
1559
1560
void
1561
http_FilterReq(struct http *to, const struct http *fm, unsigned how)
1562 3000
{
1563 3000
        unsigned u;
1564
1565 3000
        CHECK_OBJ_NOTNULL(to, HTTP_MAGIC);
1566 3000
        CHECK_OBJ_NOTNULL(fm, HTTP_MAGIC);
1567
1568 3000
        http_linkh(to, fm, HTTP_HDR_METHOD);
1569 3000
        http_SetWellKnownMethod(to);
1570 3000
        http_linkh(to, fm, HTTP_HDR_URL);
1571 3000
        http_linkh(to, fm, HTTP_HDR_PROTO);
1572 3000
        to->protover = fm->protover;
1573 3000
        to->status = fm->status;
1574
1575 3000
        to->nhd = HTTP_HDR_FIRST;
1576 16445
        for (u = HTTP_HDR_FIRST; u < fm->nhd; u++) {
1577 13445
                Tcheck(fm->hd[u]);
1578 13445
                if (http_isfiltered(fm, u, how))
1579 204
                        continue;
1580 13241
                assert (to->nhd < to->shd);
1581 13241
                to->hd[to->nhd] = fm->hd[u];
1582 13241
                to->hdf[to->nhd] = 0;
1583 13241
                http_VSLH(to, to->nhd);
1584 13241
                to->nhd++;
1585 13241
        }
1586 3000
}
1587
1588
/*--------------------------------------------------------------------
1589
 * This function copies any header fields which reference foreign
1590
 * storage into our own WS.
1591
 */
1592
1593
void
1594
http_CopyHome(const struct http *hp)
1595 2969
{
1596 2969
        unsigned u, l;
1597 2969
        const char *p;
1598
1599 32394
        for (u = 0; u < hp->nhd; u++) {
1600 29425
                if (hp->hd[u].b == NULL) {
1601 5938
                        assert(u < HTTP_HDR_FIRST);
1602 5938
                        continue;
1603 5938
                }
1604
1605 23487
                l = Tlen(hp->hd[u]);
1606 23487
                if (WS_Allocated(hp->ws, hp->hd[u].b, l))
1607 1475
                        continue;
1608
1609 22012
                p = WS_Copy(hp->ws, hp->hd[u].b, l + 1L);
1610 22012
                if (p == NULL) {
1611 0
                        http_fail(hp);
1612 0
                        VSLbs(hp->vsl, SLT_LostHeader, TOSTRAND(hp->hd[u].b));
1613 0
                        return;
1614 0
                }
1615 22012
                hp->hd[u].b = p;
1616 22012
                hp->hd[u].e = p + l;
1617 22012
        }
1618 2969
}
1619
1620
/*--------------------------------------------------------------------*/
1621
1622
void
1623
http_SetHeader(struct http *to, const char *header)
1624 18362
{
1625
1626 18362
        CHECK_OBJ_NOTNULL(to, HTTP_MAGIC);
1627 18362
        if (to->nhd >= to->shd) {
1628 1
                VSLbs(to->vsl, SLT_LostHeader, TOSTRAND(header));
1629 1
                http_fail(to);
1630 1
                return;
1631 1
        }
1632 18361
        http_SetH(to, to->nhd++, header);
1633 18361
}
1634
1635
/*--------------------------------------------------------------------*/
1636
1637
void
1638
http_ForceHeader(struct http *to, hdr_t hdr, const char *val)
1639 8183
{
1640
1641 8183
        CHECK_OBJ_NOTNULL(to, HTTP_MAGIC);
1642 8183
        if (http_HdrIs(to, hdr, val))
1643 2507
                return;
1644 5676
        http_Unset(to, hdr);
1645 5676
        http_PrintfHeader(to, "%s %s", hdr->str, val);
1646 5676
}
1647
1648
void
1649
http_AppendHeader(struct http *to, hdr_t hdr, const char *val)
1650 22396
{
1651 22396
        const char *old;
1652
1653 22396
        http_CollectHdr(to, hdr);
1654 22396
        if (http_GetHdr(to, hdr, &old)) {
1655 1507
                http_Unset(to, hdr);
1656 1507
                http_PrintfHeader(to, "%s %s, %s", hdr->str, old, val);
1657 20889
        } else {
1658 20889
                http_PrintfHeader(to, "%s %s", hdr->str, val);
1659 20889
        }
1660 22396
}
1661
1662
void
1663
http_PrintfHeader(struct http *to, const char *fmt, ...)
1664 49976
{
1665 49976
        va_list ap, ap2;
1666 49976
        struct vsb vsb[1];
1667 49976
        size_t sz;
1668 49976
        char *p;
1669
1670 49976
        CHECK_OBJ_NOTNULL(to, HTTP_MAGIC);
1671
1672 49976
        va_start(ap, fmt);
1673 49976
        va_copy(ap2, ap);
1674
1675 49976
        WS_VSB_new(vsb, to->ws);
1676 49976
        VSB_vprintf(vsb, fmt, ap);
1677 49976
        p = WS_VSB_finish(vsb, to->ws, &sz);
1678
1679 49976
        if (p == NULL || to->nhd >= to->shd) {
1680 31
                http_fail(to);
1681 31
                VSLbv(to->vsl, SLT_LostHeader, fmt, ap2);
1682 49945
        } else {
1683 49945
                http_SetH(to, to->nhd++, p);
1684 49945
        }
1685 49976
        va_end(ap);
1686 49976
        va_end(ap2);
1687 49976
}
1688
1689
void
1690
http_TimeHeader(struct http *to, const char *fmt, vtim_real now)
1691 4265
{
1692 4265
        unsigned l;
1693 4265
        char *p;
1694
1695 4265
        CHECK_OBJ_NOTNULL(to, HTTP_MAGIC);
1696 4265
        if (to->nhd >= to->shd) {
1697 0
                VSLbs(to->vsl, SLT_LostHeader, TOSTRAND(fmt));
1698 0
                http_fail(to);
1699 0
                return;
1700 0
        }
1701 4265
        l = (unsigned)vstrlen(fmt);
1702 4265
        p = WS_Alloc(to->ws, l + VTIM_FORMAT_SIZE);
1703 4265
        if (p == NULL) {
1704 76
                http_fail(to);
1705 76
                VSLbs(to->vsl, SLT_LostHeader, TOSTRAND(fmt));
1706 76
                return;
1707 76
        }
1708 4189
        strcpy(p, fmt);
1709 4189
        VTIM_format(now, p + l);
1710 4189
        http_SetH(to, to->nhd++, p);
1711 4189
}
1712
1713
const char *
1714
http_ViaHeader(void)
1715 13703
{
1716
1717 13703
        return (via_hdr);
1718 13703
}
1719
1720
/*--------------------------------------------------------------------*/
1721
1722
void
1723
http_Unset(struct http *hp, hdr_t hdr)
1724 62414
{
1725 62414
        uint16_t u, v;
1726
1727 323884
        for (v = u = HTTP_HDR_FIRST; u < hp->nhd; u++) {
1728 261470
                Tcheck(hp->hd[u]);
1729 261470
                if (http_IsHdr(&hp->hd[u], hdr)) {
1730 4415
                        http_VSLH_del(hp, u);
1731 4415
                        continue;
1732 4415
                }
1733 257055
                if (v != u) {
1734 12914
                        vmemcpy(&hp->hd[v], &hp->hd[u], sizeof *hp->hd);
1735 12914
                        vmemcpy(&hp->hdf[v], &hp->hdf[u], sizeof *hp->hdf);
1736 12914
                }
1737 257055
                v++;
1738 257055
        }
1739 62414
        hp->nhd = v;
1740 62414
}
1741
1742
void
1743
http_SetWellKnownMethod(struct http *hp)
1744 7353
{
1745 7353
        size_t l;
1746 7353
        const char *method;
1747
1748 7353
        CHECK_OBJ_NOTNULL(hp, HTTP_MAGIC);
1749
1750 7353
        method = http_GetMethod(hp);
1751 7353
        AN(method);
1752 7353
        l = vstrlen(method);
1753 7353
        hp->wkm = WKM_UNKNOWN;
1754 7353
#define WKM(wk, bit)                                                    \
1755 9483
        do {                                                            \
1756 9483
                if (l == vstrlen(#wk) && !vmemcmp(#wk, method, l)) {    \
1757 7290
                        hp->wkm = WKM_##wk;                             \
1758 7290
                        return;                                         \
1759 7290
                }                                                       \
1760 9483
        } while (0);
1761 7353
#include "tbl/well_known_methods.h"
1762 2130
#undef WKM
1763 2130
}