mirror of
https://github.com/nghttp2/nghttp2.git
synced 2025-12-08 02:58:53 +08:00
Integrate new header compression
This commit is contained in:
@@ -482,15 +482,13 @@ typedef struct {
|
||||
*/
|
||||
int32_t pri;
|
||||
/**
|
||||
* TODO Need to support binary header block.
|
||||
*
|
||||
* The name/value pairs. For i >= 0, ``nv[2*i]`` contains a pointer
|
||||
* to the name string and ``nv[2*i+1]`` contains a pointer to the
|
||||
* value string. The one beyond last value must be ``NULL``. That
|
||||
* is, if the |nv| contains N name/value pairs, ``nv[2*N]`` must be
|
||||
* ``NULL``. This member may be ``NULL``.
|
||||
* The name/value pairs.
|
||||
*/
|
||||
char **nv;
|
||||
nghttp2_nv *nva;
|
||||
/**
|
||||
* The number of name/value pairs in |nva|.
|
||||
*/
|
||||
size_t nvlen;
|
||||
nghttp2_headers_category cat;
|
||||
} nghttp2_headers;
|
||||
|
||||
@@ -1319,8 +1317,6 @@ const char* nghttp2_strerror(int lib_error_code);
|
||||
* ``:path``
|
||||
* Absolute path and parameters of this request (e.g., ``/foo``,
|
||||
* ``/foo;bar;haz?h=j&y=123``)
|
||||
* ``:version``
|
||||
* HTTP version (e.g., ``HTTP/1.1``)
|
||||
* ``:host``
|
||||
* The hostport portion of the URI for this request (e.g.,
|
||||
* ``example.org:443``). This is the same as the HTTP "Host" header
|
||||
@@ -1388,8 +1384,6 @@ int nghttp2_submit_request(nghttp2_session *session, int32_t pri,
|
||||
*
|
||||
* ``:status``
|
||||
* HTTP status code (e.g., ``200`` or ``200 OK``)
|
||||
* ``:version``
|
||||
* HTTP response version (e.g., ``HTTP/1.1``)
|
||||
*
|
||||
* If the |session| is initialized with the version
|
||||
* :macro:`NGHTTP2_PROTO_SPDY2`, the above names are translated to
|
||||
|
||||
@@ -68,214 +68,6 @@ void nghttp2_frame_unpack_frame_hd(nghttp2_frame_hd *hd, const uint8_t* buf)
|
||||
hd->stream_id = nghttp2_get_uint32(&buf[4]) & NGHTTP2_STREAM_ID_MASK;
|
||||
}
|
||||
|
||||
ssize_t nghttp2_frame_alloc_pack_nv(uint8_t **buf_ptr,
|
||||
size_t *buflen_ptr,
|
||||
uint8_t **nvbuf_ptr,
|
||||
size_t *nvbuflen_ptr,
|
||||
char **nv, size_t nv_offset,
|
||||
size_t len_size,
|
||||
nghttp2_zlib *deflater)
|
||||
{
|
||||
size_t nvspace;
|
||||
size_t maxframelen;
|
||||
ssize_t framelen;
|
||||
int r;
|
||||
nvspace = nghttp2_frame_count_nv_space(nv, len_size);
|
||||
r = nghttp2_reserve_buffer(nvbuf_ptr, nvbuflen_ptr, nvspace);
|
||||
if(r != 0) {
|
||||
return NGHTTP2_ERR_NOMEM;
|
||||
}
|
||||
maxframelen = nv_offset+nghttp2_zlib_deflate_hd_bound(deflater, nvspace);
|
||||
r = nghttp2_reserve_buffer(buf_ptr, buflen_ptr, maxframelen);
|
||||
if(r != 0) {
|
||||
return NGHTTP2_ERR_NOMEM;
|
||||
}
|
||||
nghttp2_frame_pack_nv(*nvbuf_ptr, nv, len_size);
|
||||
framelen = nghttp2_zlib_deflate_hd(deflater,
|
||||
(*buf_ptr)+nv_offset,
|
||||
maxframelen-nv_offset,
|
||||
*nvbuf_ptr, nvspace);
|
||||
if(framelen < 0) {
|
||||
return framelen;
|
||||
}
|
||||
framelen += nv_offset;
|
||||
|
||||
if(framelen - NGHTTP2_FRAME_HEAD_LENGTH >= 1 << 16) {
|
||||
/* Max frame size is 2**16 - 1 */
|
||||
return NGHTTP2_ERR_FRAME_TOO_LARGE;
|
||||
}
|
||||
return framelen;
|
||||
}
|
||||
|
||||
int nghttp2_frame_count_unpack_nv_space(size_t *nvlen_ptr, size_t *buflen_ptr,
|
||||
nghttp2_buffer *in, size_t len_size)
|
||||
{
|
||||
uint32_t n;
|
||||
size_t buflen = 0;
|
||||
size_t nvlen = 0;
|
||||
size_t off = 0;
|
||||
size_t inlen = nghttp2_buffer_length(in);
|
||||
size_t i;
|
||||
nghttp2_buffer_reader reader;
|
||||
if(inlen < len_size) {
|
||||
return NGHTTP2_ERR_INVALID_FRAME;
|
||||
}
|
||||
nghttp2_buffer_reader_init(&reader, in);
|
||||
|
||||
/* TODO limit n in a reasonable number */
|
||||
n = nghttp2_frame_get_nv_len(&reader);
|
||||
off += len_size;
|
||||
for(i = 0; i < n; ++i) {
|
||||
uint32_t len;
|
||||
size_t j;
|
||||
for(j = 0; j < 2; ++j) {
|
||||
if(inlen-off < len_size) {
|
||||
return NGHTTP2_ERR_INVALID_FRAME;
|
||||
}
|
||||
len = nghttp2_frame_get_nv_len(&reader);
|
||||
off += len_size;
|
||||
if(inlen-off < len) {
|
||||
return NGHTTP2_ERR_INVALID_FRAME;
|
||||
}
|
||||
buflen += len+1;
|
||||
off += len;
|
||||
if(j == 0) {
|
||||
nghttp2_buffer_reader_advance(&reader, len);
|
||||
}
|
||||
}
|
||||
nvlen += nghttp2_buffer_reader_count(&reader, len, '\0');
|
||||
++nvlen;
|
||||
}
|
||||
if(inlen == off) {
|
||||
*nvlen_ptr = nvlen;
|
||||
*buflen_ptr = buflen+(nvlen*2+1)*sizeof(char*);
|
||||
return 0;
|
||||
} else {
|
||||
return NGHTTP2_ERR_INVALID_FRAME;
|
||||
}
|
||||
}
|
||||
|
||||
int nghttp2_frame_unpack_nv(char ***nv_ptr, nghttp2_buffer *in,
|
||||
size_t len_size)
|
||||
{
|
||||
size_t nvlen, buflen;
|
||||
int r;
|
||||
size_t i;
|
||||
char *buf, **idx, *data;
|
||||
uint32_t n;
|
||||
int invalid_header_block = 0;
|
||||
nghttp2_buffer_reader reader;
|
||||
r = nghttp2_frame_count_unpack_nv_space(&nvlen, &buflen, in, len_size);
|
||||
if(r != 0) {
|
||||
return r;
|
||||
}
|
||||
|
||||
buf = malloc(buflen);
|
||||
if(buf == NULL) {
|
||||
return NGHTTP2_ERR_NOMEM;
|
||||
}
|
||||
nghttp2_buffer_reader_init(&reader, in);
|
||||
idx = (char**)buf;
|
||||
data = buf+(nvlen*2+1)*sizeof(char*);
|
||||
n = nghttp2_frame_get_nv_len(&reader);
|
||||
for(i = 0; i < n; ++i) {
|
||||
uint32_t len;
|
||||
char *name, *val;
|
||||
char *stop;
|
||||
int multival;
|
||||
len = nghttp2_frame_get_nv_len(&reader);
|
||||
if(len == 0) {
|
||||
invalid_header_block = 1;
|
||||
}
|
||||
name = data;
|
||||
nghttp2_buffer_reader_data(&reader, (uint8_t*)data, len);
|
||||
for(stop = data+len; data != stop; ++data) {
|
||||
unsigned char c = *data;
|
||||
if(c < 0x20 || c > 0x7e || ('A' <= c && c <= 'Z')) {
|
||||
invalid_header_block = 1;
|
||||
}
|
||||
}
|
||||
*data = '\0';
|
||||
++data;
|
||||
|
||||
len = nghttp2_frame_get_nv_len(&reader);
|
||||
val = data;
|
||||
nghttp2_buffer_reader_data(&reader, (uint8_t*)data, len);
|
||||
|
||||
multival = 0;
|
||||
for(stop = data+len; data != stop; ++data) {
|
||||
if(*data == '\0') {
|
||||
*idx++ = name;
|
||||
*idx++ = val;
|
||||
if(val == data) {
|
||||
invalid_header_block = 1;
|
||||
}
|
||||
val = data+1;
|
||||
multival = 1;
|
||||
}
|
||||
}
|
||||
*data = '\0';
|
||||
/* Check last header value is empty if NULL separator was
|
||||
found. */
|
||||
if(multival && val == data) {
|
||||
invalid_header_block = 1;
|
||||
}
|
||||
++data;
|
||||
|
||||
*idx++ = name;
|
||||
*idx++ = val;
|
||||
}
|
||||
*idx = NULL;
|
||||
assert((size_t)((char*)idx - buf) == (nvlen*2)*sizeof(char*));
|
||||
*nv_ptr = (char**)buf;
|
||||
if(!invalid_header_block) {
|
||||
nghttp2_frame_nv_sort(*nv_ptr);
|
||||
for(i = 2; i < nvlen*2; i += 2) {
|
||||
if(strcmp((*nv_ptr)[i-2], (*nv_ptr)[i]) == 0 &&
|
||||
(*nv_ptr)[i-2] != (*nv_ptr)[i]) {
|
||||
invalid_header_block = 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
return invalid_header_block ? NGHTTP2_ERR_INVALID_HEADER_BLOCK : 0;
|
||||
}
|
||||
|
||||
size_t nghttp2_frame_count_nv_space(char **nv, size_t len_size)
|
||||
{
|
||||
size_t sum = len_size;
|
||||
int i;
|
||||
const char *prev = "";
|
||||
size_t prevlen = 0;
|
||||
size_t prevvallen = 0;
|
||||
for(i = 0; nv[i]; i += 2) {
|
||||
const char *key = nv[i];
|
||||
const char *val = nv[i+1];
|
||||
size_t keylen = strlen(key);
|
||||
size_t vallen = strlen(val);
|
||||
if(prevlen == keylen && memcmp(prev, key, keylen) == 0) {
|
||||
if(vallen) {
|
||||
if(prevvallen) {
|
||||
/* Join previous value, with NULL character */
|
||||
sum += vallen+1;
|
||||
prevvallen = vallen;
|
||||
} else {
|
||||
/* Previous value is empty. In this case, drop the
|
||||
previous. */
|
||||
sum += vallen;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
prev = key;
|
||||
prevlen = keylen;
|
||||
prevvallen = vallen;
|
||||
/* SPDY NV header does not include terminating NULL byte */
|
||||
sum += keylen+vallen+len_size*2;
|
||||
}
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
|
||||
ssize_t nghttp2_frame_pack_nv(uint8_t *buf, char **nv, size_t len_size)
|
||||
{
|
||||
int i;
|
||||
@@ -404,18 +196,19 @@ static void nghttp2_frame_set_hd(nghttp2_frame_hd *hd, uint16_t length,
|
||||
|
||||
void nghttp2_frame_headers_init(nghttp2_headers *frame,
|
||||
uint8_t flags, int32_t stream_id, int32_t pri,
|
||||
char **nv)
|
||||
nghttp2_nv *nva, size_t nvlen)
|
||||
{
|
||||
memset(frame, 0, sizeof(nghttp2_headers));
|
||||
nghttp2_frame_set_hd(&frame->hd, 0, NGHTTP2_HEADERS, flags, stream_id);
|
||||
frame->pri = pri;
|
||||
frame->nv = nv;
|
||||
frame->nva = nva;
|
||||
frame->nvlen = nvlen;
|
||||
frame->cat = NGHTTP2_HCAT_START_STREAM;
|
||||
}
|
||||
|
||||
void nghttp2_frame_headers_free(nghttp2_headers *frame)
|
||||
{
|
||||
nghttp2_frame_nv_del(frame->nv);
|
||||
nghttp2_nv_array_del(frame->nva);
|
||||
}
|
||||
|
||||
void nghttp2_frame_priority_init(nghttp2_priority *frame, int32_t stream_id,
|
||||
@@ -517,31 +310,36 @@ void nghttp2_frame_data_init(nghttp2_data *frame, uint8_t flags,
|
||||
void nghttp2_frame_data_free(nghttp2_data *frame)
|
||||
{}
|
||||
|
||||
static size_t headers_nv_offset(nghttp2_headers *frame)
|
||||
{
|
||||
if(frame->hd.flags & NGHTTP2_FLAG_PRIORITY) {
|
||||
return NGHTTP2_FRAME_HEAD_LENGTH + 4;
|
||||
} else {
|
||||
return NGHTTP2_FRAME_HEAD_LENGTH;
|
||||
}
|
||||
}
|
||||
|
||||
ssize_t nghttp2_frame_pack_headers(uint8_t **buf_ptr,
|
||||
size_t *buflen_ptr,
|
||||
uint8_t **nvbuf_ptr,
|
||||
size_t *nvbuflen_ptr,
|
||||
nghttp2_headers *frame,
|
||||
nghttp2_zlib *deflater)
|
||||
nghttp2_hd_context *deflater)
|
||||
{
|
||||
ssize_t framelen;
|
||||
size_t len_size = nghttp2_frame_get_len_size();
|
||||
ssize_t nv_offset;
|
||||
if(frame->hd.flags & NGHTTP2_FLAG_PRIORITY) {
|
||||
nv_offset = NGHTTP2_FRAME_HEAD_LENGTH + 4;
|
||||
} else {
|
||||
nv_offset = NGHTTP2_FRAME_HEAD_LENGTH;
|
||||
}
|
||||
framelen = nghttp2_frame_alloc_pack_nv(buf_ptr, buflen_ptr,
|
||||
nvbuf_ptr, nvbuflen_ptr,
|
||||
frame->nv,
|
||||
nv_offset,
|
||||
len_size,
|
||||
deflater);
|
||||
if(framelen < 0) {
|
||||
return framelen;
|
||||
size_t nv_offset = headers_nv_offset(frame);
|
||||
ssize_t rv;
|
||||
rv = nghttp2_hd_deflate_hd(deflater, buf_ptr, buflen_ptr, nv_offset,
|
||||
frame->nva, frame->nvlen);
|
||||
if(rv < 0) {
|
||||
return rv;
|
||||
}
|
||||
framelen = rv + nv_offset;
|
||||
frame->hd.length = framelen - NGHTTP2_FRAME_HEAD_LENGTH;
|
||||
/* If frame->nvlen == 0, *buflen_ptr may be smaller than
|
||||
nv_offset */
|
||||
rv = nghttp2_reserve_buffer(buf_ptr, buflen_ptr, nv_offset);
|
||||
if(rv < 0) {
|
||||
return rv;
|
||||
}
|
||||
memset(*buf_ptr, 0, nv_offset);
|
||||
/* pack ctrl header after length is determined */
|
||||
nghttp2_frame_pack_frame_hd(*buf_ptr, &frame->hd);
|
||||
@@ -554,16 +352,24 @@ ssize_t nghttp2_frame_pack_headers(uint8_t **buf_ptr,
|
||||
int nghttp2_frame_unpack_headers(nghttp2_headers *frame,
|
||||
const uint8_t *head, size_t headlen,
|
||||
const uint8_t *payload, size_t payloadlen,
|
||||
nghttp2_buffer *inflatebuf)
|
||||
nghttp2_hd_context *inflater)
|
||||
{
|
||||
int r;
|
||||
size_t len_size = nghttp2_frame_get_len_size();
|
||||
ssize_t r;
|
||||
size_t pnv_offset;
|
||||
r = nghttp2_frame_unpack_headers_without_nv(frame, head, headlen,
|
||||
payload, payloadlen);
|
||||
if(r == 0) {
|
||||
r = nghttp2_frame_unpack_nv(&frame->nv, inflatebuf, len_size);
|
||||
if(r < 0) {
|
||||
return r;
|
||||
}
|
||||
return r;
|
||||
pnv_offset = headers_nv_offset(frame) - NGHTTP2_FRAME_HEAD_LENGTH;
|
||||
r = nghttp2_hd_inflate_hd(inflater, &frame->nva,
|
||||
(uint8_t*)payload + pnv_offset,
|
||||
payloadlen - pnv_offset);
|
||||
if(r < 0) {
|
||||
return r;
|
||||
}
|
||||
frame->nvlen = r;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int nghttp2_frame_unpack_headers_without_nv(nghttp2_headers *frame,
|
||||
@@ -574,17 +380,15 @@ int nghttp2_frame_unpack_headers_without_nv(nghttp2_headers *frame,
|
||||
{
|
||||
nghttp2_frame_unpack_frame_hd(&frame->hd, head);
|
||||
if(head[3] & NGHTTP2_FLAG_PRIORITY) {
|
||||
if(payloadlen != 4) {
|
||||
if(payloadlen < 4) {
|
||||
return NGHTTP2_ERR_INVALID_FRAME;
|
||||
}
|
||||
frame->pri = nghttp2_get_uint32(payload) & NGHTTP2_PRIORITY_MASK;
|
||||
} else {
|
||||
if(payloadlen != 0) {
|
||||
return NGHTTP2_ERR_INVALID_FRAME;
|
||||
}
|
||||
frame->pri = NGHTTP2_PRI_DEFAULT;
|
||||
}
|
||||
frame->nv = NULL;
|
||||
frame->nva = NULL;
|
||||
frame->nvlen = 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -847,7 +651,78 @@ int nghttp2_nv_equal(const nghttp2_nv *a, const nghttp2_nv *b)
|
||||
memcmp(a->value, b->value, a->valuelen) == 0;
|
||||
}
|
||||
|
||||
void nghttp2_nv_array_free(nghttp2_nv *nva)
|
||||
void nghttp2_nv_array_del(nghttp2_nv *nva)
|
||||
{
|
||||
free(nva);
|
||||
}
|
||||
|
||||
static int nghttp2_nv_name_compar(const void *lhs, const void *rhs)
|
||||
{
|
||||
nghttp2_nv *a = (nghttp2_nv*)lhs, *b = (nghttp2_nv*)rhs;
|
||||
if(a->namelen == b->namelen) {
|
||||
return memcmp(a->name, b->name, a->namelen);
|
||||
} else if(a->namelen < b->namelen) {
|
||||
int rv = memcmp(a->name, b->name, a->namelen);
|
||||
if(rv == 0) {
|
||||
return -1;
|
||||
} else {
|
||||
return rv;
|
||||
}
|
||||
} else {
|
||||
int rv = memcmp(a->name, b->name, b->namelen);
|
||||
if(rv == 0) {
|
||||
return 1;
|
||||
} else {
|
||||
return rv;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void nghttp2_nv_array_sort(nghttp2_nv *nva, size_t nvlen)
|
||||
{
|
||||
qsort(nva, nvlen, sizeof(nghttp2_nv), nghttp2_nv_name_compar);
|
||||
}
|
||||
|
||||
ssize_t nghttp2_nv_array_from_cstr(nghttp2_nv **nva_ptr, const char **nv)
|
||||
{
|
||||
int i;
|
||||
uint8_t *data;
|
||||
size_t buflen = 0, nvlen = 0;
|
||||
nghttp2_nv *p;
|
||||
for(i = 0; nv[i]; ++i) {
|
||||
size_t len = strlen(nv[i]);
|
||||
if(len > (1 << 16) - 1) {
|
||||
return NGHTTP2_ERR_INVALID_ARGUMENT;
|
||||
}
|
||||
buflen += len;
|
||||
}
|
||||
nvlen = i/2;
|
||||
if(nvlen == 0) {
|
||||
*nva_ptr = NULL;
|
||||
return 0;
|
||||
}
|
||||
buflen += sizeof(nghttp2_nv)*nvlen;
|
||||
*nva_ptr = malloc(buflen);
|
||||
if(*nva_ptr == NULL) {
|
||||
return NGHTTP2_ERR_NOMEM;
|
||||
}
|
||||
p = *nva_ptr;
|
||||
data = (uint8_t*)(*nva_ptr) + sizeof(nghttp2_nv)*nvlen;
|
||||
|
||||
for(i = 0; nv[i]; i += 2) {
|
||||
size_t len = strlen(nv[i]);
|
||||
memcpy(data, nv[i], len);
|
||||
p->name = data;
|
||||
p->namelen = len;
|
||||
nghttp2_downcase(p->name, p->namelen);
|
||||
data += len;
|
||||
len = strlen(nv[i+1]);
|
||||
memcpy(data, nv[i+1], len);
|
||||
p->value = data;
|
||||
p->valuelen = len;
|
||||
data += len;
|
||||
++p;
|
||||
}
|
||||
nghttp2_nv_array_sort(*nva_ptr, nvlen);
|
||||
return nvlen;
|
||||
}
|
||||
|
||||
@@ -30,7 +30,7 @@
|
||||
#endif /* HAVE_CONFIG_H */
|
||||
|
||||
#include <nghttp2/nghttp2.h>
|
||||
#include "nghttp2_zlib.h"
|
||||
#include "nghttp2_hd.h"
|
||||
#include "nghttp2_buffer.h"
|
||||
|
||||
/**
|
||||
@@ -91,13 +91,9 @@ void nghttp2_frame_unpack_frame_hd(nghttp2_frame_hd *hd, const uint8_t* buf);
|
||||
/*
|
||||
* Packs HEADERS frame |frame| in wire format and store it in
|
||||
* |*buf_ptr|. The capacity of |*buf_ptr| is |*buflen_ptr| bytes.
|
||||
* The |*nvbuf_ptr| is used to store inflated name/value pairs in wire
|
||||
* format temporarily. Its length is |*nvbuflen_ptr| bytes. This
|
||||
* function expands |*buf_ptr| and |*nvbuf_ptr| as necessary to store
|
||||
* frame and name/value pairs. When expansion occurred, memory
|
||||
* previously pointed by |*buf_ptr| and |*nvbuf_ptr| is freed.
|
||||
* |*buf_ptr|, |*buflen_ptr|, |*nvbuf_ptr| and |*nvbuflen_ptr| are
|
||||
* updated accordingly.
|
||||
* This function expands |*buf_ptr| as necessary to store frame. When
|
||||
* expansion occurred, memory previously pointed by |*buf_ptr| may be
|
||||
* freed. |*buf_ptr| and |*buflen_ptr| are updated accordingly.
|
||||
*
|
||||
* frame->hd.length is assigned after length is determined during
|
||||
* packing process.
|
||||
@@ -105,7 +101,7 @@ void nghttp2_frame_unpack_frame_hd(nghttp2_frame_hd *hd, const uint8_t* buf);
|
||||
* This function returns the size of packed frame if it succeeds, or
|
||||
* returns one of the following negative error codes:
|
||||
*
|
||||
* NGHTTP2_ERR_ZLIB
|
||||
* NGHTTP2_ERR_HEADER_COMP
|
||||
* The deflate operation failed.
|
||||
* NGHTTP2_ERR_FRAME_TOO_LARGE
|
||||
* The length of the frame is too large.
|
||||
@@ -114,10 +110,8 @@ void nghttp2_frame_unpack_frame_hd(nghttp2_frame_hd *hd, const uint8_t* buf);
|
||||
*/
|
||||
ssize_t nghttp2_frame_pack_headers(uint8_t **buf_ptr,
|
||||
size_t *buflen_ptr,
|
||||
uint8_t **nvbuf_ptr,
|
||||
size_t *nvbuflen_ptr,
|
||||
nghttp2_headers *frame,
|
||||
nghttp2_zlib *deflater);
|
||||
nghttp2_hd_context *deflater);
|
||||
|
||||
/*
|
||||
* Unpacks HEADERS frame byte sequence into |frame|. The control
|
||||
@@ -125,8 +119,7 @@ ssize_t nghttp2_frame_pack_headers(uint8_t **buf_ptr,
|
||||
* headlen is 8 bytes. |payload| is the data after frame header and
|
||||
* just before name/value header block.
|
||||
*
|
||||
* The |inflatebuf| contains inflated name/value header block in wire
|
||||
* foramt.
|
||||
* The |inflater| inflates name/value header block.
|
||||
*
|
||||
* This function also validates the name/value pairs. If unpacking
|
||||
* succeeds but validation fails, it is indicated by returning
|
||||
@@ -135,6 +128,8 @@ ssize_t nghttp2_frame_pack_headers(uint8_t **buf_ptr,
|
||||
* This function returns 0 if it succeeds or one of the following
|
||||
* negative error codes:
|
||||
*
|
||||
* NGHTTP2_ERR_HEADER_COMP
|
||||
* The inflate operation failed.
|
||||
* NGHTTP2_ERR_INVALID_HEADER_BLOCK
|
||||
* Unpacking succeeds but the header block is invalid.
|
||||
* NGHTTP2_ERR_INVALID_FRAME
|
||||
@@ -145,7 +140,7 @@ ssize_t nghttp2_frame_pack_headers(uint8_t **buf_ptr,
|
||||
int nghttp2_frame_unpack_headers(nghttp2_headers *frame,
|
||||
const uint8_t *head, size_t headlen,
|
||||
const uint8_t *payload, size_t payloadlen,
|
||||
nghttp2_buffer *inflatebuf);
|
||||
nghttp2_hd_context *inflater);
|
||||
|
||||
/*
|
||||
* Unpacks HEADERS frame byte sequence into |frame|. This function
|
||||
@@ -356,63 +351,6 @@ size_t nghttp2_frame_count_nv_space(char **nv, size_t len_size);
|
||||
*/
|
||||
ssize_t nghttp2_frame_pack_nv(uint8_t *buf, char **nv, size_t len_size);
|
||||
|
||||
/*
|
||||
* Packs name/value pairs in |nv| in |*buf_ptr| with offset
|
||||
* |nv_offset|. It means first byte of packed name/value pairs is
|
||||
* stored in |*buf_ptr|+|nv_offset|. |*buf_ptr| and |*nvbuf_ptr| are
|
||||
* expanded as necessary.
|
||||
*
|
||||
* This function returns the number of the bytes for the frame
|
||||
* containing this name/value pairs if it succeeds, or one of the
|
||||
* following negative error codes:
|
||||
*
|
||||
* NGHTTP2_ERR_ZLIB
|
||||
* The deflate operation failed.
|
||||
* NGHTTP2_ERR_FRAME_TOO_LARGE
|
||||
* The length of the frame is too large.
|
||||
* NGHTTP2_ERR_NOMEM
|
||||
* Out of memory.
|
||||
*/
|
||||
ssize_t nghttp2_frame_alloc_pack_nv(uint8_t **buf_ptr,
|
||||
size_t *buflen_ptr,
|
||||
uint8_t **nvbuf_ptr,
|
||||
size_t *nvbuflen_ptr,
|
||||
char **nv, size_t nv_offset,
|
||||
size_t len_size,
|
||||
nghttp2_zlib *deflater);
|
||||
|
||||
/*
|
||||
* Counts number of name/value pair in |in| and computes length of
|
||||
* buffers to store unpacked name/value pair and store them in
|
||||
* |*nvlen_ptr| and |*buflen_ptr| respectively. |len_size| is the
|
||||
* number of bytes in length of name/value pair and it must be 2 or
|
||||
* 4. We use folloing data structure in |*buflen_ptr| size. First
|
||||
* part of the data is array of pointer to name/value pair. Supporse
|
||||
* the buf pointer points to the data region and N is the number of
|
||||
* name/value pair. First (N*2+1)*sizeof(char*) bytes contain array
|
||||
* of pointer to name/value pair and terminating NULL. Each pointer
|
||||
* to name/value pair points to the string in remaining data. For
|
||||
* each name/value pair, the name is copied to the remaining data with
|
||||
* terminating NULL character. The value is also copied to the
|
||||
* position after the data with terminating NULL character. The
|
||||
* corresponding index is assigned to these pointers. If the value
|
||||
* contains multiple values (delimited by single NULL), for each such
|
||||
* data, corresponding index is assigned to name/value pointers. In
|
||||
* this case, the name string is reused.
|
||||
*
|
||||
* With the above stragety, |*buflen_ptr| is calculated as
|
||||
* (N*2+1)*sizeof(char*)+sum(strlen(name)+1+strlen(value)+1){for each
|
||||
* name/value pair}.
|
||||
*
|
||||
* This function returns 0 if it succeeds, or one of the following
|
||||
* negative error codes:
|
||||
*
|
||||
* NGHTTP2_ERR_INVALID_FRAME
|
||||
* The input data are invalid.
|
||||
*/
|
||||
int nghttp2_frame_count_unpack_nv_space(size_t *nvlen_ptr, size_t *buflen_ptr,
|
||||
nghttp2_buffer *in, size_t len_size);
|
||||
|
||||
/*
|
||||
* Unpacks name/value header block in wire format |in| and stores them
|
||||
* in |*nv_ptr|. Thif function allocates enough memory to store
|
||||
@@ -443,13 +381,13 @@ int nghttp2_frame_unpack_nv(char ***nv_ptr, nghttp2_buffer *in,
|
||||
size_t len_size);
|
||||
|
||||
/*
|
||||
* Initializes HEADERS frame |frame| with given values. |frame|
|
||||
* takes ownership of |nv|, so caller must not free it. If |stream_id|
|
||||
* is not assigned yet, it must be -1.
|
||||
* Initializes HEADERS frame |frame| with given values. |frame| takes
|
||||
* ownership of |nva|, so caller must not free it. If |stream_id| is
|
||||
* not assigned yet, it must be -1.
|
||||
*/
|
||||
void nghttp2_frame_headers_init(nghttp2_headers *frame,
|
||||
uint8_t flags, int32_t stream_id, int32_t pri,
|
||||
char **nv);
|
||||
nghttp2_nv *nva, size_t nvlen);
|
||||
|
||||
void nghttp2_frame_headers_free(nghttp2_headers *frame);
|
||||
|
||||
@@ -573,6 +511,27 @@ ssize_t nghttp2_frame_nv_offset(const uint8_t *head);
|
||||
*/
|
||||
int nghttp2_frame_nv_check_null(const char **nv);
|
||||
|
||||
/*
|
||||
* Sorts the |nva| in ascending order of name. The relative order of
|
||||
* the same name pair is undefined.
|
||||
*/
|
||||
void nghttp2_nv_array_sort(nghttp2_nv *nva, size_t nvlen);
|
||||
|
||||
/*
|
||||
* Copies name/value pairs from |nv| to |*nva_ptr|, which is
|
||||
* dynamically allocated so that all items can be stored.
|
||||
*
|
||||
* This function returns the number of name/value pairs in |*nva_ptr|,
|
||||
* or one of the following negative error codes:
|
||||
*
|
||||
* NGHTTP2_ERR_NOMEM
|
||||
* Out of memory.
|
||||
* NGHTTP2_ERR_INVALID_ARGUMENT
|
||||
* The length of name or value in |nv| is strictly larger than (1
|
||||
* << 16) - 1.
|
||||
*/
|
||||
ssize_t nghttp2_nv_array_from_cstr(nghttp2_nv **nva_ptr, const char **nv);
|
||||
|
||||
/*
|
||||
* Returns nonzero if the name/value pair |a| equals to |b|. The name
|
||||
* is compared in case-sensitive, because we ensure that this function
|
||||
@@ -580,6 +539,18 @@ int nghttp2_frame_nv_check_null(const char **nv);
|
||||
*/
|
||||
int nghttp2_nv_equal(const nghttp2_nv *a, const nghttp2_nv *b);
|
||||
|
||||
void nghttp2_nv_array_free(nghttp2_nv *nva);
|
||||
/*
|
||||
* Frees |nva|.
|
||||
*/
|
||||
void nghttp2_nv_array_del(nghttp2_nv *nva);
|
||||
|
||||
/*
|
||||
* Checks names are not empty string and do not contain control
|
||||
* characters and values are not NULL.
|
||||
*
|
||||
* This function returns nonzero if it succeeds, or 0.
|
||||
*/
|
||||
int nghttp2_nv_array_check_null(nghttp2_nv *nva, size_t nvlen);
|
||||
|
||||
|
||||
#endif /* NGHTTP2_FRAME_H */
|
||||
|
||||
@@ -818,6 +818,7 @@ static ssize_t build_nv_array(nghttp2_hd_context *inflater,
|
||||
break;
|
||||
}
|
||||
}
|
||||
nghttp2_nv_array_sort(*nva_ptr, nvlen);
|
||||
return nvlen;
|
||||
}
|
||||
|
||||
@@ -880,6 +881,7 @@ ssize_t nghttp2_hd_inflate_hd(nghttp2_hd_context *inflater,
|
||||
nv.value = in;
|
||||
nv.valuelen = valuelen;
|
||||
in += valuelen;
|
||||
nghttp2_downcase(nv.name, nv.namelen);
|
||||
if(c == 0x60u) {
|
||||
rv = add_workingset_literal(inflater, &nv);
|
||||
} else {
|
||||
@@ -965,6 +967,7 @@ ssize_t nghttp2_hd_inflate_hd(nghttp2_hd_context *inflater,
|
||||
nv.namelen = namelen;
|
||||
nv.valuelen = valuelen;
|
||||
in += valuelen;
|
||||
nghttp2_downcase(nv.name, nv.namelen);
|
||||
new_ent = add_hd_table_subst(inflater, &nv, subindex);
|
||||
if(new_ent) {
|
||||
rv = add_workingset(inflater, new_ent);
|
||||
|
||||
@@ -53,8 +53,9 @@ typedef enum {
|
||||
|
||||
typedef struct {
|
||||
nghttp2_nv nv;
|
||||
/* Reference count in workingset */
|
||||
/* Reference count */
|
||||
uint8_t ref;
|
||||
/* Index in the header table */
|
||||
uint8_t index;
|
||||
uint8_t flags;
|
||||
} nghttp2_hd_entry;
|
||||
@@ -186,9 +187,9 @@ ssize_t nghttp2_hd_deflate_hd(nghttp2_hd_context *deflater,
|
||||
|
||||
/*
|
||||
* Inflates name/value block stored in |in| with length |inlen|. This
|
||||
* function performs decompression. The |*nv_ptr| points to the final
|
||||
* result on succesful decompression. The caller must free |*nv_ptr|
|
||||
* using nghttp2_nv_free().
|
||||
* function performs decompression. The |*nva_ptr| points to the final
|
||||
* result on succesful decompression. The caller must free |*nva_ptr|
|
||||
* using nghttp2_nv_array_del().
|
||||
*
|
||||
* This function returns the number of bytes outputted if it succeeds,
|
||||
* or one of the following negative error codes:
|
||||
@@ -200,7 +201,6 @@ ssize_t nghttp2_hd_inflate_hd(nghttp2_hd_context *inflater,
|
||||
nghttp2_nv **nva_ptr,
|
||||
uint8_t *in, size_t inlen);
|
||||
|
||||
|
||||
/*
|
||||
* Signals the end of processing one header block. This function
|
||||
* creates new reference set from working set.
|
||||
|
||||
@@ -81,6 +81,16 @@ void* nghttp2_memdup(const void* src, size_t n)
|
||||
return dest;
|
||||
}
|
||||
|
||||
void nghttp2_downcase(uint8_t *s, size_t len)
|
||||
{
|
||||
size_t i;
|
||||
for(i = 0; i < len; ++i) {
|
||||
if('A' <= s[i] && s[i] <= 'Z') {
|
||||
s[i] += 'a'-'A';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const char* nghttp2_strerror(int error_code)
|
||||
{
|
||||
switch(error_code) {
|
||||
@@ -130,6 +140,8 @@ const char* nghttp2_strerror(int error_code)
|
||||
return "The user callback function failed due to the temporal error";
|
||||
case NGHTTP2_ERR_FRAME_TOO_LARGE:
|
||||
return "The length of the frame is too large";
|
||||
case NGHTTP2_ERR_HEADER_COMP:
|
||||
return "Header compression/decompression error";
|
||||
case NGHTTP2_ERR_NOMEM:
|
||||
return "Out of memory";
|
||||
case NGHTTP2_ERR_CALLBACK_FAILURE:
|
||||
|
||||
@@ -89,4 +89,6 @@ int nghttp2_reserve_buffer(uint8_t **buf_ptr, size_t *buflen_ptr,
|
||||
*/
|
||||
void* nghttp2_memdup(const void* src, size_t n);
|
||||
|
||||
void nghttp2_downcase(uint8_t *s, size_t len);
|
||||
|
||||
#endif /* NGHTTP2_HELPER_H */
|
||||
|
||||
@@ -113,30 +113,13 @@ static void nghttp2_inbound_frame_reset(nghttp2_inbound_frame *iframe)
|
||||
iframe->state = NGHTTP2_RECV_HEAD;
|
||||
iframe->payloadlen = iframe->buflen = iframe->off = 0;
|
||||
iframe->headbufoff = 0;
|
||||
nghttp2_buffer_reset(&iframe->inflatebuf);
|
||||
iframe->error_code = 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* Returns the number of bytes before name/value header block for the
|
||||
* incoming frame. If the incoming frame does not have name/value
|
||||
* block, this function returns -1.
|
||||
*/
|
||||
static size_t nghttp2_inbound_frame_payload_nv_offset
|
||||
(nghttp2_inbound_frame *iframe)
|
||||
{
|
||||
ssize_t offset;
|
||||
offset = nghttp2_frame_nv_offset(iframe->headbuf);
|
||||
if(offset != -1) {
|
||||
offset -= NGHTTP2_FRAME_HEAD_LENGTH;
|
||||
}
|
||||
return offset;
|
||||
}
|
||||
|
||||
static int nghttp2_session_new(nghttp2_session **session_ptr,
|
||||
const nghttp2_session_callbacks *callbacks,
|
||||
void *user_data,
|
||||
int hd_comp)
|
||||
nghttp2_hd_side side)
|
||||
{
|
||||
int r;
|
||||
*session_ptr = malloc(sizeof(nghttp2_session));
|
||||
@@ -159,16 +142,13 @@ static int nghttp2_session_new(nghttp2_session **session_ptr,
|
||||
(*session_ptr)->goaway_flags = NGHTTP2_GOAWAY_NONE;
|
||||
(*session_ptr)->last_stream_id = 0;
|
||||
|
||||
(*session_ptr)->max_recv_ctrl_frame_buf = (1 << 24)-1;
|
||||
(*session_ptr)->max_recv_ctrl_frame_buf = NGHTTP2_MAX_FRAME_SIZE;
|
||||
|
||||
r = nghttp2_zlib_deflate_hd_init(&(*session_ptr)->hd_deflater,
|
||||
hd_comp,
|
||||
(*session_ptr)->version);
|
||||
r = nghttp2_hd_deflate_init(&(*session_ptr)->hd_deflater, side);
|
||||
if(r != 0) {
|
||||
goto fail_hd_deflater;
|
||||
}
|
||||
r = nghttp2_zlib_inflate_hd_init(&(*session_ptr)->hd_inflater,
|
||||
(*session_ptr)->version);
|
||||
r = nghttp2_hd_inflate_init(&(*session_ptr)->hd_inflater, side);
|
||||
if(r != 0) {
|
||||
goto fail_hd_inflater;
|
||||
}
|
||||
@@ -220,7 +200,6 @@ static int nghttp2_session_new(nghttp2_session **session_ptr,
|
||||
goto fail_iframe_buf;
|
||||
}
|
||||
(*session_ptr)->iframe.bufmax = NGHTTP2_INITIAL_INBOUND_FRAMEBUF_LENGTH;
|
||||
nghttp2_buffer_init(&(*session_ptr)->iframe.inflatebuf, 4096);
|
||||
|
||||
nghttp2_inbound_frame_reset(&(*session_ptr)->iframe);
|
||||
|
||||
@@ -236,9 +215,9 @@ static int nghttp2_session_new(nghttp2_session **session_ptr,
|
||||
nghttp2_pq_free(&(*session_ptr)->ob_pq);
|
||||
fail_ob_pq:
|
||||
/* No need to free (*session_ptr)->streams) here. */
|
||||
nghttp2_zlib_inflate_free(&(*session_ptr)->hd_inflater);
|
||||
nghttp2_hd_inflate_free(&(*session_ptr)->hd_inflater);
|
||||
fail_hd_inflater:
|
||||
nghttp2_zlib_deflate_free(&(*session_ptr)->hd_deflater);
|
||||
nghttp2_hd_deflate_free(&(*session_ptr)->hd_deflater);
|
||||
fail_hd_deflater:
|
||||
free(*session_ptr);
|
||||
fail_session:
|
||||
@@ -251,7 +230,8 @@ 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, 0);
|
||||
r = nghttp2_session_new(session_ptr, callbacks, user_data,
|
||||
NGHTTP2_HD_SIDE_CLIENT);
|
||||
if(r == 0) {
|
||||
/* IDs for use in client */
|
||||
(*session_ptr)->next_stream_id = 1;
|
||||
@@ -266,7 +246,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, 1 /* hd_comp */);
|
||||
r = nghttp2_session_new(session_ptr, callbacks, user_data,
|
||||
NGHTTP2_HD_SIDE_SERVER);
|
||||
if(r == 0) {
|
||||
(*session_ptr)->server = 1;
|
||||
/* IDs for use in client */
|
||||
@@ -311,12 +292,11 @@ void nghttp2_session_del(nghttp2_session *session)
|
||||
nghttp2_map_each_free(&session->streams, nghttp2_free_streams, NULL);
|
||||
nghttp2_session_ob_pq_free(&session->ob_pq);
|
||||
nghttp2_session_ob_pq_free(&session->ob_ss_pq);
|
||||
nghttp2_zlib_deflate_free(&session->hd_deflater);
|
||||
nghttp2_zlib_inflate_free(&session->hd_inflater);
|
||||
nghttp2_hd_deflate_free(&session->hd_deflater);
|
||||
nghttp2_hd_inflate_free(&session->hd_inflater);
|
||||
nghttp2_active_outbound_item_reset(&session->aob);
|
||||
free(session->aob.framebuf);
|
||||
free(session->nvbuf);
|
||||
nghttp2_buffer_free(&session->iframe.inflatebuf);
|
||||
free(session->iframe.buf);
|
||||
free(session);
|
||||
}
|
||||
@@ -768,8 +748,6 @@ static ssize_t nghttp2_session_prep_frame(nghttp2_session *session,
|
||||
case NGHTTP2_HEADERS:
|
||||
if(frame->hd.stream_id == -1) {
|
||||
/* initial HEADERS, which opens stream */
|
||||
int32_t stream_id;
|
||||
nghttp2_headers_aux_data *aux_data;
|
||||
int r;
|
||||
frame->headers.cat = NGHTTP2_HCAT_START_STREAM;
|
||||
r = nghttp2_session_predicate_syn_stream_send(session,
|
||||
@@ -777,41 +755,12 @@ static ssize_t nghttp2_session_prep_frame(nghttp2_session *session,
|
||||
if(r != 0) {
|
||||
return r;
|
||||
}
|
||||
stream_id = session->next_stream_id;
|
||||
frame->hd.stream_id = stream_id;
|
||||
frame->hd.stream_id = session->next_stream_id;
|
||||
session->next_stream_id += 2;
|
||||
framebuflen = nghttp2_frame_pack_headers(&session->aob.framebuf,
|
||||
&session->aob.framebufmax,
|
||||
&session->nvbuf,
|
||||
&session->nvbuflen,
|
||||
&frame->headers,
|
||||
&session->hd_deflater);
|
||||
if(framebuflen < 0) {
|
||||
return framebuflen;
|
||||
}
|
||||
aux_data = (nghttp2_headers_aux_data*)item->aux_data;
|
||||
if(nghttp2_session_open_stream
|
||||
(session, stream_id,
|
||||
frame->hd.flags,
|
||||
frame->headers.pri,
|
||||
NGHTTP2_STREAM_INITIAL,
|
||||
aux_data ? aux_data->stream_user_data : NULL) == NULL) {
|
||||
return NGHTTP2_ERR_NOMEM;
|
||||
}
|
||||
} else if(nghttp2_session_predicate_syn_reply_send
|
||||
(session, frame->hd.stream_id) == 0) {
|
||||
frame->headers.cat = NGHTTP2_HCAT_REPLY;
|
||||
/* first response HEADERS */
|
||||
framebuflen = nghttp2_frame_pack_headers(&session->aob.framebuf,
|
||||
&session->aob.framebufmax,
|
||||
&session->nvbuf,
|
||||
&session->nvbuflen,
|
||||
&frame->headers,
|
||||
&session->hd_deflater);
|
||||
if(framebuflen < 0) {
|
||||
return framebuflen;
|
||||
}
|
||||
|
||||
frame->headers.cat = NGHTTP2_HCAT_REPLY;
|
||||
} else {
|
||||
int r;
|
||||
frame->headers.cat = NGHTTP2_HCAT_HEADERS;
|
||||
@@ -820,14 +769,25 @@ static ssize_t nghttp2_session_prep_frame(nghttp2_session *session,
|
||||
if(r != 0) {
|
||||
return r;
|
||||
}
|
||||
framebuflen = nghttp2_frame_pack_headers(&session->aob.framebuf,
|
||||
&session->aob.framebufmax,
|
||||
&session->nvbuf,
|
||||
&session->nvbuflen,
|
||||
&frame->headers,
|
||||
&session->hd_deflater);
|
||||
if(framebuflen < 0) {
|
||||
return framebuflen;
|
||||
}
|
||||
framebuflen = nghttp2_frame_pack_headers(&session->aob.framebuf,
|
||||
&session->aob.framebufmax,
|
||||
&frame->headers,
|
||||
&session->hd_deflater);
|
||||
nghttp2_hd_end_headers(&session->hd_deflater);
|
||||
if(framebuflen < 0) {
|
||||
return framebuflen;
|
||||
}
|
||||
if(frame->headers.cat == NGHTTP2_HCAT_START_STREAM) {
|
||||
nghttp2_headers_aux_data *aux_data;
|
||||
aux_data = (nghttp2_headers_aux_data*)item->aux_data;
|
||||
if(nghttp2_session_open_stream
|
||||
(session, frame->hd.stream_id,
|
||||
frame->hd.flags,
|
||||
frame->headers.pri,
|
||||
NGHTTP2_STREAM_INITIAL,
|
||||
aux_data ? aux_data->stream_user_data : NULL) == NULL) {
|
||||
return NGHTTP2_ERR_NOMEM;
|
||||
}
|
||||
}
|
||||
break;
|
||||
@@ -1939,7 +1899,7 @@ static int nghttp2_session_process_ctrl_frame(nghttp2_session *session)
|
||||
sizeof(session->iframe.headbuf),
|
||||
session->iframe.buf,
|
||||
session->iframe.buflen,
|
||||
&session->iframe.inflatebuf);
|
||||
&session->hd_inflater);
|
||||
} else if(session->iframe.error_code == NGHTTP2_ERR_FRAME_TOO_LARGE) {
|
||||
r = nghttp2_frame_unpack_headers_without_nv
|
||||
(&frame.headers,
|
||||
@@ -1972,6 +1932,7 @@ static int nghttp2_session_process_ctrl_frame(nghttp2_session *session)
|
||||
r = nghttp2_session_on_syn_stream_received(session, &frame);
|
||||
}
|
||||
nghttp2_frame_headers_free(&frame.headers);
|
||||
nghttp2_hd_end_headers(&session->hd_inflater);
|
||||
} else if(r == NGHTTP2_ERR_INVALID_HEADER_BLOCK ||
|
||||
r == NGHTTP2_ERR_FRAME_TOO_LARGE) {
|
||||
r = nghttp2_session_handle_invalid_stream
|
||||
@@ -2280,30 +2241,7 @@ ssize_t nghttp2_session_mem_recv(nghttp2_session *session,
|
||||
nghttp2_get_uint16(&session->iframe.headbuf[0]);
|
||||
if(!nghttp2_frame_is_data_frame(session->iframe.headbuf)) {
|
||||
/* control frame */
|
||||
ssize_t buflen;
|
||||
buflen = nghttp2_inbound_frame_payload_nv_offset(&session->iframe);
|
||||
if(buflen == -1) {
|
||||
/* Check if payloadlen is small enough for buffering */
|
||||
if(session->iframe.payloadlen > session->max_recv_ctrl_frame_buf) {
|
||||
session->iframe.error_code = NGHTTP2_ERR_FRAME_TOO_LARGE;
|
||||
session->iframe.state = NGHTTP2_RECV_PAYLOAD_IGN;
|
||||
buflen = 0;
|
||||
} else {
|
||||
buflen = session->iframe.payloadlen;
|
||||
}
|
||||
} else if(buflen < (ssize_t)session->iframe.payloadlen) {
|
||||
if(session->iframe.payloadlen > session->max_recv_ctrl_frame_buf) {
|
||||
session->iframe.error_code = NGHTTP2_ERR_FRAME_TOO_LARGE;
|
||||
}
|
||||
/* We are going to receive payload even if the receiving
|
||||
frame is too large to synchronize zlib context. For
|
||||
name/value header block, we will just burn zlib cycle
|
||||
and discard outputs. */
|
||||
session->iframe.state = NGHTTP2_RECV_PAYLOAD_PRE_NV;
|
||||
}
|
||||
/* buflen >= session->iframe.payloadlen means frame is
|
||||
malformed. In this case, we just buffer these bytes and
|
||||
handle error later. */
|
||||
ssize_t buflen = session->iframe.payloadlen;
|
||||
session->iframe.buflen = buflen;
|
||||
r = nghttp2_reserve_buffer(&session->iframe.buf,
|
||||
&session->iframe.bufmax,
|
||||
@@ -2328,8 +2266,6 @@ ssize_t nghttp2_session_mem_recv(nghttp2_session *session,
|
||||
}
|
||||
}
|
||||
if(session->iframe.state == NGHTTP2_RECV_PAYLOAD ||
|
||||
session->iframe.state == NGHTTP2_RECV_PAYLOAD_PRE_NV ||
|
||||
session->iframe.state == NGHTTP2_RECV_PAYLOAD_NV ||
|
||||
session->iframe.state == NGHTTP2_RECV_PAYLOAD_IGN) {
|
||||
size_t rempayloadlen;
|
||||
size_t bufavail, readlen;
|
||||
@@ -2342,49 +2278,7 @@ ssize_t nghttp2_session_mem_recv(nghttp2_session *session,
|
||||
break;
|
||||
}
|
||||
readlen = nghttp2_min(bufavail, rempayloadlen);
|
||||
if(session->iframe.state == NGHTTP2_RECV_PAYLOAD_PRE_NV) {
|
||||
size_t pnvlen, rpnvlen, readpnvlen;
|
||||
pnvlen = nghttp2_inbound_frame_payload_nv_offset(&session->iframe);
|
||||
rpnvlen = pnvlen - session->iframe.off;
|
||||
readpnvlen = nghttp2_min(rpnvlen, readlen);
|
||||
|
||||
memcpy(session->iframe.buf+session->iframe.off, inmark, readpnvlen);
|
||||
readlen -= readpnvlen;
|
||||
session->iframe.off += readpnvlen;
|
||||
inmark += readpnvlen;
|
||||
|
||||
if(session->iframe.off == pnvlen) {
|
||||
session->iframe.state = NGHTTP2_RECV_PAYLOAD_NV;
|
||||
}
|
||||
}
|
||||
if(session->iframe.state == NGHTTP2_RECV_PAYLOAD_NV) {
|
||||
/* For frame with name/value header block, the compressed
|
||||
portion of the block is incrementally decompressed. The
|
||||
result is stored in inflatebuf. */
|
||||
if(session->iframe.error_code == 0 ||
|
||||
session->iframe.error_code == NGHTTP2_ERR_FRAME_TOO_LARGE) {
|
||||
ssize_t decomplen;
|
||||
if(session->iframe.error_code == NGHTTP2_ERR_FRAME_TOO_LARGE) {
|
||||
nghttp2_buffer_reset(&session->iframe.inflatebuf);
|
||||
}
|
||||
decomplen = nghttp2_zlib_inflate_hd(&session->hd_inflater,
|
||||
&session->iframe.inflatebuf,
|
||||
inmark, readlen);
|
||||
if(decomplen < 0) {
|
||||
/* We are going to overwrite error_code here if it is
|
||||
already set. But it is fine because the only possible
|
||||
nonzero error code here is NGHTTP2_ERR_FRAME_TOO_LARGE
|
||||
and zlib/fatal error can override it. */
|
||||
session->iframe.error_code = decomplen;
|
||||
} else if(nghttp2_buffer_length(&session->iframe.inflatebuf)
|
||||
> session->max_recv_ctrl_frame_buf) {
|
||||
/* If total length in inflatebuf exceeds certain limit,
|
||||
set TOO_LARGE_FRAME to error_code and issue RST_STREAM
|
||||
later. */
|
||||
session->iframe.error_code = NGHTTP2_ERR_FRAME_TOO_LARGE;
|
||||
}
|
||||
}
|
||||
} else if(!nghttp2_frame_is_data_frame(session->iframe.headbuf)) {
|
||||
if(!nghttp2_frame_is_data_frame(session->iframe.headbuf)) {
|
||||
if(session->iframe.state != NGHTTP2_RECV_PAYLOAD_IGN) {
|
||||
memcpy(session->iframe.buf+session->iframe.off, inmark, readlen);
|
||||
}
|
||||
|
||||
@@ -33,7 +33,7 @@
|
||||
#include "nghttp2_pq.h"
|
||||
#include "nghttp2_map.h"
|
||||
#include "nghttp2_frame.h"
|
||||
#include "nghttp2_zlib.h"
|
||||
#include "nghttp2_hd.h"
|
||||
#include "nghttp2_stream.h"
|
||||
#include "nghttp2_buffer.h"
|
||||
#include "nghttp2_outbound_item.h"
|
||||
@@ -78,13 +78,7 @@ typedef enum {
|
||||
/* Receiving frame payload (comes after length field) */
|
||||
NGHTTP2_RECV_PAYLOAD,
|
||||
/* Receiving frame payload, but the received bytes are discarded. */
|
||||
NGHTTP2_RECV_PAYLOAD_IGN,
|
||||
/* Receiving frame payload that comes before name/value header
|
||||
block. Applied only for HEADERS and PUSH_PROMISE. */
|
||||
NGHTTP2_RECV_PAYLOAD_PRE_NV,
|
||||
/* Receiving name/value header block in frame payload. Applied only
|
||||
for HEADERS and PUSH_PROMISE. */
|
||||
NGHTTP2_RECV_PAYLOAD_NV
|
||||
NGHTTP2_RECV_PAYLOAD_IGN
|
||||
} nghttp2_inbound_state;
|
||||
|
||||
typedef struct {
|
||||
@@ -92,7 +86,7 @@ typedef struct {
|
||||
uint8_t headbuf[NGHTTP2_FRAME_HEAD_LENGTH];
|
||||
/* How many bytes are filled in headbuf */
|
||||
size_t headbufoff;
|
||||
/* Payload for control frames. It is not used for DATA frames */
|
||||
/* Payload for non-DATA frames. */
|
||||
uint8_t *buf;
|
||||
/* Capacity of buf */
|
||||
size_t bufmax;
|
||||
@@ -107,9 +101,6 @@ typedef struct {
|
||||
/* How many bytes are received for this frame. off <= payloadlen
|
||||
must be fulfilled. */
|
||||
size_t off;
|
||||
/* Buffer used to store name/value pairs while inflating them using
|
||||
zlib on unpack */
|
||||
nghttp2_buffer inflatebuf;
|
||||
/* Error code */
|
||||
int error_code;
|
||||
} nghttp2_inbound_frame;
|
||||
@@ -163,8 +154,8 @@ struct nghttp2_session {
|
||||
/* The number of bytes allocated for nvbuf */
|
||||
size_t nvbuflen;
|
||||
|
||||
nghttp2_zlib hd_deflater;
|
||||
nghttp2_zlib hd_inflater;
|
||||
nghttp2_hd_context hd_deflater;
|
||||
nghttp2_hd_context hd_inflater;
|
||||
|
||||
/* Flags indicating GOAWAY is sent and/or recieved. The flags are
|
||||
composed by bitwise OR-ing nghttp2_goaway_flag. */
|
||||
|
||||
@@ -41,7 +41,8 @@ static int nghttp2_submit_headers_shared
|
||||
{
|
||||
int r;
|
||||
nghttp2_frame *frame;
|
||||
char **nv_copy;
|
||||
nghttp2_nv *nva_copy;
|
||||
ssize_t nvlen;
|
||||
uint8_t flags_copy;
|
||||
nghttp2_data_provider *data_prd_copy = NULL;
|
||||
nghttp2_headers_aux_data *aux_data = NULL;
|
||||
@@ -73,19 +74,19 @@ static int nghttp2_submit_headers_shared
|
||||
free(data_prd_copy);
|
||||
return NGHTTP2_ERR_NOMEM;
|
||||
}
|
||||
nv_copy = nghttp2_frame_nv_norm_copy(nv);
|
||||
if(nv_copy == NULL) {
|
||||
nvlen = nghttp2_nv_array_from_cstr(&nva_copy, nv);
|
||||
if(nvlen < 0) {
|
||||
free(frame);
|
||||
free(aux_data);
|
||||
free(data_prd_copy);
|
||||
return NGHTTP2_ERR_NOMEM;
|
||||
return nvlen;
|
||||
}
|
||||
/* TODO Implement header continuation */
|
||||
flags_copy = (flags & (NGHTTP2_FLAG_END_STREAM | NGHTTP2_FLAG_PRIORITY)) |
|
||||
NGHTTP2_FLAG_END_HEADERS;
|
||||
|
||||
nghttp2_frame_headers_init(&frame->headers, flags_copy, stream_id, pri,
|
||||
nv_copy);
|
||||
nva_copy, nvlen);
|
||||
r = nghttp2_session_add_frame(session, NGHTTP2_CAT_CTRL, frame,
|
||||
aux_data);
|
||||
if(r != 0) {
|
||||
|
||||
Reference in New Issue
Block a user