Implement static Huffman for header compression
The current implementation uses Huffman code tables described in http://tools.ietf.org/html/draft-rpeon-httpbis-header-compression-03
This commit is contained in:
parent
5b1fc35e82
commit
50cdcca911
|
@ -37,7 +37,8 @@ OBJECTS = nghttp2_pq.c nghttp2_map.c nghttp2_queue.c \
|
|||
nghttp2_session.c nghttp2_submit.c \
|
||||
nghttp2_helper.c \
|
||||
nghttp2_npn.c nghttp2_gzip.c \
|
||||
nghttp2_hd.c nghttp2_version.c
|
||||
nghttp2_hd.c nghttp2_hd_huffman.c nghttp2_hd_huffman_data.c \
|
||||
nghttp2_version.c
|
||||
|
||||
HFILES = nghttp2_pq.h nghttp2_int.h nghttp2_map.h nghttp2_queue.h \
|
||||
nghttp2_buffer.h nghttp2_frame.h \
|
||||
|
@ -45,7 +46,7 @@ HFILES = nghttp2_pq.h nghttp2_int.h nghttp2_map.h nghttp2_queue.h \
|
|||
nghttp2_npn.h nghttp2_gzip.h \
|
||||
nghttp2_submit.h nghttp2_outbound_item.h \
|
||||
nghttp2_net.h \
|
||||
nghttp2_hd.h
|
||||
nghttp2_hd.h nghttp2_hd_huffman.h
|
||||
|
||||
libnghttp2_la_SOURCES = $(HFILES) $(OBJECTS)
|
||||
libnghttp2_la_LDFLAGS = -no-undefined \
|
||||
|
|
230
lib/nghttp2_hd.c
230
lib/nghttp2_hd.c
|
@ -26,7 +26,6 @@
|
|||
|
||||
#include <string.h>
|
||||
#include <assert.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "nghttp2_frame.h"
|
||||
#include "nghttp2_helper.h"
|
||||
|
@ -107,7 +106,8 @@ int nghttp2_hd_entry_init(nghttp2_hd_entry *ent, uint8_t flags,
|
|||
uint8_t *value, uint16_t valuelen)
|
||||
{
|
||||
int rv = 0;
|
||||
if(flags & NGHTTP2_HD_FLAG_NAME_ALLOC) {
|
||||
if((flags & NGHTTP2_HD_FLAG_NAME_ALLOC) &&
|
||||
(flags & NGHTTP2_HD_FLAG_NAME_GIFT) == 0) {
|
||||
if(namelen == 0) {
|
||||
/* We should not allow empty header field name */
|
||||
ent->nv.name = NULL;
|
||||
|
@ -121,7 +121,8 @@ int nghttp2_hd_entry_init(nghttp2_hd_entry *ent, uint8_t flags,
|
|||
} else {
|
||||
ent->nv.name = name;
|
||||
}
|
||||
if(flags & NGHTTP2_HD_FLAG_VALUE_ALLOC) {
|
||||
if((flags & NGHTTP2_HD_FLAG_VALUE_ALLOC) &&
|
||||
(flags & NGHTTP2_HD_FLAG_VALUE_GIFT) == 0) {
|
||||
if(valuelen == 0) {
|
||||
ent->nv.value = NULL;
|
||||
} else {
|
||||
|
@ -216,9 +217,10 @@ static int nghttp2_hd_context_init(nghttp2_hd_context *context,
|
|||
nghttp2_hd_side side,
|
||||
size_t hd_table_bufsize_max)
|
||||
{
|
||||
int i;
|
||||
size_t i;
|
||||
int rv;
|
||||
context->role = role;
|
||||
context->side = side;
|
||||
context->bad = 0;
|
||||
context->hd_table_bufsize_max = hd_table_bufsize_max;
|
||||
rv = nghttp2_hd_ringbuf_init(&context->hd_table,
|
||||
|
@ -227,33 +229,21 @@ static int nghttp2_hd_context_init(nghttp2_hd_context *context,
|
|||
return rv;
|
||||
}
|
||||
for(i = 0; static_table[i]; i += 2);
|
||||
/* TODO handle nomem */
|
||||
context->static_hd_table = malloc(sizeof(nghttp2_hd_entry*)*(i / 2 + 1));
|
||||
context->static_hd_table[i / 2] = NULL;
|
||||
|
||||
if(role == NGHTTP2_HD_ROLE_INFLATE) {
|
||||
context->emit_set = malloc(sizeof(nghttp2_hd_entry*)*
|
||||
NGHTTP2_INITIAL_EMIT_SET_SIZE);
|
||||
if(context->emit_set == NULL) {
|
||||
nghttp2_hd_ringbuf_free(&context->hd_table);
|
||||
return NGHTTP2_ERR_NOMEM;
|
||||
}
|
||||
memset(context->emit_set, 0, sizeof(nghttp2_hd_entry*)*
|
||||
NGHTTP2_INITIAL_EMIT_SET_SIZE);
|
||||
context->emit_set_capacity = NGHTTP2_INITIAL_EMIT_SET_SIZE;
|
||||
} else {
|
||||
context->emit_set = NULL;
|
||||
context->emit_set_capacity = 0;
|
||||
if(context->static_hd_table == NULL) {
|
||||
goto fail;
|
||||
}
|
||||
context->emit_setlen = 0;
|
||||
|
||||
context->hd_table_bufsize = 0;
|
||||
context->static_hd_table[i / 2] = NULL;
|
||||
for(i = 0; static_table[i]; i += 2) {
|
||||
nghttp2_hd_entry *p = malloc(sizeof(nghttp2_hd_entry));
|
||||
if(p == NULL) {
|
||||
nghttp2_hd_ringbuf_free(&context->hd_table);
|
||||
free(context->emit_set);
|
||||
return NGHTTP2_ERR_NOMEM;
|
||||
size_t j;
|
||||
for(j = 0; j < i / 2; ++j) {
|
||||
--context->static_hd_table[j]->ref;
|
||||
nghttp2_hd_entry_free(context->static_hd_table[j]);
|
||||
free(context->static_hd_table[j]);
|
||||
}
|
||||
goto fail;
|
||||
}
|
||||
nghttp2_hd_entry_init(p, NGHTTP2_HD_FLAG_NONE,
|
||||
(uint8_t*)static_table[i], strlen(static_table[i]),
|
||||
|
@ -261,7 +251,39 @@ static int nghttp2_hd_context_init(nghttp2_hd_context *context,
|
|||
strlen(static_table[i+1]));
|
||||
context->static_hd_table[i / 2] = p;
|
||||
}
|
||||
|
||||
if(role == NGHTTP2_HD_ROLE_INFLATE) {
|
||||
context->emit_set = malloc(sizeof(nghttp2_hd_entry*)*
|
||||
NGHTTP2_INITIAL_EMIT_SET_SIZE);
|
||||
if(context->emit_set == NULL) {
|
||||
goto fail;
|
||||
}
|
||||
memset(context->emit_set, 0, sizeof(nghttp2_hd_entry*)*
|
||||
NGHTTP2_INITIAL_EMIT_SET_SIZE);
|
||||
context->emit_set_capacity = NGHTTP2_INITIAL_EMIT_SET_SIZE;
|
||||
|
||||
context->buf_track = malloc(sizeof(uint8_t*)*
|
||||
NGHTTP2_INITIAL_BUF_TRACK_SIZE);
|
||||
if(context->buf_track == NULL) {
|
||||
goto fail;
|
||||
}
|
||||
context->buf_track_capacity = NGHTTP2_INITIAL_BUF_TRACK_SIZE;
|
||||
} else {
|
||||
context->emit_set = NULL;
|
||||
context->emit_set_capacity = 0;
|
||||
context->buf_track = NULL;
|
||||
context->buf_track_capacity = 0;
|
||||
}
|
||||
context->emit_setlen = 0;
|
||||
context->buf_tracklen = 0;
|
||||
context->hd_table_bufsize = 0;
|
||||
return 0;
|
||||
fail:
|
||||
free(context->static_hd_table);
|
||||
free(context->buf_track);
|
||||
free(context->emit_set);
|
||||
nghttp2_hd_ringbuf_free(&context->hd_table);
|
||||
return NGHTTP2_ERR_NOMEM;
|
||||
}
|
||||
|
||||
int nghttp2_hd_deflate_init(nghttp2_hd_context *deflater, nghttp2_hd_side side)
|
||||
|
@ -295,6 +317,11 @@ int nghttp2_hd_inflate_init2(nghttp2_hd_context *inflater,
|
|||
static void nghttp2_hd_context_free(nghttp2_hd_context *context)
|
||||
{
|
||||
size_t i;
|
||||
for(i = 0; i < context->buf_tracklen; ++i) {
|
||||
free(context->buf_track[i]);
|
||||
}
|
||||
free(context->buf_track);
|
||||
|
||||
for(i = 0; i < context->emit_setlen; ++i) {
|
||||
nghttp2_hd_entry *ent = context->emit_set[i];
|
||||
if(--ent->ref == 0) {
|
||||
|
@ -302,9 +329,16 @@ static void nghttp2_hd_context_free(nghttp2_hd_context *context)
|
|||
free(ent);
|
||||
}
|
||||
}
|
||||
nghttp2_hd_ringbuf_free(&context->hd_table);
|
||||
free(context->static_hd_table);
|
||||
free(context->emit_set);
|
||||
|
||||
nghttp2_hd_ringbuf_free(&context->hd_table);
|
||||
|
||||
for(i = 0; context->static_hd_table[i]; ++i) {
|
||||
--context->static_hd_table[i]->ref;
|
||||
nghttp2_hd_entry_free(context->static_hd_table[i]);
|
||||
free(context->static_hd_table[i]);
|
||||
}
|
||||
free(context->static_hd_table);
|
||||
}
|
||||
|
||||
void nghttp2_hd_deflate_free(nghttp2_hd_context *deflater)
|
||||
|
@ -344,6 +378,15 @@ static int add_nva(nghttp2_nva_out *nva_out_ptr,
|
|||
return 0;
|
||||
}
|
||||
|
||||
static int track_decode_buf(nghttp2_hd_context *context, uint8_t *buf)
|
||||
{
|
||||
if(context->buf_tracklen == context->buf_track_capacity) {
|
||||
return NGHTTP2_ERR_HEADER_COMP;
|
||||
}
|
||||
context->buf_track[context->buf_tracklen++] = buf;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int add_emit_set(nghttp2_hd_context *context, nghttp2_hd_entry *ent)
|
||||
{
|
||||
if(context->emit_setlen == context->emit_set_capacity) {
|
||||
|
@ -376,6 +419,15 @@ static int emit_newname_header(nghttp2_hd_context *context,
|
|||
nghttp2_nva_out *nva_out_ptr,
|
||||
nghttp2_nv *nv)
|
||||
{
|
||||
int rv;
|
||||
rv = track_decode_buf(context, nv->name);
|
||||
if(rv != 0) {
|
||||
return rv;
|
||||
}
|
||||
rv = track_decode_buf(context, nv->value);
|
||||
if(rv != 0) {
|
||||
return rv;
|
||||
}
|
||||
return add_nva(nva_out_ptr,
|
||||
nv->name, nv->namelen, nv->value, nv->valuelen);
|
||||
}
|
||||
|
@ -390,6 +442,10 @@ static int emit_indname_header(nghttp2_hd_context *context,
|
|||
if(rv != 0) {
|
||||
return rv;
|
||||
}
|
||||
rv = track_decode_buf(context, value);
|
||||
if(rv != 0) {
|
||||
return rv;
|
||||
}
|
||||
return add_nva(nva_out_ptr, ent->nv.name, ent->nv.namelen, value, valuelen);
|
||||
}
|
||||
|
||||
|
@ -527,47 +583,57 @@ static int emit_indexed_block(uint8_t **buf_ptr, size_t *buflen_ptr,
|
|||
static int emit_indname_block(uint8_t **buf_ptr, size_t *buflen_ptr,
|
||||
size_t *offset_ptr, size_t index,
|
||||
const uint8_t *value, size_t valuelen,
|
||||
int inc_indexing)
|
||||
int inc_indexing,
|
||||
nghttp2_hd_side side)
|
||||
{
|
||||
int rv;
|
||||
uint8_t *bufp;
|
||||
size_t encvallen = nghttp2_hd_huff_encode_count(value, valuelen, side);
|
||||
size_t blocklen = count_encoded_length(index + 1, 6) +
|
||||
count_encoded_length(valuelen, 0) + valuelen;
|
||||
count_encoded_length(encvallen, 0) + encvallen;
|
||||
rv = ensure_write_buffer(buf_ptr, buflen_ptr, *offset_ptr, blocklen);
|
||||
if(rv != 0) {
|
||||
return rv;
|
||||
}
|
||||
bufp = *buf_ptr + *offset_ptr;
|
||||
bufp += encode_length(bufp, index + 1, 6);
|
||||
bufp += encode_length(bufp, valuelen, 0);
|
||||
memcpy(bufp, value, valuelen);
|
||||
bufp += encode_length(bufp, encvallen, 0);
|
||||
nghttp2_hd_huff_encode(bufp, *buflen_ptr - (bufp - *buf_ptr),
|
||||
value, valuelen, side);
|
||||
if(!inc_indexing) {
|
||||
(*buf_ptr)[*offset_ptr] |= 0x40u;
|
||||
}
|
||||
assert(bufp+valuelen - (*buf_ptr + *offset_ptr) == (ssize_t)blocklen);
|
||||
assert(bufp+encvallen - (*buf_ptr + *offset_ptr) == (ssize_t)blocklen);
|
||||
*offset_ptr += blocklen;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int emit_newname_block(uint8_t **buf_ptr, size_t *buflen_ptr,
|
||||
size_t *offset_ptr, nghttp2_nv *nv,
|
||||
int inc_indexing)
|
||||
int inc_indexing,
|
||||
nghttp2_hd_side side)
|
||||
{
|
||||
int rv;
|
||||
uint8_t *bufp;
|
||||
size_t blocklen = 1 + count_encoded_length(nv->namelen, 0) + nv->namelen +
|
||||
count_encoded_length(nv->valuelen, 0) + nv->valuelen;
|
||||
size_t encnamelen =
|
||||
nghttp2_hd_huff_encode_count(nv->name, nv->namelen, side);
|
||||
size_t encvallen =
|
||||
nghttp2_hd_huff_encode_count(nv->value, nv->valuelen, side);
|
||||
size_t blocklen = 1 + count_encoded_length(encnamelen, 0) + encnamelen +
|
||||
count_encoded_length(encvallen, 0) + encvallen;
|
||||
rv = ensure_write_buffer(buf_ptr, buflen_ptr, *offset_ptr, blocklen);
|
||||
if(rv != 0) {
|
||||
return rv;
|
||||
}
|
||||
bufp = *buf_ptr + *offset_ptr;
|
||||
*bufp++ = inc_indexing ? 0 : 0x40u;
|
||||
bufp += encode_length(bufp, nv->namelen, 0);
|
||||
memcpy(bufp, nv->name, nv->namelen);
|
||||
bufp += nv->namelen;
|
||||
bufp += encode_length(bufp, nv->valuelen, 0);
|
||||
memcpy(bufp, nv->value, nv->valuelen);
|
||||
bufp += encode_length(bufp, encnamelen, 0);
|
||||
nghttp2_hd_huff_encode(bufp, *buflen_ptr - (bufp - *buf_ptr),
|
||||
nv->name, nv->namelen, side);
|
||||
bufp += encnamelen;
|
||||
bufp += encode_length(bufp, encvallen, 0);
|
||||
nghttp2_hd_huff_encode(bufp, *buflen_ptr - (bufp - *buf_ptr),
|
||||
nv->value, nv->valuelen, side);
|
||||
*offset_ptr += blocklen;
|
||||
return 0;
|
||||
}
|
||||
|
@ -773,10 +839,12 @@ static int deflate_nv(nghttp2_hd_context *deflater,
|
|||
incidx = 1;
|
||||
}
|
||||
if(index == -1) {
|
||||
rv = emit_newname_block(buf_ptr, buflen_ptr, offset_ptr, nv, incidx);
|
||||
rv = emit_newname_block(buf_ptr, buflen_ptr, offset_ptr, nv, incidx,
|
||||
deflater->side);
|
||||
} else {
|
||||
rv = emit_indname_block(buf_ptr, buflen_ptr, offset_ptr, index,
|
||||
nv->value, nv->valuelen, incidx);
|
||||
nv->value, nv->valuelen, incidx,
|
||||
deflater->side);
|
||||
}
|
||||
if(rv != 0) {
|
||||
return rv;
|
||||
|
@ -867,6 +935,21 @@ static int check_index_range(nghttp2_hd_context *context, size_t index)
|
|||
sizeof(static_table)/sizeof(static_table[0])/2;
|
||||
}
|
||||
|
||||
static ssize_t inflate_decode(uint8_t **dest_ptr, uint8_t *in, size_t inlen,
|
||||
nghttp2_hd_side side)
|
||||
{
|
||||
ssize_t declen = nghttp2_hd_huff_decode_count(in, inlen, side);
|
||||
if(declen == -1) {
|
||||
return NGHTTP2_ERR_HEADER_COMP;
|
||||
}
|
||||
*dest_ptr = malloc(declen);
|
||||
if(*dest_ptr == NULL) {
|
||||
return NGHTTP2_ERR_HEADER_COMP;
|
||||
}
|
||||
nghttp2_hd_huff_decode(*dest_ptr, declen, in, inlen, side);
|
||||
return declen;
|
||||
}
|
||||
|
||||
ssize_t nghttp2_hd_inflate_hd(nghttp2_hd_context *inflater,
|
||||
nghttp2_nv **nva_ptr,
|
||||
uint8_t *in, size_t inlen)
|
||||
|
@ -916,21 +999,32 @@ ssize_t nghttp2_hd_inflate_hd(nghttp2_hd_context *inflater,
|
|||
rv = NGHTTP2_ERR_HEADER_COMP;
|
||||
goto fail;
|
||||
}
|
||||
if(!nghttp2_check_header_name(in, namelen)) {
|
||||
rv = inflate_decode(&nv.name, in, namelen, inflater->side);
|
||||
if(rv < 0) {
|
||||
goto fail;
|
||||
}
|
||||
nv.namelen = rv;
|
||||
in += namelen;
|
||||
|
||||
if(!nghttp2_check_header_name(nv.name, nv.namelen)) {
|
||||
free(nv.name);
|
||||
rv = NGHTTP2_ERR_HEADER_COMP;
|
||||
goto fail;
|
||||
}
|
||||
nv.name = in;
|
||||
in += namelen;
|
||||
|
||||
in = decode_length(&valuelen, in, last, 0);
|
||||
if(valuelen < 0 || in + valuelen > last) {
|
||||
rv = NGHTTP2_ERR_HEADER_COMP;
|
||||
goto fail;
|
||||
}
|
||||
nv.namelen = namelen;
|
||||
nv.value = in;
|
||||
nv.valuelen = valuelen;
|
||||
rv = inflate_decode(&nv.value, in, valuelen, inflater->side);
|
||||
if(rv < 0) {
|
||||
free(nv.name);
|
||||
goto fail;
|
||||
}
|
||||
nv.valuelen = rv;
|
||||
in += valuelen;
|
||||
|
||||
nghttp2_downcase(nv.name, nv.namelen);
|
||||
if(c == 0x40u) {
|
||||
rv = emit_newname_header(inflater, &nva_out, &nv);
|
||||
|
@ -938,10 +1032,14 @@ ssize_t nghttp2_hd_inflate_hd(nghttp2_hd_context *inflater,
|
|||
nghttp2_hd_entry *new_ent;
|
||||
new_ent = add_hd_table_incremental(inflater, NULL, NULL, NULL, &nv,
|
||||
NGHTTP2_HD_FLAG_NAME_ALLOC |
|
||||
NGHTTP2_HD_FLAG_VALUE_ALLOC);
|
||||
NGHTTP2_HD_FLAG_VALUE_ALLOC |
|
||||
NGHTTP2_HD_FLAG_NAME_GIFT |
|
||||
NGHTTP2_HD_FLAG_VALUE_GIFT);
|
||||
if(new_ent) {
|
||||
rv = emit_indexed_header(inflater, &nva_out, new_ent);
|
||||
} else {
|
||||
free(nv.value);
|
||||
free(nv.name);
|
||||
rv = NGHTTP2_ERR_HEADER_COMP;
|
||||
}
|
||||
}
|
||||
|
@ -969,19 +1067,22 @@ ssize_t nghttp2_hd_inflate_hd(nghttp2_hd_context *inflater,
|
|||
rv = NGHTTP2_ERR_HEADER_COMP;
|
||||
goto fail;
|
||||
}
|
||||
value = in;
|
||||
rv = inflate_decode(&value, in, valuelen, inflater->side);
|
||||
if(rv < 0) {
|
||||
goto fail;
|
||||
}
|
||||
in += valuelen;
|
||||
valuelen = rv;
|
||||
if((c & 0x40u) == 0x40u) {
|
||||
rv = emit_indname_header(inflater, &nva_out, ent, value, valuelen);
|
||||
} else {
|
||||
nghttp2_nv nv;
|
||||
nghttp2_hd_entry *new_ent;
|
||||
uint8_t ent_flags = NGHTTP2_HD_FLAG_VALUE_ALLOC;
|
||||
uint8_t ent_flags = NGHTTP2_HD_FLAG_VALUE_GIFT |
|
||||
NGHTTP2_HD_FLAG_VALUE_ALLOC;
|
||||
++ent->ref;
|
||||
if(index >= inflater->hd_table.len) {
|
||||
nv.name = nghttp2_hd_table_get(inflater, index)->nv.name;
|
||||
} else {
|
||||
nv.name = ent->nv.name;
|
||||
nv.name = ent->nv.name;
|
||||
if((size_t)index < inflater->hd_table.len) {
|
||||
ent_flags |= NGHTTP2_HD_FLAG_NAME_ALLOC;
|
||||
}
|
||||
nv.namelen = ent->nv.namelen;
|
||||
|
@ -996,6 +1097,7 @@ ssize_t nghttp2_hd_inflate_hd(nghttp2_hd_context *inflater,
|
|||
if(new_ent) {
|
||||
rv = emit_indexed_header(inflater, &nva_out, new_ent);
|
||||
} else {
|
||||
free(nv.value);
|
||||
rv = NGHTTP2_ERR_HEADER_COMP;
|
||||
}
|
||||
}
|
||||
|
@ -1038,21 +1140,29 @@ int nghttp2_hd_end_headers(nghttp2_hd_context *context)
|
|||
}
|
||||
}
|
||||
context->emit_setlen = 0;
|
||||
for(i = 0; i < context->buf_tracklen; ++i) {
|
||||
free(context->buf_track[i]);
|
||||
}
|
||||
context->buf_tracklen = 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int nghttp2_hd_emit_indname_block(uint8_t **buf_ptr, size_t *buflen_ptr,
|
||||
size_t *offset_ptr, size_t index,
|
||||
const uint8_t *value, size_t valuelen,
|
||||
int inc_indexing)
|
||||
int inc_indexing,
|
||||
nghttp2_hd_side side)
|
||||
{
|
||||
return emit_indname_block(buf_ptr, buflen_ptr, offset_ptr,
|
||||
index, value, valuelen, inc_indexing);
|
||||
index, value, valuelen, inc_indexing,
|
||||
side);
|
||||
}
|
||||
|
||||
int nghttp2_hd_emit_newname_block(uint8_t **buf_ptr, size_t *buflen_ptr,
|
||||
size_t *offset_ptr, nghttp2_nv *nv,
|
||||
int inc_indexing)
|
||||
int inc_indexing,
|
||||
nghttp2_hd_side side)
|
||||
{
|
||||
return emit_newname_block(buf_ptr, buflen_ptr, offset_ptr, nv, inc_indexing);
|
||||
return emit_newname_block(buf_ptr, buflen_ptr, offset_ptr, nv, inc_indexing,
|
||||
side);
|
||||
}
|
||||
|
|
|
@ -32,6 +32,7 @@
|
|||
#include <nghttp2/nghttp2.h>
|
||||
|
||||
#define NGHTTP2_INITIAL_EMIT_SET_SIZE 128
|
||||
#define NGHTTP2_INITIAL_BUF_TRACK_SIZE 128
|
||||
|
||||
#define NGHTTP2_HD_DEFAULT_MAX_BUFFER_SIZE (1 << 12)
|
||||
#define NGHTTP2_HD_MAX_ENTRY_SIZE 3072
|
||||
|
@ -58,7 +59,13 @@ typedef enum {
|
|||
/* Indicates that the entry is emitted in the current header
|
||||
processing. */
|
||||
NGHTTP2_HD_FLAG_EMIT = 1 << 3,
|
||||
NGHTTP2_HD_FLAG_IMPLICIT_EMIT = 1 << 4
|
||||
NGHTTP2_HD_FLAG_IMPLICIT_EMIT = 1 << 4,
|
||||
/* Indicates that the name was gifted to the entry and no copying
|
||||
necessary. */
|
||||
NGHTTP2_HD_FLAG_NAME_GIFT = 1 << 5,
|
||||
/* Indicates that the value was gifted to the entry and no copying
|
||||
necessary. */
|
||||
NGHTTP2_HD_FLAG_VALUE_GIFT = 1 << 6
|
||||
} nghttp2_hd_flags;
|
||||
|
||||
typedef struct {
|
||||
|
@ -96,8 +103,18 @@ typedef struct {
|
|||
uint8_t bad;
|
||||
/* Role of this context; deflate or infalte */
|
||||
nghttp2_hd_role role;
|
||||
/* Huffman compression side: NGHTTP2_HD_SIDE_CLIENT uses huffman
|
||||
table for request. NGHTTP2_HD_SIDE_SERVER uses huffman table for
|
||||
response. */
|
||||
nghttp2_hd_side side;
|
||||
/* Maximum header table size */
|
||||
size_t hd_table_bufsize_max;
|
||||
/* Keep track of allocated buffers in inflation */
|
||||
uint8_t **buf_track;
|
||||
/* The capacity of |buf_track| */
|
||||
uint16_t buf_track_capacity;
|
||||
/* The number of entry the |buf_track| contains. */
|
||||
size_t buf_tracklen;
|
||||
} nghttp2_hd_context;
|
||||
|
||||
/*
|
||||
|
@ -227,12 +244,14 @@ int nghttp2_hd_end_headers(nghttp2_hd_context *deflater_or_inflater);
|
|||
int nghttp2_hd_emit_indname_block(uint8_t **buf_ptr, size_t *buflen_ptr,
|
||||
size_t *offset_ptr, size_t index,
|
||||
const uint8_t *value, size_t valuelen,
|
||||
int inc_indexing);
|
||||
int inc_indexing,
|
||||
nghttp2_hd_side side);
|
||||
|
||||
/* For unittesting purpose */
|
||||
int nghttp2_hd_emit_newname_block(uint8_t **buf_ptr, size_t *buflen_ptr,
|
||||
size_t *offset_ptr, nghttp2_nv *nv,
|
||||
int inc_indexing);
|
||||
int inc_indexing,
|
||||
nghttp2_hd_side side);
|
||||
|
||||
/* For unittesting purpose */
|
||||
int nghttp2_hd_emit_subst_indname_block(uint8_t **buf_ptr, size_t *buflen_ptr,
|
||||
|
@ -249,4 +268,68 @@ int nghttp2_hd_emit_subst_newname_block(uint8_t **buf_ptr, size_t *buflen_ptr,
|
|||
nghttp2_hd_entry* nghttp2_hd_table_get(nghttp2_hd_context *context,
|
||||
size_t index);
|
||||
|
||||
/* Huffman encoding/decoding functions */
|
||||
|
||||
/*
|
||||
* Counts the required bytes to encode |src| with length |len|. If
|
||||
* |side| is NGHTTP2_HD_SIDE_CLIENT, the request huffman code table is
|
||||
* used. Otherwise, the response code table is used.
|
||||
*
|
||||
* This function returns the number of required bytes to encode given
|
||||
* data, including terminal symbol code. This function always
|
||||
* succeeds.
|
||||
*/
|
||||
size_t nghttp2_hd_huff_encode_count(const uint8_t *src, size_t len,
|
||||
nghttp2_hd_side side);
|
||||
|
||||
/*
|
||||
* Encodes the given data |src| with length |srclen| to the given
|
||||
* memory location pointed by |dest|, allocated at lest |destlen|
|
||||
* bytes. The caller is responsible to specify |destlen| at least the
|
||||
* length that nghttp2_hd_huff_encode_count() returns. If |side| is
|
||||
* NGHTTP2_HD_SIDE_CLIENT, the request huffman code table is
|
||||
* used. Otherwise, the response code table is used.
|
||||
*
|
||||
* This function returns the number of written bytes, including
|
||||
* terminal symbol code. This return value is exactly the same with
|
||||
* the return value of nghttp2_hd_huff_encode_count() if it is given
|
||||
* with the same |src|, |srclen|, and |side|. This function always
|
||||
* succeeds.
|
||||
*/
|
||||
ssize_t nghttp2_hd_huff_encode(uint8_t *dest, size_t destlen,
|
||||
const uint8_t *src, size_t srclen,
|
||||
nghttp2_hd_side side);
|
||||
|
||||
/*
|
||||
* Counts the number of required bytes to decode |src| with length
|
||||
* |srclen|. The given input must be terminated with terminal code. If
|
||||
* |side| is NGHTTP2_HD_SIDE_CLIENT, the request huffman code table is
|
||||
* used. Otherwise, the response code table is used.
|
||||
*
|
||||
* This function returns the number of required bytes to decode given
|
||||
* data if it succeeds, or -1.
|
||||
*/
|
||||
ssize_t nghttp2_hd_huff_decode_count(const uint8_t *src, size_t srclen,
|
||||
nghttp2_hd_side side);
|
||||
|
||||
/*
|
||||
* Decodes the given data |src| with length |srclen| to the given
|
||||
* memory location pointed by |dest|, allocated at lest |destlen|
|
||||
* bytes. The given input must be terminated with terminal code. The
|
||||
* caller is responsible to specify |destlen| at least the length that
|
||||
* nghttp2_hd_huff_decode_count() returns. If |side| is
|
||||
* NGHTTP2_HD_SIDE_CLIENT, the request huffman code table is
|
||||
* used. Otherwise, the response code table is used.
|
||||
*
|
||||
* This function returns the number of written bytes. This return
|
||||
* value is exactly the same with the return value of
|
||||
* nghttp2_hd_huff_decode_count() if it is given with the same |src|,
|
||||
* |srclen|, and |side|.
|
||||
*
|
||||
* This function returns -1 if it fails.
|
||||
*/
|
||||
ssize_t nghttp2_hd_huff_decode(uint8_t *dest, size_t destlen,
|
||||
const uint8_t *src, size_t srclen,
|
||||
nghttp2_hd_side side);
|
||||
|
||||
#endif /* NGHTTP2_HD_COMP_H */
|
||||
|
|
|
@ -0,0 +1,254 @@
|
|||
/*
|
||||
* nghttp2 - HTTP/2.0 C Library
|
||||
*
|
||||
* 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_huffman.h"
|
||||
|
||||
#include <string.h>
|
||||
#include <assert.h>
|
||||
|
||||
#include "nghttp2_hd.h"
|
||||
|
||||
extern const nghttp2_huff_sym req_huff_sym_table[];
|
||||
extern const int16_t req_huff_decode_table[][256];
|
||||
|
||||
extern const nghttp2_huff_sym res_huff_sym_table[];
|
||||
extern const int16_t res_huff_decode_table[][256];
|
||||
|
||||
/*
|
||||
* Returns next 8 bits of data from |in|, starting |bitoff| bits
|
||||
* offset. If there are fewer bits left than |bitoff|, the left bits
|
||||
* with padded with 0 are returned. The |bitoff| must be strictly less
|
||||
* than 8.
|
||||
*/
|
||||
static uint8_t get_prefix_byte(const uint8_t *in, size_t len, size_t bitoff)
|
||||
{
|
||||
uint8_t b;
|
||||
size_t bitleft;
|
||||
if(bitoff == 0) {
|
||||
return *in;
|
||||
}
|
||||
bitleft = 8 - bitoff;
|
||||
b = (*in & ((1 << bitleft) - 1)) << bitoff;
|
||||
if(len > 1) {
|
||||
b |= *(in + 1) >> bitleft;
|
||||
}
|
||||
return b;
|
||||
}
|
||||
|
||||
/*
|
||||
* Decodes next byte from input |in| with length |len|, starting
|
||||
* |bitoff| bit offset.
|
||||
*
|
||||
* This function returns the decoded symbol number (0-255 and 256 for
|
||||
* special terminal symbol) if it succeeds, or -1.
|
||||
*/
|
||||
static int huff_decode(const uint8_t *in, size_t len, size_t bitoff,
|
||||
const nghttp2_huff_sym *huff_sym_table,
|
||||
const huff_decode_table_type *huff_decode_table)
|
||||
{
|
||||
int rv = 0;
|
||||
size_t len_orig = len;
|
||||
if(len == 0) {
|
||||
return -1;
|
||||
}
|
||||
for(;;) {
|
||||
rv = huff_decode_table[rv][get_prefix_byte(in, len, bitoff)];
|
||||
if(rv >= 0) {
|
||||
break;
|
||||
}
|
||||
/* Negative return value means we need to lookup next table. */
|
||||
rv = -rv;
|
||||
++in;
|
||||
--len;
|
||||
if(len == 0) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
if(bitoff + huff_sym_table[rv].nbits > len_orig * 8) {
|
||||
return -1;
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
|
||||
/*
|
||||
* Returns next LSB aligned |nbits| bits from huffman symbol |sym|,
|
||||
* starting |codebitoff| bit offset (from beginning of code sequence,
|
||||
* so it could be more than 8).
|
||||
*/
|
||||
static uint8_t huff_get_lsb_aligned(const nghttp2_huff_sym *sym,
|
||||
size_t codebitoff,
|
||||
size_t nbits)
|
||||
{
|
||||
uint8_t a = sym->code[codebitoff/8];
|
||||
size_t localbitoff = codebitoff & 0x7;
|
||||
size_t bitleft = 8 - localbitoff;
|
||||
|
||||
if(bitleft >= nbits) {
|
||||
return (a >> (bitleft - nbits)) & ((1 << nbits) - 1);
|
||||
} else {
|
||||
uint8_t b = 0;
|
||||
a &= ((1 << bitleft) - 1);
|
||||
a <<= nbits - bitleft;
|
||||
if((sym->nbits + 7) / 8 > codebitoff / 8 + 1) {
|
||||
b = sym->code[codebitoff / 8 + 1] >> (8 - (nbits - bitleft));
|
||||
}
|
||||
return a | b;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Encodes huffman code |sym| into |*dest_ptr|,starting |bitoff|
|
||||
* offset. The |bitoff| must be strictly less than 8. At the end of
|
||||
* the process, the |*dest_ptr| is updated and points where next
|
||||
* output should be placed. The bit offset of the pointed location is
|
||||
* returned.
|
||||
*/
|
||||
static size_t huff_encode_sym(uint8_t **dest_ptr, size_t bitoff,
|
||||
const nghttp2_huff_sym *sym)
|
||||
{
|
||||
size_t b = 0;
|
||||
**dest_ptr |= huff_get_lsb_aligned(sym, b, 8 - bitoff);
|
||||
b += 8 - bitoff;
|
||||
++*dest_ptr;
|
||||
for(; b < sym->nbits; b += 8, ++*dest_ptr) {
|
||||
**dest_ptr |= huff_get_lsb_aligned(sym, b, 8);
|
||||
}
|
||||
bitoff = 8 - (b - sym->nbits);
|
||||
if(bitoff > 0) {
|
||||
--*dest_ptr;
|
||||
}
|
||||
return bitoff;
|
||||
}
|
||||
|
||||
size_t nghttp2_hd_huff_encode_count(const uint8_t *src, size_t len,
|
||||
nghttp2_hd_side side)
|
||||
{
|
||||
size_t i;
|
||||
size_t nbits = 0;
|
||||
const nghttp2_huff_sym *huff_sym_table;
|
||||
|
||||
if(side == NGHTTP2_HD_SIDE_CLIENT) {
|
||||
huff_sym_table = req_huff_sym_table;
|
||||
} else {
|
||||
huff_sym_table = res_huff_sym_table;
|
||||
}
|
||||
for(i = 0; i < len; ++i) {
|
||||
nbits += huff_sym_table[src[i]].nbits;
|
||||
}
|
||||
/* 256 is special terminal symbol */
|
||||
return (nbits + huff_sym_table[256].nbits + 7) / 8;
|
||||
}
|
||||
|
||||
ssize_t nghttp2_hd_huff_encode(uint8_t *dest, size_t destlen,
|
||||
const uint8_t *src, size_t srclen,
|
||||
nghttp2_hd_side side)
|
||||
{
|
||||
int bitoff = 0;
|
||||
uint8_t *dest_first = dest;
|
||||
size_t i;
|
||||
const nghttp2_huff_sym *huff_sym_table;
|
||||
|
||||
if(side == NGHTTP2_HD_SIDE_CLIENT) {
|
||||
huff_sym_table = req_huff_sym_table;
|
||||
} else {
|
||||
huff_sym_table = res_huff_sym_table;
|
||||
}
|
||||
memset(dest, 0, destlen);
|
||||
for(i = 0; i < srclen; ++i) {
|
||||
const nghttp2_huff_sym *sym = &huff_sym_table[src[i]];
|
||||
bitoff = huff_encode_sym(&dest, bitoff, sym);
|
||||
}
|
||||
/* 256 is special terminal symbol */
|
||||
bitoff = huff_encode_sym(&dest, bitoff, &huff_sym_table[256]);
|
||||
return dest - dest_first + (bitoff > 0);
|
||||
}
|
||||
|
||||
ssize_t nghttp2_hd_huff_decode_count(const uint8_t *src, size_t srclen,
|
||||
nghttp2_hd_side side)
|
||||
{
|
||||
size_t bitoff = 0;
|
||||
size_t i, j;
|
||||
const nghttp2_huff_sym *huff_sym_table;
|
||||
const huff_decode_table_type *huff_decode_table;
|
||||
|
||||
if(side == NGHTTP2_HD_SIDE_CLIENT) {
|
||||
huff_sym_table = req_huff_sym_table;
|
||||
huff_decode_table = req_huff_decode_table;
|
||||
} else {
|
||||
huff_sym_table = res_huff_sym_table;
|
||||
huff_decode_table = res_huff_decode_table;
|
||||
}
|
||||
j = 0;
|
||||
for(i = 0; i < srclen;) {
|
||||
int rv = huff_decode(src + i, srclen - i, bitoff,
|
||||
huff_sym_table, huff_decode_table);
|
||||
if(rv == -1) {
|
||||
return -1;
|
||||
}
|
||||
if(rv == 256) {
|
||||
/* 256 is special terminal symbol */
|
||||
break;
|
||||
}
|
||||
j++;
|
||||
bitoff += huff_sym_table[rv].nbits;
|
||||
i += bitoff / 8;
|
||||
bitoff &= 0x7;
|
||||
}
|
||||
return j;
|
||||
}
|
||||
|
||||
ssize_t nghttp2_hd_huff_decode(uint8_t *dest, size_t destlen,
|
||||
const uint8_t *src, size_t srclen,
|
||||
nghttp2_hd_side side)
|
||||
{
|
||||
size_t bitoff = 0;
|
||||
size_t i, j;
|
||||
const nghttp2_huff_sym *huff_sym_table;
|
||||
const huff_decode_table_type *huff_decode_table;
|
||||
|
||||
if(side == NGHTTP2_HD_SIDE_CLIENT) {
|
||||
huff_sym_table = req_huff_sym_table;
|
||||
huff_decode_table = req_huff_decode_table;
|
||||
} else {
|
||||
huff_sym_table = res_huff_sym_table;
|
||||
huff_decode_table = res_huff_decode_table;
|
||||
}
|
||||
j = 0;
|
||||
for(i = 0; i < srclen;) {
|
||||
int rv = huff_decode(src + i, srclen - i, bitoff,
|
||||
huff_sym_table, huff_decode_table);
|
||||
if(rv == -1) {
|
||||
return -1;
|
||||
}
|
||||
if(rv == 256) {
|
||||
/* 256 is special terminal symbol */
|
||||
break;
|
||||
}
|
||||
dest[j++] = rv;
|
||||
bitoff += huff_sym_table[rv].nbits;
|
||||
i += bitoff / 8;
|
||||
bitoff &= 0x7;
|
||||
}
|
||||
return j;
|
||||
}
|
|
@ -0,0 +1,43 @@
|
|||
/*
|
||||
* nghttp2 - HTTP/2.0 C Library
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
#ifndef NGHTTP2_HD_HUFFMAN_H
|
||||
#define NGHTTP2_HD_HUFFMAN_H
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
# include <config.h>
|
||||
#endif /* HAVE_CONFIG_H */
|
||||
|
||||
#include <nghttp2/nghttp2.h>
|
||||
|
||||
typedef int16_t huff_decode_table_type[256];
|
||||
|
||||
typedef struct {
|
||||
/* The number of bits in this code */
|
||||
size_t nbits;
|
||||
/* Code sequence padded with 0 */
|
||||
uint8_t code[4];
|
||||
} nghttp2_huff_sym;
|
||||
|
||||
#endif // NGHTTP2_HD_HUFFMAN_H
|
File diff suppressed because it is too large
Load Diff
|
@ -246,7 +246,8 @@ void test_nghttp2_hd_inflate_indname_inc(void)
|
|||
nghttp2_hd_inflate_init(&inflater, NGHTTP2_HD_SIDE_SERVER);
|
||||
|
||||
CU_ASSERT(0 == nghttp2_hd_emit_indname_block(&buf, &buflen, &offset, 11,
|
||||
nv.value, nv.valuelen, 1));
|
||||
nv.value, nv.valuelen, 1,
|
||||
NGHTTP2_HD_SIDE_CLIENT));
|
||||
CU_ASSERT(1 == nghttp2_hd_inflate_hd(&inflater, &resnva, buf, offset));
|
||||
assert_nv_equal(&nv, resnva, 1);
|
||||
CU_ASSERT(1 == inflater.hd_table.len);
|
||||
|
@ -270,13 +271,17 @@ void test_nghttp2_hd_inflate_indname_inc_eviction(void)
|
|||
|
||||
memset(value, '0', sizeof(value));
|
||||
CU_ASSERT(0 == nghttp2_hd_emit_indname_block(&buf, &buflen, &offset, 2,
|
||||
value, sizeof(value), 1));
|
||||
value, sizeof(value), 1,
|
||||
NGHTTP2_HD_SIDE_CLIENT));
|
||||
CU_ASSERT(0 == nghttp2_hd_emit_indname_block(&buf, &buflen, &offset, 3,
|
||||
value, sizeof(value), 1));
|
||||
value, sizeof(value), 1,
|
||||
NGHTTP2_HD_SIDE_CLIENT));
|
||||
CU_ASSERT(0 == nghttp2_hd_emit_indname_block(&buf, &buflen, &offset, 4,
|
||||
value, sizeof(value), 1));
|
||||
value, sizeof(value), 1,
|
||||
NGHTTP2_HD_SIDE_CLIENT));
|
||||
CU_ASSERT(0 == nghttp2_hd_emit_indname_block(&buf, &buflen, &offset, 5,
|
||||
value, sizeof(value), 1));
|
||||
value, sizeof(value), 1,
|
||||
NGHTTP2_HD_SIDE_CLIENT));
|
||||
|
||||
CU_ASSERT(4 == nghttp2_hd_inflate_hd(&inflater, &resnva, buf, offset));
|
||||
CU_ASSERT(5 == resnva[0].namelen);
|
||||
|
@ -304,7 +309,8 @@ void test_nghttp2_hd_inflate_newname_inc(void)
|
|||
nghttp2_hd_inflate_init(&inflater, NGHTTP2_HD_SIDE_SERVER);
|
||||
|
||||
CU_ASSERT(0 == nghttp2_hd_emit_newname_block(&buf, &buflen, &offset,
|
||||
&nv, 1));
|
||||
&nv, 1,
|
||||
NGHTTP2_HD_SIDE_CLIENT));
|
||||
CU_ASSERT(1 == nghttp2_hd_inflate_hd(&inflater, &resnva, buf, offset));
|
||||
assert_nv_equal(&nv, resnva, 1);
|
||||
CU_ASSERT(1 == inflater.hd_table.len);
|
||||
|
@ -336,7 +342,8 @@ void test_nghttp2_hd_inflate_clearall_inc(void)
|
|||
nghttp2_hd_inflate_init(&inflater, NGHTTP2_HD_SIDE_SERVER);
|
||||
|
||||
CU_ASSERT(0 == nghttp2_hd_emit_newname_block(&buf, &buflen, &offset,
|
||||
&nv, 1));
|
||||
&nv, 1,
|
||||
NGHTTP2_HD_SIDE_CLIENT));
|
||||
CU_ASSERT(1 == nghttp2_hd_inflate_hd(&inflater, &resnva, buf, offset));
|
||||
assert_nv_equal(&nv, resnva, 1);
|
||||
CU_ASSERT(0 == inflater.hd_table.len);
|
||||
|
@ -358,7 +365,8 @@ void test_nghttp2_hd_inflate_clearall_inc(void)
|
|||
|
||||
offset = 0;
|
||||
CU_ASSERT(0 == nghttp2_hd_emit_newname_block(&buf, &buflen, &offset,
|
||||
&nv, 1));
|
||||
&nv, 1,
|
||||
NGHTTP2_HD_SIDE_CLIENT));
|
||||
CU_ASSERT(1 == nghttp2_hd_inflate_hd(&inflater, &resnva, buf, offset));
|
||||
assert_nv_equal(&nv, resnva, 1);
|
||||
CU_ASSERT(1 == inflater.hd_table.len);
|
||||
|
|
|
@ -386,6 +386,7 @@ void test_nghttp2_session_recv(void)
|
|||
nghttp2_outbound_item *item;
|
||||
nghttp2_nv *nva;
|
||||
ssize_t nvlen;
|
||||
nghttp2_hd_context deflater;
|
||||
|
||||
memset(&callbacks, 0, sizeof(nghttp2_session_callbacks));
|
||||
callbacks.send_callback = null_send_callback;
|
||||
|
@ -393,14 +394,15 @@ void test_nghttp2_session_recv(void)
|
|||
callbacks.on_frame_recv_callback = on_frame_recv_callback;
|
||||
user_data.df = &df;
|
||||
nghttp2_session_server_new(&session, &callbacks, &user_data);
|
||||
nghttp2_hd_deflate_init(&deflater, NGHTTP2_HD_SIDE_CLIENT);
|
||||
|
||||
nvlen = nghttp2_nv_array_from_cstr(&nva, nv);
|
||||
nghttp2_frame_headers_init(&frame.headers, NGHTTP2_FLAG_END_HEADERS,
|
||||
1, NGHTTP2_PRI_DEFAULT, nva, nvlen);
|
||||
framelen = nghttp2_frame_pack_headers(&framedata, &framedatalen,
|
||||
&frame.headers,
|
||||
&session->hd_deflater);
|
||||
nghttp2_hd_end_headers(&session->hd_deflater);
|
||||
&deflater);
|
||||
nghttp2_hd_end_headers(&deflater);
|
||||
|
||||
scripted_data_feed_init(&df, framedata, framelen);
|
||||
/* Send 1 byte per each read */
|
||||
|
@ -421,8 +423,8 @@ void test_nghttp2_session_recv(void)
|
|||
5, NGHTTP2_PRI_DEFAULT, nva, nvlen);
|
||||
framelen = nghttp2_frame_pack_headers(&framedata, &framedatalen,
|
||||
&frame.headers,
|
||||
&session->hd_deflater);
|
||||
nghttp2_hd_end_headers(&session->hd_deflater);
|
||||
&deflater);
|
||||
nghttp2_hd_end_headers(&deflater);
|
||||
|
||||
nghttp2_frame_headers_free(&frame.headers);
|
||||
|
||||
|
@ -431,6 +433,7 @@ void test_nghttp2_session_recv(void)
|
|||
CU_ASSERT(0 == nghttp2_session_recv(session));
|
||||
CU_ASSERT(1 == user_data.frame_recv_cb_called);
|
||||
|
||||
nghttp2_hd_deflate_free(&deflater);
|
||||
nghttp2_session_del(session);
|
||||
|
||||
/* Some tests for frame too large */
|
||||
|
@ -470,6 +473,8 @@ void test_nghttp2_session_recv_invalid_stream_id(void)
|
|||
nghttp2_frame frame;
|
||||
nghttp2_nv *nva;
|
||||
ssize_t nvlen;
|
||||
nghttp2_hd_context deflater;
|
||||
|
||||
memset(&callbacks, 0, sizeof(nghttp2_session_callbacks));
|
||||
callbacks.recv_callback = scripted_recv_callback;
|
||||
callbacks.on_invalid_frame_recv_callback = on_invalid_frame_recv_callback;
|
||||
|
@ -477,13 +482,14 @@ void test_nghttp2_session_recv_invalid_stream_id(void)
|
|||
user_data.df = &df;
|
||||
user_data.invalid_frame_recv_cb_called = 0;
|
||||
nghttp2_session_server_new(&session, &callbacks, &user_data);
|
||||
nghttp2_hd_deflate_init(&deflater, NGHTTP2_HD_SIDE_CLIENT);
|
||||
nvlen = nghttp2_nv_array_from_cstr(&nva, nv);
|
||||
nghttp2_frame_headers_init(&frame.headers, NGHTTP2_FLAG_END_HEADERS, 2,
|
||||
NGHTTP2_PRI_DEFAULT, nva, nvlen);
|
||||
framelen = nghttp2_frame_pack_headers(&framedata, &framedatalen,
|
||||
&frame.headers,
|
||||
&session->hd_deflater);
|
||||
nghttp2_hd_end_headers(&session->hd_deflater);
|
||||
&deflater);
|
||||
nghttp2_hd_end_headers(&deflater);
|
||||
|
||||
scripted_data_feed_init(&df, framedata, framelen);
|
||||
nghttp2_frame_headers_free(&frame.headers);
|
||||
|
@ -492,6 +498,7 @@ void test_nghttp2_session_recv_invalid_stream_id(void)
|
|||
CU_ASSERT(1 == user_data.invalid_frame_recv_cb_called);
|
||||
|
||||
free(framedata);
|
||||
nghttp2_hd_deflate_free(&deflater);
|
||||
nghttp2_session_del(session);
|
||||
}
|
||||
|
||||
|
@ -510,6 +517,7 @@ void test_nghttp2_session_recv_invalid_frame(void)
|
|||
nghttp2_frame frame;
|
||||
nghttp2_nv *nva;
|
||||
ssize_t nvlen;
|
||||
nghttp2_hd_context deflater;
|
||||
|
||||
memset(&callbacks, 0, sizeof(nghttp2_session_callbacks));
|
||||
callbacks.recv_callback = scripted_recv_callback;
|
||||
|
@ -519,13 +527,14 @@ void test_nghttp2_session_recv_invalid_frame(void)
|
|||
user_data.df = &df;
|
||||
user_data.frame_send_cb_called = 0;
|
||||
nghttp2_session_server_new(&session, &callbacks, &user_data);
|
||||
nghttp2_hd_deflate_init(&deflater, NGHTTP2_HD_SIDE_CLIENT);
|
||||
nvlen = nghttp2_nv_array_from_cstr(&nva, nv);
|
||||
nghttp2_frame_headers_init(&frame.headers, NGHTTP2_FLAG_END_HEADERS, 1,
|
||||
NGHTTP2_PRI_DEFAULT, nva, nvlen);
|
||||
framelen = nghttp2_frame_pack_headers(&framedata, &framedatalen,
|
||||
&frame.headers,
|
||||
&session->hd_deflater);
|
||||
nghttp2_hd_end_headers(&session->hd_deflater);
|
||||
&deflater);
|
||||
nghttp2_hd_end_headers(&deflater);
|
||||
|
||||
scripted_data_feed_init(&df, framedata, framelen);
|
||||
|
||||
|
@ -544,6 +553,7 @@ void test_nghttp2_session_recv_invalid_frame(void)
|
|||
free(framedata);
|
||||
nghttp2_frame_headers_free(&frame.headers);
|
||||
|
||||
nghttp2_hd_deflate_free(&deflater);
|
||||
nghttp2_session_del(session);
|
||||
}
|
||||
|
||||
|
@ -711,6 +721,7 @@ void test_nghttp2_session_continue(void)
|
|||
const nghttp2_frame *recv_frame;
|
||||
nghttp2_nv nvcheck;
|
||||
nghttp2_frame_hd data_hd;
|
||||
nghttp2_hd_context deflater;
|
||||
|
||||
memset(&callbacks, 0, sizeof(nghttp2_session_callbacks));
|
||||
callbacks.send_callback = null_send_callback;
|
||||
|
@ -720,16 +731,18 @@ void test_nghttp2_session_continue(void)
|
|||
|
||||
nghttp2_session_server_new(&session, &callbacks, &user_data);
|
||||
|
||||
nghttp2_hd_deflate_init(&deflater, NGHTTP2_HD_SIDE_CLIENT);
|
||||
|
||||
/* Make 2 HEADERS frames */
|
||||
nvlen = nghttp2_nv_array_from_cstr(&nva, nv1);
|
||||
nghttp2_frame_headers_init(&frame.headers, NGHTTP2_FLAG_END_HEADERS,
|
||||
1, NGHTTP2_PRI_DEFAULT, nva, nvlen);
|
||||
framelen1 = nghttp2_frame_pack_headers(&framedata, &framedatalen,
|
||||
&frame.headers,
|
||||
&session->hd_deflater);
|
||||
&deflater);
|
||||
nghttp2_frame_headers_free(&frame.headers);
|
||||
|
||||
nghttp2_hd_end_headers(&session->hd_deflater);
|
||||
nghttp2_hd_end_headers(&deflater);
|
||||
memcpy(buffer, framedata, framelen1);
|
||||
|
||||
nvlen = nghttp2_nv_array_from_cstr(&nva, nv2);
|
||||
|
@ -737,10 +750,10 @@ void test_nghttp2_session_continue(void)
|
|||
3, NGHTTP2_PRI_DEFAULT, nva, nvlen);
|
||||
framelen2 = nghttp2_frame_pack_headers(&framedata, &framedatalen,
|
||||
&frame.headers,
|
||||
&session->hd_deflater);
|
||||
&deflater);
|
||||
nghttp2_frame_headers_free(&frame.headers);
|
||||
|
||||
nghttp2_hd_end_headers(&session->hd_deflater);
|
||||
nghttp2_hd_end_headers(&deflater);
|
||||
memcpy(buffer + framelen1, framedata, framelen2);
|
||||
buflen = framelen1 + framelen2;
|
||||
|
||||
|
@ -796,6 +809,7 @@ void test_nghttp2_session_continue(void)
|
|||
CU_ASSERT(1 == user_data.data_recv_cb_called);
|
||||
|
||||
free(framedata);
|
||||
nghttp2_hd_deflate_free(&deflater);
|
||||
nghttp2_session_del(session);
|
||||
}
|
||||
|
||||
|
@ -1874,12 +1888,14 @@ void test_nghttp2_submit_response_without_data(void)
|
|||
nghttp2_outbound_item *item;
|
||||
my_user_data ud;
|
||||
nghttp2_frame frame;
|
||||
nghttp2_hd_context inflater;
|
||||
|
||||
acc.length = 0;
|
||||
ud.acc = &acc;
|
||||
memset(&callbacks, 0, sizeof(callbacks));
|
||||
callbacks.send_callback = accumulator_send_callback;
|
||||
CU_ASSERT(0 == nghttp2_session_server_new(&session, &callbacks, &ud));
|
||||
nghttp2_hd_inflate_init(&inflater, NGHTTP2_HD_SIDE_CLIENT);
|
||||
nghttp2_session_open_stream(session, 1, NGHTTP2_FLAG_END_STREAM,
|
||||
NGHTTP2_PRI_DEFAULT,
|
||||
NGHTTP2_STREAM_OPENING, NULL);
|
||||
|
@ -1890,11 +1906,13 @@ void test_nghttp2_submit_response_without_data(void)
|
|||
|
||||
CU_ASSERT(0 == nghttp2_session_send(session));
|
||||
CU_ASSERT(0 == unpack_frame_with_nv_block(&frame, NGHTTP2_HEADERS,
|
||||
&session->hd_inflater,
|
||||
&inflater,
|
||||
acc.buf, acc.length));
|
||||
CU_ASSERT(nvnameeq(":version", &frame.headers.nva[0]));
|
||||
nghttp2_frame_headers_free(&frame.headers);
|
||||
nghttp2_hd_end_headers(&inflater);
|
||||
|
||||
nghttp2_hd_inflate_free(&inflater);
|
||||
nghttp2_session_del(session);
|
||||
}
|
||||
|
||||
|
@ -1933,12 +1951,14 @@ void test_nghttp2_submit_request_without_data(void)
|
|||
nghttp2_outbound_item *item;
|
||||
my_user_data ud;
|
||||
nghttp2_frame frame;
|
||||
nghttp2_hd_context inflater;
|
||||
|
||||
acc.length = 0;
|
||||
ud.acc = &acc;
|
||||
memset(&callbacks, 0, sizeof(nghttp2_session_callbacks));
|
||||
callbacks.send_callback = accumulator_send_callback;
|
||||
CU_ASSERT(0 == nghttp2_session_client_new(&session, &callbacks, &ud));
|
||||
nghttp2_hd_inflate_init(&inflater, NGHTTP2_HD_SIDE_SERVER);
|
||||
CU_ASSERT(0 == nghttp2_submit_request(session, NGHTTP2_PRI_DEFAULT, nv,
|
||||
&data_prd, NULL));
|
||||
item = nghttp2_session_get_next_ob_item(session);
|
||||
|
@ -1947,11 +1967,13 @@ void test_nghttp2_submit_request_without_data(void)
|
|||
|
||||
CU_ASSERT(0 == nghttp2_session_send(session));
|
||||
CU_ASSERT(0 == unpack_frame_with_nv_block(&frame, NGHTTP2_HEADERS,
|
||||
&session->hd_inflater,
|
||||
&inflater,
|
||||
acc.buf, acc.length));
|
||||
CU_ASSERT(nvnameeq(":version", &frame.headers.nva[0]));
|
||||
nghttp2_frame_headers_free(&frame.headers);
|
||||
nghttp2_hd_end_headers(&inflater);
|
||||
|
||||
nghttp2_hd_inflate_free(&inflater);
|
||||
nghttp2_session_del(session);
|
||||
}
|
||||
|
||||
|
@ -1990,12 +2012,14 @@ void test_nghttp2_submit_request2_without_data(void)
|
|||
nghttp2_outbound_item *item;
|
||||
my_user_data ud;
|
||||
nghttp2_frame frame;
|
||||
nghttp2_hd_context inflater;
|
||||
|
||||
acc.length = 0;
|
||||
ud.acc = &acc;
|
||||
memset(&callbacks, 0, sizeof(nghttp2_session_callbacks));
|
||||
callbacks.send_callback = accumulator_send_callback;
|
||||
CU_ASSERT(0 == nghttp2_session_client_new(&session, &callbacks, &ud));
|
||||
nghttp2_hd_inflate_init(&inflater, NGHTTP2_HD_SIDE_SERVER);
|
||||
CU_ASSERT(0 == nghttp2_submit_request2(session, NGHTTP2_PRI_DEFAULT,
|
||||
nva, ARRLEN(nva), &data_prd, NULL));
|
||||
item = nghttp2_session_get_next_ob_item(session);
|
||||
|
@ -2004,11 +2028,13 @@ void test_nghttp2_submit_request2_without_data(void)
|
|||
|
||||
CU_ASSERT(0 == nghttp2_session_send(session));
|
||||
CU_ASSERT(0 == unpack_frame_with_nv_block(&frame, NGHTTP2_HEADERS,
|
||||
&session->hd_inflater,
|
||||
&inflater,
|
||||
acc.buf, acc.length));
|
||||
CU_ASSERT(nvnameeq(":version", &frame.headers.nva[0]));
|
||||
nghttp2_frame_headers_free(&frame.headers);
|
||||
nghttp2_hd_end_headers(&inflater);
|
||||
|
||||
nghttp2_hd_inflate_free(&inflater);
|
||||
nghttp2_session_del(session);
|
||||
}
|
||||
|
||||
|
@ -2141,6 +2167,7 @@ void test_nghttp2_submit_headers(void)
|
|||
nghttp2_stream *stream;
|
||||
accumulator acc;
|
||||
nghttp2_frame frame;
|
||||
nghttp2_hd_context inflater;
|
||||
|
||||
acc.length = 0;
|
||||
ud.acc = &acc;
|
||||
|
@ -2149,6 +2176,7 @@ void test_nghttp2_submit_headers(void)
|
|||
callbacks.on_frame_send_callback = on_frame_send_callback;
|
||||
|
||||
CU_ASSERT(0 == nghttp2_session_client_new(&session, &callbacks, &ud));
|
||||
nghttp2_hd_inflate_init(&inflater, NGHTTP2_HD_SIDE_SERVER);
|
||||
CU_ASSERT(0 == nghttp2_submit_headers(session,
|
||||
NGHTTP2_FLAG_END_STREAM,
|
||||
1, NGHTTP2_PRI_DEFAULT,
|
||||
|
@ -2180,11 +2208,13 @@ void test_nghttp2_submit_headers(void)
|
|||
|
||||
CU_ASSERT(0 == unpack_frame_with_nv_block(&frame,
|
||||
NGHTTP2_HEADERS,
|
||||
&session->hd_inflater,
|
||||
&inflater,
|
||||
acc.buf, acc.length));
|
||||
CU_ASSERT(nvnameeq(":version", &frame.headers.nva[0]));
|
||||
nghttp2_frame_headers_free(&frame.headers);
|
||||
nghttp2_hd_end_headers(&inflater);
|
||||
|
||||
nghttp2_hd_inflate_free(&inflater);
|
||||
nghttp2_session_del(session);
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue