增加GZIP压缩功能及对应的单元测试
This commit is contained in:
@@ -2,11 +2,11 @@
|
||||
#include <http_common.h>
|
||||
#include <http_half.h>
|
||||
#include <http_convert.h>
|
||||
#include <event2/buffer.h>
|
||||
|
||||
struct hf_content_converter
|
||||
struct hf_content_uncompress
|
||||
{
|
||||
/* MODE AND CALLBACKS */
|
||||
enum hf_content_conv_work_mode mode;
|
||||
unsigned int content_encode;
|
||||
hf_private_cb * data_cb;
|
||||
void * data_cb_user;
|
||||
@@ -17,31 +17,26 @@ struct hf_content_converter
|
||||
size_t sz_chunk;
|
||||
};
|
||||
|
||||
void hf_content_converter_destroy(struct hf_content_converter * cv_object)
|
||||
struct hf_content_compress
|
||||
{
|
||||
if (cv_object->z_stream_ptr && cv_object->mode == HF_CONTENT_CONV_COMPRASS)
|
||||
{
|
||||
(void)deflateEnd(cv_object->z_stream_ptr);
|
||||
free(cv_object->z_stream_ptr);
|
||||
}
|
||||
|
||||
if (cv_object->z_stream_ptr && cv_object->mode == HF_CONTENT_CONV_UNCOMPRASS)
|
||||
{
|
||||
(void)inflateEnd(cv_object->z_stream_ptr);
|
||||
free(cv_object->z_stream_ptr);
|
||||
}
|
||||
z_stream * z_stream_ptr;
|
||||
unsigned int content_encode;
|
||||
};
|
||||
|
||||
void hf_content_uncompress_destroy(struct hf_content_uncompress * cv_object)
|
||||
{
|
||||
(void) inflateEnd(cv_object->z_stream_ptr);
|
||||
free(cv_object->z_stream_ptr);
|
||||
cv_object->z_stream_ptr = NULL;
|
||||
free(cv_object);
|
||||
}
|
||||
|
||||
struct hf_content_converter * hf_content_converter_create(enum hf_content_conv_work_mode mode,
|
||||
unsigned int content_encode, hf_private_cb * data_cb, void * data_cb_user)
|
||||
struct hf_content_uncompress * hf_content_uncompress_create(unsigned int content_encode,
|
||||
hf_private_cb * data_cb, void * data_cb_user)
|
||||
{
|
||||
struct hf_content_converter * cv_object = ALLOC(struct hf_content_converter, 1);
|
||||
struct hf_content_uncompress * cv_object = ALLOC(struct hf_content_uncompress, 1);
|
||||
assert(data_cb != NULL);
|
||||
|
||||
cv_object->mode = mode;
|
||||
cv_object->content_encode = content_encode;
|
||||
cv_object->data_cb = data_cb;
|
||||
cv_object->data_cb_user = data_cb_user;
|
||||
@@ -55,8 +50,8 @@ struct hf_content_converter * hf_content_converter_create(enum hf_content_conv_w
|
||||
cv_object->z_stream_ptr->next_in = Z_NULL;
|
||||
|
||||
/* CHUNK, 4K */
|
||||
#define CHUNK_SIZE (1024 * 1024 * 4)
|
||||
cv_object->chunk = (unsigned char *)malloc(CHUNK_SIZE);
|
||||
#define CHUNK_SIZE (1024 * 1024 * 4)
|
||||
cv_object->chunk = (unsigned char *) malloc(CHUNK_SIZE);
|
||||
cv_object->sz_chunk = CHUNK_SIZE;
|
||||
|
||||
int ret = 0;
|
||||
@@ -79,16 +74,16 @@ __errout:
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int hf_content_converter_write(struct hf_content_converter * cv_object,
|
||||
int hf_content_uncompress_write(struct hf_content_uncompress * cv_object,
|
||||
struct http_half_private * hf_private, tfe_http_event http_ev, const unsigned char * data, size_t datalen)
|
||||
{
|
||||
z_stream * z_stream_ptr = cv_object->z_stream_ptr;
|
||||
z_stream_ptr->avail_in = (unsigned int)datalen;
|
||||
z_stream_ptr->next_in = (unsigned char *)data;
|
||||
z_stream_ptr->avail_in = (unsigned int) datalen;
|
||||
z_stream_ptr->next_in = (unsigned char *) data;
|
||||
|
||||
if (z_stream_ptr->avail_in == 0)
|
||||
{
|
||||
(void)inflateEnd(z_stream_ptr);
|
||||
(void) inflateEnd(z_stream_ptr);
|
||||
return Z_ERRNO;
|
||||
}
|
||||
|
||||
@@ -116,6 +111,90 @@ int hf_content_converter_write(struct hf_content_converter * cv_object,
|
||||
return ret;
|
||||
|
||||
__error:
|
||||
(void)inflateEnd(z_stream_ptr);
|
||||
(void) inflateEnd(z_stream_ptr);
|
||||
return ret;
|
||||
}
|
||||
|
||||
struct hf_content_compress * hf_content_compress_create(unsigned int content_encode)
|
||||
{
|
||||
struct hf_content_compress * cv_object = ALLOC(struct hf_content_compress, 1);
|
||||
cv_object->content_encode = content_encode;
|
||||
|
||||
/* ZSTREAM */
|
||||
cv_object->z_stream_ptr = ALLOC(z_stream, 1);
|
||||
cv_object->z_stream_ptr->zalloc = NULL;
|
||||
cv_object->z_stream_ptr->zfree = NULL;
|
||||
cv_object->z_stream_ptr->opaque = NULL;
|
||||
cv_object->z_stream_ptr->avail_in = 0;
|
||||
cv_object->z_stream_ptr->next_in = Z_NULL;
|
||||
|
||||
int __windows_bits = 0;
|
||||
if (content_encode == HTTP_ACCEPT_ENCODING_GZIP)
|
||||
{
|
||||
__windows_bits = MAX_WBITS + 16;
|
||||
}
|
||||
|
||||
if (content_encode == HTTP_ACCEPT_ENCODING_DEFLATE)
|
||||
{
|
||||
__windows_bits = -MAX_WBITS;
|
||||
}
|
||||
|
||||
int ret = deflateInit2(cv_object->z_stream_ptr, Z_DEFAULT_COMPRESSION,
|
||||
Z_DEFLATED, __windows_bits, 8, Z_DEFAULT_STRATEGY);
|
||||
|
||||
if (ret != Z_OK) goto __errout;
|
||||
return cv_object;
|
||||
|
||||
__errout:
|
||||
free(cv_object->z_stream_ptr);
|
||||
free(cv_object);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int hf_content_compress_write(struct hf_content_compress * cv_object,
|
||||
const unsigned char * in_data, size_t sz_in_data, struct evbuffer * out_ev_buf, int end)
|
||||
{
|
||||
#define SZ_IOVEC 2
|
||||
struct evbuffer_iovec v[SZ_IOVEC];
|
||||
|
||||
/* Reserve the space, because the length of the compressed data will be short
|
||||
* than uncompressed data in usually, we set the reserve space as much as sz_in_data */
|
||||
size_t __sz_reserve_space = sz_in_data > 512 ? sz_in_data : 512;
|
||||
int iov_count = evbuffer_reserve_space(out_ev_buf, __sz_reserve_space, v, SZ_IOVEC);
|
||||
if (iov_count < 1 || iov_count > SZ_IOVEC) return -1;
|
||||
|
||||
z_stream * z = cv_object->z_stream_ptr;
|
||||
z->next_in = (unsigned char *) in_data;
|
||||
z->avail_in = (unsigned int) sz_in_data;
|
||||
|
||||
unsigned int iov_offset = 0;
|
||||
z->next_out = (unsigned char *) v[iov_offset].iov_base;
|
||||
z->avail_out = (unsigned int) v[iov_offset].iov_len;
|
||||
|
||||
int flush = end ? Z_FINISH : Z_NO_FLUSH;
|
||||
int ret = 0;
|
||||
do
|
||||
{
|
||||
ret = deflate(z, flush);
|
||||
assert(ret != Z_STREAM_ERROR);
|
||||
assert(iov_offset < SZ_IOVEC);
|
||||
|
||||
if (z->avail_out == 0 || z->avail_in == 0)
|
||||
{
|
||||
unsigned int len = (unsigned int) v[iov_offset].iov_len - z->avail_out;
|
||||
v[iov_offset].iov_len = (size_t) len;
|
||||
|
||||
iov_offset++;
|
||||
z->next_out = (unsigned char *) v[iov_offset].iov_base;
|
||||
z->avail_out = (unsigned int) v[iov_offset].iov_len;
|
||||
}
|
||||
} while (z->avail_in > 0);
|
||||
|
||||
assert(end == 0 || ret == Z_STREAM_END);
|
||||
return evbuffer_commit_space(out_ev_buf, v, iov_count);
|
||||
}
|
||||
|
||||
void hf_content_compress_destroy(hf_content_compress * cv_object)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -332,10 +332,10 @@ static int __parser_callback_on_body(struct http_parser * parser, const char * a
|
||||
/* Create ungzip context */
|
||||
if (hf_private->content_encoding != HTTP_ACCEPT_ENCODING_NONE)
|
||||
{
|
||||
hf_private->cv_unpress_object = hf_content_converter_create(HF_CONTENT_CONV_UNCOMPRASS,
|
||||
hf_private->cv_uncompress_object = hf_content_uncompress_create(
|
||||
hf_private->content_encoding, hf_private->event_cb, hf_private->event_cb_user);
|
||||
|
||||
if (unlikely(hf_private->cv_unpress_object == NULL)) assert(0);
|
||||
if (unlikely(hf_private->cv_uncompress_object == NULL)) assert(0);
|
||||
}
|
||||
|
||||
hf_private->event_cb(hf_private, ev_body_begin, NULL, parser->content_length, hf_private->event_cb_user);
|
||||
@@ -345,10 +345,10 @@ static int __parser_callback_on_body(struct http_parser * parser, const char * a
|
||||
int ret = 0;
|
||||
if (hf_private->event_cb && length != 0)
|
||||
{
|
||||
if (hf_private->cv_unpress_object != NULL)
|
||||
if (hf_private->cv_uncompress_object != NULL)
|
||||
{
|
||||
ret = hf_content_converter_write(hf_private->cv_unpress_object, hf_private, ev_body_cont,
|
||||
(const unsigned char *)at, length);
|
||||
ret = hf_content_uncompress_write(hf_private->cv_uncompress_object, hf_private, ev_body_cont,
|
||||
(const unsigned char *) at, length);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user