userver: userver/server/http/http_request.hpp Source File
Loading...
Searching...
No Matches
http_request.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/server/http/http_request.hpp
4/// @brief @copybrief server::http::HttpRequest
5
6#include <chrono>
7#include <functional>
8#include <string>
9#include <unordered_map>
10#include <vector>
11
12#include <userver/engine/io/sockaddr.hpp>
13#include <userver/http/header_map.hpp>
14#include <userver/logging/log_helper_fwd.hpp>
15#include <userver/server/http/form_data_arg.hpp>
16#include <userver/server/http/http_method.hpp>
17#include <userver/server/http/http_response.hpp>
18#include <userver/utils/impl/projecting_view.hpp>
19#include <userver/utils/str_icase.hpp>
20
21USERVER_NAMESPACE_BEGIN
22
23namespace engine::io {
24class Socket;
25class Sockaddr;
26} // namespace engine::io
27
28namespace server::handlers {
29class HttpHandlerBase;
30}
31
32/// Server parts of the HTTP protocol implementation.
33namespace server::http {
34
35class HttpRequestImpl;
36
37/// @brief HTTP Request data
38class HttpRequest final {
39public:
40 using HeadersMap = USERVER_NAMESPACE::http::headers::HeaderMap;
41
42 using HeadersMapKeys = decltype(utils::impl::MakeKeysView(HeadersMap()));
43
44 using CookiesMap = std::unordered_map<std::string, std::string, utils::StrCaseHash>;
45
46 using CookiesMapKeys = decltype(utils::impl::MakeKeysView(CookiesMap()));
47
48 /// @cond
49 explicit HttpRequest(HttpRequestImpl& impl);
50 ~HttpRequest() = default;
51
52 request::ResponseBase& GetResponse() const;
53 /// @endcond
54
55 /// @brief Returns a container that should be filled with response data to
56 /// this request.
57 HttpResponse& GetHttpResponse() const;
58
59 const HttpMethod& GetMethod() const;
60 const std::string& GetMethodStr() const;
61
62 /// @return Major version of HTTP. For example, for HTTP 1.0 it returns 1
63 int GetHttpMajor() const;
64
65 /// @return Minor version of HTTP. For example, for HTTP 1.0 it returns 0
66 int GetHttpMinor() const;
67
68 /// @return Request URL
69 const std::string& GetUrl() const;
70
71 /// @return Request path
72 const std::string& GetRequestPath() const;
73
74 /// @return Request path suffix, i.e. part of the path that remains after
75 /// matching the path of a handler.
76 const std::string& GetPathSuffix() const;
77
78 std::chrono::duration<double> GetRequestTime() const;
79 std::chrono::duration<double> GetResponseTime() const;
80
81 /// @return Host from the URL.
82 const std::string& GetHost() const;
83
84 /// @return Request remote address
85 const engine::io::Sockaddr& GetRemoteAddress() const;
86
87 /// @return First argument value with name `arg_name` or an empty string if no
88 /// such argument.
89 /// Arguments are extracted from:
90 /// - query part of the URL,
91 /// - the HTTP body (only if `parse_args_from_body: true` for handler is set).
92 ///
93 /// In both cases, arg keys and values are url-decoded automatically when
94 /// parsing into the HttpRequest.
95 const std::string& GetArg(std::string_view arg_name) const;
96
97 /// @return Argument values with name `arg_name` or an empty vector if no
98 /// such argument.
99 /// Arguments are extracted from:
100 /// - query part of the URL,
101 /// - the HTTP body (only if `parse_args_from_body: true` for handler is set).
102 ///
103 /// In both cases, arg keys and values are url-decoded automatically when
104 /// parsing into the HttpRequest.
106
107 /// @return true if argument with name arg_name exists, false otherwise.
108 /// Arguments are extracted from:
109 /// - query part of the URL,
110 /// - the HTTP body (only if `parse_args_from_body: true` for handler is set).
111 ///
112 /// In both cases, arg keys and values are url-decoded automatically when
113 /// parsing into the HttpRequest.
114 bool HasArg(std::string_view arg_name) const;
115
116 /// @return Count of arguments.
117 /// Arguments are extracted from:
118 /// - query part of the URL,
119 /// - the HTTP body (only if `parse_args_from_body: true` for handler is set).
121
122 /// @return List of names of arguments.
123 /// Arguments are extracted from:
124 /// - query part of the URL,
125 /// - the HTTP body (only if `parse_args_from_body: true` for handler is set).
127
128 /// @return First argument value with name arg_name from multipart/form-data
129 /// request or an empty FormDataArg if no such argument.
130 const FormDataArg& GetFormDataArg(std::string_view arg_name) const;
131
132 /// @return Argument values with name arg_name from multipart/form-data
133 /// request or an empty FormDataArg if no such argument.
135
136 /// @return true if argument with name arg_name exists in multipart/form-data
137 /// request, false otherwise.
138 bool HasFormDataArg(std::string_view arg_name) const;
139
140 /// @return Count of multipart/form-data arguments.
142
143 /// @return List of names of multipart/form-data arguments.
145
146 /// @return Named argument from URL path with wildcards.
147 /// @note Path args are currently NOT url-decoded automatically.
148 const std::string& GetPathArg(std::string_view arg_name) const;
149
150 /// @return Argument from URL path with wildcards by its 0-based index.
151 /// @note Path args are currently NOT url-decoded automatically.
152 const std::string& GetPathArg(size_t index) const;
153
154 /// @return true if named argument from URL path with wildcards exists, false
155 /// otherwise.
156 bool HasPathArg(std::string_view arg_name) const;
157
158 /// @return true if argument with index from URL path with wildcards exists,
159 /// false otherwise.
160 bool HasPathArg(size_t index) const;
161
162 /// @return Number of wildcard arguments in URL path.
164
165 /// @return Value of the header with case insensitive name header_name, or an
166 /// empty string if no such header.
167 const std::string& GetHeader(std::string_view header_name) const;
168 /// @overload
169 const std::string& GetHeader(const USERVER_NAMESPACE::http::headers::PredefinedHeader& header_name) const;
170 const HeadersMap& GetHeaders() const;
171
172 /// @return true if header with case insensitive name header_name exists,
173 /// false otherwise.
174 bool HasHeader(std::string_view header_name) const;
175 /// @overload
176 bool HasHeader(const USERVER_NAMESPACE::http::headers::PredefinedHeader& header_name) const;
177
178 /// @return Number of headers.
180
181 /// @return List of headers names.
183
184 /// Removes the header with case insensitive name header_name.
185 void RemoveHeader(std::string_view header_name);
186 /// @overload
187 void RemoveHeader(const USERVER_NAMESPACE::http::headers::PredefinedHeader& header_name);
188
189 /// @return Value of the cookie with case sensitive name cookie_name, or an
190 /// empty string if no such cookie exists.
191 const std::string& GetCookie(const std::string& cookie_name) const;
192
193 /// @return true if cookie with case sensitive name cookie_name exists, false
194 /// otherwise.
195 bool HasCookie(const std::string& cookie_name) const;
196
197 /// @return Number of cookies.
199
200 /// @return List of cookies names.
202
203 /// @return HTTP body.
204 const std::string& RequestBody() const;
205
206 /// @return HTTP headers.
207 const HeadersMap& RequestHeaders() const;
208
209 /// @return HTTP cookies.
211
212 /// @cond
213 void SetRequestBody(std::string body);
214 void ParseArgsFromBody();
215
216 std::chrono::steady_clock::time_point GetStartTime() const;
217 /// @endcond
218
219 /// @brief Set the response status code.
220 ///
221 /// Equivalent to this->GetHttpResponse().SetStatus(status).
222 void SetResponseStatus(HttpStatus status) const;
223
224 /// @return true if the body of the request is still compressed. In other
225 /// words returns true if the static option `decompress_request` of a handler
226 /// was set to `false` and this is a compressed request.
227 bool IsBodyCompressed() const;
228
229 /// @cond
230 void SetUpgradeWebsocket(std::function<void(std::unique_ptr<engine::io::RwBase>&&, engine::io::Sockaddr&&)> cb
231 ) const;
232 void DoUpgrade(std::unique_ptr<engine::io::RwBase>&&, engine::io::Sockaddr&& peer_name) const;
233 /// @endcond
234
235private:
236 friend class handlers::HttpHandlerBase;
237
238 HttpRequestImpl& impl_;
239};
240
241} // namespace server::http
242
243USERVER_NAMESPACE_END