HTTP/2: externalized various constants and interfaces.

This commit is contained in:
Maxim Dounin 2018-03-17 23:04:20 +03:00
parent b84b67bc0f
commit c554dd1434
6 changed files with 130 additions and 91 deletions

View File

@ -428,6 +428,7 @@ if [ $HTTP = YES ]; then
src/http/v2/ngx_http_v2_module.h" src/http/v2/ngx_http_v2_module.h"
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_huff_decode.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_huff_encode.c \
src/http/v2/ngx_http_v2_module.c" src/http/v2/ngx_http_v2_module.c"

View File

@ -54,8 +54,6 @@ typedef struct {
#define NGX_HTTP_V2_FRAME_BUFFER_SIZE 24 #define NGX_HTTP_V2_FRAME_BUFFER_SIZE 24
#define NGX_HTTP_V2_DEFAULT_FRAME_SIZE (1 << 14)
#define NGX_HTTP_V2_ROOT (void *) -1 #define NGX_HTTP_V2_ROOT (void *) -1

View File

@ -18,6 +18,7 @@
#define NGX_HTTP_V2_STATE_BUFFER_SIZE 16 #define NGX_HTTP_V2_STATE_BUFFER_SIZE 16
#define NGX_HTTP_V2_DEFAULT_FRAME_SIZE (1 << 14)
#define NGX_HTTP_V2_MAX_FRAME_SIZE ((1 << 24) - 1) #define NGX_HTTP_V2_MAX_FRAME_SIZE ((1 << 24) - 1)
#define NGX_HTTP_V2_INT_OCTETS 4 #define NGX_HTTP_V2_INT_OCTETS 4
@ -291,6 +292,9 @@ void ngx_http_v2_close_stream(ngx_http_v2_stream_t *stream, ngx_int_t rc);
ngx_int_t ngx_http_v2_send_output_queue(ngx_http_v2_connection_t *h2c); ngx_int_t ngx_http_v2_send_output_queue(ngx_http_v2_connection_t *h2c);
ngx_str_t *ngx_http_v2_get_static_name(ngx_uint_t index);
ngx_str_t *ngx_http_v2_get_static_value(ngx_uint_t index);
ngx_int_t ngx_http_v2_get_indexed_header(ngx_http_v2_connection_t *h2c, ngx_int_t ngx_http_v2_get_indexed_header(ngx_http_v2_connection_t *h2c,
ngx_uint_t index, ngx_uint_t name_only); ngx_uint_t index, ngx_uint_t name_only);
ngx_int_t ngx_http_v2_add_header(ngx_http_v2_connection_t *h2c, ngx_int_t ngx_http_v2_add_header(ngx_http_v2_connection_t *h2c,
@ -357,4 +361,53 @@ size_t ngx_http_v2_huff_encode(u_char *src, size_t len, u_char *dst,
#define ngx_http_v2_write_sid ngx_http_v2_write_uint32 #define ngx_http_v2_write_sid ngx_http_v2_write_uint32
#define ngx_http_v2_indexed(i) (128 + (i))
#define ngx_http_v2_inc_indexed(i) (64 + (i))
#define ngx_http_v2_write_name(dst, src, len, tmp) \
ngx_http_v2_string_encode(dst, src, len, tmp, 1)
#define ngx_http_v2_write_value(dst, src, len, tmp) \
ngx_http_v2_string_encode(dst, src, len, tmp, 0)
#define NGX_HTTP_V2_ENCODE_RAW 0
#define NGX_HTTP_V2_ENCODE_HUFF 0x80
#define NGX_HTTP_V2_AUTHORITY_INDEX 1
#define NGX_HTTP_V2_METHOD_INDEX 2
#define NGX_HTTP_V2_METHOD_GET_INDEX 2
#define NGX_HTTP_V2_METHOD_POST_INDEX 3
#define NGX_HTTP_V2_PATH_INDEX 4
#define NGX_HTTP_V2_PATH_ROOT_INDEX 4
#define NGX_HTTP_V2_SCHEME_HTTP_INDEX 6
#define NGX_HTTP_V2_SCHEME_HTTPS_INDEX 7
#define NGX_HTTP_V2_STATUS_INDEX 8
#define NGX_HTTP_V2_STATUS_200_INDEX 8
#define NGX_HTTP_V2_STATUS_204_INDEX 9
#define NGX_HTTP_V2_STATUS_206_INDEX 10
#define NGX_HTTP_V2_STATUS_304_INDEX 11
#define NGX_HTTP_V2_STATUS_400_INDEX 12
#define NGX_HTTP_V2_STATUS_404_INDEX 13
#define NGX_HTTP_V2_STATUS_500_INDEX 14
#define NGX_HTTP_V2_ACCEPT_ENCODING_INDEX 16
#define NGX_HTTP_V2_ACCEPT_LANGUAGE_INDEX 17
#define NGX_HTTP_V2_CONTENT_LENGTH_INDEX 28
#define NGX_HTTP_V2_CONTENT_TYPE_INDEX 31
#define NGX_HTTP_V2_DATE_INDEX 33
#define NGX_HTTP_V2_LAST_MODIFIED_INDEX 44
#define NGX_HTTP_V2_LOCATION_INDEX 46
#define NGX_HTTP_V2_SERVER_INDEX 54
#define NGX_HTTP_V2_USER_AGENT_INDEX 58
#define NGX_HTTP_V2_VARY_INDEX 59
u_char *ngx_http_v2_string_encode(u_char *dst, u_char *src, size_t len,
u_char *tmp, ngx_uint_t lower);
#endif /* _NGX_HTTP_V2_H_INCLUDED_ */ #endif /* _NGX_HTTP_V2_H_INCLUDED_ */

View File

@ -0,0 +1,62 @@
/*
* Copyright (C) Nginx, Inc.
* Copyright (C) Valentin V. Bartenev
*/
#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_http.h>
static u_char *ngx_http_v2_write_int(u_char *pos, ngx_uint_t prefix,
ngx_uint_t value);
u_char *
ngx_http_v2_string_encode(u_char *dst, u_char *src, size_t len, u_char *tmp,
ngx_uint_t lower)
{
size_t hlen;
hlen = ngx_http_v2_huff_encode(src, len, tmp, lower);
if (hlen > 0) {
*dst = NGX_HTTP_V2_ENCODE_HUFF;
dst = ngx_http_v2_write_int(dst, ngx_http_v2_prefix(7), hlen);
return ngx_cpymem(dst, tmp, hlen);
}
*dst = NGX_HTTP_V2_ENCODE_RAW;
dst = ngx_http_v2_write_int(dst, ngx_http_v2_prefix(7), len);
if (lower) {
ngx_strlow(dst, src, len);
return dst + len;
}
return ngx_cpymem(dst, src, len);
}
static u_char *
ngx_http_v2_write_int(u_char *pos, ngx_uint_t prefix, ngx_uint_t value)
{
if (value < prefix) {
*pos++ |= value;
return pos;
}
*pos++ |= prefix;
value -= prefix;
while (value >= 128) {
*pos++ = value % 128 + 128;
value /= 128;
}
*pos++ = (u_char) value;
return pos;
}

View File

@ -23,43 +23,6 @@
#define ngx_http_v2_literal_size(h) \ #define ngx_http_v2_literal_size(h) \
(ngx_http_v2_integer_octets(sizeof(h) - 1) + sizeof(h) - 1) (ngx_http_v2_integer_octets(sizeof(h) - 1) + sizeof(h) - 1)
#define ngx_http_v2_indexed(i) (128 + (i))
#define ngx_http_v2_inc_indexed(i) (64 + (i))
#define ngx_http_v2_write_name(dst, src, len, tmp) \
ngx_http_v2_string_encode(dst, src, len, tmp, 1)
#define ngx_http_v2_write_value(dst, src, len, tmp) \
ngx_http_v2_string_encode(dst, src, len, tmp, 0)
#define NGX_HTTP_V2_ENCODE_RAW 0
#define NGX_HTTP_V2_ENCODE_HUFF 0x80
#define NGX_HTTP_V2_AUTHORITY_INDEX 1
#define NGX_HTTP_V2_METHOD_GET_INDEX 2
#define NGX_HTTP_V2_PATH_INDEX 4
#define NGX_HTTP_V2_SCHEME_HTTP_INDEX 6
#define NGX_HTTP_V2_SCHEME_HTTPS_INDEX 7
#define NGX_HTTP_V2_STATUS_INDEX 8
#define NGX_HTTP_V2_STATUS_200_INDEX 8
#define NGX_HTTP_V2_STATUS_204_INDEX 9
#define NGX_HTTP_V2_STATUS_206_INDEX 10
#define NGX_HTTP_V2_STATUS_304_INDEX 11
#define NGX_HTTP_V2_STATUS_400_INDEX 12
#define NGX_HTTP_V2_STATUS_404_INDEX 13
#define NGX_HTTP_V2_STATUS_500_INDEX 14
#define NGX_HTTP_V2_ACCEPT_ENCODING_INDEX 16
#define NGX_HTTP_V2_ACCEPT_LANGUAGE_INDEX 17
#define NGX_HTTP_V2_CONTENT_LENGTH_INDEX 28
#define NGX_HTTP_V2_CONTENT_TYPE_INDEX 31
#define NGX_HTTP_V2_DATE_INDEX 33
#define NGX_HTTP_V2_LAST_MODIFIED_INDEX 44
#define NGX_HTTP_V2_LOCATION_INDEX 46
#define NGX_HTTP_V2_SERVER_INDEX 54
#define NGX_HTTP_V2_USER_AGENT_INDEX 58
#define NGX_HTTP_V2_VARY_INDEX 59
#define NGX_HTTP_V2_NO_TRAILERS (ngx_http_v2_out_frame_t *) -1 #define NGX_HTTP_V2_NO_TRAILERS (ngx_http_v2_out_frame_t *) -1
@ -93,10 +56,6 @@ static ngx_int_t ngx_http_v2_push_resources(ngx_http_request_t *r);
static ngx_int_t ngx_http_v2_push_resource(ngx_http_request_t *r, static ngx_int_t ngx_http_v2_push_resource(ngx_http_request_t *r,
ngx_str_t *path, ngx_str_t *binary); ngx_str_t *path, ngx_str_t *binary);
static u_char *ngx_http_v2_string_encode(u_char *dst, u_char *src, size_t len,
u_char *tmp, ngx_uint_t lower);
static u_char *ngx_http_v2_write_int(u_char *pos, ngx_uint_t prefix,
ngx_uint_t value);
static ngx_http_v2_out_frame_t *ngx_http_v2_create_headers_frame( static ngx_http_v2_out_frame_t *ngx_http_v2_create_headers_frame(
ngx_http_request_t *r, u_char *pos, u_char *end, ngx_uint_t fin); ngx_http_request_t *r, u_char *pos, u_char *end, ngx_uint_t fin);
static ngx_http_v2_out_frame_t *ngx_http_v2_create_push_frame( static ngx_http_v2_out_frame_t *ngx_http_v2_create_push_frame(
@ -1111,54 +1070,6 @@ ngx_http_v2_push_resource(ngx_http_request_t *r, ngx_str_t *path,
} }
static u_char *
ngx_http_v2_string_encode(u_char *dst, u_char *src, size_t len, u_char *tmp,
ngx_uint_t lower)
{
size_t hlen;
hlen = ngx_http_v2_huff_encode(src, len, tmp, lower);
if (hlen > 0) {
*dst = NGX_HTTP_V2_ENCODE_HUFF;
dst = ngx_http_v2_write_int(dst, ngx_http_v2_prefix(7), hlen);
return ngx_cpymem(dst, tmp, hlen);
}
*dst = NGX_HTTP_V2_ENCODE_RAW;
dst = ngx_http_v2_write_int(dst, ngx_http_v2_prefix(7), len);
if (lower) {
ngx_strlow(dst, src, len);
return dst + len;
}
return ngx_cpymem(dst, src, len);
}
static u_char *
ngx_http_v2_write_int(u_char *pos, ngx_uint_t prefix, ngx_uint_t value)
{
if (value < prefix) {
*pos++ |= value;
return pos;
}
*pos++ |= prefix;
value -= prefix;
while (value >= 128) {
*pos++ = value % 128 + 128;
value /= 128;
}
*pos++ = (u_char) value;
return pos;
}
static ngx_http_v2_out_frame_t * static ngx_http_v2_out_frame_t *
ngx_http_v2_create_headers_frame(ngx_http_request_t *r, u_char *pos, ngx_http_v2_create_headers_frame(ngx_http_request_t *r, u_char *pos,
u_char *end, ngx_uint_t fin) u_char *end, ngx_uint_t fin)

View File

@ -86,6 +86,20 @@ static ngx_http_v2_header_t ngx_http_v2_static_table[] = {
/ sizeof(ngx_http_v2_header_t)) / sizeof(ngx_http_v2_header_t))
ngx_str_t *
ngx_http_v2_get_static_name(ngx_uint_t index)
{
return &ngx_http_v2_static_table[index - 1].name;
}
ngx_str_t *
ngx_http_v2_get_static_value(ngx_uint_t index)
{
return &ngx_http_v2_static_table[index - 1].value;
}
ngx_int_t ngx_int_t
ngx_http_v2_get_indexed_header(ngx_http_v2_connection_t *h2c, ngx_uint_t index, ngx_http_v2_get_indexed_header(ngx_http_v2_connection_t *h2c, ngx_uint_t index,
ngx_uint_t name_only) ngx_uint_t name_only)