46 std::shared_ptr<BackendInterface const>
const backend_;
47 std::shared_ptr<RPCEngineType>
const rpcEngine_;
48 std::shared_ptr<etl::ETLServiceInterface const>
const etl_;
51 std::reference_wrapper<web::dosguard::DOSGuardInterface> dosguard_;
68 std::shared_ptr<BackendInterface const>
const& backend,
69 std::shared_ptr<RPCEngineType>
const& rpcEngine,
70 std::shared_ptr<etl::ETLServiceInterface const>
const& etl,
74 , rpcEngine_(rpcEngine)
77 , apiVersionParser_(config.getObject(
"api_version"))
89 operator()(std::string
const& request, std::shared_ptr<web::ConnectionBase>
const& connection)
91 if (not dosguard_.get().isOk(connection->clientIp())) {
92 connection->sendSlowDown(request);
97 auto req = boost::json::parse(request).as_object();
98 LOG(perfLog_.debug()) << connection->tag() <<
"Adding to work queue";
100 if (not connection->upgraded and shouldReplaceParams(req))
101 req[JS(params)] = boost::json::array({boost::json::object{}});
103 if (not dosguard_.get().request(connection->clientIp(), req)) {
104 connection->sendSlowDown(request);
108 if (!rpcEngine_->post(
109 [
this, request = std::move(req), connection](
110 boost::asio::yield_context yield
111 )
mutable { handleRequest(yield, std::move(request), connection); },
112 connection->clientIp()
114 rpcEngine_->notifyTooBusy();
117 }
catch (boost::system::system_error
const& ex) {
119 rpcEngine_->notifyBadSyntax();
121 LOG(log_.warn()) <<
"Error parsing JSON: " << ex.what() <<
". For request: " << request;
122 }
catch (std::invalid_argument
const& ex) {
124 rpcEngine_->notifyBadSyntax();
125 LOG(log_.warn()) <<
"Invalid argument error: " << ex.what()
126 <<
". For request: " << request;
128 }
catch (std::exception
const& ex) {
129 LOG(perfLog_.error()) << connection->tag() <<
"Caught exception: " << ex.what();
130 rpcEngine_->notifyInternalError();
138 boost::asio::yield_context yield,
139 boost::json::object&& request,
140 std::shared_ptr<web::ConnectionBase>
const& connection
143 LOG(log_.
info()) << connection->tag() << (connection->upgraded ?
"ws" :
"http")
145 <<
" ip = " << connection->clientIp();
148 auto const range = backend_->fetchLedgerRange();
151 rpcEngine_->notifyNotReady();
157 auto const context = [&] {
158 if (connection->upgraded) {
162 connection->makeSubscriptionContext(tagFactory_),
163 tagFactory_.
with(connection->tag()),
165 connection->clientIp(),
166 std::cref(apiVersionParser_),
167 connection->isAdmin()
173 tagFactory_.with(connection->tag()),
175 connection->clientIp(),
176 std::cref(apiVersionParser_),
177 connection->isAdmin()
182 auto const err = context.error();
184 << connection->tag() <<
"Could not create Web context: " << err;
185 LOG(log_.warn()) << connection->tag() <<
"Could not create Web context: " << err;
190 rpcEngine_->notifyBadSyntax();
191 web::impl::ErrorHelper(connection, std::move(request)).sendError(err);
196 auto [result, timeDiff] =
197 util::timed([&]() {
return rpcEngine_->buildResponse(*context); });
199 auto const us = std::chrono::duration<int, std::milli>(timeDiff);
202 boost::json::object response;
204 if (!result.response.has_value()) {
206 response = web::impl::ErrorHelper(connection, request)
207 .composeError(result.response.error());
208 auto const responseStr = boost::json::serialize(response);
210 LOG(perfLog_.debug()) << context->tag() <<
"Encountered error: " << responseStr;
211 LOG(log_.debug()) << context->tag() <<
"Encountered error: " << responseStr;
213 auto& json = result.response.value();
214 auto const isForwarded = json.contains(
"forwarded") &&
215 json.at(
"forwarded").is_bool() && json.at(
"forwarded").as_bool();
219 rpcEngine_->notifyComplete(*context, us, isForwarded);
222 json.erase(
"forwarded");
227 if (isForwarded && (json.contains(JS(result)) || connection->upgraded)) {
228 for (
auto const& [k, v] : json)
229 response.insert_or_assign(k, v);
231 response[JS(result)] = json;
235 response[
"forwarded"] =
true;
239 if (connection->upgraded) {
240 auto const appendFieldIfExist = [&](
auto const& field) {
241 if (request.contains(field) and not request.at(field).is_null())
242 response[field] = request.at(field);
245 appendFieldIfExist(JS(
id));
246 appendFieldIfExist(JS(api_version));
248 if (!response.contains(JS(error)))
249 response[JS(status)] = JS(success);
251 response[JS(type)] = JS(response);
253 if (response.contains(JS(result)) &&
254 !response[JS(result)].as_object().contains(JS(error)))
255 response[JS(result)].as_object()[JS(status)] = JS(success);
259 boost::json::array warnings = std::move(result.warnings);
262 if (etl_->lastCloseAgeSeconds() >= 60)
265 response[
"warnings"] = warnings;
266 connection->send(boost::json::serialize(response));
267 }
catch (std::exception
const& ex) {
270 LOG(perfLog_.error()) << connection->tag() <<
"Caught exception: " << ex.what();
271 LOG(log_.error()) << connection->tag() <<
"Caught exception: " << ex.what();
273 rpcEngine_->notifyInternalError();
274 web::impl::ErrorHelper(connection, std::move(request)).sendInternalError();
281 shouldReplaceParams(boost::json::object
const& req)
const
283 auto const hasParams = req.contains(JS(params));
284 auto const paramsIsArray = hasParams and req.at(JS(params)).is_array();
285 auto const paramsIsEmptyString =
286 hasParams and req.at(JS(params)).is_string() and req.at(JS(params)).as_string().empty();
287 auto const paramsIsEmptyObject =
288 hasParams and req.at(JS(params)).is_object() and req.at(JS(params)).as_object().empty();
289 auto const paramsIsNull = hasParams and req.at(JS(params)).is_null();
290 auto const arrayIsEmpty = paramsIsArray and req.at(JS(params)).as_array().empty();
291 auto const arrayIsNotEmpty = paramsIsArray and not req.at(JS(params)).as_array().empty();
292 auto const firstArgIsNull =
293 arrayIsNotEmpty and req.at(JS(params)).as_array().at(0).is_null();
294 auto const firstArgIsEmptyString = arrayIsNotEmpty and
295 req.at(JS(params)).as_array().at(0).is_string() and
296 req.at(JS(params)).as_array().at(0).as_string().empty();
299 return not hasParams or paramsIsEmptyString or paramsIsNull or paramsIsEmptyObject or
300 arrayIsEmpty or firstArgIsEmptyString or firstArgIsNull;
std::expected< web::Context, Status > makeWsContext(boost::asio::yield_context yc, boost::json::object const &request, web::SubscriptionContextPtr session, util::TagDecoratorFactory const &tagFactory, data::LedgerRange const &range, std::string const &clientIp, std::reference_wrapper< APIVersionParser const > apiVersionParser, bool isAdmin)
A factory function that creates a Websocket context.
Definition Factories.cpp:28
std::expected< web::Context, Status > makeHttpContext(boost::asio::yield_context yc, boost::json::object const &request, util::TagDecoratorFactory const &tagFactory, data::LedgerRange const &range, std::string const &clientIp, std::reference_wrapper< APIVersionParser const > apiVersionParser, bool const isAdmin)
A factory function that creates a HTTP context.
Definition Factories.cpp:63