diff options
author | Alexey Bykov <alexei4203@yandex.ru> | 2022-02-10 16:47:16 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:47:16 +0300 |
commit | 4cadece7a57ab767e762a0bea1995a596aefeb11 (patch) | |
tree | 7649c16cf4b52e994709f6c9e1716c993ca28759 /library/cpp/http | |
parent | 143876304996506751ade0b80b3c47f188b9834f (diff) | |
download | ydb-4cadece7a57ab767e762a0bea1995a596aefeb11.tar.gz |
Restoring authorship annotation for Alexey Bykov <alexei4203@yandex.ru>. Commit 1 of 2.
Diffstat (limited to 'library/cpp/http')
-rw-r--r-- | library/cpp/http/server/http.cpp | 90 | ||||
-rw-r--r-- | library/cpp/http/server/http_ut.cpp | 80 | ||||
-rw-r--r-- | library/cpp/http/server/options.h | 6 |
3 files changed, 88 insertions, 88 deletions
diff --git a/library/cpp/http/server/http.cpp b/library/cpp/http/server/http.cpp index 128583bdd7..4cf7fa0a91 100644 --- a/library/cpp/http/server/http.cpp +++ b/library/cpp/http/server/http.cpp @@ -37,14 +37,14 @@ namespace { virtual ~IPollAble() { } - virtual void OnPollEvent(TInstant now) = 0; + virtual void OnPollEvent(TInstant now) = 0; }; struct TShouldStop { }; struct TWakeupPollAble: public IPollAble { - void OnPollEvent(TInstant) override { + void OnPollEvent(TInstant) override { throw TShouldStop(); } }; @@ -55,9 +55,9 @@ public: TClientConnection(const TSocket& s, THttpServer::TImpl* serv, NAddr::IRemoteAddrRef listenerSockAddrRef); ~TClientConnection() override; - void OnPollEvent(TInstant now) override; + void OnPollEvent(TInstant now) override; - inline void Activate(TInstant now) noexcept; + inline void Activate(TInstant now) noexcept; inline void DeActivate(); inline void Reject(); @@ -66,7 +66,7 @@ public: NAddr::IRemoteAddrRef ListenerSockAddrRef_; THttpServer::TImpl* HttpServ_ = nullptr; bool Reject_ = false; - TInstant LastUsed; + TInstant LastUsed; TInstant AcceptMoment; size_t ReceivedRequests = 0; }; @@ -75,9 +75,9 @@ class THttpServer::TImpl { public: class TConnections { public: - inline TConnections(TSocketPoller* poller, const THttpServerOptions& options) + inline TConnections(TSocketPoller* poller, const THttpServerOptions& options) : Poller_(poller) - , Options(options) + , Options(options) { } @@ -91,12 +91,12 @@ public: Poller_->WaitRead(c->Socket_, (void*)static_cast<const IPollAble*>(c)); } - inline void Erase(TClientConnection* c, TInstant now) noexcept { + inline void Erase(TClientConnection* c, TInstant now) noexcept { TGuard<TMutex> g(Mutex_); - EraseUnsafe(c); - if (Options.ExpirationTimeout > TDuration::Zero()) { - TryRemovingUnsafe(now - Options.ExpirationTimeout); - } + EraseUnsafe(c); + if (Options.ExpirationTimeout > TDuration::Zero()) { + TryRemovingUnsafe(now - Options.ExpirationTimeout); + } } inline void Clear() noexcept { @@ -105,34 +105,34 @@ public: Conns_.Clear(); } - inline bool RemoveOld(TInstant border) noexcept { + inline bool RemoveOld(TInstant border) noexcept { TGuard<TMutex> g(Mutex_); - return TryRemovingUnsafe(border); - } + return TryRemovingUnsafe(border); + } - bool TryRemovingUnsafe(TInstant border) noexcept { + bool TryRemovingUnsafe(TInstant border) noexcept { if (Conns_.Empty()) { return false; } TClientConnection* c = &*(Conns_.Begin()); - if (c->LastUsed > border) { - return false; - } - EraseUnsafe(c); + if (c->LastUsed > border) { + return false; + } + EraseUnsafe(c); delete c; return true; } - void EraseUnsafe(TClientConnection* c) noexcept { - Poller_->Unwait(c->Socket_); - c->Unlink(); - } - + void EraseUnsafe(TClientConnection* c) noexcept { + Poller_->Unwait(c->Socket_); + c->Unlink(); + } + public: TMutex Mutex_; TIntrusiveListWithAutoDelete<TClientConnection, TDelete> Conns_; TSocketPoller* Poller_ = nullptr; - const THttpServerOptions& Options; + const THttpServerOptions& Options; }; static void* ListenSocketFunction(void* param) { @@ -156,7 +156,7 @@ public: void AddRequestFromSocket(const TSocket& s, TInstant now, NAddr::IRemoteAddrRef listenerSockAddrRef) { if (MaxRequestsReached()) { Cb_->OnMaxConn(); - bool wasRemoved = Connections->RemoveOld(TInstant::Max()); + bool wasRemoved = Connections->RemoveOld(TInstant::Max()); if (!wasRemoved && Options_.RejectExcessConnections) { (new TClientConnection(s, this, listenerSockAddrRef))->Reject(); return; @@ -164,8 +164,8 @@ public: } auto connection = new TClientConnection(s, this, listenerSockAddrRef); - connection->LastUsed = now; - connection->DeActivate(); + connection->LastUsed = now; + connection->DeActivate(); } void SaveErrorCode() { @@ -182,7 +182,7 @@ public: bool Start() { Poller.Reset(new TSocketPoller()); - Connections.Reset(new TConnections(Poller.Get(), Options_)); + Connections.Reset(new TConnections(Poller.Get(), Options_)); // Start the listener thread ListenerRunningOK = false; @@ -348,26 +348,26 @@ public: TVector<void*> events; events.resize(1); - TInstant now = TInstant::Now(); + TInstant now = TInstant::Now(); for (;;) { try { - const TInstant deadline = Options_.PollTimeout == TDuration::Zero() ? TInstant::Max() : now + Options_.PollTimeout; + const TInstant deadline = Options_.PollTimeout == TDuration::Zero() ? TInstant::Max() : now + Options_.PollTimeout; const size_t ret = Poller->WaitD(events.data(), events.size(), deadline); - now = TInstant::Now(); + now = TInstant::Now(); for (size_t i = 0; i < ret; ++i) { - ((IPollAble*)events[i])->OnPollEvent(now); - } - - if (ret == 0 && Options_.ExpirationTimeout > TDuration::Zero()) { - Connections->RemoveOld(now - Options_.ExpirationTimeout); + ((IPollAble*)events[i])->OnPollEvent(now); } - // When MaxConnections is limited or ExpirationTimeout is set, OnPollEvent can call + if (ret == 0 && Options_.ExpirationTimeout > TDuration::Zero()) { + Connections->RemoveOld(now - Options_.ExpirationTimeout); + } + + // When MaxConnections is limited or ExpirationTimeout is set, OnPollEvent can call // RemoveOld and destroy other IPollAble* objects in the // poller. Thus in this case we can safely process only one // event from the poller at a time. - if (!Options_.MaxConnections && Options_.ExpirationTimeout == TDuration::Zero()) { + if (!Options_.MaxConnections && Options_.ExpirationTimeout == TDuration::Zero()) { if (ret >= events.size()) { events.resize(ret * 2); } @@ -570,9 +570,9 @@ TClientConnection::~TClientConnection() { HttpServ_->DecreaseConnections(); } -void TClientConnection::OnPollEvent(TInstant now) { +void TClientConnection::OnPollEvent(TInstant now) { THolder<TClientConnection> this_(this); - Activate(now); + Activate(now); { char tmp[1]; @@ -594,9 +594,9 @@ void TClientConnection::OnPollEvent(TInstant now) { HttpServ_->AddRequest(obj, Reject_); } -void TClientConnection::Activate(TInstant now) noexcept { - HttpServ_->Connections->Erase(this, now); - LastUsed = now; +void TClientConnection::Activate(TInstant now) noexcept { + HttpServ_->Connections->Erase(this, now); + LastUsed = now; ++ReceivedRequests; } diff --git a/library/cpp/http/server/http_ut.cpp b/library/cpp/http/server/http_ut.cpp index cc62bb988e..027c03ea44 100644 --- a/library/cpp/http/server/http_ut.cpp +++ b/library/cpp/http/server/http_ut.cpp @@ -558,22 +558,22 @@ Y_UNIT_TEST_SUITE(THttpServerTest) { UNIT_ASSERT_STRINGS_EQUAL(serverImpl.ExceptionMessage, "(yexception) some error"); }; - + THttpInput SendRequest(TSocket& socket, ui16 port) { - TSocketInput si(socket); - TSocketOutput so(socket); - THttpOutput out(&so); - out.EnableKeepAlive(true); - out << "GET / HTTP/1.1" << CrLf; - out << "Host: localhost:" + ToString(port) << CrLf; - out << CrLf; - out.Flush(); - - THttpInput input(&si); - input.ReadAll(); + TSocketInput si(socket); + TSocketOutput so(socket); + THttpOutput out(&so); + out.EnableKeepAlive(true); + out << "GET / HTTP/1.1" << CrLf; + out << "Host: localhost:" + ToString(port) << CrLf; + out << CrLf; + out.Flush(); + + THttpInput input(&si); + input.ReadAll(); return input; - } - + } + THttpInput SendRequestWithBody(TSocket& socket, ui16 port, TString body) { TSocketInput si(socket); TSocketOutput so(socket); @@ -590,32 +590,32 @@ Y_UNIT_TEST_SUITE(THttpServerTest) { return input; } - Y_UNIT_TEST(TTestExpirationTimeout) { - TPortManager pm; - const ui16 port = pm.GetPort(); - - TEchoServer serverImpl("test_data"); - THttpServer::TOptions options(port); - options.nThreads = 1; - options.MaxQueueSize = 0; - options.MaxConnections = 0; - options.KeepAliveEnabled = true; - options.ExpirationTimeout = TDuration::Seconds(1); - options.PollTimeout = TDuration::MilliSeconds(100); - THttpServer server(&serverImpl, options); - UNIT_ASSERT(server.Start()); - - TSocket socket(TNetworkAddress("localhost", port), TDuration::Seconds(10)); - - SendRequest(socket, port); - SendRequest(socket, port); - - Sleep(TDuration::Seconds(5)); - UNIT_ASSERT_EXCEPTION(SendRequest(socket, port), THttpReadException); - - server.Stop(); - } - + Y_UNIT_TEST(TTestExpirationTimeout) { + TPortManager pm; + const ui16 port = pm.GetPort(); + + TEchoServer serverImpl("test_data"); + THttpServer::TOptions options(port); + options.nThreads = 1; + options.MaxQueueSize = 0; + options.MaxConnections = 0; + options.KeepAliveEnabled = true; + options.ExpirationTimeout = TDuration::Seconds(1); + options.PollTimeout = TDuration::MilliSeconds(100); + THttpServer server(&serverImpl, options); + UNIT_ASSERT(server.Start()); + + TSocket socket(TNetworkAddress("localhost", port), TDuration::Seconds(10)); + + SendRequest(socket, port); + SendRequest(socket, port); + + Sleep(TDuration::Seconds(5)); + UNIT_ASSERT_EXCEPTION(SendRequest(socket, port), THttpReadException); + + server.Stop(); + } + Y_UNIT_TEST(TTestContentLengthTooLarge) { TPortManager pm; const ui16 port = pm.GetPort(); diff --git a/library/cpp/http/server/options.h b/library/cpp/http/server/options.h index 38eda0e5e7..3378d60549 100644 --- a/library/cpp/http/server/options.h +++ b/library/cpp/http/server/options.h @@ -166,9 +166,9 @@ public: 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; - - TDuration PollTimeout; // timeout of TSocketPoller::WaitT call - TDuration ExpirationTimeout; // drop inactive connections after ExpirationTimeout (should be > 0) + + TDuration PollTimeout; // timeout of TSocketPoller::WaitT call + TDuration ExpirationTimeout; // drop inactive connections after ExpirationTimeout (should be > 0) TString ListenThreadName = "HttpListen"; TString RequestsThreadName = "HttpServer"; |