nghttp_hd: Use NGHTTP2_HD_SIDE_{REQUEST,RESPONSE} instead of {CLIENT,SERVER}

This change conveys better notion about compression context.
This commit is contained in:
Tatsuhiro Tsujikawa 2013-10-21 00:44:39 +09:00
parent 8ef134a702
commit 9e50ae46d9
7 changed files with 62 additions and 57 deletions

View File

@ -296,7 +296,7 @@ int nghttp2_hd_deflate_init2(nghttp2_hd_context *deflater,
int nghttp2_hd_inflate_init(nghttp2_hd_context *inflater, nghttp2_hd_side side)
{
return nghttp2_hd_context_init(inflater, NGHTTP2_HD_ROLE_INFLATE, side^1,
return nghttp2_hd_context_init(inflater, NGHTTP2_HD_ROLE_INFLATE, side,
NGHTTP2_HD_DEFAULT_MAX_BUFFER_SIZE);
}
@ -304,7 +304,7 @@ int nghttp2_hd_inflate_init2(nghttp2_hd_context *inflater,
nghttp2_hd_side side,
size_t hd_table_bufsize_max)
{
return nghttp2_hd_context_init(inflater, NGHTTP2_HD_ROLE_INFLATE, side^1,
return nghttp2_hd_context_init(inflater, NGHTTP2_HD_ROLE_INFLATE, side,
hd_table_bufsize_max);
}

View File

@ -39,8 +39,8 @@
#define NGHTTP2_HD_ENTRY_OVERHEAD 32
typedef enum {
NGHTTP2_HD_SIDE_CLIENT = 0,
NGHTTP2_HD_SIDE_SERVER = 1
NGHTTP2_HD_SIDE_REQUEST = 0,
NGHTTP2_HD_SIDE_RESPONSE = 1
} nghttp2_hd_side;
typedef enum {
@ -103,8 +103,7 @@ 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
/* NGHTTP2_HD_SIDE_REQUEST for processing request, otherwise
response. */
nghttp2_hd_side side;
/* Maximum header table size */
@ -272,8 +271,8 @@ nghttp2_hd_entry* nghttp2_hd_table_get(nghttp2_hd_context *context,
/*
* 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.
* |side| is NGHTTP2_HD_SIDE_REQUEST, 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
@ -287,7 +286,7 @@ size_t nghttp2_hd_huff_encode_count(const uint8_t *src, size_t len,
* 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
* NGHTTP2_HD_SIDE_REQUEST, the request huffman code table is
* used. Otherwise, the response code table is used.
*
* This function returns the number of written bytes, including
@ -303,8 +302,8 @@ ssize_t nghttp2_hd_huff_encode(uint8_t *dest, size_t destlen,
/*
* 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.
* |side| is NGHTTP2_HD_SIDE_REQUEST, 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.
@ -318,7 +317,7 @@ ssize_t nghttp2_hd_huff_decode_count(const uint8_t *src, size_t srclen,
* 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
* NGHTTP2_HD_SIDE_REQUEST, the request huffman code table is
* used. Otherwise, the response code table is used.
*
* This function returns the number of written bytes. This return

View File

@ -148,7 +148,7 @@ size_t nghttp2_hd_huff_encode_count(const uint8_t *src, size_t len,
size_t nbits = 0;
const nghttp2_huff_sym *huff_sym_table;
if(side == NGHTTP2_HD_SIDE_CLIENT) {
if(side == NGHTTP2_HD_SIDE_REQUEST) {
huff_sym_table = req_huff_sym_table;
} else {
huff_sym_table = res_huff_sym_table;
@ -169,7 +169,7 @@ ssize_t nghttp2_hd_huff_encode(uint8_t *dest, size_t destlen,
size_t i;
const nghttp2_huff_sym *huff_sym_table;
if(side == NGHTTP2_HD_SIDE_CLIENT) {
if(side == NGHTTP2_HD_SIDE_REQUEST) {
huff_sym_table = req_huff_sym_table;
} else {
huff_sym_table = res_huff_sym_table;
@ -192,7 +192,7 @@ ssize_t nghttp2_hd_huff_decode_count(const uint8_t *src, size_t srclen,
const nghttp2_huff_sym *huff_sym_table;
const huff_decode_table_type *huff_decode_table;
if(side == NGHTTP2_HD_SIDE_CLIENT) {
if(side == NGHTTP2_HD_SIDE_REQUEST) {
huff_sym_table = req_huff_sym_table;
huff_decode_table = req_huff_decode_table;
} else {
@ -227,7 +227,7 @@ ssize_t nghttp2_hd_huff_decode(uint8_t *dest, size_t destlen,
const nghttp2_huff_sym *huff_sym_table;
const huff_decode_table_type *huff_decode_table;
if(side == NGHTTP2_HD_SIDE_CLIENT) {
if(side == NGHTTP2_HD_SIDE_REQUEST) {
huff_sym_table = req_huff_sym_table;
huff_decode_table = req_huff_decode_table;
} else {

View File

@ -164,9 +164,10 @@ static void nghttp2_inbound_frame_reset(nghttp2_session *session)
static int nghttp2_session_new(nghttp2_session **session_ptr,
const nghttp2_session_callbacks *callbacks,
void *user_data,
nghttp2_hd_side side)
int server)
{
int r;
nghttp2_hd_side side_deflate, side_inflate;
*session_ptr = malloc(sizeof(nghttp2_session));
if(*session_ptr == NULL) {
r = NGHTTP2_ERR_NOMEM;
@ -189,11 +190,19 @@ static int nghttp2_session_new(nghttp2_session **session_ptr,
(*session_ptr)->goaway_flags = NGHTTP2_GOAWAY_NONE;
(*session_ptr)->last_stream_id = 0;
r = nghttp2_hd_deflate_init(&(*session_ptr)->hd_deflater, side);
if(server) {
(*session_ptr)->server = 1;
side_deflate = NGHTTP2_HD_SIDE_RESPONSE;
side_inflate = NGHTTP2_HD_SIDE_REQUEST;
} else {
side_deflate = NGHTTP2_HD_SIDE_REQUEST;
side_inflate = NGHTTP2_HD_SIDE_RESPONSE;
}
r = nghttp2_hd_deflate_init(&(*session_ptr)->hd_deflater, side_deflate);
if(r != 0) {
goto fail_hd_deflater;
}
r = nghttp2_hd_inflate_init(&(*session_ptr)->hd_inflater, side);
r = nghttp2_hd_inflate_init(&(*session_ptr)->hd_inflater, side_inflate);
if(r != 0) {
goto fail_hd_inflater;
}
@ -275,8 +284,7 @@ int nghttp2_session_client_new(nghttp2_session **session_ptr,
{
int r;
/* For client side session, header compression is disabled. */
r = nghttp2_session_new(session_ptr, callbacks, user_data,
NGHTTP2_HD_SIDE_CLIENT);
r = nghttp2_session_new(session_ptr, callbacks, user_data, 0);
if(r == 0) {
/* IDs for use in client */
(*session_ptr)->next_stream_id = 1;
@ -291,10 +299,8 @@ int nghttp2_session_server_new(nghttp2_session **session_ptr,
{
int r;
/* Enable header compression on server side. */
r = nghttp2_session_new(session_ptr, callbacks, user_data,
NGHTTP2_HD_SIDE_SERVER);
r = nghttp2_session_new(session_ptr, callbacks, user_data, 1);
if(r == 0) {
(*session_ptr)->server = 1;
/* IDs for use in client */
(*session_ptr)->next_stream_id = 2;
(*session_ptr)->last_recv_stream_id = 0;

View File

@ -76,8 +76,8 @@ void test_nghttp2_frame_pack_headers()
nghttp2_nv *nva;
ssize_t nvlen;
nghttp2_hd_deflate_init(&deflater, NGHTTP2_HD_SIDE_CLIENT);
nghttp2_hd_inflate_init(&inflater, NGHTTP2_HD_SIDE_SERVER);
nghttp2_hd_deflate_init(&deflater, NGHTTP2_HD_SIDE_REQUEST);
nghttp2_hd_inflate_init(&inflater, NGHTTP2_HD_SIDE_REQUEST);
nvlen = nghttp2_nv_array_from_cstr(&nva, headers);
nghttp2_frame_headers_init(&frame,
@ -150,7 +150,7 @@ void test_nghttp2_frame_pack_headers_frame_too_large(void)
big_hds[big_hdslen] = NULL;
nvlen = nghttp2_nv_array_from_cstr(&nva, (const char**)big_hds);
nghttp2_hd_deflate_init(&deflater, NGHTTP2_HD_SIDE_CLIENT);
nghttp2_hd_deflate_init(&deflater, NGHTTP2_HD_SIDE_REQUEST);
nghttp2_frame_headers_init(&frame,
NGHTTP2_FLAG_END_STREAM|NGHTTP2_FLAG_END_HEADERS,
1000000007,
@ -255,8 +255,8 @@ void test_nghttp2_frame_pack_push_promise()
nghttp2_nv *nva;
ssize_t nvlen;
nghttp2_hd_deflate_init(&deflater, NGHTTP2_HD_SIDE_CLIENT);
nghttp2_hd_inflate_init(&inflater, NGHTTP2_HD_SIDE_SERVER);
nghttp2_hd_deflate_init(&deflater, NGHTTP2_HD_SIDE_RESPONSE);
nghttp2_hd_inflate_init(&inflater, NGHTTP2_HD_SIDE_RESPONSE);
nvlen = nghttp2_nv_array_from_cstr(&nva, headers);
nghttp2_frame_push_promise_init(&frame, NGHTTP2_FLAG_END_PUSH_PROMISE,

View File

@ -65,8 +65,8 @@ void test_nghttp2_hd_deflate(void)
nghttp2_nv *resnva;
ssize_t blocklen;
CU_ASSERT(0 == nghttp2_hd_deflate_init(&deflater, NGHTTP2_HD_SIDE_CLIENT));
CU_ASSERT(0 == nghttp2_hd_inflate_init(&inflater, NGHTTP2_HD_SIDE_SERVER));
CU_ASSERT(0 == nghttp2_hd_deflate_init(&deflater, NGHTTP2_HD_SIDE_REQUEST));
CU_ASSERT(0 == nghttp2_hd_inflate_init(&inflater, NGHTTP2_HD_SIDE_REQUEST));
blocklen = nghttp2_hd_deflate_hd(&deflater, &buf, &buflen, nv_offset, nva1,
sizeof(nva1)/sizeof(nghttp2_nv));
CU_ASSERT(blocklen > 0);
@ -152,8 +152,8 @@ void test_nghttp2_hd_deflate_same_indexed_repr(void)
nghttp2_nv *resnva;
ssize_t blocklen;
CU_ASSERT(0 == nghttp2_hd_deflate_init(&deflater, NGHTTP2_HD_SIDE_CLIENT));
CU_ASSERT(0 == nghttp2_hd_inflate_init(&inflater, NGHTTP2_HD_SIDE_SERVER));
CU_ASSERT(0 == nghttp2_hd_deflate_init(&deflater, NGHTTP2_HD_SIDE_REQUEST));
CU_ASSERT(0 == nghttp2_hd_inflate_init(&inflater, NGHTTP2_HD_SIDE_REQUEST));
/* Encode 2 same headers. cookie:alpha is not in the reference set,
so first emit literal repr and then 2 emits of indexed repr. */
@ -209,8 +209,8 @@ void test_nghttp2_hd_deflate_common_header_eviction(void)
nva[1].value = value;
nva[1].valuelen = sizeof(value);
nghttp2_hd_deflate_init(&deflater, NGHTTP2_HD_SIDE_CLIENT);
nghttp2_hd_inflate_init(&inflater, NGHTTP2_HD_SIDE_SERVER);
nghttp2_hd_deflate_init(&deflater, NGHTTP2_HD_SIDE_REQUEST);
nghttp2_hd_inflate_init(&inflater, NGHTTP2_HD_SIDE_REQUEST);
/* Put :scheme: http (index = 0) in reference set */
GET_TABLE_ENT(&deflater, 0)->flags |= NGHTTP2_HD_FLAG_REFSET;
@ -243,11 +243,11 @@ void test_nghttp2_hd_inflate_indname_inc(void)
size_t offset = 0;
nghttp2_nv nv = MAKE_NV("user-agent", "nghttp2");
nghttp2_nv *resnva;
nghttp2_hd_inflate_init(&inflater, NGHTTP2_HD_SIDE_SERVER);
nghttp2_hd_inflate_init(&inflater, NGHTTP2_HD_SIDE_REQUEST);
CU_ASSERT(0 == nghttp2_hd_emit_indname_block(&buf, &buflen, &offset, 51,
nv.value, nv.valuelen, 1,
NGHTTP2_HD_SIDE_CLIENT));
NGHTTP2_HD_SIDE_REQUEST));
CU_ASSERT(1 == nghttp2_hd_inflate_hd(&inflater, &resnva, buf, offset));
assert_nv_equal(&nv, resnva, 1);
CU_ASSERT(1 == inflater.hd_table.len);
@ -267,21 +267,21 @@ void test_nghttp2_hd_inflate_indname_inc_eviction(void)
size_t offset = 0;
uint8_t value[1024];
nghttp2_nv *resnva;
nghttp2_hd_inflate_init(&inflater, NGHTTP2_HD_SIDE_SERVER);
nghttp2_hd_inflate_init(&inflater, NGHTTP2_HD_SIDE_REQUEST);
memset(value, '0', sizeof(value));
CU_ASSERT(0 == nghttp2_hd_emit_indname_block(&buf, &buflen, &offset, 7,
value, sizeof(value), 1,
NGHTTP2_HD_SIDE_CLIENT));
NGHTTP2_HD_SIDE_REQUEST));
CU_ASSERT(0 == nghttp2_hd_emit_indname_block(&buf, &buflen, &offset, 8,
value, sizeof(value), 1,
NGHTTP2_HD_SIDE_CLIENT));
NGHTTP2_HD_SIDE_REQUEST));
CU_ASSERT(0 == nghttp2_hd_emit_indname_block(&buf, &buflen, &offset, 9,
value, sizeof(value), 1,
NGHTTP2_HD_SIDE_CLIENT));
NGHTTP2_HD_SIDE_REQUEST));
CU_ASSERT(0 == nghttp2_hd_emit_indname_block(&buf, &buflen, &offset, 10,
value, sizeof(value), 1,
NGHTTP2_HD_SIDE_CLIENT));
NGHTTP2_HD_SIDE_REQUEST));
CU_ASSERT(4 == nghttp2_hd_inflate_hd(&inflater, &resnva, buf, offset));
CU_ASSERT(14 == resnva[0].namelen);
@ -306,11 +306,11 @@ void test_nghttp2_hd_inflate_newname_inc(void)
size_t offset = 0;
nghttp2_nv nv = MAKE_NV("x-rel", "nghttp2");
nghttp2_nv *resnva;
nghttp2_hd_inflate_init(&inflater, NGHTTP2_HD_SIDE_SERVER);
nghttp2_hd_inflate_init(&inflater, NGHTTP2_HD_SIDE_REQUEST);
CU_ASSERT(0 == nghttp2_hd_emit_newname_block(&buf, &buflen, &offset,
&nv, 1,
NGHTTP2_HD_SIDE_CLIENT));
NGHTTP2_HD_SIDE_REQUEST));
CU_ASSERT(1 == nghttp2_hd_inflate_hd(&inflater, &resnva, buf, offset));
assert_nv_equal(&nv, resnva, 1);
CU_ASSERT(1 == inflater.hd_table.len);
@ -339,11 +339,11 @@ void test_nghttp2_hd_inflate_clearall_inc(void)
nv.value = value;
nv.valuelen = sizeof(value);
nghttp2_hd_inflate_init(&inflater, NGHTTP2_HD_SIDE_SERVER);
nghttp2_hd_inflate_init(&inflater, NGHTTP2_HD_SIDE_REQUEST);
CU_ASSERT(0 == nghttp2_hd_emit_newname_block(&buf, &buflen, &offset,
&nv, 1,
NGHTTP2_HD_SIDE_CLIENT));
NGHTTP2_HD_SIDE_REQUEST));
CU_ASSERT(1 == nghttp2_hd_inflate_hd(&inflater, &resnva, buf, offset));
assert_nv_equal(&nv, resnva, 1);
CU_ASSERT(0 == inflater.hd_table.len);
@ -366,7 +366,7 @@ void test_nghttp2_hd_inflate_clearall_inc(void)
offset = 0;
CU_ASSERT(0 == nghttp2_hd_emit_newname_block(&buf, &buflen, &offset,
&nv, 1,
NGHTTP2_HD_SIDE_CLIENT));
NGHTTP2_HD_SIDE_REQUEST));
CU_ASSERT(1 == nghttp2_hd_inflate_hd(&inflater, &resnva, buf, offset));
assert_nv_equal(&nv, resnva, 1);
CU_ASSERT(1 == inflater.hd_table.len);
@ -537,8 +537,8 @@ void test_nghttp2_hd_deflate_inflate(void)
MAKE_NV("x-cache-lookup", "HIT from alphabravo:3128"),
};
nghttp2_hd_deflate_init(&deflater, NGHTTP2_HD_SIDE_SERVER);
nghttp2_hd_inflate_init(&inflater, NGHTTP2_HD_SIDE_CLIENT);
nghttp2_hd_deflate_init(&deflater, NGHTTP2_HD_SIDE_REQUEST);
nghttp2_hd_inflate_init(&inflater, NGHTTP2_HD_SIDE_REQUEST);
check_deflate_inflate(&deflater, &inflater, nv1, ARRLEN(nv1));
check_deflate_inflate(&deflater, &inflater, nv2, ARRLEN(nv2));

View File

@ -404,7 +404,7 @@ 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);
nghttp2_hd_deflate_init(&deflater, NGHTTP2_HD_SIDE_REQUEST);
nvlen = nghttp2_nv_array_from_cstr(&nva, nv);
nghttp2_frame_headers_init(&frame.headers, NGHTTP2_FLAG_END_HEADERS,
@ -492,7 +492,7 @@ 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);
nghttp2_hd_deflate_init(&deflater, NGHTTP2_HD_SIDE_REQUEST);
nvlen = nghttp2_nv_array_from_cstr(&nva, nv);
nghttp2_frame_headers_init(&frame.headers, NGHTTP2_FLAG_END_HEADERS, 2,
NGHTTP2_PRI_DEFAULT, nva, nvlen);
@ -537,7 +537,7 @@ 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);
nghttp2_hd_deflate_init(&deflater, NGHTTP2_HD_SIDE_REQUEST);
nvlen = nghttp2_nv_array_from_cstr(&nva, nv);
nghttp2_frame_headers_init(&frame.headers, NGHTTP2_FLAG_END_HEADERS, 1,
NGHTTP2_PRI_DEFAULT, nva, nvlen);
@ -741,7 +741,7 @@ void test_nghttp2_session_continue(void)
nghttp2_session_server_new(&session, &callbacks, &user_data);
nghttp2_hd_deflate_init(&deflater, NGHTTP2_HD_SIDE_CLIENT);
nghttp2_hd_deflate_init(&deflater, NGHTTP2_HD_SIDE_REQUEST);
/* Make 2 HEADERS frames */
nvlen = nghttp2_nv_array_from_cstr(&nva, nv1);
@ -1905,7 +1905,7 @@ void test_nghttp2_submit_response_without_data(void)
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_hd_inflate_init(&inflater, NGHTTP2_HD_SIDE_RESPONSE);
nghttp2_session_open_stream(session, 1, NGHTTP2_FLAG_END_STREAM,
NGHTTP2_PRI_DEFAULT,
NGHTTP2_STREAM_OPENING, NULL);
@ -1968,7 +1968,7 @@ void test_nghttp2_submit_request_without_data(void)
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);
nghttp2_hd_inflate_init(&inflater, NGHTTP2_HD_SIDE_REQUEST);
CU_ASSERT(0 == nghttp2_submit_request(session, NGHTTP2_PRI_DEFAULT, nv,
&data_prd, NULL));
item = nghttp2_session_get_next_ob_item(session);
@ -2029,7 +2029,7 @@ void test_nghttp2_submit_request2_without_data(void)
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);
nghttp2_hd_inflate_init(&inflater, NGHTTP2_HD_SIDE_REQUEST);
CU_ASSERT(0 == nghttp2_submit_request2(session, NGHTTP2_PRI_DEFAULT,
nva, ARRLEN(nva), &data_prd, NULL));
item = nghttp2_session_get_next_ob_item(session);
@ -2186,7 +2186,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);
nghttp2_hd_inflate_init(&inflater, NGHTTP2_HD_SIDE_REQUEST);
CU_ASSERT(0 == nghttp2_submit_headers(session,
NGHTTP2_FLAG_END_STREAM,
1, NGHTTP2_PRI_DEFAULT,