lsquic_http1x_if.c revision 3b55e6ae
13b55e6aeSDmitri Tikhonov/* Copyright (c) 2017 - 2018 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
143b55e6aeSDmitri Tikhonov#define LSQUIC_LOG_CONN_ID hwc->hwc_cid
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{
393b55e6aeSDmitri Tikhonov    lsquic_cid_t                 hwc_cid;
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;
553b55e6aeSDmitri Tikhonov};
563b55e6aeSDmitri Tikhonov
573b55e6aeSDmitri Tikhonov
583b55e6aeSDmitri Tikhonov#define HWC_PSEH_LEN(hwc, ph) ((int) strlen((hwc)->pseh_bufs[ph]))
593b55e6aeSDmitri Tikhonov
603b55e6aeSDmitri Tikhonov#define HWC_PSEH_VAL(hwc, ph) ((hwc)->pseh_bufs[ph])
613b55e6aeSDmitri Tikhonov
623b55e6aeSDmitri Tikhonovstatic void *
633b55e6aeSDmitri Tikhonovh1h_create_header_set (void *ctx, int is_push_promise)
643b55e6aeSDmitri Tikhonov{
653b55e6aeSDmitri Tikhonov    const struct http1x_ctor_ctx *hcc = ctx;
663b55e6aeSDmitri Tikhonov    struct header_writer_ctx *hwc;
673b55e6aeSDmitri Tikhonov
683b55e6aeSDmitri Tikhonov    hwc = calloc(1, sizeof(*hwc));
693b55e6aeSDmitri Tikhonov    if (!hwc)
703b55e6aeSDmitri Tikhonov        return NULL;
713b55e6aeSDmitri Tikhonov
723b55e6aeSDmitri Tikhonov    hwc->hwc_flags = HWC_EXPECT_COLON;
733b55e6aeSDmitri Tikhonov    if (hcc->is_server)
743b55e6aeSDmitri Tikhonov        hwc->hwc_flags |= HWC_SERVER;
753b55e6aeSDmitri Tikhonov    if (is_push_promise)
763b55e6aeSDmitri Tikhonov        hwc->hwc_flags |= HWC_PUSH_PROMISE;
773b55e6aeSDmitri Tikhonov    hwc->max_headers_sz = hcc->max_headers_sz;
783b55e6aeSDmitri Tikhonov    hwc->hwc_cid = hcc->cid;
793b55e6aeSDmitri Tikhonov    return &hwc->hwc_h1h;
803b55e6aeSDmitri Tikhonov}
813b55e6aeSDmitri Tikhonov
823b55e6aeSDmitri Tikhonov
833b55e6aeSDmitri Tikhonovstatic int
843b55e6aeSDmitri Tikhonovhwc_uh_write (struct header_writer_ctx *hwc, const void *buf, size_t sz)
853b55e6aeSDmitri Tikhonov{
863b55e6aeSDmitri Tikhonov    char *h1h_buf;
873b55e6aeSDmitri Tikhonov
883b55e6aeSDmitri Tikhonov    if (hwc->w_off + sz > hwc->headers_sz)
893b55e6aeSDmitri Tikhonov    {
903b55e6aeSDmitri Tikhonov        if (hwc->headers_sz * 2 >= hwc->w_off + sz)
913b55e6aeSDmitri Tikhonov            hwc->headers_sz *= 2;
923b55e6aeSDmitri Tikhonov        else
933b55e6aeSDmitri Tikhonov            hwc->headers_sz = hwc->w_off + sz;
943b55e6aeSDmitri Tikhonov        h1h_buf = realloc(hwc->hwc_h1h.h1h_buf, hwc->headers_sz);
953b55e6aeSDmitri Tikhonov        if (!buf)
963b55e6aeSDmitri Tikhonov            return -1;
973b55e6aeSDmitri Tikhonov        hwc->hwc_h1h.h1h_buf = h1h_buf;
983b55e6aeSDmitri Tikhonov    }
993b55e6aeSDmitri Tikhonov    memcpy(&hwc->hwc_h1h.h1h_buf[hwc->w_off], buf, sz);
1003b55e6aeSDmitri Tikhonov    hwc->w_off += sz;
1013b55e6aeSDmitri Tikhonov    return 0;
1023b55e6aeSDmitri Tikhonov}
1033b55e6aeSDmitri Tikhonov
1043b55e6aeSDmitri Tikhonov
1053b55e6aeSDmitri Tikhonovstatic enum lsquic_header_status
1063b55e6aeSDmitri Tikhonovsave_pseudo_header (struct header_writer_ctx *hwc, enum pseudo_header ph,
1073b55e6aeSDmitri Tikhonov                    const char *val, unsigned val_len)
1083b55e6aeSDmitri Tikhonov{
1093b55e6aeSDmitri Tikhonov    if (0 == (hwc->pseh_mask & BIT(ph)))
1103b55e6aeSDmitri Tikhonov    {
1113b55e6aeSDmitri Tikhonov        assert(!hwc->pseh_bufs[ph]);
1123b55e6aeSDmitri Tikhonov        hwc->pseh_bufs[ph] = malloc(val_len + 1);
1133b55e6aeSDmitri Tikhonov        if (!hwc->pseh_bufs[ph])
1143b55e6aeSDmitri Tikhonov            return LSQUIC_HDR_ERR_NOMEM;
1153b55e6aeSDmitri Tikhonov        hwc->pseh_mask |= BIT(ph);
1163b55e6aeSDmitri Tikhonov        memcpy(hwc->pseh_bufs[ph], val, val_len);
1173b55e6aeSDmitri Tikhonov        hwc->pseh_bufs[ph][val_len] = '\0';
1183b55e6aeSDmitri Tikhonov        return LSQUIC_HDR_OK;
1193b55e6aeSDmitri Tikhonov    }
1203b55e6aeSDmitri Tikhonov    else
1213b55e6aeSDmitri Tikhonov    {
1223b55e6aeSDmitri Tikhonov        LSQ_INFO("header %u is already present", ph);
1233b55e6aeSDmitri Tikhonov        return LSQUIC_HDR_ERR_DUPLICATE_PSDO_HDR;
1243b55e6aeSDmitri Tikhonov    }
1253b55e6aeSDmitri Tikhonov}
1263b55e6aeSDmitri Tikhonov
1273b55e6aeSDmitri Tikhonov
1283b55e6aeSDmitri Tikhonovstatic enum lsquic_header_status
1293b55e6aeSDmitri Tikhonovadd_pseudo_header (struct header_writer_ctx *hwc, const char *name,
1303b55e6aeSDmitri Tikhonov                         unsigned name_len, const char *val, unsigned val_len)
1313b55e6aeSDmitri Tikhonov{
1323b55e6aeSDmitri Tikhonov    if (!(hwc->hwc_flags & HWC_EXPECT_COLON))
1333b55e6aeSDmitri Tikhonov    {
1343b55e6aeSDmitri Tikhonov        LSQ_INFO("unexpected colon");
1353b55e6aeSDmitri Tikhonov        return LSQUIC_HDR_ERR_MISPLACED_PSDO_HDR;
1363b55e6aeSDmitri Tikhonov    }
1373b55e6aeSDmitri Tikhonov
1383b55e6aeSDmitri Tikhonov    switch (name_len)
1393b55e6aeSDmitri Tikhonov    {
1403b55e6aeSDmitri Tikhonov    case 5:
1413b55e6aeSDmitri Tikhonov        if (0 == memcmp(name,     ":path", 5))
1423b55e6aeSDmitri Tikhonov            return save_pseudo_header(hwc, PSEH_PATH, val, val_len);
1433b55e6aeSDmitri Tikhonov        break;
1443b55e6aeSDmitri Tikhonov    case 7:
1453b55e6aeSDmitri Tikhonov        switch (name[2])
1463b55e6aeSDmitri Tikhonov        {
1473b55e6aeSDmitri Tikhonov        case 'c':
1483b55e6aeSDmitri Tikhonov            if (0 == memcmp(name, ":scheme", 7))
1493b55e6aeSDmitri Tikhonov                return save_pseudo_header(hwc, PSEH_SCHEME, val, val_len);
1503b55e6aeSDmitri Tikhonov            break;
1513b55e6aeSDmitri Tikhonov        case 'e':
1523b55e6aeSDmitri Tikhonov            if (0 == memcmp(name, ":method", 7))
1533b55e6aeSDmitri Tikhonov                return save_pseudo_header(hwc, PSEH_METHOD, val, val_len);
1543b55e6aeSDmitri Tikhonov            break;
1553b55e6aeSDmitri Tikhonov        case 't':
1563b55e6aeSDmitri Tikhonov            if (0 == memcmp(name, ":status", 7))
1573b55e6aeSDmitri Tikhonov                return save_pseudo_header(hwc, PSEH_STATUS, val, val_len);
1583b55e6aeSDmitri Tikhonov            break;
1593b55e6aeSDmitri Tikhonov        }
1603b55e6aeSDmitri Tikhonov        break;
1613b55e6aeSDmitri Tikhonov    case 10:
1623b55e6aeSDmitri Tikhonov        if (0 == memcmp(name,     ":authority", 10))
1633b55e6aeSDmitri Tikhonov            return save_pseudo_header(hwc, PSEH_AUTHORITY, val, val_len);
1643b55e6aeSDmitri Tikhonov        break;
1653b55e6aeSDmitri Tikhonov    }
1663b55e6aeSDmitri Tikhonov
1673b55e6aeSDmitri Tikhonov    LSQ_INFO("unknown pseudo-header `%.*s'", name_len, name);
1683b55e6aeSDmitri Tikhonov    return LSQUIC_HDR_ERR_UNKNOWN_PSDO_HDR;
1693b55e6aeSDmitri Tikhonov}
1703b55e6aeSDmitri Tikhonov
1713b55e6aeSDmitri Tikhonov
1723b55e6aeSDmitri Tikhonov#define HTTP_CODE_LEN 3
1733b55e6aeSDmitri Tikhonov
1743b55e6aeSDmitri Tikhonovstatic const char *
1753b55e6aeSDmitri Tikhonovcode_str_to_reason (const char code_str[HTTP_CODE_LEN])
1763b55e6aeSDmitri Tikhonov{
1773b55e6aeSDmitri Tikhonov    /* RFC 7231, Section 6: */
1783b55e6aeSDmitri Tikhonov    static const char *const http_reason_phrases[] =
1793b55e6aeSDmitri Tikhonov    {
1803b55e6aeSDmitri Tikhonov    #define HTTP_REASON_CODE(code, reason) [code - 100] = reason
1813b55e6aeSDmitri Tikhonov        HTTP_REASON_CODE(100, "Continue"),
1823b55e6aeSDmitri Tikhonov        HTTP_REASON_CODE(101, "Switching Protocols"),
1833b55e6aeSDmitri Tikhonov        HTTP_REASON_CODE(200, "OK"),
1843b55e6aeSDmitri Tikhonov        HTTP_REASON_CODE(201, "Created"),
1853b55e6aeSDmitri Tikhonov        HTTP_REASON_CODE(202, "Accepted"),
1863b55e6aeSDmitri Tikhonov        HTTP_REASON_CODE(203, "Non-Authoritative Information"),
1873b55e6aeSDmitri Tikhonov        HTTP_REASON_CODE(204, "No Content"),
1883b55e6aeSDmitri Tikhonov        HTTP_REASON_CODE(205, "Reset Content"),
1893b55e6aeSDmitri Tikhonov        HTTP_REASON_CODE(206, "Partial Content"),
1903b55e6aeSDmitri Tikhonov        HTTP_REASON_CODE(300, "Multiple Choices"),
1913b55e6aeSDmitri Tikhonov        HTTP_REASON_CODE(301, "Moved Permanently"),
1923b55e6aeSDmitri Tikhonov        HTTP_REASON_CODE(302, "Found"),
1933b55e6aeSDmitri Tikhonov        HTTP_REASON_CODE(303, "See Other"),
1943b55e6aeSDmitri Tikhonov        HTTP_REASON_CODE(304, "Not Modified"),
1953b55e6aeSDmitri Tikhonov        HTTP_REASON_CODE(305, "Use Proxy"),
1963b55e6aeSDmitri Tikhonov        HTTP_REASON_CODE(307, "Temporary Redirect"),
1973b55e6aeSDmitri Tikhonov        HTTP_REASON_CODE(400, "Bad Request"),
1983b55e6aeSDmitri Tikhonov        HTTP_REASON_CODE(401, "Unauthorized"),
1993b55e6aeSDmitri Tikhonov        HTTP_REASON_CODE(402, "Payment Required"),
2003b55e6aeSDmitri Tikhonov        HTTP_REASON_CODE(403, "Forbidden"),
2013b55e6aeSDmitri Tikhonov        HTTP_REASON_CODE(404, "Not Found"),
2023b55e6aeSDmitri Tikhonov        HTTP_REASON_CODE(405, "Method Not Allowed"),
2033b55e6aeSDmitri Tikhonov        HTTP_REASON_CODE(406, "Not Acceptable"),
2043b55e6aeSDmitri Tikhonov        HTTP_REASON_CODE(407, "Proxy Authentication Required"),
2053b55e6aeSDmitri Tikhonov        HTTP_REASON_CODE(408, "Request Timeout"),
2063b55e6aeSDmitri Tikhonov        HTTP_REASON_CODE(409, "Conflict"),
2073b55e6aeSDmitri Tikhonov        HTTP_REASON_CODE(410, "Gone"),
2083b55e6aeSDmitri Tikhonov        HTTP_REASON_CODE(411, "Length Required"),
2093b55e6aeSDmitri Tikhonov        HTTP_REASON_CODE(412, "Precondition Failed"),
2103b55e6aeSDmitri Tikhonov        HTTP_REASON_CODE(413, "Payload Too Large"),
2113b55e6aeSDmitri Tikhonov        HTTP_REASON_CODE(414, "URI Too Long"),
2123b55e6aeSDmitri Tikhonov        HTTP_REASON_CODE(415, "Unsupported Media Type"),
2133b55e6aeSDmitri Tikhonov        HTTP_REASON_CODE(416, "Range Not Satisfiable"),
2143b55e6aeSDmitri Tikhonov        HTTP_REASON_CODE(417, "Expectation Failed"),
2153b55e6aeSDmitri Tikhonov        HTTP_REASON_CODE(426, "Upgrade Required"),
2163b55e6aeSDmitri Tikhonov        HTTP_REASON_CODE(500, "Internal Server Error"),
2173b55e6aeSDmitri Tikhonov        HTTP_REASON_CODE(501, "Not Implemented"),
2183b55e6aeSDmitri Tikhonov        HTTP_REASON_CODE(502, "Bad Gateway"),
2193b55e6aeSDmitri Tikhonov        HTTP_REASON_CODE(503, "Service Unavailable"),
2203b55e6aeSDmitri Tikhonov        HTTP_REASON_CODE(504, "Gateway Timeout"),
2213b55e6aeSDmitri Tikhonov        HTTP_REASON_CODE(505, "HTTP Version Not Supported"),
2223b55e6aeSDmitri Tikhonov    #undef HTTP_REASON_CODE
2233b55e6aeSDmitri Tikhonov    };
2243b55e6aeSDmitri Tikhonov
2253b55e6aeSDmitri Tikhonov    long code;
2263b55e6aeSDmitri Tikhonov    char code_buf[HTTP_CODE_LEN + 1];
2273b55e6aeSDmitri Tikhonov
2283b55e6aeSDmitri Tikhonov    memcpy(code_buf, code_str, HTTP_CODE_LEN);
2293b55e6aeSDmitri Tikhonov    code_buf[HTTP_CODE_LEN] = '\0';
2303b55e6aeSDmitri Tikhonov    code = strtol(code_buf, NULL, 10) - 100;
2313b55e6aeSDmitri Tikhonov    if (code > 0 && code < (long) (sizeof(http_reason_phrases) /
2323b55e6aeSDmitri Tikhonov                                        sizeof(http_reason_phrases[0])))
2333b55e6aeSDmitri Tikhonov        return http_reason_phrases[code];
2343b55e6aeSDmitri Tikhonov    else
2353b55e6aeSDmitri Tikhonov        return NULL;
2363b55e6aeSDmitri Tikhonov}
2373b55e6aeSDmitri Tikhonov
2383b55e6aeSDmitri Tikhonov
2393b55e6aeSDmitri Tikhonovstatic enum lsquic_header_status
2403b55e6aeSDmitri Tikhonovconvert_response_pseudo_headers (struct header_writer_ctx *hwc)
2413b55e6aeSDmitri Tikhonov{
2423b55e6aeSDmitri Tikhonov    if ((hwc->pseh_mask & REQUIRED_SERVER_PSEH) != REQUIRED_SERVER_PSEH)
2433b55e6aeSDmitri Tikhonov    {
2443b55e6aeSDmitri Tikhonov        LSQ_INFO("not all response pseudo-headers are specified");
2453b55e6aeSDmitri Tikhonov        return LSQUIC_HDR_ERR_INCOMPL_RESP_PSDO_HDR;
2463b55e6aeSDmitri Tikhonov    }
2473b55e6aeSDmitri Tikhonov    if (hwc->pseh_mask & ALL_REQUEST_PSEH)
2483b55e6aeSDmitri Tikhonov    {
2493b55e6aeSDmitri Tikhonov        LSQ_INFO("response pseudo-headers contain request-only headers");
2503b55e6aeSDmitri Tikhonov        return LSQUIC_HDR_ERR_UNNEC_REQ_PSDO_HDR;
2513b55e6aeSDmitri Tikhonov    }
2523b55e6aeSDmitri Tikhonov
2533b55e6aeSDmitri Tikhonov    const char *code_str, *reason;
2543b55e6aeSDmitri Tikhonov    int code_len;
2553b55e6aeSDmitri Tikhonov
2563b55e6aeSDmitri Tikhonov    code_str = HWC_PSEH_VAL(hwc, PSEH_STATUS);
2573b55e6aeSDmitri Tikhonov    code_len = HWC_PSEH_LEN(hwc, PSEH_STATUS);
2583b55e6aeSDmitri Tikhonov
2593b55e6aeSDmitri Tikhonov#define HWC_UH_WRITE(h, buf, sz) do {                                   \
2603b55e6aeSDmitri Tikhonov    if (0 != hwc_uh_write(h, buf, sz))                                  \
2613b55e6aeSDmitri Tikhonov        return LSQUIC_HDR_ERR_NOMEM;                                    \
2623b55e6aeSDmitri Tikhonov} while (0)
2633b55e6aeSDmitri Tikhonov
2643b55e6aeSDmitri Tikhonov    HWC_UH_WRITE(hwc, "HTTP/1.1 ", 9);
2653b55e6aeSDmitri Tikhonov    HWC_UH_WRITE(hwc, code_str, code_len);
2663b55e6aeSDmitri Tikhonov    if (HTTP_CODE_LEN == code_len && (reason = code_str_to_reason(code_str)))
2673b55e6aeSDmitri Tikhonov    {
2683b55e6aeSDmitri Tikhonov        HWC_UH_WRITE(hwc, " ", 1);
2693b55e6aeSDmitri Tikhonov        HWC_UH_WRITE(hwc, reason, strlen(reason));
2703b55e6aeSDmitri Tikhonov        HWC_UH_WRITE(hwc, "\r\n", 2);
2713b55e6aeSDmitri Tikhonov    }
2723b55e6aeSDmitri Tikhonov    else
2733b55e6aeSDmitri Tikhonov        HWC_UH_WRITE(hwc, " \r\n", 3);
2743b55e6aeSDmitri Tikhonov    if (hwc->max_headers_sz && hwc->w_off > hwc->max_headers_sz)
2753b55e6aeSDmitri Tikhonov    {
2763b55e6aeSDmitri Tikhonov        LSQ_INFO("headers too large");
2773b55e6aeSDmitri Tikhonov        return LSQUIC_HDR_ERR_HEADERS_TOO_LARGE;
2783b55e6aeSDmitri Tikhonov    }
2793b55e6aeSDmitri Tikhonov    return LSQUIC_HDR_OK;
2803b55e6aeSDmitri Tikhonov
2813b55e6aeSDmitri Tikhonov#undef HWC_UH_WRITE
2823b55e6aeSDmitri Tikhonov}
2833b55e6aeSDmitri Tikhonov
2843b55e6aeSDmitri Tikhonov
2853b55e6aeSDmitri Tikhonovstatic enum lsquic_header_status
2863b55e6aeSDmitri Tikhonovconvert_request_pseudo_headers (struct header_writer_ctx *hwc)
2873b55e6aeSDmitri Tikhonov{
2883b55e6aeSDmitri Tikhonov    if ((hwc->pseh_mask & REQUIRED_REQUEST_PSEH) != REQUIRED_REQUEST_PSEH)
2893b55e6aeSDmitri Tikhonov    {
2903b55e6aeSDmitri Tikhonov        LSQ_INFO("not all request pseudo-headers are specified");
2913b55e6aeSDmitri Tikhonov        return LSQUIC_HDR_ERR_INCOMPL_REQ_PSDO_HDR;
2923b55e6aeSDmitri Tikhonov    }
2933b55e6aeSDmitri Tikhonov    if (hwc->pseh_mask & ALL_SERVER_PSEH)
2943b55e6aeSDmitri Tikhonov    {
2953b55e6aeSDmitri Tikhonov        LSQ_INFO("request pseudo-headers contain response-only headers");
2963b55e6aeSDmitri Tikhonov        return LSQUIC_HDR_ERR_UNNEC_RESP_PSDO_HDR;
2973b55e6aeSDmitri Tikhonov    }
2983b55e6aeSDmitri Tikhonov
2993b55e6aeSDmitri Tikhonov#define HWC_UH_WRITE(h, buf, sz) do {                                   \
3003b55e6aeSDmitri Tikhonov    if (0 != hwc_uh_write(h, buf, sz))                                  \
3013b55e6aeSDmitri Tikhonov        return LSQUIC_HDR_ERR_NOMEM;                                    \
3023b55e6aeSDmitri Tikhonov} while (0)
3033b55e6aeSDmitri Tikhonov
3043b55e6aeSDmitri Tikhonov    HWC_UH_WRITE(hwc, HWC_PSEH_VAL(hwc, PSEH_METHOD), HWC_PSEH_LEN(hwc, PSEH_METHOD));
3053b55e6aeSDmitri Tikhonov    HWC_UH_WRITE(hwc, " ", 1);
3063b55e6aeSDmitri Tikhonov    HWC_UH_WRITE(hwc, HWC_PSEH_VAL(hwc, PSEH_PATH), HWC_PSEH_LEN(hwc, PSEH_PATH));
3073b55e6aeSDmitri Tikhonov    HWC_UH_WRITE(hwc, " HTTP/1.1\r\n", 11);
3083b55e6aeSDmitri Tikhonov
3093b55e6aeSDmitri Tikhonov    if (hwc->max_headers_sz && hwc->w_off > hwc->max_headers_sz)
3103b55e6aeSDmitri Tikhonov    {
3113b55e6aeSDmitri Tikhonov        LSQ_INFO("headers too large");
3123b55e6aeSDmitri Tikhonov        return LSQUIC_HDR_ERR_HEADERS_TOO_LARGE;
3133b55e6aeSDmitri Tikhonov    }
3143b55e6aeSDmitri Tikhonov
3153b55e6aeSDmitri Tikhonov    return 0;
3163b55e6aeSDmitri Tikhonov
3173b55e6aeSDmitri Tikhonov#undef HWC_UH_WRITE
3183b55e6aeSDmitri Tikhonov}
3193b55e6aeSDmitri Tikhonov
3203b55e6aeSDmitri Tikhonov
3213b55e6aeSDmitri Tikhonovstatic enum lsquic_header_status
3223b55e6aeSDmitri Tikhonovconvert_pseudo_headers (struct header_writer_ctx *hwc)
3233b55e6aeSDmitri Tikhonov{
3243b55e6aeSDmitri Tikhonov    /* We are *reading* the message.  Thus, a server expects a request, and a
3253b55e6aeSDmitri Tikhonov     * client expects a response.  Unless we receive a push promise from the
3263b55e6aeSDmitri Tikhonov     * server, in which case this should also be a request.
3273b55e6aeSDmitri Tikhonov     */
3283b55e6aeSDmitri Tikhonov    if (hwc->hwc_flags & (HWC_SERVER|HWC_PUSH_PROMISE))
3293b55e6aeSDmitri Tikhonov        return convert_request_pseudo_headers(hwc);
3303b55e6aeSDmitri Tikhonov    else
3313b55e6aeSDmitri Tikhonov        return convert_response_pseudo_headers(hwc);
3323b55e6aeSDmitri Tikhonov}
3333b55e6aeSDmitri Tikhonov
3343b55e6aeSDmitri Tikhonov
3353b55e6aeSDmitri Tikhonovstatic enum lsquic_header_status
3363b55e6aeSDmitri Tikhonovsave_cookie (struct header_writer_ctx *hwc, const char *val, unsigned val_len)
3373b55e6aeSDmitri Tikhonov{
3383b55e6aeSDmitri Tikhonov    char *cookie_val;
3393b55e6aeSDmitri Tikhonov
3403b55e6aeSDmitri Tikhonov    if (0 == hwc->cookie_sz)
3413b55e6aeSDmitri Tikhonov    {
3423b55e6aeSDmitri Tikhonov        hwc->cookie_nalloc = hwc->cookie_sz = val_len;
3433b55e6aeSDmitri Tikhonov        cookie_val = malloc(hwc->cookie_nalloc);
3443b55e6aeSDmitri Tikhonov        if (!cookie_val)
3453b55e6aeSDmitri Tikhonov            return LSQUIC_HDR_ERR_NOMEM;
3463b55e6aeSDmitri Tikhonov        hwc->cookie_val = cookie_val;
3473b55e6aeSDmitri Tikhonov        memcpy(hwc->cookie_val, val, val_len);
3483b55e6aeSDmitri Tikhonov    }
3493b55e6aeSDmitri Tikhonov    else
3503b55e6aeSDmitri Tikhonov    {
3513b55e6aeSDmitri Tikhonov        hwc->cookie_sz += val_len + 2 /* "; " */;
3523b55e6aeSDmitri Tikhonov        if (hwc->cookie_sz > hwc->cookie_nalloc)
3533b55e6aeSDmitri Tikhonov        {
3543b55e6aeSDmitri Tikhonov            hwc->cookie_nalloc = hwc->cookie_nalloc * 2 + val_len + 2;
3553b55e6aeSDmitri Tikhonov            cookie_val = realloc(hwc->cookie_val, hwc->cookie_nalloc);
3563b55e6aeSDmitri Tikhonov            if (!cookie_val)
3573b55e6aeSDmitri Tikhonov                return LSQUIC_HDR_ERR_NOMEM;
3583b55e6aeSDmitri Tikhonov            hwc->cookie_val = cookie_val;
3593b55e6aeSDmitri Tikhonov        }
3603b55e6aeSDmitri Tikhonov        memcpy(hwc->cookie_val + hwc->cookie_sz - val_len - 2, "; ", 2);
3613b55e6aeSDmitri Tikhonov        memcpy(hwc->cookie_val + hwc->cookie_sz - val_len, val, val_len);
3623b55e6aeSDmitri Tikhonov    }
3633b55e6aeSDmitri Tikhonov
3643b55e6aeSDmitri Tikhonov    return 0;
3653b55e6aeSDmitri Tikhonov}
3663b55e6aeSDmitri Tikhonov
3673b55e6aeSDmitri Tikhonov
3683b55e6aeSDmitri Tikhonovstatic enum lsquic_header_status
3693b55e6aeSDmitri Tikhonovadd_real_header (struct header_writer_ctx *hwc, const char *name,
3703b55e6aeSDmitri Tikhonov                 unsigned name_len, const char *val, unsigned val_len)
3713b55e6aeSDmitri Tikhonov{
3723b55e6aeSDmitri Tikhonov    enum lsquic_header_status err;
3733b55e6aeSDmitri Tikhonov    unsigned i;
3743b55e6aeSDmitri Tikhonov    int n_upper;
3753b55e6aeSDmitri Tikhonov
3763b55e6aeSDmitri Tikhonov    if (hwc->hwc_flags & HWC_EXPECT_COLON)
3773b55e6aeSDmitri Tikhonov    {
3783b55e6aeSDmitri Tikhonov        if (0 != (err = convert_pseudo_headers(hwc)))
3793b55e6aeSDmitri Tikhonov            return err;
3803b55e6aeSDmitri Tikhonov        hwc->hwc_flags &= ~HWC_EXPECT_COLON;
3813b55e6aeSDmitri Tikhonov    }
3823b55e6aeSDmitri Tikhonov
3833b55e6aeSDmitri Tikhonov    if (4 == name_len && 0 == memcmp(name, "host", 4))
3843b55e6aeSDmitri Tikhonov        hwc->hwc_flags |= HWC_SEEN_HOST;
3853b55e6aeSDmitri Tikhonov
3863b55e6aeSDmitri Tikhonov    n_upper = 0;
3873b55e6aeSDmitri Tikhonov    for (i = 0; i < name_len; ++i)
3883b55e6aeSDmitri Tikhonov        n_upper += isupper(name[i]);
3893b55e6aeSDmitri Tikhonov    if (n_upper > 0)
3903b55e6aeSDmitri Tikhonov    {
3913b55e6aeSDmitri Tikhonov        LSQ_INFO("Header name `%.*s' contains uppercase letters",
3923b55e6aeSDmitri Tikhonov            name_len, name);
3933b55e6aeSDmitri Tikhonov        return LSQUIC_HDR_ERR_UPPERCASE_HEADER;
3943b55e6aeSDmitri Tikhonov    }
3953b55e6aeSDmitri Tikhonov
3963b55e6aeSDmitri Tikhonov    if (6 == name_len && memcmp(name, "cookie", 6) == 0)
3973b55e6aeSDmitri Tikhonov    {
3983b55e6aeSDmitri Tikhonov        return save_cookie(hwc, val, val_len);
3993b55e6aeSDmitri Tikhonov    }
4003b55e6aeSDmitri Tikhonov
4013b55e6aeSDmitri Tikhonov#define HWC_UH_WRITE(h, buf, sz) do {                                   \
4023b55e6aeSDmitri Tikhonov    if (0 != hwc_uh_write(h, buf, sz))                                  \
4033b55e6aeSDmitri Tikhonov        return LSQUIC_HDR_ERR_NOMEM;                                    \
4043b55e6aeSDmitri Tikhonov} while (0)
4053b55e6aeSDmitri Tikhonov
4063b55e6aeSDmitri Tikhonov    HWC_UH_WRITE(hwc, name, name_len);
4073b55e6aeSDmitri Tikhonov    HWC_UH_WRITE(hwc, ": ", 2);
4083b55e6aeSDmitri Tikhonov    HWC_UH_WRITE(hwc, val, val_len);
4093b55e6aeSDmitri Tikhonov    HWC_UH_WRITE(hwc, "\r\n", 2);
4103b55e6aeSDmitri Tikhonov
4113b55e6aeSDmitri Tikhonov    if (hwc->max_headers_sz && hwc->w_off > hwc->max_headers_sz)
4123b55e6aeSDmitri Tikhonov    {
4133b55e6aeSDmitri Tikhonov        LSQ_INFO("headers too large");
4143b55e6aeSDmitri Tikhonov        return LSQUIC_HDR_ERR_HEADERS_TOO_LARGE;
4153b55e6aeSDmitri Tikhonov    }
4163b55e6aeSDmitri Tikhonov
4173b55e6aeSDmitri Tikhonov    return 0;
4183b55e6aeSDmitri Tikhonov
4193b55e6aeSDmitri Tikhonov#undef HWC_UH_WRITE
4203b55e6aeSDmitri Tikhonov}
4213b55e6aeSDmitri Tikhonov
4223b55e6aeSDmitri Tikhonov
4233b55e6aeSDmitri Tikhonovstatic enum lsquic_header_status
4243b55e6aeSDmitri Tikhonovadd_header_to_uh (struct header_writer_ctx *hwc, const char *name,
4253b55e6aeSDmitri Tikhonov                  unsigned name_len, const char *val, unsigned val_len)
4263b55e6aeSDmitri Tikhonov{
4273b55e6aeSDmitri Tikhonov    LSQ_DEBUG("Got header '%.*s': '%.*s'", name_len, name, val_len, val);
4283b55e6aeSDmitri Tikhonov    if (':' == name[0])
4293b55e6aeSDmitri Tikhonov        return add_pseudo_header(hwc, name, name_len, val, val_len);
4303b55e6aeSDmitri Tikhonov    else
4313b55e6aeSDmitri Tikhonov        return add_real_header(hwc, name, name_len, val, val_len);
4323b55e6aeSDmitri Tikhonov}
4333b55e6aeSDmitri Tikhonov
4343b55e6aeSDmitri Tikhonov
4353b55e6aeSDmitri Tikhonovstatic enum lsquic_header_status
4363b55e6aeSDmitri Tikhonovh1h_finish_hset (struct header_writer_ctx *hwc)
4373b55e6aeSDmitri Tikhonov{
4383b55e6aeSDmitri Tikhonov    enum lsquic_header_status st;
4393b55e6aeSDmitri Tikhonov
4403b55e6aeSDmitri Tikhonov    if (hwc->hwc_flags & HWC_EXPECT_COLON)
4413b55e6aeSDmitri Tikhonov    {
4423b55e6aeSDmitri Tikhonov        st = convert_pseudo_headers(hwc);
4433b55e6aeSDmitri Tikhonov        if (0 != st)
4443b55e6aeSDmitri Tikhonov            return st;
4453b55e6aeSDmitri Tikhonov        hwc->hwc_flags &= ~HWC_EXPECT_COLON;
4463b55e6aeSDmitri Tikhonov    }
4473b55e6aeSDmitri Tikhonov
4483b55e6aeSDmitri Tikhonov#define HWC_UH_WRITE(h, buf, sz) do {                                   \
4493b55e6aeSDmitri Tikhonov    st = hwc_uh_write(h, buf, sz);                                      \
4503b55e6aeSDmitri Tikhonov    if (0 != st)                                                        \
4513b55e6aeSDmitri Tikhonov        return st;                                                      \
4523b55e6aeSDmitri Tikhonov} while (0)
4533b55e6aeSDmitri Tikhonov
4543b55e6aeSDmitri Tikhonov    if ((hwc->pseh_mask & BIT(PSEH_AUTHORITY)) &&
4553b55e6aeSDmitri Tikhonov                                0 == (hwc->hwc_flags & HWC_SEEN_HOST))
4563b55e6aeSDmitri Tikhonov    {
4573b55e6aeSDmitri Tikhonov        LSQ_DEBUG("Setting 'Host: %.*s'", HWC_PSEH_LEN(hwc, PSEH_AUTHORITY),
4583b55e6aeSDmitri Tikhonov                                            HWC_PSEH_VAL(hwc, PSEH_AUTHORITY));
4593b55e6aeSDmitri Tikhonov        HWC_UH_WRITE(hwc, "Host: ", 6);
4603b55e6aeSDmitri Tikhonov        HWC_UH_WRITE(hwc, HWC_PSEH_VAL(hwc, PSEH_AUTHORITY),
4613b55e6aeSDmitri Tikhonov                                        HWC_PSEH_LEN(hwc, PSEH_AUTHORITY));
4623b55e6aeSDmitri Tikhonov        HWC_UH_WRITE(hwc, "\r\n", 2);
4633b55e6aeSDmitri Tikhonov    }
4643b55e6aeSDmitri Tikhonov
4653b55e6aeSDmitri Tikhonov    if (hwc->cookie_val)
4663b55e6aeSDmitri Tikhonov    {
4673b55e6aeSDmitri Tikhonov        LSQ_DEBUG("Setting 'Cookie: %.*s'", hwc->cookie_sz, hwc->cookie_val);
4683b55e6aeSDmitri Tikhonov        HWC_UH_WRITE(hwc, "Cookie: ", 8);
4693b55e6aeSDmitri Tikhonov        HWC_UH_WRITE(hwc, hwc->cookie_val, hwc->cookie_sz);
4703b55e6aeSDmitri Tikhonov        HWC_UH_WRITE(hwc, "\r\n", 2);
4713b55e6aeSDmitri Tikhonov    }
4723b55e6aeSDmitri Tikhonov
4733b55e6aeSDmitri Tikhonov    HWC_UH_WRITE(hwc, "\r\n", 2 + 1 /* NUL byte */);
4743b55e6aeSDmitri Tikhonov    hwc->w_off -= 1;     /* Do not count NUL byte */
4753b55e6aeSDmitri Tikhonov    hwc->hwc_h1h.h1h_size = hwc->w_off;
4763b55e6aeSDmitri Tikhonov
4773b55e6aeSDmitri Tikhonov    if (hwc->max_headers_sz && hwc->w_off > hwc->max_headers_sz)
4783b55e6aeSDmitri Tikhonov    {
4793b55e6aeSDmitri Tikhonov        LSQ_INFO("headers too large");
4803b55e6aeSDmitri Tikhonov        return LSQUIC_HDR_ERR_HEADERS_TOO_LARGE;
4813b55e6aeSDmitri Tikhonov    }
4823b55e6aeSDmitri Tikhonov
4833b55e6aeSDmitri Tikhonov    return LSQUIC_HDR_OK;
4843b55e6aeSDmitri Tikhonov}
4853b55e6aeSDmitri Tikhonov
4863b55e6aeSDmitri Tikhonov#define HWC_PTR(data_in) (struct header_writer_ctx *) \
4873b55e6aeSDmitri Tikhonov    ((unsigned char *) (hset) - offsetof(struct header_writer_ctx, hwc_h1h))
4883b55e6aeSDmitri Tikhonov
4893b55e6aeSDmitri Tikhonovstatic enum lsquic_header_status
4903b55e6aeSDmitri Tikhonovh1h_process_header (void *hset, unsigned name_idx,
4913b55e6aeSDmitri Tikhonov                    const char *name, unsigned name_len,
4923b55e6aeSDmitri Tikhonov                    const char *value, unsigned value_len)
4933b55e6aeSDmitri Tikhonov{
4943b55e6aeSDmitri Tikhonov    struct header_writer_ctx *const hwc = HWC_PTR(hset);
4953b55e6aeSDmitri Tikhonov    if (name)
4963b55e6aeSDmitri Tikhonov        return add_header_to_uh(hwc, name, name_len, value, value_len);
4973b55e6aeSDmitri Tikhonov    else
4983b55e6aeSDmitri Tikhonov        return h1h_finish_hset(hwc);
4993b55e6aeSDmitri Tikhonov}
5003b55e6aeSDmitri Tikhonov
5013b55e6aeSDmitri Tikhonov
5023b55e6aeSDmitri Tikhonovstatic void
5033b55e6aeSDmitri Tikhonovh1h_discard_header_set (void *hset)
5043b55e6aeSDmitri Tikhonov{
5053b55e6aeSDmitri Tikhonov    struct header_writer_ctx *const hwc = HWC_PTR(hset);
5063b55e6aeSDmitri Tikhonov    unsigned i;
5073b55e6aeSDmitri Tikhonov
5083b55e6aeSDmitri Tikhonov    for (i = 0; i < sizeof(hwc->pseh_bufs) / sizeof(hwc->pseh_bufs[0]); ++i)
5093b55e6aeSDmitri Tikhonov        if (hwc->pseh_bufs[i])
5103b55e6aeSDmitri Tikhonov            free(hwc->pseh_bufs[i]);
5113b55e6aeSDmitri Tikhonov    if (hwc->cookie_val)
5123b55e6aeSDmitri Tikhonov        free(hwc->cookie_val);
5133b55e6aeSDmitri Tikhonov    free(hwc->hwc_h1h.h1h_buf);
5143b55e6aeSDmitri Tikhonov    free(hwc);
5153b55e6aeSDmitri Tikhonov}
5163b55e6aeSDmitri Tikhonov
5173b55e6aeSDmitri Tikhonov
5183b55e6aeSDmitri Tikhonovstatic const struct lsquic_hset_if http1x_if =
5193b55e6aeSDmitri Tikhonov{
5203b55e6aeSDmitri Tikhonov    .hsi_create_header_set  = h1h_create_header_set,
5213b55e6aeSDmitri Tikhonov    .hsi_process_header     = h1h_process_header,
5223b55e6aeSDmitri Tikhonov    .hsi_discard_header_set = h1h_discard_header_set,
5233b55e6aeSDmitri Tikhonov};
5243b55e6aeSDmitri Tikhonov
5253b55e6aeSDmitri Tikhonovconst struct lsquic_hset_if *const lsquic_http1x_if = &http1x_if;
526