Moved Huffman coding out of HTTP/2.

ngx_http_v2_huff_decode.c and ngx_http_v2_huff_encode.c are renamed
to ngx_http_huff_decode.c and ngx_http_huff_encode.c.
This commit is contained in:
Ruslan Ermilov 2021-12-21 07:54:16 +03:00
parent b5d022e797
commit 363505e806
9 changed files with 53 additions and 45 deletions

View File

@ -102,6 +102,11 @@ if [ $HTTP = YES ]; then
fi fi
if [ $HTTP_V2 = YES ]; then
HTTP_SRCS="$HTTP_SRCS $HTTP_HUFF_SRCS"
fi
# the module order is important # the module order is important
# ngx_http_static_module # ngx_http_static_module
# ngx_http_gzip_static_module # ngx_http_gzip_static_module
@ -414,8 +419,6 @@ if [ $HTTP = YES ]; then
ngx_module_srcs="src/http/v2/ngx_http_v2.c \ ngx_module_srcs="src/http/v2/ngx_http_v2.c \
src/http/v2/ngx_http_v2_table.c \ src/http/v2/ngx_http_v2_table.c \
src/http/v2/ngx_http_v2_encode.c \ src/http/v2/ngx_http_v2_encode.c \
src/http/v2/ngx_http_v2_huff_decode.c \
src/http/v2/ngx_http_v2_huff_encode.c \
src/http/v2/ngx_http_v2_module.c" src/http/v2/ngx_http_v2_module.c"
ngx_module_libs= ngx_module_libs=
ngx_module_link=$HTTP_V2 ngx_module_link=$HTTP_V2

View File

@ -255,3 +255,6 @@ NGX_WIN32_RC="src/os/win32/nginx.rc"
HTTP_FILE_CACHE_SRCS=src/http/ngx_http_file_cache.c HTTP_FILE_CACHE_SRCS=src/http/ngx_http_file_cache.c
HTTP_HUFF_SRCS="src/http/ngx_http_huff_decode.c
src/http/ngx_http_huff_encode.c"

View File

@ -3180,10 +3180,10 @@ ngx_http_grpc_parse_fragment(ngx_http_request_t *r, ngx_http_grpc_ctx_t *ctx,
ctx->field_rest -= size; ctx->field_rest -= size;
if (ctx->field_huffman) { if (ctx->field_huffman) {
if (ngx_http_v2_huff_decode(&ctx->field_state, p, size, if (ngx_http_huff_decode(&ctx->field_state, p, size,
&ctx->field_end, &ctx->field_end,
ctx->field_rest == 0, ctx->field_rest == 0,
r->connection->log) r->connection->log)
!= NGX_OK) != NGX_OK)
{ {
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
@ -3289,10 +3289,10 @@ ngx_http_grpc_parse_fragment(ngx_http_request_t *r, ngx_http_grpc_ctx_t *ctx,
ctx->field_rest -= size; ctx->field_rest -= size;
if (ctx->field_huffman) { if (ctx->field_huffman) {
if (ngx_http_v2_huff_decode(&ctx->field_state, p, size, if (ngx_http_huff_decode(&ctx->field_state, p, size,
&ctx->field_end, &ctx->field_end,
ctx->field_rest == 0, ctx->field_rest == 0,
r->connection->log) r->connection->log)
!= NGX_OK) != NGX_OK)
{ {
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,

View File

@ -167,6 +167,14 @@ ngx_uint_t ngx_http_degraded(ngx_http_request_t *);
#endif #endif
#if (NGX_HTTP_V2)
ngx_int_t ngx_http_huff_decode(u_char *state, u_char *src, size_t len,
u_char **dst, ngx_uint_t last, ngx_log_t *log);
size_t ngx_http_huff_encode(u_char *src, size_t len, u_char *dst,
ngx_uint_t lower);
#endif
extern ngx_module_t ngx_http_module; extern ngx_module_t ngx_http_module;
extern ngx_str_t ngx_http_html_default_types[]; extern ngx_str_t ngx_http_html_default_types[];

View File

@ -15,14 +15,14 @@ typedef struct {
u_char emit; u_char emit;
u_char sym; u_char sym;
u_char ending; u_char ending;
} ngx_http_v2_huff_decode_code_t; } ngx_http_huff_decode_code_t;
static ngx_inline ngx_int_t ngx_http_v2_huff_decode_bits(u_char *state, static ngx_inline ngx_int_t ngx_http_huff_decode_bits(u_char *state,
u_char *ending, ngx_uint_t bits, u_char **dst); u_char *ending, ngx_uint_t bits, u_char **dst);
static ngx_http_v2_huff_decode_code_t ngx_http_v2_huff_decode_codes[256][16] = static ngx_http_huff_decode_code_t ngx_http_huff_decode_codes[256][16] =
{ {
/* 0 */ /* 0 */
{ {
@ -2640,7 +2640,7 @@ static ngx_http_v2_huff_decode_code_t ngx_http_v2_huff_decode_codes[256][16] =
ngx_int_t ngx_int_t
ngx_http_v2_huff_decode(u_char *state, u_char *src, size_t len, u_char **dst, ngx_http_huff_decode(u_char *state, u_char *src, size_t len, u_char **dst,
ngx_uint_t last, ngx_log_t *log) ngx_uint_t last, ngx_log_t *log)
{ {
u_char *end, ch, ending; u_char *end, ch, ending;
@ -2653,7 +2653,7 @@ ngx_http_v2_huff_decode(u_char *state, u_char *src, size_t len, u_char **dst,
while (src != end) { while (src != end) {
ch = *src++; ch = *src++;
if (ngx_http_v2_huff_decode_bits(state, &ending, ch >> 4, dst) if (ngx_http_huff_decode_bits(state, &ending, ch >> 4, dst)
!= NGX_OK) != NGX_OK)
{ {
ngx_log_debug2(NGX_LOG_DEBUG_HTTP, log, 0, ngx_log_debug2(NGX_LOG_DEBUG_HTTP, log, 0,
@ -2663,7 +2663,7 @@ ngx_http_v2_huff_decode(u_char *state, u_char *src, size_t len, u_char **dst,
return NGX_ERROR; return NGX_ERROR;
} }
if (ngx_http_v2_huff_decode_bits(state, &ending, ch & 0xf, dst) if (ngx_http_huff_decode_bits(state, &ending, ch & 0xf, dst)
!= NGX_OK) != NGX_OK)
{ {
ngx_log_debug2(NGX_LOG_DEBUG_HTTP, log, 0, ngx_log_debug2(NGX_LOG_DEBUG_HTTP, log, 0,
@ -2692,12 +2692,12 @@ ngx_http_v2_huff_decode(u_char *state, u_char *src, size_t len, u_char **dst,
static ngx_inline ngx_int_t static ngx_inline ngx_int_t
ngx_http_v2_huff_decode_bits(u_char *state, u_char *ending, ngx_uint_t bits, ngx_http_huff_decode_bits(u_char *state, u_char *ending, ngx_uint_t bits,
u_char **dst) u_char **dst)
{ {
ngx_http_v2_huff_decode_code_t code; ngx_http_huff_decode_code_t code;
code = ngx_http_v2_huff_decode_codes[*state][bits]; code = ngx_http_huff_decode_codes[*state][bits];
if (code.next == *state) { if (code.next == *state) {
return NGX_ERROR; return NGX_ERROR;

View File

@ -14,10 +14,10 @@
typedef struct { typedef struct {
uint32_t code; uint32_t code;
uint32_t len; uint32_t len;
} ngx_http_v2_huff_encode_code_t; } ngx_http_huff_encode_code_t;
static ngx_http_v2_huff_encode_code_t ngx_http_v2_huff_encode_table[256] = static ngx_http_huff_encode_code_t ngx_http_huff_encode_table[256] =
{ {
{0x00001ff8, 13}, {0x007fffd8, 23}, {0x0fffffe2, 28}, {0x0fffffe3, 28}, {0x00001ff8, 13}, {0x007fffd8, 23}, {0x0fffffe2, 28}, {0x0fffffe3, 28},
{0x0fffffe4, 28}, {0x0fffffe5, 28}, {0x0fffffe6, 28}, {0x0fffffe7, 28}, {0x0fffffe4, 28}, {0x0fffffe5, 28}, {0x0fffffe6, 28}, {0x0fffffe7, 28},
@ -87,7 +87,7 @@ static ngx_http_v2_huff_encode_code_t ngx_http_v2_huff_encode_table[256] =
/* same as above, but embeds lowercase transformation */ /* same as above, but embeds lowercase transformation */
static ngx_http_v2_huff_encode_code_t ngx_http_v2_huff_encode_table_lc[256] = static ngx_http_huff_encode_code_t ngx_http_huff_encode_table_lc[256] =
{ {
{0x00001ff8, 13}, {0x007fffd8, 23}, {0x0fffffe2, 28}, {0x0fffffe3, 28}, {0x00001ff8, 13}, {0x007fffd8, 23}, {0x0fffffe2, 28}, {0x0fffffe3, 28},
{0x0fffffe4, 28}, {0x0fffffe5, 28}, {0x0fffffe6, 28}, {0x0fffffe7, 28}, {0x0fffffe4, 28}, {0x0fffffe5, 28}, {0x0fffffe6, 28}, {0x0fffffe7, 28},
@ -161,10 +161,10 @@ static ngx_http_v2_huff_encode_code_t ngx_http_v2_huff_encode_table_lc[256] =
#if (NGX_HAVE_LITTLE_ENDIAN) #if (NGX_HAVE_LITTLE_ENDIAN)
#if (NGX_HAVE_GCC_BSWAP64) #if (NGX_HAVE_GCC_BSWAP64)
#define ngx_http_v2_huff_encode_buf(dst, buf) \ #define ngx_http_huff_encode_buf(dst, buf) \
(*(uint64_t *) (dst) = __builtin_bswap64(buf)) (*(uint64_t *) (dst) = __builtin_bswap64(buf))
#else #else
#define ngx_http_v2_huff_encode_buf(dst, buf) \ #define ngx_http_huff_encode_buf(dst, buf) \
((dst)[0] = (u_char) ((buf) >> 56), \ ((dst)[0] = (u_char) ((buf) >> 56), \
(dst)[1] = (u_char) ((buf) >> 48), \ (dst)[1] = (u_char) ((buf) >> 48), \
(dst)[2] = (u_char) ((buf) >> 40), \ (dst)[2] = (u_char) ((buf) >> 40), \
@ -176,28 +176,28 @@ static ngx_http_v2_huff_encode_code_t ngx_http_v2_huff_encode_table_lc[256] =
#endif #endif
#else /* !NGX_HAVE_LITTLE_ENDIAN */ #else /* !NGX_HAVE_LITTLE_ENDIAN */
#define ngx_http_v2_huff_encode_buf(dst, buf) \ #define ngx_http_huff_encode_buf(dst, buf) \
(*(uint64_t *) (dst) = (buf)) (*(uint64_t *) (dst) = (buf))
#endif #endif
#else /* NGX_PTR_SIZE == 4 */ #else /* NGX_PTR_SIZE == 4 */
#define ngx_http_v2_huff_encode_buf(dst, buf) \ #define ngx_http_huff_encode_buf(dst, buf) \
(*(uint32_t *) (dst) = htonl(buf)) (*(uint32_t *) (dst) = htonl(buf))
#endif #endif
size_t size_t
ngx_http_v2_huff_encode(u_char *src, size_t len, u_char *dst, ngx_uint_t lower) ngx_http_huff_encode(u_char *src, size_t len, u_char *dst, ngx_uint_t lower)
{ {
u_char *end; u_char *end;
size_t hlen; size_t hlen;
ngx_uint_t buf, pending, code; ngx_uint_t buf, pending, code;
ngx_http_v2_huff_encode_code_t *table, *next; ngx_http_huff_encode_code_t *table, *next;
table = lower ? ngx_http_v2_huff_encode_table_lc table = lower ? ngx_http_huff_encode_table_lc
: ngx_http_v2_huff_encode_table; : ngx_http_huff_encode_table;
hlen = 0; hlen = 0;
buf = 0; buf = 0;
pending = 0; pending = 0;
@ -224,7 +224,7 @@ ngx_http_v2_huff_encode(u_char *src, size_t len, u_char *dst, ngx_uint_t lower)
buf |= code >> pending; buf |= code >> pending;
ngx_http_v2_huff_encode_buf(&dst[hlen], buf); ngx_http_huff_encode_buf(&dst[hlen], buf);
hlen += sizeof(buf); hlen += sizeof(buf);

View File

@ -1600,10 +1600,10 @@ ngx_http_v2_state_field_huff(ngx_http_v2_connection_t *h2c, u_char *pos,
h2c->state.length -= size; h2c->state.length -= size;
h2c->state.field_rest -= size; h2c->state.field_rest -= size;
if (ngx_http_v2_huff_decode(&h2c->state.field_state, pos, size, if (ngx_http_huff_decode(&h2c->state.field_state, pos, size,
&h2c->state.field_end, &h2c->state.field_end,
h2c->state.field_rest == 0, h2c->state.field_rest == 0,
h2c->connection->log) h2c->connection->log)
!= NGX_OK) != NGX_OK)
{ {
ngx_log_error(NGX_LOG_INFO, h2c->connection->log, 0, ngx_log_error(NGX_LOG_INFO, h2c->connection->log, 0,

View File

@ -311,12 +311,6 @@ ngx_int_t ngx_http_v2_add_header(ngx_http_v2_connection_t *h2c,
ngx_int_t ngx_http_v2_table_size(ngx_http_v2_connection_t *h2c, size_t size); ngx_int_t ngx_http_v2_table_size(ngx_http_v2_connection_t *h2c, size_t size);
ngx_int_t ngx_http_v2_huff_decode(u_char *state, u_char *src, size_t len,
u_char **dst, ngx_uint_t last, ngx_log_t *log);
size_t ngx_http_v2_huff_encode(u_char *src, size_t len, u_char *dst,
ngx_uint_t lower);
#define ngx_http_v2_prefix(bits) ((1 << (bits)) - 1) #define ngx_http_v2_prefix(bits) ((1 << (bits)) - 1)

View File

@ -20,7 +20,7 @@ ngx_http_v2_string_encode(u_char *dst, u_char *src, size_t len, u_char *tmp,
{ {
size_t hlen; size_t hlen;
hlen = ngx_http_v2_huff_encode(src, len, tmp, lower); hlen = ngx_http_huff_encode(src, len, tmp, lower);
if (hlen > 0) { if (hlen > 0) {
*dst = NGX_HTTP_V2_ENCODE_HUFF; *dst = NGX_HTTP_V2_ENCODE_HUFF;