From 61bf7c6b02a03d7caa6e73f4e4f6905017b8bee2 Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Sat, 20 Jul 2013 00:08:14 +0900 Subject: [PATCH] Integrate new header compression --- lib/includes/nghttp2/nghttp2.h | 18 +- lib/nghttp2_frame.c | 357 +++++++++----------------- lib/nghttp2_frame.h | 125 ++++------ lib/nghttp2_hd.c | 3 + lib/nghttp2_hd.h | 10 +- lib/nghttp2_helper.c | 12 + lib/nghttp2_helper.h | 2 + lib/nghttp2_session.c | 180 +++----------- lib/nghttp2_session.h | 19 +- lib/nghttp2_submit.c | 11 +- src/SpdyServer.cc | 13 +- src/nghttp2_ssl.cc | 17 +- src/spdycat.cc | 24 +- src/util.cc | 10 + src/util.h | 2 + tests/main.c | 14 -- tests/nghttp2_frame_test.c | 441 +++------------------------------ tests/nghttp2_frame_test.h | 7 - tests/nghttp2_hd_test.c | 23 +- tests/nghttp2_session_test.c | 192 ++++++-------- tests/nghttp2_test_helper.c | 38 +-- tests/nghttp2_test_helper.h | 10 +- 22 files changed, 430 insertions(+), 1098 deletions(-) diff --git a/lib/includes/nghttp2/nghttp2.h b/lib/includes/nghttp2/nghttp2.h index 3f6d2680..bff1d0ee 100644 --- a/lib/includes/nghttp2/nghttp2.h +++ b/lib/includes/nghttp2/nghttp2.h @@ -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 diff --git a/lib/nghttp2_frame.c b/lib/nghttp2_frame.c index 7edf2be1..12e9a36f 100644 --- a/lib/nghttp2_frame.c +++ b/lib/nghttp2_frame.c @@ -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; +} diff --git a/lib/nghttp2_frame.h b/lib/nghttp2_frame.h index 8283165c..346ee0a1 100644 --- a/lib/nghttp2_frame.h +++ b/lib/nghttp2_frame.h @@ -30,7 +30,7 @@ #endif /* HAVE_CONFIG_H */ #include -#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 */ diff --git a/lib/nghttp2_hd.c b/lib/nghttp2_hd.c index b7870ead..9a3d8262 100644 --- a/lib/nghttp2_hd.c +++ b/lib/nghttp2_hd.c @@ -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); diff --git a/lib/nghttp2_hd.h b/lib/nghttp2_hd.h index eed147c0..52ccb4b8 100644 --- a/lib/nghttp2_hd.h +++ b/lib/nghttp2_hd.h @@ -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. diff --git a/lib/nghttp2_helper.c b/lib/nghttp2_helper.c index f2d65b50..c0ce9c88 100644 --- a/lib/nghttp2_helper.c +++ b/lib/nghttp2_helper.c @@ -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: diff --git a/lib/nghttp2_helper.h b/lib/nghttp2_helper.h index 5874e895..ea4a996c 100644 --- a/lib/nghttp2_helper.h +++ b/lib/nghttp2_helper.h @@ -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 */ diff --git a/lib/nghttp2_session.c b/lib/nghttp2_session.c index 8eb78dbc..521be0a4 100644 --- a/lib/nghttp2_session.c +++ b/lib/nghttp2_session.c @@ -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); } diff --git a/lib/nghttp2_session.h b/lib/nghttp2_session.h index 788750a0..e90b3ed1 100644 --- a/lib/nghttp2_session.h +++ b/lib/nghttp2_session.h @@ -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. */ diff --git a/lib/nghttp2_submit.c b/lib/nghttp2_submit.c index 1c703518..b962ccc1 100644 --- a/lib/nghttp2_submit.c +++ b/lib/nghttp2_submit.c @@ -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) { diff --git a/src/SpdyServer.cc b/src/SpdyServer.cc index 0e3a2016..63de92ee 100644 --- a/src/SpdyServer.cc +++ b/src/SpdyServer.cc @@ -541,10 +541,13 @@ void prepare_response(Request *req, SpdyEventHandler *hd) } // namespace namespace { -void append_nv(Request *req, char **nv) +void append_nv(Request *req, nghttp2_nv *nva, size_t nvlen) { - for(int i = 0; nv[i]; i += 2) { - req->headers.push_back(std::make_pair(nv[i], nv[i+1])); + for(size_t i = 0; i < nvlen; ++i) { + req->headers.push_back({ + std::string(nva[i].name, nva[i].name + nva[i].namelen), + std::string(nva[i].value, nva[i].value + nva[i].valuelen) + }); } } } // namespace @@ -564,13 +567,13 @@ void hd_on_frame_recv_callback case NGHTTP2_HCAT_START_STREAM: { int32_t stream_id = frame->hd.stream_id; auto req = new Request(stream_id); - append_nv(req, frame->headers.nv); + append_nv(req, frame->headers.nva, frame->headers.nvlen); hd->add_stream(stream_id, req); break; } case NGHTTP2_HCAT_HEADERS: { Request *req = hd->get_stream(frame->hd.stream_id); - append_nv(req, frame->headers.nv); + append_nv(req, frame->headers.nva, frame->headers.nvlen); break; } default: diff --git a/src/nghttp2_ssl.cc b/src/nghttp2_ssl.cc index d7a533df..8d8f910d 100644 --- a/src/nghttp2_ssl.cc +++ b/src/nghttp2_ssl.cc @@ -523,14 +523,17 @@ const char* ansi_escend() } } // namespace -void print_nv(char **nv) + +void print_nv(nghttp2_nv *nva, size_t nvlen) { - int i; - for(i = 0; nv[i]; i += 2) { + size_t i; + for(i = 0; i < nvlen; ++i) { print_frame_attr_indent(); - printf("%s%s%s: %s\n", - ansi_esc("\033[1;34m"), nv[i], - ansi_escend(), nv[i+1]); + printf("%s", ansi_esc("\033[1;34m")); + fwrite(nva[i].name, nva[i].namelen, 1, stdout); + printf("%s: ", ansi_escend()); + fwrite(nva[i].value, nva[i].valuelen, 1, stdout); + printf("\n"); } } @@ -641,7 +644,7 @@ void print_frame(print_type ptype, nghttp2_frame *frame) default: break; } - print_nv(frame->headers.nv); + print_nv(frame->headers.nva, frame->headers.nvlen); break; case NGHTTP2_RST_STREAM: print_frame_attr_indent(); diff --git a/src/spdycat.cc b/src/spdycat.cc index 07dd1d23..8962f89d 100644 --- a/src/spdycat.cc +++ b/src/spdycat.cc @@ -472,26 +472,24 @@ void on_frame_send_callback2 void check_response_header (nghttp2_session *session, nghttp2_frame *frame, void *user_data) { - char **nv; - int32_t stream_id; - if(frame->hd.type == NGHTTP2_HEADERS) { - nv = frame->headers.nv; - stream_id = frame->hd.stream_id; - } else { + if(frame->hd.type != NGHTTP2_HEADERS || + frame->headers.cat != NGHTTP2_HCAT_REPLY) { return; } - auto req = (Request*)nghttp2_session_get_stream_user_data(session, - stream_id); + auto req = (Request*)nghttp2_session_get_stream_user_data + (session, frame->hd.stream_id); if(!req) { // Server-pushed stream does not have stream user data return; } bool gzip = false; - for(size_t i = 0; nv[i]; i += 2) { - if(strcmp("content-encoding", nv[i]) == 0) { - gzip = util::strieq("gzip", nv[i+1]) || util::strieq("deflate", nv[i+1]); - } else if(strcmp(":status", nv[i]) == 0) { - req->status = nv[i+1]; + for(size_t i = 0; i < frame->headers.nvlen; ++i) { + auto nv = &frame->headers.nva[i]; + if(util::strieq("content-encoding", nv->name, nv->namelen) == 0) { + gzip = util::strieq("gzip", nv->value, nv->valuelen) || + util::strieq("deflate", nv->value, nv->valuelen); + } else if(util::strieq(":status", nv->name, nv->namelen) == 0) { + req->status.assign(nv->value, nv->value + nv->valuelen); } } if(gzip) { diff --git a/src/util.cc b/src/util.cc index 9f70e8f9..fb272d6d 100644 --- a/src/util.cc +++ b/src/util.cc @@ -160,6 +160,16 @@ bool strieq(const char *a, const char *b) return !*a && !*b; } +bool strieq(const char *a, const uint8_t *b, size_t bn) +{ + if(!a || !b) { + return false; + } + size_t i; + for(i = 0; i < bn && *a && lowcase(*a) == lowcase(*b); ++a, ++b); + return !*a && i == bn; +} + bool strifind(const char *a, const char *b) { if(!a || !b) { diff --git a/src/util.h b/src/util.h index c8382f5a..c6074104 100644 --- a/src/util.h +++ b/src/util.h @@ -294,6 +294,8 @@ bool strieq(const std::string& a, const std::string& b); bool strieq(const char *a, const char *b); +bool strieq(const char *a, const uint8_t *b, size_t n); + bool strifind(const char *a, const char *b); char upcase(char c); diff --git a/tests/main.c b/tests/main.c index fa9a6966..15c60701 100644 --- a/tests/main.c +++ b/tests/main.c @@ -162,20 +162,6 @@ int main(int argc, char* argv[]) test_nghttp2_session_set_option) || !CU_add_test(pSuite, "session_data_backoff_by_high_pri_frame", test_nghttp2_session_data_backoff_by_high_pri_frame) || - !CU_add_test(pSuite, "frame_unpack_nv", - test_nghttp2_frame_unpack_nv) || - !CU_add_test(pSuite, "frame_unpack_nv_check_name", - test_nghttp2_frame_unpack_nv_check_name) || - !CU_add_test(pSuite, "frame_unpack_nv_last_empty_value", - test_nghttp2_frame_unpack_nv_last_empty_value) || - !CU_add_test(pSuite, "frame_pack_nv_duplicate_keys", - test_nghttp2_frame_pack_nv_duplicate_keys) || - !CU_add_test(pSuite, "frame_count_nv_space", - test_nghttp2_frame_count_nv_space) || - !CU_add_test(pSuite, "frame_pack_nv_empty_value", - test_nghttp2_frame_pack_nv_empty_value) || - !CU_add_test(pSuite, "frame_count_unpack_nv_space", - test_nghttp2_frame_count_unpack_nv_space) || !CU_add_test(pSuite, "frame_nv_sort", test_nghttp2_frame_nv_sort) || !CU_add_test(pSuite, "frame_nv_downcase", test_nghttp2_frame_nv_downcase) || diff --git a/tests/nghttp2_frame_test.c b/tests/nghttp2_frame_test.c index 239b5c21..cbea7b44 100644 --- a/tests/nghttp2_frame_test.c +++ b/tests/nghttp2_frame_test.c @@ -33,13 +33,6 @@ #include "nghttp2_helper.h" #include "nghttp2_test_helper.h" -/* Reads |len_size| byte from |data| as 2 bytes network byte - order integer, and returns it in host byte order. */ -static int get_packed_hd_len(uint8_t *data, size_t len_size) -{ - return nghttp2_get_uint16(data); -} - static const char *headers[] = { "method", "GET", "scheme", "https", @@ -51,376 +44,6 @@ static const char *headers[] = { NULL }; -void test_nghttp2_frame_unpack_nv() -{ - size_t len_size = 2; - uint8_t out[1024]; - char **nv; - size_t inlen = nghttp2_frame_pack_nv(out, (char**)headers, len_size); - nghttp2_buffer buffer; - - nghttp2_buffer_init(&buffer, 4096); - nghttp2_buffer_write(&buffer, out, inlen); - - CU_ASSERT(0 == nghttp2_frame_unpack_nv(&nv, &buffer, len_size)); - CU_ASSERT(strcmp("method", nv[0]) == 0); - CU_ASSERT(strcmp("GET", nv[1]) == 0); - CU_ASSERT(strcmp("scheme", nv[2]) == 0); - CU_ASSERT(strcmp("https", nv[3]) == 0); - CU_ASSERT(strcmp("url", nv[4]) == 0); - CU_ASSERT(strcmp("/", nv[5]) == 0); - CU_ASSERT(strcmp("version", nv[6]) == 0); - CU_ASSERT(strcmp("HTTP/1.1", nv[7]) == 0); - CU_ASSERT(strcmp("x-empty", nv[8]) == 0); - CU_ASSERT(strcmp("", nv[9]) == 0); - CU_ASSERT(strcmp("x-head", nv[10]) == 0); - CU_ASSERT(strcmp("foo", nv[11]) == 0); - CU_ASSERT(strcmp("x-head", nv[12]) == 0); - CU_ASSERT(strcmp("bar", nv[13]) == 0); - nghttp2_frame_nv_del(nv); - - /* Create in-sequence NUL bytes */ - /* Assuming first chunk has enough space to store 1st name/value - pair. */ - memcpy(&buffer.root.next->data[len_size + - len_size + strlen(headers[0]) + - len_size + strlen(headers[1])-2], - "\0\0", 2); - CU_ASSERT(NGHTTP2_ERR_INVALID_HEADER_BLOCK == - nghttp2_frame_unpack_nv(&nv, &buffer, len_size)); - - nghttp2_frame_nv_del(nv); - nghttp2_buffer_free(&buffer); -} - -/* This function intentionally does not merge same header field into - one */ -static size_t nghttp2_pack_nv(uint8_t *buf, size_t buflen, const char **nv, - size_t len_size) -{ - size_t i, n; - uint8_t *buf_ptr; - buf_ptr = buf; - for(n = 0; nv[n]; ++n); - nghttp2_frame_put_nv_len(buf_ptr, n/2); - buf_ptr += len_size; - for(i = 0; i < n; ++i) { - size_t len = strlen(nv[i]); - nghttp2_frame_put_nv_len(buf_ptr, len); - buf_ptr += len_size; - memcpy(buf_ptr, nv[i], len); - buf_ptr += len; - } - return buf_ptr-buf; -} - -static const char *empty_name_headers[] = { - "method", "GET", - "", "https", - "url", "/", - NULL -}; - -static const char non_ascii_header_name[] = { (char)0xff }; - -static const char *non_ascii_headers[] = { - non_ascii_header_name, "foo", - NULL -}; - -void test_nghttp2_frame_unpack_nv_check_name() -{ - size_t len_size = 2; - uint8_t nvbuf[1024]; - size_t nvbuflen; - nghttp2_buffer buffer; - char **nv; - - nghttp2_buffer_init(&buffer, 32); - - nvbuflen = nghttp2_pack_nv(nvbuf, sizeof(nvbuf), headers, len_size); - nghttp2_buffer_write(&buffer, nvbuf, nvbuflen); - - CU_ASSERT(NGHTTP2_ERR_INVALID_HEADER_BLOCK == - nghttp2_frame_unpack_nv(&nv, &buffer, len_size)); - - nghttp2_frame_nv_del(nv); - nghttp2_buffer_reset(&buffer); - - nvbuflen = nghttp2_pack_nv(nvbuf, sizeof(nvbuf), empty_name_headers, - len_size); - nghttp2_buffer_write(&buffer, nvbuf, nvbuflen); - - CU_ASSERT(NGHTTP2_ERR_INVALID_HEADER_BLOCK == - nghttp2_frame_unpack_nv(&nv, &buffer, len_size)); - - nghttp2_frame_nv_del(nv); - nghttp2_buffer_reset(&buffer); - - nvbuflen = nghttp2_pack_nv(nvbuf, sizeof(nvbuf), non_ascii_headers, - len_size); - nghttp2_buffer_write(&buffer, nvbuf, nvbuflen); - CU_ASSERT(NGHTTP2_ERR_INVALID_HEADER_BLOCK == - nghttp2_frame_unpack_nv(&nv, &buffer, len_size)); - - nghttp2_frame_nv_del(nv); - nghttp2_buffer_free(&buffer); -} - -void test_nghttp2_frame_unpack_nv_last_empty_value() -{ - size_t len_size = 2; - size_t nvbuflen; - uint8_t nvbuf[256]; - uint8_t *nvbufptr; - nghttp2_buffer buffer; - char **outnv = 0; - const char hdname[] = "method"; - - nvbufptr = nvbuf; - nghttp2_frame_put_nv_len(nvbufptr, 1); - nvbufptr += len_size; - nghttp2_frame_put_nv_len(nvbufptr, sizeof(hdname)-1); - nvbufptr += len_size; - memcpy(nvbufptr, hdname, sizeof(hdname)-1); - nvbufptr += sizeof(hdname)-1; - nghttp2_frame_put_nv_len(nvbufptr, 4); - nvbufptr += len_size; - /* Copy including terminating NULL */ - memcpy(nvbufptr, "GET", 4); - nvbufptr += 4; - nvbuflen = nvbufptr - nvbuf; - - nghttp2_buffer_init(&buffer, 32); - - nghttp2_buffer_write(&buffer, nvbuf, nvbuflen); - CU_ASSERT(NGHTTP2_ERR_INVALID_HEADER_BLOCK == - nghttp2_frame_unpack_nv(&outnv, &buffer, len_size)); - - nghttp2_frame_nv_del(outnv); - nghttp2_buffer_free(&buffer); -} - -void test_nghttp2_frame_pack_nv_duplicate_keys(void) -{ - uint8_t out[1024]; - size_t len_size = 2; - const char *nv_src[] = { - "method", "GET", - "scheme", "https", - "url", "/", - "X-hEad", "foo", - "x-heaD", "bar", - "version", "HTTP/1.1", - NULL - }; - char **nv = nghttp2_frame_nv_norm_copy(nv_src); - const uint8_t *outptr; - int pairs, len; - /* size_t inlen = */ nghttp2_frame_pack_nv(out, nv, len_size); - outptr = out; - - pairs = nghttp2_get_uint16(outptr); - CU_ASSERT(pairs == 5); - outptr += 2; - - len = nghttp2_get_uint16(outptr); - outptr += 2; - CU_ASSERT(len == 6); - CU_ASSERT(memcmp(outptr, "method", len) == 0); - outptr += len; - - len = nghttp2_get_uint16(outptr); - outptr += 2; - CU_ASSERT(len == 3); - CU_ASSERT(memcmp(outptr, "GET", len) == 0); - outptr += len; - - len = nghttp2_get_uint16(outptr); - outptr += 2; - CU_ASSERT(len == 6); - CU_ASSERT(memcmp(outptr, "scheme", len) == 0); - outptr += len; - - len = nghttp2_get_uint16(outptr); - outptr += 2; - CU_ASSERT(len == 5); - CU_ASSERT(memcmp(outptr, "https", len) == 0); - outptr += len; - - len = nghttp2_get_uint16(outptr); - outptr += 2; - CU_ASSERT(len == 3); - CU_ASSERT(memcmp(outptr, "url", len) == 0); - outptr += len; - - len = nghttp2_get_uint16(outptr); - outptr += 2; - CU_ASSERT(len == 1); - CU_ASSERT(memcmp(outptr, "/", len) == 0); - outptr += len; - - len = nghttp2_get_uint16(outptr); - outptr += 2; - CU_ASSERT(len == 7); - CU_ASSERT(memcmp(outptr, "version", len) == 0); - outptr += len; - - len = nghttp2_get_uint16(outptr); - outptr += 2; - CU_ASSERT(len == 8); - CU_ASSERT(memcmp(outptr, "HTTP/1.1", len) == 0); - outptr += len; - - - len = nghttp2_get_uint16(outptr); - outptr += 2; - CU_ASSERT(len == 6); - CU_ASSERT(memcmp(outptr, "x-head", len) == 0); - outptr += len; - - len = nghttp2_get_uint16(outptr); - outptr += 2; - CU_ASSERT(len == 7); - CU_ASSERT(memcmp(outptr, "foo\0bar", len) == 0); - outptr += len; - - nghttp2_frame_nv_del(nv); -} - -static const char *multi_empty_headers1[] = { - "a", "", - "a", "", - NULL -}; - -static const char *multi_empty_headers2[] = { - "a", "/", - "a", "", - NULL -}; - -static const char *multi_empty_headers3[] = { - "a", "", - "a", "/", - NULL -}; - -void test_nghttp2_frame_count_nv_space(void) -{ - size_t len_size = 2; - CU_ASSERT(85 == nghttp2_frame_count_nv_space((char**)headers, len_size)); - len_size = 4; - CU_ASSERT(111 == nghttp2_frame_count_nv_space((char**)headers, len_size)); - /* only ("a", "") is counted */ - CU_ASSERT(13 == nghttp2_frame_count_nv_space((char**)multi_empty_headers1, - len_size)); - /* only ("a", "/") is counted */ - CU_ASSERT(14 == nghttp2_frame_count_nv_space((char**)multi_empty_headers2, - len_size)); - /* only ("a", "/") is counted */ - CU_ASSERT(14 == nghttp2_frame_count_nv_space((char**)multi_empty_headers3, - len_size)); -} - -static void frame_pack_nv_empty_value_check(uint8_t *outptr, - int vallen, - const char *val, - size_t len_size) -{ - int len; - len = get_packed_hd_len(outptr, len_size); - CU_ASSERT(1 == len); - outptr += len_size; - len = get_packed_hd_len(outptr, len_size); - CU_ASSERT(1 == len); - outptr += len_size; - CU_ASSERT(0 == memcmp("a", outptr, len)); - outptr += len; - len = get_packed_hd_len(outptr, len_size); - CU_ASSERT(vallen == len); - len += len_size; - if(vallen == len) { - CU_ASSERT(0 == memcmp(val, outptr, vallen)); - } -} - -void test_nghttp2_frame_pack_nv_empty_value() -{ - size_t len_size = 2; - uint8_t out[256]; - char **nv; - ssize_t rv; - int off = (len_size == 2 ? -6 : 0); - - nv = nghttp2_frame_nv_copy(multi_empty_headers1); - rv = nghttp2_frame_pack_nv(out, nv, len_size); - CU_ASSERT(13+off == rv); - frame_pack_nv_empty_value_check(out, 0, NULL, len_size); - nghttp2_frame_nv_del(nv); - - nv = nghttp2_frame_nv_copy(multi_empty_headers2); - rv = nghttp2_frame_pack_nv(out, nv, len_size); - CU_ASSERT(14+off == rv); - frame_pack_nv_empty_value_check(out, 1, "/", len_size); - nghttp2_frame_nv_del(nv); - - nv = nghttp2_frame_nv_copy(multi_empty_headers3); - rv = nghttp2_frame_pack_nv(out, nv, len_size); - CU_ASSERT(14+off == rv); - frame_pack_nv_empty_value_check(out, 1, "/", len_size); - nghttp2_frame_nv_del(nv); -} - -void test_nghttp2_frame_count_unpack_nv_space(void) -{ - size_t nvlen, buflen; - uint8_t out[1024]; - size_t len_size = 2; - size_t inlen = nghttp2_frame_pack_nv(out, (char**)headers, len_size); - uint16_t temp; - size_t expected_buflen; - nghttp2_buffer buffer; - uint8_t *chunk; - - nghttp2_buffer_init(&buffer, 4096); - nghttp2_buffer_write(&buffer, out, inlen); - - CU_ASSERT(0 == nghttp2_frame_count_unpack_nv_space(&nvlen, &buflen, - &buffer, len_size)); - CU_ASSERT(7 == nvlen); - expected_buflen = 71+(nvlen*2+1)*sizeof(char*); - CU_ASSERT(expected_buflen == buflen); - - chunk = buffer.root.next->data; - /* Change number of nv pair to a bogus value */ - temp = nghttp2_get_uint16(chunk); - nghttp2_put_uint16be(chunk, temp+1); - CU_ASSERT(NGHTTP2_ERR_INVALID_FRAME == - nghttp2_frame_count_unpack_nv_space(&nvlen, &buflen, &buffer, - len_size)); - nghttp2_put_uint16be(chunk, temp); - - /* Change the length of name to a bogus value */ - temp = nghttp2_get_uint16(chunk+2); - nghttp2_put_uint16be(chunk+2, temp+1); - CU_ASSERT(NGHTTP2_ERR_INVALID_FRAME == - nghttp2_frame_count_unpack_nv_space(&nvlen, &buflen, &buffer, - len_size)); - nghttp2_put_uint16be(chunk+2, 65535); - CU_ASSERT(NGHTTP2_ERR_INVALID_FRAME == - nghttp2_frame_count_unpack_nv_space(&nvlen, &buflen, &buffer, - len_size)); - - /* Trailing garbage */ - nghttp2_buffer_advance(&buffer, 2); - CU_ASSERT(NGHTTP2_ERR_INVALID_FRAME == - nghttp2_frame_count_unpack_nv_space(&nvlen, &buflen, - &buffer, len_size)); - /* We advanced buffer 2 bytes, so it is not valid any more. */ - nghttp2_buffer_free(&buffer); -} - void test_nghttp2_frame_nv_sort(void) { char *nv[7]; @@ -481,17 +104,21 @@ static void check_frame_header(uint16_t length, uint8_t type, uint8_t flags, void test_nghttp2_frame_pack_headers() { - nghttp2_zlib deflater, inflater; + nghttp2_hd_context deflater, inflater; nghttp2_headers frame, oframe; - uint8_t *buf = NULL, *nvbuf = NULL; - size_t buflen = 0, nvbuflen = 0; + uint8_t *buf = NULL; + size_t buflen = 0; ssize_t framelen; - nghttp2_zlib_deflate_hd_init(&deflater, 1, 0); - nghttp2_zlib_inflate_hd_init(&inflater, 0); + nghttp2_nv *nva; + ssize_t nvlen; + + nghttp2_hd_deflate_init(&deflater, NGHTTP2_HD_SIDE_CLIENT); + nghttp2_hd_inflate_init(&inflater, NGHTTP2_HD_SIDE_SERVER); + + nvlen = nghttp2_nv_array_from_cstr(&nva, headers); nghttp2_frame_headers_init(&frame, NGHTTP2_FLAG_END_STREAM, 1000000007, - 1 << 20, nghttp2_frame_nv_copy(headers)); - framelen = nghttp2_frame_pack_headers(&buf, &buflen, &nvbuf, &nvbuflen, - &frame, &deflater); + 1 << 20, nva, nvlen); + framelen = nghttp2_frame_pack_headers(&buf, &buflen, &frame, &deflater); CU_ASSERT(0 == unpack_frame_with_nv_block((nghttp2_frame*)&oframe, NGHTTP2_HEADERS, @@ -501,16 +128,16 @@ void test_nghttp2_frame_pack_headers() NGHTTP2_FLAG_END_STREAM, 1000000007, &oframe.hd); /* We didn't include PRIORITY flag so priority is not packed */ CU_ASSERT(1 << 30 == oframe.pri); - CU_ASSERT(strcmp("method", oframe.nv[0]) == 0); - CU_ASSERT(strcmp("GET", oframe.nv[1]) == 0); - CU_ASSERT(NULL == oframe.nv[14]); + CU_ASSERT(7 == oframe.nvlen); + CU_ASSERT(memcmp("method", oframe.nva[0].name, oframe.nva[0].namelen) == 0); + CU_ASSERT(nvnameeq("method", &oframe.nva[0])); + CU_ASSERT(nvvalueeq("GET", &oframe.nva[0])); nghttp2_frame_headers_free(&oframe); memset(&oframe, 0, sizeof(oframe)); /* Next, include PRIORITY flag */ frame.hd.flags |= NGHTTP2_FLAG_PRIORITY; - framelen = nghttp2_frame_pack_headers(&buf, &buflen, &nvbuf, &nvbuflen, - &frame, &deflater); + framelen = nghttp2_frame_pack_headers(&buf, &buflen, &frame, &deflater); CU_ASSERT(0 == unpack_frame_with_nv_block((nghttp2_frame*)&oframe, NGHTTP2_HEADERS, @@ -520,42 +147,42 @@ void test_nghttp2_frame_pack_headers() NGHTTP2_FLAG_END_STREAM | NGHTTP2_FLAG_PRIORITY, 1000000007, &oframe.hd); CU_ASSERT(1 << 20 == oframe.pri); - CU_ASSERT(strcmp("method", oframe.nv[0]) == 0); + CU_ASSERT(nvnameeq("method", &oframe.nva[0])); free(buf); - free(nvbuf); nghttp2_frame_headers_free(&oframe); nghttp2_frame_headers_free(&frame); - nghttp2_zlib_inflate_free(&inflater); - nghttp2_zlib_deflate_free(&deflater); + nghttp2_hd_inflate_free(&inflater); + nghttp2_hd_deflate_free(&deflater); } void test_nghttp2_frame_pack_headers_frame_too_large(void) { - nghttp2_zlib deflater; + nghttp2_hd_context deflater; nghttp2_headers frame; - uint8_t *buf = NULL, *nvbuf = NULL; - size_t buflen = 0, nvbuflen = 0; + uint8_t *buf = NULL; + size_t buflen = 0; ssize_t framelen; - size_t big_vallen = 1 << 16; + nghttp2_nv *nva; + ssize_t nvlen; + size_t big_vallen = (1 << 16) - 1; char *big_val = malloc(big_vallen + 1); const char *big_hds[] = { "header", big_val, NULL }; + memset(big_val, '0', big_vallen); big_val[big_vallen] = '\0'; - /* No compression */ - nghttp2_zlib_deflate_hd_init(&deflater, 0, 0); + nvlen = nghttp2_nv_array_from_cstr(&nva, big_hds); + nghttp2_hd_deflate_init(&deflater, NGHTTP2_HD_SIDE_CLIENT); nghttp2_frame_headers_init(&frame, NGHTTP2_FLAG_END_STREAM, 1000000007, - 0, nghttp2_frame_nv_copy(big_hds)); - framelen = nghttp2_frame_pack_headers(&buf, &buflen, - &nvbuf, &nvbuflen, - &frame, &deflater); - CU_ASSERT_EQUAL(NGHTTP2_ERR_FRAME_TOO_LARGE, framelen); + 0, nva, nvlen); + framelen = nghttp2_frame_pack_headers(&buf, &buflen, &frame, &deflater); + printf("framelen=%ld\n", framelen); + CU_ASSERT_EQUAL(NGHTTP2_ERR_HEADER_COMP, framelen); nghttp2_frame_headers_free(&frame); free(buf); - free(nvbuf); free(big_val); - nghttp2_zlib_deflate_free(&deflater); + nghttp2_hd_deflate_free(&deflater); } void test_nghttp2_frame_pack_priority(void) diff --git a/tests/nghttp2_frame_test.h b/tests/nghttp2_frame_test.h index 7f47203c..680433d6 100644 --- a/tests/nghttp2_frame_test.h +++ b/tests/nghttp2_frame_test.h @@ -25,13 +25,6 @@ #ifndef NGHTTP2_FRAME_TEST_H #define NGHTTP2_FRAME_TEST_H -void test_nghttp2_frame_unpack_nv(void); -void test_nghttp2_frame_unpack_nv_check_name(void); -void test_nghttp2_frame_unpack_nv_last_empty_value(void); -void test_nghttp2_frame_pack_nv_duplicate_keys(void); -void test_nghttp2_frame_count_nv_space(void); -void test_nghttp2_frame_pack_nv_empty_value(void); -void test_nghttp2_frame_count_unpack_nv_space(void); void test_nghttp2_frame_nv_sort(void); void test_nghttp2_frame_nv_downcase(void); void test_nghttp2_frame_nv_check_null(void); diff --git a/tests/nghttp2_hd_test.c b/tests/nghttp2_hd_test.c index 0d33aff1..d6b80933 100644 --- a/tests/nghttp2_hd_test.c +++ b/tests/nghttp2_hd_test.c @@ -51,7 +51,6 @@ void test_nghttp2_hd_deflate(void) MAKE_NV("hello", "world")}; nghttp2_nv nva2[] = {MAKE_NV(":path", "/script.js"), MAKE_NV(":scheme", "https")}; - nghttp2_nv nvtemp; size_t nv_offset = 12; uint8_t *buf = NULL; size_t buflen = 0; @@ -71,7 +70,7 @@ void test_nghttp2_hd_deflate(void) assert_nv_equal(nva1, resnva, 3); - nghttp2_nv_array_free(resnva); + nghttp2_nv_array_del(resnva); nghttp2_hd_end_headers(&inflater); CU_ASSERT(1 == inflater.refsetlen); @@ -85,13 +84,9 @@ void test_nghttp2_hd_deflate(void) CU_ASSERT(2 == nghttp2_hd_inflate_hd(&inflater, &resnva, buf + nv_offset, blocklen)); - /* First and second header fields are interchanged their positions. */ - nvtemp = nva2[0]; - nva2[0] = nva2[1]; - nva2[1] = nvtemp; assert_nv_equal(nva2, resnva, 2); - nghttp2_nv_array_free(resnva); + nghttp2_nv_array_del(resnva); nghttp2_hd_end_headers(&inflater); free(buf); @@ -118,7 +113,7 @@ void test_nghttp2_hd_inflate_indname_inc(void) CU_ASSERT(39 == inflater.hd_tablelen); assert_nv_equal(&nv, &inflater.hd_table[inflater.hd_tablelen-1]->nv, 1); - nghttp2_nv_array_free(resnva); + nghttp2_nv_array_del(resnva); free(buf); nghttp2_hd_inflate_free(&inflater); } @@ -144,7 +139,7 @@ void test_nghttp2_hd_inflate_indname_inc_eviction(void) CU_ASSERT(0 == memcmp(":host", resnva[0].name, resnva[0].namelen)); CU_ASSERT(sizeof(value) == resnva[0].valuelen); - nghttp2_nv_array_free(resnva); + nghttp2_nv_array_del(resnva); nghttp2_hd_end_headers(&inflater); CU_ASSERT(38 == inflater.hd_tablelen); @@ -171,7 +166,7 @@ void test_nghttp2_hd_inflate_newname_inc(void) CU_ASSERT(39 == inflater.hd_tablelen); assert_nv_equal(&nv, &inflater.hd_table[inflater.hd_tablelen-1]->nv, 1); - nghttp2_nv_array_free(resnva); + nghttp2_nv_array_del(resnva); free(buf); nghttp2_hd_inflate_free(&inflater); } @@ -195,7 +190,7 @@ void test_nghttp2_hd_inflate_indname_subst(void) CU_ASSERT(38 == inflater.hd_tablelen); assert_nv_equal(&nv, &inflater.hd_table[12]->nv, 1); - nghttp2_nv_array_free(resnva); + nghttp2_nv_array_del(resnva); free(buf); nghttp2_hd_inflate_free(&inflater); } @@ -221,7 +216,7 @@ void test_nghttp2_hd_inflate_indname_subst_eviction(void) CU_ASSERT(0 == memcmp(":host", resnva[0].name, resnva[0].namelen)); CU_ASSERT(sizeof(value) == resnva[0].valuelen); - nghttp2_nv_array_free(resnva); + nghttp2_nv_array_del(resnva); nghttp2_hd_end_headers(&inflater); CU_ASSERT(37 == inflater.hd_tablelen); @@ -252,7 +247,7 @@ void test_nghttp2_hd_inflate_indname_subst_eviction_neg(void) CU_ASSERT(0 == memcmp(":host", resnva[0].name, resnva[0].namelen)); CU_ASSERT(sizeof(value) == resnva[0].valuelen); - nghttp2_nv_array_free(resnva); + nghttp2_nv_array_del(resnva); nghttp2_hd_end_headers(&inflater); CU_ASSERT(37 == inflater.hd_tablelen); @@ -279,7 +274,7 @@ void test_nghttp2_hd_inflate_newname_subst(void) CU_ASSERT(38 == inflater.hd_tablelen); assert_nv_equal(&nv, &inflater.hd_table[1]->nv, 1); - nghttp2_nv_array_free(resnva); + nghttp2_nv_array_del(resnva); free(buf); nghttp2_hd_inflate_free(&inflater); } diff --git a/tests/nghttp2_session_test.c b/tests/nghttp2_session_test.c index 7b262f2e..a9921937 100644 --- a/tests/nghttp2_session_test.c +++ b/tests/nghttp2_session_test.c @@ -268,11 +268,6 @@ static void stream_close_callback(nghttp2_session *session, int32_t stream_id, CU_ASSERT(stream_data != NULL); } -static char** dup_nv(const char **src) -{ - return nghttp2_frame_nv_copy(src); -} - static nghttp2_settings_entry* dup_iv(const nghttp2_settings_entry *iv, size_t niv) { @@ -296,28 +291,17 @@ void test_nghttp2_session_recv(void) const char *nv[] = { "url", "/", NULL }; - const char *upcase_nv[] = { - "URL", "/", NULL - }; const char *empty_nv[] = { NULL }; - const char *mid_nv[] = { - "method", "GET", - "scheme", "https", - "url", "/", - "x-head", "foo", - "x-head", "bar", - "version", "HTTP/1.1", - "x-empty", "", - NULL - }; - uint8_t *framedata = NULL, *nvbuf = NULL; - size_t framedatalen = 0, nvbuflen = 0; + uint8_t *framedata = NULL; + size_t framedatalen = 0; ssize_t framelen; nghttp2_frame frame; int i; nghttp2_outbound_item *item; + nghttp2_nv *nva; + ssize_t nvlen; memset(&callbacks, 0, sizeof(nghttp2_session_callbacks)); callbacks.send_callback = null_send_callback; @@ -325,10 +309,11 @@ void test_nghttp2_session_recv(void) callbacks.on_frame_recv_callback = on_frame_recv_callback; user_data.df = &df; nghttp2_session_server_new(&session, &callbacks, &user_data); + + nvlen = nghttp2_nv_array_from_cstr(&nva, nv); nghttp2_frame_headers_init(&frame.headers, NGHTTP2_FLAG_END_HEADERS, - 1, NGHTTP2_PRI_DEFAULT, dup_nv(nv)); + 1, NGHTTP2_PRI_DEFAULT, nva, nvlen); framelen = nghttp2_frame_pack_headers(&framedata, &framedatalen, - &nvbuf, &nvbuflen, &frame.headers, &session->hd_deflater); scripted_data_feed_init(&df, framedata, framelen); @@ -344,29 +329,11 @@ void test_nghttp2_session_recv(void) } CU_ASSERT(1 == user_data.frame_recv_cb_called); - /* Receive HEADERS with invalid header block */ - nghttp2_frame_headers_init(&frame.headers, NGHTTP2_FLAG_END_HEADERS, - 3, NGHTTP2_PRI_DEFAULT, dup_nv(upcase_nv)); - framelen = nghttp2_frame_pack_headers(&framedata, &framedatalen, - &nvbuf, &nvbuflen, - &frame.headers, - &session->hd_deflater); - nghttp2_frame_headers_free(&frame.headers); - - scripted_data_feed_init(&df, framedata, framelen); - user_data.frame_recv_cb_called = 0; - CU_ASSERT(0 == nghttp2_session_recv(session)); - CU_ASSERT(0 == user_data.frame_recv_cb_called); - item = nghttp2_session_get_next_ob_item(session); - CU_ASSERT(NGHTTP2_RST_STREAM == OB_CTRL_TYPE(item)); - CU_ASSERT(NGHTTP2_PROTOCOL_ERROR == OB_CTRL(item)->rst_stream.error_code); - CU_ASSERT(0 == nghttp2_session_send(session)); - /* Received HEADERS without header block, which is valid */ + nvlen = nghttp2_nv_array_from_cstr(&nva, empty_nv); nghttp2_frame_headers_init(&frame.headers, NGHTTP2_FLAG_END_HEADERS, - 5, NGHTTP2_PRI_DEFAULT, dup_nv(empty_nv)); + 5, NGHTTP2_PRI_DEFAULT, nva, nvlen); framelen = nghttp2_frame_pack_headers(&framedata, &framedatalen, - &nvbuf, &nvbuflen, &frame.headers, &session->hd_deflater); nghttp2_frame_headers_free(&frame.headers); @@ -383,25 +350,6 @@ void test_nghttp2_session_recv(void) /* made max buffer small to cause error intentionally */ session->max_recv_ctrl_frame_buf = 8; - /* Receive HEADERS with too large payload */ - nghttp2_frame_headers_init(&frame.headers, NGHTTP2_FLAG_END_HEADERS, - 1, NGHTTP2_PRI_DEFAULT, dup_nv(mid_nv)); - framelen = nghttp2_frame_pack_headers(&framedata, &framedatalen, - &nvbuf, &nvbuflen, - &frame.headers, - &session->hd_deflater); - nghttp2_frame_headers_free(&frame.headers); - - scripted_data_feed_init(&df, framedata, framelen); - user_data.frame_recv_cb_called = 0; - CU_ASSERT(0 == nghttp2_session_recv(session)); - CU_ASSERT(0 == user_data.frame_recv_cb_called); - item = nghttp2_session_get_next_ob_item(session); - CU_ASSERT(NGHTTP2_RST_STREAM == OB_CTRL_TYPE(item)); - CU_ASSERT(NGHTTP2_FRAME_TOO_LARGE == OB_CTRL(item)->rst_stream.error_code); - CU_ASSERT(1 == OB_CTRL(item)->hd.stream_id); - CU_ASSERT(0 == nghttp2_session_send(session)); - /* Receive PING with too large payload */ nghttp2_frame_ping_init(&frame.ping, NGHTTP2_FLAG_NONE, NULL); nghttp2_reserve_buffer(&framedata, &framedatalen, 77); @@ -419,33 +367,7 @@ void test_nghttp2_session_recv(void) CU_ASSERT(NGHTTP2_PROTOCOL_ERROR == OB_CTRL(item)->goaway.error_code); CU_ASSERT(0 == nghttp2_session_send(session)); - nghttp2_session_del(session); - - nghttp2_session_client_new(&session, &callbacks, &user_data); - /* Receive HEADERS with invalid header block */ - nghttp2_session_open_stream(session, 1, NGHTTP2_FLAG_NONE, - NGHTTP2_PRI_DEFAULT, - NGHTTP2_STREAM_OPENING, NULL); - nghttp2_frame_headers_init(&frame.headers, NGHTTP2_FLAG_END_HEADERS, 1, - NGHTTP2_PRI_DEFAULT, dup_nv(upcase_nv)); - framelen = nghttp2_frame_pack_headers(&framedata, &framedatalen, - &nvbuf, &nvbuflen, - &frame.headers, - &session->hd_deflater); - nghttp2_frame_headers_free(&frame.headers); - - scripted_data_feed_init(&df, framedata, framelen); - user_data.frame_recv_cb_called = 0; - CU_ASSERT(0 == nghttp2_session_recv(session)); - CU_ASSERT(0 == user_data.frame_recv_cb_called); - item = nghttp2_session_get_next_ob_item(session); - CU_ASSERT(NGHTTP2_RST_STREAM == OB_CTRL_TYPE(item)); - CU_ASSERT(NGHTTP2_PROTOCOL_ERROR == OB_CTRL(item)->rst_stream.error_code); - - CU_ASSERT(0 == nghttp2_session_send(session)); - free(framedata); - free(nvbuf); nghttp2_session_del(session); } @@ -456,10 +378,12 @@ void test_nghttp2_session_recv_invalid_stream_id(void) scripted_data_feed df; my_user_data user_data; const char *nv[] = { NULL }; - uint8_t *framedata = NULL, *nvbuf = NULL; - size_t framedatalen = 0, nvbuflen = 0; + uint8_t *framedata = NULL; + size_t framedatalen = 0; ssize_t framelen; nghttp2_frame frame; + nghttp2_nv *nva; + ssize_t nvlen; memset(&callbacks, 0, sizeof(nghttp2_session_callbacks)); callbacks.recv_callback = scripted_recv_callback; callbacks.on_invalid_frame_recv_callback = on_invalid_frame_recv_callback; @@ -467,10 +391,10 @@ void test_nghttp2_session_recv_invalid_stream_id(void) user_data.df = &df; user_data.invalid_frame_recv_cb_called = 0; nghttp2_session_server_new(&session, &callbacks, &user_data); + nvlen = nghttp2_nv_array_from_cstr(&nva, nv); nghttp2_frame_headers_init(&frame.headers, NGHTTP2_FLAG_END_HEADERS, 2, - NGHTTP2_PRI_DEFAULT, dup_nv(nv)); + NGHTTP2_PRI_DEFAULT, nva, nvlen); framelen = nghttp2_frame_pack_headers(&framedata, &framedatalen, - &nvbuf, &nvbuflen, &frame.headers, &session->hd_deflater); scripted_data_feed_init(&df, framedata, framelen); @@ -480,7 +404,6 @@ void test_nghttp2_session_recv_invalid_stream_id(void) CU_ASSERT(1 == user_data.invalid_frame_recv_cb_called); free(framedata); - free(nvbuf); nghttp2_session_del(session); } @@ -493,10 +416,12 @@ void test_nghttp2_session_recv_invalid_frame(void) const char *nv[] = { "url", "/", NULL }; - uint8_t *framedata = NULL, *nvbuf = NULL; - size_t framedatalen = 0, nvbuflen = 0; + uint8_t *framedata = NULL; + size_t framedatalen = 0; ssize_t framelen; nghttp2_frame frame; + nghttp2_nv *nva; + ssize_t nvlen; memset(&callbacks, 0, sizeof(nghttp2_session_callbacks)); callbacks.recv_callback = scripted_recv_callback; @@ -506,10 +431,10 @@ void test_nghttp2_session_recv_invalid_frame(void) user_data.df = &df; user_data.frame_send_cb_called = 0; nghttp2_session_server_new(&session, &callbacks, &user_data); + nvlen = nghttp2_nv_array_from_cstr(&nva, nv); nghttp2_frame_headers_init(&frame.headers, NGHTTP2_FLAG_END_HEADERS, 1, - NGHTTP2_PRI_DEFAULT, dup_nv(nv)); + NGHTTP2_PRI_DEFAULT, nva, nvlen); framelen = nghttp2_frame_pack_headers(&framedata, &framedatalen, - &nvbuf, &nvbuflen, &frame.headers, &session->hd_deflater); scripted_data_feed_init(&df, framedata, framelen); @@ -527,7 +452,6 @@ void test_nghttp2_session_recv_invalid_frame(void) CU_ASSERT(NGHTTP2_GOAWAY == user_data.sent_frame_type); free(framedata); - free(nvbuf); nghttp2_frame_headers_free(&frame.headers); nghttp2_session_del(session); @@ -652,6 +576,9 @@ void test_nghttp2_session_add_frame(void) nghttp2_frame *frame; nghttp2_headers_aux_data *aux_data = malloc(sizeof(nghttp2_headers_aux_data)); + nghttp2_nv *nva; + ssize_t nvlen; + memset(&callbacks, 0, sizeof(nghttp2_session_callbacks)); callbacks.send_callback = accumulator_send_callback; memset(aux_data, 0, sizeof(nghttp2_headers_aux_data)); @@ -660,9 +587,10 @@ void test_nghttp2_session_add_frame(void) CU_ASSERT(0 == nghttp2_session_client_new(&session, &callbacks, &user_data)); frame = malloc(sizeof(nghttp2_frame)); + nvlen = nghttp2_nv_array_from_cstr(&nva, nv); nghttp2_frame_headers_init(&frame->headers, NGHTTP2_FLAG_END_HEADERS | NGHTTP2_FLAG_PRIORITY, - -1, NGHTTP2_PRI_DEFAULT, dup_nv(nv)); + -1, NGHTTP2_PRI_DEFAULT, nva, nvlen); CU_ASSERT(0 == nghttp2_session_add_frame(session, NGHTTP2_CAT_CTRL, frame, aux_data)); @@ -685,6 +613,9 @@ void test_nghttp2_session_on_syn_stream_received(void) nghttp2_frame frame; nghttp2_stream *stream; int32_t stream_id = 1; + nghttp2_nv *nva; + ssize_t nvlen; + memset(&callbacks, 0, sizeof(nghttp2_session_callbacks)); callbacks.on_frame_recv_callback = on_frame_recv_callback; callbacks.on_invalid_frame_recv_callback = on_invalid_frame_recv_callback; @@ -692,9 +623,10 @@ void test_nghttp2_session_on_syn_stream_received(void) user_data.invalid_frame_recv_cb_called = 0; nghttp2_session_server_new(&session, &callbacks, &user_data); + nvlen = nghttp2_nv_array_from_cstr(&nva, nv); nghttp2_frame_headers_init(&frame.headers, NGHTTP2_FLAG_END_HEADERS | NGHTTP2_FLAG_PRIORITY, - stream_id, 1 << 20, dup_nv(nv)); + stream_id, 1 << 20, nva, nvlen); CU_ASSERT(0 == nghttp2_session_on_syn_stream_received(session, &frame)); CU_ASSERT(1 == user_data.frame_recv_cb_called); @@ -706,8 +638,9 @@ void test_nghttp2_session_on_syn_stream_received(void) /* More than max concurrent streams leads REFUSED_STREAM */ session->local_settings[NGHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS] = 1; + nvlen = nghttp2_nv_array_from_cstr(&nva, nv); nghttp2_frame_headers_init(&frame.headers, NGHTTP2_FLAG_END_HEADERS, - 3, NGHTTP2_PRI_DEFAULT, dup_nv(nv)); + 3, NGHTTP2_PRI_DEFAULT, nva, nvlen); user_data.invalid_frame_recv_cb_called = 0; CU_ASSERT(0 == nghttp2_session_on_syn_stream_received(session, &frame)); CU_ASSERT(1 == user_data.invalid_frame_recv_cb_called); @@ -719,8 +652,9 @@ void test_nghttp2_session_on_syn_stream_received(void) /* Stream ID less than or equal to the previouly received SYN_STREAM leads to connection error */ + nvlen = nghttp2_nv_array_from_cstr(&nva, nv); nghttp2_frame_headers_init(&frame.headers, NGHTTP2_FLAG_END_HEADERS, - 3, NGHTTP2_PRI_DEFAULT, dup_nv(nv)); + 3, NGHTTP2_PRI_DEFAULT, nva, nvlen); user_data.invalid_frame_recv_cb_called = 0; CU_ASSERT(0 == nghttp2_session_on_syn_stream_received(session, &frame)); CU_ASSERT(1 == user_data.invalid_frame_recv_cb_called); @@ -740,6 +674,8 @@ void test_nghttp2_session_on_syn_reply_received(void) nghttp2_frame frame; nghttp2_stream *stream; nghttp2_outbound_item *item; + nghttp2_nv *nva; + ssize_t nvlen; memset(&callbacks, 0, sizeof(nghttp2_session_callbacks)); callbacks.on_frame_recv_callback = on_frame_recv_callback; @@ -751,8 +687,9 @@ void test_nghttp2_session_on_syn_reply_received(void) stream = nghttp2_session_open_stream(session, 1, NGHTTP2_FLAG_NONE, NGHTTP2_PRI_DEFAULT, NGHTTP2_STREAM_OPENING, NULL); + nvlen = nghttp2_nv_array_from_cstr(&nva, nv); nghttp2_frame_headers_init(&frame.headers, NGHTTP2_FLAG_END_HEADERS, 1, - NGHTTP2_PRI_DEFAULT, dup_nv(nv)); + NGHTTP2_PRI_DEFAULT, nva, nvlen); CU_ASSERT(0 == nghttp2_session_on_syn_reply_received(session, &frame, stream)); @@ -786,6 +723,9 @@ void test_nghttp2_session_on_headers_received(void) const char *nv[] = { NULL }; nghttp2_frame frame; nghttp2_stream *stream; + nghttp2_nv *nva; + ssize_t nvlen; + memset(&callbacks, 0, sizeof(nghttp2_session_callbacks)); callbacks.on_frame_recv_callback = on_frame_recv_callback; callbacks.on_invalid_frame_recv_callback = on_invalid_frame_recv_callback; @@ -797,8 +737,9 @@ void test_nghttp2_session_on_headers_received(void) NGHTTP2_PRI_DEFAULT, NGHTTP2_STREAM_OPENED, NULL); nghttp2_stream_shutdown(stream, NGHTTP2_SHUT_WR); + nvlen = nghttp2_nv_array_from_cstr(&nva, nv); nghttp2_frame_headers_init(&frame.headers, NGHTTP2_FLAG_END_HEADERS, 1, - NGHTTP2_PRI_DEFAULT, dup_nv(nv)); + NGHTTP2_PRI_DEFAULT, nva, nvlen); CU_ASSERT(0 == nghttp2_session_on_headers_received(session, &frame, stream)); CU_ASSERT(1 == user_data.frame_recv_cb_called); @@ -1108,13 +1049,17 @@ void test_nghttp2_session_send_headers_start_stream(void) nghttp2_stream *stream; nghttp2_headers_aux_data *aux_data = malloc(sizeof(nghttp2_headers_aux_data)); + nghttp2_nv *nva; + ssize_t nvlen; + memset(aux_data, 0, sizeof(nghttp2_headers_aux_data)); memset(&callbacks, 0, sizeof(nghttp2_session_callbacks)); callbacks.send_callback = null_send_callback; nghttp2_session_client_new(&session, &callbacks, NULL); + nvlen = nghttp2_nv_array_from_cstr(&nva, nv); nghttp2_frame_headers_init(&frame->headers, NGHTTP2_FLAG_END_HEADERS, -1, - NGHTTP2_PRI_DEFAULT, dup_nv(nv)); + NGHTTP2_PRI_DEFAULT, nva, nvlen); nghttp2_session_add_frame(session, NGHTTP2_CAT_CTRL, frame, aux_data); CU_ASSERT(0 == nghttp2_session_send(session)); stream = nghttp2_session_get_stream(session, 1); @@ -1130,6 +1075,8 @@ void test_nghttp2_session_send_headers_reply(void) const char *nv[] = { NULL }; nghttp2_frame *frame = malloc(sizeof(nghttp2_frame)); nghttp2_stream *stream; + nghttp2_nv *nva; + ssize_t nvlen; memset(&callbacks, 0, sizeof(nghttp2_session_callbacks)); callbacks.send_callback = null_send_callback; @@ -1138,8 +1085,9 @@ void test_nghttp2_session_send_headers_reply(void) nghttp2_session_open_stream(session, 2, NGHTTP2_FLAG_NONE, NGHTTP2_PRI_DEFAULT, NGHTTP2_STREAM_OPENING, NULL); + nvlen = nghttp2_nv_array_from_cstr(&nva, nv); nghttp2_frame_headers_init(&frame->headers, NGHTTP2_FLAG_END_HEADERS, 2, - NGHTTP2_PRI_DEFAULT, dup_nv(nv)); + NGHTTP2_PRI_DEFAULT, nva, nvlen); nghttp2_session_add_frame(session, NGHTTP2_CAT_CTRL, frame, NULL); CU_ASSERT(0 == nghttp2_session_send(session)); stream = nghttp2_session_get_stream(session, 2); @@ -1213,7 +1161,7 @@ void test_nghttp2_submit_response(void) NGHTTP2_STREAM_OPENING, NULL); CU_ASSERT(0 == nghttp2_submit_response(session, 2, nv, &data_prd)); item = nghttp2_session_get_next_ob_item(session); - CU_ASSERT(0 == strcmp("content-length", OB_CTRL(item)->headers.nv[0])); + CU_ASSERT(nvnameeq("content-length", &OB_CTRL(item)->headers.nva[0])); CU_ASSERT(0 == nghttp2_session_send(session)); nghttp2_session_del(session); } @@ -1239,14 +1187,14 @@ void test_nghttp2_submit_response_without_data(void) NGHTTP2_STREAM_OPENING, NULL); CU_ASSERT(0 == nghttp2_submit_response(session, 1, nv, &data_prd)); item = nghttp2_session_get_next_ob_item(session); - CU_ASSERT(0 == strcmp(":version", OB_CTRL(item)->headers.nv[0])); + CU_ASSERT(nvnameeq(":version", &OB_CTRL(item)->headers.nva[0])); CU_ASSERT(OB_CTRL(item)->hd.flags & NGHTTP2_FLAG_END_STREAM); CU_ASSERT(0 == nghttp2_session_send(session)); CU_ASSERT(0 == unpack_frame_with_nv_block(&frame, NGHTTP2_HEADERS, &session->hd_inflater, acc.buf, acc.length)); - CU_ASSERT(0 == strcmp(":version", frame.headers.nv[0])); + CU_ASSERT(nvnameeq(":version", &frame.headers.nva[0])); nghttp2_frame_headers_free(&frame.headers); nghttp2_session_del(session); @@ -1270,7 +1218,7 @@ void test_nghttp2_submit_request_with_data(void) CU_ASSERT(0 == nghttp2_submit_request(session, NGHTTP2_PRI_DEFAULT, nv, &data_prd, NULL)); item = nghttp2_session_get_next_ob_item(session); - CU_ASSERT(0 == strcmp(":version", OB_CTRL(item)->headers.nv[0])); + CU_ASSERT(nvnameeq(":version", &OB_CTRL(item)->headers.nva[0])); CU_ASSERT(0 == nghttp2_session_send(session)); CU_ASSERT(0 == ud.data_source_length); @@ -1296,14 +1244,14 @@ void test_nghttp2_submit_request_without_data(void) CU_ASSERT(0 == nghttp2_submit_request(session, NGHTTP2_PRI_DEFAULT, nv, &data_prd, NULL)); item = nghttp2_session_get_next_ob_item(session); - CU_ASSERT(0 == strcmp(":version", OB_CTRL(item)->headers.nv[0])); + CU_ASSERT(nvnameeq(":version", &OB_CTRL(item)->headers.nva[0])); CU_ASSERT(OB_CTRL(item)->hd.flags & NGHTTP2_FLAG_END_STREAM); CU_ASSERT(0 == nghttp2_session_send(session)); CU_ASSERT(0 == unpack_frame_with_nv_block(&frame, NGHTTP2_HEADERS, &session->hd_inflater, acc.buf, acc.length)); - CU_ASSERT(0 == strcmp(":version", frame.headers.nv[0])); + CU_ASSERT(nvnameeq(":version", &frame.headers.nva[0])); nghttp2_frame_headers_free(&frame.headers); nghttp2_session_del(session); @@ -1324,7 +1272,7 @@ void test_nghttp2_submit_headers_start_stream(void) -1, NGHTTP2_PRI_DEFAULT, nv, NULL)); item = nghttp2_session_get_next_ob_item(session); - CU_ASSERT(0 == strcmp(":version", OB_CTRL(item)->headers.nv[0])); + CU_ASSERT(nvnameeq(":version", &OB_CTRL(item)->headers.nva[0])); CU_ASSERT((NGHTTP2_FLAG_END_HEADERS | NGHTTP2_FLAG_END_STREAM) == OB_CTRL(item)->hd.flags); CU_ASSERT(NGHTTP2_PRI_DEFAULT == OB_CTRL(item)->headers.pri); @@ -1351,7 +1299,7 @@ void test_nghttp2_submit_headers_reply(void) 1, NGHTTP2_PRI_DEFAULT, nv, NULL)); item = nghttp2_session_get_next_ob_item(session); - CU_ASSERT(0 == strcmp(":version", OB_CTRL(item)->headers.nv[0])); + CU_ASSERT(nvnameeq(":version", &OB_CTRL(item)->headers.nva[0])); CU_ASSERT((NGHTTP2_FLAG_END_STREAM | NGHTTP2_FLAG_END_HEADERS) == OB_CTRL(item)->hd.flags); @@ -1402,7 +1350,7 @@ void test_nghttp2_submit_headers(void) 1, NGHTTP2_PRI_DEFAULT, nv, NULL)); item = nghttp2_session_get_next_ob_item(session); - CU_ASSERT(0 == strcmp(":version", OB_CTRL(item)->headers.nv[0])); + CU_ASSERT(nvnameeq(":version", &OB_CTRL(item)->headers.nva[0])); CU_ASSERT((NGHTTP2_FLAG_END_STREAM | NGHTTP2_FLAG_END_HEADERS) == OB_CTRL(item)->hd.flags); @@ -1430,7 +1378,7 @@ void test_nghttp2_submit_headers(void) NGHTTP2_HEADERS, &session->hd_inflater, acc.buf, acc.length)); - CU_ASSERT(0 == strcmp(":version", frame.headers.nv[0])); + CU_ASSERT(nvnameeq(":version", &frame.headers.nva[0])); nghttp2_frame_headers_free(&frame.headers); nghttp2_session_del(session); @@ -1734,14 +1682,17 @@ void test_nghttp2_session_max_concurrent_streams(void) nghttp2_frame frame; const char *nv[] = { NULL }; nghttp2_outbound_item *item; + nghttp2_nv *nva; + ssize_t nvlen; memset(&callbacks, 0, sizeof(nghttp2_session_callbacks)); nghttp2_session_server_new(&session, &callbacks, NULL); nghttp2_session_open_stream(session, 1, NGHTTP2_FLAG_NONE, NGHTTP2_PRI_DEFAULT, NGHTTP2_STREAM_OPENED, NULL); + nvlen = nghttp2_nv_array_from_cstr(&nva, nv); nghttp2_frame_headers_init(&frame.headers, NGHTTP2_FLAG_NONE, 3, - NGHTTP2_PRI_DEFAULT, dup_nv(nv)); + NGHTTP2_PRI_DEFAULT, nva, nvlen); session->local_settings[NGHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS] = 1; CU_ASSERT(0 == nghttp2_session_on_syn_stream_received(session, &frame)); @@ -2125,13 +2076,17 @@ void test_nghttp2_session_on_request_recv_callback(void) const char *nv[] = { NULL }; nghttp2_frame frame; nghttp2_stream *stream; + nghttp2_nv *nva; + ssize_t nvlen; + memset(&callbacks, 0, sizeof(nghttp2_session_callbacks)); callbacks.on_request_recv_callback = on_request_recv_callback; user_data.stream_id = 0; nghttp2_session_server_new(&session, &callbacks, &user_data); + nvlen = nghttp2_nv_array_from_cstr(&nva, nv); nghttp2_frame_headers_init(&frame.headers, NGHTTP2_FLAG_END_HEADERS, - 1, NGHTTP2_PRI_DEFAULT, dup_nv(nv)); + 1, NGHTTP2_PRI_DEFAULT, nva, nvlen); CU_ASSERT(0 == nghttp2_session_on_syn_stream_received(session, &frame)); CU_ASSERT(0 == user_data.stream_id); @@ -2148,8 +2103,9 @@ void test_nghttp2_session_on_request_recv_callback(void) stream = nghttp2_session_open_stream(session, 5, NGHTTP2_FLAG_NONE, NGHTTP2_PRI_DEFAULT, NGHTTP2_STREAM_OPENING, NULL); + nvlen = nghttp2_nv_array_from_cstr(&nva, nv); nghttp2_frame_headers_init(&frame.headers, NGHTTP2_FLAG_END_HEADERS, - 5, NGHTTP2_PRI_DEFAULT, dup_nv(nv)); + 5, NGHTTP2_PRI_DEFAULT, nva, nvlen); CU_ASSERT(0 == nghttp2_session_on_headers_received(session, &frame, stream)); CU_ASSERT(0 == user_data.stream_id); diff --git a/tests/nghttp2_test_helper.c b/tests/nghttp2_test_helper.c index 7c68fc7c..e6dbbd3b 100644 --- a/tests/nghttp2_test_helper.c +++ b/tests/nghttp2_test_helper.c @@ -32,33 +32,22 @@ ssize_t unpack_frame_with_nv_block(nghttp2_frame *frame, nghttp2_frame_type type, - nghttp2_zlib *inflater, + nghttp2_hd_context *inflater, const uint8_t *in, size_t len) { - nghttp2_buffer buffer; ssize_t rv; - ssize_t pnvlen; - pnvlen = nghttp2_frame_nv_offset(in); - assert(pnvlen > 0); - - nghttp2_buffer_init(&buffer, 4096); - rv = nghttp2_zlib_inflate_hd(inflater, &buffer, &in[pnvlen], len - pnvlen); - if(rv < 0) { - return rv; - } switch(type) { case NGHTTP2_HEADERS: rv = nghttp2_frame_unpack_headers((nghttp2_headers*)frame, &in[0], NGHTTP2_FRAME_HEAD_LENGTH, &in[NGHTTP2_FRAME_HEAD_LENGTH], - pnvlen - NGHTTP2_FRAME_HEAD_LENGTH, - &buffer); + len - NGHTTP2_FRAME_HEAD_LENGTH, + inflater); break; default: /* Must not be reachable */ assert(0); } - nghttp2_buffer_free(&buffer); return rv; } @@ -70,3 +59,24 @@ char* strcopy(const char* s) dest[len] = '\0'; return dest; } + +int strmemeq(const char *a, const uint8_t *b, size_t bn) +{ + const uint8_t *c; + if(!a || !b) { + return 0; + } + c = b + bn; + for(; *a && b != c && *a == *b; ++a, ++b); + return !*a && b == c; +} + +int nvnameeq(const char *a, nghttp2_nv *nv) +{ + return strmemeq(a, nv->name, nv->namelen); +} + +int nvvalueeq(const char *a, nghttp2_nv *nv) +{ + return strmemeq(a, nv->value, nv->valuelen); +} diff --git a/tests/nghttp2_test_helper.h b/tests/nghttp2_test_helper.h index e5aeea3b..cf3234fd 100644 --- a/tests/nghttp2_test_helper.h +++ b/tests/nghttp2_test_helper.h @@ -30,13 +30,19 @@ #endif /* HAVE_CONFIG_H */ #include "nghttp2_frame.h" -#include "nghttp2_zlib.h" +#include "nghttp2_hd.h" ssize_t unpack_frame_with_nv_block(nghttp2_frame *frame, nghttp2_frame_type type, - nghttp2_zlib *inflater, + nghttp2_hd_context *inflater, const uint8_t *in, size_t len); char* strcopy(const char* s); +int strmemeq(const char *a, const uint8_t *b, size_t bn); + +int nvnameeq(const char *a, nghttp2_nv *nv); + +int nvvalueeq(const char *a, nghttp2_nv *nv); + #endif /* NGHTTP2_TEST_HELPER_H */