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/http/header_map.hpp>
13#include <userver/logging/log_helper_fwd.hpp>
14#include <userver/server/http/form_data_arg.hpp>
15#include <userver/server/http/http_method.hpp>
16#include <userver/server/http/http_response.hpp>
17#include <userver/utils/impl/projecting_view.hpp>
18#include <userver/utils/str_icase.hpp>
19
20USERVER_NAMESPACE_BEGIN
21
22namespace engine::io {
23class Socket;
24class Sockaddr;
25} // namespace engine::io
26
27namespace server::handlers {
28class HttpHandlerBase;
29}
30
31/// Server parts of the HTTP protocol implementation.
32namespace server::http {
33
34class HttpRequestImpl;
35
36/// @brief HTTP Request data
37class HttpRequest final {
38 public:
39 using HeadersMap = USERVER_NAMESPACE::http::headers::HeaderMap;
40
41 using HeadersMapKeys = decltype(utils::impl::MakeKeysView(HeadersMap()));
42
43 using CookiesMap =
44 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 First argument value with name `arg_name` or an empty string if no
85 /// such argument.
86 /// Arguments are extracted from:
87 /// - query part of the URL,
88 /// - the HTTP body (only if `parse_args_from_body: true` for handler is set).
89 ///
90 /// In both cases, arg keys and values are url-decoded automatically when
91 /// parsing into the HttpRequest.
92 const std::string& GetArg(std::string_view arg_name) const;
93
94 /// @return Argument values with name `arg_name` or an empty vector if no
95 /// such argument.
96 /// Arguments are extracted from:
97 /// - query part of the URL,
98 /// - the HTTP body (only if `parse_args_from_body: true` for handler is set).
99 ///
100 /// In both cases, arg keys and values are url-decoded automatically when
101 /// parsing into the HttpRequest.
103
104 /// @return true if argument with name arg_name exists, false otherwise.
105 /// Arguments are extracted from:
106 /// - query part of the URL,
107 /// - the HTTP body (only if `parse_args_from_body: true` for handler is set).
108 ///
109 /// In both cases, arg keys and values are url-decoded automatically when
110 /// parsing into the HttpRequest.
111 bool HasArg(std::string_view arg_name) const;
112
113 /// @return Count of arguments.
114 /// Arguments are extracted from:
115 /// - query part of the URL,
116 /// - the HTTP body (only if `parse_args_from_body: true` for handler is set).
118
119 /// @return List of names of arguments.
120 /// Arguments are extracted from:
121 /// - query part of the URL,
122 /// - the HTTP body (only if `parse_args_from_body: true` for handler is set).
124
125 /// @return First argument value with name arg_name from multipart/form-data
126 /// request or an empty FormDataArg if no such argument.
127 const FormDataArg& GetFormDataArg(std::string_view arg_name) const;
128
129 /// @return Argument values with name arg_name from multipart/form-data
130 /// request or an empty FormDataArg if no such argument.
132 std::string_view arg_name) const;
133
134 /// @return true if argument with name arg_name exists in multipart/form-data
135 /// request, false otherwise.
136 bool HasFormDataArg(std::string_view arg_name) const;
137
138 /// @return Count of multipart/form-data arguments.
140
141 /// @return List of names of multipart/form-data arguments.
143
144 /// @return Named argument from URL path with wildcards.
145 /// @note Path args are currently NOT url-decoded automatically.
146 const std::string& GetPathArg(std::string_view arg_name) const;
147
148 /// @return Argument from URL path with wildcards by its 0-based index.
149 /// @note Path args are currently NOT url-decoded automatically.
150 const std::string& GetPathArg(size_t index) const;
151
152 /// @return true if named argument from URL path with wildcards exists, false
153 /// otherwise.
154 bool HasPathArg(std::string_view arg_name) const;
155
156 /// @return true if argument with index from URL path with wildcards exists,
157 /// false otherwise.
158 bool HasPathArg(size_t index) const;
159
160 /// @return Number of wildcard arguments in URL path.
162
163 /// @return Value of the header with case insensitive name header_name, or an
164 /// empty string if no such header.
165 const std::string& GetHeader(std::string_view header_name) const;
166 /// @overload
167 const std::string& GetHeader(
168 const USERVER_NAMESPACE::http::headers::PredefinedHeader& header_name)
169 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&
177 header_name) const;
178
179 /// @return Number of headers.
181
182 /// @return List of headers names.
184
185 /// Removes the header with case insensitive name header_name.
186 void RemoveHeader(std::string_view header_name);
187 /// @overload
189 const USERVER_NAMESPACE::http::headers::PredefinedHeader& header_name);
190
191 /// @return Value of the cookie with case sensitive name cookie_name, or an
192 /// empty string if no such cookie exists.
193 const std::string& GetCookie(const std::string& cookie_name) const;
194
195 /// @return true if cookie with case sensitive name cookie_name exists, false
196 /// otherwise.
197 bool HasCookie(const std::string& cookie_name) const;
198
199 /// @return Number of cookies.
201
202 /// @return List of cookies names.
204
205 /// @return HTTP body.
206 const std::string& RequestBody() const;
207
208 /// @return HTTP headers.
209 const HeadersMap& RequestHeaders() const;
210
211 /// @return HTTP cookies.
213
214 /// @cond
215 void SetRequestBody(std::string body);
216 void ParseArgsFromBody();
217
218 std::chrono::steady_clock::time_point GetStartTime() const;
219 /// @endcond
220
221 /// @brief Set the response status code.
222 ///
223 /// Equivalent to this->GetHttpResponse().SetStatus(status).
224 void SetResponseStatus(HttpStatus status) const;
225
226 /// @return true if the body of the request was compressed
227 bool IsBodyCompressed() const;
228
229 /// @cond
230 void SetUpgradeWebsocket(
231 std::function<void(std::unique_ptr<engine::io::RwBase>&&,
232 engine::io::Sockaddr&&)>
233 cb) const;
234 void DoUpgrade(std::unique_ptr<engine::io::RwBase>&&,
235 engine::io::Sockaddr&& peer_name) const;
236 /// @endcond
237
238 private:
239 friend class handlers::HttpHandlerBase;
240
241 HttpRequestImpl& impl_;
242};
243
244} // namespace server::http
245
246USERVER_NAMESPACE_END