diff --git a/core/functional_tests/basic_chaos/httpclient_handlers.hpp b/core/functional_tests/basic_chaos/httpclient_handlers.hpp index 10315e98f386..e528c271a9d7 100644 --- a/core/functional_tests/basic_chaos/httpclient_handlers.hpp +++ b/core/functional_tests/basic_chaos/httpclient_handlers.hpp @@ -68,7 +68,7 @@ class StreamHandler : public server::handlers::HttpHandlerBase { /// [HandleStreamRequest] void HandleStreamRequest( - const server::http::HttpRequest& request, + server::http::HttpRequest& request, server::request::RequestContext&, server::http::ResponseBodyStream& response_body_stream ) const override { diff --git a/core/functional_tests/http2server/service.cpp b/core/functional_tests/http2server/service.cpp index 4a54a2bbf575..31609782922d 100644 --- a/core/functional_tests/http2server/service.cpp +++ b/core/functional_tests/http2server/service.cpp @@ -36,7 +36,7 @@ class HandlerHttp2 final : public server::handlers::HttpHandlerBase { }; void HandleStreamRequest( - const server::http::HttpRequest& req, + server::http::HttpRequest& req, server::request::RequestContext&, server::http::ResponseBodyStream& stream ) const override { diff --git a/core/include/userver/server/handlers/handler_base.hpp b/core/include/userver/server/handlers/handler_base.hpp index a58a7980624a..dc50ad868f01 100644 --- a/core/include/userver/server/handlers/handler_base.hpp +++ b/core/include/userver/server/handlers/handler_base.hpp @@ -57,7 +57,7 @@ class HandlerBase : public components::ComponentBase { /// Parses request, executes processing routines, and fills response /// accordingly. Does not throw. - virtual void HandleRequest(http::HttpRequest& request, request::RequestContext& context) const = 0; + virtual void PrepareAndHandleRequest(http::HttpRequest& request, request::RequestContext& context) const = 0; /// Produces response to a request unrecognized by the protocol based on /// provided generic response. Does not throw. diff --git a/core/include/userver/server/handlers/http_handler_base.hpp b/core/include/userver/server/handlers/http_handler_base.hpp index af214ff55743..675f82644f4f 100644 --- a/core/include/userver/server/handlers/http_handler_base.hpp +++ b/core/include/userver/server/handlers/http_handler_base.hpp @@ -72,7 +72,7 @@ class HttpHandlerBase : public HandlerBase { ~HttpHandlerBase() override; - void HandleRequest(http::HttpRequest& request, request::RequestContext& context) const override; + void PrepareAndHandleRequest(http::HttpRequest& request, request::RequestContext& context) const override; void ReportMalformedRequest(http::HttpRequest& request) const final; @@ -119,11 +119,14 @@ class HttpHandlerBase : public HandlerBase { protected: [[noreturn]] void ThrowUnsupportedHttpMethod(const http::HttpRequest& request) const; + /// Same as `HandleRequest`. + virtual std::string HandleRequestThrow(const http::HttpRequest& request, request::RequestContext& context) const; + /// The core method for HTTP request handling. /// `request` arg contains HTTP headers, full body, etc. /// The method should return response body. /// @note It is used only if IsStreamed() returned `false`. - virtual std::string HandleRequestThrow(const http::HttpRequest& request, request::RequestContext& context) const; + virtual std::string HandleRequest(http::HttpRequest& request, request::RequestContext& context) const; /// The core method for HTTP request handling. /// `request` arg contains HTTP headers, full body, etc. @@ -137,12 +140,12 @@ class HttpHandlerBase : public HandlerBase { /// in memory. /// @note It is used only if IsStreamed() returned `true`. virtual void - HandleStreamRequest(const server::http::HttpRequest&, server::request::RequestContext&, server::http::ResponseBodyStream&) + HandleStreamRequest(server::http::HttpRequest&, server::request::RequestContext&, server::http::ResponseBodyStream&) const; /// If IsStreamed() returns `true`, call HandleStreamRequest() - /// for request handling, HandleRequestThrow() is not called. - /// If it returns `false`, HandleRequestThrow() is called instead, + /// for request handling, HandleRequest() is not called. + /// If it returns `false`, HandleRequest() is called instead, /// and HandleStreamRequest() is not called. /// @note The default implementation returns the cached value of /// "response-body-streamed" value from static config. @@ -182,7 +185,7 @@ class HttpHandlerBase : public HandlerBase { void HandleHttpRequest(http::HttpRequest& request, request::RequestContext& context) const; - void HandleRequestStream(const http::HttpRequest& http_request, request::RequestContext& context) const; + void HandleRequestStream(http::HttpRequest& http_request, request::RequestContext& context) const; std::string GetRequestBodyForLoggingChecked( const http::HttpRequest& request, diff --git a/core/include/userver/server/http/http_request.hpp b/core/include/userver/server/http/http_request.hpp index 5ba7afccd351..5820c7f57bfc 100644 --- a/core/include/userver/server/http/http_request.hpp +++ b/core/include/userver/server/http/http_request.hpp @@ -207,6 +207,9 @@ class HttpRequest final { /// @return HTTP body. const std::string& RequestBody() const; + /// @return moved out HTTP body. `this` is modified. + std::string ExtractRequestBody(); + /// @cond void SetRequestBody(std::string body); void ParseArgsFromBody(); diff --git a/core/src/server/handlers/http_handler_base.cpp b/core/src/server/handlers/http_handler_base.cpp index d364a499043b..a1789c05dfe3 100644 --- a/core/src/server/handlers/http_handler_base.cpp +++ b/core/src/server/handlers/http_handler_base.cpp @@ -198,8 +198,7 @@ HttpHandlerBase::HttpHandlerBase( HttpHandlerBase::~HttpHandlerBase() { statistics_holder_.Unregister(); } -void HttpHandlerBase::HandleRequestStream(const http::HttpRequest& http_request, request::RequestContext& context) - const { +void HttpHandlerBase::HandleRequestStream(http::HttpRequest& http_request, request::RequestContext& context) const { auto& response = http_request.GetHttpResponse(); const utils::ScopeGuard scope([&response] { response.SetHeadersEnd(); }); @@ -254,11 +253,11 @@ void HttpHandlerBase::HandleHttpRequest(http::HttpRequest& http_request, request HandleRequestStream(http_request, context); } else { // !IsBodyStreamed() - response.SetData(HandleRequestThrow(http_request, context)); + response.SetData(HandleRequest(http_request, context)); } } -void HttpHandlerBase::HandleRequest(http::HttpRequest& http_request, request::RequestContext& context) const { +void HttpHandlerBase::PrepareAndHandleRequest(http::HttpRequest& http_request, request::RequestContext& context) const { auto& response = http_request.GetHttpResponse(); context.GetInternalContext().SetConfigSnapshot(config_source_.GetSnapshot()); @@ -288,13 +287,18 @@ void HttpHandlerBase::ThrowUnsupportedHttpMethod(const http::HttpRequest& reques std::string HttpHandlerBase::HandleRequestThrow(const http::HttpRequest&, request::RequestContext&) const { throw std::runtime_error( - "non-stream HandleRequestThrow() is executed, but the handler doesn't " - "override HandleRequestThrow()." + "non-stream HandleRequest() is executed, but the handler doesn't " + "override HandleRequest()." ); } +std::string HttpHandlerBase::HandleRequest(http::HttpRequest& request, request::RequestContext& context) const { + // Default implementation proxies the request to legacy HandleRequestThrow() + return HandleRequestThrow(request, context); +} + void HttpHandlerBase:: - HandleStreamRequest(const server::http::HttpRequest&, server::request::RequestContext&, server::http::ResponseBodyStream&) + HandleStreamRequest(server::http::HttpRequest&, server::request::RequestContext&, server::http::ResponseBodyStream&) const { throw std::runtime_error( "stream HandleStreamRequest() is executed, but the handler doesn't " diff --git a/core/src/server/http/http_request.cpp b/core/src/server/http/http_request.cpp index 36a3bac34a5f..9715834dea3a 100644 --- a/core/src/server/http/http_request.cpp +++ b/core/src/server/http/http_request.cpp @@ -227,6 +227,8 @@ const HttpRequest::CookiesMap& HttpRequest::RequestCookies() const { return pimp const std::string& HttpRequest::RequestBody() const { return pimpl_->request_body_; } +std::string HttpRequest::ExtractRequestBody() { return std::move(pimpl_->request_body_); } + void HttpRequest::SetRequestBody(std::string body) { pimpl_->request_body_ = std::move(body); } void HttpRequest::ParseArgsFromBody() { diff --git a/core/src/server/http/http_request_handler.cpp b/core/src/server/http/http_request_handler.cpp index 2ea67a044633..d4671a36ed22 100644 --- a/core/src/server/http/http_request_handler.cpp +++ b/core/src/server/http/http_request_handler.cpp @@ -153,7 +153,7 @@ engine::TaskWithResult HttpRequestHandler::StartRequestTask(std::shared_pt request->SetTaskStartTime(); request::RequestContext context; - handler->HandleRequest(*request, context); + handler->PrepareAndHandleRequest(*request, context); const auto now = std::chrono::steady_clock::now(); request->SetResponseNotifyTime(now); diff --git a/samples/chaotic_service/src/hello_service.cpp b/samples/chaotic_service/src/hello_service.cpp index 6a04e85a78a4..20ff134b0024 100644 --- a/samples/chaotic_service/src/hello_service.cpp +++ b/samples/chaotic_service/src/hello_service.cpp @@ -19,8 +19,7 @@ class Hello final : public server::handlers::HttpHandlerBase { using HttpHandlerBase::HttpHandlerBase; /// [Handler] - std::string HandleRequestThrow(const server::http::HttpRequest& request, server::request::RequestContext&) - const override { + std::string HandleRequest(server::http::HttpRequest& request, server::request::RequestContext&) const override { request.GetHttpResponse().SetContentType(http::content_type::kApplicationJson); auto request_json = formats::json::FromString(request.RequestBody()); diff --git a/samples/clickhouse_service/clickhouse_service.cpp b/samples/clickhouse_service/clickhouse_service.cpp index f167d51807d0..c4072202d8b6 100644 --- a/samples/clickhouse_service/clickhouse_service.cpp +++ b/samples/clickhouse_service/clickhouse_service.cpp @@ -34,7 +34,7 @@ class HandlerDb final : public server::handlers::HttpHandlerBase { HandlerDb(const components::ComponentConfig& config, const components::ComponentContext& context); - std::string HandleRequestThrow(const server::http::HttpRequest& request, server::request::RequestContext& context) + std::string HandleRequest(server::http::HttpRequest& request, server::request::RequestContext& context) const override; private: @@ -45,8 +45,7 @@ HandlerDb::HandlerDb(const components::ComponentConfig& config, const components : server::handlers::HttpHandlerBase{config, context}, clickhouse_{context.FindComponent("clickhouse-database").GetCluster()} {} -std::string HandlerDb::HandleRequestThrow(const server::http::HttpRequest& request, server::request::RequestContext&) - const { +std::string HandlerDb::HandleRequest(server::http::HttpRequest& request, server::request::RequestContext&) const { const auto& limit = request.GetArg("limit"); // FP?: pfr magic // NOLINTNEXTLINE(clang-analyzer-core.uninitialized.UndefReturn) diff --git a/samples/digest_auth_service/digest_auth_service.cpp b/samples/digest_auth_service/digest_auth_service.cpp index b4d299cca18b..6473e6bb0109 100644 --- a/samples/digest_auth_service/digest_auth_service.cpp +++ b/samples/digest_auth_service/digest_auth_service.cpp @@ -23,7 +23,7 @@ class Hello final : public server::handlers::HttpHandlerBase { using HttpHandlerBase::HttpHandlerBase; - std::string HandleRequestThrow(const server::http::HttpRequest&, server::request::RequestContext&) const override { + std::string HandleRequest(server::http::HttpRequest&, server::request::RequestContext&) const override { return "Hello world"; } }; diff --git a/samples/grpc_middleware_service/src/http_handlers/say-hello/view.cpp b/samples/grpc_middleware_service/src/http_handlers/say-hello/view.cpp index c633565ad865..39bf0b9ad832 100644 --- a/samples/grpc_middleware_service/src/http_handlers/say-hello/view.cpp +++ b/samples/grpc_middleware_service/src/http_handlers/say-hello/view.cpp @@ -11,8 +11,7 @@ GreeterHttpHandler::GreeterHttpHandler( ) : HttpHandlerBase(config, context), grpc_greeter_client_(context.FindComponent()) {} -std::string -GreeterHttpHandler::HandleRequestThrow(const server::http::HttpRequest& request, server::request::RequestContext&) +std::string GreeterHttpHandler::HandleRequest(server::http::HttpRequest& request, server::request::RequestContext&) const { return grpc_greeter_client_.SayHello(request.RequestBody()); } diff --git a/samples/grpc_middleware_service/src/http_handlers/say-hello/view.hpp b/samples/grpc_middleware_service/src/http_handlers/say-hello/view.hpp index 162709b1f47d..28c1fd0f400d 100644 --- a/samples/grpc_middleware_service/src/http_handlers/say-hello/view.hpp +++ b/samples/grpc_middleware_service/src/http_handlers/say-hello/view.hpp @@ -14,8 +14,7 @@ class GreeterHttpHandler final : public server::handlers::HttpHandlerBase { GreeterHttpHandler(const components::ComponentConfig& config, const components::ComponentContext& context); - std::string HandleRequestThrow(const server::http::HttpRequest& request, server::request::RequestContext&) - const override; + std::string HandleRequest(server::http::HttpRequest& request, server::request::RequestContext&) const override; private: GreeterClient& grpc_greeter_client_; diff --git a/samples/grpc_service/src/call_greeter_client_test_handler.hpp b/samples/grpc_service/src/call_greeter_client_test_handler.hpp index 240fbf6a5000..6fb2eb9b33c1 100644 --- a/samples/grpc_service/src/call_greeter_client_test_handler.hpp +++ b/samples/grpc_service/src/call_greeter_client_test_handler.hpp @@ -20,8 +20,7 @@ class CallGreeterClientTestHandler final : public server::handlers::HttpHandlerB : HttpHandlerBase(config, context), grpc_greeter_client_(context.FindComponent().GetClient()) {} - std::string HandleRequestThrow(const server::http::HttpRequest& request, server::request::RequestContext&) - const override { + std::string HandleRequest(server::http::HttpRequest& request, server::request::RequestContext&) const override { const auto& arg_case = request.GetArg("case"); request.GetHttpResponse().SetContentType(http::content_type::kTextPlain); diff --git a/samples/hello_service/src/hello_handler.cpp b/samples/hello_service/src/hello_handler.cpp index e608870f3262..0142b353b0a5 100644 --- a/samples/hello_service/src/hello_handler.cpp +++ b/samples/hello_service/src/hello_handler.cpp @@ -4,9 +4,9 @@ namespace samples::hello { -std::string HelloHandler:: - HandleRequestThrow(const server::http::HttpRequest& request, server::request::RequestContext& /*request_context*/) - const { +std::string +HelloHandler::HandleRequest(server::http::HttpRequest& request, server::request::RequestContext& /*request_context*/) + const { // Setting Content-Type: text/plain in a microservice response ensures // the client interprets it as plain text, preventing misinterpretation or // errors. Without this header, the client might assume a different format, diff --git a/samples/hello_service/src/hello_handler.hpp b/samples/hello_service/src/hello_handler.hpp index b97439164eee..efa54cf9d598 100644 --- a/samples/hello_service/src/hello_handler.hpp +++ b/samples/hello_service/src/hello_handler.hpp @@ -16,8 +16,7 @@ class HelloHandler final : public server::handlers::HttpHandlerBase { // Component is valid after construction and is able to accept requests using HttpHandlerBase::HttpHandlerBase; - std::string HandleRequestThrow(const server::http::HttpRequest& request, server::request::RequestContext&) - const override; + std::string HandleRequest(server::http::HttpRequest& request, server::request::RequestContext&) const override; }; } // namespace samples::hello diff --git a/samples/http_caching/http_caching.cpp b/samples/http_caching/http_caching.cpp index 59e506b62274..c0c9b6b50407 100644 --- a/samples/http_caching/http_caching.cpp +++ b/samples/http_caching/http_caching.cpp @@ -188,8 +188,7 @@ class GreetUser final : public server::handlers::HttpHandlerBase { GreetUser(const components::ComponentConfig& config, const components::ComponentContext& context) : HttpHandlerBase(config, context), cache_(context.FindComponent()) {} - std::string HandleRequestThrow(const server::http::HttpRequest& request, server::request::RequestContext&) - const override { + std::string HandleRequest(server::http::HttpRequest& request, server::request::RequestContext&) const override { const auto cache_snapshot = cache_.Get(); using samples::http_cache::KeyLang; diff --git a/samples/http_middleware_service/http_middleware_service.cpp b/samples/http_middleware_service/http_middleware_service.cpp index d0b8755a4485..aa09ba281a5a 100644 --- a/samples/http_middleware_service/http_middleware_service.cpp +++ b/samples/http_middleware_service/http_middleware_service.cpp @@ -15,7 +15,7 @@ class Handler final : public server::handlers::HttpHandlerBase { public: using HttpHandlerBase::HttpHandlerBase; - std::string HandleRequestThrow(const server::http::HttpRequest&, server::request::RequestContext&) const override { + std::string HandleRequest(server::http::HttpRequest&, server::request::RequestContext&) const override { return "Hello world!\n"; } }; diff --git a/samples/https_service/hello_service.cpp b/samples/https_service/hello_service.cpp index 73ab569dfdcf..ff0ebfaa19ea 100644 --- a/samples/https_service/hello_service.cpp +++ b/samples/https_service/hello_service.cpp @@ -17,8 +17,7 @@ class Hello final : public server::handlers::HttpHandlerBase { // Component is valid after construction and is able to accept requests using HttpHandlerBase::HttpHandlerBase; - std::string HandleRequestThrow(const server::http::HttpRequest& request, server::request::RequestContext&) - const override { + std::string HandleRequest(server::http::HttpRequest& request, server::request::RequestContext&) const override { request.GetHttpResponse().SetContentType(http::content_type::kTextPlain); return "Hello world!\n"; } diff --git a/samples/mongo_service/mongo_service.cpp b/samples/mongo_service/mongo_service.cpp index ad5d4d05e3c1..99e4359624de 100644 --- a/samples/mongo_service/mongo_service.cpp +++ b/samples/mongo_service/mongo_service.cpp @@ -18,8 +18,7 @@ class Translations final : public server::handlers::HttpHandlerBase { Translations(const components::ComponentConfig& config, const components::ComponentContext& context) : HttpHandlerBase(config, context), pool_(context.FindComponent("mongo-tr").GetPool()) {} - std::string HandleRequestThrow(const server::http::HttpRequest& request, server::request::RequestContext&) - const override { + std::string HandleRequest(server::http::HttpRequest& request, server::request::RequestContext&) const override { request.GetHttpResponse().SetContentType(http::content_type::kApplicationJson); if (request.GetMethod() == server::http::HttpMethod::kPatch) { InsertNew(request); diff --git a/samples/multipart_service/service.cpp b/samples/multipart_service/service.cpp index 1f557ac077a7..e53b3caa5ee1 100644 --- a/samples/multipart_service/service.cpp +++ b/samples/multipart_service/service.cpp @@ -17,14 +17,12 @@ class Multipart final : public server::handlers::HttpHandlerBase { // Component is valid after construction and is able to accept requests using HttpHandlerBase::HttpHandlerBase; - std::string HandleRequestThrow(const server::http::HttpRequest& req, server::request::RequestContext&) - const override; + std::string HandleRequest(server::http::HttpRequest& req, server::request::RequestContext&) const override; }; /// [Multipart service sample - component] /// [Multipart service sample - HandleRequestThrow] -std::string Multipart::HandleRequestThrow(const server::http::HttpRequest& req, server::request::RequestContext&) - const { +std::string Multipart::HandleRequest(server::http::HttpRequest& req, server::request::RequestContext&) const { const auto content_type = http::ContentType(req.GetHeader(http::headers::kContentType)); if (content_type != "multipart/form-data") { req.GetHttpResponse().SetStatus(server::http::HttpStatus::kBadRequest); diff --git a/samples/postgres_auth/postgres_service.cpp b/samples/postgres_auth/postgres_service.cpp index 86aa5e9178ff..7a5075daceed 100644 --- a/samples/postgres_auth/postgres_service.cpp +++ b/samples/postgres_auth/postgres_service.cpp @@ -22,8 +22,7 @@ class Hello final : public server::handlers::HttpHandlerBase { using HttpHandlerBase::HttpHandlerBase; - std::string HandleRequestThrow(const server::http::HttpRequest& request, server::request::RequestContext& ctx) - const override { + std::string HandleRequest(server::http::HttpRequest& request, server::request::RequestContext& ctx) const override { request.GetHttpResponse().SetContentType(http::content_type::kTextPlain); return "Hello world, " + ctx.GetData("name") + "!\n"; } diff --git a/samples/postgres_service/postgres_service.cpp b/samples/postgres_service/postgres_service.cpp index 0afcae2881db..9af390d92499 100644 --- a/samples/postgres_service/postgres_service.cpp +++ b/samples/postgres_service/postgres_service.cpp @@ -22,8 +22,7 @@ class KeyValue final : public server::handlers::HttpHandlerBase { KeyValue(const components::ComponentConfig& config, const components::ComponentContext& context); - std::string HandleRequestThrow(const server::http::HttpRequest& request, server::request::RequestContext&) - const override; + std::string HandleRequest(server::http::HttpRequest& request, server::request::RequestContext&) const override; private: std::string GetValue(std::string_view key, const server::http::HttpRequest& request) const; @@ -48,8 +47,7 @@ KeyValue::KeyValue(const components::ComponentConfig& config, const components:: /// [Postgres service sample - component constructor] /// [Postgres service sample - HandleRequestThrow] -std::string KeyValue::HandleRequestThrow(const server::http::HttpRequest& request, server::request::RequestContext&) - const { +std::string KeyValue::HandleRequest(server::http::HttpRequest& request, server::request::RequestContext&) const { const auto& key = request.GetArg("key"); if (key.empty()) { throw server::handlers::ClientError(server::handlers::ExternalBody{"No 'key' query argument"}); diff --git a/samples/redis_service/redis_service.cpp b/samples/redis_service/redis_service.cpp index c404e80fc542..681e0854a68f 100644 --- a/samples/redis_service/redis_service.cpp +++ b/samples/redis_service/redis_service.cpp @@ -23,8 +23,7 @@ class EvalSha final : public server::handlers::HttpHandlerBase { EvalSha(const components::ComponentConfig& config, const components::ComponentContext& context); - std::string HandleRequestThrow(const server::http::HttpRequest& request, server::request::RequestContext&) - const override; + std::string HandleRequest(server::http::HttpRequest& request, server::request::RequestContext&) const override; private: std::string EvalShaRequest(const server::http::HttpRequest& request) const; @@ -40,8 +39,7 @@ class KeyValue final : public server::handlers::HttpHandlerBase { KeyValue(const components::ComponentConfig& config, const components::ComponentContext& context); - std::string HandleRequestThrow(const server::http::HttpRequest& request, server::request::RequestContext&) - const override; + std::string HandleRequest(server::http::HttpRequest& request, server::request::RequestContext&) const override; private: std::string GetValue(std::string_view key, const server::http::HttpRequest& request) const; @@ -65,8 +63,7 @@ KeyValue::KeyValue(const components::ComponentConfig& config, const components:: /// [Redis service sample - component constructor] /// [Redis service sample - HandleRequestThrow] -std::string -KeyValue::HandleRequestThrow(const server::http::HttpRequest& request, server::request::RequestContext& /*context*/) +std::string KeyValue::HandleRequest(server::http::HttpRequest& request, server::request::RequestContext& /*context*/) const { const auto& key = request.GetArg("key"); if (key.empty()) { @@ -124,8 +121,7 @@ EvalSha::EvalSha(const components::ComponentConfig& config, const components::Co : server::handlers::HttpHandlerBase(config, context), redis_client_{context.FindComponent("key-value-database").GetClient("taxi-tmp")} {} -std::string EvalSha::HandleRequestThrow(const server::http::HttpRequest& request, server::request::RequestContext&) - const { +std::string EvalSha::HandleRequest(server::http::HttpRequest& request, server::request::RequestContext&) const { const auto& command = request.GetArg("command"); request.GetHttpResponse().SetContentType(http::content_type::kTextPlain); diff --git a/samples/testsuite-support/src/dynamic_config.cpp b/samples/testsuite-support/src/dynamic_config.cpp index 31b9fb45aa34..a82a4bd51e95 100644 --- a/samples/testsuite-support/src/dynamic_config.cpp +++ b/samples/testsuite-support/src/dynamic_config.cpp @@ -17,8 +17,8 @@ DynamicConfig::DynamicConfig(const components::ComponentConfig& config, const co : server::handlers::HttpHandlerBase(config, context), config_source_(context.FindComponent().GetSource()) {} -std::string DynamicConfig::HandleRequestThrow( - [[maybe_unused]] const server::http::HttpRequest& request, +std::string DynamicConfig::HandleRequest( + [[maybe_unused]] server::http::HttpRequest& request, [[maybe_unused]] server::request::RequestContext& context ) const { request.GetHttpResponse().SetContentType(http::content_type::kTextPlain); diff --git a/samples/testsuite-support/src/dynamic_config.hpp b/samples/testsuite-support/src/dynamic_config.hpp index dd93d8c0f78c..3de531eee8c9 100644 --- a/samples/testsuite-support/src/dynamic_config.hpp +++ b/samples/testsuite-support/src/dynamic_config.hpp @@ -12,7 +12,7 @@ class DynamicConfig final : public server::handlers::HttpHandlerBase { DynamicConfig(const components::ComponentConfig&, const components::ComponentContext&); - std::string HandleRequestThrow(const server::http::HttpRequest& request, server::request::RequestContext& context) + std::string HandleRequest(server::http::HttpRequest& request, server::request::RequestContext& context) const override; private: diff --git a/samples/ydb_service/views/bson-reading/post/view.cpp b/samples/ydb_service/views/bson-reading/post/view.cpp index 499840b622ce..b27e34f4f5c3 100644 --- a/samples/ydb_service/views/bson-reading/post/view.cpp +++ b/samples/ydb_service/views/bson-reading/post/view.cpp @@ -7,8 +7,7 @@ namespace sample { -std::string -BsonReadingHandler::HandleRequestThrow(const server::http::HttpRequest& request, server::request::RequestContext&) +std::string BsonReadingHandler::HandleRequest(server::http::HttpRequest& request, server::request::RequestContext&) const { static const std::string kSelectQuery = R"( --!syntax_v1 diff --git a/samples/ydb_service/views/bson-reading/post/view.hpp b/samples/ydb_service/views/bson-reading/post/view.hpp index 732a06302679..e2c5970daca8 100644 --- a/samples/ydb_service/views/bson-reading/post/view.hpp +++ b/samples/ydb_service/views/bson-reading/post/view.hpp @@ -15,7 +15,7 @@ class BsonReadingHandler final : public server::handlers::HttpHandlerBase { : HttpHandlerBase(config, context), ydb_client_(context.FindComponent().GetTableClient("sampledb")) {} - std::string HandleRequestThrow(const server::http::HttpRequest& request, server::request::RequestContext& context) + std::string HandleRequest(server::http::HttpRequest& request, server::request::RequestContext& context) const override; private: diff --git a/samples/ydb_service/views/bson-upserting/post/view.cpp b/samples/ydb_service/views/bson-upserting/post/view.cpp index a90cb9156299..47e36640bc81 100644 --- a/samples/ydb_service/views/bson-upserting/post/view.cpp +++ b/samples/ydb_service/views/bson-upserting/post/view.cpp @@ -16,8 +16,7 @@ VALUES ($id, $doc); )"; } -std::string -BsonUpsertingHandler::HandleRequestThrow(const server::http::HttpRequest& request, server::request::RequestContext&) +std::string BsonUpsertingHandler::HandleRequest(server::http::HttpRequest& request, server::request::RequestContext&) const { const auto& id = request.GetArg("id"); const auto& body = request.RequestBody(); diff --git a/samples/ydb_service/views/bson-upserting/post/view.hpp b/samples/ydb_service/views/bson-upserting/post/view.hpp index a03b25766dcf..d005c21cb6aa 100644 --- a/samples/ydb_service/views/bson-upserting/post/view.hpp +++ b/samples/ydb_service/views/bson-upserting/post/view.hpp @@ -15,7 +15,7 @@ class BsonUpsertingHandler final : public server::handlers::HttpHandlerBase { : HttpHandlerBase(config, context), ydb_client_(context.FindComponent().GetTableClient("sampledb")) {} - std::string HandleRequestThrow(const server::http::HttpRequest& request, server::request::RequestContext& context) + std::string HandleRequest(server::http::HttpRequest& request, server::request::RequestContext& context) const override; private: diff --git a/scripts/docs/en/userver/http_server.md b/scripts/docs/en/userver/http_server.md index c240d59e06f5..fc0beb8920f7 100644 --- a/scripts/docs/en/userver/http_server.md +++ b/scripts/docs/en/userver/http_server.md @@ -41,7 +41,7 @@ To enable Streaming API in your handler: ```cpp #include ... - void HandleStreamRequest(const server::http::HttpRequest&, + void HandleStreamRequest(server::http::HttpRequest&, server::request::RequestContext&, server::http::ResponseBodyStream&) const override; ``` diff --git a/scripts/docs/en/userver/tutorial/hello_service.md b/scripts/docs/en/userver/tutorial/hello_service.md index d0549112e224..7a5d26c6f195 100644 --- a/scripts/docs/en/userver/tutorial/hello_service.md +++ b/scripts/docs/en/userver/tutorial/hello_service.md @@ -50,7 +50,7 @@ implementation details are hidden and the header is lightweight to include: HTTP handlers must derive from `server::handlers::HttpHandlerBase` and have a name, that is obtainable at compile time via `kName` variable and is obtainable at runtime via `HandlerName()`. -The primary functionality of the handler should be located in `HandleRequestThrow` function. +The primary functionality of the handler should be located in `HandleRequest` function. Return value of this function is the HTTP response body. If an exception `exc` derived from `server::handlers::CustomHandlerException` is thrown from the function then the HTTP response code will be set to `exc.GetCode()` and `exc.GetExternalErrorBody()` diff --git a/scripts/docs/en/userver/tutorial/http_caching.md b/scripts/docs/en/userver/tutorial/http_caching.md index 82cae3252326..72e37fa76e32 100644 --- a/scripts/docs/en/userver/tutorial/http_caching.md +++ b/scripts/docs/en/userver/tutorial/http_caching.md @@ -141,7 +141,7 @@ should be provided: ### Cache component usage Now the cache could be used just as any other component. For example, a handler -could get a reference to the cache and use it in `HandleRequestThrow`: +could get a reference to the cache and use it in `HandleRequest`: @snippet samples/http_caching/http_caching.cpp HTTP caching sample - GreetUser diff --git a/scripts/docs/en/userver/tutorial/multipart_service.md b/scripts/docs/en/userver/tutorial/multipart_service.md index 39a7365f6790..67ce9922154d 100644 --- a/scripts/docs/en/userver/tutorial/multipart_service.md +++ b/scripts/docs/en/userver/tutorial/multipart_service.md @@ -52,7 +52,7 @@ server::handlers::HttpHandlerBase: @snippet samples/multipart_service/service.cpp Multipart service sample - component The primary functionality of the handler should be located in -`HandleRequestThrow` function. To work with the `multipart/form-data` +`HandleRequest` function. To work with the `multipart/form-data` parameters use the appropriate server::http::HttpRequest functions: