2013-07-19 09:50:31 +02:00
|
|
|
/*
|
2014-03-30 12:09:21 +02:00
|
|
|
* nghttp2 - HTTP/2 C Library
|
2013-07-19 09:50:31 +02:00
|
|
|
*
|
|
|
|
* Copyright (c) 2013 Tatsuhiro Tsujikawa
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining
|
|
|
|
* a copy of this software and associated documentation files (the
|
|
|
|
* "Software"), to deal in the Software without restriction, including
|
|
|
|
* without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
* distribute, sublicense, and/or sell copies of the Software, and to
|
|
|
|
* permit persons to whom the Software is furnished to do so, subject to
|
|
|
|
* the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be
|
|
|
|
* included in all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
|
|
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
|
|
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
|
|
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
|
|
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
#include "nghttp2_hd.h"
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include <assert.h>
|
2013-12-16 15:54:20 +01:00
|
|
|
#include <stdio.h>
|
2013-07-19 09:50:31 +02:00
|
|
|
|
|
|
|
#include "nghttp2_helper.h"
|
2014-04-30 15:02:37 +02:00
|
|
|
#include "nghttp2_int.h"
|
2013-07-19 09:50:31 +02:00
|
|
|
|
2014-09-29 14:58:37 +02:00
|
|
|
#define STATIC_TABLE_LENGTH 61
|
|
|
|
|
2013-10-24 15:09:40 +02:00
|
|
|
/* Make scalar initialization form of nghttp2_nv */
|
2014-09-29 14:58:37 +02:00
|
|
|
#define MAKE_STATIC_ENT(I, N, V, NH, VH) \
|
|
|
|
{ { { (uint8_t*)N, (uint8_t*)V, sizeof(N) - 1, sizeof(V) - 1, 0 }, \
|
|
|
|
NH, VH, 1, NGHTTP2_HD_FLAG_NONE }, I }
|
|
|
|
|
2014-10-25 11:14:53 +02:00
|
|
|
/* Generated by mkstatictbl.py */
|
2014-09-29 14:58:37 +02:00
|
|
|
/* Sorted by hash(name) and its table index */
|
|
|
|
static nghttp2_hd_static_entry static_table[] = {
|
|
|
|
MAKE_STATIC_ENT(20, "age", "", 96511u, 0u),
|
|
|
|
MAKE_STATIC_ENT(59, "via", "", 116750u, 0u),
|
|
|
|
MAKE_STATIC_ENT(32, "date", "", 3076014u, 0u),
|
|
|
|
MAKE_STATIC_ENT(33, "etag", "", 3123477u, 0u),
|
|
|
|
MAKE_STATIC_ENT(36, "from", "", 3151786u, 0u),
|
|
|
|
MAKE_STATIC_ENT(37, "host", "", 3208616u, 0u),
|
|
|
|
MAKE_STATIC_ENT(44, "link", "", 3321850u, 0u),
|
|
|
|
MAKE_STATIC_ENT(58, "vary", "", 3612210u, 0u),
|
|
|
|
MAKE_STATIC_ENT(38, "if-match", "", 34533653u, 0u),
|
|
|
|
MAKE_STATIC_ENT(41, "if-range", "", 39145613u, 0u),
|
|
|
|
MAKE_STATIC_ENT(3, ":path", "/", 56997727u, 47u),
|
|
|
|
MAKE_STATIC_ENT(4, ":path", "/index.html", 56997727u, 2144181430u),
|
|
|
|
MAKE_STATIC_ENT(21, "allow", "", 92906313u, 0u),
|
|
|
|
MAKE_STATIC_ENT(49, "range", "", 108280125u, 0u),
|
|
|
|
MAKE_STATIC_ENT(14, "accept-charset", "", 124285319u, 0u),
|
|
|
|
MAKE_STATIC_ENT(43, "last-modified", "", 150043680u, 0u),
|
|
|
|
MAKE_STATIC_ENT(48, "proxy-authorization", "", 329532250u, 0u),
|
|
|
|
MAKE_STATIC_ENT(57, "user-agent", "", 486342275u, 0u),
|
|
|
|
MAKE_STATIC_ENT(40, "if-none-match", "", 646073760u, 0u),
|
|
|
|
MAKE_STATIC_ENT(30, "content-type", "", 785670158u, 0u),
|
|
|
|
MAKE_STATIC_ENT(16, "accept-language", "", 802785917u, 0u),
|
|
|
|
MAKE_STATIC_ENT(50, "referer", "", 1085069613u, 0u),
|
|
|
|
MAKE_STATIC_ENT(51, "refresh", "", 1085444827u, 0u),
|
|
|
|
MAKE_STATIC_ENT(55, "strict-transport-security", "", 1153852136u, 0u),
|
|
|
|
MAKE_STATIC_ENT(54, "set-cookie", "", 1237214767u, 0u),
|
|
|
|
MAKE_STATIC_ENT(56, "transfer-encoding", "", 1274458357u, 0u),
|
|
|
|
MAKE_STATIC_ENT(17, "accept-ranges", "", 1397189435u, 0u),
|
|
|
|
MAKE_STATIC_ENT(42, "if-unmodified-since", "", 1454068927u, 0u),
|
|
|
|
MAKE_STATIC_ENT(46, "max-forwards", "", 1619948695u, 0u),
|
|
|
|
MAKE_STATIC_ENT(45, "location", "", 1901043637u, 0u),
|
|
|
|
MAKE_STATIC_ENT(52, "retry-after", "", 1933352567u, 0u),
|
|
|
|
MAKE_STATIC_ENT(25, "content-encoding", "", 2095084583u, 0u),
|
|
|
|
MAKE_STATIC_ENT(28, "content-location", "", 2284906121u, 0u),
|
|
|
|
MAKE_STATIC_ENT(39, "if-modified-since", "", 2302095846u, 0u),
|
|
|
|
MAKE_STATIC_ENT(18, "accept", "", 2871506184u, 0u),
|
|
|
|
MAKE_STATIC_ENT(29, "content-range", "", 2878374633u, 0u),
|
|
|
|
MAKE_STATIC_ENT(22, "authorization", "", 2909397113u, 0u),
|
|
|
|
MAKE_STATIC_ENT(31, "cookie", "", 2940209764u, 0u),
|
|
|
|
MAKE_STATIC_ENT(0, ":authority", "", 2962729033u, 0u),
|
|
|
|
MAKE_STATIC_ENT(35, "expires", "", 2985731892u, 0u),
|
|
|
|
MAKE_STATIC_ENT(34, "expect", "", 3005803609u, 0u),
|
|
|
|
MAKE_STATIC_ENT(24, "content-disposition", "", 3027699811u, 0u),
|
|
|
|
MAKE_STATIC_ENT(26, "content-language", "", 3065240108u, 0u),
|
|
|
|
MAKE_STATIC_ENT(1, ":method", "GET", 3153018267u, 70454u),
|
|
|
|
MAKE_STATIC_ENT(2, ":method", "POST", 3153018267u, 2461856u),
|
|
|
|
MAKE_STATIC_ENT(27, "content-length", "", 3162187450u, 0u),
|
|
|
|
MAKE_STATIC_ENT(19, "access-control-allow-origin", "", 3297999203u, 0u),
|
|
|
|
MAKE_STATIC_ENT(5, ":scheme", "http", 3322585695u, 3213448u),
|
|
|
|
MAKE_STATIC_ENT(6, ":scheme", "https", 3322585695u, 99617003u),
|
|
|
|
MAKE_STATIC_ENT(7, ":status", "200", 3338091692u, 49586u),
|
|
|
|
MAKE_STATIC_ENT(8, ":status", "204", 3338091692u, 49590u),
|
|
|
|
MAKE_STATIC_ENT(9, ":status", "206", 3338091692u, 49592u),
|
|
|
|
MAKE_STATIC_ENT(10, ":status", "304", 3338091692u, 50551u),
|
|
|
|
MAKE_STATIC_ENT(11, ":status", "400", 3338091692u, 51508u),
|
|
|
|
MAKE_STATIC_ENT(12, ":status", "404", 3338091692u, 51512u),
|
|
|
|
MAKE_STATIC_ENT(13, ":status", "500", 3338091692u, 52469u),
|
|
|
|
MAKE_STATIC_ENT(53, "server", "", 3389140803u, 0u),
|
|
|
|
MAKE_STATIC_ENT(47, "proxy-authenticate", "", 3993199572u, 0u),
|
|
|
|
MAKE_STATIC_ENT(60, "www-authenticate", "", 4051929931u, 0u),
|
|
|
|
MAKE_STATIC_ENT(23, "cache-control", "", 4086191634u, 0u),
|
|
|
|
MAKE_STATIC_ENT(15, "accept-encoding", "gzip, deflate", 4127597688u, 1733326877u),
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Index to the position in static_table */
|
|
|
|
const size_t static_table_index[] = {
|
|
|
|
38, 43, 44, 10, 11, 47, 48, 49, 50, 51, 52, 53, 54, 55, 14, 60,
|
|
|
|
20, 26, 34, 46, 0 , 12, 36, 59, 41, 31, 42, 45, 32, 35, 19, 37,
|
|
|
|
2 , 3 , 40, 39, 4 , 5 , 8 , 33, 18, 9 , 27, 15, 6 , 29, 28, 57,
|
|
|
|
16, 13, 21, 22, 30, 56, 24, 23, 25, 17, 7 , 1 , 58
|
2013-07-19 09:50:31 +02:00
|
|
|
};
|
|
|
|
|
2014-07-16 16:04:00 +02:00
|
|
|
const size_t NGHTTP2_STATIC_TABLE_LENGTH =
|
2013-10-24 15:09:40 +02:00
|
|
|
sizeof(static_table)/sizeof(static_table[0]);
|
|
|
|
|
2014-01-07 13:51:48 +01:00
|
|
|
static int memeq(const void *s1, const void *s2, size_t n)
|
|
|
|
{
|
|
|
|
const uint8_t *a = (const uint8_t*)s1, *b = (const uint8_t*)s2;
|
|
|
|
uint8_t c = 0;
|
|
|
|
while(n > 0) {
|
|
|
|
c |= (*a++) ^ (*b++);
|
|
|
|
--n;
|
|
|
|
}
|
|
|
|
return c == 0;
|
|
|
|
}
|
|
|
|
|
2014-01-07 14:43:34 +01:00
|
|
|
static uint32_t hash(const uint8_t *s, size_t n)
|
|
|
|
{
|
|
|
|
uint32_t h = 0;
|
|
|
|
while(n > 0) {
|
|
|
|
h = h * 31 + *s++;
|
|
|
|
--n;
|
|
|
|
}
|
|
|
|
return h;
|
|
|
|
}
|
|
|
|
|
2013-10-12 14:49:01 +02:00
|
|
|
int nghttp2_hd_entry_init(nghttp2_hd_entry *ent, uint8_t flags,
|
2014-04-30 16:08:34 +02:00
|
|
|
uint8_t *name, size_t namelen,
|
2014-08-06 15:00:12 +02:00
|
|
|
uint8_t *value, size_t valuelen,
|
|
|
|
uint32_t name_hash, uint32_t value_hash)
|
2013-07-19 09:50:31 +02:00
|
|
|
{
|
|
|
|
int rv = 0;
|
2014-04-01 16:17:50 +02:00
|
|
|
|
|
|
|
/* Since nghttp2_hd_entry is used for indexing, ent->nv.flags always
|
|
|
|
NGHTTP2_NV_FLAG_NONE */
|
|
|
|
ent->nv.flags = NGHTTP2_NV_FLAG_NONE;
|
|
|
|
|
2013-10-14 16:38:12 +02:00
|
|
|
if((flags & NGHTTP2_HD_FLAG_NAME_ALLOC) &&
|
|
|
|
(flags & NGHTTP2_HD_FLAG_NAME_GIFT) == 0) {
|
2013-07-23 16:10:53 +02:00
|
|
|
if(namelen == 0) {
|
|
|
|
/* We should not allow empty header field name */
|
|
|
|
ent->nv.name = NULL;
|
|
|
|
} else {
|
|
|
|
ent->nv.name = nghttp2_memdup(name, namelen);
|
|
|
|
if(ent->nv.name == NULL) {
|
|
|
|
rv = NGHTTP2_ERR_NOMEM;
|
|
|
|
goto fail;
|
|
|
|
}
|
2013-07-19 09:50:31 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ent->nv.name = name;
|
|
|
|
}
|
2013-10-14 16:38:12 +02:00
|
|
|
if((flags & NGHTTP2_HD_FLAG_VALUE_ALLOC) &&
|
|
|
|
(flags & NGHTTP2_HD_FLAG_VALUE_GIFT) == 0) {
|
2013-07-23 16:10:53 +02:00
|
|
|
if(valuelen == 0) {
|
|
|
|
ent->nv.value = NULL;
|
|
|
|
} else {
|
|
|
|
ent->nv.value = nghttp2_memdup(value, valuelen);
|
|
|
|
if(ent->nv.value == NULL) {
|
|
|
|
rv = NGHTTP2_ERR_NOMEM;
|
|
|
|
goto fail2;
|
|
|
|
}
|
2013-07-19 09:50:31 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ent->nv.value = value;
|
|
|
|
}
|
|
|
|
ent->nv.namelen = namelen;
|
|
|
|
ent->nv.valuelen = valuelen;
|
|
|
|
ent->ref = 1;
|
|
|
|
ent->flags = flags;
|
2014-08-06 15:00:12 +02:00
|
|
|
|
|
|
|
ent->name_hash = name_hash;
|
|
|
|
ent->value_hash = value_hash;
|
|
|
|
|
2013-07-19 09:50:31 +02:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
fail2:
|
|
|
|
if(flags & NGHTTP2_HD_FLAG_NAME_ALLOC) {
|
|
|
|
free(ent->nv.name);
|
|
|
|
}
|
|
|
|
fail:
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
void nghttp2_hd_entry_free(nghttp2_hd_entry *ent)
|
|
|
|
{
|
|
|
|
assert(ent->ref == 0);
|
|
|
|
if(ent->flags & NGHTTP2_HD_FLAG_NAME_ALLOC) {
|
|
|
|
free(ent->nv.name);
|
|
|
|
}
|
|
|
|
if(ent->flags & NGHTTP2_HD_FLAG_VALUE_ALLOC) {
|
|
|
|
free(ent->nv.value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-08 16:54:07 +02:00
|
|
|
static int hd_ringbuf_init(nghttp2_hd_ringbuf *ringbuf, size_t bufsize)
|
2013-10-12 14:49:01 +02:00
|
|
|
{
|
|
|
|
size_t size;
|
|
|
|
for(size = 1; size < bufsize; size <<= 1);
|
|
|
|
ringbuf->buffer = malloc(sizeof(nghttp2_hd_entry*)*size);
|
|
|
|
if(ringbuf->buffer == NULL) {
|
|
|
|
return NGHTTP2_ERR_NOMEM;
|
|
|
|
}
|
|
|
|
ringbuf->mask = size - 1;
|
|
|
|
ringbuf->first = 0;
|
|
|
|
ringbuf->len = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-05-08 16:54:07 +02:00
|
|
|
static nghttp2_hd_entry* hd_ringbuf_get(nghttp2_hd_ringbuf *ringbuf,
|
2014-05-27 22:28:12 +02:00
|
|
|
size_t idx)
|
2013-10-12 14:49:01 +02:00
|
|
|
{
|
2014-05-27 22:28:12 +02:00
|
|
|
assert(idx < ringbuf->len);
|
|
|
|
return ringbuf->buffer[(ringbuf->first + idx) & ringbuf->mask];
|
2013-10-12 14:49:01 +02:00
|
|
|
}
|
|
|
|
|
2014-05-08 16:54:07 +02:00
|
|
|
static int hd_ringbuf_reserve(nghttp2_hd_ringbuf *ringbuf, size_t bufsize)
|
2013-10-25 18:01:28 +02:00
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
size_t size;
|
|
|
|
nghttp2_hd_entry **buffer;
|
|
|
|
|
|
|
|
if(ringbuf->mask + 1 >= bufsize) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
for(size = 1; size < bufsize; size <<= 1);
|
|
|
|
buffer = malloc(sizeof(nghttp2_hd_entry*) * size);
|
|
|
|
if(buffer == NULL) {
|
|
|
|
return NGHTTP2_ERR_NOMEM;
|
|
|
|
}
|
|
|
|
for(i = 0; i < ringbuf->len; ++i) {
|
2014-05-08 16:54:07 +02:00
|
|
|
buffer[i] = hd_ringbuf_get(ringbuf, i);
|
2013-10-25 18:01:28 +02:00
|
|
|
}
|
|
|
|
free(ringbuf->buffer);
|
|
|
|
ringbuf->buffer = buffer;
|
|
|
|
ringbuf->mask = size - 1;
|
|
|
|
ringbuf->first = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-05-08 16:54:07 +02:00
|
|
|
static void hd_ringbuf_free(nghttp2_hd_ringbuf *ringbuf)
|
2013-10-12 14:49:01 +02:00
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
if(ringbuf == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
for(i = 0; i < ringbuf->len; ++i) {
|
2014-05-08 16:54:07 +02:00
|
|
|
nghttp2_hd_entry *ent = hd_ringbuf_get(ringbuf, i);
|
2013-10-12 14:49:01 +02:00
|
|
|
--ent->ref;
|
|
|
|
nghttp2_hd_entry_free(ent);
|
|
|
|
free(ent);
|
|
|
|
}
|
|
|
|
free(ringbuf->buffer);
|
|
|
|
}
|
|
|
|
|
2014-06-22 06:39:17 +02:00
|
|
|
static int hd_ringbuf_push_front(nghttp2_hd_ringbuf *ringbuf,
|
|
|
|
nghttp2_hd_entry *ent)
|
2013-10-12 14:49:01 +02:00
|
|
|
{
|
2014-06-22 06:39:17 +02:00
|
|
|
int rv;
|
|
|
|
|
|
|
|
rv = hd_ringbuf_reserve(ringbuf, ringbuf->len + 1);
|
|
|
|
|
|
|
|
if(rv != 0) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2013-10-12 14:49:01 +02:00
|
|
|
ringbuf->buffer[--ringbuf->first & ringbuf->mask] = ent;
|
|
|
|
++ringbuf->len;
|
2014-06-22 06:39:17 +02:00
|
|
|
|
2013-10-12 14:49:01 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-05-08 16:54:07 +02:00
|
|
|
static void hd_ringbuf_pop_back(nghttp2_hd_ringbuf *ringbuf)
|
2013-10-12 14:49:01 +02:00
|
|
|
{
|
|
|
|
assert(ringbuf->len > 0);
|
|
|
|
--ringbuf->len;
|
|
|
|
}
|
|
|
|
|
2014-07-21 19:03:18 +02:00
|
|
|
static int hd_context_init(nghttp2_hd_context *context)
|
2013-07-19 09:50:31 +02:00
|
|
|
{
|
2013-10-12 14:49:01 +02:00
|
|
|
int rv;
|
2013-07-25 18:34:28 +02:00
|
|
|
context->bad = 0;
|
2013-10-25 18:01:28 +02:00
|
|
|
context->hd_table_bufsize_max = NGHTTP2_HD_DEFAULT_MAX_BUFFER_SIZE;
|
2014-05-08 16:54:07 +02:00
|
|
|
rv = hd_ringbuf_init
|
2013-10-25 18:01:28 +02:00
|
|
|
(&context->hd_table,
|
|
|
|
context->hd_table_bufsize_max/NGHTTP2_HD_ENTRY_OVERHEAD);
|
2013-10-12 14:49:01 +02:00
|
|
|
if(rv != 0) {
|
|
|
|
return rv;
|
2013-08-22 18:28:01 +02:00
|
|
|
}
|
2013-07-19 09:50:31 +02:00
|
|
|
|
2013-07-19 18:27:26 +02:00
|
|
|
context->hd_table_bufsize = 0;
|
2013-07-19 09:50:31 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-05-08 16:54:07 +02:00
|
|
|
static void hd_context_free(nghttp2_hd_context *context)
|
2014-04-30 04:32:05 +02:00
|
|
|
{
|
2014-05-08 16:54:07 +02:00
|
|
|
hd_ringbuf_free(&context->hd_table);
|
2014-04-30 04:32:05 +02:00
|
|
|
}
|
|
|
|
|
2014-02-13 15:22:52 +01:00
|
|
|
int nghttp2_hd_deflate_init(nghttp2_hd_deflater *deflater)
|
2013-07-19 09:50:31 +02:00
|
|
|
{
|
2014-02-13 15:22:52 +01:00
|
|
|
return nghttp2_hd_deflate_init2(deflater,
|
2014-01-16 15:41:13 +01:00
|
|
|
NGHTTP2_HD_DEFAULT_MAX_DEFLATE_BUFFER_SIZE);
|
2013-10-13 12:24:21 +02:00
|
|
|
}
|
|
|
|
|
2014-01-26 09:53:04 +01:00
|
|
|
int nghttp2_hd_deflate_init2(nghttp2_hd_deflater *deflater,
|
2013-12-14 10:48:41 +01:00
|
|
|
size_t deflate_hd_table_bufsize_max)
|
2013-10-13 12:24:21 +02:00
|
|
|
{
|
2014-01-26 09:53:04 +01:00
|
|
|
int rv;
|
2014-07-21 19:03:18 +02:00
|
|
|
rv = hd_context_init(&deflater->ctx);
|
2014-01-26 09:53:04 +01:00
|
|
|
if(rv != 0) {
|
|
|
|
return rv;
|
|
|
|
}
|
2014-05-11 14:25:27 +02:00
|
|
|
|
|
|
|
if(deflate_hd_table_bufsize_max < NGHTTP2_HD_DEFAULT_MAX_BUFFER_SIZE) {
|
|
|
|
deflater->notify_table_size_change = 1;
|
|
|
|
deflater->ctx.hd_table_bufsize_max = deflate_hd_table_bufsize_max;
|
|
|
|
} else {
|
|
|
|
deflater->notify_table_size_change = 0;
|
|
|
|
}
|
|
|
|
|
2014-02-13 15:22:52 +01:00
|
|
|
deflater->deflate_hd_table_bufsize_max = deflate_hd_table_bufsize_max;
|
2014-07-25 15:55:45 +02:00
|
|
|
deflater->min_hd_table_bufsize_max = UINT32_MAX;
|
2014-05-11 14:25:27 +02:00
|
|
|
|
2014-01-26 09:53:04 +01:00
|
|
|
return 0;
|
2013-07-19 09:50:31 +02:00
|
|
|
}
|
|
|
|
|
2014-02-13 15:22:52 +01:00
|
|
|
int nghttp2_hd_inflate_init(nghttp2_hd_inflater *inflater)
|
2013-08-22 20:45:26 +02:00
|
|
|
{
|
2014-01-25 10:24:15 +01:00
|
|
|
int rv;
|
2014-02-13 15:22:52 +01:00
|
|
|
|
2014-07-21 19:03:18 +02:00
|
|
|
rv = hd_context_init(&inflater->ctx);
|
2014-01-25 10:24:15 +01:00
|
|
|
if(rv != 0) {
|
2014-04-30 04:32:05 +02:00
|
|
|
goto fail;
|
2014-01-25 10:24:15 +01:00
|
|
|
}
|
2014-02-13 15:22:52 +01:00
|
|
|
|
|
|
|
inflater->settings_hd_table_bufsize_max =
|
|
|
|
NGHTTP2_HD_DEFAULT_MAX_BUFFER_SIZE;
|
|
|
|
|
2014-01-26 09:53:04 +01:00
|
|
|
inflater->ent_keep = NULL;
|
2014-05-28 16:02:23 +02:00
|
|
|
inflater->nv_keep = NULL;
|
2014-01-26 09:53:04 +01:00
|
|
|
|
2014-01-25 10:24:15 +01:00
|
|
|
inflater->opcode = NGHTTP2_HD_OPCODE_NONE;
|
|
|
|
inflater->state = NGHTTP2_HD_STATE_OPCODE;
|
2014-04-30 04:32:05 +02:00
|
|
|
|
2014-06-28 04:04:41 +02:00
|
|
|
rv = nghttp2_bufs_init3(&inflater->nvbufs, NGHTTP2_HD_MAX_NV / 8, 8, 1, 0);
|
2014-04-30 04:32:05 +02:00
|
|
|
|
|
|
|
if(rv != 0) {
|
2014-05-28 16:02:23 +02:00
|
|
|
goto nvbufs_fail;
|
2014-04-30 04:32:05 +02:00
|
|
|
}
|
|
|
|
|
2014-01-25 10:24:15 +01:00
|
|
|
inflater->huffman_encoded = 0;
|
|
|
|
inflater->index = 0;
|
|
|
|
inflater->left = 0;
|
2014-07-21 18:50:29 +02:00
|
|
|
inflater->shift = 0;
|
2014-05-28 16:02:23 +02:00
|
|
|
inflater->newnamelen = 0;
|
2014-01-25 10:24:15 +01:00
|
|
|
inflater->index_required = 0;
|
2014-04-01 18:25:44 +02:00
|
|
|
inflater->no_index = 0;
|
2014-04-30 04:32:05 +02:00
|
|
|
|
2014-01-25 10:24:15 +01:00
|
|
|
return 0;
|
2014-04-30 04:32:05 +02:00
|
|
|
|
2014-05-28 16:02:23 +02:00
|
|
|
nvbufs_fail:
|
2014-05-08 16:54:07 +02:00
|
|
|
hd_context_free(&inflater->ctx);
|
2014-04-30 04:32:05 +02:00
|
|
|
fail:
|
|
|
|
return rv;
|
2013-08-22 20:45:26 +02:00
|
|
|
}
|
|
|
|
|
2014-01-26 09:53:04 +01:00
|
|
|
static void hd_inflate_keep_free(nghttp2_hd_inflater *inflater)
|
2013-07-19 09:50:31 +02:00
|
|
|
{
|
2014-01-16 15:41:13 +01:00
|
|
|
if(inflater->ent_keep) {
|
|
|
|
if(inflater->ent_keep->ref == 0) {
|
|
|
|
nghttp2_hd_entry_free(inflater->ent_keep);
|
|
|
|
free(inflater->ent_keep);
|
2013-07-19 09:50:31 +02:00
|
|
|
}
|
2014-01-16 15:41:13 +01:00
|
|
|
inflater->ent_keep = NULL;
|
2013-07-19 09:50:31 +02:00
|
|
|
}
|
2014-05-28 16:02:23 +02:00
|
|
|
|
|
|
|
free(inflater->nv_keep);
|
|
|
|
inflater->nv_keep = NULL;
|
2014-01-16 15:41:13 +01:00
|
|
|
}
|
2013-10-14 16:38:12 +02:00
|
|
|
|
2014-01-26 09:53:04 +01:00
|
|
|
void nghttp2_hd_deflate_free(nghttp2_hd_deflater *deflater)
|
2013-07-19 09:50:31 +02:00
|
|
|
{
|
2014-05-08 16:54:07 +02:00
|
|
|
hd_context_free(&deflater->ctx);
|
2013-07-19 09:50:31 +02:00
|
|
|
}
|
|
|
|
|
2014-01-26 09:53:04 +01:00
|
|
|
void nghttp2_hd_inflate_free(nghttp2_hd_inflater *inflater)
|
2013-07-19 09:50:31 +02:00
|
|
|
{
|
2014-01-16 15:41:13 +01:00
|
|
|
hd_inflate_keep_free(inflater);
|
2014-05-28 16:02:23 +02:00
|
|
|
nghttp2_bufs_free(&inflater->nvbufs);
|
2014-05-08 16:54:07 +02:00
|
|
|
hd_context_free(&inflater->ctx);
|
2013-07-19 09:50:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static size_t entry_room(size_t namelen, size_t valuelen)
|
|
|
|
{
|
|
|
|
return NGHTTP2_HD_ENTRY_OVERHEAD + namelen + valuelen;
|
|
|
|
}
|
|
|
|
|
2014-01-26 09:53:04 +01:00
|
|
|
static int emit_indexed_header(nghttp2_nv *nv_out, nghttp2_hd_entry *ent)
|
2013-08-23 16:38:28 +02:00
|
|
|
{
|
2014-03-19 16:27:39 +01:00
|
|
|
DEBUGF(fprintf(stderr, "inflatehd: header emission: "));
|
2013-12-16 15:54:20 +01:00
|
|
|
DEBUGF(fwrite(ent->nv.name, ent->nv.namelen, 1, stderr));
|
|
|
|
DEBUGF(fprintf(stderr, ": "));
|
|
|
|
DEBUGF(fwrite(ent->nv.value, ent->nv.valuelen, 1, stderr));
|
|
|
|
DEBUGF(fprintf(stderr, "\n"));
|
2014-07-12 11:47:55 +02:00
|
|
|
/* ent->ref may be 0. This happens if the encoder emits literal
|
|
|
|
block larger than header table capacity with indexing. */
|
2014-01-16 15:41:13 +01:00
|
|
|
*nv_out = ent->nv;
|
|
|
|
return 0;
|
2013-08-23 16:38:28 +02:00
|
|
|
}
|
|
|
|
|
2014-03-11 13:18:28 +01:00
|
|
|
static int emit_literal_header(nghttp2_nv *nv_out, nghttp2_nv *nv)
|
2013-08-23 16:38:28 +02:00
|
|
|
{
|
2014-03-19 16:27:39 +01:00
|
|
|
DEBUGF(fprintf(stderr, "inflatehd: header emission: "));
|
2013-12-16 15:54:20 +01:00
|
|
|
DEBUGF(fwrite(nv->name, nv->namelen, 1, stderr));
|
|
|
|
DEBUGF(fprintf(stderr, ": "));
|
|
|
|
DEBUGF(fwrite(nv->value, nv->valuelen, 1, stderr));
|
|
|
|
DEBUGF(fprintf(stderr, "\n"));
|
2014-01-16 15:41:13 +01:00
|
|
|
*nv_out = *nv;
|
2013-10-24 16:49:37 +02:00
|
|
|
return 0;
|
2013-08-23 16:38:28 +02:00
|
|
|
}
|
|
|
|
|
2014-06-11 16:37:16 +02:00
|
|
|
static size_t count_encoded_length(size_t n, size_t prefix)
|
2013-07-19 09:50:31 +02:00
|
|
|
{
|
2013-10-18 12:27:15 +02:00
|
|
|
size_t k = (1 << prefix) - 1;
|
2013-07-19 09:50:31 +02:00
|
|
|
size_t len = 0;
|
2014-11-20 17:59:20 +01:00
|
|
|
|
|
|
|
if(n < k) {
|
2013-10-18 12:27:15 +02:00
|
|
|
return 1;
|
2013-07-19 09:50:31 +02:00
|
|
|
}
|
2014-11-20 17:59:20 +01:00
|
|
|
|
|
|
|
n -= k;
|
|
|
|
++len;
|
|
|
|
|
|
|
|
for(; n >= 128; n >>= 7, ++len);
|
|
|
|
|
|
|
|
return len + 1;
|
2013-07-19 09:50:31 +02:00
|
|
|
}
|
|
|
|
|
2014-06-11 16:37:16 +02:00
|
|
|
static size_t encode_length(uint8_t *buf, size_t n, size_t prefix)
|
2013-07-19 09:50:31 +02:00
|
|
|
{
|
2013-10-18 12:27:15 +02:00
|
|
|
size_t k = (1 << prefix) - 1;
|
2014-11-20 17:59:20 +01:00
|
|
|
uint8_t *begin = buf;
|
2014-07-26 11:19:25 +02:00
|
|
|
|
2013-10-20 09:28:52 +02:00
|
|
|
*buf &= ~k;
|
2014-07-26 11:19:25 +02:00
|
|
|
|
|
|
|
if(n < k) {
|
2014-11-20 17:59:20 +01:00
|
|
|
*buf |= n;
|
2013-10-18 12:27:15 +02:00
|
|
|
return 1;
|
2013-07-19 09:50:31 +02:00
|
|
|
}
|
2014-07-26 11:19:25 +02:00
|
|
|
|
|
|
|
*buf++ |= k;
|
|
|
|
n -= k;
|
|
|
|
|
2014-11-20 17:59:20 +01:00
|
|
|
for(; n >= 128; n >>= 7) {
|
|
|
|
*buf++ = (1 << 7) | (n & 0x7f);
|
|
|
|
}
|
|
|
|
|
|
|
|
*buf++ = (uint8_t)n;
|
|
|
|
|
|
|
|
return (size_t)(buf - begin);
|
2013-07-19 09:50:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2014-08-03 07:07:35 +02:00
|
|
|
* Decodes |prefix| prefixed integer stored from |in|. The |last|
|
2013-07-19 09:50:31 +02:00
|
|
|
* represents the 1 beyond the last of the valid contiguous memory
|
2014-07-22 15:38:18 +02:00
|
|
|
* region from |in|. The decoded integer must be less than or equal
|
|
|
|
* to UINT32_MAX.
|
2013-07-19 09:50:31 +02:00
|
|
|
*
|
2014-01-25 10:24:15 +01:00
|
|
|
* If the |initial| is nonzero, it is used as a initial value, this
|
|
|
|
* function assumes the |in| starts with intermediate data.
|
|
|
|
*
|
|
|
|
* An entire integer is decoded successfully, decoded, the |*final| is
|
|
|
|
* set to nonzero.
|
|
|
|
*
|
2014-07-22 15:38:18 +02:00
|
|
|
* This function stores the decoded integer in |*res| if it succeed,
|
|
|
|
* including partial decoding (in this case, number of shift to make
|
|
|
|
* in the next call will be stored in |*shift_ptr|) and returns number
|
|
|
|
* of bytes processed, or returns -1, indicating decoding error.
|
2013-07-19 09:50:31 +02:00
|
|
|
*/
|
2014-07-22 15:38:18 +02:00
|
|
|
static ssize_t decode_length(uint32_t *res, size_t *shift_ptr, int *final,
|
|
|
|
uint32_t initial, size_t shift,
|
|
|
|
uint8_t *in, uint8_t *last, size_t prefix)
|
2013-07-19 09:50:31 +02:00
|
|
|
{
|
2014-07-22 15:38:18 +02:00
|
|
|
uint32_t k = (1 << prefix) - 1;
|
|
|
|
uint32_t n = initial;
|
|
|
|
uint8_t *start = in;
|
2014-07-21 18:50:29 +02:00
|
|
|
|
|
|
|
*shift_ptr = 0;
|
2014-01-25 10:24:15 +01:00
|
|
|
*final = 0;
|
2014-07-21 18:50:29 +02:00
|
|
|
|
2014-01-25 10:24:15 +01:00
|
|
|
if(n == 0) {
|
2014-07-22 15:38:18 +02:00
|
|
|
if((*in & k) != k) {
|
2014-01-25 10:24:15 +01:00
|
|
|
*res = (*in) & k;
|
|
|
|
*final = 1;
|
2014-07-22 15:38:18 +02:00
|
|
|
return 1;
|
2014-01-25 10:24:15 +01:00
|
|
|
}
|
2014-07-22 15:38:18 +02:00
|
|
|
|
|
|
|
n = k;
|
|
|
|
|
2014-02-26 13:44:48 +01:00
|
|
|
if(++in == last) {
|
|
|
|
*res = n;
|
2014-08-04 09:04:29 +02:00
|
|
|
return (ssize_t)(in - start);
|
2014-02-26 13:44:48 +01:00
|
|
|
}
|
2013-07-19 09:50:31 +02:00
|
|
|
}
|
2014-07-22 15:38:18 +02:00
|
|
|
|
2014-07-21 18:50:29 +02:00
|
|
|
for(; in != last; ++in, shift += 7) {
|
2014-07-22 15:38:18 +02:00
|
|
|
uint32_t add = *in & 0x7f;
|
|
|
|
|
|
|
|
if((UINT32_MAX >> shift) < add) {
|
|
|
|
DEBUGF(fprintf(stderr, "inflate: integer overflow on shift\n"));
|
|
|
|
return -1;
|
2013-07-19 09:50:31 +02:00
|
|
|
}
|
2014-07-22 15:38:18 +02:00
|
|
|
|
|
|
|
add <<= shift;
|
|
|
|
|
|
|
|
if(UINT32_MAX - add < n) {
|
|
|
|
DEBUGF(fprintf(stderr, "inflate: integer overflow on addition\n"));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
n += add;
|
|
|
|
|
2013-07-19 09:50:31 +02:00
|
|
|
if((*in & (1 << 7)) == 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2014-07-21 18:50:29 +02:00
|
|
|
|
|
|
|
*shift_ptr = shift;
|
|
|
|
|
2014-01-25 10:24:15 +01:00
|
|
|
if(in == last) {
|
|
|
|
*res = n;
|
2014-08-04 09:04:29 +02:00
|
|
|
return (ssize_t)(in - start);
|
2014-01-25 10:24:15 +01:00
|
|
|
}
|
2014-07-21 18:52:51 +02:00
|
|
|
|
2014-01-25 10:24:15 +01:00
|
|
|
*res = n;
|
|
|
|
*final = 1;
|
2014-08-04 09:04:29 +02:00
|
|
|
return (ssize_t)(in + 1 - start);
|
2013-07-19 09:50:31 +02:00
|
|
|
}
|
|
|
|
|
2014-03-13 14:11:02 +01:00
|
|
|
static int emit_table_size(nghttp2_bufs *bufs, size_t table_size)
|
2014-02-13 15:22:52 +01:00
|
|
|
{
|
|
|
|
int rv;
|
|
|
|
uint8_t *bufp;
|
2014-03-10 17:47:38 +01:00
|
|
|
size_t blocklen;
|
2014-03-13 14:11:02 +01:00
|
|
|
uint8_t sb[16];
|
2014-03-10 17:47:38 +01:00
|
|
|
|
2014-03-19 16:27:39 +01:00
|
|
|
DEBUGF(fprintf(stderr, "deflatehd: emit table_size=%zu\n", table_size));
|
|
|
|
|
2014-07-12 11:47:55 +02:00
|
|
|
blocklen = count_encoded_length(table_size, 5);
|
2014-03-10 17:47:38 +01:00
|
|
|
|
2014-03-13 14:11:02 +01:00
|
|
|
if(sizeof(sb) < blocklen) {
|
|
|
|
return NGHTTP2_ERR_HEADER_COMP;
|
2014-02-13 15:22:52 +01:00
|
|
|
}
|
2014-03-10 17:47:38 +01:00
|
|
|
|
2014-03-13 14:11:02 +01:00
|
|
|
bufp = sb;
|
2014-03-10 17:47:38 +01:00
|
|
|
|
2014-04-01 18:25:44 +02:00
|
|
|
*bufp = 0x20u;
|
|
|
|
|
2014-07-12 11:47:55 +02:00
|
|
|
encode_length(bufp, table_size, 5);
|
2014-03-10 17:47:38 +01:00
|
|
|
|
2014-03-13 14:11:02 +01:00
|
|
|
rv = nghttp2_bufs_add(bufs, sb, blocklen);
|
|
|
|
if(rv != 0) {
|
2014-05-12 14:48:00 +02:00
|
|
|
return rv;
|
2014-03-13 14:11:02 +01:00
|
|
|
}
|
2014-03-10 17:47:38 +01:00
|
|
|
|
2013-11-16 09:05:18 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-05-27 22:28:12 +02:00
|
|
|
static int emit_indexed_block(nghttp2_bufs *bufs, size_t idx)
|
2013-07-19 09:50:31 +02:00
|
|
|
{
|
|
|
|
int rv;
|
2014-03-10 17:47:38 +01:00
|
|
|
size_t blocklen;
|
2014-03-13 14:11:02 +01:00
|
|
|
uint8_t sb[16];
|
|
|
|
uint8_t *bufp;
|
2014-03-10 17:47:38 +01:00
|
|
|
|
2014-05-27 22:28:12 +02:00
|
|
|
blocklen = count_encoded_length(idx + 1, 7);
|
2014-03-10 17:47:38 +01:00
|
|
|
|
2014-03-19 16:27:39 +01:00
|
|
|
DEBUGF(fprintf(stderr, "deflatehd: emit indexed index=%zu, %zu bytes\n",
|
2014-05-27 22:28:12 +02:00
|
|
|
idx, blocklen));
|
2014-03-19 16:27:39 +01:00
|
|
|
|
2014-03-13 14:11:02 +01:00
|
|
|
if(sizeof(sb) < blocklen) {
|
|
|
|
return NGHTTP2_ERR_HEADER_COMP;
|
2013-07-19 09:50:31 +02:00
|
|
|
}
|
2014-03-10 17:47:38 +01:00
|
|
|
|
2014-03-13 14:11:02 +01:00
|
|
|
bufp = sb;
|
|
|
|
*bufp = 0x80u;
|
2014-05-27 22:28:12 +02:00
|
|
|
encode_length(bufp, idx + 1, 7);
|
2014-03-10 17:47:38 +01:00
|
|
|
|
2014-03-13 14:11:02 +01:00
|
|
|
rv = nghttp2_bufs_add(bufs, sb, blocklen);
|
|
|
|
if(rv != 0) {
|
2014-05-12 14:48:00 +02:00
|
|
|
return rv;
|
2014-03-13 14:11:02 +01:00
|
|
|
}
|
2014-03-10 17:47:38 +01:00
|
|
|
|
2013-07-19 09:50:31 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-07-26 11:19:25 +02:00
|
|
|
static int emit_string(nghttp2_bufs *bufs, const uint8_t *str, size_t len)
|
2013-10-20 17:22:14 +02:00
|
|
|
{
|
2014-06-11 16:37:16 +02:00
|
|
|
int rv;
|
2014-03-13 14:11:02 +01:00
|
|
|
uint8_t sb[16];
|
|
|
|
uint8_t *bufp;
|
|
|
|
size_t blocklen;
|
2014-07-26 11:19:25 +02:00
|
|
|
size_t enclen;
|
|
|
|
int huffman = 0;
|
|
|
|
|
|
|
|
enclen = nghttp2_hd_huff_encode_count(str, len);
|
|
|
|
|
|
|
|
if(enclen < len) {
|
|
|
|
huffman = 1;
|
|
|
|
} else {
|
|
|
|
enclen = len;
|
|
|
|
}
|
2014-03-13 14:11:02 +01:00
|
|
|
|
|
|
|
blocklen = count_encoded_length(enclen, 7);
|
|
|
|
|
2014-03-19 16:27:39 +01:00
|
|
|
DEBUGF(fprintf(stderr,
|
|
|
|
"deflatehd: emit string str="));
|
|
|
|
DEBUGF(fwrite(str, len, 1, stderr));
|
|
|
|
DEBUGF(fprintf(stderr, ", length=%zu, huffman=%d, encoded_length=%zu\n",
|
|
|
|
len, huffman, enclen));
|
|
|
|
|
2014-03-13 14:11:02 +01:00
|
|
|
if(sizeof(sb) < blocklen) {
|
|
|
|
return NGHTTP2_ERR_HEADER_COMP;
|
|
|
|
}
|
|
|
|
|
|
|
|
bufp = sb;
|
|
|
|
*bufp = huffman ? 1 << 7 : 0;
|
2014-04-30 08:54:02 +02:00
|
|
|
encode_length(bufp, enclen, 7);
|
2014-03-13 14:11:02 +01:00
|
|
|
|
|
|
|
rv = nghttp2_bufs_add(bufs, sb, blocklen);
|
|
|
|
if(rv != 0) {
|
2014-05-12 14:48:00 +02:00
|
|
|
return rv;
|
2014-03-13 14:11:02 +01:00
|
|
|
}
|
2014-03-10 17:47:38 +01:00
|
|
|
|
2013-10-20 17:22:14 +02:00
|
|
|
if(huffman) {
|
2014-03-13 14:11:02 +01:00
|
|
|
rv = nghttp2_hd_huff_encode(bufs, str, len);
|
2013-10-20 17:22:14 +02:00
|
|
|
} else {
|
|
|
|
assert(enclen == len);
|
2014-03-13 14:11:02 +01:00
|
|
|
rv = nghttp2_bufs_add(bufs, str, len);
|
2013-10-20 17:22:14 +02:00
|
|
|
}
|
2014-03-13 14:11:02 +01:00
|
|
|
|
2014-05-12 14:48:00 +02:00
|
|
|
return rv;
|
2013-10-20 17:22:14 +02:00
|
|
|
}
|
|
|
|
|
2014-04-01 18:25:44 +02:00
|
|
|
static uint8_t pack_first_byte(int inc_indexing, int no_index)
|
|
|
|
{
|
|
|
|
if(inc_indexing) {
|
|
|
|
return 0x40u;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(no_index) {
|
|
|
|
return 0x10u;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-05-27 22:28:12 +02:00
|
|
|
static int emit_indname_block(nghttp2_bufs *bufs, size_t idx,
|
2014-06-29 16:43:14 +02:00
|
|
|
const nghttp2_nv *nv,
|
2014-02-13 15:22:52 +01:00
|
|
|
int inc_indexing)
|
2013-07-19 09:50:31 +02:00
|
|
|
{
|
|
|
|
int rv;
|
|
|
|
uint8_t *bufp;
|
2014-03-10 17:47:38 +01:00
|
|
|
size_t blocklen;
|
2014-03-13 14:11:02 +01:00
|
|
|
uint8_t sb[16];
|
2014-04-01 18:25:44 +02:00
|
|
|
size_t prefixlen;
|
|
|
|
int no_index;
|
|
|
|
|
|
|
|
no_index = (nv->flags & NGHTTP2_NV_FLAG_NO_INDEX) != 0;
|
|
|
|
|
|
|
|
if(inc_indexing) {
|
|
|
|
prefixlen = 6;
|
|
|
|
} else {
|
|
|
|
prefixlen = 4;
|
|
|
|
}
|
2014-03-10 17:47:38 +01:00
|
|
|
|
2014-03-19 16:27:39 +01:00
|
|
|
DEBUGF(fprintf(stderr,
|
2014-04-30 16:08:34 +02:00
|
|
|
"deflatehd: emit indname index=%zu, valuelen=%zu, "
|
2014-04-01 18:25:44 +02:00
|
|
|
"indexing=%d, no_index=%d\n",
|
2014-05-27 22:28:12 +02:00
|
|
|
idx, nv->valuelen, inc_indexing, no_index));
|
2014-03-19 16:27:39 +01:00
|
|
|
|
2014-05-27 22:28:12 +02:00
|
|
|
blocklen = count_encoded_length(idx + 1, prefixlen);
|
2014-03-10 17:47:38 +01:00
|
|
|
|
2014-03-13 14:11:02 +01:00
|
|
|
if(sizeof(sb) < blocklen) {
|
|
|
|
return NGHTTP2_ERR_HEADER_COMP;
|
2013-07-19 09:50:31 +02:00
|
|
|
}
|
2014-03-10 17:47:38 +01:00
|
|
|
|
2014-03-13 14:11:02 +01:00
|
|
|
bufp = sb;
|
2014-03-10 17:47:38 +01:00
|
|
|
|
2014-04-01 18:25:44 +02:00
|
|
|
*bufp = pack_first_byte(inc_indexing, no_index);
|
|
|
|
|
2014-05-27 22:28:12 +02:00
|
|
|
encode_length(bufp, idx + 1, prefixlen);
|
2014-03-10 17:47:38 +01:00
|
|
|
|
2014-03-13 14:11:02 +01:00
|
|
|
rv = nghttp2_bufs_add(bufs, sb, blocklen);
|
|
|
|
if(rv != 0) {
|
2014-05-12 14:48:00 +02:00
|
|
|
return rv;
|
2014-03-13 14:11:02 +01:00
|
|
|
}
|
2014-03-10 17:47:38 +01:00
|
|
|
|
2014-07-26 11:19:25 +02:00
|
|
|
rv = emit_string(bufs, nv->value, nv->valuelen);
|
2014-03-13 14:11:02 +01:00
|
|
|
if(rv != 0) {
|
|
|
|
return rv;
|
|
|
|
}
|
2014-03-10 17:47:38 +01:00
|
|
|
|
2013-07-19 09:50:31 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-06-29 16:43:14 +02:00
|
|
|
static int emit_newname_block(nghttp2_bufs *bufs, const nghttp2_nv *nv,
|
2014-02-13 15:22:52 +01:00
|
|
|
int inc_indexing)
|
2013-07-19 09:50:31 +02:00
|
|
|
{
|
|
|
|
int rv;
|
2014-04-01 18:25:44 +02:00
|
|
|
int no_index;
|
|
|
|
|
|
|
|
no_index = (nv->flags & NGHTTP2_NV_FLAG_NO_INDEX) != 0;
|
2014-03-10 17:47:38 +01:00
|
|
|
|
2014-03-19 16:27:39 +01:00
|
|
|
DEBUGF(fprintf(stderr,
|
2014-04-30 16:08:34 +02:00
|
|
|
"deflatehd: emit newname namelen=%zu, valuelen=%zu, "
|
2014-04-01 18:25:44 +02:00
|
|
|
"indexing=%d, no_index=%d\n",
|
|
|
|
nv->namelen, nv->valuelen, inc_indexing, no_index));
|
2014-03-19 16:27:39 +01:00
|
|
|
|
2014-04-01 18:25:44 +02:00
|
|
|
rv = nghttp2_bufs_addb(bufs, pack_first_byte(inc_indexing, no_index));
|
2014-03-13 14:11:02 +01:00
|
|
|
if(rv != 0) {
|
2014-05-12 14:48:00 +02:00
|
|
|
return rv;
|
2014-03-13 14:11:02 +01:00
|
|
|
}
|
2014-03-10 17:47:38 +01:00
|
|
|
|
2014-07-26 11:19:25 +02:00
|
|
|
rv = emit_string(bufs, nv->name, nv->namelen);
|
2013-07-19 09:50:31 +02:00
|
|
|
if(rv != 0) {
|
|
|
|
return rv;
|
|
|
|
}
|
2014-03-10 17:47:38 +01:00
|
|
|
|
2014-07-26 11:19:25 +02:00
|
|
|
rv = emit_string(bufs, nv->value, nv->valuelen);
|
2014-03-13 14:11:02 +01:00
|
|
|
if(rv != 0) {
|
|
|
|
return rv;
|
|
|
|
}
|
2014-03-10 17:47:38 +01:00
|
|
|
|
2013-07-19 09:50:31 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-09-06 14:53:28 +02:00
|
|
|
static nghttp2_hd_entry* add_hd_table_incremental(nghttp2_hd_context *context,
|
2014-06-29 16:43:14 +02:00
|
|
|
const nghttp2_nv *nv,
|
2014-08-06 15:00:12 +02:00
|
|
|
uint32_t name_hash,
|
|
|
|
uint32_t value_hash,
|
2013-10-12 15:56:29 +02:00
|
|
|
uint8_t entry_flags)
|
2013-09-06 14:53:28 +02:00
|
|
|
{
|
|
|
|
int rv;
|
|
|
|
nghttp2_hd_entry *new_ent;
|
2014-03-10 17:47:38 +01:00
|
|
|
size_t room;
|
|
|
|
|
|
|
|
room = entry_room(nv->namelen, nv->valuelen);
|
|
|
|
|
2013-10-25 18:01:28 +02:00
|
|
|
while(context->hd_table_bufsize + room > context->hd_table_bufsize_max &&
|
2013-10-12 14:49:01 +02:00
|
|
|
context->hd_table.len > 0) {
|
2014-03-10 17:47:38 +01:00
|
|
|
|
2014-05-27 22:28:12 +02:00
|
|
|
size_t idx = context->hd_table.len - 1;
|
|
|
|
nghttp2_hd_entry* ent = hd_ringbuf_get(&context->hd_table, idx);
|
2014-03-10 17:47:38 +01:00
|
|
|
|
2013-09-06 14:53:28 +02:00
|
|
|
context->hd_table_bufsize -= entry_room(ent->nv.namelen, ent->nv.valuelen);
|
2014-07-12 11:47:55 +02:00
|
|
|
|
2014-03-19 16:27:39 +01:00
|
|
|
DEBUGF(fprintf(stderr, "hpack: remove item from header table: "));
|
2013-12-16 15:54:20 +01:00
|
|
|
DEBUGF(fwrite(ent->nv.name, ent->nv.namelen, 1, stderr));
|
|
|
|
DEBUGF(fprintf(stderr, ": "));
|
|
|
|
DEBUGF(fwrite(ent->nv.value, ent->nv.valuelen, 1, stderr));
|
|
|
|
DEBUGF(fprintf(stderr, "\n"));
|
2014-05-08 16:54:07 +02:00
|
|
|
hd_ringbuf_pop_back(&context->hd_table);
|
2013-09-06 14:53:28 +02:00
|
|
|
if(--ent->ref == 0) {
|
|
|
|
nghttp2_hd_entry_free(ent);
|
|
|
|
free(ent);
|
|
|
|
}
|
|
|
|
}
|
2013-10-25 18:01:28 +02:00
|
|
|
|
2013-09-06 14:53:28 +02:00
|
|
|
new_ent = malloc(sizeof(nghttp2_hd_entry));
|
|
|
|
if(new_ent == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2013-10-25 18:01:28 +02:00
|
|
|
|
2014-02-13 15:22:52 +01:00
|
|
|
rv = nghttp2_hd_entry_init(new_ent, entry_flags,
|
2014-08-06 15:00:12 +02:00
|
|
|
nv->name, nv->namelen, nv->value, nv->valuelen,
|
|
|
|
name_hash, value_hash);
|
2014-02-13 15:22:52 +01:00
|
|
|
if(rv != 0) {
|
|
|
|
free(new_ent);
|
|
|
|
return NULL;
|
2013-09-06 14:53:28 +02:00
|
|
|
}
|
2014-02-13 15:22:52 +01:00
|
|
|
|
2013-10-13 12:24:21 +02:00
|
|
|
if(room > context->hd_table_bufsize_max) {
|
2013-09-06 14:53:28 +02:00
|
|
|
/* The entry taking more than NGHTTP2_HD_MAX_BUFFER_SIZE is
|
|
|
|
immediately evicted. */
|
|
|
|
--new_ent->ref;
|
|
|
|
} else {
|
2014-06-22 06:39:17 +02:00
|
|
|
rv = hd_ringbuf_push_front(&context->hd_table, new_ent);
|
|
|
|
|
|
|
|
if(rv != 0) {
|
|
|
|
--new_ent->ref;
|
2014-07-15 15:47:04 +02:00
|
|
|
|
|
|
|
/* nv->name and nv->value are managed by caller. */
|
|
|
|
new_ent->nv.name = NULL;
|
|
|
|
new_ent->nv.namelen = 0;
|
|
|
|
new_ent->nv.value = NULL;
|
|
|
|
new_ent->nv.valuelen = 0;
|
|
|
|
|
2014-06-22 06:39:17 +02:00
|
|
|
nghttp2_hd_entry_free(new_ent);
|
|
|
|
free(new_ent);
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2013-10-25 18:01:28 +02:00
|
|
|
context->hd_table_bufsize += room;
|
2013-09-06 14:53:28 +02:00
|
|
|
}
|
|
|
|
return new_ent;
|
|
|
|
}
|
|
|
|
|
2013-12-18 13:45:15 +01:00
|
|
|
static int name_eq(const nghttp2_nv *a, const nghttp2_nv *b)
|
2013-09-06 14:53:28 +02:00
|
|
|
{
|
2014-01-07 13:51:48 +01:00
|
|
|
return a->namelen == b->namelen && memeq(a->name, b->name, a->namelen);
|
2013-09-06 14:53:28 +02:00
|
|
|
}
|
|
|
|
|
2013-12-18 13:45:15 +01:00
|
|
|
static int value_eq(const nghttp2_nv *a, const nghttp2_nv *b)
|
|
|
|
{
|
2014-01-07 13:51:48 +01:00
|
|
|
return a->valuelen == b->valuelen && memeq(a->value, b->value, a->valuelen);
|
2013-12-18 13:45:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
ssize_t index;
|
|
|
|
/* Nonzero if both name and value are matched. */
|
|
|
|
uint8_t name_value_match;
|
|
|
|
} search_result;
|
|
|
|
|
|
|
|
static search_result search_hd_table(nghttp2_hd_context *context,
|
2014-08-06 15:00:12 +02:00
|
|
|
const nghttp2_nv *nv,
|
|
|
|
uint32_t name_hash, uint32_t value_hash)
|
2013-09-06 14:53:28 +02:00
|
|
|
{
|
2014-09-29 14:58:37 +02:00
|
|
|
ssize_t left = -1, right = (ssize_t)STATIC_TABLE_LENGTH;
|
2013-12-18 13:45:15 +01:00
|
|
|
search_result res = { -1, 0 };
|
2013-09-06 14:53:28 +02:00
|
|
|
size_t i;
|
2014-04-01 18:25:44 +02:00
|
|
|
int use_index = (nv->flags & NGHTTP2_NV_FLAG_NO_INDEX) == 0;
|
|
|
|
|
2014-09-27 16:08:14 +02:00
|
|
|
/* Search dynamic table first, so that we can find recently used
|
|
|
|
entry first */
|
|
|
|
if(use_index) {
|
|
|
|
for(i = 0; i < context->hd_table.len; ++i) {
|
|
|
|
nghttp2_hd_entry *ent = hd_ringbuf_get(&context->hd_table, i);
|
|
|
|
if(ent->name_hash != name_hash || !name_eq(&ent->nv, nv)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(res.index == -1) {
|
|
|
|
res.index = (ssize_t)(i + NGHTTP2_STATIC_TABLE_LENGTH);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(ent->value_hash == value_hash && value_eq(&ent->nv, nv)) {
|
|
|
|
res.index = (ssize_t)(i + NGHTTP2_STATIC_TABLE_LENGTH);
|
|
|
|
res.name_value_match = 1;
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-29 14:58:37 +02:00
|
|
|
while(right - left > 1) {
|
|
|
|
ssize_t mid = (left + right) / 2;
|
|
|
|
nghttp2_hd_entry *ent = &static_table[mid].ent;
|
|
|
|
if(ent->name_hash < name_hash) {
|
|
|
|
left = mid;
|
|
|
|
} else {
|
|
|
|
right = mid;
|
2014-01-07 15:41:09 +01:00
|
|
|
}
|
2014-09-29 14:58:37 +02:00
|
|
|
}
|
2014-04-01 18:25:44 +02:00
|
|
|
|
2014-09-29 14:58:37 +02:00
|
|
|
for(i = right; i < STATIC_TABLE_LENGTH; ++i) {
|
|
|
|
nghttp2_hd_entry *ent = &static_table[i].ent;
|
|
|
|
if(ent->name_hash != name_hash) {
|
|
|
|
break;
|
2014-01-07 15:41:09 +01:00
|
|
|
}
|
2014-07-20 12:13:56 +02:00
|
|
|
|
2014-09-29 14:58:37 +02:00
|
|
|
if(name_eq(&ent->nv, nv)) {
|
|
|
|
if(res.index == -1) {
|
|
|
|
res.index = (ssize_t)(static_table[i].index);
|
|
|
|
}
|
|
|
|
if(use_index &&
|
|
|
|
ent->value_hash == value_hash && value_eq(&ent->nv, nv)) {
|
|
|
|
res.index = (ssize_t)(static_table[i].index);
|
|
|
|
res.name_value_match = 1;
|
|
|
|
return res;
|
|
|
|
}
|
2013-09-06 14:53:28 +02:00
|
|
|
}
|
|
|
|
}
|
2014-07-16 16:04:00 +02:00
|
|
|
|
2013-12-18 13:45:15 +01:00
|
|
|
return res;
|
2013-10-12 14:49:01 +02:00
|
|
|
}
|
|
|
|
|
2014-02-13 15:22:52 +01:00
|
|
|
static void hd_context_shrink_table_size(nghttp2_hd_context *context)
|
2013-10-25 18:01:28 +02:00
|
|
|
{
|
|
|
|
while(context->hd_table_bufsize > context->hd_table_bufsize_max &&
|
|
|
|
context->hd_table.len > 0) {
|
2014-05-27 22:28:12 +02:00
|
|
|
size_t idx = context->hd_table.len - 1;
|
|
|
|
nghttp2_hd_entry* ent = hd_ringbuf_get(&context->hd_table, idx);
|
2013-10-25 18:01:28 +02:00
|
|
|
context->hd_table_bufsize -= entry_room(ent->nv.namelen, ent->nv.valuelen);
|
2014-05-08 16:54:07 +02:00
|
|
|
hd_ringbuf_pop_back(&context->hd_table);
|
2013-10-25 18:01:28 +02:00
|
|
|
if(--ent->ref == 0) {
|
|
|
|
nghttp2_hd_entry_free(ent);
|
|
|
|
free(ent);
|
|
|
|
}
|
|
|
|
}
|
2014-02-13 15:22:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int nghttp2_hd_deflate_change_table_size(nghttp2_hd_deflater *deflater,
|
|
|
|
size_t settings_hd_table_bufsize_max)
|
|
|
|
{
|
|
|
|
size_t next_bufsize = nghttp2_min(settings_hd_table_bufsize_max,
|
|
|
|
deflater->deflate_hd_table_bufsize_max);
|
|
|
|
|
2014-05-11 14:25:27 +02:00
|
|
|
deflater->ctx.hd_table_bufsize_max = next_bufsize;
|
|
|
|
|
2014-07-25 15:55:45 +02:00
|
|
|
deflater->min_hd_table_bufsize_max =
|
|
|
|
nghttp2_min(deflater->min_hd_table_bufsize_max, next_bufsize);
|
|
|
|
|
2014-05-11 14:25:27 +02:00
|
|
|
deflater->notify_table_size_change = 1;
|
2014-02-13 15:22:52 +01:00
|
|
|
|
|
|
|
hd_context_shrink_table_size(&deflater->ctx);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int nghttp2_hd_inflate_change_table_size(nghttp2_hd_inflater *inflater,
|
|
|
|
size_t settings_hd_table_bufsize_max)
|
|
|
|
{
|
|
|
|
inflater->settings_hd_table_bufsize_max = settings_hd_table_bufsize_max;
|
|
|
|
inflater->ctx.hd_table_bufsize_max = settings_hd_table_bufsize_max;
|
|
|
|
hd_context_shrink_table_size(&inflater->ctx);
|
2013-10-25 18:01:28 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-06-16 18:52:11 +02:00
|
|
|
#define INDEX_RANGE_VALID(context, idx) \
|
2014-07-16 16:04:00 +02:00
|
|
|
((idx) < (context)->hd_table.len + NGHTTP2_STATIC_TABLE_LENGTH)
|
2013-10-24 15:09:40 +02:00
|
|
|
|
2014-06-11 16:37:16 +02:00
|
|
|
static size_t get_max_index(nghttp2_hd_context *context)
|
2014-01-25 10:24:15 +01:00
|
|
|
{
|
2014-07-16 16:04:00 +02:00
|
|
|
return context->hd_table.len + NGHTTP2_STATIC_TABLE_LENGTH - 1;
|
2014-01-25 10:24:15 +01:00
|
|
|
}
|
|
|
|
|
2013-10-12 14:49:01 +02:00
|
|
|
nghttp2_hd_entry* nghttp2_hd_table_get(nghttp2_hd_context *context,
|
2014-05-27 22:28:12 +02:00
|
|
|
size_t idx)
|
2013-10-12 14:49:01 +02:00
|
|
|
{
|
2014-06-16 18:52:11 +02:00
|
|
|
assert(INDEX_RANGE_VALID(context, idx));
|
2014-07-16 16:04:00 +02:00
|
|
|
if(idx >= NGHTTP2_STATIC_TABLE_LENGTH) {
|
|
|
|
return hd_ringbuf_get(&context->hd_table, idx - NGHTTP2_STATIC_TABLE_LENGTH);
|
2013-10-12 14:49:01 +02:00
|
|
|
} else {
|
2014-09-29 14:58:37 +02:00
|
|
|
return &static_table[static_table_index[idx]].ent;
|
2013-10-12 14:49:01 +02:00
|
|
|
}
|
2013-08-23 16:38:28 +02:00
|
|
|
}
|
|
|
|
|
2014-01-07 13:51:48 +01:00
|
|
|
#define name_match(NV, NAME) \
|
|
|
|
(nv->namelen == sizeof(NAME) - 1 && memeq(nv->name, NAME, sizeof(NAME) - 1))
|
2013-11-19 13:41:02 +01:00
|
|
|
|
2014-01-26 09:53:04 +01:00
|
|
|
static int hd_deflate_should_indexing(nghttp2_hd_deflater *deflater,
|
2014-01-21 13:48:25 +01:00
|
|
|
const nghttp2_nv *nv)
|
2013-11-19 13:41:02 +01:00
|
|
|
{
|
2014-04-01 18:25:44 +02:00
|
|
|
if((nv->flags & NGHTTP2_NV_FLAG_NO_INDEX) ||
|
|
|
|
entry_room(nv->namelen, nv->valuelen) >
|
2014-02-13 15:22:52 +01:00
|
|
|
deflater->ctx.hd_table_bufsize_max * 3 / 4) {
|
2014-01-21 13:48:25 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2013-11-19 13:41:02 +01:00
|
|
|
#ifdef NGHTTP2_XHD
|
|
|
|
return !name_match(nv, NGHTTP2_XHD);
|
|
|
|
#else /* !NGHTTP2_XHD */
|
|
|
|
return
|
2014-08-06 14:50:54 +02:00
|
|
|
!name_match(nv, ":path") &&
|
2013-11-19 13:41:02 +01:00
|
|
|
!name_match(nv, "content-length") &&
|
2014-08-06 14:50:54 +02:00
|
|
|
!name_match(nv, "set-cookie") &&
|
2013-11-19 13:41:02 +01:00
|
|
|
!name_match(nv, "etag") &&
|
2014-08-06 14:50:54 +02:00
|
|
|
!name_match(nv, "if-modified-since") &&
|
|
|
|
!name_match(nv, "if-none-match") &&
|
|
|
|
!name_match(nv, "location") &&
|
|
|
|
!name_match(nv, "age");
|
2013-11-19 13:41:02 +01:00
|
|
|
#endif /* !NGHTTP2_XHD */
|
|
|
|
}
|
|
|
|
|
2014-01-26 09:53:04 +01:00
|
|
|
static int deflate_nv(nghttp2_hd_deflater *deflater,
|
2014-06-29 16:43:14 +02:00
|
|
|
nghttp2_bufs *bufs, const nghttp2_nv *nv)
|
2013-08-24 15:58:26 +02:00
|
|
|
{
|
|
|
|
int rv;
|
2013-12-18 13:45:15 +01:00
|
|
|
search_result res;
|
2014-10-30 15:31:28 +01:00
|
|
|
ssize_t idx;
|
2014-07-26 11:19:25 +02:00
|
|
|
int incidx = 0;
|
2014-08-06 15:00:12 +02:00
|
|
|
uint32_t name_hash = hash(nv->name, nv->namelen);
|
|
|
|
uint32_t value_hash = hash(nv->value, nv->valuelen);
|
2014-03-10 17:47:38 +01:00
|
|
|
|
2014-03-19 16:27:39 +01:00
|
|
|
DEBUGF(fprintf(stderr, "deflatehd: deflating "));
|
|
|
|
DEBUGF(fwrite(nv->name, nv->namelen, 1, stderr));
|
|
|
|
DEBUGF(fprintf(stderr, ": "));
|
|
|
|
DEBUGF(fwrite(nv->value, nv->valuelen, 1, stderr));
|
|
|
|
DEBUGF(fprintf(stderr, "\n"));
|
|
|
|
|
2014-08-06 15:00:12 +02:00
|
|
|
|
|
|
|
res = search_hd_table(&deflater->ctx, nv, name_hash, value_hash);
|
2014-03-10 17:47:38 +01:00
|
|
|
|
2014-07-26 11:19:25 +02:00
|
|
|
idx = res.index;
|
2014-03-10 17:47:38 +01:00
|
|
|
|
2014-07-26 11:19:25 +02:00
|
|
|
if(res.name_value_match) {
|
|
|
|
|
|
|
|
DEBUGF(fprintf(stderr, "deflatehd: name/value match index=%zd\n", idx));
|
2014-03-19 16:27:39 +01:00
|
|
|
|
2014-07-16 16:04:00 +02:00
|
|
|
rv = emit_indexed_block(bufs, idx);
|
|
|
|
if(rv != 0) {
|
|
|
|
return rv;
|
2013-08-24 15:58:26 +02:00
|
|
|
}
|
2014-03-19 16:27:39 +01:00
|
|
|
|
2014-07-26 11:19:25 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(res.index != -1) {
|
|
|
|
DEBUGF(fprintf(stderr, "deflatehd: name match index=%zd\n",
|
|
|
|
res.index));
|
|
|
|
}
|
|
|
|
|
|
|
|
if(hd_deflate_should_indexing(deflater, nv)) {
|
|
|
|
nghttp2_hd_entry *new_ent;
|
|
|
|
if(idx != -1 && idx < (ssize_t)NGHTTP2_STATIC_TABLE_LENGTH) {
|
|
|
|
nghttp2_nv nv_indname;
|
|
|
|
nv_indname = *nv;
|
|
|
|
nv_indname.name = nghttp2_hd_table_get(&deflater->ctx, idx)->nv.name;
|
2014-08-02 09:21:42 +02:00
|
|
|
new_ent = add_hd_table_incremental(&deflater->ctx, &nv_indname,
|
2014-08-06 15:00:12 +02:00
|
|
|
name_hash, value_hash,
|
2014-07-26 11:19:25 +02:00
|
|
|
NGHTTP2_HD_FLAG_VALUE_ALLOC);
|
2013-08-24 15:58:26 +02:00
|
|
|
} else {
|
2014-08-02 09:21:42 +02:00
|
|
|
new_ent = add_hd_table_incremental(&deflater->ctx, nv,
|
2014-08-06 15:00:12 +02:00
|
|
|
name_hash, value_hash,
|
2014-07-26 11:19:25 +02:00
|
|
|
NGHTTP2_HD_FLAG_NAME_ALLOC |
|
|
|
|
NGHTTP2_HD_FLAG_VALUE_ALLOC);
|
2013-08-24 15:58:26 +02:00
|
|
|
}
|
2014-07-26 11:19:25 +02:00
|
|
|
if(!new_ent) {
|
|
|
|
return NGHTTP2_ERR_HEADER_COMP;
|
|
|
|
}
|
|
|
|
if(new_ent->ref == 0) {
|
|
|
|
nghttp2_hd_entry_free(new_ent);
|
|
|
|
free(new_ent);
|
2013-08-24 15:58:26 +02:00
|
|
|
}
|
2014-07-26 11:19:25 +02:00
|
|
|
incidx = 1;
|
2013-08-24 15:58:26 +02:00
|
|
|
}
|
2014-07-26 11:19:25 +02:00
|
|
|
if(idx == -1) {
|
|
|
|
rv = emit_newname_block(bufs, nv, incidx);
|
|
|
|
} else {
|
|
|
|
rv = emit_indname_block(bufs, idx, nv, incidx);
|
|
|
|
}
|
|
|
|
if(rv != 0) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2013-08-24 15:58:26 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-05-13 16:42:55 +02:00
|
|
|
int nghttp2_hd_deflate_hd_bufs(nghttp2_hd_deflater *deflater,
|
|
|
|
nghttp2_bufs *bufs,
|
2014-06-29 16:43:14 +02:00
|
|
|
const nghttp2_nv *nv, size_t nvlen)
|
2013-07-19 09:50:31 +02:00
|
|
|
{
|
2014-03-10 17:47:38 +01:00
|
|
|
size_t i;
|
2013-07-25 18:34:28 +02:00
|
|
|
int rv = 0;
|
2014-03-10 17:47:38 +01:00
|
|
|
|
2014-01-26 09:53:04 +01:00
|
|
|
if(deflater->ctx.bad) {
|
2013-07-25 18:34:28 +02:00
|
|
|
return NGHTTP2_ERR_HEADER_COMP;
|
|
|
|
}
|
2014-02-13 15:22:52 +01:00
|
|
|
|
2014-05-11 14:25:27 +02:00
|
|
|
if(deflater->notify_table_size_change) {
|
2014-07-25 15:55:45 +02:00
|
|
|
size_t min_hd_table_bufsize_max;
|
|
|
|
|
|
|
|
min_hd_table_bufsize_max = deflater->min_hd_table_bufsize_max;
|
2014-05-11 14:25:27 +02:00
|
|
|
|
|
|
|
deflater->notify_table_size_change = 0;
|
2014-07-25 15:55:45 +02:00
|
|
|
deflater->min_hd_table_bufsize_max = UINT32_MAX;
|
|
|
|
|
|
|
|
if(deflater->ctx.hd_table_bufsize_max > min_hd_table_bufsize_max) {
|
|
|
|
|
|
|
|
rv = emit_table_size(bufs, min_hd_table_bufsize_max);
|
|
|
|
|
|
|
|
if(rv != 0) {
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
}
|
2014-05-11 14:25:27 +02:00
|
|
|
|
|
|
|
rv = emit_table_size(bufs, deflater->ctx.hd_table_bufsize_max);
|
|
|
|
|
2014-02-13 15:22:52 +01:00
|
|
|
if(rv != 0) {
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-06 14:53:28 +02:00
|
|
|
for(i = 0; i < nvlen; ++i) {
|
2014-03-13 14:11:02 +01:00
|
|
|
rv = deflate_nv(deflater, bufs, &nv[i]);
|
2013-08-24 15:58:26 +02:00
|
|
|
if(rv != 0) {
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
}
|
2014-03-19 16:27:39 +01:00
|
|
|
|
|
|
|
DEBUGF(fprintf(stderr,
|
|
|
|
"deflatehd: all input name/value pairs were deflated\n"));
|
|
|
|
|
2014-03-13 14:11:02 +01:00
|
|
|
return 0;
|
2013-07-25 18:34:28 +02:00
|
|
|
fail:
|
2014-03-19 16:27:39 +01:00
|
|
|
DEBUGF(fprintf(stderr, "deflatehd: error return %d\n", rv));
|
|
|
|
|
2014-01-26 09:53:04 +01:00
|
|
|
deflater->ctx.bad = 1;
|
2013-07-25 18:34:28 +02:00
|
|
|
return rv;
|
2013-07-19 09:50:31 +02:00
|
|
|
}
|
|
|
|
|
2014-05-13 16:42:55 +02:00
|
|
|
ssize_t nghttp2_hd_deflate_hd(nghttp2_hd_deflater *deflater,
|
|
|
|
uint8_t *buf, size_t buflen,
|
2014-06-29 16:43:14 +02:00
|
|
|
const nghttp2_nv *nv, size_t nvlen)
|
2014-05-13 16:42:55 +02:00
|
|
|
{
|
|
|
|
nghttp2_bufs bufs;
|
|
|
|
int rv;
|
|
|
|
|
|
|
|
rv = nghttp2_bufs_wrap_init(&bufs, buf, buflen);
|
|
|
|
|
|
|
|
if(rv != 0) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = nghttp2_hd_deflate_hd_bufs(deflater, &bufs, nv, nvlen);
|
|
|
|
|
|
|
|
buflen = nghttp2_bufs_len(&bufs);
|
|
|
|
|
|
|
|
nghttp2_bufs_wrap_free(&bufs);
|
|
|
|
|
|
|
|
if(rv == NGHTTP2_ERR_BUFFER_ERROR) {
|
|
|
|
return NGHTTP2_ERR_INSUFF_BUFSIZE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(rv != 0) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2014-06-16 19:17:49 +02:00
|
|
|
return (ssize_t)buflen;
|
2014-05-13 16:42:55 +02:00
|
|
|
}
|
|
|
|
|
2014-10-19 15:19:33 +02:00
|
|
|
size_t nghttp2_hd_deflate_bound(nghttp2_hd_deflater *deflater _U_,
|
2014-05-13 16:42:55 +02:00
|
|
|
const nghttp2_nv *nva, size_t nvlen)
|
|
|
|
{
|
2014-07-12 11:47:55 +02:00
|
|
|
size_t n = 0;
|
2014-05-13 16:42:55 +02:00
|
|
|
size_t i;
|
|
|
|
|
|
|
|
/* Possible Maximum Header Table Size Change. Encoding (1u << 31) -
|
2014-07-25 18:12:19 +02:00
|
|
|
1 using 4 bit prefix requires 6 bytes. We may emit this at most
|
|
|
|
twice. */
|
|
|
|
n += 12;
|
2014-05-13 16:42:55 +02:00
|
|
|
|
|
|
|
/* Use Literal Header Field without indexing - New Name, since it is
|
|
|
|
most space consuming format. Also we choose the less one between
|
|
|
|
non-huffman and huffman, so using literal byte count is
|
|
|
|
sufficient for upper bound.
|
|
|
|
|
|
|
|
Encoding (1u << 31) - 1 using 7 bit prefix requires 6 bytes. We
|
|
|
|
need 2 of this for |nvlen| header fields. */
|
|
|
|
n += 6 * 2 * nvlen;
|
|
|
|
|
|
|
|
for(i = 0; i < nvlen; ++i) {
|
|
|
|
n += nva[i].namelen + nva[i].valuelen;
|
|
|
|
}
|
|
|
|
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
int nghttp2_hd_deflate_new(nghttp2_hd_deflater **deflater_ptr,
|
|
|
|
size_t deflate_hd_table_bufsize_max)
|
|
|
|
{
|
2014-06-25 14:26:47 +02:00
|
|
|
int rv;
|
|
|
|
nghttp2_hd_deflater *deflater;
|
|
|
|
|
|
|
|
deflater = malloc(sizeof(nghttp2_hd_deflater));
|
2014-05-13 16:42:55 +02:00
|
|
|
|
2014-06-25 14:26:47 +02:00
|
|
|
if(deflater == NULL) {
|
2014-05-13 16:42:55 +02:00
|
|
|
return NGHTTP2_ERR_NOMEM;
|
|
|
|
}
|
|
|
|
|
2014-06-25 14:26:47 +02:00
|
|
|
rv = nghttp2_hd_deflate_init2(deflater, deflate_hd_table_bufsize_max);
|
|
|
|
|
|
|
|
if(rv != 0) {
|
2014-07-17 17:21:15 +02:00
|
|
|
free(deflater);
|
|
|
|
|
2014-06-25 14:26:47 +02:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
*deflater_ptr = deflater;
|
|
|
|
|
|
|
|
return 0;
|
2014-05-13 16:42:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void nghttp2_hd_deflate_del(nghttp2_hd_deflater *deflater)
|
|
|
|
{
|
|
|
|
nghttp2_hd_deflate_free(deflater);
|
|
|
|
|
|
|
|
free(deflater);
|
|
|
|
}
|
|
|
|
|
2014-01-26 09:53:04 +01:00
|
|
|
static void hd_inflate_set_huffman_encoded(nghttp2_hd_inflater *inflater,
|
2014-01-25 10:24:15 +01:00
|
|
|
const uint8_t *in)
|
2013-07-19 09:50:31 +02:00
|
|
|
{
|
2014-01-25 10:24:15 +01:00
|
|
|
inflater->huffman_encoded = (*in & (1 << 7)) != 0;
|
|
|
|
}
|
2014-01-16 15:41:13 +01:00
|
|
|
|
2014-01-25 10:24:15 +01:00
|
|
|
/*
|
|
|
|
* Decodes the integer from the range [in, last). The result is
|
|
|
|
* assigned to |inflater->left|. If the |inflater->left| is 0, then
|
|
|
|
* it performs variable integer decoding from scratch. Otherwise, it
|
|
|
|
* uses the |inflater->left| as the initial value and continues to
|
|
|
|
* decode assuming that [in, last) begins with intermediary sequence.
|
|
|
|
*
|
|
|
|
* This function returns the number of bytes read if it succeeds, or
|
|
|
|
* one of the following negative error codes:
|
|
|
|
*
|
|
|
|
* NGHTTP2_ERR_HEADER_COMP
|
|
|
|
* Integer decoding failed
|
|
|
|
*/
|
2014-01-26 09:53:04 +01:00
|
|
|
static ssize_t hd_inflate_read_len(nghttp2_hd_inflater *inflater,
|
2014-01-25 10:24:15 +01:00
|
|
|
int *rfin,
|
|
|
|
uint8_t *in, uint8_t *last,
|
2014-06-11 16:37:16 +02:00
|
|
|
size_t prefix, size_t maxlen)
|
2014-01-25 10:24:15 +01:00
|
|
|
{
|
2014-07-22 15:38:18 +02:00
|
|
|
ssize_t rv;
|
|
|
|
uint32_t out;
|
|
|
|
|
2014-01-25 10:24:15 +01:00
|
|
|
*rfin = 0;
|
2014-07-22 15:38:18 +02:00
|
|
|
|
|
|
|
rv = decode_length(&out, &inflater->shift, rfin, (uint32_t)inflater->left,
|
|
|
|
inflater->shift, in, last, prefix);
|
|
|
|
|
|
|
|
if(rv == -1) {
|
|
|
|
DEBUGF(fprintf(stderr, "inflatehd: integer decoding failed\n"));
|
2014-01-25 10:24:15 +01:00
|
|
|
return NGHTTP2_ERR_HEADER_COMP;
|
|
|
|
}
|
2014-07-22 15:38:18 +02:00
|
|
|
|
|
|
|
if(out > maxlen) {
|
2014-03-19 16:27:39 +01:00
|
|
|
DEBUGF(fprintf(stderr,
|
|
|
|
"inflatehd: integer exceeded the maximum value %zu\n",
|
|
|
|
maxlen));
|
2013-07-25 18:34:28 +02:00
|
|
|
return NGHTTP2_ERR_HEADER_COMP;
|
|
|
|
}
|
2014-07-22 15:38:18 +02:00
|
|
|
|
|
|
|
inflater->left = out;
|
|
|
|
|
2014-08-02 03:40:25 +02:00
|
|
|
DEBUGF(fprintf(stderr, "inflatehd: decoded integer is %u\n", out));
|
|
|
|
|
2014-07-22 15:38:18 +02:00
|
|
|
return rv;
|
2014-01-25 10:24:15 +01:00
|
|
|
}
|
2014-01-16 15:41:13 +01:00
|
|
|
|
2014-01-25 10:24:15 +01:00
|
|
|
/*
|
|
|
|
* Reads |inflater->left| bytes from the range [in, last) and performs
|
|
|
|
* huffman decoding against them and pushes the result into the
|
|
|
|
* |buffer|.
|
|
|
|
*
|
|
|
|
* This function returns the number of bytes read if it succeeds, or
|
|
|
|
* one of the following negative error codes:
|
|
|
|
*
|
|
|
|
* NGHTTP2_ERR_NOMEM
|
|
|
|
* Out of memory
|
|
|
|
* NGHTTP2_ERR_HEADER_COMP
|
|
|
|
* Huffman decoding failed
|
2014-05-12 14:48:00 +02:00
|
|
|
* NGHTTP2_ERR_BUFFER_ERROR
|
|
|
|
* Out of buffer space.
|
2014-01-25 10:24:15 +01:00
|
|
|
*/
|
2014-01-26 09:53:04 +01:00
|
|
|
static ssize_t hd_inflate_read_huff(nghttp2_hd_inflater *inflater,
|
2014-03-11 13:18:28 +01:00
|
|
|
nghttp2_bufs *bufs,
|
2014-01-25 10:24:15 +01:00
|
|
|
uint8_t *in, uint8_t *last)
|
|
|
|
{
|
2014-06-11 16:37:16 +02:00
|
|
|
ssize_t readlen;
|
2014-01-25 10:24:15 +01:00
|
|
|
int final = 0;
|
2014-07-22 15:38:18 +02:00
|
|
|
if((size_t)(last - in) >= inflater->left) {
|
2014-01-25 10:24:15 +01:00
|
|
|
last = in + inflater->left;
|
|
|
|
final = 1;
|
|
|
|
}
|
2014-06-11 16:37:16 +02:00
|
|
|
readlen = nghttp2_hd_huff_decode(&inflater->huff_decode_ctx, bufs,
|
|
|
|
in, last - in, final);
|
2014-05-12 14:48:00 +02:00
|
|
|
|
2014-06-11 16:37:16 +02:00
|
|
|
if(readlen < 0) {
|
2014-03-19 16:27:39 +01:00
|
|
|
DEBUGF(fprintf(stderr, "inflatehd: huffman decoding failed\n"));
|
2014-06-11 16:37:16 +02:00
|
|
|
return readlen;
|
2014-01-25 10:24:15 +01:00
|
|
|
}
|
2014-07-22 15:38:18 +02:00
|
|
|
inflater->left -= (size_t)readlen;
|
2014-06-11 16:37:16 +02:00
|
|
|
return readlen;
|
2014-01-25 10:24:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Reads |inflater->left| bytes from the range [in, last) and copies
|
|
|
|
* them into the |buffer|.
|
|
|
|
*
|
|
|
|
* This function returns the number of bytes read if it succeeds, or
|
|
|
|
* one of the following negative error codes:
|
|
|
|
*
|
|
|
|
* NGHTTP2_ERR_NOMEM
|
|
|
|
* Out of memory
|
|
|
|
* NGHTTP2_ERR_HEADER_COMP
|
|
|
|
* Header decompression failed
|
2014-05-12 14:48:00 +02:00
|
|
|
* NGHTTP2_ERR_BUFFER_ERROR
|
|
|
|
* Out of buffer space.
|
2014-01-25 10:24:15 +01:00
|
|
|
*/
|
2014-01-26 09:53:04 +01:00
|
|
|
static ssize_t hd_inflate_read(nghttp2_hd_inflater *inflater,
|
2014-03-11 13:18:28 +01:00
|
|
|
nghttp2_bufs *bufs,
|
2014-01-25 10:24:15 +01:00
|
|
|
uint8_t *in, uint8_t *last)
|
|
|
|
{
|
|
|
|
int rv;
|
2014-07-22 15:38:18 +02:00
|
|
|
size_t len = nghttp2_min((size_t)(last - in), inflater->left);
|
2014-03-11 13:18:28 +01:00
|
|
|
rv = nghttp2_bufs_add(bufs, in, len);
|
2014-01-25 10:24:15 +01:00
|
|
|
if(rv != 0) {
|
|
|
|
return rv;
|
|
|
|
}
|
2014-07-22 15:38:18 +02:00
|
|
|
inflater->left -= len;
|
2014-06-16 19:17:49 +02:00
|
|
|
return (ssize_t)len;
|
2014-01-25 10:24:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Finalize indexed header representation reception. If header is
|
|
|
|
* emitted, |*nv_out| is filled with that value and 0 is returned. If
|
|
|
|
* no header is emitted, 1 is returned.
|
|
|
|
*
|
|
|
|
* This function returns either 0 or 1 if it succeeds, or one of the
|
|
|
|
* following negative error codes:
|
|
|
|
*
|
|
|
|
* NGHTTP2_ERR_NOMEM
|
|
|
|
* Out of memory
|
|
|
|
*/
|
2014-01-26 09:53:04 +01:00
|
|
|
static int hd_inflate_commit_indexed(nghttp2_hd_inflater *inflater,
|
2014-01-25 10:24:15 +01:00
|
|
|
nghttp2_nv *nv_out)
|
|
|
|
{
|
2014-01-26 09:53:04 +01:00
|
|
|
nghttp2_hd_entry *ent = nghttp2_hd_table_get(&inflater->ctx, inflater->index);
|
2014-07-12 11:47:55 +02:00
|
|
|
|
|
|
|
emit_indexed_header(nv_out, ent);
|
|
|
|
|
|
|
|
return 0;
|
2014-01-25 10:24:15 +01:00
|
|
|
}
|
2014-01-16 15:41:13 +01:00
|
|
|
|
2014-03-11 13:18:28 +01:00
|
|
|
static int hd_inflate_remove_bufs(nghttp2_hd_inflater *inflater,
|
2014-05-08 16:54:07 +02:00
|
|
|
nghttp2_nv *nv, int value_only)
|
2014-03-11 13:18:28 +01:00
|
|
|
{
|
|
|
|
ssize_t rv;
|
2014-05-28 16:02:23 +02:00
|
|
|
size_t buflen;
|
|
|
|
uint8_t *buf;
|
2014-09-30 16:01:58 +02:00
|
|
|
nghttp2_buf *pbuf;
|
2014-03-11 13:18:28 +01:00
|
|
|
|
2014-09-30 16:01:58 +02:00
|
|
|
if(inflater->index_required ||
|
|
|
|
inflater->nvbufs.head != inflater->nvbufs.cur) {
|
2014-03-11 13:18:28 +01:00
|
|
|
|
2014-09-30 16:01:58 +02:00
|
|
|
rv = nghttp2_bufs_remove(&inflater->nvbufs, &buf);
|
|
|
|
|
|
|
|
if(rv < 0) {
|
|
|
|
return NGHTTP2_ERR_NOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
buflen = rv;
|
|
|
|
|
|
|
|
if(value_only) {
|
|
|
|
nv->name = NULL;
|
|
|
|
nv->namelen = 0;
|
|
|
|
} else {
|
|
|
|
nv->name = buf;
|
|
|
|
nv->namelen = inflater->newnamelen;
|
|
|
|
}
|
|
|
|
|
|
|
|
nv->value = buf + nv->namelen;
|
|
|
|
nv->valuelen = buflen - nv->namelen;
|
|
|
|
|
|
|
|
return 0;
|
2014-03-11 13:18:28 +01:00
|
|
|
}
|
|
|
|
|
2014-09-30 16:01:58 +02:00
|
|
|
/* If we are not going to store header in header table and
|
|
|
|
name/value are in first chunk, we just refer them from nv,
|
|
|
|
instead of mallocing another memory. */
|
|
|
|
|
|
|
|
pbuf = &inflater->nvbufs.head->buf;
|
2014-03-11 13:18:28 +01:00
|
|
|
|
2014-05-28 16:02:23 +02:00
|
|
|
if(value_only) {
|
|
|
|
nv->name = NULL;
|
|
|
|
nv->namelen = 0;
|
|
|
|
} else {
|
2014-09-30 16:01:58 +02:00
|
|
|
nv->name = pbuf->pos;
|
2014-05-28 16:02:23 +02:00
|
|
|
nv->namelen = inflater->newnamelen;
|
2014-03-11 13:18:28 +01:00
|
|
|
}
|
|
|
|
|
2014-09-30 16:01:58 +02:00
|
|
|
nv->value = pbuf->pos + nv->namelen;
|
|
|
|
nv->valuelen = nghttp2_buf_len(pbuf) - nv->namelen;
|
|
|
|
|
|
|
|
/* Resetting does not change the content of first buffer */
|
|
|
|
nghttp2_bufs_reset(&inflater->nvbufs);
|
2014-03-11 13:18:28 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-01-25 10:24:15 +01:00
|
|
|
/*
|
|
|
|
* Finalize literal header representation - new name- reception. If
|
|
|
|
* header is emitted, |*nv_out| is filled with that value and 0 is
|
|
|
|
* returned.
|
|
|
|
*
|
|
|
|
* This function returns 0 if it succeeds, or one of the following
|
|
|
|
* negative error codes:
|
|
|
|
*
|
|
|
|
* NGHTTP2_ERR_NOMEM
|
|
|
|
* Out of memory
|
|
|
|
*/
|
2014-01-26 09:53:04 +01:00
|
|
|
static int hd_inflate_commit_newname(nghttp2_hd_inflater *inflater,
|
2014-01-25 10:24:15 +01:00
|
|
|
nghttp2_nv *nv_out)
|
|
|
|
{
|
2014-03-11 13:18:28 +01:00
|
|
|
int rv;
|
|
|
|
nghttp2_nv nv;
|
|
|
|
|
|
|
|
rv = hd_inflate_remove_bufs(inflater, &nv, 0 /* name and value */);
|
|
|
|
if(rv != 0) {
|
|
|
|
return NGHTTP2_ERR_NOMEM;
|
|
|
|
}
|
|
|
|
|
2014-04-01 18:25:44 +02:00
|
|
|
if(inflater->no_index) {
|
|
|
|
nv.flags = NGHTTP2_NV_FLAG_NO_INDEX;
|
|
|
|
} else {
|
|
|
|
nv.flags = NGHTTP2_NV_FLAG_NONE;
|
|
|
|
}
|
2014-04-01 16:17:50 +02:00
|
|
|
|
2014-01-25 10:24:15 +01:00
|
|
|
if(inflater->index_required) {
|
|
|
|
nghttp2_hd_entry *new_ent;
|
2014-03-11 13:18:28 +01:00
|
|
|
uint8_t ent_flags;
|
|
|
|
|
2014-05-28 16:02:23 +02:00
|
|
|
/* nv->value points to the middle of the buffer pointed by
|
|
|
|
nv->name. So we just need to keep track of nv->name for memory
|
|
|
|
management. */
|
|
|
|
ent_flags = NGHTTP2_HD_FLAG_NAME_ALLOC | NGHTTP2_HD_FLAG_NAME_GIFT;
|
2014-03-11 13:18:28 +01:00
|
|
|
|
2014-08-06 15:00:12 +02:00
|
|
|
new_ent = add_hd_table_incremental(&inflater->ctx, &nv,
|
|
|
|
hash(nv.name, nv.namelen),
|
|
|
|
hash(nv.value, nv.valuelen),
|
|
|
|
ent_flags);
|
2014-03-11 13:18:28 +01:00
|
|
|
|
2014-01-25 10:24:15 +01:00
|
|
|
if(new_ent) {
|
2014-01-26 09:53:04 +01:00
|
|
|
emit_indexed_header(nv_out, new_ent);
|
2014-01-25 10:24:15 +01:00
|
|
|
inflater->ent_keep = new_ent;
|
2014-03-11 13:18:28 +01:00
|
|
|
|
2014-01-25 10:24:15 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2014-03-11 13:18:28 +01:00
|
|
|
|
|
|
|
free(nv.name);
|
|
|
|
|
2014-01-25 10:24:15 +01:00
|
|
|
return NGHTTP2_ERR_NOMEM;
|
|
|
|
}
|
2014-03-11 13:18:28 +01:00
|
|
|
|
|
|
|
emit_literal_header(nv_out, &nv);
|
|
|
|
|
2014-09-30 16:01:58 +02:00
|
|
|
if(nv.name != inflater->nvbufs.head->buf.pos) {
|
|
|
|
inflater->nv_keep = nv.name;
|
|
|
|
}
|
2014-03-11 13:18:28 +01:00
|
|
|
|
2014-01-25 10:24:15 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Finalize literal header representation - indexed name-
|
|
|
|
* reception. If header is emitted, |*nv_out| is filled with that
|
|
|
|
* value and 0 is returned.
|
|
|
|
*
|
|
|
|
* This function returns 0 if it succeeds, or one of the following
|
|
|
|
* negative error codes:
|
|
|
|
*
|
|
|
|
* NGHTTP2_ERR_NOMEM
|
|
|
|
* Out of memory
|
|
|
|
*/
|
2014-01-26 09:53:04 +01:00
|
|
|
static int hd_inflate_commit_indname(nghttp2_hd_inflater *inflater,
|
2014-01-25 10:24:15 +01:00
|
|
|
nghttp2_nv *nv_out)
|
|
|
|
{
|
2014-03-11 13:18:28 +01:00
|
|
|
int rv;
|
|
|
|
nghttp2_nv nv;
|
2014-07-21 19:09:01 +02:00
|
|
|
nghttp2_hd_entry *ent_name;
|
2014-03-11 13:18:28 +01:00
|
|
|
|
|
|
|
rv = hd_inflate_remove_bufs(inflater, &nv, 1 /* value only */);
|
|
|
|
if(rv != 0) {
|
|
|
|
return NGHTTP2_ERR_NOMEM;
|
|
|
|
}
|
|
|
|
|
2014-04-01 18:25:44 +02:00
|
|
|
if(inflater->no_index) {
|
|
|
|
nv.flags = NGHTTP2_NV_FLAG_NO_INDEX;
|
|
|
|
} else {
|
|
|
|
nv.flags = NGHTTP2_NV_FLAG_NONE;
|
|
|
|
}
|
2014-04-01 16:17:50 +02:00
|
|
|
|
2014-07-21 19:09:01 +02:00
|
|
|
ent_name = nghttp2_hd_table_get(&inflater->ctx, inflater->index);
|
|
|
|
|
|
|
|
nv.name = ent_name->nv.name;
|
|
|
|
nv.namelen = ent_name->nv.namelen;
|
2014-03-11 13:18:28 +01:00
|
|
|
|
2014-01-25 10:24:15 +01:00
|
|
|
if(inflater->index_required) {
|
|
|
|
nghttp2_hd_entry *new_ent;
|
2014-03-11 13:18:28 +01:00
|
|
|
uint8_t ent_flags;
|
|
|
|
int static_name;
|
|
|
|
|
|
|
|
ent_flags = NGHTTP2_HD_FLAG_VALUE_ALLOC | NGHTTP2_HD_FLAG_VALUE_GIFT;
|
2014-07-16 16:04:00 +02:00
|
|
|
static_name = inflater->index < NGHTTP2_STATIC_TABLE_LENGTH;
|
2014-01-25 10:24:15 +01:00
|
|
|
|
2014-03-02 16:53:04 +01:00
|
|
|
if(!static_name) {
|
2014-01-25 10:24:15 +01:00
|
|
|
ent_flags |= NGHTTP2_HD_FLAG_NAME_ALLOC;
|
2014-03-02 16:53:04 +01:00
|
|
|
/* For entry in static table, we must not touch ref, because it
|
|
|
|
is shared by threads */
|
2014-07-21 19:09:01 +02:00
|
|
|
++ent_name->ref;
|
2014-01-25 10:24:15 +01:00
|
|
|
}
|
2014-03-11 13:18:28 +01:00
|
|
|
|
2014-08-06 15:00:12 +02:00
|
|
|
new_ent = add_hd_table_incremental(&inflater->ctx, &nv,
|
|
|
|
ent_name->name_hash,
|
|
|
|
hash(nv.value, nv.valuelen),
|
|
|
|
ent_flags);
|
2014-03-11 13:18:28 +01:00
|
|
|
|
2014-07-21 19:09:01 +02:00
|
|
|
if(!static_name && --ent_name->ref == 0) {
|
|
|
|
nghttp2_hd_entry_free(ent_name);
|
|
|
|
free(ent_name);
|
2014-01-25 10:24:15 +01:00
|
|
|
}
|
2014-03-11 13:18:28 +01:00
|
|
|
|
2014-01-25 10:24:15 +01:00
|
|
|
if(new_ent) {
|
2014-01-26 09:53:04 +01:00
|
|
|
emit_indexed_header(nv_out, new_ent);
|
2014-03-11 13:18:28 +01:00
|
|
|
|
2014-01-25 10:24:15 +01:00
|
|
|
inflater->ent_keep = new_ent;
|
2014-03-11 13:18:28 +01:00
|
|
|
|
2014-01-25 10:24:15 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2014-03-11 13:18:28 +01:00
|
|
|
|
|
|
|
free(nv.value);
|
|
|
|
|
2014-01-25 10:24:15 +01:00
|
|
|
return NGHTTP2_ERR_NOMEM;
|
|
|
|
}
|
|
|
|
|
2014-03-11 13:18:28 +01:00
|
|
|
emit_literal_header(nv_out, &nv);
|
|
|
|
|
2014-09-30 16:01:58 +02:00
|
|
|
if(nv.value != inflater->nvbufs.head->buf.pos) {
|
|
|
|
inflater->nv_keep = nv.value;
|
|
|
|
}
|
2014-03-11 13:18:28 +01:00
|
|
|
|
|
|
|
return 0;
|
2014-01-25 10:24:15 +01:00
|
|
|
}
|
|
|
|
|
2014-01-26 09:53:04 +01:00
|
|
|
ssize_t nghttp2_hd_inflate_hd(nghttp2_hd_inflater *inflater,
|
2014-01-25 10:24:15 +01:00
|
|
|
nghttp2_nv *nv_out, int *inflate_flags,
|
|
|
|
uint8_t *in, size_t inlen, int in_final)
|
|
|
|
{
|
|
|
|
ssize_t rv = 0;
|
|
|
|
uint8_t *first = in;
|
|
|
|
uint8_t *last = in + inlen;
|
|
|
|
int rfin = 0;
|
|
|
|
|
2014-02-26 15:20:52 +01:00
|
|
|
if(inflater->ctx.bad) {
|
|
|
|
return NGHTTP2_ERR_HEADER_COMP;
|
|
|
|
}
|
|
|
|
|
2014-03-19 16:27:39 +01:00
|
|
|
DEBUGF(fprintf(stderr, "inflatehd: start state=%d\n",
|
2014-01-25 10:24:15 +01:00
|
|
|
inflater->state));
|
2014-02-13 15:22:52 +01:00
|
|
|
hd_inflate_keep_free(inflater);
|
2014-01-25 10:24:15 +01:00
|
|
|
*inflate_flags = NGHTTP2_HD_INFLATE_NONE;
|
2013-07-19 09:50:31 +02:00
|
|
|
for(; in != last;) {
|
2014-01-25 10:24:15 +01:00
|
|
|
switch(inflater->state) {
|
|
|
|
case NGHTTP2_HD_STATE_OPCODE:
|
2014-07-12 11:47:55 +02:00
|
|
|
if((*in & 0xe0u) == 0x20u) {
|
2014-04-01 18:25:44 +02:00
|
|
|
DEBUGF(fprintf(stderr, "inflatehd: header table size change\n"));
|
|
|
|
inflater->opcode = NGHTTP2_HD_OPCODE_INDEXED;
|
|
|
|
inflater->state = NGHTTP2_HD_STATE_READ_TABLE_SIZE;
|
2014-02-13 15:22:52 +01:00
|
|
|
} else if(*in & 0x80u) {
|
2014-03-19 16:27:39 +01:00
|
|
|
DEBUGF(fprintf(stderr, "inflatehd: indexed repr\n"));
|
2014-01-25 10:24:15 +01:00
|
|
|
inflater->opcode = NGHTTP2_HD_OPCODE_INDEXED;
|
|
|
|
inflater->state = NGHTTP2_HD_STATE_READ_INDEX;
|
|
|
|
} else {
|
2014-04-01 18:25:44 +02:00
|
|
|
if(*in == 0x40u || *in == 0 || *in == 0x10u) {
|
2014-03-19 16:27:39 +01:00
|
|
|
DEBUGF(fprintf(stderr,
|
|
|
|
"inflatehd: literal header repr - new name\n"));
|
2014-01-25 10:24:15 +01:00
|
|
|
inflater->opcode = NGHTTP2_HD_OPCODE_NEWNAME;
|
|
|
|
inflater->state = NGHTTP2_HD_STATE_NEWNAME_CHECK_NAMELEN;
|
|
|
|
} else {
|
2014-03-19 16:27:39 +01:00
|
|
|
DEBUGF(fprintf(stderr,
|
|
|
|
"inflatehd: literal header repr - indexed name\n"));
|
2014-01-25 10:24:15 +01:00
|
|
|
inflater->opcode = NGHTTP2_HD_OPCODE_INDNAME;
|
|
|
|
inflater->state = NGHTTP2_HD_STATE_READ_INDEX;
|
|
|
|
}
|
2014-04-01 18:25:44 +02:00
|
|
|
inflater->index_required = (*in & 0x40) != 0;
|
2014-07-31 16:08:51 +02:00
|
|
|
inflater->no_index = (*in & 0xf0u) == 0x10u;
|
2014-04-01 18:25:44 +02:00
|
|
|
DEBUGF(fprintf(stderr,
|
|
|
|
"inflatehd: indexing required=%d, no_index=%d\n",
|
|
|
|
inflater->index_required,
|
|
|
|
inflater->no_index));
|
2014-01-25 10:24:15 +01:00
|
|
|
if(inflater->opcode == NGHTTP2_HD_OPCODE_NEWNAME) {
|
|
|
|
++in;
|
|
|
|
}
|
2013-11-16 09:05:18 +01:00
|
|
|
}
|
2014-01-25 10:24:15 +01:00
|
|
|
inflater->left = 0;
|
2014-07-21 18:50:29 +02:00
|
|
|
inflater->shift = 0;
|
2014-02-13 15:22:52 +01:00
|
|
|
break;
|
|
|
|
case NGHTTP2_HD_STATE_READ_TABLE_SIZE:
|
|
|
|
rfin = 0;
|
2014-07-12 11:47:55 +02:00
|
|
|
rv = hd_inflate_read_len(inflater, &rfin, in, last, 5,
|
2014-02-13 15:22:52 +01:00
|
|
|
inflater->settings_hd_table_bufsize_max);
|
|
|
|
if(rv < 0) {
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
in += rv;
|
|
|
|
if(!rfin) {
|
2014-03-13 16:51:26 +01:00
|
|
|
goto almost_ok;
|
2014-02-13 15:22:52 +01:00
|
|
|
}
|
2014-07-22 15:38:18 +02:00
|
|
|
DEBUGF(fprintf(stderr, "inflatehd: table_size=%zu\n", inflater->left));
|
2014-02-13 15:22:52 +01:00
|
|
|
inflater->ctx.hd_table_bufsize_max = inflater->left;
|
|
|
|
hd_context_shrink_table_size(&inflater->ctx);
|
|
|
|
inflater->state = NGHTTP2_HD_STATE_OPCODE;
|
|
|
|
break;
|
2014-04-01 18:25:44 +02:00
|
|
|
case NGHTTP2_HD_STATE_READ_INDEX: {
|
|
|
|
size_t prefixlen;
|
|
|
|
|
|
|
|
if(inflater->opcode == NGHTTP2_HD_OPCODE_INDEXED) {
|
|
|
|
prefixlen = 7;
|
|
|
|
} else if(inflater->index_required) {
|
|
|
|
prefixlen = 6;
|
|
|
|
} else {
|
|
|
|
prefixlen = 4;
|
|
|
|
}
|
|
|
|
|
2014-01-25 10:24:15 +01:00
|
|
|
rfin = 0;
|
2014-04-01 18:25:44 +02:00
|
|
|
rv = hd_inflate_read_len(inflater, &rfin, in, last, prefixlen,
|
2014-01-26 09:53:04 +01:00
|
|
|
get_max_index(&inflater->ctx) + 1);
|
2014-01-25 10:24:15 +01:00
|
|
|
if(rv < 0) {
|
2013-08-22 18:28:01 +02:00
|
|
|
goto fail;
|
|
|
|
}
|
2014-05-01 02:06:54 +02:00
|
|
|
|
2014-01-25 10:24:15 +01:00
|
|
|
in += rv;
|
2014-05-01 02:06:54 +02:00
|
|
|
|
2014-07-21 19:09:01 +02:00
|
|
|
if(!rfin) {
|
|
|
|
goto almost_ok;
|
|
|
|
}
|
|
|
|
|
2014-05-01 02:06:54 +02:00
|
|
|
if(inflater->left == 0) {
|
|
|
|
rv = NGHTTP2_ERR_HEADER_COMP;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2014-07-22 15:38:18 +02:00
|
|
|
DEBUGF(fprintf(stderr, "inflatehd: index=%zu\n", inflater->left));
|
2014-01-25 10:24:15 +01:00
|
|
|
if(inflater->opcode == NGHTTP2_HD_OPCODE_INDEXED) {
|
|
|
|
inflater->index = inflater->left;
|
|
|
|
--inflater->index;
|
2014-07-21 19:09:01 +02:00
|
|
|
|
2014-01-25 10:24:15 +01:00
|
|
|
rv = hd_inflate_commit_indexed(inflater, nv_out);
|
|
|
|
if(rv < 0) {
|
2013-07-25 18:34:28 +02:00
|
|
|
goto fail;
|
2013-07-19 09:50:31 +02:00
|
|
|
}
|
2014-01-25 10:24:15 +01:00
|
|
|
inflater->state = NGHTTP2_HD_STATE_OPCODE;
|
|
|
|
/* If rv == 1, no header was emitted */
|
|
|
|
if(rv == 0) {
|
|
|
|
*inflate_flags |= NGHTTP2_HD_INFLATE_EMIT;
|
2014-06-16 19:17:49 +02:00
|
|
|
return (ssize_t)(in - first);
|
2013-10-24 14:52:02 +02:00
|
|
|
}
|
2014-01-25 10:24:15 +01:00
|
|
|
} else {
|
|
|
|
inflater->index = inflater->left;
|
|
|
|
--inflater->index;
|
2014-07-21 19:09:01 +02:00
|
|
|
|
2014-01-25 10:24:15 +01:00
|
|
|
inflater->state = NGHTTP2_HD_STATE_CHECK_VALUELEN;
|
2013-10-24 14:52:02 +02:00
|
|
|
}
|
2014-01-25 10:24:15 +01:00
|
|
|
break;
|
2014-04-01 18:25:44 +02:00
|
|
|
}
|
2014-01-25 10:24:15 +01:00
|
|
|
case NGHTTP2_HD_STATE_NEWNAME_CHECK_NAMELEN:
|
|
|
|
hd_inflate_set_huffman_encoded(inflater, in);
|
|
|
|
inflater->state = NGHTTP2_HD_STATE_NEWNAME_READ_NAMELEN;
|
|
|
|
inflater->left = 0;
|
2014-07-21 18:50:29 +02:00
|
|
|
inflater->shift = 0;
|
2014-03-19 16:27:39 +01:00
|
|
|
DEBUGF(fprintf(stderr, "inflatehd: huffman encoded=%d\n",
|
2014-01-25 10:24:15 +01:00
|
|
|
inflater->huffman_encoded != 0));
|
|
|
|
/* Fall through */
|
|
|
|
case NGHTTP2_HD_STATE_NEWNAME_READ_NAMELEN:
|
|
|
|
rfin = 0;
|
|
|
|
rv = hd_inflate_read_len(inflater, &rfin, in, last, 7,
|
2014-05-28 16:02:23 +02:00
|
|
|
NGHTTP2_HD_MAX_NV);
|
2014-01-25 10:24:15 +01:00
|
|
|
if(rv < 0) {
|
2013-07-25 18:34:28 +02:00
|
|
|
goto fail;
|
2013-07-19 09:50:31 +02:00
|
|
|
}
|
2014-01-25 10:24:15 +01:00
|
|
|
in += rv;
|
|
|
|
if(!rfin) {
|
2014-03-19 16:27:39 +01:00
|
|
|
DEBUGF(fprintf(stderr,
|
2014-07-22 15:38:18 +02:00
|
|
|
"inflatehd: integer not fully decoded. current=%zu\n",
|
2014-01-25 10:24:15 +01:00
|
|
|
inflater->left));
|
2014-03-13 16:51:26 +01:00
|
|
|
|
|
|
|
goto almost_ok;
|
2013-07-19 09:50:31 +02:00
|
|
|
}
|
2014-03-11 13:18:28 +01:00
|
|
|
|
2014-01-25 10:24:15 +01:00
|
|
|
if(inflater->huffman_encoded) {
|
2014-02-13 15:22:52 +01:00
|
|
|
nghttp2_hd_huff_decode_context_init(&inflater->huff_decode_ctx);
|
2014-03-11 13:18:28 +01:00
|
|
|
|
2014-01-25 10:24:15 +01:00
|
|
|
inflater->state = NGHTTP2_HD_STATE_NEWNAME_READ_NAMEHUFF;
|
2013-10-20 16:59:15 +02:00
|
|
|
} else {
|
2014-01-25 10:24:15 +01:00
|
|
|
inflater->state = NGHTTP2_HD_STATE_NEWNAME_READ_NAME;
|
2013-08-25 10:39:29 +02:00
|
|
|
}
|
2014-01-25 10:24:15 +01:00
|
|
|
break;
|
|
|
|
case NGHTTP2_HD_STATE_NEWNAME_READ_NAMEHUFF:
|
2014-05-28 16:02:23 +02:00
|
|
|
rv = hd_inflate_read_huff(inflater, &inflater->nvbufs, in, last);
|
2014-01-25 10:24:15 +01:00
|
|
|
if(rv < 0) {
|
2013-10-20 16:59:15 +02:00
|
|
|
goto fail;
|
|
|
|
}
|
2014-03-11 13:18:28 +01:00
|
|
|
|
2014-01-25 10:24:15 +01:00
|
|
|
in += rv;
|
2014-03-11 13:18:28 +01:00
|
|
|
|
2014-03-19 16:27:39 +01:00
|
|
|
DEBUGF(fprintf(stderr, "inflatehd: %zd bytes read\n", rv));
|
2014-03-11 13:18:28 +01:00
|
|
|
|
2014-01-25 10:24:15 +01:00
|
|
|
if(inflater->left) {
|
2014-03-19 16:27:39 +01:00
|
|
|
DEBUGF(fprintf(stderr,
|
2014-07-22 15:38:18 +02:00
|
|
|
"inflatehd: still %zu bytes to go\n", inflater->left));
|
2014-03-11 13:18:28 +01:00
|
|
|
|
2014-03-13 16:51:26 +01:00
|
|
|
goto almost_ok;
|
2014-01-25 10:24:15 +01:00
|
|
|
}
|
2014-03-11 13:18:28 +01:00
|
|
|
|
2014-05-28 16:02:23 +02:00
|
|
|
inflater->newnamelen = nghttp2_bufs_len(&inflater->nvbufs);
|
|
|
|
|
2014-01-25 10:24:15 +01:00
|
|
|
inflater->state = NGHTTP2_HD_STATE_CHECK_VALUELEN;
|
2014-03-11 13:18:28 +01:00
|
|
|
|
2014-01-25 10:24:15 +01:00
|
|
|
break;
|
|
|
|
case NGHTTP2_HD_STATE_NEWNAME_READ_NAME:
|
2014-05-28 16:02:23 +02:00
|
|
|
rv = hd_inflate_read(inflater, &inflater->nvbufs, in, last);
|
2014-01-25 10:24:15 +01:00
|
|
|
if(rv < 0) {
|
2013-07-25 18:34:28 +02:00
|
|
|
goto fail;
|
2013-07-19 09:50:31 +02:00
|
|
|
}
|
2014-03-11 13:18:28 +01:00
|
|
|
|
2014-01-25 10:24:15 +01:00
|
|
|
in += rv;
|
2014-03-11 13:18:28 +01:00
|
|
|
|
2014-03-19 16:27:39 +01:00
|
|
|
DEBUGF(fprintf(stderr, "inflatehd: %zd bytes read\n", rv));
|
2014-01-25 10:24:15 +01:00
|
|
|
if(inflater->left) {
|
2014-03-19 16:27:39 +01:00
|
|
|
DEBUGF(fprintf(stderr,
|
2014-07-22 15:38:18 +02:00
|
|
|
"inflatehd: still %zu bytes to go\n", inflater->left));
|
2014-03-11 13:18:28 +01:00
|
|
|
|
2014-03-13 16:51:26 +01:00
|
|
|
goto almost_ok;
|
2013-10-14 16:38:12 +02:00
|
|
|
}
|
2014-03-11 13:18:28 +01:00
|
|
|
|
2014-05-28 16:02:23 +02:00
|
|
|
inflater->newnamelen = nghttp2_bufs_len(&inflater->nvbufs);
|
|
|
|
|
2014-01-25 10:24:15 +01:00
|
|
|
inflater->state = NGHTTP2_HD_STATE_CHECK_VALUELEN;
|
2014-03-11 13:18:28 +01:00
|
|
|
|
2014-01-25 10:24:15 +01:00
|
|
|
break;
|
|
|
|
case NGHTTP2_HD_STATE_CHECK_VALUELEN:
|
|
|
|
hd_inflate_set_huffman_encoded(inflater, in);
|
|
|
|
inflater->state = NGHTTP2_HD_STATE_READ_VALUELEN;
|
|
|
|
inflater->left = 0;
|
2014-07-21 18:50:29 +02:00
|
|
|
inflater->shift = 0;
|
2014-03-19 16:27:39 +01:00
|
|
|
DEBUGF(fprintf(stderr, "inflatehd: huffman encoded=%d\n",
|
2014-01-25 10:24:15 +01:00
|
|
|
inflater->huffman_encoded != 0));
|
|
|
|
/* Fall through */
|
|
|
|
case NGHTTP2_HD_STATE_READ_VALUELEN:
|
|
|
|
rfin = 0;
|
|
|
|
rv = hd_inflate_read_len(inflater, &rfin, in, last, 7,
|
2014-05-28 16:02:23 +02:00
|
|
|
NGHTTP2_HD_MAX_NV);
|
2014-01-25 10:24:15 +01:00
|
|
|
if(rv < 0) {
|
|
|
|
goto fail;
|
|
|
|
}
|
2014-03-11 13:18:28 +01:00
|
|
|
|
2014-01-25 10:24:15 +01:00
|
|
|
in += rv;
|
2014-03-11 13:18:28 +01:00
|
|
|
|
2014-01-25 10:24:15 +01:00
|
|
|
if(!rfin) {
|
2014-03-13 16:51:26 +01:00
|
|
|
goto almost_ok;
|
2014-01-25 10:24:15 +01:00
|
|
|
}
|
2014-03-11 13:18:28 +01:00
|
|
|
|
2014-07-22 15:38:18 +02:00
|
|
|
DEBUGF(fprintf(stderr, "inflatehd: valuelen=%zu\n", inflater->left));
|
2014-01-25 10:24:15 +01:00
|
|
|
if(inflater->left == 0) {
|
|
|
|
if(inflater->opcode == NGHTTP2_HD_OPCODE_NEWNAME) {
|
|
|
|
rv = hd_inflate_commit_newname(inflater, nv_out);
|
|
|
|
} else {
|
|
|
|
rv = hd_inflate_commit_indname(inflater, nv_out);
|
2013-10-20 16:59:15 +02:00
|
|
|
}
|
2014-01-25 10:24:15 +01:00
|
|
|
if(rv != 0) {
|
|
|
|
goto fail;
|
2013-10-20 16:59:15 +02:00
|
|
|
}
|
2014-01-25 10:24:15 +01:00
|
|
|
inflater->state = NGHTTP2_HD_STATE_OPCODE;
|
|
|
|
*inflate_flags |= NGHTTP2_HD_INFLATE_EMIT;
|
2014-06-16 19:17:49 +02:00
|
|
|
return (ssize_t)(in - first);
|
2014-01-25 10:24:15 +01:00
|
|
|
}
|
2014-03-11 13:18:28 +01:00
|
|
|
|
2014-01-25 10:24:15 +01:00
|
|
|
if(inflater->huffman_encoded) {
|
2014-02-13 15:22:52 +01:00
|
|
|
nghttp2_hd_huff_decode_context_init(&inflater->huff_decode_ctx);
|
2014-03-11 13:18:28 +01:00
|
|
|
|
2014-01-25 10:24:15 +01:00
|
|
|
inflater->state = NGHTTP2_HD_STATE_READ_VALUEHUFF;
|
2013-07-19 09:50:31 +02:00
|
|
|
} else {
|
2014-01-25 10:24:15 +01:00
|
|
|
inflater->state = NGHTTP2_HD_STATE_READ_VALUE;
|
2013-07-19 09:50:31 +02:00
|
|
|
}
|
2014-01-25 10:24:15 +01:00
|
|
|
break;
|
|
|
|
case NGHTTP2_HD_STATE_READ_VALUEHUFF:
|
2014-05-28 16:02:23 +02:00
|
|
|
rv = hd_inflate_read_huff(inflater, &inflater->nvbufs, in, last);
|
2014-01-25 10:24:15 +01:00
|
|
|
if(rv < 0) {
|
2013-07-25 18:34:28 +02:00
|
|
|
goto fail;
|
2013-07-19 09:50:31 +02:00
|
|
|
}
|
2014-03-11 13:18:28 +01:00
|
|
|
|
2014-01-25 10:24:15 +01:00
|
|
|
in += rv;
|
2014-03-11 13:18:28 +01:00
|
|
|
|
2014-03-19 16:27:39 +01:00
|
|
|
DEBUGF(fprintf(stderr, "inflatehd: %zd bytes read\n", rv));
|
2014-03-11 13:18:28 +01:00
|
|
|
|
2014-01-25 10:24:15 +01:00
|
|
|
if(inflater->left) {
|
2014-03-19 16:27:39 +01:00
|
|
|
DEBUGF(fprintf(stderr,
|
2014-07-22 15:38:18 +02:00
|
|
|
"inflatehd: still %zu bytes to go\n", inflater->left));
|
2014-03-11 13:18:28 +01:00
|
|
|
|
2014-03-13 16:51:26 +01:00
|
|
|
goto almost_ok;
|
2013-07-19 09:50:31 +02:00
|
|
|
}
|
2014-03-11 13:18:28 +01:00
|
|
|
|
2014-01-25 10:24:15 +01:00
|
|
|
if(inflater->opcode == NGHTTP2_HD_OPCODE_NEWNAME) {
|
|
|
|
rv = hd_inflate_commit_newname(inflater, nv_out);
|
|
|
|
} else {
|
|
|
|
rv = hd_inflate_commit_indname(inflater, nv_out);
|
2013-07-19 09:50:31 +02:00
|
|
|
}
|
2014-03-11 13:18:28 +01:00
|
|
|
|
2014-01-25 10:24:15 +01:00
|
|
|
if(rv != 0) {
|
2013-07-25 18:34:28 +02:00
|
|
|
goto fail;
|
2013-07-19 09:50:31 +02:00
|
|
|
}
|
2014-03-11 13:18:28 +01:00
|
|
|
|
2014-01-25 10:24:15 +01:00
|
|
|
inflater->state = NGHTTP2_HD_STATE_OPCODE;
|
|
|
|
*inflate_flags |= NGHTTP2_HD_INFLATE_EMIT;
|
2014-03-11 13:18:28 +01:00
|
|
|
|
2014-06-16 19:17:49 +02:00
|
|
|
return (ssize_t)(in - first);
|
2014-01-25 10:24:15 +01:00
|
|
|
case NGHTTP2_HD_STATE_READ_VALUE:
|
2014-05-28 16:02:23 +02:00
|
|
|
rv = hd_inflate_read(inflater, &inflater->nvbufs, in, last);
|
2014-01-25 10:24:15 +01:00
|
|
|
if(rv < 0) {
|
2014-03-19 16:27:39 +01:00
|
|
|
DEBUGF(fprintf(stderr, "inflatehd: value read failure %zd: %s\n",
|
2014-06-11 16:37:16 +02:00
|
|
|
rv, nghttp2_strerror((int)rv)));
|
2013-10-14 16:38:12 +02:00
|
|
|
goto fail;
|
|
|
|
}
|
2014-03-11 13:18:28 +01:00
|
|
|
|
2014-01-25 10:24:15 +01:00
|
|
|
in += rv;
|
2014-03-11 13:18:28 +01:00
|
|
|
|
2014-03-19 16:27:39 +01:00
|
|
|
DEBUGF(fprintf(stderr, "inflatehd: %zd bytes read\n", rv));
|
2014-03-11 13:18:28 +01:00
|
|
|
|
2014-01-25 10:24:15 +01:00
|
|
|
if(inflater->left) {
|
2014-03-19 16:27:39 +01:00
|
|
|
DEBUGF(fprintf(stderr,
|
2014-07-22 15:38:18 +02:00
|
|
|
"inflatehd: still %zu bytes to go\n", inflater->left));
|
2014-03-13 16:51:26 +01:00
|
|
|
goto almost_ok;
|
2014-01-25 10:24:15 +01:00
|
|
|
}
|
2014-03-11 13:18:28 +01:00
|
|
|
|
2014-01-25 10:24:15 +01:00
|
|
|
if(inflater->opcode == NGHTTP2_HD_OPCODE_NEWNAME) {
|
|
|
|
rv = hd_inflate_commit_newname(inflater, nv_out);
|
2013-07-19 09:50:31 +02:00
|
|
|
} else {
|
2014-01-25 10:24:15 +01:00
|
|
|
rv = hd_inflate_commit_indname(inflater, nv_out);
|
2013-07-19 09:50:31 +02:00
|
|
|
}
|
2014-03-11 13:18:28 +01:00
|
|
|
|
2013-08-28 14:33:57 +02:00
|
|
|
if(rv != 0) {
|
2013-07-25 18:34:28 +02:00
|
|
|
goto fail;
|
2013-07-19 09:50:31 +02:00
|
|
|
}
|
2014-03-11 13:18:28 +01:00
|
|
|
|
2014-01-25 10:24:15 +01:00
|
|
|
inflater->state = NGHTTP2_HD_STATE_OPCODE;
|
|
|
|
*inflate_flags |= NGHTTP2_HD_INFLATE_EMIT;
|
2014-03-11 13:18:28 +01:00
|
|
|
|
2014-06-16 19:17:49 +02:00
|
|
|
return (ssize_t)(in - first);
|
2013-07-19 09:50:31 +02:00
|
|
|
}
|
|
|
|
}
|
2014-03-19 16:27:39 +01:00
|
|
|
|
2014-01-25 10:24:15 +01:00
|
|
|
assert(in == last);
|
2014-03-19 16:27:39 +01:00
|
|
|
|
|
|
|
DEBUGF(fprintf(stderr, "inflatehd: all input bytes were processed\n"));
|
|
|
|
|
2014-01-25 10:24:15 +01:00
|
|
|
if(in_final) {
|
2014-03-19 16:27:39 +01:00
|
|
|
DEBUGF(fprintf(stderr, "inflatehd: in_final set\n"));
|
|
|
|
|
2014-02-11 08:00:59 +01:00
|
|
|
if(inflater->state != NGHTTP2_HD_STATE_OPCODE) {
|
2014-03-19 16:27:39 +01:00
|
|
|
DEBUGF(fprintf(stderr, "inflatehd: unacceptable state=%d\n",
|
|
|
|
inflater->state));
|
2014-02-11 08:00:59 +01:00
|
|
|
rv = NGHTTP2_ERR_HEADER_COMP;
|
2014-03-19 16:27:39 +01:00
|
|
|
|
2014-02-11 08:00:59 +01:00
|
|
|
goto fail;
|
|
|
|
}
|
2014-01-25 10:24:15 +01:00
|
|
|
*inflate_flags |= NGHTTP2_HD_INFLATE_FINAL;
|
2013-10-12 14:49:01 +02:00
|
|
|
}
|
2014-06-16 19:17:49 +02:00
|
|
|
return (ssize_t)(in - first);
|
2014-03-13 16:51:26 +01:00
|
|
|
|
|
|
|
almost_ok:
|
|
|
|
if(in_final && inflater->state != NGHTTP2_HD_STATE_OPCODE) {
|
2014-03-19 16:27:39 +01:00
|
|
|
DEBUGF(fprintf(stderr, "inflatehd: input ended prematurely\n"));
|
|
|
|
|
2014-03-13 16:51:26 +01:00
|
|
|
rv = NGHTTP2_ERR_HEADER_COMP;
|
2014-03-19 16:27:39 +01:00
|
|
|
|
2014-03-13 16:51:26 +01:00
|
|
|
goto fail;
|
|
|
|
}
|
2014-08-02 15:42:55 +02:00
|
|
|
return (ssize_t)(in - first);
|
2014-03-13 16:51:26 +01:00
|
|
|
|
2013-07-25 18:34:28 +02:00
|
|
|
fail:
|
2014-03-19 16:27:39 +01:00
|
|
|
DEBUGF(fprintf(stderr, "inflatehd: error return %zd\n", rv));
|
|
|
|
|
2014-01-26 09:53:04 +01:00
|
|
|
inflater->ctx.bad = 1;
|
2013-07-25 18:34:28 +02:00
|
|
|
return rv;
|
2013-07-19 09:50:31 +02:00
|
|
|
}
|
|
|
|
|
2014-01-26 09:53:04 +01:00
|
|
|
int nghttp2_hd_inflate_end_headers(nghttp2_hd_inflater *inflater)
|
2013-07-19 09:50:31 +02:00
|
|
|
{
|
2014-01-16 15:41:13 +01:00
|
|
|
hd_inflate_keep_free(inflater);
|
2013-07-19 09:50:31 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-04-29 08:53:46 +02:00
|
|
|
int nghttp2_hd_inflate_new(nghttp2_hd_inflater **inflater_ptr)
|
|
|
|
{
|
2014-06-25 14:26:47 +02:00
|
|
|
int rv;
|
|
|
|
nghttp2_hd_inflater *inflater;
|
|
|
|
|
|
|
|
inflater = malloc(sizeof(nghttp2_hd_inflater));
|
2014-04-29 08:53:46 +02:00
|
|
|
|
2014-06-25 14:26:47 +02:00
|
|
|
if(inflater == NULL) {
|
2014-04-30 15:16:21 +02:00
|
|
|
return NGHTTP2_ERR_NOMEM;
|
2014-04-29 08:53:46 +02:00
|
|
|
}
|
|
|
|
|
2014-06-25 14:26:47 +02:00
|
|
|
rv = nghttp2_hd_inflate_init(inflater);
|
|
|
|
|
|
|
|
if(rv != 0) {
|
2014-07-17 17:21:15 +02:00
|
|
|
free(inflater);
|
|
|
|
|
2014-06-25 14:26:47 +02:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
*inflater_ptr = inflater;
|
|
|
|
|
|
|
|
return 0;
|
2014-04-29 08:53:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void nghttp2_hd_inflate_del(nghttp2_hd_inflater *inflater)
|
|
|
|
{
|
|
|
|
nghttp2_hd_inflate_free(inflater);
|
|
|
|
|
|
|
|
free(inflater);
|
|
|
|
}
|
|
|
|
|
2014-05-27 22:28:12 +02:00
|
|
|
int nghttp2_hd_emit_indname_block(nghttp2_bufs *bufs, size_t idx,
|
2014-04-01 18:25:44 +02:00
|
|
|
nghttp2_nv *nv, int inc_indexing)
|
2013-07-19 09:50:31 +02:00
|
|
|
{
|
2014-04-01 18:25:44 +02:00
|
|
|
|
2014-05-27 22:28:12 +02:00
|
|
|
return emit_indname_block(bufs, idx, nv, inc_indexing);
|
2013-07-19 09:50:31 +02:00
|
|
|
}
|
|
|
|
|
2014-03-13 14:11:02 +01:00
|
|
|
int nghttp2_hd_emit_newname_block(nghttp2_bufs *bufs, nghttp2_nv *nv,
|
2014-02-13 15:22:52 +01:00
|
|
|
int inc_indexing)
|
|
|
|
{
|
2014-03-13 14:11:02 +01:00
|
|
|
return emit_newname_block(bufs, nv, inc_indexing);
|
2014-02-13 15:22:52 +01:00
|
|
|
}
|
|
|
|
|
2014-03-13 14:11:02 +01:00
|
|
|
int nghttp2_hd_emit_table_size(nghttp2_bufs *bufs, size_t table_size)
|
2013-07-19 09:50:31 +02:00
|
|
|
{
|
2014-03-13 14:11:02 +01:00
|
|
|
return emit_table_size(bufs, table_size);
|
2013-07-19 09:50:31 +02:00
|
|
|
}
|
2014-07-22 15:38:18 +02:00
|
|
|
|
|
|
|
ssize_t nghttp2_hd_decode_length(uint32_t *res, size_t *shift_ptr, int *final,
|
|
|
|
uint32_t initial, size_t shift,
|
|
|
|
uint8_t *in, uint8_t *last, size_t prefix)
|
|
|
|
{
|
|
|
|
return decode_length(res, shift_ptr, final, initial, shift, in, last,
|
|
|
|
prefix);
|
|
|
|
}
|