nghttpd_hd: Update static table and use 1-based index

The specification now says that index to the header table entry is
1-based. Since 0-based index is very handy to access arrays, we
internally uses 0-based index. We just convert it to 1-based when
we emit to the block and convert 1-based to 0-based on decoding.
This commit is contained in:
Tatsuhiro Tsujikawa 2013-11-16 16:18:44 +09:00
parent a255e4efa6
commit 94ec4dbe42
3 changed files with 68 additions and 66 deletions

View File

@ -43,7 +43,7 @@ json_t* dump_header_table(nghttp2_hd_context *context)
for(i = 0; i < context->hd_table.len; ++i) { for(i = 0; i < context->hd_table.len; ++i) {
nghttp2_hd_entry *ent = nghttp2_hd_table_get(context, i); nghttp2_hd_entry *ent = nghttp2_hd_table_get(context, i);
json_t *outent = json_object(); json_t *outent = json_object();
json_object_set_new(outent, "index", json_integer(i)); json_object_set_new(outent, "index", json_integer(i + 1));
dump_val(outent, "name", ent->nv.name, ent->nv.namelen); dump_val(outent, "name", ent->nv.name, ent->nv.namelen);
dump_val(outent, "value", ent->nv.value, ent->nv.valuelen); dump_val(outent, "value", ent->nv.value, ent->nv.valuelen);
json_object_set_new(outent, "referenced", json_object_set_new(outent, "referenced",

View File

@ -36,65 +36,66 @@
1, NGHTTP2_HD_FLAG_NONE } 1, NGHTTP2_HD_FLAG_NONE }
static nghttp2_hd_entry static_table[] = { static nghttp2_hd_entry static_table[] = {
/* 0 */ MAKE_NV(":authority", ""), /* 1 */ MAKE_NV(":authority", ""),
/* 1 */ MAKE_NV(":method", "GET"), /* 2 */ MAKE_NV(":method", "GET"),
/* 2 */ MAKE_NV(":method", "POST"), /* 3 */ MAKE_NV(":method", "POST"),
/* 3 */ MAKE_NV(":path", "/"), /* 4 */ MAKE_NV(":path", "/"),
/* 4 */ MAKE_NV(":path", "/index.html"), /* 5 */ MAKE_NV(":path", "/index.html"),
/* 5 */ MAKE_NV(":scheme", "http"), /* 6 */ MAKE_NV(":scheme", "http"),
/* 6 */ MAKE_NV(":scheme", "https"), /* 7 */ MAKE_NV(":scheme", "https"),
/* 7 */ MAKE_NV(":status", "200"), /* 8 */ MAKE_NV(":status", "200"),
/* 8 */ MAKE_NV(":status", "500"), /* 9 */ MAKE_NV(":status", "500"),
/* 9 */ MAKE_NV(":status", "404"), /* 10 */ MAKE_NV(":status", "404"),
/* 10 */ MAKE_NV(":status", "403"), /* 11 */ MAKE_NV(":status", "403"),
/* 11 */ MAKE_NV(":status", "400"), /* 12 */ MAKE_NV(":status", "400"),
/* 12 */ MAKE_NV(":status", "401"), /* 13 */ MAKE_NV(":status", "401"),
/* 13 */ MAKE_NV("accept-charset", ""), /* 14 */ MAKE_NV("accept-charset", ""),
/* 14 */ MAKE_NV("accept-encoding", ""), /* 15 */ MAKE_NV("accept-encoding", ""),
/* 15 */ MAKE_NV("accept-language", ""), /* 16 */ MAKE_NV("accept-language", ""),
/* 16 */ MAKE_NV("accept-ranges", ""), /* 17 */ MAKE_NV("accept-ranges", ""),
/* 17 */ MAKE_NV("accept", ""), /* 18 */ MAKE_NV("accept", ""),
/* 18 */ MAKE_NV("access-control-allow-origin", ""), /* 19 */ MAKE_NV("access-control-allow-origin", ""),
/* 19 */ MAKE_NV("age", ""), /* 20 */ MAKE_NV("age", ""),
/* 20 */ MAKE_NV("allow", ""), /* 21 */ MAKE_NV("allow", ""),
/* 21 */ MAKE_NV("authorization", ""), /* 22 */ MAKE_NV("authorization", ""),
/* 22 */ MAKE_NV("cache-control", ""), /* 23 */ MAKE_NV("cache-control", ""),
/* 23 */ MAKE_NV("content-disposition", ""), /* 24 */ MAKE_NV("content-disposition", ""),
/* 24 */ MAKE_NV("content-encoding", ""), /* 25 */ MAKE_NV("content-encoding", ""),
/* 25 */ MAKE_NV("content-language", ""), /* 26 */ MAKE_NV("content-language", ""),
/* 26 */ MAKE_NV("content-length", ""), /* 27 */ MAKE_NV("content-length", ""),
/* 27 */ MAKE_NV("content-location", ""), /* 28 */ MAKE_NV("content-location", ""),
/* 28 */ MAKE_NV("content-range", ""), /* 29 */ MAKE_NV("content-range", ""),
/* 29 */ MAKE_NV("content-type", ""), /* 30 */ MAKE_NV("content-type", ""),
/* 30 */ MAKE_NV("cookie", ""), /* 31 */ MAKE_NV("cookie", ""),
/* 31 */ MAKE_NV("date", ""), /* 32 */ MAKE_NV("date", ""),
/* 32 */ MAKE_NV("etag", ""), /* 33 */ MAKE_NV("etag", ""),
/* 33 */ MAKE_NV("expect", ""), /* 34 */ MAKE_NV("expect", ""),
/* 34 */ MAKE_NV("expires", ""), /* 35 */ MAKE_NV("expires", ""),
/* 35 */ MAKE_NV("from", ""), /* 36 */ MAKE_NV("from", ""),
/* 36 */ MAKE_NV("if-match", ""), /* 37 */ MAKE_NV("host", ""),
/* 37 */ MAKE_NV("if-modified-since", ""), /* 38 */ MAKE_NV("if-match", ""),
/* 38 */ MAKE_NV("if-none-match", ""), /* 39 */ MAKE_NV("if-modified-since", ""),
/* 39 */ MAKE_NV("if-range", ""), /* 40 */ MAKE_NV("if-none-match", ""),
/* 40 */ MAKE_NV("if-unmodified-since", ""), /* 41 */ MAKE_NV("if-range", ""),
/* 41 */ MAKE_NV("last-modified", ""), /* 42 */ MAKE_NV("if-unmodified-since", ""),
/* 42 */ MAKE_NV("link", ""), /* 43 */ MAKE_NV("last-modified", ""),
/* 43 */ MAKE_NV("location", ""), /* 44 */ MAKE_NV("link", ""),
/* 44 */ MAKE_NV("max-forwards", ""), /* 45 */ MAKE_NV("location", ""),
/* 45 */ MAKE_NV("proxy-authenticate", ""), /* 46 */ MAKE_NV("max-forwards", ""),
/* 46 */ MAKE_NV("proxy-authorization", ""), /* 47 */ MAKE_NV("proxy-authenticate", ""),
/* 47 */ MAKE_NV("range", ""), /* 48 */ MAKE_NV("proxy-authorization", ""),
/* 48 */ MAKE_NV("referer", ""), /* 49 */ MAKE_NV("range", ""),
/* 49 */ MAKE_NV("refresh", ""), /* 50 */ MAKE_NV("referer", ""),
/* 50 */ MAKE_NV("retry-after", ""), /* 51 */ MAKE_NV("refresh", ""),
/* 51 */ MAKE_NV("server", ""), /* 52 */ MAKE_NV("retry-after", ""),
/* 52 */ MAKE_NV("set-cookie", ""), /* 53 */ MAKE_NV("server", ""),
/* 53 */ MAKE_NV("strict-transport-security", ""), /* 54 */ MAKE_NV("set-cookie", ""),
/* 54 */ MAKE_NV("transfer-encoding", ""), /* 55 */ MAKE_NV("strict-transport-security", ""),
/* 55 */ MAKE_NV("user-agent", ""), /* 56 */ MAKE_NV("transfer-encoding", ""),
/* 56 */ MAKE_NV("vary", ""), /* 57 */ MAKE_NV("user-agent", ""),
/* 57 */ MAKE_NV("via", ""), /* 58 */ MAKE_NV("vary", ""),
/* 58 */ MAKE_NV("www-authenticate", "") /* 59 */ MAKE_NV("via", ""),
/* 60 */ MAKE_NV("www-authenticate", "")
}; };
static const size_t STATIC_TABLE_LENGTH = static const size_t STATIC_TABLE_LENGTH =
@ -578,14 +579,14 @@ static int emit_indexed_block(uint8_t **buf_ptr, size_t *buflen_ptr,
{ {
int rv; int rv;
uint8_t *bufp; uint8_t *bufp;
size_t blocklen = count_encoded_length(index, 7); size_t blocklen = count_encoded_length(index + 1, 7);
rv = ensure_write_buffer(buf_ptr, buflen_ptr, *offset_ptr, blocklen); rv = ensure_write_buffer(buf_ptr, buflen_ptr, *offset_ptr, blocklen);
if(rv != 0) { if(rv != 0) {
return rv; return rv;
} }
bufp = *buf_ptr + *offset_ptr; bufp = *buf_ptr + *offset_ptr;
*bufp = 0x80u; *bufp = 0x80u;
encode_length(bufp, index, 7); encode_length(bufp, index + 1, 7);
*offset_ptr += blocklen; *offset_ptr += blocklen;
return 0; return 0;
} }
@ -1135,10 +1136,11 @@ ssize_t nghttp2_hd_inflate_hd(nghttp2_hd_context *inflater,
ssize_t index; ssize_t index;
nghttp2_hd_entry *ent; nghttp2_hd_entry *ent;
in = decode_length(&index, in, last, 7); in = decode_length(&index, in, last, 7);
if(index < 0) { if(index <= 0) {
rv = NGHTTP2_ERR_HEADER_COMP; rv = NGHTTP2_ERR_HEADER_COMP;
goto fail; goto fail;
} }
--index;
if(!check_index_range(inflater, index)) { if(!check_index_range(inflater, index)) {
rv = NGHTTP2_ERR_HEADER_COMP; rv = NGHTTP2_ERR_HEADER_COMP;
goto fail; goto fail;
@ -1270,7 +1272,7 @@ ssize_t nghttp2_hd_inflate_hd(nghttp2_hd_context *inflater,
int value_huffman; int value_huffman;
uint8_t *decoded_huffman_value = NULL; uint8_t *decoded_huffman_value = NULL;
in = decode_length(&index, in, last, 6); in = decode_length(&index, in, last, 6);
if(index < 0) { if(index <= 0) {
rv = NGHTTP2_ERR_HEADER_COMP; rv = NGHTTP2_ERR_HEADER_COMP;
goto fail; goto fail;
} }

View File

@ -458,7 +458,7 @@ void test_nghttp2_hd_inflate_indname_noinc(void)
for(i = 0; i < ARRLEN(nv); ++i) { for(i = 0; i < ARRLEN(nv); ++i) {
offset = 0; offset = 0;
CU_ASSERT(0 == nghttp2_hd_emit_indname_block(&buf, &buflen, &offset, 55, CU_ASSERT(0 == nghttp2_hd_emit_indname_block(&buf, &buflen, &offset, 56,
nv[i].value, nv[i].valuelen, nv[i].value, nv[i].valuelen,
0, 0,
NGHTTP2_HD_SIDE_REQUEST)); NGHTTP2_HD_SIDE_REQUEST));
@ -484,7 +484,7 @@ void test_nghttp2_hd_inflate_indname_inc(void)
nghttp2_nv *resnva; nghttp2_nv *resnva;
nghttp2_hd_inflate_init(&inflater, NGHTTP2_HD_SIDE_REQUEST); nghttp2_hd_inflate_init(&inflater, NGHTTP2_HD_SIDE_REQUEST);
CU_ASSERT(0 == nghttp2_hd_emit_indname_block(&buf, &buflen, &offset, 55, CU_ASSERT(0 == nghttp2_hd_emit_indname_block(&buf, &buflen, &offset, 56,
nv.value, nv.valuelen, 1, nv.value, nv.valuelen, 1,
NGHTTP2_HD_SIDE_REQUEST)); NGHTTP2_HD_SIDE_REQUEST));
CU_ASSERT(1 == nghttp2_hd_inflate_hd(&inflater, &resnva, buf, offset)); CU_ASSERT(1 == nghttp2_hd_inflate_hd(&inflater, &resnva, buf, offset));