From 6f70a53da6738587628faf2f9ca3419c128dfde6 Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Mon, 15 Dec 2014 23:04:45 +0900 Subject: [PATCH] src: http2::add_header: strip white spaces in value --- src/http2.cc | 9 +++++++++ src/http2.h | 2 +- src/http2_test.cc | 30 ++++++++++++++++++++++++++++++ 3 files changed, 40 insertions(+), 1 deletion(-) diff --git a/src/http2.cc b/src/http2.cc index f781c24a..fc4faed8 100644 --- a/src/http2.cc +++ b/src/http2.cc @@ -316,6 +316,15 @@ Headers::value_type to_header(const uint8_t *name, size_t namelen, void add_header(Headers &nva, const uint8_t *name, size_t namelen, const uint8_t *value, size_t valuelen, bool no_index) { + if (valuelen > 0) { + size_t i, j; + for (i = 0; i < valuelen && (value[i] == ' ' || value[i] == '\t'); ++i) + ; + for (j = valuelen - 1; j > i && (value[j] == ' ' || value[j] == '\t'); --j) + ; + value += i; + valuelen -= i + (valuelen - j - 1); + } nva.push_back(to_header(name, namelen, value, valuelen, no_index)); } diff --git a/src/http2.h b/src/http2.h index 7e10bd64..d4b500a0 100644 --- a/src/http2.h +++ b/src/http2.h @@ -111,7 +111,7 @@ Headers::value_type to_header(const uint8_t *name, size_t namelen, // Add name/value pairs to |nva|. If |no_index| is true, this // name/value pair won't be indexed when it is forwarded to the next -// hop. +// hop. This function strips white spaces around |value|. void add_header(Headers &nva, const uint8_t *name, size_t namelen, const uint8_t *value, size_t valuelen, bool no_index); diff --git a/src/http2_test.cc b/src/http2_test.cc index e4fb24c6..aa78209e 100644 --- a/src/http2_test.cc +++ b/src/http2_test.cc @@ -68,6 +68,36 @@ void test_http2_add_header(void) { true); CU_ASSERT(Headers::value_type("alpha", "") == nva[0]); CU_ASSERT(nva[0].no_index); + + nva.clear(); + + http2::add_header(nva, (const uint8_t *)"a", 1, (const uint8_t *)" b", 2, + false); + CU_ASSERT(Headers::value_type("a", "b") == nva[0]); + + nva.clear(); + + http2::add_header(nva, (const uint8_t *)"a", 1, (const uint8_t *)"b ", 2, + false); + CU_ASSERT(Headers::value_type("a", "b") == nva[0]); + + nva.clear(); + + http2::add_header(nva, (const uint8_t *)"a", 1, (const uint8_t *)" b ", 5, + false); + CU_ASSERT(Headers::value_type("a", "b") == nva[0]); + + nva.clear(); + + http2::add_header(nva, (const uint8_t *)"a", 1, (const uint8_t *)" bravo ", + 9, false); + CU_ASSERT(Headers::value_type("a", "bravo") == nva[0]); + + nva.clear(); + + http2::add_header(nva, (const uint8_t *)"a", 1, (const uint8_t *)" ", 4, + false); + CU_ASSERT(Headers::value_type("a", "") == nva[0]); } void test_http2_check_http2_headers(void) {