diff options
author | vadim-xd <vadim-xd@yandex-team.com> | 2024-04-16 09:38:35 +0300 |
---|---|---|
committer | vadim-xd <vadim-xd@yandex-team.com> | 2024-04-16 09:47:02 +0300 |
commit | ea11c4b61ef4e491c701847533ff93d3de81f127 (patch) | |
tree | 379f3f5dc74a110c36c021a8ebee828e725e2f8b /library | |
parent | c56359b9090f00a5a3c40c739daf6848cfc40689 (diff) | |
download | ydb-ea11c4b61ef4e491c701847533ff93d3de81f127.tar.gz |
Add THttpHeaders constructor from TArrayRef
351519c01d45a22beceb491029a8f516619673a0
Diffstat (limited to 'library')
-rw-r--r-- | library/cpp/http/io/compression.h | 1 | ||||
-rw-r--r-- | library/cpp/http/io/headers.cpp | 7 | ||||
-rw-r--r-- | library/cpp/http/io/headers.h | 9 | ||||
-rw-r--r-- | library/cpp/http/io/headers_ut.cpp | 22 | ||||
-rw-r--r-- | library/cpp/http/server/response.cpp | 1 |
5 files changed, 38 insertions, 2 deletions
diff --git a/library/cpp/http/io/compression.h b/library/cpp/http/io/compression.h index f16c4a18eb..038ac47629 100644 --- a/library/cpp/http/io/compression.h +++ b/library/cpp/http/io/compression.h @@ -4,6 +4,7 @@ #include <util/generic/deque.h> #include <util/generic/hash.h> +#include <util/generic/vector.h> class TCompressionCodecFactory { public: diff --git a/library/cpp/http/io/headers.cpp b/library/cpp/http/io/headers.cpp index f2baf64021..3f3a5a2d07 100644 --- a/library/cpp/http/io/headers.cpp +++ b/library/cpp/http/io/headers.cpp @@ -66,6 +66,13 @@ THttpHeaders::THttpHeaders(IInputStream* stream) { } } +THttpHeaders::THttpHeaders(TArrayRef<const THttpInputHeader> headers) { + for (const auto& header : headers) { + AddHeader(header); + } +} + + bool THttpHeaders::HasHeader(const TStringBuf header) const { return FindHeader(header); } diff --git a/library/cpp/http/io/headers.h b/library/cpp/http/io/headers.h index cfb4a9c054..3ae40683e2 100644 --- a/library/cpp/http/io/headers.h +++ b/library/cpp/http/io/headers.h @@ -1,9 +1,10 @@ #pragma once +#include <util/generic/array_ref.h> +#include <util/generic/deque.h> #include <util/generic/string.h> #include <util/generic/strbuf.h> -#include <util/generic/deque.h> -#include <util/generic/vector.h> +#include <util/generic/vector.h> // XXX unused - remove after fixing transitive includes. #include <util/string/cast.h> class IInputStream; @@ -65,6 +66,10 @@ public: /// Добавляет каждую строку из потока в контейнер, считая ее правильным заголовком. THttpHeaders(IInputStream* stream); + /// Создаёт контейнер из initializer-list'а или массива/вектора хедеров. + /// Пример: `THttpHeaders headers({{"Host", "example.com"}});` + THttpHeaders(TArrayRef<const THttpInputHeader> headers); + /// Стандартный итератор. inline TConstIterator Begin() const noexcept { return Headers_.begin(); diff --git a/library/cpp/http/io/headers_ut.cpp b/library/cpp/http/io/headers_ut.cpp index 1d23ef8fdc..6205efb154 100644 --- a/library/cpp/http/io/headers_ut.cpp +++ b/library/cpp/http/io/headers_ut.cpp @@ -44,6 +44,7 @@ bool operator==(const THttpHeaders& lhs, const THeadersExistence& rhs) { class THttpHeadersTest: public TTestBase { UNIT_TEST_SUITE(THttpHeadersTest); + UNIT_TEST(TestConstructorFromArrayRef); UNIT_TEST(TestAddOperation1Arg); UNIT_TEST(TestAddOperation2Args); UNIT_TEST(TestAddOrReplaceOperation1Arg); @@ -57,6 +58,7 @@ private: typedef void (*TAddOrReplaceHeaderFunction)(THttpHeaders&, TStringBuf name, TStringBuf value); public: + void TestConstructorFromArrayRef(); void TestAddOperation1Arg(); void TestAddOperation2Args(); void TestAddOrReplaceOperation1Arg(); @@ -87,6 +89,26 @@ private: UNIT_TEST_SUITE_REGISTRATION(THttpHeadersTest); +void THttpHeadersTest::TestConstructorFromArrayRef() { + THeadersExistence expected; + expected.Add("h1", "v1"); + expected.Add("h2", "v2"); + + // Construct from vector + TVector<THttpInputHeader> headerVec{ + {"h1", "v1"}, + {"h2", "v2"} + }; + THttpHeaders h1(headerVec); + UNIT_ASSERT(expected == h1); + + // Construct from initializer list + THttpHeaders h2({ + {"h1", "v1"}, + {"h2", "v2"} + }); + UNIT_ASSERT(expected == h2); +} void THttpHeadersTest::TestAddOperation1Arg() { DoTestAddOperation(AddHeaderImpl1Arg); } diff --git a/library/cpp/http/server/response.cpp b/library/cpp/http/server/response.cpp index 52d64c91ce..ff4d3e07f3 100644 --- a/library/cpp/http/server/response.cpp +++ b/library/cpp/http/server/response.cpp @@ -1,5 +1,6 @@ #include "response.h" +#include <util/generic/vector.h> #include <util/stream/output.h> #include <util/stream/mem.h> #include <util/string/cast.h> |