hpackenc.c 7.31 KB
Newer Older
1
/*****************************************************************************
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
2
 * hpackenc.c: HPACK Header Compression for HTTP/2 encoder
3 4 5 6 7 8 9 10 11 12 13
 *****************************************************************************
 * Copyright (C) 2015 Rémi Denis-Courmont
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU Lesser General Public License for more details.
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
 *****************************************************************************/

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#ifdef ENC_TEST
# undef NDEBUG
#endif

#include <assert.h>
#include <stdint.h>
#include <string.h>

#include "hpack.h"

/*
 * This is curently the simplest possible HPACK compressor: it does not
 * compress anything and is stateless.
 * TODO:
 *  - use static Huffman compression when useful,
 *  - use header names and values from the static HPACK table,
 *  - become stateful and let the caller specify the indexed flag,
 *  - let caller specify the never-indexed flag.
 */

static size_t hpack_encode_int(uint8_t *restrict buf, size_t size,
                               uintmax_t value, unsigned n)
{
    assert(n >= 1 && n <= 8);

    unsigned mask = (1 << n) - 1;
    size_t ret = 1;

    if (value < mask)
    {
        if (size > 0)
            buf[0] |= value;
        return 1;
    }

    if (size > 0)
       *(buf++) |= mask;
    value -= mask;

    while (value >= 128)
    {
        if (ret++ < size)
            *(buf++) = 0x80 | (value & 0x7F);
        value >>= 7;
    }

    if (ret++ < size)
        *(buf++) = value;
    return ret;
}

static size_t hpack_encode_str_raw(uint8_t *restrict buf, size_t size,
                                   const char *str)
{
    size_t len = strlen(str);

    if (size > 0)
        *buf = 0;

    size_t ret = hpack_encode_int(buf, size, len, 7);
    if (ret < size)
    {
        buf += ret;
        size -= ret;

        memcpy(buf, str, (len <= size) ? len : size);
    }
    ret += len;
    return ret;
}

static size_t hpack_encode_str_raw_lower(uint8_t *restrict buf, size_t size,
                                         const char *str)
{
    size_t len = strlen(str);

    if (size > 0)
        *buf = 0;

    size_t ret = hpack_encode_int(buf, size, len, 7);
    if (ret < size)
    {
        buf += ret;
        size -= ret;

        for (size_t i = 0; i < len && i < size; i++)
            if (str[i] < 'A' || str[i] > 'Z')
                buf[i] = str[i];
            else
                buf[i] = str[i] - 'A' + 'a';
    }
    ret += len;
    return ret;
}

size_t hpack_encode_hdr_neverindex(uint8_t *restrict buf, size_t size,
                                   const char *name, const char *value)
{
    size_t ret = 1, val;

    if (size > 0)
    {
        *(buf++) = 0x10;
        size--;
    }

    val = hpack_encode_str_raw_lower(buf, size, name);
    if (size >= val)
    {
        buf += val;
        size -= val;
    }
    else
        size = 0;
    ret += val;

    val = hpack_encode_str_raw(buf, size, value);
    if (size >= val)
    {
        buf += val;
        size -= val;
    }
    else
        size = 0;
    ret += val;

    return ret;
}

size_t hpack_encode(uint8_t *restrict buf, size_t size,
                    const char *const headers[][2], unsigned count)
{
    size_t ret = 0;

    while (count > 0)
    {
        size_t val = hpack_encode_hdr_neverindex(buf, size, headers[0][0],
                                                 headers[0][1]);
        if (size >= val)
        {
            buf += val;
            size -= val;
        }
        else
            size = 0;

        ret += val;
        headers++;
        count--;
    }
    return ret;
}

/*** Test cases ***/
#ifdef ENC_TEST
# include <stdarg.h>
# include <stdlib.h>
# include <stdio.h>

static void test_integer(unsigned n, uintmax_t value)
{
    uint8_t buf[3];
    size_t ret;

    for (unsigned i = 0; i < 3; i++)
    {
        ret = hpack_encode_int(buf, sizeof (buf), value, n);
        assert(ret > 0);
    }
}

static void test_integers(void)
{
    test_integer(5, 10);
    test_integer(5, 1337);
    test_integer(8, 42);
}

static void test_lowercase(const char *str)
{
    char c;

    while ((c = *(str++)) != '\0')
        assert(c < 'A' || c > 'Z');
}

static void test_block(const char *name, ...)
{
    const char *headers[16][2];
    unsigned count = 0;
    va_list ap;

    va_start(ap, name);
    while (name != NULL)
    {
        headers[count][0] = name;
        headers[count][1] = va_arg(ap, const char *);
        name = va_arg(ap, const char *);
        count++;
    }
    va_end(ap);

    printf(" %u header(s):\n", count);

    uint8_t buf[1024];

    size_t length = hpack_encode(NULL, 0, headers, count);

    for (size_t i = 0; i < sizeof (buf); i++)
        assert(hpack_encode(buf, i, headers, count) == length);

    memset(buf, 0xAA, sizeof (buf));
    assert(hpack_encode(buf, length, headers, count) == length);

    char *eheaders[16][2];

    struct hpack_decoder *dec = hpack_decode_init(4096);
    assert(dec != NULL);

    int ecount = hpack_decode(dec, buf, length, eheaders, 16);
    assert((unsigned)ecount == count);

    hpack_decode_destroy(dec);

    for (unsigned i = 0; i < count; i++)
    {
        printf("  %s: %s\n", eheaders[i][0], eheaders[i][1]);
        test_lowercase(eheaders[i][0]);
        assert(!strcasecmp(eheaders[i][0], headers[i][0]));
        assert(!strcmp(eheaders[i][1], headers[i][1]));
        free(eheaders[i][1]);
        free(eheaders[i][0]);
    }
}

static void test_reqs(void)
{
    test_block(NULL);
    test_block(":method", "GET", ":scheme", "http", ":path", "/",
               ":authority", "www.example.com", NULL);
    test_block(":method", "GET", ":scheme", "http", ":path", "/",
               ":authority", "www.example.com", "Cache-Control", "no-cache",
               NULL);
    test_block(":method", "GET", ":scheme", "https", ":path", "/index.html",
               ":authority", "www.example.com", "custom-key", "custom-value",
               NULL);
}

static void test_resps(void)
{
    test_block(":status", "302", "Cache-Control", "private",
               "Date", "Mon, 21 Oct 2013 20:13:21 GMT",
               "Location", "https://www.example.com", NULL);
    test_block(":status", "307", "Cache-Control", "private",
               "Date", "Mon, 21 Oct 2013 20:13:21 GMT",
               "Location", "https://www.example.com", NULL);
    test_block(":status", "200", "Cache-Control", "private",
               "Date", "Mon, 21 Oct 2013 20:13:22 GMT",
               "Location", "https://www.example.com",
               "Content-Encoding", "gzip",
               "Set-Cookie", "foo=ASDJKHQKBZXOQWEOPIUAXQWEOIU; "
                   "max-age=3600; version=1",
               NULL);
}

int main(void)
{
    test_integers();
    test_reqs();
    test_resps();
}
#endif /* TEST */