lsquic_http1x_if.c revision a5fa05f9
17d09751dSDmitri Tikhonov/* Copyright (c) 2017 - 2020 LiteSpeed Technologies Inc.  See LICENSE. */
23b55e6aeSDmitri Tikhonov#include <assert.h>
33b55e6aeSDmitri Tikhonov#include <ctype.h>
43b55e6aeSDmitri Tikhonov#include <stddef.h>
53b55e6aeSDmitri Tikhonov#include <stdlib.h>
63b55e6aeSDmitri Tikhonov#include <string.h>
73b55e6aeSDmitri Tikhonov
83b55e6aeSDmitri Tikhonov#include "lsquic.h"
93b55e6aeSDmitri Tikhonov#include "lsquic_headers.h"
103b55e6aeSDmitri Tikhonov#include "lsquic_http1x_if.h"
113b55e6aeSDmitri Tikhonov#include "lshpack.h"
123b55e6aeSDmitri Tikhonov
133b55e6aeSDmitri Tikhonov#define LSQUIC_LOGGER_MODULE LSQLM_HTTP1X
145392f7a3SLiteSpeed Tech#define LSQUIC_LOG_CONN_ID lsquic_conn_log_cid(hwc->hwc_conn)
153b55e6aeSDmitri Tikhonov#include "lsquic_logger.h"
163b55e6aeSDmitri Tikhonov
173b55e6aeSDmitri Tikhonovenum pseudo_header
183b55e6aeSDmitri Tikhonov{
193b55e6aeSDmitri Tikhonov    PSEH_METHOD,
203b55e6aeSDmitri Tikhonov    PSEH_SCHEME,
213b55e6aeSDmitri Tikhonov    PSEH_AUTHORITY,
223b55e6aeSDmitri Tikhonov    PSEH_PATH,
233b55e6aeSDmitri Tikhonov    PSEH_STATUS,
243b55e6aeSDmitri Tikhonov    N_PSEH
253b55e6aeSDmitri Tikhonov};
263b55e6aeSDmitri Tikhonov
273b55e6aeSDmitri Tikhonov#define BIT(x) (1 << (x))
283b55e6aeSDmitri Tikhonov
293b55e6aeSDmitri Tikhonov#define ALL_REQUEST_PSEH (BIT(PSEH_METHOD)|BIT(PSEH_SCHEME)|BIT(PSEH_AUTHORITY)|BIT(PSEH_PATH))
303b55e6aeSDmitri Tikhonov#define REQUIRED_REQUEST_PSEH (BIT(PSEH_METHOD)|BIT(PSEH_SCHEME)|BIT(PSEH_PATH))
313b55e6aeSDmitri Tikhonov
323b55e6aeSDmitri Tikhonov#define ALL_SERVER_PSEH BIT(PSEH_STATUS)
333b55e6aeSDmitri Tikhonov#define REQUIRED_SERVER_PSEH ALL_SERVER_PSEH
343b55e6aeSDmitri Tikhonov
353b55e6aeSDmitri Tikhonov#define PSEH_LEN(h) (sizeof(#h) - 5)
363b55e6aeSDmitri Tikhonov
373b55e6aeSDmitri Tikhonovstruct header_writer_ctx
383b55e6aeSDmitri Tikhonov{
395392f7a3SLiteSpeed Tech    const struct lsquic_conn    *hwc_conn;
403b55e6aeSDmitri Tikhonov    char                        *buf;
413b55e6aeSDmitri Tikhonov    char                        *cookie_val;
423b55e6aeSDmitri Tikhonov    unsigned                     cookie_sz, cookie_nalloc;
433b55e6aeSDmitri Tikhonov    unsigned                     max_headers_sz,
443b55e6aeSDmitri Tikhonov                                 headers_sz,
453b55e6aeSDmitri Tikhonov                                 w_off;
463b55e6aeSDmitri Tikhonov    enum {
473b55e6aeSDmitri Tikhonov        HWC_SERVER       = 1 << 0,
483b55e6aeSDmitri Tikhonov        HWC_EXPECT_COLON = 1 << 1,
493b55e6aeSDmitri Tikhonov        HWC_SEEN_HOST    = 1 << 2,
503b55e6aeSDmitri Tikhonov        HWC_PUSH_PROMISE = 1 << 3,
513b55e6aeSDmitri Tikhonov    }                            hwc_flags;
523b55e6aeSDmitri Tikhonov    enum pseudo_header           pseh_mask;
533b55e6aeSDmitri Tikhonov    char                        *pseh_bufs[N_PSEH];
543b55e6aeSDmitri Tikhonov    struct http1x_headers        hwc_h1h;
55a5fa05f9SDmitri Tikhonov    char                        *hwc_header_buf;
56a5fa05f9SDmitri Tikhonov    size_t                       hwc_header_buf_nalloc;
57a5fa05f9SDmitri Tikhonov    struct lsxpack_header        hwc_xhdr;
583b55e6aeSDmitri Tikhonov};
593b55e6aeSDmitri Tikhonov
603b55e6aeSDmitri Tikhonov
613b55e6aeSDmitri Tikhonov#define HWC_PSEH_LEN(hwc, ph) ((int) strlen((hwc)->pseh_bufs[ph]))
623b55e6aeSDmitri Tikhonov
633b55e6aeSDmitri Tikhonov#define HWC_PSEH_VAL(hwc, ph) ((hwc)->pseh_bufs[ph])
643b55e6aeSDmitri Tikhonov
653b55e6aeSDmitri Tikhonovstatic void *
663b55e6aeSDmitri Tikhonovh1h_create_header_set (void *ctx, int is_push_promise)
673b55e6aeSDmitri Tikhonov{
683b55e6aeSDmitri Tikhonov    const struct http1x_ctor_ctx *hcc = ctx;
693b55e6aeSDmitri Tikhonov    struct header_writer_ctx *hwc;
703b55e6aeSDmitri Tikhonov
713b55e6aeSDmitri Tikhonov    hwc = calloc(1, sizeof(*hwc));
723b55e6aeSDmitri Tikhonov    if (!hwc)
733b55e6aeSDmitri Tikhonov        return NULL;
743b55e6aeSDmitri Tikhonov
753b55e6aeSDmitri Tikhonov    hwc->hwc_flags = HWC_EXPECT_COLON;
763b55e6aeSDmitri Tikhonov    if (hcc->is_server)
773b55e6aeSDmitri Tikhonov        hwc->hwc_flags |= HWC_SERVER;
783b55e6aeSDmitri Tikhonov    if (is_push_promise)
793b55e6aeSDmitri Tikhonov        hwc->hwc_flags |= HWC_PUSH_PROMISE;
803b55e6aeSDmitri Tikhonov    hwc->max_headers_sz = hcc->max_headers_sz;
815392f7a3SLiteSpeed Tech    hwc->hwc_conn = hcc->conn;
823b55e6aeSDmitri Tikhonov    return &hwc->hwc_h1h;
833b55e6aeSDmitri Tikhonov}
843b55e6aeSDmitri Tikhonov
853b55e6aeSDmitri Tikhonov
863b55e6aeSDmitri Tikhonovstatic int
873b55e6aeSDmitri Tikhonovhwc_uh_write (struct header_writer_ctx *hwc, const void *buf, size_t sz)
883b55e6aeSDmitri Tikhonov{
893b55e6aeSDmitri Tikhonov    char *h1h_buf;
903b55e6aeSDmitri Tikhonov
913b55e6aeSDmitri Tikhonov    if (hwc->w_off + sz > hwc->headers_sz)
923b55e6aeSDmitri Tikhonov    {
933b55e6aeSDmitri Tikhonov        if (hwc->headers_sz * 2 >= hwc->w_off + sz)
943b55e6aeSDmitri Tikhonov            hwc->headers_sz *= 2;
953b55e6aeSDmitri Tikhonov        else
963b55e6aeSDmitri Tikhonov            hwc->headers_sz = hwc->w_off + sz;
973b55e6aeSDmitri Tikhonov        h1h_buf = realloc(hwc->hwc_h1h.h1h_buf, hwc->headers_sz);
982d296031SDmitri Tikhonov        if (!h1h_buf)
993b55e6aeSDmitri Tikhonov            return -1;
1003b55e6aeSDmitri Tikhonov        hwc->hwc_h1h.h1h_buf = h1h_buf;
1013b55e6aeSDmitri Tikhonov    }
1023b55e6aeSDmitri Tikhonov    memcpy(&hwc->hwc_h1h.h1h_buf[hwc->w_off], buf, sz);
1033b55e6aeSDmitri Tikhonov    hwc->w_off += sz;
1043b55e6aeSDmitri Tikhonov    return 0;
1053b55e6aeSDmitri Tikhonov}
1063b55e6aeSDmitri Tikhonov
1073b55e6aeSDmitri Tikhonov
108a5fa05f9SDmitri Tikhonovstatic int
1093b55e6aeSDmitri Tikhonovsave_pseudo_header (struct header_writer_ctx *hwc, enum pseudo_header ph,
1103b55e6aeSDmitri Tikhonov                    const char *val, unsigned val_len)
1113b55e6aeSDmitri Tikhonov{
1123b55e6aeSDmitri Tikhonov    if (0 == (hwc->pseh_mask & BIT(ph)))
1133b55e6aeSDmitri Tikhonov    {
1143b55e6aeSDmitri Tikhonov        assert(!hwc->pseh_bufs[ph]);
1153b55e6aeSDmitri Tikhonov        hwc->pseh_bufs[ph] = malloc(val_len + 1);
1163b55e6aeSDmitri Tikhonov        if (!hwc->pseh_bufs[ph])
117a5fa05f9SDmitri Tikhonov            return -1;
1183b55e6aeSDmitri Tikhonov        hwc->pseh_mask |= BIT(ph);
1193b55e6aeSDmitri Tikhonov        memcpy(hwc->pseh_bufs[ph], val, val_len);
1203b55e6aeSDmitri Tikhonov        hwc->pseh_bufs[ph][val_len] = '\0';
121a5fa05f9SDmitri Tikhonov        return 0;
1223b55e6aeSDmitri Tikhonov    }
1233b55e6aeSDmitri Tikhonov    else
1243b55e6aeSDmitri Tikhonov    {
1253b55e6aeSDmitri Tikhonov        LSQ_INFO("header %u is already present", ph);
126a5fa05f9SDmitri Tikhonov        return 1;
1273b55e6aeSDmitri Tikhonov    }
1283b55e6aeSDmitri Tikhonov}
1293b55e6aeSDmitri Tikhonov
1303b55e6aeSDmitri Tikhonov
131a5fa05f9SDmitri Tikhonovstatic int
132a5fa05f9SDmitri Tikhonovadd_pseudo_header (struct header_writer_ctx *hwc, struct lsxpack_header *xhdr)
1333b55e6aeSDmitri Tikhonov{
134a5fa05f9SDmitri Tikhonov    const char *name, *val;
135a5fa05f9SDmitri Tikhonov    unsigned name_len, val_len;
136a5fa05f9SDmitri Tikhonov
1373b55e6aeSDmitri Tikhonov    if (!(hwc->hwc_flags & HWC_EXPECT_COLON))
1383b55e6aeSDmitri Tikhonov    {
1393b55e6aeSDmitri Tikhonov        LSQ_INFO("unexpected colon");
140a5fa05f9SDmitri Tikhonov        return 1;
1413b55e6aeSDmitri Tikhonov    }
1423b55e6aeSDmitri Tikhonov
143a5fa05f9SDmitri Tikhonov    name = lsxpack_header_get_name(xhdr);
144a5fa05f9SDmitri Tikhonov    val = lsxpack_header_get_value(xhdr);
145a5fa05f9SDmitri Tikhonov    name_len = xhdr->name_len;
146a5fa05f9SDmitri Tikhonov    val_len = xhdr->val_len;
147a5fa05f9SDmitri Tikhonov
1483b55e6aeSDmitri Tikhonov    switch (name_len)
1493b55e6aeSDmitri Tikhonov    {
1503b55e6aeSDmitri Tikhonov    case 5:
1513b55e6aeSDmitri Tikhonov        if (0 == memcmp(name,     ":path", 5))
1523b55e6aeSDmitri Tikhonov            return save_pseudo_header(hwc, PSEH_PATH, val, val_len);
1533b55e6aeSDmitri Tikhonov        break;
1543b55e6aeSDmitri Tikhonov    case 7:
1553b55e6aeSDmitri Tikhonov        switch (name[2])
1563b55e6aeSDmitri Tikhonov        {
1573b55e6aeSDmitri Tikhonov        case 'c':
1583b55e6aeSDmitri Tikhonov            if (0 == memcmp(name, ":scheme", 7))
1593b55e6aeSDmitri Tikhonov                return save_pseudo_header(hwc, PSEH_SCHEME, val, val_len);
1603b55e6aeSDmitri Tikhonov            break;
1613b55e6aeSDmitri Tikhonov        case 'e':
1623b55e6aeSDmitri Tikhonov            if (0 == memcmp(name, ":method", 7))
1633b55e6aeSDmitri Tikhonov                return save_pseudo_header(hwc, PSEH_METHOD, val, val_len);
1643b55e6aeSDmitri Tikhonov            break;
1653b55e6aeSDmitri Tikhonov        case 't':
1663b55e6aeSDmitri Tikhonov            if (0 == memcmp(name, ":status", 7))
1673b55e6aeSDmitri Tikhonov                return save_pseudo_header(hwc, PSEH_STATUS, val, val_len);
1683b55e6aeSDmitri Tikhonov            break;
1693b55e6aeSDmitri Tikhonov        }
1703b55e6aeSDmitri Tikhonov        break;
1713b55e6aeSDmitri Tikhonov    case 10:
1723b55e6aeSDmitri Tikhonov        if (0 == memcmp(name,     ":authority", 10))
1733b55e6aeSDmitri Tikhonov            return save_pseudo_header(hwc, PSEH_AUTHORITY, val, val_len);
1743b55e6aeSDmitri Tikhonov        break;
1753b55e6aeSDmitri Tikhonov    }
1763b55e6aeSDmitri Tikhonov
1773b55e6aeSDmitri Tikhonov    LSQ_INFO("unknown pseudo-header `%.*s'", name_len, name);
178a5fa05f9SDmitri Tikhonov    return 1;
1793b55e6aeSDmitri Tikhonov}
1803b55e6aeSDmitri Tikhonov
1813b55e6aeSDmitri Tikhonov
1823b55e6aeSDmitri Tikhonov#define HTTP_CODE_LEN 3
1833b55e6aeSDmitri Tikhonov
1843b55e6aeSDmitri Tikhonovstatic const char *
1853b55e6aeSDmitri Tikhonovcode_str_to_reason (const char code_str[HTTP_CODE_LEN])
1863b55e6aeSDmitri Tikhonov{
1873b55e6aeSDmitri Tikhonov    /* RFC 7231, Section 6: */
1883b55e6aeSDmitri Tikhonov    static const char *const http_reason_phrases[] =
1893b55e6aeSDmitri Tikhonov    {
1903b55e6aeSDmitri Tikhonov    #define HTTP_REASON_CODE(code, reason) [code - 100] = reason
1913b55e6aeSDmitri Tikhonov        HTTP_REASON_CODE(100, "Continue"),
1923b55e6aeSDmitri Tikhonov        HTTP_REASON_CODE(101, "Switching Protocols"),
1933b55e6aeSDmitri Tikhonov        HTTP_REASON_CODE(200, "OK"),
1943b55e6aeSDmitri Tikhonov        HTTP_REASON_CODE(201, "Created"),
1953b55e6aeSDmitri Tikhonov        HTTP_REASON_CODE(202, "Accepted"),
1963b55e6aeSDmitri Tikhonov        HTTP_REASON_CODE(203, "Non-Authoritative Information"),
1973b55e6aeSDmitri Tikhonov        HTTP_REASON_CODE(204, "No Content"),
1983b55e6aeSDmitri Tikhonov        HTTP_REASON_CODE(205, "Reset Content"),
1993b55e6aeSDmitri Tikhonov        HTTP_REASON_CODE(206, "Partial Content"),
2003b55e6aeSDmitri Tikhonov        HTTP_REASON_CODE(300, "Multiple Choices"),
2013b55e6aeSDmitri Tikhonov        HTTP_REASON_CODE(301, "Moved Permanently"),
2023b55e6aeSDmitri Tikhonov        HTTP_REASON_CODE(302, "Found"),
2033b55e6aeSDmitri Tikhonov        HTTP_REASON_CODE(303, "See Other"),
2043b55e6aeSDmitri Tikhonov        HTTP_REASON_CODE(304, "Not Modified"),
2053b55e6aeSDmitri Tikhonov        HTTP_REASON_CODE(305, "Use Proxy"),
2063b55e6aeSDmitri Tikhonov        HTTP_REASON_CODE(307, "Temporary Redirect"),
2073b55e6aeSDmitri Tikhonov        HTTP_REASON_CODE(400, "Bad Request"),
2083b55e6aeSDmitri Tikhonov        HTTP_REASON_CODE(401, "Unauthorized"),
2093b55e6aeSDmitri Tikhonov        HTTP_REASON_CODE(402, "Payment Required"),
2103b55e6aeSDmitri Tikhonov        HTTP_REASON_CODE(403, "Forbidden"),
2113b55e6aeSDmitri Tikhonov        HTTP_REASON_CODE(404, "Not Found"),
2123b55e6aeSDmitri Tikhonov        HTTP_REASON_CODE(405, "Method Not Allowed"),
2133b55e6aeSDmitri Tikhonov        HTTP_REASON_CODE(406, "Not Acceptable"),
2143b55e6aeSDmitri Tikhonov        HTTP_REASON_CODE(407, "Proxy Authentication Required"),
2153b55e6aeSDmitri Tikhonov        HTTP_REASON_CODE(408, "Request Timeout"),
2163b55e6aeSDmitri Tikhonov        HTTP_REASON_CODE(409, "Conflict"),
2173b55e6aeSDmitri Tikhonov        HTTP_REASON_CODE(410, "Gone"),
2183b55e6aeSDmitri Tikhonov        HTTP_REASON_CODE(411, "Length Required"),
2193b55e6aeSDmitri Tikhonov        HTTP_REASON_CODE(412, "Precondition Failed"),
2203b55e6aeSDmitri Tikhonov        HTTP_REASON_CODE(413, "Payload Too Large"),
2213b55e6aeSDmitri Tikhonov        HTTP_REASON_CODE(414, "URI Too Long"),
2223b55e6aeSDmitri Tikhonov        HTTP_REASON_CODE(415, "Unsupported Media Type"),
2233b55e6aeSDmitri Tikhonov        HTTP_REASON_CODE(416, "Range Not Satisfiable"),
2243b55e6aeSDmitri Tikhonov        HTTP_REASON_CODE(417, "Expectation Failed"),
2253b55e6aeSDmitri Tikhonov        HTTP_REASON_CODE(426, "Upgrade Required"),
2263b55e6aeSDmitri Tikhonov        HTTP_REASON_CODE(500, "Internal Server Error"),
2273b55e6aeSDmitri Tikhonov        HTTP_REASON_CODE(501, "Not Implemented"),
2283b55e6aeSDmitri Tikhonov        HTTP_REASON_CODE(502, "Bad Gateway"),
2293b55e6aeSDmitri Tikhonov        HTTP_REASON_CODE(503, "Service Unavailable"),
2303b55e6aeSDmitri Tikhonov        HTTP_REASON_CODE(504, "Gateway Timeout"),
2313b55e6aeSDmitri Tikhonov        HTTP_REASON_CODE(505, "HTTP Version Not Supported"),
2323b55e6aeSDmitri Tikhonov    #undef HTTP_REASON_CODE
2333b55e6aeSDmitri Tikhonov    };
2343b55e6aeSDmitri Tikhonov
2353b55e6aeSDmitri Tikhonov    long code;
2363b55e6aeSDmitri Tikhonov    char code_buf[HTTP_CODE_LEN + 1];
2373b55e6aeSDmitri Tikhonov
2383b55e6aeSDmitri Tikhonov    memcpy(code_buf, code_str, HTTP_CODE_LEN);
2393b55e6aeSDmitri Tikhonov    code_buf[HTTP_CODE_LEN] = '\0';
2403b55e6aeSDmitri Tikhonov    code = strtol(code_buf, NULL, 10) - 100;
2413b55e6aeSDmitri Tikhonov    if (code > 0 && code < (long) (sizeof(http_reason_phrases) /
2423b55e6aeSDmitri Tikhonov                                        sizeof(http_reason_phrases[0])))
2433b55e6aeSDmitri Tikhonov        return http_reason_phrases[code];
2443b55e6aeSDmitri Tikhonov    else
2453b55e6aeSDmitri Tikhonov        return NULL;
2463b55e6aeSDmitri Tikhonov}
2473b55e6aeSDmitri Tikhonov
2483b55e6aeSDmitri Tikhonov
249a5fa05f9SDmitri Tikhonovstatic int
2503b55e6aeSDmitri Tikhonovconvert_response_pseudo_headers (struct header_writer_ctx *hwc)
2513b55e6aeSDmitri Tikhonov{
2523b55e6aeSDmitri Tikhonov    if ((hwc->pseh_mask & REQUIRED_SERVER_PSEH) != REQUIRED_SERVER_PSEH)
2533b55e6aeSDmitri Tikhonov    {
2543b55e6aeSDmitri Tikhonov        LSQ_INFO("not all response pseudo-headers are specified");
255a5fa05f9SDmitri Tikhonov        return 1;
2563b55e6aeSDmitri Tikhonov    }
2573b55e6aeSDmitri Tikhonov    if (hwc->pseh_mask & ALL_REQUEST_PSEH)
2583b55e6aeSDmitri Tikhonov    {
2593b55e6aeSDmitri Tikhonov        LSQ_INFO("response pseudo-headers contain request-only headers");
260a5fa05f9SDmitri Tikhonov        return 1;
2613b55e6aeSDmitri Tikhonov    }
2623b55e6aeSDmitri Tikhonov
2633b55e6aeSDmitri Tikhonov    const char *code_str, *reason;
2643b55e6aeSDmitri Tikhonov    int code_len;
2653b55e6aeSDmitri Tikhonov
2663b55e6aeSDmitri Tikhonov    code_str = HWC_PSEH_VAL(hwc, PSEH_STATUS);
2673b55e6aeSDmitri Tikhonov    code_len = HWC_PSEH_LEN(hwc, PSEH_STATUS);
2683b55e6aeSDmitri Tikhonov
2693b55e6aeSDmitri Tikhonov#define HWC_UH_WRITE(h, buf, sz) do {                                   \
2703b55e6aeSDmitri Tikhonov    if (0 != hwc_uh_write(h, buf, sz))                                  \
271a5fa05f9SDmitri Tikhonov        return -1;                                                      \
2723b55e6aeSDmitri Tikhonov} while (0)
2733b55e6aeSDmitri Tikhonov
2743b55e6aeSDmitri Tikhonov    HWC_UH_WRITE(hwc, "HTTP/1.1 ", 9);
2753b55e6aeSDmitri Tikhonov    HWC_UH_WRITE(hwc, code_str, code_len);
2763b55e6aeSDmitri Tikhonov    if (HTTP_CODE_LEN == code_len && (reason = code_str_to_reason(code_str)))
2773b55e6aeSDmitri Tikhonov    {
2783b55e6aeSDmitri Tikhonov        HWC_UH_WRITE(hwc, " ", 1);
2793b55e6aeSDmitri Tikhonov        HWC_UH_WRITE(hwc, reason, strlen(reason));
2803b55e6aeSDmitri Tikhonov        HWC_UH_WRITE(hwc, "\r\n", 2);
2813b55e6aeSDmitri Tikhonov    }
2823b55e6aeSDmitri Tikhonov    else
2833b55e6aeSDmitri Tikhonov        HWC_UH_WRITE(hwc, " \r\n", 3);
2843b55e6aeSDmitri Tikhonov    if (hwc->max_headers_sz && hwc->w_off > hwc->max_headers_sz)
2853b55e6aeSDmitri Tikhonov    {
2863b55e6aeSDmitri Tikhonov        LSQ_INFO("headers too large");
287a5fa05f9SDmitri Tikhonov        return 1;
2883b55e6aeSDmitri Tikhonov    }
289a5fa05f9SDmitri Tikhonov    return 0;
2903b55e6aeSDmitri Tikhonov
2913b55e6aeSDmitri Tikhonov#undef HWC_UH_WRITE
2923b55e6aeSDmitri Tikhonov}
2933b55e6aeSDmitri Tikhonov
2943b55e6aeSDmitri Tikhonov
295a5fa05f9SDmitri Tikhonovstatic int
2963b55e6aeSDmitri Tikhonovconvert_request_pseudo_headers (struct header_writer_ctx *hwc)
2973b55e6aeSDmitri Tikhonov{
2983b55e6aeSDmitri Tikhonov    if ((hwc->pseh_mask & REQUIRED_REQUEST_PSEH) != REQUIRED_REQUEST_PSEH)
2993b55e6aeSDmitri Tikhonov    {
3003b55e6aeSDmitri Tikhonov        LSQ_INFO("not all request pseudo-headers are specified");
301a5fa05f9SDmitri Tikhonov        return 1;
3023b55e6aeSDmitri Tikhonov    }
3033b55e6aeSDmitri Tikhonov    if (hwc->pseh_mask & ALL_SERVER_PSEH)
3043b55e6aeSDmitri Tikhonov    {
3053b55e6aeSDmitri Tikhonov        LSQ_INFO("request pseudo-headers contain response-only headers");
306a5fa05f9SDmitri Tikhonov        return 1;
3073b55e6aeSDmitri Tikhonov    }
3083b55e6aeSDmitri Tikhonov
3093b55e6aeSDmitri Tikhonov#define HWC_UH_WRITE(h, buf, sz) do {                                   \
3103b55e6aeSDmitri Tikhonov    if (0 != hwc_uh_write(h, buf, sz))                                  \
311a5fa05f9SDmitri Tikhonov        return -1;                                                      \
3123b55e6aeSDmitri Tikhonov} while (0)
3133b55e6aeSDmitri Tikhonov
3143b55e6aeSDmitri Tikhonov    HWC_UH_WRITE(hwc, HWC_PSEH_VAL(hwc, PSEH_METHOD), HWC_PSEH_LEN(hwc, PSEH_METHOD));
3153b55e6aeSDmitri Tikhonov    HWC_UH_WRITE(hwc, " ", 1);
3163b55e6aeSDmitri Tikhonov    HWC_UH_WRITE(hwc, HWC_PSEH_VAL(hwc, PSEH_PATH), HWC_PSEH_LEN(hwc, PSEH_PATH));
3173b55e6aeSDmitri Tikhonov    HWC_UH_WRITE(hwc, " HTTP/1.1\r\n", 11);
3183b55e6aeSDmitri Tikhonov
3193b55e6aeSDmitri Tikhonov    if (hwc->max_headers_sz && hwc->w_off > hwc->max_headers_sz)
3203b55e6aeSDmitri Tikhonov    {
3213b55e6aeSDmitri Tikhonov        LSQ_INFO("headers too large");
322a5fa05f9SDmitri Tikhonov        return 1;
3233b55e6aeSDmitri Tikhonov    }
3243b55e6aeSDmitri Tikhonov
3253b55e6aeSDmitri Tikhonov    return 0;
3263b55e6aeSDmitri Tikhonov
3273b55e6aeSDmitri Tikhonov#undef HWC_UH_WRITE
3283b55e6aeSDmitri Tikhonov}
3293b55e6aeSDmitri Tikhonov
3303b55e6aeSDmitri Tikhonov
331a5fa05f9SDmitri Tikhonovstatic int
3323b55e6aeSDmitri Tikhonovconvert_pseudo_headers (struct header_writer_ctx *hwc)
3333b55e6aeSDmitri Tikhonov{
3343b55e6aeSDmitri Tikhonov    /* We are *reading* the message.  Thus, a server expects a request, and a
3353b55e6aeSDmitri Tikhonov     * client expects a response.  Unless we receive a push promise from the
3363b55e6aeSDmitri Tikhonov     * server, in which case this should also be a request.
3373b55e6aeSDmitri Tikhonov     */
3383b55e6aeSDmitri Tikhonov    if (hwc->hwc_flags & (HWC_SERVER|HWC_PUSH_PROMISE))
3393b55e6aeSDmitri Tikhonov        return convert_request_pseudo_headers(hwc);
3403b55e6aeSDmitri Tikhonov    else
3413b55e6aeSDmitri Tikhonov        return convert_response_pseudo_headers(hwc);
3423b55e6aeSDmitri Tikhonov}
3433b55e6aeSDmitri Tikhonov
3443b55e6aeSDmitri Tikhonov
345a5fa05f9SDmitri Tikhonovstatic int
3463b55e6aeSDmitri Tikhonovsave_cookie (struct header_writer_ctx *hwc, const char *val, unsigned val_len)
3473b55e6aeSDmitri Tikhonov{
3483b55e6aeSDmitri Tikhonov    char *cookie_val;
3493b55e6aeSDmitri Tikhonov
3503b55e6aeSDmitri Tikhonov    if (0 == hwc->cookie_sz)
3513b55e6aeSDmitri Tikhonov    {
3523b55e6aeSDmitri Tikhonov        hwc->cookie_nalloc = hwc->cookie_sz = val_len;
3533b55e6aeSDmitri Tikhonov        cookie_val = malloc(hwc->cookie_nalloc);
3543b55e6aeSDmitri Tikhonov        if (!cookie_val)
355a5fa05f9SDmitri Tikhonov            return -1;
3563b55e6aeSDmitri Tikhonov        hwc->cookie_val = cookie_val;
3573b55e6aeSDmitri Tikhonov        memcpy(hwc->cookie_val, val, val_len);
3583b55e6aeSDmitri Tikhonov    }
3593b55e6aeSDmitri Tikhonov    else
3603b55e6aeSDmitri Tikhonov    {
3613b55e6aeSDmitri Tikhonov        hwc->cookie_sz += val_len + 2 /* "; " */;
3623b55e6aeSDmitri Tikhonov        if (hwc->cookie_sz > hwc->cookie_nalloc)
3633b55e6aeSDmitri Tikhonov        {
3643b55e6aeSDmitri Tikhonov            hwc->cookie_nalloc = hwc->cookie_nalloc * 2 + val_len + 2;
3653b55e6aeSDmitri Tikhonov            cookie_val = realloc(hwc->cookie_val, hwc->cookie_nalloc);
3663b55e6aeSDmitri Tikhonov            if (!cookie_val)
367a5fa05f9SDmitri Tikhonov                return -1;
3683b55e6aeSDmitri Tikhonov            hwc->cookie_val = cookie_val;
3693b55e6aeSDmitri Tikhonov        }
3703b55e6aeSDmitri Tikhonov        memcpy(hwc->cookie_val + hwc->cookie_sz - val_len - 2, "; ", 2);
3713b55e6aeSDmitri Tikhonov        memcpy(hwc->cookie_val + hwc->cookie_sz - val_len, val, val_len);
3723b55e6aeSDmitri Tikhonov    }
3733b55e6aeSDmitri Tikhonov
3743b55e6aeSDmitri Tikhonov    return 0;
3753b55e6aeSDmitri Tikhonov}
3763b55e6aeSDmitri Tikhonov
3773b55e6aeSDmitri Tikhonov
378a5fa05f9SDmitri Tikhonovstatic int
379a5fa05f9SDmitri Tikhonovadd_real_header (struct header_writer_ctx *hwc, struct lsxpack_header *xhdr)
3803b55e6aeSDmitri Tikhonov{
381a5fa05f9SDmitri Tikhonov    int err;
3823b55e6aeSDmitri Tikhonov    unsigned i;
3833b55e6aeSDmitri Tikhonov    int n_upper;
384a5fa05f9SDmitri Tikhonov    const char *name, *val;
385a5fa05f9SDmitri Tikhonov    unsigned name_len, val_len;
3863b55e6aeSDmitri Tikhonov
3873b55e6aeSDmitri Tikhonov    if (hwc->hwc_flags & HWC_EXPECT_COLON)
3883b55e6aeSDmitri Tikhonov    {
3893b55e6aeSDmitri Tikhonov        if (0 != (err = convert_pseudo_headers(hwc)))
3903b55e6aeSDmitri Tikhonov            return err;
3913b55e6aeSDmitri Tikhonov        hwc->hwc_flags &= ~HWC_EXPECT_COLON;
3923b55e6aeSDmitri Tikhonov    }
3933b55e6aeSDmitri Tikhonov
394a5fa05f9SDmitri Tikhonov    name = lsxpack_header_get_name(xhdr);
395a5fa05f9SDmitri Tikhonov    val = lsxpack_header_get_value(xhdr);
396a5fa05f9SDmitri Tikhonov    name_len = xhdr->name_len;
397a5fa05f9SDmitri Tikhonov    val_len = xhdr->val_len;
398a5fa05f9SDmitri Tikhonov
3993b55e6aeSDmitri Tikhonov    if (4 == name_len && 0 == memcmp(name, "host", 4))
4003b55e6aeSDmitri Tikhonov        hwc->hwc_flags |= HWC_SEEN_HOST;
4013b55e6aeSDmitri Tikhonov
4023b55e6aeSDmitri Tikhonov    n_upper = 0;
4033b55e6aeSDmitri Tikhonov    for (i = 0; i < name_len; ++i)
4043b55e6aeSDmitri Tikhonov        n_upper += isupper(name[i]);
4053b55e6aeSDmitri Tikhonov    if (n_upper > 0)
4063b55e6aeSDmitri Tikhonov    {
4073b55e6aeSDmitri Tikhonov        LSQ_INFO("Header name `%.*s' contains uppercase letters",
4083b55e6aeSDmitri Tikhonov            name_len, name);
409a5fa05f9SDmitri Tikhonov        return 1;
4103b55e6aeSDmitri Tikhonov    }
4113b55e6aeSDmitri Tikhonov
4123b55e6aeSDmitri Tikhonov    if (6 == name_len && memcmp(name, "cookie", 6) == 0)
4133b55e6aeSDmitri Tikhonov    {
4143b55e6aeSDmitri Tikhonov        return save_cookie(hwc, val, val_len);
4153b55e6aeSDmitri Tikhonov    }
4163b55e6aeSDmitri Tikhonov
4173b55e6aeSDmitri Tikhonov#define HWC_UH_WRITE(h, buf, sz) do {                                   \
4183b55e6aeSDmitri Tikhonov    if (0 != hwc_uh_write(h, buf, sz))                                  \
419a5fa05f9SDmitri Tikhonov        return -1;                                                      \
4203b55e6aeSDmitri Tikhonov} while (0)
4213b55e6aeSDmitri Tikhonov
4223b55e6aeSDmitri Tikhonov    HWC_UH_WRITE(hwc, name, name_len);
4233b55e6aeSDmitri Tikhonov    HWC_UH_WRITE(hwc, ": ", 2);
4243b55e6aeSDmitri Tikhonov    HWC_UH_WRITE(hwc, val, val_len);
4253b55e6aeSDmitri Tikhonov    HWC_UH_WRITE(hwc, "\r\n", 2);
4263b55e6aeSDmitri Tikhonov
4273b55e6aeSDmitri Tikhonov    if (hwc->max_headers_sz && hwc->w_off > hwc->max_headers_sz)
4283b55e6aeSDmitri Tikhonov    {
4293b55e6aeSDmitri Tikhonov        LSQ_INFO("headers too large");
430a5fa05f9SDmitri Tikhonov        return 1;
4313b55e6aeSDmitri Tikhonov    }
4323b55e6aeSDmitri Tikhonov
4333b55e6aeSDmitri Tikhonov    return 0;
4343b55e6aeSDmitri Tikhonov
4353b55e6aeSDmitri Tikhonov#undef HWC_UH_WRITE
4363b55e6aeSDmitri Tikhonov}
4373b55e6aeSDmitri Tikhonov
4383b55e6aeSDmitri Tikhonov
439a5fa05f9SDmitri Tikhonovstatic int
440a5fa05f9SDmitri Tikhonovadd_header_to_uh (struct header_writer_ctx *hwc, struct lsxpack_header *xhdr)
4413b55e6aeSDmitri Tikhonov{
442a5fa05f9SDmitri Tikhonov    const char *name;
443a5fa05f9SDmitri Tikhonov
444a5fa05f9SDmitri Tikhonov    name = lsxpack_header_get_name(xhdr);
445a5fa05f9SDmitri Tikhonov    LSQ_DEBUG("Got header '%.*s': '%.*s'", (int) xhdr->name_len, name,
446a5fa05f9SDmitri Tikhonov                        (int) xhdr->val_len, lsxpack_header_get_value(xhdr));
4473b55e6aeSDmitri Tikhonov    if (':' == name[0])
448a5fa05f9SDmitri Tikhonov        return add_pseudo_header(hwc, xhdr);
4493b55e6aeSDmitri Tikhonov    else
450a5fa05f9SDmitri Tikhonov        return add_real_header(hwc, xhdr);
4513b55e6aeSDmitri Tikhonov}
4523b55e6aeSDmitri Tikhonov
4533b55e6aeSDmitri Tikhonov
454a5fa05f9SDmitri Tikhonovstatic int
4553b55e6aeSDmitri Tikhonovh1h_finish_hset (struct header_writer_ctx *hwc)
4563b55e6aeSDmitri Tikhonov{
457a5fa05f9SDmitri Tikhonov    int st;
4583b55e6aeSDmitri Tikhonov
4593b55e6aeSDmitri Tikhonov    if (hwc->hwc_flags & HWC_EXPECT_COLON)
4603b55e6aeSDmitri Tikhonov    {
4613b55e6aeSDmitri Tikhonov        st = convert_pseudo_headers(hwc);
4623b55e6aeSDmitri Tikhonov        if (0 != st)
4633b55e6aeSDmitri Tikhonov            return st;
4643b55e6aeSDmitri Tikhonov        hwc->hwc_flags &= ~HWC_EXPECT_COLON;
4653b55e6aeSDmitri Tikhonov    }
4663b55e6aeSDmitri Tikhonov
4673b55e6aeSDmitri Tikhonov#define HWC_UH_WRITE(h, buf, sz) do {                                   \
4683b55e6aeSDmitri Tikhonov    st = hwc_uh_write(h, buf, sz);                                      \
4693b55e6aeSDmitri Tikhonov    if (0 != st)                                                        \
4703b55e6aeSDmitri Tikhonov        return st;                                                      \
4713b55e6aeSDmitri Tikhonov} while (0)
4723b55e6aeSDmitri Tikhonov
4733b55e6aeSDmitri Tikhonov    if ((hwc->pseh_mask & BIT(PSEH_AUTHORITY)) &&
4743b55e6aeSDmitri Tikhonov                                0 == (hwc->hwc_flags & HWC_SEEN_HOST))
4753b55e6aeSDmitri Tikhonov    {
4763b55e6aeSDmitri Tikhonov        LSQ_DEBUG("Setting 'Host: %.*s'", HWC_PSEH_LEN(hwc, PSEH_AUTHORITY),
4773b55e6aeSDmitri Tikhonov                                            HWC_PSEH_VAL(hwc, PSEH_AUTHORITY));
4783b55e6aeSDmitri Tikhonov        HWC_UH_WRITE(hwc, "Host: ", 6);
4793b55e6aeSDmitri Tikhonov        HWC_UH_WRITE(hwc, HWC_PSEH_VAL(hwc, PSEH_AUTHORITY),
4803b55e6aeSDmitri Tikhonov                                        HWC_PSEH_LEN(hwc, PSEH_AUTHORITY));
4813b55e6aeSDmitri Tikhonov        HWC_UH_WRITE(hwc, "\r\n", 2);
4823b55e6aeSDmitri Tikhonov    }
4833b55e6aeSDmitri Tikhonov
4843b55e6aeSDmitri Tikhonov    if (hwc->cookie_val)
4853b55e6aeSDmitri Tikhonov    {
4863b55e6aeSDmitri Tikhonov        LSQ_DEBUG("Setting 'Cookie: %.*s'", hwc->cookie_sz, hwc->cookie_val);
4873b55e6aeSDmitri Tikhonov        HWC_UH_WRITE(hwc, "Cookie: ", 8);
4883b55e6aeSDmitri Tikhonov        HWC_UH_WRITE(hwc, hwc->cookie_val, hwc->cookie_sz);
4893b55e6aeSDmitri Tikhonov        HWC_UH_WRITE(hwc, "\r\n", 2);
4903b55e6aeSDmitri Tikhonov    }
4913b55e6aeSDmitri Tikhonov
4923b55e6aeSDmitri Tikhonov    HWC_UH_WRITE(hwc, "\r\n", 2 + 1 /* NUL byte */);
4933b55e6aeSDmitri Tikhonov    hwc->w_off -= 1;     /* Do not count NUL byte */
4943b55e6aeSDmitri Tikhonov    hwc->hwc_h1h.h1h_size = hwc->w_off;
4953b55e6aeSDmitri Tikhonov
4963b55e6aeSDmitri Tikhonov    if (hwc->max_headers_sz && hwc->w_off > hwc->max_headers_sz)
4973b55e6aeSDmitri Tikhonov    {
4983b55e6aeSDmitri Tikhonov        LSQ_INFO("headers too large");
499a5fa05f9SDmitri Tikhonov        return 1;
5003b55e6aeSDmitri Tikhonov    }
5013b55e6aeSDmitri Tikhonov
502a5fa05f9SDmitri Tikhonov    return 0;
5033b55e6aeSDmitri Tikhonov}
5043b55e6aeSDmitri Tikhonov
5053b55e6aeSDmitri Tikhonov#define HWC_PTR(data_in) (struct header_writer_ctx *) \
5063b55e6aeSDmitri Tikhonov    ((unsigned char *) (hset) - offsetof(struct header_writer_ctx, hwc_h1h))
5073b55e6aeSDmitri Tikhonov
508a5fa05f9SDmitri Tikhonov
509a5fa05f9SDmitri Tikhonovstatic struct lsxpack_header *
510a5fa05f9SDmitri Tikhonovh1h_prepare_decode (void *hset, struct lsxpack_header *xhdr, size_t extra_space)
511a5fa05f9SDmitri Tikhonov{
512a5fa05f9SDmitri Tikhonov    struct header_writer_ctx *const hwc = HWC_PTR(hset);
513a5fa05f9SDmitri Tikhonov    size_t min_space;
514a5fa05f9SDmitri Tikhonov
515a5fa05f9SDmitri Tikhonov    if (0 == extra_space)
516a5fa05f9SDmitri Tikhonov        min_space = 0x100;
517a5fa05f9SDmitri Tikhonov    else
518a5fa05f9SDmitri Tikhonov        min_space = extra_space;
519a5fa05f9SDmitri Tikhonov
520a5fa05f9SDmitri Tikhonov    if (xhdr)
521a5fa05f9SDmitri Tikhonov    {
522a5fa05f9SDmitri Tikhonov        assert(xhdr == &hwc->hwc_xhdr);
523a5fa05f9SDmitri Tikhonov        min_space += xhdr->val_len;
524a5fa05f9SDmitri Tikhonov    }
525a5fa05f9SDmitri Tikhonov
526a5fa05f9SDmitri Tikhonov    if (min_space > MAX_HTTP1X_HEADERS_SIZE || min_space > LSXPACK_MAX_STRLEN)
527a5fa05f9SDmitri Tikhonov    {
528a5fa05f9SDmitri Tikhonov        LSQ_DEBUG("requested space for header is too large: %zd bytes",
529a5fa05f9SDmitri Tikhonov                                                                    min_space);
530a5fa05f9SDmitri Tikhonov        return NULL;
531a5fa05f9SDmitri Tikhonov    }
532a5fa05f9SDmitri Tikhonov
533a5fa05f9SDmitri Tikhonov    if (min_space > hwc->hwc_header_buf_nalloc)
534a5fa05f9SDmitri Tikhonov    {
535a5fa05f9SDmitri Tikhonov        free(hwc->hwc_header_buf);
536a5fa05f9SDmitri Tikhonov        hwc->hwc_header_buf_nalloc = 0;
537a5fa05f9SDmitri Tikhonov        hwc->hwc_header_buf = malloc(min_space);
538a5fa05f9SDmitri Tikhonov        if (!hwc->hwc_header_buf)
539a5fa05f9SDmitri Tikhonov        {
540a5fa05f9SDmitri Tikhonov            LSQ_DEBUG("cannot allocate %zd bytes", min_space);
541a5fa05f9SDmitri Tikhonov            return NULL;
542a5fa05f9SDmitri Tikhonov        }
543a5fa05f9SDmitri Tikhonov        hwc->hwc_header_buf_nalloc = min_space;
544a5fa05f9SDmitri Tikhonov    }
545a5fa05f9SDmitri Tikhonov
546a5fa05f9SDmitri Tikhonov    lsxpack_header_prepare_decode(&hwc->hwc_xhdr, hwc->hwc_header_buf,
547a5fa05f9SDmitri Tikhonov                                            0, hwc->hwc_header_buf_nalloc);
548a5fa05f9SDmitri Tikhonov    return &hwc->hwc_xhdr;
549a5fa05f9SDmitri Tikhonov}
550a5fa05f9SDmitri Tikhonov
551a5fa05f9SDmitri Tikhonov
552a5fa05f9SDmitri Tikhonovstatic int
553a5fa05f9SDmitri Tikhonovh1h_process_header (void *hset, struct lsxpack_header *xhdr)
5543b55e6aeSDmitri Tikhonov{
5553b55e6aeSDmitri Tikhonov    struct header_writer_ctx *const hwc = HWC_PTR(hset);
556a5fa05f9SDmitri Tikhonov    if (xhdr)
557a5fa05f9SDmitri Tikhonov        return add_header_to_uh(hwc, xhdr);
5583b55e6aeSDmitri Tikhonov    else
5593b55e6aeSDmitri Tikhonov        return h1h_finish_hset(hwc);
5603b55e6aeSDmitri Tikhonov}
5613b55e6aeSDmitri Tikhonov
5623b55e6aeSDmitri Tikhonov
5633b55e6aeSDmitri Tikhonovstatic void
5643b55e6aeSDmitri Tikhonovh1h_discard_header_set (void *hset)
5653b55e6aeSDmitri Tikhonov{
5663b55e6aeSDmitri Tikhonov    struct header_writer_ctx *const hwc = HWC_PTR(hset);
5673b55e6aeSDmitri Tikhonov    unsigned i;
5683b55e6aeSDmitri Tikhonov
5693b55e6aeSDmitri Tikhonov    for (i = 0; i < sizeof(hwc->pseh_bufs) / sizeof(hwc->pseh_bufs[0]); ++i)
5703b55e6aeSDmitri Tikhonov        if (hwc->pseh_bufs[i])
5713b55e6aeSDmitri Tikhonov            free(hwc->pseh_bufs[i]);
5723b55e6aeSDmitri Tikhonov    if (hwc->cookie_val)
5733b55e6aeSDmitri Tikhonov        free(hwc->cookie_val);
5743b55e6aeSDmitri Tikhonov    free(hwc->hwc_h1h.h1h_buf);
575a5fa05f9SDmitri Tikhonov    free(hwc->hwc_header_buf);
5763b55e6aeSDmitri Tikhonov    free(hwc);
5773b55e6aeSDmitri Tikhonov}
5783b55e6aeSDmitri Tikhonov
5793b55e6aeSDmitri Tikhonov
5803b55e6aeSDmitri Tikhonovstatic const struct lsquic_hset_if http1x_if =
5813b55e6aeSDmitri Tikhonov{
5823b55e6aeSDmitri Tikhonov    .hsi_create_header_set  = h1h_create_header_set,
583a5fa05f9SDmitri Tikhonov    .hsi_prepare_decode     = h1h_prepare_decode,
5843b55e6aeSDmitri Tikhonov    .hsi_process_header     = h1h_process_header,
5853b55e6aeSDmitri Tikhonov    .hsi_discard_header_set = h1h_discard_header_set,
5863b55e6aeSDmitri Tikhonov};
5873b55e6aeSDmitri Tikhonov
5883b55e6aeSDmitri Tikhonovconst struct lsquic_hset_if *const lsquic_http1x_if = &http1x_if;
589