diff options
author | elviandante <elviandante@yandex-team.ru> | 2022-02-10 16:49:47 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:49:47 +0300 |
commit | 621a17b75565a8d70df465a0ac5c93a7c6d2e61f (patch) | |
tree | 5d5cb817648f650d76cf1076100726fd9b8448e8 /library/cpp/monlib/service | |
parent | 643ddee8bd6125a18c4b1506c35bee857f64f4d2 (diff) | |
download | ydb-621a17b75565a8d70df465a0ac5c93a7c6d2e61f.tar.gz |
Restoring authorship annotation for <elviandante@yandex-team.ru>. Commit 2 of 2.
Diffstat (limited to 'library/cpp/monlib/service')
-rw-r--r-- | library/cpp/monlib/service/service.cpp | 268 | ||||
-rw-r--r-- | library/cpp/monlib/service/service.h | 110 |
2 files changed, 189 insertions, 189 deletions
diff --git a/library/cpp/monlib/service/service.cpp b/library/cpp/monlib/service/service.cpp index 559aba661ef..929efbf8162 100644 --- a/library/cpp/monlib/service/service.cpp +++ b/library/cpp/monlib/service/service.cpp @@ -5,64 +5,64 @@ #include <library/cpp/http/fetch/httpheader.h> #include <library/cpp/http/fetch/httpfsm.h> #include <library/cpp/uri/http_url.h> - + #include <util/generic/buffer.h> #include <util/stream/str.h> #include <util/stream/buffer.h> #include <util/stream/zerocopy.h> #include <util/string/vector.h> -namespace NMonitoring { +namespace NMonitoring { class THttpClient: public IHttpRequest { - public: + public: void ServeRequest(THttpInput& in, IOutputStream& out, const NAddr::IRemoteAddr* remoteAddr, const THandler& Handler) { - try { - try { + try { + try { RemoteAddr = remoteAddr; - THttpHeaderParser parser; - parser.Init(&Header); + THttpHeaderParser parser; + parser.Init(&Header); if (parser.Execute(in.FirstLine().data(), in.FirstLine().size()) < 0) { - out << "HTTP/1.1 400 Bad request\r\nConnection: Close\r\n\r\n"; - return; - } + out << "HTTP/1.1 400 Bad request\r\nConnection: Close\r\n\r\n"; + return; + } if (Url.Parse(Header.GetUrl().data()) != THttpURL::ParsedOK) { - out << "HTTP/1.1 400 Invalid url\r\nConnection: Close\r\n\r\n"; - return; - } + out << "HTTP/1.1 400 Invalid url\r\nConnection: Close\r\n\r\n"; + return; + } TString path = GetPath(); if (!path.StartsWith('/')) { out << "HTTP/1.1 400 Bad request\r\nConnection: Close\r\n\r\n"; return; } Headers = &in.Headers(); - CgiParams.Scan(Url.Get(THttpURL::FieldQuery)); + CgiParams.Scan(Url.Get(THttpURL::FieldQuery)); } catch (...) { - out << "HTTP/1.1 500 Internal server error\r\nConnection: Close\r\n\r\n"; + out << "HTTP/1.1 500 Internal server error\r\nConnection: Close\r\n\r\n"; YSYSLOG(TLOG_ERR, "THttpClient: internal error while serving monitoring request: %s", CurrentExceptionMessage().data()); - } - + } + if (Header.http_method == HTTP_METHOD_POST) TransferData(&in, &PostContent); - Handler(out, *this); - out.Finish(); + Handler(out, *this); + out.Finish(); } catch (...) { auto msg = CurrentExceptionMessage(); out << "HTTP/1.1 500 Internal server error\r\nConnection: Close\r\n\r\n" << msg; out.Finish(); YSYSLOG(TLOG_ERR, "THttpClient: error while serving monitoring request: %s", msg.data()); - } - } - + } + } + const char* GetURI() const override { return Header.request_uri.c_str(); - } + } const char* GetPath() const override { - return Url.Get(THttpURL::FieldPath); - } + return Url.Get(THttpURL::FieldPath); + } const TCgiParameters& GetParams() const override { - return CgiParams; - } + return CgiParams; + } const TCgiParameters& GetPostParams() const override { if (PostParams.empty() && !PostContent.Buffer().Empty()) const_cast<THttpClient*>(this)->ScanPostParams(); @@ -90,125 +90,125 @@ namespace NMonitoring { return RemoteAddr ? NAddr::PrintHostAndPort(*RemoteAddr) : TString(); } - private: - THttpRequestHeader Header; + private: + THttpRequestHeader Header; const THttpHeaders* Headers = nullptr; - THttpURL Url; + THttpURL Url; TCgiParameters CgiParams; TCgiParameters PostParams; TBufferOutput PostContent; const NAddr::IRemoteAddr* RemoteAddr = nullptr; - }; - - /* TCoHttpServer */ - + }; + + /* TCoHttpServer */ + class TCoHttpServer::TConnection: public THttpClient { - public: + public: TConnection(const TCoHttpServer::TAcceptFull& acc, const TCoHttpServer& parent) : Socket(acc.S->Release()) , RemoteAddr(acc.Remote) - , Parent(parent) - { - } - + , Parent(parent) + { + } + void operator()(TCont* c) { - try { - THolder<TConnection> me(this); - TContIO io(Socket, c); - THttpInput in(&io); - THttpOutput out(&io, &in); - // buffer reply so there will be ne context switching - TStringStream s; + try { + THolder<TConnection> me(this); + TContIO io(Socket, c); + THttpInput in(&io); + THttpOutput out(&io, &in); + // buffer reply so there will be ne context switching + TStringStream s; ServeRequest(in, s, RemoteAddr, Parent.Handler); - out << s.Str(); - out.Finish(); + out << s.Str(); + out.Finish(); } catch (...) { YSYSLOG(TLOG_WARNING, "TCoHttpServer::TConnection: error: %s\n", CurrentExceptionMessage().data()); - } - } + } + } - private: - TSocketHolder Socket; + private: + TSocketHolder Socket; const NAddr::IRemoteAddr* RemoteAddr; - const TCoHttpServer& Parent; - }; - + const TCoHttpServer& Parent; + }; + TCoHttpServer::TCoHttpServer(TContExecutor& executor, const TString& bindAddr, TIpPort port, THandler handler) - : Executor(executor) - , Listener(this, &executor) + : Executor(executor) + , Listener(this, &executor) , Handler(std::move(handler)) - , BindAddr(bindAddr) - , Port(port) - { + , BindAddr(bindAddr) + , Port(port) + { try { Listener.Bind(TIpAddress(bindAddr, port)); } catch (yexception e) { Y_FAIL("TCoHttpServer::TCoHttpServer: couldn't bind to %s:%d\n", bindAddr.data(), port); } - } - - void TCoHttpServer::Start() { - Listener.Listen(); - } - - void TCoHttpServer::Stop() { - Listener.Stop(); - } - + } + + void TCoHttpServer::Start() { + Listener.Listen(); + } + + void TCoHttpServer::Stop() { + Listener.Stop(); + } + void TCoHttpServer::OnAcceptFull(const TAcceptFull& acc) { THolder<TConnection> conn(new TConnection(acc, *this)); - Executor.Create(*conn, "client"); + Executor.Create(*conn, "client"); Y_UNUSED(conn.Release()); - } - - void TCoHttpServer::OnError() { - throw; // just rethrow - } - + } + + void TCoHttpServer::OnError() { + throw; // just rethrow + } + void TCoHttpServer::ProcessRequest(IOutputStream& out, const IHttpRequest& request) { - try { - TNetworkAddress addr(BindAddr, Port); - TSocket sock(addr); - TSocketOutput sock_out(sock); - TSocketInput sock_in(sock); + try { + TNetworkAddress addr(BindAddr, Port); + TSocket sock(addr); + TSocketOutput sock_out(sock); + TSocketInput sock_in(sock); sock_out << "GET " << request.GetURI() << " HTTP/1.0\r\n\r\n"; - THttpInput http_in(&sock_in); - try { - out << "HTTP/1.1 200 Ok\nConnection: Close\n\n"; - TransferData(&http_in, &out); + THttpInput http_in(&sock_in); + try { + out << "HTTP/1.1 200 Ok\nConnection: Close\n\n"; + TransferData(&http_in, &out); } catch (...) { YSYSLOG(TLOG_DEBUG, "TCoHttpServer: while getting data from backend: %s", CurrentExceptionMessage().data()); - } + } } catch (const yexception& /*e*/) { - out << "HTTP/1.1 500 Internal server error\nConnection: Close\n\n"; + out << "HTTP/1.1 500 Internal server error\nConnection: Close\n\n"; YSYSLOG(TLOG_DEBUG, "TCoHttpServer: while getting data from backend: %s", CurrentExceptionMessage().data()); - } - } - - /* TMtHttpServer */ - + } + } + + /* TMtHttpServer */ + class TMtHttpServer::TConnection: public TClientRequest, public THttpClient { - public: - TConnection(const TMtHttpServer& parent) - : Parent(parent) - { - } - + public: + TConnection(const TMtHttpServer& parent) + : Parent(parent) + { + } + bool Reply(void*) override { ServeRequest(Input(), Output(), NAddr::GetPeerAddr(Socket()).Get(), Parent.Handler); - return true; - } + return true; + } + + private: + const TMtHttpServer& Parent; + }; - private: - const TMtHttpServer& Parent; - }; - TMtHttpServer::TMtHttpServer(const TOptions& options, THandler handler, IThreadFactory* pool) - : THttpServer(this, options, pool) + : THttpServer(this, options, pool) , Handler(std::move(handler)) - { - } - + { + } + TMtHttpServer::TMtHttpServer(const TOptions& options, THandler handler, TSimpleSharedPtr<IThreadPool> pool) : THttpServer(this, /* mainWorkers = */pool, /* failWorkers = */pool, options) , Handler(std::move(handler)) @@ -233,36 +233,36 @@ namespace NMonitoring { THttpServer::Stop(); } - TClientRequest* TMtHttpServer::CreateClient() { - return new TConnection(*this); - } - - /* TService */ - + TClientRequest* TMtHttpServer::CreateClient() { + return new TConnection(*this); + } + + /* TService */ + TMonService::TMonService(TContExecutor& executor, TIpPort internalPort, TIpPort externalPort, THandler coHandler, THandler mtHandler) : CoServer(executor, "127.0.0.1", internalPort, std::move(coHandler)) , MtServer(THttpServerOptions(externalPort), std::bind(&TMonService::DispatchRequest, this, std::placeholders::_1, std::placeholders::_2)) , MtHandler(std::move(mtHandler)) - { - } - - void TMonService::Start() { - MtServer.Start(); - CoServer.Start(); - } - - void TMonService::Stop() { - MtServer.Stop(); - CoServer.Stop(); - } - + { + } + + void TMonService::Start() { + MtServer.Start(); + CoServer.Start(); + } + + void TMonService::Stop() { + MtServer.Stop(); + CoServer.Stop(); + } + void TMonService::DispatchRequest(IOutputStream& out, const IHttpRequest& request) { - if (strcmp(request.GetPath(), "/") == 0) { - out << "HTTP/1.1 200 Ok\nConnection: Close\n\n"; - MtHandler(out, request); + if (strcmp(request.GetPath(), "/") == 0) { + out << "HTTP/1.1 200 Ok\nConnection: Close\n\n"; + MtHandler(out, request); } else - CoServer.ProcessRequest(out, request); - } - + CoServer.ProcessRequest(out, request); + } + } diff --git a/library/cpp/monlib/service/service.h b/library/cpp/monlib/service/service.h index 2ea5f0f4a2d..2f66dddaf87 100644 --- a/library/cpp/monlib/service/service.h +++ b/library/cpp/monlib/service/service.h @@ -1,65 +1,65 @@ #pragma once - + #include <library/cpp/coroutine/engine/impl.h> #include <library/cpp/coroutine/listener/listen.h> #include <library/cpp/http/fetch/httpheader.h> #include <library/cpp/http/server/http.h> #include <library/cpp/logger/all.h> -#include <util/network/ip.h> +#include <util/network/ip.h> #include <library/cpp/cgiparam/cgiparam.h> - + #include <functional> - + struct TMonitor; - -namespace NMonitoring { - struct IHttpRequest { + +namespace NMonitoring { + struct IHttpRequest { virtual ~IHttpRequest() { } - virtual const char* GetURI() const = 0; - virtual const char* GetPath() const = 0; + virtual const char* GetURI() const = 0; + virtual const char* GetPath() const = 0; virtual const TCgiParameters& GetParams() const = 0; virtual const TCgiParameters& GetPostParams() const = 0; virtual TStringBuf GetPostContent() const = 0; virtual HTTP_METHOD GetMethod() const = 0; virtual const THttpHeaders& GetHeaders() const = 0; virtual TString GetRemoteAddr() const = 0; - }; - // first param - output stream to write result to - // second param - URL of request + }; + // first param - output stream to write result to + // second param - URL of request typedef std::function<void(IOutputStream&, const IHttpRequest&)> THandler; - + class TCoHttpServer: private TContListener::ICallBack { - public: - // initialize and schedule coroutines for execution + public: + // initialize and schedule coroutines for execution TCoHttpServer(TContExecutor& executor, const TString& bindAddr, TIpPort port, THandler handler); - void Start(); - void Stop(); - - // this function implements THandler interface - // by forwarding it to the httpserver - // @note this call may be blocking; don't use inside coroutines - // @throws may throw in case of connection error, etc + void Start(); + void Stop(); + + // this function implements THandler interface + // by forwarding it to the httpserver + // @note this call may be blocking; don't use inside coroutines + // @throws may throw in case of connection error, etc void ProcessRequest(IOutputStream&, const IHttpRequest&); - private: - class TConnection; - - // ICallBack implementation + private: + class TConnection; + + // ICallBack implementation void OnAcceptFull(const TAcceptFull& a) override; void OnError() override; - private: - TContExecutor& Executor; - TContListener Listener; - THandler Handler; + private: + TContExecutor& Executor; + TContListener Listener; + THandler Handler; TString BindAddr; - TIpPort Port; - }; - + TIpPort Port; + }; + class TMtHttpServer: public THttpServer, private THttpServer::ICallBack { - public: + public: TMtHttpServer(const TOptions& options, THandler handler, IThreadFactory* pool = nullptr); TMtHttpServer(const TOptions& options, THandler handler, TSimpleSharedPtr<IThreadPool> pool); @@ -82,31 +82,31 @@ namespace NMonitoring { */ void Stop(); - private: - class TConnection; + private: + class TConnection; TClientRequest* CreateClient() override; - - THandler Handler; - }; - - // this class implements hybrid coroutine and threaded approach - // requests for main page which holds counters and simple tables are served in a thread - // requests for other pages which include access with inter-thread synchonization - // will be served in a coroutine context - class TMonService { - public: + + THandler Handler; + }; + + // this class implements hybrid coroutine and threaded approach + // requests for main page which holds counters and simple tables are served in a thread + // requests for other pages which include access with inter-thread synchonization + // will be served in a coroutine context + class TMonService { + public: TMonService(TContExecutor& executor, TIpPort internalPort, TIpPort externalPort, THandler coHandler, THandler mtHandler); - void Start(); - void Stop(); + void Start(); + void Stop(); - protected: + protected: void DispatchRequest(IOutputStream& out, const IHttpRequest&); - private: - TCoHttpServer CoServer; - TMtHttpServer MtServer; - THandler MtHandler; - }; - + private: + TCoHttpServer CoServer; + TMtHttpServer MtServer; + THandler MtHandler; + }; + } |