Enable name/value header compression on server side

This commit is contained in:
Tatsuhiro Tsujikawa 2012-09-15 00:47:17 +09:00
parent 088e4f15a2
commit 58619a4f97
6 changed files with 29 additions and 25 deletions

View File

@ -140,7 +140,8 @@ static int spdylay_session_new(spdylay_session **session_ptr,
uint16_t version,
const spdylay_session_callbacks *callbacks,
void *user_data,
size_t cli_certvec_length)
size_t cli_certvec_length,
int hd_comp)
{
int r;
if(version != SPDYLAY_PROTO_SPDY2 && version != SPDYLAY_PROTO_SPDY3) {
@ -172,6 +173,7 @@ static int spdylay_session_new(spdylay_session **session_ptr,
(*session_ptr)->max_recv_ctrl_frame_buf = (1 << 24)-1;
r = spdylay_zlib_deflate_hd_init(&(*session_ptr)->hd_deflater,
hd_comp,
(*session_ptr)->version);
if(r != 0) {
goto fail_hd_deflater;
@ -268,8 +270,9 @@ int spdylay_session_client_new(spdylay_session **session_ptr,
void *user_data)
{
int r;
/* For client side session, header compression is disabled. */
r = spdylay_session_new(session_ptr, version, callbacks, user_data,
SPDYLAY_INITIAL_CLIENT_CERT_VECTOR_LENGTH);
SPDYLAY_INITIAL_CLIENT_CERT_VECTOR_LENGTH, 0);
if(r == 0) {
/* IDs for use in client */
(*session_ptr)->next_stream_id = 1;
@ -285,8 +288,9 @@ int spdylay_session_server_new(spdylay_session **session_ptr,
void *user_data)
{
int r;
/* Enable header compression on server side. */
r = spdylay_session_new(session_ptr, version, callbacks, user_data,
0);
0, 1 /* hd_comp */);
if(r == 0) {
(*session_ptr)->server = 1;
/* IDs for use in client */

View File

@ -26,13 +26,7 @@
#include <assert.h>
/* By default header compression is disabled */
#undef ENABLE_HD_COMP
#ifdef ENABLE_HD_COMP
# define COMPRESSION_LEVEL 9
#else /* !ENABLE_HD_COMP */
# define COMPRESSION_LEVEL 0
#endif /* !ENABLE_HD_COMP */
#define COMPRESSION_LEVEL 9
#define WINDOW_BITS 11
#define MEM_LEVEL 1
@ -246,7 +240,8 @@ static const uint8_t* spdylay_select_hd_dict(size_t *len_ptr, uint16_t version)
return hd_dict;
}
int spdylay_zlib_deflate_hd_init(spdylay_zlib *deflater, uint16_t version)
int spdylay_zlib_deflate_hd_init(spdylay_zlib *deflater, int comp,
uint16_t version)
{
const unsigned char *hd_dict;
size_t hd_dict_length;
@ -259,8 +254,10 @@ int spdylay_zlib_deflate_hd_init(spdylay_zlib *deflater, uint16_t version)
if(hd_dict == NULL) {
return SPDYLAY_ERR_UNSUPPORTED_VERSION;
}
if(Z_OK != deflateInit2(&deflater->zst, COMPRESSION_LEVEL, Z_DEFLATED,
WINDOW_BITS, MEM_LEVEL, Z_DEFAULT_STRATEGY)) {
if(Z_OK != deflateInit2(&deflater->zst,
comp ? COMPRESSION_LEVEL : Z_NO_COMPRESSION,
Z_DEFLATED, WINDOW_BITS, MEM_LEVEL,
Z_DEFAULT_STRATEGY)) {
return SPDYLAY_ERR_ZLIB;
}
if(Z_OK != deflateSetDictionary(&deflater->zst, (uint8_t*)hd_dict,

View File

@ -40,8 +40,9 @@ typedef struct {
} spdylay_zlib;
/*
* Initializes |deflater| for deflating name/values pairs in the
* frame of the protocol version |version|.
* Initializes |deflater| for deflating name/values pairs in the frame
* of the protocol version |version|. If the |comp| is zero,
* compression level becomes 0, which means no compression.
*
* This function returns 0 if it succeeds, or one of the following
* negative error codes:
@ -51,7 +52,8 @@ typedef struct {
* SPDYLAY_ERR_UNSUPPORTED_VERSION
* The version is not supported.
*/
int spdylay_zlib_deflate_hd_init(spdylay_zlib *deflater, uint16_t version);
int spdylay_zlib_deflate_hd_init(spdylay_zlib *deflater, int comp,
uint16_t version);
/*
* Initializes |inflater| for inflating name/values pairs in the

View File

@ -299,7 +299,7 @@ static void run_spdylay_session_recv(void)
ud.df = &df;
spdylay_failmalloc_pause();
spdylay_zlib_deflate_hd_init(&deflater, SPDYLAY_PROTO_SPDY3);
spdylay_zlib_deflate_hd_init(&deflater, 1, SPDYLAY_PROTO_SPDY3);
spdylay_session_server_new(&session, SPDYLAY_PROTO_SPDY3, &callbacks, &ud);
spdylay_failmalloc_unpause();
@ -414,7 +414,7 @@ static void run_spdylay_frame_pack_syn_stream(void)
int rv;
spdylay_buffer_init(&inflatebuf, 4096);
rv = spdylay_zlib_deflate_hd_init(&deflater, SPDYLAY_PROTO_SPDY3);
rv = spdylay_zlib_deflate_hd_init(&deflater, 1, SPDYLAY_PROTO_SPDY3);
if(rv != 0) {
goto deflate_init_fail;
}

View File

@ -305,7 +305,7 @@ static void test_spdylay_frame_pack_syn_stream_version(uint16_t version)
size_t buflen = 0, nvbuflen = 0;
ssize_t framelen;
spdylay_zlib_deflate_hd_init(&deflater, version);
spdylay_zlib_deflate_hd_init(&deflater, 1, version);
spdylay_zlib_inflate_hd_init(&inflater, version);
spdylay_frame_syn_stream_init(&frame.syn_stream, version,
SPDYLAY_CTRL_FLAG_FIN, 65536, 1000000007, 3,
@ -358,8 +358,8 @@ void test_spdylay_frame_pack_syn_stream_frame_too_large(void)
const char *big_hds[] = { "header", big_val, NULL };
memset(big_val, '0', big_vallen);
big_val[big_vallen] = '\0';
spdylay_zlib_deflate_hd_init(&deflater, SPDYLAY_PROTO_SPDY3);
/* No compression */
spdylay_zlib_deflate_hd_init(&deflater, 0, SPDYLAY_PROTO_SPDY3);
spdylay_frame_syn_stream_init(&frame.syn_stream, SPDYLAY_PROTO_SPDY3,
SPDYLAY_CTRL_FLAG_FIN, 65536, 1000000007, 3,
spdylay_frame_nv_copy(big_hds));
@ -382,7 +382,7 @@ static void test_spdylay_frame_pack_syn_reply_version(uint16_t version)
uint8_t *buf = NULL, *nvbuf = NULL;
size_t buflen = 0, nvbuflen = 0;
ssize_t framelen;
spdylay_zlib_deflate_hd_init(&deflater, version);
spdylay_zlib_deflate_hd_init(&deflater, 1, version);
spdylay_zlib_inflate_hd_init(&inflater, version);
spdylay_frame_syn_reply_init(&frame.syn_reply, version,
SPDYLAY_CTRL_FLAG_FIN, 3,
@ -430,7 +430,7 @@ static void test_spdylay_frame_pack_headers_version(uint16_t version)
spdylay_buffer inflatebuf;
ssize_t framelen;
spdylay_buffer_init(&inflatebuf, 4096);
spdylay_zlib_deflate_hd_init(&deflater, version);
spdylay_zlib_deflate_hd_init(&deflater, 1, version);
spdylay_zlib_inflate_hd_init(&inflater, version);
spdylay_frame_headers_init(&frame.headers, version,
SPDYLAY_CTRL_FLAG_FIN, 3,

View File

@ -49,8 +49,9 @@ static void test_spdylay_zlib_with(uint16_t version)
size_t deflatebuf_max;
ssize_t deflatebuf_len;
spdylay_buffer_init(&buf, 4096);
CU_ASSERT(0 == spdylay_zlib_deflate_hd_init(&deflater, version));
CU_ASSERT(0 == spdylay_zlib_deflate_hd_init(&deflater, 1,
version));
CU_ASSERT(0 == spdylay_zlib_inflate_hd_init(&inflater, version));
deflatebuf_max = spdylay_zlib_deflate_hd_bound(&deflater, sizeof(msg));