86 return static_cast<Derived<HandlerType>&
>(*this);
93 explicit SendLambda(HttpBase& self) : self(self)
97 template <
bool IsRequest,
typename Body,
typename Fields>
99 operator()(http::message<IsRequest, Body, Fields>&& msg)
const
107 std::make_shared<http::message<IsRequest, Body, Fields>>(std::move(msg));
114 self.derived().stream(),
116 boost::beast::bind_front_handler(
117 &HttpBase::onWrite, self.derived().shared_from_this(), sp->need_eof()
123 std::shared_ptr<void> res_;
125 std::shared_ptr<AdminVerificationStrategy> adminVerification_;
126 std::shared_ptr<ProxyIpResolver> proxyIpResolver_;
127 bool isProxyConnection_ =
false;
130 boost::beast::flat_buffer buffer_;
131 http::request<http::string_body> req_;
132 std::reference_wrapper<dosguard::DOSGuardInterface> dosGuard_;
133 std::shared_ptr<HandlerType>
const handler_;
134 std::reference_wrapper<data::LedgerCacheInterface const> cache_;
139 httpFail(boost::beast::error_code ec,
char const* what)
158 if (ec == boost::asio::ssl::error::stream_truncated)
161 if (!ec_ && ec != boost::asio::error::operation_aborted) {
163 LOG(perfLog_.info()) <<
tag() <<
": " << what <<
": " << ec.message();
164 boost::beast::get_lowest_layer(derived().stream()).socket().close(ec);
170 std::string
const& ip,
171 std::reference_wrapper<util::TagDecoratorFactory const> tagFactory,
172 std::shared_ptr<AdminVerificationStrategy> adminVerification,
173 std::shared_ptr<ProxyIpResolver> proxyIpResolver,
174 std::reference_wrapper<dosguard::DOSGuardInterface> dosGuard,
175 std::shared_ptr<HandlerType> handler,
176 std::reference_wrapper<data::LedgerCacheInterface const> cache,
177 boost::beast::flat_buffer buffer
181 , adminVerification_(std::move(adminVerification))
182 , proxyIpResolver_(std::move(proxyIpResolver))
183 , buffer_(std::move(buffer))
184 , dosGuard_(dosGuard)
185 , handler_(std::move(handler))
188 LOG(perfLog_.debug()) <<
tag() <<
"http session created";
189 dosGuard_.get().increment(ip);
194 LOG(perfLog_.debug()) <<
tag() <<
"http session closed";
196 dosGuard_.get().decrement(clientIp_);
209 boost::beast::get_lowest_layer(derived().stream()).expires_after(std::chrono::seconds(30));
215 boost::beast::bind_front_handler(&HttpBase::onRead, derived().shared_from_this())
220 onRead(boost::beast::error_code ec, [[maybe_unused]] std::size_t bytesTransferred)
222 if (ec == http::error::end_of_stream)
223 return derived().doClose();
226 return httpFail(ec,
"read");
228 auto const updateClientIp = [&](std::string newIp) {
229 if (newIp == clientIp_)
231 LOG(log_.info()) <<
tag()
232 <<
"Detected a forwarded request from proxy. Resolved client ip: "
234 dosGuard_.get().decrement(clientIp_);
235 clientIp_ = std::move(newIp);
236 dosGuard_.get().increment(clientIp_);
239 if (isProxyConnection_) {
241 updateClientIp(std::move(*resolvedIp));
243 auto resolvedIp = proxyIpResolver_->resolveClientIp(clientIp_, req_);
244 resolvedIp.has_value()
246 updateClientIp(std::move(*resolvedIp));
247 isProxyConnection_ =
true;
250 if (req_.method() == http::verb::get and req_.target() ==
"/health")
251 return sender_(httpResponse(http::status::ok,
"text/html", kHealthCheckHtml));
253 if (req_.method() == http::verb::get and req_.target() ==
"/cache_state") {
254 if (cache_.get().isFull()) {
255 return sender_(httpResponse(http::status::ok,
"text/html", kCacheCheckLoadedHtml));
258 return sender_(httpResponse(
259 http::status::service_unavailable,
"text/html", kCacheCheckNotLoadedHtml
264 ConnectionBase::isAdmin_ = adminVerification_->isAdmin(req_, clientIp_);
266 if (boost::beast::websocket::is_upgrade(req_)) {
267 if (dosGuard_.get().isOk(clientIp_)) {
269 boost::beast::get_lowest_layer(derived().stream()).expires_never();
272 return derived().upgrade();
276 httpResponse(http::status::too_many_requests,
"text/html",
"Too many requests")
280 if (
auto response = util::prometheus::handlePrometheusRequest(req_,
isAdmin());
281 response.has_value())
282 return sender_(std::move(response.value()));
284 if (req_.method() != http::verb::post) {
286 httpResponse(http::status::bad_request,
"text/html",
"Expected a POST request")
290 LOG(log_.info()) <<
tag() <<
"Received request from ip = " << clientIp_;
293 (*handler_)(req_.body(), derived().shared_from_this());
294 }
catch (std::exception
const&) {
295 return sender_(httpResponse(
296 http::status::internal_server_error,
298 boost::json::serialize(
rpc::makeError(rpc::XrpldError::RpcInternal))
306 sender_(httpResponse(
307 http::status::service_unavailable,
309 boost::json::serialize(
rpc::makeError(rpc::XrpldError::RpcSlowDown))
321 send(std::string&& msg, http::status status = http::status::ok)
override
323 if (!dosGuard_.get().add(clientIp_, msg.size())) {
326 msg = boost::json::serialize(*warned);
329 sender_(httpResponse(status,
"application/json", std::move(msg)));
335 ASSERT(
false,
"SubscriptionContext can't be created for a HTTP connection");
340 onWrite(
bool close, boost::beast::error_code ec, std::size_t bytesTransferred)
342 boost::ignore_unused(bytesTransferred);
345 return httpFail(ec,
"write");
350 return derived().doClose();
357 [[nodiscard]] http::response<http::string_body>
358 httpResponse(http::status status, std::string contentType, std::string message)
const
360 http::response<http::string_body> res{status, req_.version()};
361 res.set(http::field::server,
"clio-server-" + util::build::getClioVersionString());
362 res.set(http::field::content_type, contentType);
363 res.keep_alive(req_.keep_alive());
364 res.body() = std::move(message);
365 res.prepare_payload();