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 <string>
8#include <system_error>
9
10#include <userver/utils/not_null.hpp>
11#include <userver/utils/span.hpp>
12#include <userver/utils/zstring_view.hpp>
13
14USERVER_NAMESPACE_BEGIN
15
16namespace tracing {
17class Span;
18}
19
20namespace clients::http {
21
22class RequestState;
23class Response;
24
25/// @brief Auxiliary entity that allows editing request to a client
26/// from middlewares
27class MiddlewareRequest final {
28public:
29 /// @cond
30 explicit MiddlewareRequest(RequestState& state);
31 /// @endcond
32
33 void SetHeader(std::string_view name, std::string_view value);
34
35 void AddQueryParams(std::string_view params);
36
37 void SetTimeout(std::chrono::milliseconds ms);
38
39 void SetProxy(utils::zstring_view value);
40
41 bool IsProxySet() const;
42
43 const std::string& GetOriginalUrl() const;
44
45private:
46 RequestState& state_;
47};
48
49/// @brief Base class for HTTP Client middlewares
51public:
52 virtual ~MiddlewareBase();
53
54 MiddlewareBase(const MiddlewareBase&) = delete;
55 MiddlewareBase(MiddlewareBase&&) = delete;
56
57 MiddlewareBase& operator=(const MiddlewareBase&) = delete;
58 MiddlewareBase& operator=(MiddlewareBase&&) = delete;
59
60 /// @brief The hook is called just after the "external" Span is created.
61 /// You might want to add custom tags from the hook.
62 /// The hook is called only once before any network interaction and is NOT called before each retry.
63 /// The hook is executed in the context of the parent task which created the request.
64 /// The hook does nothing by default.
65 virtual void HookCreateSpan(MiddlewareRequest& request, tracing::Span& span);
66
67 /// @brief The hook is called before actual HTTP request sending and before
68 /// DNS name resolution.
69 /// This hook is called on each retry.
70 /// The hook does nothing by default.
71 ///
72 /// @warning The hook is called in libev thread, not in coroutine context! Do
73 /// not do any heavy work here, offload it to other hooks.
74 virtual void HookPerformRequest(MiddlewareRequest& request);
75
76 /// @brief The hook is called after the HTTP response is received. It does not
77 /// include timeout, network or other error.
78 /// The hook does nothing by default.
79 ///
80 /// @warning The hook is called in libev thread, not in coroutine context! Do
81 /// not do any heavy work here, offload it to other hooks.
82 virtual void HookOnCompleted(MiddlewareRequest& request, Response& response);
83
84 /// @brief The hook is called on timeout and other errors. The hook is not called
85 /// for error HTTP responses, use HookOnCompleted instead.
86 /// The hook does nothing by default.
87 ///
88 /// @warning The hook is called in libev thread, not in coroutine context! Do
89 /// not do any heavy work here, offload it to other hooks.
90 virtual void HookOnError(MiddlewareRequest& request, std::error_code ec);
91
92 /// @brief The hook is called before every call attempt except the first one.
93 /// The hook allows retry by default.
94 /// @return whether the retry is allowed.
95 virtual bool HookOnRetry(MiddlewareRequest& request);
96
97protected:
98 MiddlewareBase();
99};
100
101} // namespace clients::http
102
103USERVER_NAMESPACE_END