aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/actors/http/http_proxy_incoming.cpp
blob: 3dd06d9bfc32fc767ef54394afbd1e67dbaaab49 (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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
#include "http_proxy.h"
#include "http_proxy_sock_impl.h"

namespace NHttp {

using namespace NActors;

template <typename TSocketImpl>
class TIncomingConnectionActor : public TActor<TIncomingConnectionActor<TSocketImpl>>, public TSocketImpl, virtual public THttpConfig {
public:
    using TBase = TActor<TIncomingConnectionActor<TSocketImpl>>;
    static constexpr bool RecycleRequests = true;

    std::shared_ptr<TPrivateEndpointInfo> Endpoint;
    SocketAddressType Address;
    TList<THttpIncomingRequestPtr> Requests;
    THashMap<THttpIncomingRequestPtr, THttpOutgoingResponsePtr> Responses;
    THttpIncomingRequestPtr CurrentRequest;
    THttpOutgoingResponsePtr CurrentResponse;
    TDeque<THttpIncomingRequestPtr> RecycledRequests;

    THPTimer InactivityTimer;
    static constexpr TDuration InactivityTimeout = TDuration::Minutes(2);
    TEvPollerReady* InactivityEvent = nullptr;

    TPollerToken::TPtr PollerToken;

    TIncomingConnectionActor(
            std::shared_ptr<TPrivateEndpointInfo> endpoint,
            TIntrusivePtr<TSocketDescriptor> socket,
            SocketAddressType address,
            THttpIncomingRequestPtr recycledRequest = nullptr)
        : TBase(&TIncomingConnectionActor::StateAccepting)
        , TSocketImpl(std::move(socket))
        , Endpoint(std::move(endpoint))
        , Address(address)
    {
        if (recycledRequest != nullptr) {
            RecycledRequests.emplace_back(std::move(recycledRequest));
        }
        TSocketImpl::SetNonBlock();
    }

    static constexpr char ActorName[] = "IN_CONNECTION_ACTOR";

    void CleanupRequest(THttpIncomingRequestPtr& request) {
        if (RecycleRequests) {
            request->Clear();
            RecycledRequests.push_back(std::move(request));
        } else {
            request = nullptr;
        }
    }

    void CleanupResponse(THttpOutgoingResponsePtr& response) {
        CleanupRequest(response->Request);
        // TODO: maybe recycle too?
        response = nullptr;
    }

    TAutoPtr<IEventHandle> AfterRegister(const TActorId& self, const TActorId& parent) override {
        return new IEventHandle(self, parent, new TEvents::TEvBootstrap());
    }

    void Die(const TActorContext& ctx) override {
        ctx.Send(Endpoint->Owner, new TEvHttpProxy::TEvHttpConnectionClosed(ctx.SelfID, std::move(RecycledRequests)));
        TSocketImpl::Shutdown();
        TBase::Die(ctx);
    }

protected:
    void Bootstrap(const TActorContext& ctx) {
        InactivityTimer.Reset();
        ctx.Schedule(InactivityTimeout, InactivityEvent = new TEvPollerReady(nullptr, false, false));
        LOG_DEBUG_S(ctx, HttpLog, "(#" << TSocketImpl::GetRawSocket() << "," << Address << ") incoming connection opened");
        OnAccept(ctx);
    }

    void OnAccept(const NActors::TActorContext& ctx) {
        int res;
        bool read = false, write = false;
        if ((res = TSocketImpl::OnAccept(Endpoint, read, write)) != 1) {
            if (-res == EAGAIN) {
                if (PollerToken) {
                    PollerToken->Request(read, write);
                }
                return; // wait for further notifications
            } else {
                LOG_ERROR_S(ctx, HttpLog, "(#" << TSocketImpl::GetRawSocket() << "," << Address << ") connection closed - error in Accept: " << strerror(-res));
                return Die(ctx);
            }
        }
        TBase::Become(&TIncomingConnectionActor::StateConnected);
        ctx.Send(ctx.SelfID, new TEvPollerReady(nullptr, true, true));
    }

    void HandleAccepting(TEvPollerRegisterResult::TPtr ev, const NActors::TActorContext& ctx) {
        PollerToken = std::move(ev->Get()->PollerToken);
        OnAccept(ctx);
    }

    void HandleAccepting(NActors::TEvPollerReady::TPtr, const NActors::TActorContext& ctx) {
        OnAccept(ctx);
    }

    void HandleConnected(TEvPollerReady::TPtr event, const TActorContext& ctx) {
        if (event->Get()->Read) {
            for (;;) {
                if (CurrentRequest == nullptr) {
                    if (RecycleRequests && !RecycledRequests.empty()) {
                        CurrentRequest = std::move(RecycledRequests.front());
                        RecycledRequests.pop_front();
                        CurrentRequest->Address = Address;
                        CurrentRequest->Endpoint = Endpoint;
                    }  else {
                        CurrentRequest = new THttpIncomingRequest(Endpoint, Address);
                    }
                }
                if (!CurrentRequest->EnsureEnoughSpaceAvailable()) {
                    LOG_DEBUG_S(ctx, HttpLog, "(#" << TSocketImpl::GetRawSocket() << "," << Address << ") connection closed - not enough space available");
                    return Die(ctx);
                }
                ssize_t need = CurrentRequest->Avail();
                bool read = false, write = false;
                ssize_t res = TSocketImpl::Recv(CurrentRequest->Pos(), need, read, write);
                if (res > 0) {
                    InactivityTimer.Reset();
                    CurrentRequest->Advance(res);
                    if (CurrentRequest->IsDone()) {
                        Requests.emplace_back(CurrentRequest);
                        CurrentRequest->Timer.Reset();
                        if (CurrentRequest->IsReady()) {
                            LOG_DEBUG_S(ctx, HttpLog, "(#" << TSocketImpl::GetRawSocket() << "," << Address << ") -> (" << CurrentRequest->Method << " " << CurrentRequest->URL << ")");
                            ctx.Send(Endpoint->Proxy, new TEvHttpProxy::TEvHttpIncomingRequest(CurrentRequest));
                            CurrentRequest = nullptr;
                        } else if (CurrentRequest->IsError()) {
                            LOG_DEBUG_S(ctx, HttpLog, "(#" << TSocketImpl::GetRawSocket() << "," << Address << ") -! (" << CurrentRequest->Method << " " << CurrentRequest->URL << ")");
                            bool success = Respond(CurrentRequest->CreateResponseBadRequest(), ctx);
                            if (!success) {
                                return;
                            }
                            CurrentRequest = nullptr;
                        }
                    }
                } else if (-res == EAGAIN || -res == EWOULDBLOCK) {
                    if (PollerToken) {
                        if (!read && !write) {
                            read = true;
                        }
                        PollerToken->Request(read, write);
                    }
                    break;
                } else if (-res == EINTR) {
                    continue;
                } else if (!res) {
                    // connection closed
                    LOG_DEBUG_S(ctx, HttpLog, "(#" << TSocketImpl::GetRawSocket() << "," << Address << ") connection closed");
                    return Die(ctx);
                } else {
                    LOG_ERROR_S(ctx, HttpLog, "(#" << TSocketImpl::GetRawSocket() << "," << Address << ") connection closed - error in Receive: " << strerror(-res));
                    return Die(ctx);
                }
            }
            if (event->Get() == InactivityEvent) {
                const TDuration passed = TDuration::Seconds(std::abs(InactivityTimer.Passed()));
                if (passed >= InactivityTimeout) {
                    LOG_DEBUG_S(ctx, HttpLog, "(#" << TSocketImpl::GetRawSocket() << "," << Address << ") connection closed by inactivity timeout");
                    return Die(ctx); // timeout
                } else {
                    ctx.Schedule(InactivityTimeout - passed, InactivityEvent = new TEvPollerReady(nullptr, false, false));
                }
            }
        }
        if (event->Get()->Write) {
            FlushOutput(ctx);
        }
    }

    void HandleConnected(TEvPollerRegisterResult::TPtr ev, const TActorContext& /*ctx*/) {
        PollerToken = std::move(ev->Get()->PollerToken);
        PollerToken->Request(true, true);
    }

    void HandleConnected(TEvHttpProxy::TEvHttpOutgoingResponse::TPtr event, const TActorContext& ctx) {
        Respond(event->Get()->Response, ctx);
    }

    bool Respond(THttpOutgoingResponsePtr response, const TActorContext& ctx) {
        THttpIncomingRequestPtr request = response->GetRequest();
        response->Finish();
        LOG_DEBUG_S(ctx, HttpLog, "(#" << TSocketImpl::GetRawSocket() << "," << Address << ") <- (" << response->Status << " " << response->Message << ")");
        if (response->Status != "200" && response->Status != "404") {
            static constexpr size_t MAX_LOGGED_SIZE = 1024;
            LOG_DEBUG_S(ctx, HttpLog,
                        "(#"
                        << TSocketImpl::GetRawSocket()
                        << ","
                        << Address
                        << ") Request: "
                        << request->GetObfuscatedData().substr(0, MAX_LOGGED_SIZE));
            LOG_DEBUG_S(ctx, HttpLog,
                        "(#"
                        << TSocketImpl::GetRawSocket()
                        << ","
                        << Address
                        << ") Response: "
                        << TString(response->GetRawData()).substr(0, MAX_LOGGED_SIZE));
        }
        THolder<TEvHttpProxy::TEvReportSensors> sensors(BuildIncomingRequestSensors(request, response));
        ctx.Send(Endpoint->Owner, sensors.Release());
        if (request == Requests.front() && CurrentResponse == nullptr) {
            CurrentResponse = response;
            return FlushOutput(ctx);
        } else {
            // we are ahead of our pipeline
            Responses.emplace(request, response);
            return true;
        }
    }

    bool FlushOutput(const TActorContext& ctx) {
        while (CurrentResponse != nullptr) {
            size_t size = CurrentResponse->Size();
            if (size == 0) {
                Y_VERIFY(Requests.front() == CurrentResponse->GetRequest());
                bool close = CurrentResponse->IsConnectionClose();
                Requests.pop_front();
                CleanupResponse(CurrentResponse);
                if (!Requests.empty()) {
                    auto it = Responses.find(Requests.front());
                    if (it != Responses.end()) {
                        CurrentResponse = it->second;
                        Responses.erase(it);
                        continue;
                    } else {
                        LOG_ERROR_S(ctx, HttpLog, "(#" << TSocketImpl::GetRawSocket() << "," << Address << ") connection closed - FlushOutput request not found");
                        Die(ctx);
                        return false;
                    }
                } else {
                    if (close) {
                        LOG_DEBUG_S(ctx, HttpLog, "(#" << TSocketImpl::GetRawSocket() << "," << Address << ") connection closed");
                        Die(ctx);
                        return false;
                    } else {
                        continue;
                    }
                }
            }
            bool read = false, write = false;
            ssize_t res = TSocketImpl::Send(CurrentResponse->Data(), size, read, write);
            if (res > 0) {
                CurrentResponse->ChopHead(res);
            } else if (-res == EINTR) {
                continue;
            } else if (-res == EAGAIN || -res == EWOULDBLOCK) {
                if (PollerToken) {
                    if (!read && !write) {
                        write = true;
                    }
                    PollerToken->Request(read, write);
                }
                break;
            } else {
                CleanupResponse(CurrentResponse);
                LOG_ERROR_S(ctx, HttpLog, "(#" << TSocketImpl::GetRawSocket() << "," << Address << ") connection closed - error in FlushOutput: " << strerror(-res));
                Die(ctx);
                return false;
            }
        }
        return true;
    }

    STFUNC(StateAccepting) {
        switch (ev->GetTypeRewrite()) {
            CFunc(TEvents::TEvBootstrap::EventType, Bootstrap);
            HFunc(TEvPollerReady, HandleAccepting);
            HFunc(TEvPollerRegisterResult, HandleAccepting);
        }
    }

    STFUNC(StateConnected) {
        switch (ev->GetTypeRewrite()) {
            HFunc(TEvPollerReady, HandleConnected);
            HFunc(TEvHttpProxy::TEvHttpOutgoingResponse, HandleConnected);
            HFunc(TEvPollerRegisterResult, HandleConnected);
        }
    }
};

IActor* CreateIncomingConnectionActor(
        std::shared_ptr<TPrivateEndpointInfo> endpoint,
        TIntrusivePtr<TSocketDescriptor> socket,
        THttpConfig::SocketAddressType address,
        THttpIncomingRequestPtr recycledRequest) {
    if (endpoint->Secure) {
        return new TIncomingConnectionActor<TSecureSocketImpl>(std::move(endpoint), std::move(socket), address, std::move(recycledRequest));
    } else {
        return new TIncomingConnectionActor<TPlainSocketImpl>(std::move(endpoint), std::move(socket), address, std::move(recycledRequest));
    }
}

}