aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/actors/http/http_proxy.h
blob: afd0170997e800b4efc8c6b9aee5904d643be96d (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
#pragma once
#include <library/cpp/actors/core/actorsystem.h>
#include <library/cpp/actors/core/actor.h>
#include <library/cpp/actors/core/hfunc.h>
#include <library/cpp/actors/core/events.h>
#include <library/cpp/actors/core/event_local.h>
#include <library/cpp/actors/core/actor_bootstrapped.h>
#include <library/cpp/actors/core/log.h>
#include <library/cpp/actors/interconnect/poller_actor.h>
#include <library/cpp/dns/cache.h>
#include <library/cpp/monlib/metrics/metric_registry.h>
#include <util/generic/variant.h>
#include "http.h"
#include "http_proxy_ssl.h"

namespace NHttp {

struct TSocketDescriptor : NActors::TSharedDescriptor, THttpConfig {
    SocketType Socket;

    int GetDescriptor() override {
        return static_cast<SOCKET>(Socket);
    }
};

struct TEvHttpProxy {
    enum EEv {
        EvAddListeningPort = EventSpaceBegin(NActors::TEvents::ES_HTTP),
        EvConfirmListen,
        EvRegisterHandler,
        EvHttpIncomingRequest,
        EvHttpOutgoingRequest,
        EvHttpIncomingResponse,
        EvHttpOutgoingResponse,
        EvHttpConnectionOpened,
        EvHttpConnectionClosed,
        EvHttpAcceptorClosed,
        EvResolveHostRequest,
        EvResolveHostResponse,
        EvReportSensors,
        EvEnd
    };

    static_assert(EvEnd < EventSpaceEnd(NActors::TEvents::ES_HTTP), "ES_HTTP event space is too small.");

    struct TEvAddListeningPort : NActors::TEventLocal<TEvAddListeningPort, EvAddListeningPort> {
        TIpPort Port;
        TString WorkerName;
        bool Secure = false;
        TString CertificateFile;
        TString PrivateKeyFile;
        TString SslCertificatePem;

        TEvAddListeningPort(TIpPort port)
            : Port(port)
        {}

        TEvAddListeningPort(TIpPort port, const TString& workerName)
            : Port(port)
            , WorkerName(workerName)
        {}
    };

    struct TEvConfirmListen : NActors::TEventLocal<TEvConfirmListen, EvConfirmListen> {
        THttpConfig::SocketAddressType Address;

        TEvConfirmListen(const THttpConfig::SocketAddressType& address)
            : Address(address)
        {}
    };

    struct TEvRegisterHandler : NActors::TEventLocal<TEvRegisterHandler, EvRegisterHandler> {
        TString Path;
        TActorId Handler;

        TEvRegisterHandler(const TString& path, const TActorId& handler)
            : Path(path)
            , Handler(handler)
        {}
    };

    struct TEvHttpIncomingRequest : NActors::TEventLocal<TEvHttpIncomingRequest, EvHttpIncomingRequest> {
        THttpIncomingRequestPtr Request;

        TEvHttpIncomingRequest(THttpIncomingRequestPtr request)
            : Request(std::move(request))
        {}
    };

    struct TEvHttpOutgoingRequest : NActors::TEventLocal<TEvHttpOutgoingRequest, EvHttpOutgoingRequest> {
        THttpOutgoingRequestPtr Request;
        TDuration Timeout;

        TEvHttpOutgoingRequest(THttpOutgoingRequestPtr request)
            : Request(std::move(request))
        {}

        TEvHttpOutgoingRequest(THttpOutgoingRequestPtr request, TDuration timeout)
            : Request(std::move(request))
            , Timeout(timeout)
        {}
    };

    struct TEvHttpIncomingResponse : NActors::TEventLocal<TEvHttpIncomingResponse, EvHttpIncomingResponse> {
        THttpOutgoingRequestPtr Request;
        THttpIncomingResponsePtr Response;
        TString Error;

        TEvHttpIncomingResponse(THttpOutgoingRequestPtr request, THttpIncomingResponsePtr response, const TString& error)
            : Request(std::move(request))
            , Response(std::move(response))
            , Error(error)
        {}

        TEvHttpIncomingResponse(THttpOutgoingRequestPtr request, THttpIncomingResponsePtr response)
            : Request(std::move(request))
            , Response(std::move(response))
        {}

        TString GetError() const {
            TStringBuilder error;
            if (Response != nullptr && !Response->Status.StartsWith('2')) {
                error << Response->Status << ' ' << Response->Message;
            }
            if (!Error.empty()) {
                if (!error.empty()) {
                    error << ';';
                }
                error << Error;
            }
            return error;
        }
    };

    struct TEvHttpOutgoingResponse : NActors::TEventLocal<TEvHttpOutgoingResponse, EvHttpOutgoingResponse> {
        THttpOutgoingResponsePtr Response;

        TEvHttpOutgoingResponse(THttpOutgoingResponsePtr response)
            : Response(std::move(response))
        {}
    };

    struct TEvHttpConnectionOpened : NActors::TEventLocal<TEvHttpConnectionOpened, EvHttpConnectionOpened> {
        TString PeerAddress;
        TActorId ConnectionID;

        TEvHttpConnectionOpened(const TString& peerAddress, const TActorId& connectionID)
            : PeerAddress(peerAddress)
            , ConnectionID(connectionID)
        {}
    };

    struct TEvHttpConnectionClosed : NActors::TEventLocal<TEvHttpConnectionClosed, EvHttpConnectionClosed> {
        TActorId ConnectionID;
        TDeque<THttpIncomingRequestPtr> RecycledRequests;

        TEvHttpConnectionClosed(const TActorId& connectionID)
            : ConnectionID(connectionID)
        {}

        TEvHttpConnectionClosed(const TActorId& connectionID, TDeque<THttpIncomingRequestPtr> recycledRequests)
            : ConnectionID(connectionID)
            , RecycledRequests(std::move(recycledRequests))
        {}
    };

    struct TEvHttpAcceptorClosed : NActors::TEventLocal<TEvHttpAcceptorClosed, EvHttpAcceptorClosed> {
        TActorId ConnectionID;

        TEvHttpAcceptorClosed(const TActorId& connectionID)
            : ConnectionID(connectionID)
        {}
    };

    struct TEvResolveHostRequest : NActors::TEventLocal<TEvResolveHostRequest, EvResolveHostRequest> {
        TString Host;

        TEvResolveHostRequest(const TString& host)
            : Host(host)
        {}
    };

    struct TEvResolveHostResponse : NActors::TEventLocal<TEvResolveHostResponse, EvResolveHostResponse> {
        TString Host;
        TSockAddrInet6 Address;
        TString Error;

        TEvResolveHostResponse(const TString& host, const TSockAddrInet6& address)
            : Host(host)
            , Address(address)
        {}

        TEvResolveHostResponse(const TString& error)
            : Error(error)
        {}
    };

    struct TEvReportSensors : NActors::TEventLocal<TEvReportSensors, EvReportSensors> {
        TString Direction;
        TString Host;
        TString Url;
        TString Status;
        TDuration Time;

        TEvReportSensors(
                TStringBuf direction,
                TStringBuf host,
                TStringBuf url,
                TStringBuf status,
                TDuration time)
            : Direction(direction)
            , Host(host)
            , Url(url)
            , Status(status)
            , Time(time)
        {}
    };
};

struct TEndpointInfo {
    TActorId Proxy;
    TActorId Owner;
    TString WorkerName;
    bool Secure;
    TSslHelpers::TSslHolder<SSL_CTX> SecureContext;
};

NActors::IActor* CreateHttpProxy(NMonitoring::TMetricRegistry& sensors);
NActors::IActor* CreateHttpAcceptorActor(const TActorId& owner, const TActorId& poller);
NActors::IActor* CreateOutgoingConnectionActor(const TActorId& owner, const TString& host, bool secure, const TActorId& poller);
NActors::IActor* CreateIncomingConnectionActor(
        const TEndpointInfo& endpoint,
        TIntrusivePtr<TSocketDescriptor> socket,
        THttpConfig::SocketAddressType address,
        THttpIncomingRequestPtr recycledRequest = nullptr);
TEvHttpProxy::TEvReportSensors* BuildOutgoingRequestSensors(const THttpOutgoingRequestPtr& request, const THttpIncomingResponsePtr& response);
TEvHttpProxy::TEvReportSensors* BuildIncomingRequestSensors(const THttpIncomingRequestPtr& request, const THttpOutgoingResponsePtr& response);

}