diff options
author | trofimenkov <trofimenkov@yandex-team.ru> | 2022-02-10 16:49:30 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:49:30 +0300 |
commit | 30cebc2cfa79af3b577760a113e203a79450e6b6 (patch) | |
tree | 49327bf3c28fab534b04b312a39179e70f7c2763 /library/cpp/http | |
parent | a2d2743094c8d255cda4011b317235874db4d01c (diff) | |
download | ydb-30cebc2cfa79af3b577760a113e203a79450e6b6.tar.gz |
Restoring authorship annotation for <trofimenkov@yandex-team.ru>. Commit 1 of 2.
Diffstat (limited to 'library/cpp/http')
-rw-r--r-- | library/cpp/http/io/headers.h | 6 | ||||
-rw-r--r-- | library/cpp/http/io/headers_ut.cpp | 256 | ||||
-rw-r--r-- | library/cpp/http/misc/httpreqdata.cpp | 8 | ||||
-rw-r--r-- | library/cpp/http/misc/httpreqdata.h | 10 | ||||
-rw-r--r-- | library/cpp/http/misc/httpreqdata_ut.cpp | 62 | ||||
-rw-r--r-- | library/cpp/http/server/http.cpp | 38 | ||||
-rw-r--r-- | library/cpp/http/server/options.h | 24 |
7 files changed, 202 insertions, 202 deletions
diff --git a/library/cpp/http/io/headers.h b/library/cpp/http/io/headers.h index a71793d1c6..5df294fa71 100644 --- a/library/cpp/http/io/headers.h +++ b/library/cpp/http/io/headers.h @@ -84,11 +84,11 @@ public: /// Добавляет заголовок в контейнер. void AddHeader(THttpInputHeader header); - template <typename ValueType> + template <typename ValueType> void AddHeader(TString name, const ValueType& value) { AddHeader(THttpInputHeader(std::move(name), ToString(value))); - } - + } + /// Добавляет заголовок в контейнер, если тот не содержит заголовка /// c таким же параметром. В противном случае, заменяет существующий /// заголовок на новый. diff --git a/library/cpp/http/io/headers_ut.cpp b/library/cpp/http/io/headers_ut.cpp index 1d23ef8fdc..2f6c19b812 100644 --- a/library/cpp/http/io/headers_ut.cpp +++ b/library/cpp/http/io/headers_ut.cpp @@ -1,78 +1,78 @@ -#include <util/generic/set.h> +#include <util/generic/set.h> #include <util/generic/string.h> -#include <util/generic/strbuf.h> +#include <util/generic/strbuf.h> #include <utility> - + #include <library/cpp/http/io/headers.h> #include <library/cpp/testing/unittest/registar.h> - -namespace { - class THeadersExistence { - public: + +namespace { + class THeadersExistence { + public: THeadersExistence() = default; - - THeadersExistence(const THttpHeaders& headers) { - for (THttpHeaders::TConstIterator it = headers.Begin(); + + THeadersExistence(const THttpHeaders& headers) { + for (THttpHeaders::TConstIterator it = headers.Begin(); it != headers.End(); ++it) { - Add(it->Name(), it->Value()); - } - } - - public: + Add(it->Name(), it->Value()); + } + } + + public: void Add(TStringBuf name, TStringBuf value) { Impl.emplace(TString(name), TString(value)); - } - + } + bool operator==(const THeadersExistence& rhs) const { - return Impl == rhs.Impl; - } - - private: + return Impl == rhs.Impl; + } + + private: typedef TMultiSet<std::pair<TString, TString>> TImpl; - TImpl Impl; - }; -} - + TImpl Impl; + }; +} + bool operator==(const THeadersExistence& lhs, const THttpHeaders& rhs) { - return lhs == THeadersExistence(rhs); -} - + return lhs == THeadersExistence(rhs); +} + bool operator==(const THttpHeaders& lhs, const THeadersExistence& rhs) { - return THeadersExistence(lhs) == rhs; -} - -class THttpHeadersTest: public TTestBase { - UNIT_TEST_SUITE(THttpHeadersTest); - UNIT_TEST(TestAddOperation1Arg); - UNIT_TEST(TestAddOperation2Args); - UNIT_TEST(TestAddOrReplaceOperation1Arg); - UNIT_TEST(TestAddOrReplaceOperation2Args); - UNIT_TEST(TestAddHeaderTemplateness); + return THeadersExistence(lhs) == rhs; +} + +class THttpHeadersTest: public TTestBase { + UNIT_TEST_SUITE(THttpHeadersTest); + UNIT_TEST(TestAddOperation1Arg); + UNIT_TEST(TestAddOperation2Args); + UNIT_TEST(TestAddOrReplaceOperation1Arg); + UNIT_TEST(TestAddOrReplaceOperation2Args); + UNIT_TEST(TestAddHeaderTemplateness); UNIT_TEST(TestFindHeader); - UNIT_TEST_SUITE_END(); - -private: + UNIT_TEST_SUITE_END(); + +private: typedef void (*TAddHeaderFunction)(THttpHeaders&, TStringBuf name, TStringBuf value); typedef void (*TAddOrReplaceHeaderFunction)(THttpHeaders&, TStringBuf name, TStringBuf value); - -public: - void TestAddOperation1Arg(); - void TestAddOperation2Args(); - void TestAddOrReplaceOperation1Arg(); - void TestAddOrReplaceOperation2Args(); - void TestAddHeaderTemplateness(); + +public: + void TestAddOperation1Arg(); + void TestAddOperation2Args(); + void TestAddOrReplaceOperation1Arg(); + void TestAddOrReplaceOperation2Args(); + void TestAddHeaderTemplateness(); void TestFindHeader(); - -private: + +private: static void AddHeaderImpl1Arg(THttpHeaders& headers, TStringBuf name, TStringBuf value) { headers.AddHeader(THttpInputHeader(TString(name), TString(value))); - } - + } + static void AddHeaderImpl2Args(THttpHeaders& headers, TStringBuf name, TStringBuf value) { headers.AddHeader(TString(name), TString(value)); - } - + } + static void AddOrReplaceHeaderImpl1Arg(THttpHeaders& headers, TStringBuf name, TStringBuf value) { headers.AddOrReplaceHeader(THttpInputHeader(TString(name), TString(value))); } @@ -81,91 +81,91 @@ private: headers.AddOrReplaceHeader(TString(name), TString(value)); } - void DoTestAddOperation(TAddHeaderFunction); + void DoTestAddOperation(TAddHeaderFunction); void DoTestAddOrReplaceOperation(TAddHeaderFunction, TAddOrReplaceHeaderFunction); -}; - -UNIT_TEST_SUITE_REGISTRATION(THttpHeadersTest); - -void THttpHeadersTest::TestAddOperation1Arg() { - DoTestAddOperation(AddHeaderImpl1Arg); -} -void THttpHeadersTest::TestAddOperation2Args() { - DoTestAddOperation(AddHeaderImpl2Args); -} - -void THttpHeadersTest::TestAddOrReplaceOperation1Arg() { +}; + +UNIT_TEST_SUITE_REGISTRATION(THttpHeadersTest); + +void THttpHeadersTest::TestAddOperation1Arg() { + DoTestAddOperation(AddHeaderImpl1Arg); +} +void THttpHeadersTest::TestAddOperation2Args() { + DoTestAddOperation(AddHeaderImpl2Args); +} + +void THttpHeadersTest::TestAddOrReplaceOperation1Arg() { DoTestAddOrReplaceOperation(AddHeaderImpl1Arg, AddOrReplaceHeaderImpl1Arg); -} -void THttpHeadersTest::TestAddOrReplaceOperation2Args() { +} +void THttpHeadersTest::TestAddOrReplaceOperation2Args() { DoTestAddOrReplaceOperation(AddHeaderImpl2Args, AddOrReplaceHeaderImpl2Args); -} - -void THttpHeadersTest::DoTestAddOperation(TAddHeaderFunction addHeader) { - THttpHeaders h1; - - addHeader(h1, "h1", "v1"); - addHeader(h1, "h2", "v1"); - - addHeader(h1, "h3", "v1"); - addHeader(h1, "h3", "v2"); - addHeader(h1, "h3", "v2"); - - THeadersExistence h2; - - h2.Add("h1", "v1"); - h2.Add("h2", "v1"); - - h2.Add("h3", "v1"); - h2.Add("h3", "v2"); - h2.Add("h3", "v2"); - - UNIT_ASSERT(h2 == h1); -} - -// Sorry, but AddOrReplaceHeader replaces only first occurence +} + +void THttpHeadersTest::DoTestAddOperation(TAddHeaderFunction addHeader) { + THttpHeaders h1; + + addHeader(h1, "h1", "v1"); + addHeader(h1, "h2", "v1"); + + addHeader(h1, "h3", "v1"); + addHeader(h1, "h3", "v2"); + addHeader(h1, "h3", "v2"); + + THeadersExistence h2; + + h2.Add("h1", "v1"); + h2.Add("h2", "v1"); + + h2.Add("h3", "v1"); + h2.Add("h3", "v2"); + h2.Add("h3", "v2"); + + UNIT_ASSERT(h2 == h1); +} + +// Sorry, but AddOrReplaceHeader replaces only first occurence void THttpHeadersTest::DoTestAddOrReplaceOperation(TAddHeaderFunction addHeader, TAddOrReplaceHeaderFunction addOrReplaceHeader) { - THttpHeaders h1; - - addHeader(h1, "h1", "v1"); - + THttpHeaders h1; + + addHeader(h1, "h1", "v1"); + addOrReplaceHeader(h1, "h2", "v1"); addOrReplaceHeader(h1, "h2", "v2"); addOrReplaceHeader(h1, "h2", "v3"); - addHeader(h1, "h2", "v4"); - - addHeader(h1, "h3", "v1"); - addHeader(h1, "h3", "v2"); + addHeader(h1, "h2", "v4"); + + addHeader(h1, "h3", "v1"); + addHeader(h1, "h3", "v2"); addOrReplaceHeader(h1, "h3", "v3"); - - THeadersExistence h2; - - h2.Add("h1", "v1"); - - h2.Add("h2", "v3"); - h2.Add("h2", "v4"); - - h2.Add("h3", "v2"); - h2.Add("h3", "v3"); - - UNIT_ASSERT(h2 == h1); -} - -void THttpHeadersTest::TestAddHeaderTemplateness() { - THttpHeaders h1; - h1.AddHeader("h1", "v1"); + + THeadersExistence h2; + + h2.Add("h1", "v1"); + + h2.Add("h2", "v3"); + h2.Add("h2", "v4"); + + h2.Add("h3", "v2"); + h2.Add("h3", "v3"); + + UNIT_ASSERT(h2 == h1); +} + +void THttpHeadersTest::TestAddHeaderTemplateness() { + THttpHeaders h1; + h1.AddHeader("h1", "v1"); h1.AddHeader("h2", TString("v2")); h1.AddHeader("h3", TStringBuf("v3")); h1.AddHeader("h4", TStringBuf("v4")); - - THeadersExistence h2; - h2.Add("h1", "v1"); - h2.Add("h2", "v2"); - h2.Add("h3", "v3"); - h2.Add("h4", "v4"); - - UNIT_ASSERT(h1 == h2); -} + + THeadersExistence h2; + h2.Add("h1", "v1"); + h2.Add("h2", "v2"); + h2.Add("h3", "v3"); + h2.Add("h4", "v4"); + + UNIT_ASSERT(h1 == h2); +} void THttpHeadersTest::TestFindHeader() { THttpHeaders sut; diff --git a/library/cpp/http/misc/httpreqdata.cpp b/library/cpp/http/misc/httpreqdata.cpp index f6951f68cd..4384caf0cf 100644 --- a/library/cpp/http/misc/httpreqdata.cpp +++ b/library/cpp/http/misc/httpreqdata.cpp @@ -21,7 +21,7 @@ TBaseServerRequestData::TBaseServerRequestData(const char* qs, SOCKET s) , Path(nullptr) , Search((char*)qs) , SearchLength(qs ? strlen(qs) : 0) - , OrigSearch(Search, SearchLength) + , OrigSearch(Search, SearchLength) , Socket(s) , BeginTime(MicroSeconds()) { @@ -110,7 +110,7 @@ const char* TBaseServerRequestData::Environment(const char* key) const { void TBaseServerRequestData::Clear() { HeadersIn_.clear(); Addr = Path = Search = nullptr; - OrigSearch = {}; + OrigSearch = {}; SearchLength = 0; Host.clear(); Port.clear(); @@ -174,8 +174,8 @@ bool TBaseServerRequestData::Parse(const char* origReq) { } else { SearchLength = 0; } - OrigSearch = {Search, SearchLength}; - + OrigSearch = {Search, SearchLength}; + return true; } diff --git a/library/cpp/http/misc/httpreqdata.h b/library/cpp/http/misc/httpreqdata.h index 16e59c4d78..09389fa4af 100644 --- a/library/cpp/http/misc/httpreqdata.h +++ b/library/cpp/http/misc/httpreqdata.h @@ -51,10 +51,10 @@ public: return TStringBuf(Search, SearchLength); } - TStringBuf OrigQueryStringBuf() const { - return OrigSearch; - } - + TStringBuf OrigQueryStringBuf() const { + return OrigSearch; + } + void AppendQueryString(const char* str, size_t length); const char* RemoteAddr() const; void SetRemoteAddr(TStringBuf addr); @@ -94,7 +94,7 @@ private: char* Path; char* Search; size_t SearchLength; // length of Search - TStringBuf OrigSearch; + TStringBuf OrigSearch; THttpHeadersContainer HeadersIn_; mutable char AddrData[INET6_ADDRSTRLEN]; SOCKET Socket; diff --git a/library/cpp/http/misc/httpreqdata_ut.cpp b/library/cpp/http/misc/httpreqdata_ut.cpp index e7f16ef27c..a0ae26ea84 100644 --- a/library/cpp/http/misc/httpreqdata_ut.cpp +++ b/library/cpp/http/misc/httpreqdata_ut.cpp @@ -51,9 +51,9 @@ Y_UNIT_TEST_SUITE(TRequestServerDataTest) { // This should work UNIT_ASSERT(rd.Parse(" /yandsearch?>a=fake&haha=da HTTP 1.1 OK")); - UNIT_ASSERT_STRINGS_EQUAL(rd.QueryStringBuf(), ">a=fake&haha=da"); - UNIT_ASSERT_STRINGS_EQUAL(rd.QueryStringBuf(), rd.OrigQueryStringBuf()); - + UNIT_ASSERT_STRINGS_EQUAL(rd.QueryStringBuf(), ">a=fake&haha=da"); + UNIT_ASSERT_STRINGS_EQUAL(rd.QueryStringBuf(), rd.OrigQueryStringBuf()); + rd.Scan(); UNIT_ASSERT(rd.CgiParam.Has("gta", "fake")); UNIT_ASSERT(rd.CgiParam.Has("haha", "da")); @@ -63,27 +63,27 @@ Y_UNIT_TEST_SUITE(TRequestServerDataTest) { } Y_UNIT_TEST(Ctor) { - const TString qs("gta=fake&haha=da"); - TServerRequestData rd(qs.c_str()); - - UNIT_ASSERT_STRINGS_EQUAL(rd.QueryStringBuf(), qs); - UNIT_ASSERT_STRINGS_EQUAL(rd.OrigQueryStringBuf(), qs); - + const TString qs("gta=fake&haha=da"); + TServerRequestData rd(qs.c_str()); + + UNIT_ASSERT_STRINGS_EQUAL(rd.QueryStringBuf(), qs); + UNIT_ASSERT_STRINGS_EQUAL(rd.OrigQueryStringBuf(), qs); + UNIT_ASSERT(rd.CgiParam.Has("gta")); UNIT_ASSERT(rd.CgiParam.Has("haha")); UNIT_ASSERT(!rd.CgiParam.Has("no-param")); } Y_UNIT_TEST(HashCut) { - const TString qs(">a=fake&haha=da"); - const TString header = " /yandsearch?" + qs + "#&uberParam=yes&q=? HTTP 1.1 OK"; - + const TString qs(">a=fake&haha=da"); + const TString header = " /yandsearch?" + qs + "#&uberParam=yes&q=? HTTP 1.1 OK"; + TServerRequestData rd; - rd.Parse(header.c_str()); - - UNIT_ASSERT_STRINGS_EQUAL(rd.QueryStringBuf(), qs); - UNIT_ASSERT_STRINGS_EQUAL(rd.OrigQueryStringBuf(), qs); + rd.Parse(header.c_str()); + UNIT_ASSERT_STRINGS_EQUAL(rd.QueryStringBuf(), qs); + UNIT_ASSERT_STRINGS_EQUAL(rd.OrigQueryStringBuf(), qs); + rd.Scan(); UNIT_ASSERT(rd.CgiParam.Has("gta")); UNIT_ASSERT(rd.CgiParam.Has("haha")); @@ -94,9 +94,9 @@ Y_UNIT_TEST_SUITE(TRequestServerDataTest) { TServerRequestData rd; rd.Parse(" /y#ndsearch?>a=fake&haha=da&uberParam=yes&q=? HTTP 1.1 OK"); - UNIT_ASSERT_STRINGS_EQUAL(rd.QueryStringBuf(), ""); - UNIT_ASSERT_STRINGS_EQUAL(rd.OrigQueryStringBuf(), ""); - + UNIT_ASSERT_STRINGS_EQUAL(rd.QueryStringBuf(), ""); + UNIT_ASSERT_STRINGS_EQUAL(rd.OrigQueryStringBuf(), ""); + rd.Scan(); UNIT_ASSERT(rd.CgiParam.empty()); } @@ -105,29 +105,29 @@ Y_UNIT_TEST_SUITE(TRequestServerDataTest) { TServerRequestData rd; rd.Parse(" /yandsearch?#"); - UNIT_ASSERT_STRINGS_EQUAL(rd.QueryStringBuf(), ""); - UNIT_ASSERT_STRINGS_EQUAL(rd.OrigQueryStringBuf(), ""); - + UNIT_ASSERT_STRINGS_EQUAL(rd.QueryStringBuf(), ""); + UNIT_ASSERT_STRINGS_EQUAL(rd.OrigQueryStringBuf(), ""); + rd.Scan(); UNIT_ASSERT(rd.CgiParam.empty()); } Y_UNIT_TEST(AppendQueryString) { - const TString qs("gta=fake&haha=da"); - TServerRequestData rd(qs.c_str()); + const TString qs("gta=fake&haha=da"); + TServerRequestData rd(qs.c_str()); UNIT_ASSERT(rd.CgiParam.Has("gta", "fake")); UNIT_ASSERT(rd.CgiParam.Has("haha", "da")); - UNIT_ASSERT_STRINGS_EQUAL(rd.QueryStringBuf(), qs); - UNIT_ASSERT_STRINGS_EQUAL(rd.QueryStringBuf(), rd.OrigQueryStringBuf()); - + UNIT_ASSERT_STRINGS_EQUAL(rd.QueryStringBuf(), qs); + UNIT_ASSERT_STRINGS_EQUAL(rd.QueryStringBuf(), rd.OrigQueryStringBuf()); + constexpr TStringBuf appendix = "gta=true>a=new"; rd.AppendQueryString(appendix.data(), appendix.size()); - - UNIT_ASSERT_STRINGS_EQUAL(rd.QueryStringBuf(), qs + '&' + appendix); - UNIT_ASSERT_STRINGS_EQUAL(rd.OrigQueryStringBuf(), qs); - + + UNIT_ASSERT_STRINGS_EQUAL(rd.QueryStringBuf(), qs + '&' + appendix); + UNIT_ASSERT_STRINGS_EQUAL(rd.OrigQueryStringBuf(), qs); + rd.Scan(); UNIT_ASSERT(rd.CgiParam.Has("gta", "true")); diff --git a/library/cpp/http/server/http.cpp b/library/cpp/http/server/http.cpp index 128583bdd7..6683052c8e 100644 --- a/library/cpp/http/server/http.cpp +++ b/library/cpp/http/server/http.cpp @@ -321,8 +321,8 @@ public: }; void ListenSocket() { - TThread::SetCurrentThreadName(Options_.ListenThreadName.c_str()); - + TThread::SetCurrentThreadName(Options_.ListenThreadName.c_str()); + ErrorCode = 0; TIntrusiveListWithAutoDelete<TListenSocket, TDelete> Reqs; @@ -410,8 +410,8 @@ public: : TImpl( parent, cb, - MakeThreadPool<TSimpleThreadPool>(factory, options.UseElasticQueues, cb, options.RequestsThreadName), - MakeThreadPool<TThreadPool>(factory, options.UseElasticQueues, nullptr, options.FailRequestsThreadName), + MakeThreadPool<TSimpleThreadPool>(factory, options.UseElasticQueues, cb, options.RequestsThreadName), + MakeThreadPool<TThreadPool>(factory, options.UseElasticQueues, nullptr, options.FailRequestsThreadName), options) { } @@ -461,17 +461,17 @@ public: private: template <class TThreadPool_> - static THolder<IThreadPool> MakeThreadPool(IThreadFactory* factory, bool elastic, ICallBack* callback = nullptr, const TString& threadName = {}) { + static THolder<IThreadPool> MakeThreadPool(IThreadFactory* factory, bool elastic, ICallBack* callback = nullptr, const TString& threadName = {}) { if (!factory) { factory = SystemThreadFactory(); } THolder<IThreadPool> pool; - const auto params = IThreadPool::TParams().SetFactory(factory).SetThreadName(threadName); + const auto params = IThreadPool::TParams().SetFactory(factory).SetThreadName(threadName); if (callback) { - pool = MakeHolder<TThreadPoolBinder<TThreadPool_, THttpServer::ICallBack>>(callback, params); + pool = MakeHolder<TThreadPoolBinder<TThreadPool_, THttpServer::ICallBack>>(callback, params); } else { - pool = MakeHolder<TThreadPool_>(params); + pool = MakeHolder<TThreadPool_>(params); } if (elastic) { @@ -631,20 +631,20 @@ bool TClientRequest::Reply(void* /*ThreadSpecificResource*/) { bool TClientRequest::IsLocal() const { return HasLocalAddress(Socket()); -} - -bool TClientRequest::CheckLoopback() { - bool isLocal = false; - - try { - isLocal = IsLocal(); - } catch (const yexception& e) { +} + +bool TClientRequest::CheckLoopback() { + bool isLocal = false; + + try { + isLocal = IsLocal(); + } catch (const yexception& e) { Output() << "HTTP/1.0 500 Oops\r\n\r\n" << e.what() << "\r\n"; return false; - } - - if (!isLocal) { + } + + if (!isLocal) { Output() << "HTTP/1.0 403 Permission denied\r\n" "Content-Type: text/html; charset=windows-1251\r\n" "Connection: close\r\n" diff --git a/library/cpp/http/server/options.h b/library/cpp/http/server/options.h index 38eda0e5e7..8510261b72 100644 --- a/library/cpp/http/server/options.h +++ b/library/cpp/http/server/options.h @@ -131,14 +131,14 @@ public: return *this; } - inline THttpServerOptions& SetThreadsName(const TString& listenThreadName, const TString& requestsThreadName, const TString& failRequestsThreadName) noexcept { - ListenThreadName = listenThreadName; - RequestsThreadName = requestsThreadName; - FailRequestsThreadName = failRequestsThreadName; - - return *this; - } - + inline THttpServerOptions& SetThreadsName(const TString& listenThreadName, const TString& requestsThreadName, const TString& failRequestsThreadName) noexcept { + ListenThreadName = listenThreadName; + RequestsThreadName = requestsThreadName; + FailRequestsThreadName = failRequestsThreadName; + + return *this; + } + struct TAddr { TString Addr; ui16 Port; @@ -169,8 +169,8 @@ public: TDuration PollTimeout; // timeout of TSocketPoller::WaitT call TDuration ExpirationTimeout; // drop inactive connections after ExpirationTimeout (should be > 0) - - TString ListenThreadName = "HttpListen"; - TString RequestsThreadName = "HttpServer"; - TString FailRequestsThreadName = "HttpServer"; + + TString ListenThreadName = "HttpListen"; + TString RequestsThreadName = "HttpServer"; + TString FailRequestsThreadName = "HttpServer"; }; |