85 return static_cast<Derived<HandlerType>&
>(*this);
92 explicit SendLambda(HttpBase& self) : self(self)
96 template <
bool IsRequest,
typename Body,
typename Fields>
98 operator()(http::message<IsRequest, Body, Fields>&& msg)
const
105 auto sp = std::make_shared<http::message<IsRequest, Body, Fields>>(std::move(msg));
112 self.derived().stream(),
114 boost::beast::bind_front_handler(
115 &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_;
131 std::reference_wrapper<data::LedgerCacheInterface const> cache_;
136 httpFail(boost::beast::error_code ec,
char const* what)
155 if (ec == boost::asio::ssl::error::stream_truncated)
158 if (!ec_ && ec != boost::asio::error::operation_aborted) {
160 LOG(perfLog_.info()) <<
tag() <<
": " << what <<
": " << ec.message();
161 boost::beast::get_lowest_layer(derived().stream()).socket().close(ec);
167 std::string
const& ip,
168 std::reference_wrapper<util::TagDecoratorFactory const> tagFactory,
169 std::shared_ptr<AdminVerificationStrategy> adminVerification,
170 std::shared_ptr<ProxyIpResolver> proxyIpResolver,
171 std::reference_wrapper<dosguard::DOSGuardInterface> dosGuard,
172 std::shared_ptr<HandlerType> handler,
173 std::reference_wrapper<data::LedgerCacheInterface const> cache,
174 boost::beast::flat_buffer buffer
178 , adminVerification_(std::move(adminVerification))
179 , proxyIpResolver_(std::move(proxyIpResolver))
180 , buffer_(std::move(buffer))
181 , dosGuard_(dosGuard)
182 , handler_(std::move(handler))
185 LOG(perfLog_.debug()) <<
tag() <<
"http session created";
186 dosGuard_.get().increment(ip);
191 LOG(perfLog_.debug()) <<
tag() <<
"http session closed";
193 dosGuard_.get().decrement(clientIp_);
206 boost::beast::get_lowest_layer(derived().stream()).expires_after(std::chrono::seconds(30));
212 boost::beast::bind_front_handler(&HttpBase::onRead, derived().shared_from_this())
217 onRead(boost::beast::error_code ec, [[maybe_unused]] std::size_t bytesTransferred)
219 if (ec == http::error::end_of_stream)
220 return derived().doClose();
223 return httpFail(ec,
"read");
225 if (
auto resolvedIp = proxyIpResolver_->resolveClientIp(clientIp_, req_);
226 resolvedIp != clientIp_) {
227 LOG(log_.info()) <<
tag()
228 <<
"Detected a forwarded request from proxy. Proxy ip: " << clientIp_
229 <<
". Resolved client ip: " << resolvedIp;
230 dosGuard_.get().decrement(clientIp_);
231 clientIp_ = std::move(resolvedIp);
232 dosGuard_.get().increment(clientIp_);
235 if (req_.method() == http::verb::get and req_.target() ==
"/health")
236 return sender_(httpResponse(http::status::ok,
"text/html", kHEALTH_CHECK_HTML));
238 if (req_.method() == http::verb::get and req_.target() ==
"/cache_state") {
239 if (cache_.get().isFull()) {
241 httpResponse(http::status::ok,
"text/html", kCACHE_CHECK_LOADED_HTML)
245 return sender_(httpResponse(
246 http::status::service_unavailable,
"text/html", kCACHE_CHECK_NOT_LOADED_HTML
251 ConnectionBase::isAdmin_ = adminVerification_->isAdmin(req_, clientIp_);
253 if (boost::beast::websocket::is_upgrade(req_)) {
254 if (dosGuard_.get().isOk(clientIp_)) {
256 boost::beast::get_lowest_layer(derived().stream()).expires_never();
259 return derived().upgrade();
263 httpResponse(http::status::too_many_requests,
"text/html",
"Too many requests")
267 if (
auto response = util::prometheus::handlePrometheusRequest(req_,
isAdmin());
268 response.has_value())
269 return sender_(std::move(response.value()));
271 if (req_.method() != http::verb::post) {
273 httpResponse(http::status::bad_request,
"text/html",
"Expected a POST request")
277 LOG(log_.info()) <<
tag() <<
"Received request from ip = " << clientIp_;
280 (*handler_)(req_.body(), derived().shared_from_this());
281 }
catch (std::exception
const&) {
282 return sender_(httpResponse(
283 http::status::internal_server_error,
285 boost::json::serialize(
rpc::makeError(rpc::RippledError::rpcINTERNAL))
293 sender_(httpResponse(
294 http::status::service_unavailable,
296 boost::json::serialize(
rpc::makeError(rpc::RippledError::rpcSLOW_DOWN))
306 send(std::string&& msg, http::status status = http::status::ok)
override
308 if (!dosGuard_.get().add(clientIp_, msg.size())) {
309 auto jsonResponse = boost::json::parse(msg).as_object();
310 jsonResponse[
"warning"] =
"load";
311 if (jsonResponse.contains(
"warnings") && jsonResponse[
"warnings"].is_array()) {
312 jsonResponse[
"warnings"].as_array().push_back(
316 jsonResponse[
"warnings"] =
321 msg = boost::json::serialize(jsonResponse);
323 sender_(httpResponse(status,
"application/json", std::move(msg)));
329 ASSERT(
false,
"SubscriptionContext can't be created for a HTTP connection");
334 onWrite(
bool close, boost::beast::error_code ec, std::size_t bytesTransferred)
336 boost::ignore_unused(bytesTransferred);
339 return httpFail(ec,
"write");
344 return derived().doClose();
351 http::response<http::string_body>
352 httpResponse(http::status status, std::string contentType, std::string message)
const
354 http::response<http::string_body> res{status, req_.version()};
355 res.set(http::field::server,
"clio-server-" + util::build::getClioVersionString());
356 res.set(http::field::content_type, contentType);
357 res.keep_alive(req_.keep_alive());
358 res.body() = std::move(message);
359 res.prepare_payload();