userver: userver/server/http/http_request.hpp Source File
⚠️ This is the documentation for an old userver version. Click here to switch to the latest version.
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages Concepts
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 const std::string& GetArg(std::string_view arg_name) const;
90
91 /// @return Argument values with name `arg_name` or an empty vector if no
92 /// such argument.
93 /// Arguments are extracted from:
94 /// - query part of the URL,
95 /// - the HTTP body (only if `parse_args_from_body: true` for handler is set).
97
98 /// @return true if argument with name arg_name exists, false otherwise.
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 bool HasArg(std::string_view arg_name) const;
103
104 /// @return Count of arguments.
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).
109
110 /// @return List of names of arguments.
111 /// Arguments are extracted from:
112 /// - query part of the URL,
113 /// - the HTTP body (only if `parse_args_from_body: true` for handler is set).
115
116 /// @return First argument value with name arg_name from multipart/form-data
117 /// request or an empty FormDataArg if no such argument.
118 const FormDataArg& GetFormDataArg(std::string_view arg_name) const;
119
120 /// @return Argument values with name arg_name from multipart/form-data
121 /// request or an empty FormDataArg if no such argument.
123 std::string_view arg_name) const;
124
125 /// @return true if argument with name arg_name exists in multipart/form-data
126 /// request, false otherwise.
127 bool HasFormDataArg(std::string_view arg_name) const;
128
129 /// @return Count of multipart/form-data arguments.
131
132 /// @return List of names of multipart/form-data arguments.
134
135 /// @return Named argument from URL path with wildcards.
136 const std::string& GetPathArg(std::string_view arg_name) const;
137
138 /// @return Argument from URL path with wildcards by its 0-based index
139 const std::string& GetPathArg(size_t index) const;
140
141 /// @return true if named argument from URL path with wildcards exists, false
142 /// otherwise.
143 bool HasPathArg(std::string_view arg_name) const;
144
145 /// @return true if argument with index from URL path with wildcards exists,
146 /// false otherwise.
147 bool HasPathArg(size_t index) const;
148
149 /// @return Number of wildcard arguments in URL path.
151
152 /// @return Value of the header with case insensitive name header_name, or an
153 /// empty string if no such header.
154 const std::string& GetHeader(std::string_view header_name) const;
155 /// @overload
156 const std::string& GetHeader(
157 const USERVER_NAMESPACE::http::headers::PredefinedHeader& header_name)
158 const;
159 const HeadersMap& GetHeaders() const;
160
161 /// @return true if header with case insensitive name header_name exists,
162 /// false otherwise.
163 bool HasHeader(std::string_view header_name) const;
164 /// @overload
165 bool HasHeader(const USERVER_NAMESPACE::http::headers::PredefinedHeader&
166 header_name) const;
167
168 /// @return Number of headers.
170
171 /// @return List of headers names.
173
174 /// Removes the header with case insensitive name header_name.
175 void RemoveHeader(std::string_view header_name);
176 /// @overload
178 const USERVER_NAMESPACE::http::headers::PredefinedHeader& header_name);
179
180 /// @return Value of the cookie with case sensitive name cookie_name, or an
181 /// empty string if no such cookie exists.
182 const std::string& GetCookie(const std::string& cookie_name) const;
183
184 /// @return true if cookie with case sensitive name cookie_name exists, false
185 /// otherwise.
186 bool HasCookie(const std::string& cookie_name) const;
187
188 /// @return Number of cookies.
190
191 /// @return List of cookies names.
193
194 /// @return HTTP body.
195 const std::string& RequestBody() const;
196
197 /// @return HTTP headers.
198 const HeadersMap& RequestHeaders() const;
199
200 /// @return HTTP cookies.
202
203 /// @cond
204 void SetRequestBody(std::string body);
205 void ParseArgsFromBody();
206
207 std::chrono::steady_clock::time_point GetStartTime() const;
208 /// @endcond
209
210 /// @brief Set the response status code.
211 ///
212 /// Equivalent to this->GetHttpResponse().SetStatus(status).
213 void SetResponseStatus(HttpStatus status) const;
214
215 /// @return true if the body of the request was compressed
216 bool IsBodyCompressed() const;
217
218 /// @cond
219 void SetUpgradeWebsocket(
220 std::function<void(std::unique_ptr<engine::io::RwBase>&&,
221 engine::io::Sockaddr&&)>
222 cb) const;
223 void DoUpgrade(std::unique_ptr<engine::io::RwBase>&&,
224 engine::io::Sockaddr&& peer_name) const;
225 /// @endcond
226
227 private:
228 friend class handlers::HttpHandlerBase;
229
230 HttpRequestImpl& impl_;
231};
232
233} // namespace server::http
234
235USERVER_NAMESPACE_END