summaryrefslogtreecommitdiff
path: root/http/server_sent_event.hpp
blob: 58659c84a025ccf8331516b77fa710ccbda2e427 (plain)
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
#pragma once
#include "async_resolve.hpp"
#include "async_resp.hpp"
#include "http_request.hpp"
#include "http_response.hpp"

#include <boost/algorithm/string/predicate.hpp>
#include <boost/asio/buffer.hpp>
#include <boost/asio/steady_timer.hpp>
#include <boost/beast/core/multi_buffer.hpp>
#include <boost/beast/http/buffer_body.hpp>
#include <boost/beast/websocket.hpp>

#include <array>
#include <functional>

#ifdef BMCWEB_ENABLE_SSL
#include <boost/beast/websocket/ssl.hpp>
#endif

namespace crow
{

namespace sse_socket
{
static constexpr const std::array<const char*, 1> sseRoutes = {
    "/redfish/v1/EventService/SSE"};

struct Connection : std::enable_shared_from_this<Connection>
{
  public:
    explicit Connection(const crow::Request& reqIn) : req(reqIn) {}

    Connection(const Connection&) = delete;
    Connection(Connection&&) = delete;
    Connection& operator=(const Connection&) = delete;
    Connection& operator=(const Connection&&) = delete;
    virtual ~Connection() = default;

    virtual boost::asio::io_context& getIoContext() = 0;
    virtual void sendSSEHeader() = 0;
    virtual void completeRequest(crow::Response& thisRes) = 0;
    virtual void close(std::string_view msg = "quit") = 0;
    virtual void sendEvent(std::string_view id, std::string_view msg) = 0;

    crow::Request req;
};

template <typename Adaptor>
class ConnectionImpl : public Connection
{
  public:
    ConnectionImpl(
        const crow::Request& reqIn, Adaptor adaptorIn,
        std::function<void(std::shared_ptr<Connection>&, const crow::Request&,
                           const std::shared_ptr<bmcweb::AsyncResp>&)>
            openHandlerIn,
        std::function<void(std::shared_ptr<Connection>&)> closeHandlerIn) :
        Connection(reqIn),
        adaptor(std::move(adaptorIn)), openHandler(std::move(openHandlerIn)),
        closeHandler(std::move(closeHandlerIn))
    {
        BMCWEB_LOG_DEBUG << "SseConnectionImpl: SSE constructor " << this;
    }

    ConnectionImpl(const ConnectionImpl&) = delete;
    ConnectionImpl(const ConnectionImpl&&) = delete;
    ConnectionImpl& operator=(const ConnectionImpl&) = delete;
    ConnectionImpl& operator=(const ConnectionImpl&&) = delete;

    ~ConnectionImpl() override
    {
        BMCWEB_LOG_DEBUG << "SSE ConnectionImpl: SSE destructor " << this;
    }

    boost::asio::io_context& getIoContext() override
    {
        return static_cast<boost::asio::io_context&>(
            adaptor.get_executor().context());
    }

    void start()
    {
        if (openHandler)
        {
            auto asyncResp = std::make_shared<bmcweb::AsyncResp>();
            std::shared_ptr<Connection> self = this->shared_from_this();

            asyncResp->res.setCompleteRequestHandler(
                [self(shared_from_this())](crow::Response& thisRes) {
                if (thisRes.resultInt() != 200)
                {
                    self->completeRequest(thisRes);
                }
            });

            openHandler(self, req, asyncResp);
        }
    }

    void close(const std::string_view msg) override
    {
        BMCWEB_LOG_DEBUG << "Closing SSE connection " << this << " - " << msg;
        boost::beast::get_lowest_layer(adaptor).close();

        // send notification to handler for cleanup
        if (closeHandler)
        {
            std::shared_ptr<Connection> self = shared_from_this();
            closeHandler(self);
        }
    }

    void sendSSEHeader() override
    {
        BMCWEB_LOG_DEBUG << "Starting SSE connection";
        auto asyncResp = std::make_shared<bmcweb::AsyncResp>();
        using BodyType = boost::beast::http::buffer_body;
        auto response =
            std::make_shared<boost::beast::http::response<BodyType>>(
                boost::beast::http::status::ok, 11);

        serializer.emplace(*asyncResp->res.stringResponse);

        response->set(boost::beast::http::field::content_type,
                      "text/event-stream");
        response->body().more = true;

        boost::beast::http::async_write_header(
            adaptor, *serializer,
            std::bind_front(&ConnectionImpl::sendSSEHeaderCallback, this,
                            shared_from_this()));
    }

    void sendSSEHeaderCallback(const std::shared_ptr<Connection>& /*self*/,
                               const boost::beast::error_code& ec,
                               const std::size_t& /*unused*/)
    {
        if (ec)
        {
            BMCWEB_LOG_ERROR << "Error sending header" << ec;
            close("async_write_header failed");
            return;
        }
        BMCWEB_LOG_DEBUG << "SSE header sent - Connection established";

        serializer.reset();

        // SSE stream header sent, So let us setup monitor.
        // Any read data on this stream will be error in case of SSE.
        setupRead();
    }

    void setupRead()
    {
        std::weak_ptr<Connection> weakSelf = weak_from_this();

        boost::beast::http::async_read_some(
            adaptor, outputBuffer, *parser,
            std::bind_front(&ConnectionImpl::setupReadCallback, this,
                            weak_from_this()));
    }

    void setupReadCallback(const std::weak_ptr<Connection>& weakSelf,
                           const boost::system::error_code& ec,
                           size_t bytesRead)
    {
        std::shared_ptr<Connection> self = weakSelf.lock();
        BMCWEB_LOG_DEBUG << "async_read_some: Read " << bytesRead << " bytes";
        if (ec)
        {
            BMCWEB_LOG_ERROR << "Read error: " << ec;
        }

        // After establishing SSE stream, Reading data on this
        // stream means client is disobeys the SSE protocol.
        // Read the data to avoid buffer attacks and close connection.

        self->close("Close SSE connection");
    }

    void doWrite()
    {
        onTimeout();

        if (doingWrite)
        {
            return;
        }
        if (inputBuffer.size() == 0)
        {
            BMCWEB_LOG_DEBUG << "inputBuffer is empty... Bailing out";
            return;
        }
        doingWrite = true;

        adaptor.async_write_some(
            inputBuffer.data(),
            std::bind_front(&ConnectionImpl::doWriteCallback, this,
                            shared_from_this()));
    }

    void doWriteCallback(const std::shared_ptr<Connection>& /*self*/,
                         const boost::beast::error_code& ec,
                         const size_t bytesTransferred)
    {
        doingWrite = false;
        inputBuffer.consume(bytesTransferred);

        if (ec == boost::asio::error::eof)
        {
            BMCWEB_LOG_ERROR << "async_write_some() SSE stream closed";
            close("SSE stream closed");
            return;
        }

        if (ec)
        {
            BMCWEB_LOG_ERROR << "async_write_some() failed: " << ec.message();
            close("async_write_some failed");
            return;
        }
        BMCWEB_LOG_DEBUG << "async_write_some() bytes transferred: "
                         << bytesTransferred;

        doWrite();
    }

    void completeRequest(crow::Response& thisRes) override
    {
        auto asyncResp = std::make_shared<bmcweb::AsyncResp>();
        asyncResp->res = std::move(thisRes);

        if (asyncResp->res.body().empty() && !asyncResp->res.jsonValue.empty())
        {
            asyncResp->res.addHeader(boost::beast::http::field::content_type,
                                     "application/json");
            asyncResp->res.body() = asyncResp->res.jsonValue.dump(
                2, ' ', true, nlohmann::json::error_handler_t::replace);
        }

        asyncResp->res.preparePayload();

        serializer.emplace(*asyncResp->res.stringResponse);

        boost::beast::http::async_write_some(
            adaptor, *serializer,
            std::bind_front(&ConnectionImpl::completeRequestCallback, this,
                            shared_from_this()));
    }

    void completeRequestCallback(const std::shared_ptr<Connection>& /*self*/,
                                 const boost::system::error_code& ec,
                                 std::size_t bytesTransferred)
    {
        auto asyncResp = std::make_shared<bmcweb::AsyncResp>();
        BMCWEB_LOG_DEBUG << this << " async_write " << bytesTransferred
                         << " bytes";
        if (ec)
        {
            BMCWEB_LOG_DEBUG << this << " from async_write failed";
            return;
        }

        BMCWEB_LOG_DEBUG << this << " Closing SSE connection - Request invalid";
        serializer.reset();
        close("Request invalid");
        asyncResp->res.releaseCompleteRequestHandler();
    }

    void sendEvent(std::string_view id, std::string_view msg) override
    {
        if (msg.empty())
        {
            BMCWEB_LOG_DEBUG << "Empty data, bailing out.";
            return;
        }

        dataFormat(id);

        doWrite();
    }

    void dataFormat(std::string_view id)
    {
        std::string_view msg;
        std::string rawData;
        if (!id.empty())
        {
            rawData += "id: ";
            rawData.append(id.begin(), id.end());
            rawData += "\n";
        }

        rawData += "data: ";
        for (char character : msg)
        {
            rawData += character;
            if (character == '\n')
            {
                rawData += "data: ";
            }
        }
        rawData += "\n\n";

        boost::asio::buffer_copy(inputBuffer.prepare(rawData.size()),
                                 boost::asio::buffer(rawData));
        inputBuffer.commit(rawData.size());
    }

    void onTimeout()
    {
        boost::asio::steady_timer timer(ioc);
        std::weak_ptr<Connection> weakSelf = weak_from_this();
        timer.expires_after(std::chrono::seconds(30));
        timer.async_wait(std::bind_front(&ConnectionImpl::onTimeoutCallback,
                                         this, weak_from_this()));
    }

    void onTimeoutCallback(const std::weak_ptr<Connection>& weakSelf,
                           const boost::system::error_code ec)
    {
        std::shared_ptr<Connection> self = weakSelf.lock();
        if (!self)
        {
            BMCWEB_LOG_CRITICAL << self << " Failed to capture connection";
            return;
        }

        if (ec == boost::asio::error::operation_aborted)
        {
            BMCWEB_LOG_DEBUG << "operation aborted";
            // Canceled wait means the path succeeeded.
            return;
        }
        if (ec)
        {
            BMCWEB_LOG_CRITICAL << self << " timer failed " << ec;
        }

        BMCWEB_LOG_WARNING << self << "Connection timed out, closing";

        self->close("closing connection");
    }

  private:
    Adaptor adaptor;

    boost::beast::multi_buffer outputBuffer;
    boost::beast::multi_buffer inputBuffer;

    std::optional<boost::beast::http::response_serializer<
        boost::beast::http::string_body>>
        serializer;
    boost::asio::io_context& ioc =
        crow::connections::systemBus->get_io_context();
    bool doingWrite = false;
    std::optional<
        boost::beast::http::request_parser<boost::beast::http::string_body>>
        parser;

    std::function<void(std::shared_ptr<Connection>&, const crow::Request&,
                       const std::shared_ptr<bmcweb::AsyncResp>&)>
        openHandler;
    std::function<void(std::shared_ptr<Connection>&)> closeHandler;
};
} // namespace sse_socket
} // namespace crow