diff --git a/lib/nghttp2_hd.c b/lib/nghttp2_hd.c index 0aab61d8..725e5004 100644 --- a/lib/nghttp2_hd.c +++ b/lib/nghttp2_hd.c @@ -118,19 +118,28 @@ int nghttp2_hd_entry_init(nghttp2_hd_entry *ent, uint8_t index, uint8_t flags, { int rv = 0; if(flags & NGHTTP2_HD_FLAG_NAME_ALLOC) { - ent->nv.name = nghttp2_memdup(name, namelen); - if(ent->nv.name == NULL) { - rv = NGHTTP2_ERR_NOMEM; - goto fail; + if(namelen == 0) { + /* We should not allow empty header field name */ + ent->nv.name = NULL; + } else { + ent->nv.name = nghttp2_memdup(name, namelen); + if(ent->nv.name == NULL) { + rv = NGHTTP2_ERR_NOMEM; + goto fail; + } } } else { ent->nv.name = name; } if(flags & NGHTTP2_HD_FLAG_VALUE_ALLOC) { - ent->nv.value = nghttp2_memdup(value, valuelen); - if(ent->nv.value == NULL) { - rv = NGHTTP2_ERR_NOMEM; - goto fail2; + if(valuelen == 0) { + ent->nv.value = NULL; + } else { + ent->nv.value = nghttp2_memdup(value, valuelen); + if(ent->nv.value == NULL) { + rv = NGHTTP2_ERR_NOMEM; + goto fail2; + } } } else { ent->nv.value = value; diff --git a/tests/nghttp2_hd_test.c b/tests/nghttp2_hd_test.c index cc5eb6b6..45d12662 100644 --- a/tests/nghttp2_hd_test.c +++ b/tests/nghttp2_hd_test.c @@ -57,6 +57,8 @@ void test_nghttp2_hd_deflate(void) nghttp2_nv nva4[] = {MAKE_NV(":path", "/style.css"), MAKE_NV("cookie", "k1=v1"), MAKE_NV("cookie", "k1=v1")}; + nghttp2_nv nva5[] = {MAKE_NV(":path", "/style.css"), + MAKE_NV("x-nghttp2", "")}; size_t nv_offset = 12; uint8_t *buf = NULL; size_t buflen = 0; @@ -125,6 +127,21 @@ void test_nghttp2_hd_deflate(void) nghttp2_nv_array_del(resnva); nghttp2_hd_end_headers(&inflater); + /* Fifth headers includes empty value */ + blocklen = nghttp2_hd_deflate_hd(&deflater, &buf, &buflen, nv_offset, nva5, + sizeof(nva5)/sizeof(nghttp2_nv)); + CU_ASSERT(blocklen > 0); + nghttp2_hd_end_headers(&deflater); + + CU_ASSERT(2 == nghttp2_hd_inflate_hd(&inflater, &resnva, buf + nv_offset, + blocklen)); + + assert_nv_equal(nva5, resnva, 2); + + nghttp2_nv_array_del(resnva); + nghttp2_hd_end_headers(&inflater); + + /* Cleanup */ free(buf); nghttp2_hd_inflate_free(&inflater); nghttp2_hd_deflate_free(&deflater);