101 Derived<HandlerType>&
104 return static_cast<Derived<HandlerType>&
>(*this);
111 explicit SendLambda(HttpBase& self) : self(self)
115 template <
bool IsRequest,
typename Body,
typename Fields>
117 operator()(http::message<IsRequest, Body, Fields>&& msg)
const
124 auto sp = std::make_shared<http::message<IsRequest, Body, Fields>>(std::move(msg));
131 self.derived().stream(),
133 boost::beast::bind_front_handler(&HttpBase::onWrite, self.derived().shared_from_this(), sp->need_eof())
138 std::shared_ptr<void> res_;
140 std::shared_ptr<AdminVerificationStrategy> adminVerification_;
141 std::shared_ptr<ProxyIpResolver> proxyIpResolver_;
144 boost::beast::flat_buffer buffer_;
145 http::request<http::string_body> req_;
146 std::reference_wrapper<dosguard::DOSGuardInterface> dosGuard_;
147 std::shared_ptr<HandlerType>
const handler_;
148 std::reference_wrapper<data::LedgerCacheInterface const> cache_;
153 httpFail(boost::beast::error_code ec,
char const* what)
172 if (ec == boost::asio::ssl::error::stream_truncated)
175 if (!ec_ && ec != boost::asio::error::operation_aborted) {
177 LOG(perfLog_.info()) <<
tag() <<
": " << what <<
": " << ec.message();
178 boost::beast::get_lowest_layer(derived().stream()).socket().close(ec);
184 std::string
const& ip,
185 std::reference_wrapper<util::TagDecoratorFactory const> tagFactory,
186 std::shared_ptr<AdminVerificationStrategy> adminVerification,
187 std::shared_ptr<ProxyIpResolver> proxyIpResolver,
188 std::reference_wrapper<dosguard::DOSGuardInterface> dosGuard,
189 std::shared_ptr<HandlerType> handler,
190 std::reference_wrapper<data::LedgerCacheInterface const> cache,
191 boost::beast::flat_buffer buffer
195 , adminVerification_(std::move(adminVerification))
196 , proxyIpResolver_(std::move(proxyIpResolver))
197 , buffer_(std::move(buffer))
198 , dosGuard_(dosGuard)
199 , handler_(std::move(handler))
202 LOG(perfLog_.debug()) <<
tag() <<
"http session created";
203 dosGuard_.get().increment(ip);
208 LOG(perfLog_.debug()) <<
tag() <<
"http session closed";
210 dosGuard_.get().decrement(clientIp_);
223 boost::beast::get_lowest_layer(derived().stream()).expires_after(std::chrono::seconds(30));
229 boost::beast::bind_front_handler(&HttpBase::onRead, derived().shared_from_this())
234 onRead(boost::beast::error_code ec, [[maybe_unused]] std::size_t bytesTransferred)
236 if (ec == http::error::end_of_stream)
237 return derived().doClose();
240 return httpFail(ec,
"read");
242 if (req_.method() == http::verb::get and req_.target() ==
"/health")
243 return sender_(httpResponse(http::status::ok,
"text/html", kHEALTH_CHECK_HTML));
245 if (req_.method() == http::verb::get and req_.target() ==
"/cache_state") {
246 if (cache_.get().isFull())
247 return sender_(httpResponse(http::status::ok,
"text/html", kCACHE_CHECK_LOADED_HTML));
249 return sender_(httpResponse(http::status::service_unavailable,
"text/html", kCACHE_CHECK_NOT_LOADED_HTML));
252 if (
auto resolvedIp = proxyIpResolver_->resolveClientIp(clientIp_, req_); resolvedIp != clientIp_) {
253 LOG(log_.info()) <<
tag() <<
"Detected a forwarded request from proxy. Proxy ip: " << clientIp_
254 <<
". Resolved client ip: " << resolvedIp;
255 dosGuard_.get().decrement(clientIp_);
256 clientIp_ = std::move(resolvedIp);
257 dosGuard_.get().increment(clientIp_);
261 ConnectionBase::isAdmin_ = adminVerification_->isAdmin(req_, clientIp_);
263 if (boost::beast::websocket::is_upgrade(req_)) {
264 if (dosGuard_.get().isOk(clientIp_)) {
266 boost::beast::get_lowest_layer(derived().stream()).expires_never();
269 return derived().upgrade();
272 return sender_(httpResponse(http::status::too_many_requests,
"text/html",
"Too many requests"));
275 if (
auto response = util::prometheus::handlePrometheusRequest(req_,
isAdmin()); response.has_value())
276 return sender_(std::move(response.value()));
278 if (req_.method() != http::verb::post) {
279 return sender_(httpResponse(http::status::bad_request,
"text/html",
"Expected a POST request"));
282 LOG(log_.info()) <<
tag() <<
"Received request from ip = " << clientIp_;
285 (*handler_)(req_.body(), derived().shared_from_this());
286 }
catch (std::exception
const&) {
287 return sender_(httpResponse(
288 http::status::internal_server_error,
290 boost::json::serialize(
rpc::makeError(rpc::RippledError::rpcINTERNAL))
298 sender_(httpResponse(
299 http::status::service_unavailable,
301 boost::json::serialize(
rpc::makeError(rpc::RippledError::rpcSLOW_DOWN))
311 send(std::string&& msg, http::status status = http::status::ok)
override
313 if (!dosGuard_.get().add(clientIp_, msg.size())) {
314 auto jsonResponse = boost::json::parse(msg).as_object();
315 jsonResponse[
"warning"] =
"load";
316 if (jsonResponse.contains(
"warnings") && jsonResponse[
"warnings"].is_array()) {
317 jsonResponse[
"warnings"].as_array().push_back(
rpc::makeWarning(rpc::WarnRpcRateLimit));
319 jsonResponse[
"warnings"] = boost::json::array{
rpc::makeWarning(rpc::WarnRpcRateLimit)};
323 msg = boost::json::serialize(jsonResponse);
325 sender_(httpResponse(status,
"application/json", std::move(msg)));
331 ASSERT(
false,
"SubscriptionContext can't be created for a HTTP connection");
336 onWrite(
bool close, boost::beast::error_code ec, std::size_t bytesTransferred)
338 boost::ignore_unused(bytesTransferred);
341 return httpFail(ec,
"write");
346 return derived().doClose();
353 http::response<http::string_body>
354 httpResponse(http::status status, std::string contentType, std::string message)
const
356 http::response<http::string_body> res{status, req_.version()};
357 res.set(http::field::server,
"clio-server-" + util::build::getClioVersionString());
358 res.set(http::field::content_type, contentType);
359 res.keep_alive(req_.keep_alive());
360 res.body() = std::move(message);
361 res.prepare_payload();