userver: userver/clients/http/middlewares/base.hpp Source File
Loading...
Searching...
No Matches
base.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/clients/http/middlewares/base.hpp
4/// @brief @copybrief clients::http::MiddlewareBase
5
6#include <chrono>
7#include <optional>
8#include <string>
9#include <string_view>
10#include <system_error>
11
12#include <userver/utils/not_null.hpp>
13#include <userver/utils/span.hpp>
14#include <userver/utils/zstring_view.hpp>
15
16USERVER_NAMESPACE_BEGIN
17
18namespace tracing {
19class Span;
20}
21
22namespace clients::http {
23
24enum class HttpMethod;
25class RequestState;
26class Response;
27
28/// @brief Auxiliary entity that allows editing request to a client
29/// from middlewares
30class MiddlewareRequest final {
31public:
32 /// @cond
33 explicit MiddlewareRequest(RequestState& state);
34 /// @endcond
35
36 void SetHeader(std::string_view name, std::string_view value);
37
38 void AddQueryParams(std::string_view params);
39
40 void SetTimeout(std::chrono::milliseconds ms);
41
42 void SetProxy(utils::zstring_view value);
43
44 bool IsProxySet() const;
45
46 const std::string& GetOriginalUrl() const;
47
48 /// @brief Returns the HTTP method of the request.
49 HttpMethod GetMethod() const noexcept;
50
51 /// @brief Returns the URL template set by the client, if any.
52 /// The view is valid only during the middleware hook.
53 std::optional<std::string_view> GetUrlTemplate() const noexcept;
54
55private:
56 RequestState& state_;
57};
58
59/// @brief Base class for HTTP Client middlewares
61public:
62 virtual ~MiddlewareBase();
63
64 MiddlewareBase(const MiddlewareBase&) = delete;
65 MiddlewareBase(MiddlewareBase&&) = delete;
66
67 MiddlewareBase& operator=(const MiddlewareBase&) = delete;
68 MiddlewareBase& operator=(MiddlewareBase&&) = delete;
69
70 /// @brief The hook is called just after the "external" Span is created.
71 /// You might want to add custom tags from the hook.
72 /// The hook is called only once before any network interaction and is NOT called before each retry.
73 /// The hook is executed in the context of the parent task which created the request.
74 /// The hook does nothing by default.
75 virtual void HookCreateSpan(MiddlewareRequest& request, tracing::Span& span);
76
77 /// @brief The hook is called before actual HTTP request sending and before
78 /// DNS name resolution.
79 /// This hook is called on each retry.
80 /// The hook does nothing by default.
81 ///
82 /// @warning The hook is called in libev thread, not in coroutine context! Do
83 /// not do any heavy work here, offload it to other hooks.
84 virtual void HookPerformRequest(MiddlewareRequest& request);
85
86 /// @brief The hook is called after the HTTP response is received. It does not
87 /// include timeout, network or other error.
88 /// The hook does nothing by default.
89 ///
90 /// @warning The hook is called in libev thread, not in coroutine context! Do
91 /// not do any heavy work here, offload it to other hooks.
92 virtual void HookOnCompleted(MiddlewareRequest& request, Response& response);
93
94 /// @brief The hook is called on timeout and other errors. The hook is not called
95 /// for error HTTP responses, use HookOnCompleted instead.
96 /// The hook does nothing by default.
97 ///
98 /// @warning The hook is called in libev thread, not in coroutine context! Do
99 /// not do any heavy work here, offload it to other hooks.
100 virtual void HookOnError(MiddlewareRequest& request, std::error_code ec);
101
102 /// @brief Called once at the end of the request, after all retries.
103 /// Includes HTTP responses, transport errors, and deadline failures before sending.
104 /// The span covers the entire request and can be used to measure its duration.
105 ///
106 /// @warning The hook can run outside a coroutine. Do not do heavy work here.
107 virtual void HookOnRequestFinished(const MiddlewareRequest& request, const tracing::Span& span) noexcept;
108
109 /// @brief The hook is called before every call attempt except the first one.
110 /// The hook allows retry by default.
111 /// @return whether the retry is allowed.
112 virtual bool HookOnRetry(MiddlewareRequest& request);
113
114protected:
115 MiddlewareBase();
116};
117
118} // namespace clients::http
119
120USERVER_NAMESPACE_END