87 return static_cast<Derived<HandlerType>&
>(*this);
94 explicit SendLambda(
HttpBase& self) : self(self)
98 template <
bool IsRequest,
typename Body,
typename Fields>
100 operator()(http::message<IsRequest, Body, Fields>&& msg)
const
107 auto sp = std::make_shared<http::message<IsRequest, Body, Fields>>(std::move(msg));
114 self.derived().stream(),
116 boost::beast::bind_front_handler(&HttpBase::onWrite, self.derived().shared_from_this(), sp->need_eof())
121 std::shared_ptr<void> res_;
123 std::shared_ptr<AdminVerificationStrategy> adminVerification_;
124 std::shared_ptr<ProxyIpResolver> proxyIpResolver_;
127 boost::beast::flat_buffer buffer_;
128 http::request<http::string_body> req_;
129 std::reference_wrapper<dosguard::DOSGuardInterface> dosGuard_;
130 std::shared_ptr<HandlerType>
const handler_;
135 httpFail(boost::beast::error_code ec,
char const* what)
154 if (ec == boost::asio::ssl::error::stream_truncated)
157 if (!ec_ && ec != boost::asio::error::operation_aborted) {
159 LOG(perfLog_.
info()) <<
tag() <<
": " << what <<
": " << ec.message();
160 boost::beast::get_lowest_layer(derived().stream()).socket().close(ec);
166 std::string
const& ip,
167 std::reference_wrapper<util::TagDecoratorFactory const> tagFactory,
168 std::shared_ptr<AdminVerificationStrategy> adminVerification,
169 std::shared_ptr<ProxyIpResolver> proxyIpResolver,
170 std::reference_wrapper<dosguard::DOSGuardInterface> dosGuard,
171 std::shared_ptr<HandlerType> handler,
172 boost::beast::flat_buffer buffer
176 , adminVerification_(std::move(adminVerification))
177 , proxyIpResolver_(std::move(proxyIpResolver))
178 , buffer_(std::move(buffer))
179 , dosGuard_(dosGuard)
180 , handler_(std::move(handler))
182 LOG(perfLog_.
debug()) <<
tag() <<
"http session created";
183 dosGuard_.get().increment(ip);
188 LOG(perfLog_.
debug()) <<
tag() <<
"http session closed";
190 dosGuard_.get().decrement(clientIp_);
203 boost::beast::get_lowest_layer(derived().stream()).expires_after(std::chrono::seconds(30));
209 boost::beast::bind_front_handler(&HttpBase::onRead, derived().shared_from_this())
214 onRead(boost::beast::error_code ec, [[maybe_unused]] std::size_t bytesTransferred)
216 if (ec == http::error::end_of_stream)
217 return derived().doClose();
220 return httpFail(ec,
"read");
222 if (req_.method() == http::verb::get and req_.target() ==
"/health")
223 return sender_(httpResponse(http::status::ok,
"text/html", kHEALTH_CHECK_HTML));
225 if (
auto resolvedIp = proxyIpResolver_->resolveClientIp(clientIp_, req_); resolvedIp != clientIp_) {
226 LOG(log_.
info()) <<
tag() <<
"Detected a forwarded request from proxy. Proxy ip: " << clientIp_
227 <<
". Resolved client ip: " << resolvedIp;
228 dosGuard_.get().decrement(clientIp_);
229 clientIp_ = std::move(resolvedIp);
230 dosGuard_.get().increment(clientIp_);
234 ConnectionBase::isAdmin_ = adminVerification_->isAdmin(req_, clientIp_);
236 if (boost::beast::websocket::is_upgrade(req_)) {
237 if (dosGuard_.get().isOk(clientIp_)) {
239 boost::beast::get_lowest_layer(derived().stream()).expires_never();
242 return derived().upgrade();
245 return sender_(httpResponse(http::status::too_many_requests,
"text/html",
"Too many requests"));
248 if (
auto response = util::prometheus::handlePrometheusRequest(req_,
isAdmin()); response.has_value())
249 return sender_(std::move(response.value()));
251 if (req_.method() != http::verb::post) {
252 return sender_(httpResponse(http::status::bad_request,
"text/html",
"Expected a POST request"));
255 LOG(log_.
info()) <<
tag() <<
"Received request from ip = " << clientIp_;
258 (*handler_)(req_.body(), derived().shared_from_this());
259 }
catch (std::exception
const&) {
260 return sender_(httpResponse(
261 http::status::internal_server_error,
263 boost::json::serialize(
rpc::makeError(rpc::RippledError::rpcINTERNAL))
271 sender_(httpResponse(
272 http::status::service_unavailable,
274 boost::json::serialize(
rpc::makeError(rpc::RippledError::rpcSLOW_DOWN))
284 send(std::string&& msg, http::status status = http::status::ok)
override
286 if (!dosGuard_.get().add(clientIp_, msg.size())) {
287 auto jsonResponse = boost::json::parse(msg).as_object();
288 jsonResponse[
"warning"] =
"load";
289 if (jsonResponse.contains(
"warnings") && jsonResponse[
"warnings"].is_array()) {
290 jsonResponse[
"warnings"].as_array().push_back(
rpc::makeWarning(rpc::WarnRpcRateLimit));
292 jsonResponse[
"warnings"] = boost::json::array{
rpc::makeWarning(rpc::WarnRpcRateLimit)};
296 msg = boost::json::serialize(jsonResponse);
298 sender_(httpResponse(status,
"application/json", std::move(msg)));
304 ASSERT(
false,
"SubscriptionContext can't be created for a HTTP connection");
309 onWrite(
bool close, boost::beast::error_code ec, std::size_t bytesTransferred)
311 boost::ignore_unused(bytesTransferred);
314 return httpFail(ec,
"write");
319 return derived().doClose();
326 http::response<http::string_body>
327 httpResponse(http::status status, std::string contentType, std::string message)
const
329 http::response<http::string_body> res{status, req_.version()};
330 res.set(http::field::server,
"clio-server-" + util::build::getClioVersionString());
331 res.set(http::field::content_type, contentType);
332 res.keep_alive(req_.keep_alive());
333 res.body() = std::move(message);
334 res.prepare_payload();