diff options
author | kulikov <kulikov@yandex-team.ru> | 2022-02-10 16:49:34 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:49:34 +0300 |
commit | c707901605d7b7c6cba0998cd52e1ae619c97762 (patch) | |
tree | 5d5cb817648f650d76cf1076100726fd9b8448e8 /library/cpp/http | |
parent | 65e5266709e7ff94b14ae128309e229de714b0df (diff) | |
download | ydb-c707901605d7b7c6cba0998cd52e1ae619c97762.tar.gz |
Restoring authorship annotation for <kulikov@yandex-team.ru>. Commit 2 of 2.
Diffstat (limited to 'library/cpp/http')
-rw-r--r-- | library/cpp/http/io/compression.h | 44 | ||||
-rw-r--r-- | library/cpp/http/io/compression_ut.cpp | 30 | ||||
-rw-r--r-- | library/cpp/http/io/stream.cpp | 14 | ||||
-rw-r--r-- | library/cpp/http/io/stream_ut.cpp | 98 | ||||
-rw-r--r-- | library/cpp/http/server/conn.cpp | 16 | ||||
-rw-r--r-- | library/cpp/http/server/http.cpp | 36 | ||||
-rw-r--r-- | library/cpp/http/server/http_ut.cpp | 90 | ||||
-rw-r--r-- | library/cpp/http/server/options.h | 18 | ||||
-rw-r--r-- | library/cpp/http/server/response.h | 8 | ||||
-rw-r--r-- | library/cpp/http/server/response_ut.cpp | 38 |
10 files changed, 196 insertions, 196 deletions
diff --git a/library/cpp/http/io/compression.h b/library/cpp/http/io/compression.h index 30eccdaca5..f16c4a18eb 100644 --- a/library/cpp/http/io/compression.h +++ b/library/cpp/http/io/compression.h @@ -48,25 +48,25 @@ private: THashMap<TStringBuf, TCodec> Codecs_; TVector<TStringBuf> BestCodecs_; }; - -namespace NHttp { - template <typename F> - TString ChooseBestCompressionScheme(F accepted, TArrayRef<const TStringBuf> available) { - if (available.empty()) { - return "identity"; - } - - if (accepted("*")) { - return TString(available[0]); - } - - for (const auto& coding : available) { - TString s(coding); - if (accepted(s)) { - return s; - } - } - - return "identity"; - } -} + +namespace NHttp { + template <typename F> + TString ChooseBestCompressionScheme(F accepted, TArrayRef<const TStringBuf> available) { + if (available.empty()) { + return "identity"; + } + + if (accepted("*")) { + return TString(available[0]); + } + + for (const auto& coding : available) { + TString s(coding); + if (accepted(s)) { + return s; + } + } + + return "identity"; + } +} diff --git a/library/cpp/http/io/compression_ut.cpp b/library/cpp/http/io/compression_ut.cpp index d8a2d11a08..2f3d131f8c 100644 --- a/library/cpp/http/io/compression_ut.cpp +++ b/library/cpp/http/io/compression_ut.cpp @@ -5,7 +5,7 @@ #include <library/cpp/testing/unittest/tests_data.h> #include <util/stream/zlib.h> -#include <util/generic/hash_set.h> +#include <util/generic/hash_set.h> Y_UNIT_TEST_SUITE(THttpCompressionTest) { static const TString DATA = "I'm a teapot"; @@ -43,18 +43,18 @@ Y_UNIT_TEST_SUITE(THttpCompressionTest) { auto decodedStream = (*decoder)(&buffer); UNIT_ASSERT_EQUAL(decodedStream->ReadAll(), DATA); } - - Y_UNIT_TEST(TestChooseBestCompressionScheme) { - THashSet<TString> accepted; - - auto checkAccepted = [&accepted](const TString& v) { - return accepted.contains(v); - }; - - UNIT_ASSERT_VALUES_EQUAL("identity", NHttp::ChooseBestCompressionScheme(checkAccepted, {"gzip", "deflate"})); - accepted.insert("deflate"); - UNIT_ASSERT_VALUES_EQUAL("deflate", NHttp::ChooseBestCompressionScheme(checkAccepted, {"gzip", "deflate"})); - accepted.insert("*"); - UNIT_ASSERT_VALUES_EQUAL("gzip", NHttp::ChooseBestCompressionScheme(checkAccepted, {"gzip", "deflate"})); - } + + Y_UNIT_TEST(TestChooseBestCompressionScheme) { + THashSet<TString> accepted; + + auto checkAccepted = [&accepted](const TString& v) { + return accepted.contains(v); + }; + + UNIT_ASSERT_VALUES_EQUAL("identity", NHttp::ChooseBestCompressionScheme(checkAccepted, {"gzip", "deflate"})); + accepted.insert("deflate"); + UNIT_ASSERT_VALUES_EQUAL("deflate", NHttp::ChooseBestCompressionScheme(checkAccepted, {"gzip", "deflate"})); + accepted.insert("*"); + UNIT_ASSERT_VALUES_EQUAL("gzip", NHttp::ChooseBestCompressionScheme(checkAccepted, {"gzip", "deflate"})); + } } // THttpCompressionTest suite diff --git a/library/cpp/http/io/stream.cpp b/library/cpp/http/io/stream.cpp index 728d1a89c1..6689be684f 100644 --- a/library/cpp/http/io/stream.cpp +++ b/library/cpp/http/io/stream.cpp @@ -286,7 +286,7 @@ private: TParsedHeaders p; size_t pos = FirstLine_.rfind(' '); - // In HTTP/1.1 Keep-Alive is turned on by default + // In HTTP/1.1 Keep-Alive is turned on by default if (pos != TString::npos && strcmp(FirstLine_.c_str() + pos + 1, "HTTP/1.1") == 0) { p.KeepAlive = true; //request } else if (strnicmp(FirstLine_.data(), "HTTP/1.1", 8) == 0) { @@ -428,12 +428,12 @@ bool THttpInput::AcceptEncoding(const TString& coding) const { } TString THttpInput::BestCompressionScheme(TArrayRef<const TStringBuf> codings) const { - return NHttp::ChooseBestCompressionScheme( - [this](const TString& coding) { - return AcceptEncoding(coding); - }, - codings - ); + return NHttp::ChooseBestCompressionScheme( + [this](const TString& coding) { + return AcceptEncoding(coding); + }, + codings + ); } TString THttpInput::BestCompressionScheme() const { diff --git a/library/cpp/http/io/stream_ut.cpp b/library/cpp/http/io/stream_ut.cpp index 1d78c82e0e..1ea35df675 100644 --- a/library/cpp/http/io/stream_ut.cpp +++ b/library/cpp/http/io/stream_ut.cpp @@ -179,63 +179,63 @@ Y_UNIT_TEST_SUITE(THttpStreamTest) { } Y_UNIT_TEST(TestKeepAlive) { - { + { TString s = "GET / HTTP/1.0\r\n\r\n"; - TStringInput si(s); - THttpInput in(&si); - UNIT_ASSERT(!in.IsKeepAlive()); - } - - { + TStringInput si(s); + THttpInput in(&si); + UNIT_ASSERT(!in.IsKeepAlive()); + } + + { TString s = "GET / HTTP/1.0\r\nConnection: keep-alive\r\n\r\n"; - TStringInput si(s); - THttpInput in(&si); - UNIT_ASSERT(in.IsKeepAlive()); - } - - { + TStringInput si(s); + THttpInput in(&si); + UNIT_ASSERT(in.IsKeepAlive()); + } + + { TString s = "GET / HTTP/1.1\r\n\r\n"; - TStringInput si(s); - THttpInput in(&si); - UNIT_ASSERT(in.IsKeepAlive()); - } - - { + TStringInput si(s); + THttpInput in(&si); + UNIT_ASSERT(in.IsKeepAlive()); + } + + { TString s = "GET / HTTP/1.1\r\nConnection: close\r\n\r\n"; - TStringInput si(s); - THttpInput in(&si); - UNIT_ASSERT(!in.IsKeepAlive()); - } - - { + TStringInput si(s); + THttpInput in(&si); + UNIT_ASSERT(!in.IsKeepAlive()); + } + + { TString s = "HTTP/1.0 200 Ok\r\n\r\n"; - TStringInput si(s); - THttpInput in(&si); - UNIT_ASSERT(!in.IsKeepAlive()); - } - - { + TStringInput si(s); + THttpInput in(&si); + UNIT_ASSERT(!in.IsKeepAlive()); + } + + { TString s = "HTTP/1.0 200 Ok\r\nConnection: keep-alive\r\n\r\n"; - TStringInput si(s); - THttpInput in(&si); - UNIT_ASSERT(in.IsKeepAlive()); - } - - { + TStringInput si(s); + THttpInput in(&si); + UNIT_ASSERT(in.IsKeepAlive()); + } + + { TString s = "HTTP/1.1 200 Ok\r\n\r\n"; - TStringInput si(s); - THttpInput in(&si); - UNIT_ASSERT(in.IsKeepAlive()); - } - - { + TStringInput si(s); + THttpInput in(&si); + UNIT_ASSERT(in.IsKeepAlive()); + } + + { TString s = "HTTP/1.1 200 Ok\r\nConnection: close\r\n\r\n"; - TStringInput si(s); - THttpInput in(&si); - UNIT_ASSERT(!in.IsKeepAlive()); - } - } - + TStringInput si(s); + THttpInput in(&si); + UNIT_ASSERT(!in.IsKeepAlive()); + } + } + Y_UNIT_TEST(TestMinRequest) { TString res = "qqqqqq"; TPortManager pm; diff --git a/library/cpp/http/server/conn.cpp b/library/cpp/http/server/conn.cpp index 37f82f9f7b..38a76c4c30 100644 --- a/library/cpp/http/server/conn.cpp +++ b/library/cpp/http/server/conn.cpp @@ -5,11 +5,11 @@ class THttpServerConn::TImpl { public: - inline TImpl(const TSocket& s, size_t outputBufferSize) + inline TImpl(const TSocket& s, size_t outputBufferSize) : S_(s) , SI_(S_) , SO_(S_) - , BO_(&SO_, outputBufferSize) + , BO_(&SO_, outputBufferSize) , HI_(&SI_) , HO_(&BO_, &HI_) { @@ -44,15 +44,15 @@ private: }; THttpServerConn::THttpServerConn(const TSocket& s) - : THttpServerConn(s, s.MaximumTransferUnit()) + : THttpServerConn(s, s.MaximumTransferUnit()) +{ +} + +THttpServerConn::THttpServerConn(const TSocket& s, size_t outputBufferSize) + : Impl_(new TImpl(s, outputBufferSize)) { } -THttpServerConn::THttpServerConn(const TSocket& s, size_t outputBufferSize) - : Impl_(new TImpl(s, outputBufferSize)) -{ -} - THttpServerConn::~THttpServerConn() { } diff --git a/library/cpp/http/server/http.cpp b/library/cpp/http/server/http.cpp index 3d21108b02..128583bdd7 100644 --- a/library/cpp/http/server/http.cpp +++ b/library/cpp/http/server/http.cpp @@ -1,5 +1,5 @@ #include "http.h" -#include "http_ex.h" +#include "http_ex.h" #include <library/cpp/threading/equeue/equeue.h> @@ -243,17 +243,17 @@ public: } void AddRequest(TAutoPtr<TClientRequest> req, bool fail) { - struct TFailRequest: public THttpClientRequestEx { + struct TFailRequest: public THttpClientRequestEx { inline TFailRequest(TAutoPtr<TClientRequest> parent) { Conn_.Reset(parent->Conn_.Release()); HttpConn_.Reset(parent->HttpConn_.Release()); } bool Reply(void*) override { - if (!ProcessHeaders()) { + if (!ProcessHeaders()) { return true; - } - + } + ProcessFailRequest(0); return true; } @@ -558,11 +558,11 @@ TClientConnection::TClientConnection(const TSocket& s, THttpServer::TImpl* serv, { SetNoDelay(Socket_, true); - const TDuration& clientTimeout = HttpServ_->Options().ClientTimeout; - if (clientTimeout != TDuration::Zero()) { + const TDuration& clientTimeout = HttpServ_->Options().ClientTimeout; + if (clientTimeout != TDuration::Zero()) { SetSocketTimeout(Socket_, (long)clientTimeout.Seconds(), clientTimeout.MilliSecondsOfSecond()); - } - + } + HttpServ_->IncreaseConnections(); } @@ -679,16 +679,16 @@ void TClientRequest::ResetConnection() { void TClientRequest::Process(void* ThreadSpecificResource) { THolder<TClientRequest> this_(this); - auto* serverImpl = Conn_->HttpServ_; - + auto* serverImpl = Conn_->HttpServ_; + try { if (!HttpConn_) { - const size_t outputBufferSize = HttpServ()->Options().OutputBufferSize; - if (outputBufferSize) { - HttpConn_.Reset(new THttpServerConn(Socket(), outputBufferSize)); - } else { - HttpConn_.Reset(new THttpServerConn(Socket())); - } + const size_t outputBufferSize = HttpServ()->Options().OutputBufferSize; + if (outputBufferSize) { + HttpConn_.Reset(new THttpServerConn(Socket(), outputBufferSize)); + } else { + HttpConn_.Reset(new THttpServerConn(Socket())); + } auto maxRequestsPerConnection = HttpServ()->Options().MaxRequestsPerConnection; HttpConn_->Output()->EnableKeepAlive(HttpServ()->Options().KeepAliveEnabled && (!maxRequestsPerConnection || Conn_->ReceivedRequests < maxRequestsPerConnection)); @@ -715,7 +715,7 @@ void TClientRequest::Process(void* ThreadSpecificResource) { return; } } catch (...) { - serverImpl->Cb_->OnException(); + serverImpl->Cb_->OnException(); throw; } diff --git a/library/cpp/http/server/http_ut.cpp b/library/cpp/http/server/http_ut.cpp index 42580906f6..cc62bb988e 100644 --- a/library/cpp/http/server/http_ut.cpp +++ b/library/cpp/http/server/http_ut.cpp @@ -456,31 +456,31 @@ Y_UNIT_TEST_SUITE(THttpServerTest) { server.Stop(); } - class TReleaseConnectionServer: public THttpServer::ICallBack { - class TRequest: public THttpClientRequestEx { - public: - bool Reply(void* /*tsr*/) override { - Output() << "HTTP/1.1 200 Ok\r\n\r\n"; - Output() << "reply"; - Output().Finish(); - - ReleaseConnection(); - - throw yexception() << "some error"; - - return true; - } - }; - - public: - TClientRequest* CreateClient() override { - return new TRequest(); - } - - void OnException() override { - ExceptionMessage = CurrentExceptionMessage(); - } - + class TReleaseConnectionServer: public THttpServer::ICallBack { + class TRequest: public THttpClientRequestEx { + public: + bool Reply(void* /*tsr*/) override { + Output() << "HTTP/1.1 200 Ok\r\n\r\n"; + Output() << "reply"; + Output().Finish(); + + ReleaseConnection(); + + throw yexception() << "some error"; + + return true; + } + }; + + public: + TClientRequest* CreateClient() override { + return new TRequest(); + } + + void OnException() override { + ExceptionMessage = CurrentExceptionMessage(); + } + TString ExceptionMessage; }; @@ -495,7 +495,7 @@ Y_UNIT_TEST_SUITE(THttpServerTest) { } }; - public: + public: TClientRequest* CreateClient() override { return new TRequest(); } @@ -504,9 +504,9 @@ Y_UNIT_TEST_SUITE(THttpServerTest) { ExceptionMessage = CurrentExceptionMessage(); } - TString ExceptionMessage; - }; - + TString ExceptionMessage; + }; + class TListenerSockAddrReplyServer: public THttpServer::ICallBack { class TRequest: public TClientRequest { public: @@ -542,22 +542,22 @@ Y_UNIT_TEST_SUITE(THttpServerTest) { }; Y_UNIT_TEST(TTestReleaseConnection) { - TPortManager pm; - const ui16 port = pm.GetPort(); - - TReleaseConnectionServer serverImpl; - THttpServer server(&serverImpl, THttpServer::TOptions(port).EnableKeepAlive(true)); - UNIT_ASSERT(server.Start()); - - TTestRequest r(port, "request"); - r.KeepAliveConnection = true; - - UNIT_ASSERT_C(r.Execute() == "reply", "diff echo response for request:\n" + r.GetDescription()); - - server.Stop(); - - UNIT_ASSERT_STRINGS_EQUAL(serverImpl.ExceptionMessage, "(yexception) some error"); - }; + TPortManager pm; + const ui16 port = pm.GetPort(); + + TReleaseConnectionServer serverImpl; + THttpServer server(&serverImpl, THttpServer::TOptions(port).EnableKeepAlive(true)); + UNIT_ASSERT(server.Start()); + + TTestRequest r(port, "request"); + r.KeepAliveConnection = true; + + UNIT_ASSERT_C(r.Execute() == "reply", "diff echo response for request:\n" + r.GetDescription()); + + server.Stop(); + + UNIT_ASSERT_STRINGS_EQUAL(serverImpl.ExceptionMessage, "(yexception) some error"); + }; THttpInput SendRequest(TSocket& socket, ui16 port) { TSocketInput si(socket); diff --git a/library/cpp/http/server/options.h b/library/cpp/http/server/options.h index 602649e430..38eda0e5e7 100644 --- a/library/cpp/http/server/options.h +++ b/library/cpp/http/server/options.h @@ -6,7 +6,7 @@ #include <util/generic/size_literals.h> #include <util/generic/string.h> #include <util/generic/vector.h> -#include <util/datetime/base.h> +#include <util/datetime/base.h> class THttpServerOptions { public: @@ -93,10 +93,10 @@ public: return *this; } - + inline THttpServerOptions& SetClientTimeout(const TDuration& timeout) noexcept { ClientTimeout = timeout; - + return *this; } @@ -107,11 +107,11 @@ public: } inline THttpServerOptions& SetOutputBufferSize(size_t val) noexcept { - OutputBufferSize = val; - - return *this; - } - + OutputBufferSize = val; + + return *this; + } + inline THttpServerOptions& SetMaxInputContentLength(ui64 val) noexcept { MaxInputContentLength = val; @@ -162,7 +162,7 @@ public: ui32 MaxConnections = 100; int ListenBacklog = SOMAXCONN; TDuration ClientTimeout; - size_t OutputBufferSize = 0; + size_t OutputBufferSize = 0; ui64 MaxInputContentLength = sizeof(size_t) <= 4 ? 2_GB : 64_GB; size_t MaxRequestsPerConnection = 0; // If keep-alive is enabled, request limit before connection is closed bool UseElasticQueues = false; diff --git a/library/cpp/http/server/response.h b/library/cpp/http/server/response.h index eed4afc7b6..a75cb85605 100644 --- a/library/cpp/http/server/response.h +++ b/library/cpp/http/server/response.h @@ -34,10 +34,10 @@ public: THttpResponse& AddMultipleHeaders(const THttpHeaders& headers); - const THttpHeaders& GetHeaders() const { - return Headers; - } - + const THttpHeaders& GetHeaders() const { + return Headers; + } + THttpResponse& SetContentType(const TStringBuf& contentType); /** diff --git a/library/cpp/http/server/response_ut.cpp b/library/cpp/http/server/response_ut.cpp index 8a142fb1ba..73e2112ad3 100644 --- a/library/cpp/http/server/response_ut.cpp +++ b/library/cpp/http/server/response_ut.cpp @@ -46,25 +46,25 @@ Y_UNIT_TEST_SUITE(TestHttpResponse) { EXPECTED); } - Y_UNIT_TEST(TestGetHeaders) { - THttpResponse resp(HTTP_FORBIDDEN); - - THttpHeaders headers; - headers.AddHeader(THttpInputHeader("X-Header-1", "ValueOne")); - headers.AddHeader(THttpInputHeader("X-Header-2", "ValueTwo")); - headers.AddHeader(THttpInputHeader("X-Header-3", "ValueThree")); - resp.AddMultipleHeaders(headers); - resp.AddHeader("X-Header-4", "ValueFour"); - - const THttpHeaders& gotHeaders = resp.GetHeaders(); - UNIT_ASSERT_VALUES_EQUAL(gotHeaders.Count(), 4); - UNIT_ASSERT(gotHeaders.HasHeader("X-Header-1")); - UNIT_ASSERT_STRINGS_EQUAL(gotHeaders.FindHeader("X-Header-1")->Value(), "ValueOne"); - UNIT_ASSERT(gotHeaders.HasHeader("X-Header-4")); - UNIT_ASSERT_STRINGS_EQUAL(gotHeaders.FindHeader("X-Header-4")->Value(), "ValueFour"); - } - - + Y_UNIT_TEST(TestGetHeaders) { + THttpResponse resp(HTTP_FORBIDDEN); + + THttpHeaders headers; + headers.AddHeader(THttpInputHeader("X-Header-1", "ValueOne")); + headers.AddHeader(THttpInputHeader("X-Header-2", "ValueTwo")); + headers.AddHeader(THttpInputHeader("X-Header-3", "ValueThree")); + resp.AddMultipleHeaders(headers); + resp.AddHeader("X-Header-4", "ValueFour"); + + const THttpHeaders& gotHeaders = resp.GetHeaders(); + UNIT_ASSERT_VALUES_EQUAL(gotHeaders.Count(), 4); + UNIT_ASSERT(gotHeaders.HasHeader("X-Header-1")); + UNIT_ASSERT_STRINGS_EQUAL(gotHeaders.FindHeader("X-Header-1")->Value(), "ValueOne"); + UNIT_ASSERT(gotHeaders.HasHeader("X-Header-4")); + UNIT_ASSERT_STRINGS_EQUAL(gotHeaders.FindHeader("X-Header-4")->Value(), "ValueFour"); + } + + Y_UNIT_TEST(TestSetContent) { const char* EXPECTED = "HTTP/1.1 200 Ok\r\n" "Content-Length: 10\r\n" |