forked from ibm-openbmc/bmcweb
-
Notifications
You must be signed in to change notification settings - Fork 1
/
http_stream.hpp
153 lines (137 loc) · 4.94 KB
/
http_stream.hpp
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
#pragma once
#include "http/http_request.hpp"
#include "http/http_response.hpp"
#include <boost/algorithm/string/predicate.hpp>
#include <boost/asio/io_context.hpp>
#include <boost/asio/steady_timer.hpp>
#include <boost/beast/core/ostream.hpp>
#include <boost/beast/http/basic_dynamic_body.hpp>
namespace crow
{
namespace streaming_response
{
struct Connection : std::enable_shared_from_this<Connection>
{
public:
explicit Connection(const crow::Request& reqIn) : req(reqIn.req)
{}
virtual void sendMessage(const boost::asio::mutable_buffer& buffer,
std::function<void()> handler) = 0;
virtual void close() = 0;
virtual boost::asio::io_context* getIoContext() = 0;
virtual void sendStreamHeaders(const std::string& streamDataSize,
const std::string& contentType) = 0;
virtual void sendStreamErrorStatus(boost::beast::http::status status) = 0;
virtual ~Connection() = default;
boost::beast::http::request<boost::beast::http::string_body> req;
crow::DynamicResponse streamres;
};
template <typename Adaptor>
class ConnectionImpl : public Connection
{
public:
ConnectionImpl(const crow::Request& reqIn, Adaptor&& adaptorIn,
std::function<void(Connection&)> openHandler,
std::function<void(Connection&, const std::string&, bool)>
messageHandler,
std::function<void(Connection&)> closeHandler,
std::function<void(Connection&)> errorHandler) :
Connection(reqIn),
adaptor(std::move(adaptorIn)), waitTimer(*reqIn.ioService),
openHandler(std::move(openHandler)),
messageHandler(std::move(messageHandler)),
closeHandler(std::move(closeHandler)),
errorHandler(std::move(errorHandler)), req(reqIn)
{}
boost::asio::io_context* getIoContext() override
{
return req.ioService;
}
void start()
{
streamres.completeRequestHandler = [this, self(shared_from_this())] {
BMCWEB_LOG_DEBUG << "running completeRequestHandler";
this->close();
};
openHandler(*this);
}
void sendStreamErrorStatus(boost::beast::http::status status) override
{
streamres.result(status);
boost::beast::http::async_write(
adaptor, *streamres.bufferResponse,
[this, self(shared_from_this())](
const boost::system::error_code& ec2, std::size_t) {
if (ec2)
{
BMCWEB_LOG_DEBUG << "Error while writing on socket" << ec2;
close();
return;
}
});
}
void sendStreamHeaders(const std::string& streamDataSize,
const std::string& contentType) override
{
streamres.addHeader("Content-Length", streamDataSize);
streamres.addHeader("Content-Type", contentType);
boost::beast::http::async_write(
adaptor, *streamres.bufferResponse,
[this, self(shared_from_this())](
const boost::system::error_code& ec2, std::size_t) {
if (ec2)
{
BMCWEB_LOG_DEBUG << "Error while writing on socket" << ec2;
close();
return;
}
});
}
void sendMessage(const boost::asio::mutable_buffer& buffer,
std::function<void()> handler) override
{
if (buffer.size())
{
this->handlerFunc = handler;
auto bytes = boost::asio::buffer_copy(
streamres.bufferResponse->body().prepare(buffer.size()),
buffer);
streamres.bufferResponse->body().commit(bytes);
doWrite();
}
}
void close() override
{
streamres.end();
boost::beast::get_lowest_layer(adaptor).close();
closeHandler(*this);
}
void doWrite()
{
boost::asio::async_write(
adaptor, streamres.bufferResponse->body().data(),
[this, self(shared_from_this())](boost::beast::error_code ec,
std::size_t bytesWritten) {
streamres.bufferResponse->body().consume(bytesWritten);
if (ec)
{
BMCWEB_LOG_DEBUG << "Error in async_write " << ec;
close();
return;
}
(handlerFunc)();
});
}
private:
Adaptor adaptor;
boost::asio::steady_timer waitTimer;
bool doingWrite = false;
std::function<void(Connection&)> openHandler;
std::function<void(Connection&, const std::string&, bool)> messageHandler;
std::function<void(Connection&)> closeHandler;
std::function<void(Connection&)> errorHandler;
std::function<void()> handlerFunc;
crow::Request req;
};
} // namespace streaming_response
} // namespace crow