aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/monlib/service/service.cpp
blob: e486b15dd41393ce1e0ddf4e539cb0c6776a1853 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
#include "service.h"

#include <library/cpp/coroutine/engine/sockpool.h>
#include <library/cpp/http/io/stream.h>
#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 {
    class THttpClient: public IHttpRequest {
    public:
        void ServeRequest(THttpInput& in, IOutputStream& out, const NAddr::IRemoteAddr* remoteAddr, const THandler& Handler) {
            try {
                try {
                    RemoteAddr = remoteAddr;
                    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;
                    }
                    if (Url.Parse(Header.GetUrl().data()) != THttpURL::ParsedOK) {
                        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));
                } catch (...) {
                    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();
            } 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);
        }
        const TCgiParameters& GetParams() const override {
            return CgiParams;
        }
        const TCgiParameters& GetPostParams() const override {
            if (PostParams.empty() && !PostContent.Buffer().Empty())
                const_cast<THttpClient*>(this)->ScanPostParams();
            return PostParams;
        }
        TStringBuf GetPostContent() const override {
            return TStringBuf(PostContent.Buffer().Data(), PostContent.Buffer().Size());
        }
        HTTP_METHOD GetMethod() const override {
            return (HTTP_METHOD)Header.http_method;
        }
        void ScanPostParams() {
            PostParams.Scan(TStringBuf(PostContent.Buffer().data(), PostContent.Buffer().size()));
        }

        const THttpHeaders& GetHeaders() const override {
            if (Headers != nullptr) {
                return *Headers;
            }
            static THttpHeaders defaultHeaders;
            return defaultHeaders;
        }

        TString GetRemoteAddr() const override {
            return RemoteAddr ? NAddr::PrintHostAndPort(*RemoteAddr) : TString();
        }

    private:
        THttpRequestHeader Header;
        const THttpHeaders* Headers = nullptr;
        THttpURL Url;
        TCgiParameters CgiParams;
        TCgiParameters PostParams;
        TBufferOutput PostContent;
        const NAddr::IRemoteAddr* RemoteAddr = nullptr;
    };

    /* TCoHttpServer */

    class TCoHttpServer::TConnection: public THttpClient {
    public:
        TConnection(const TCoHttpServer::TAcceptFull& acc, const TCoHttpServer& parent)
            : Socket(acc.S->Release())
            , RemoteAddr(acc.Remote)
            , 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;
                ServeRequest(in, s, RemoteAddr, Parent.Handler);
                out << s.Str();
                out.Finish();
            } catch (...) {
                YSYSLOG(TLOG_WARNING, "TCoHttpServer::TConnection: error: %s\n", CurrentExceptionMessage().data());
            }
        }

    private:
        TSocketHolder Socket;
        const NAddr::IRemoteAddr* RemoteAddr;
        const TCoHttpServer& Parent;
    };

    TCoHttpServer::TCoHttpServer(TContExecutor& executor, const TString& bindAddr, TIpPort port, THandler handler)
        : Executor(executor)
        , Listener(this, &executor)
        , Handler(std::move(handler))
        , 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::OnAcceptFull(const TAcceptFull& acc) {
        THolder<TConnection> conn(new TConnection(acc, *this));
        Executor.Create(*conn, "client");
        Y_UNUSED(conn.Release());
    }

    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);
            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);
            } 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";
            YSYSLOG(TLOG_DEBUG, "TCoHttpServer: while getting data from backend: %s", CurrentExceptionMessage().data());
        }
    }

    /* TMtHttpServer */

    class TMtHttpServer::TConnection: public TClientRequest, public THttpClient {
    public:
        TConnection(const TMtHttpServer& parent)
            : Parent(parent)
        {
        }

        bool Reply(void*) override {
            ServeRequest(Input(), Output(), NAddr::GetPeerAddr(Socket()).Get(), Parent.Handler);
            return true;
        }

    private:
        const TMtHttpServer& Parent;
    };

    TMtHttpServer::TMtHttpServer(const TOptions& options, THandler handler, IThreadFactory* 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))
    {
    }

    bool TMtHttpServer::Start() {
        return THttpServer::Start();
    }

    void TMtHttpServer::StartOrThrow() {
        if (!Start()) {
            const auto& opts = THttpServer::Options();
            TNetworkAddress addr = opts.Host
                                       ? TNetworkAddress(opts.Host, opts.Port)
                                       : TNetworkAddress(opts.Port);
            ythrow TSystemError(GetErrorCode()) << addr;
        }
    }

    void TMtHttpServer::Stop() {
        THttpServer::Stop();
    }

    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::DispatchRequest(IOutputStream& out, const IHttpRequest& request) { 
        if (strcmp(request.GetPath(), "/") == 0) {
            out << "HTTP/1.1 200 Ok\nConnection: Close\n\n";
            MtHandler(out, request);
        } else
            CoServer.ProcessRequest(out, request);
    }

}