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
27/// Server parts of the HTTP protocol implementation.
28namespace server::http {
29
30class HttpRequestImpl;
31
32/// @brief HTTP Request data
33class HttpRequest final {
34 public:
35 using HeadersMap = USERVER_NAMESPACE::http::headers::HeaderMap;
36
37 using HeadersMapKeys = decltype(utils::impl::MakeKeysView(HeadersMap()));
38
39 using CookiesMap =
40 std::unordered_map<std::string, std::string, utils::StrCaseHash>;
41
42 using CookiesMapKeys = decltype(utils::impl::MakeKeysView(CookiesMap()));
43
44 /// @cond
45 explicit HttpRequest(HttpRequestImpl& impl);
46 ~HttpRequest() = default;
47
48 request::ResponseBase& GetResponse() const;
49 /// @endcond
50
51 /// @brief Returns a container that should be filled with response data to
52 /// this request.
53 HttpResponse& GetHttpResponse() const;
54
55 const HttpMethod& GetMethod() const;
56 const std::string& GetMethodStr() const;
57
58 /// @return Major version of HTTP. For example, for HTTP 1.0 it returns 1
59 int GetHttpMajor() const;
60
61 /// @return Minor version of HTTP. For example, for HTTP 1.0 it returns 0
62 int GetHttpMinor() const;
63
64 /// @return Request URL
65 const std::string& GetUrl() const;
66
67 /// @return Request path
68 const std::string& GetRequestPath() const;
69
70 /// @return Request path suffix, i.e. part of the path that remains after
71 /// matching the path of a handler.
72 const std::string& GetPathSuffix() const;
73
74 std::chrono::duration<double> GetRequestTime() const;
75 std::chrono::duration<double> GetResponseTime() const;
76
77 /// @return Host from the URL.
78 const std::string& GetHost() const;
79
80 /// @return First argument value with name arg_name or an empty string if no
81 /// such argument. Arguments are extracted from query part of the URL and from
82 /// the HTTP body.
83 const std::string& GetArg(const std::string& arg_name) const;
84
85 /// @return Argument values with name arg_name or an empty string if no
86 /// such argument. Arguments are extracted from query part of the URL and from
87 /// the HTTP body.
89 const std::string& arg_name) const;
90
91 /// @return true if argument with name arg_name exists, false otherwise.
92 /// Arguments are extracted from query part of the URL and from
93 /// the HTTP body.
94 bool HasArg(const std::string& arg_name) const;
95
96 /// @return Count of arguments. Arguments are extracted from query part of the
97 /// URL and from the HTTP body.
99
100 /// @return List of names of arguments. Arguments are extracted from query
101 /// part of the URL and from the HTTP body.
103
104 /// @return First argument value with name arg_name from multipart/form-data
105 /// request or an empty FormDataArg if no such argument.
106 const FormDataArg& GetFormDataArg(const std::string& arg_name) const;
107
108 /// @return Argument values with name arg_name from multipart/form-data
109 /// request or an empty FormDataArg if no such argument.
111 const std::string& arg_name) const;
112
113 /// @return true if argument with name arg_name exists in multipart/form-data
114 /// request, false otherwise.
115 bool HasFormDataArg(const std::string& arg_name) const;
116
117 /// @return Count of multipart/form-data arguments.
119
120 /// @return List of names of multipart/form-data arguments.
122
123 /// @return Named argument from URL path with wildcards.
124 const std::string& GetPathArg(const std::string& arg_name) const;
125
126 /// @return Argument from URL path with wildcards by its 0-based index
127 const std::string& GetPathArg(size_t index) const;
128
129 /// @return true if named argument from URL path with wildcards exists, false
130 /// otherwise.
131 bool HasPathArg(const std::string& arg_name) const;
132
133 /// @return true if argument with index from URL path with wildcards exists,
134 /// false otherwise.
135 bool HasPathArg(size_t index) const;
136
137 /// @return Number of wildcard arguments in URL path.
139
140 /// @return Value of the header with case insensitive name header_name, or an
141 /// empty string if no such header.
142 const std::string& GetHeader(std::string_view header_name) const;
143 /// @overload
144 const std::string& GetHeader(
145 const USERVER_NAMESPACE::http::headers::PredefinedHeader& header_name)
146 const;
147 const HeadersMap& GetHeaders() const;
148
149 /// @return true if header with case insensitive name header_name exists,
150 /// false otherwise.
151 bool HasHeader(std::string_view header_name) const;
152 /// @overload
153 bool HasHeader(const USERVER_NAMESPACE::http::headers::PredefinedHeader&
154 header_name) const;
155
156 /// @return Number of headers.
158
159 /// @return List of headers names.
161
162 /// Removes the header with case insensitive name header_name.
163 void RemoveHeader(std::string_view header_name);
164 /// @overload
166 const USERVER_NAMESPACE::http::headers::PredefinedHeader& header_name);
167
168 /// @return Value of the cookie with case sensitive name cookie_name, or an
169 /// empty string if no such cookie exists.
170 const std::string& GetCookie(const std::string& cookie_name) const;
171
172 /// @return true if cookie with case sensitive name cookie_name exists, false
173 /// otherwise.
174 bool HasCookie(const std::string& cookie_name) const;
175
176 /// @return Number of cookies.
178
179 /// @return List of cookies names.
181
182 /// @return HTTP body.
183 const std::string& RequestBody() const;
184
185 /// @return HTTP headers.
186 const HeadersMap& RequestHeaders() const;
187
188 /// @return HTTP cookies.
190
191 /// @cond
192 void SetRequestBody(std::string body);
193 void ParseArgsFromBody();
194
195 std::chrono::steady_clock::time_point GetStartTime() const;
196 /// @endcond
197
198 /// @brief Set the response status code.
199 ///
200 /// Equivalent to this->GetHttpResponse().SetStatus(status).
201 void SetResponseStatus(HttpStatus status) const;
202
203 /// @return true if the body of the request was compressed
204 bool IsBodyCompressed() const;
205
206 /// @cond
207 void SetUpgradeWebsocket(
208 std::function<void(std::unique_ptr<engine::io::RwBase>&&,
209 engine::io::Sockaddr&&)>
210 cb) const;
211 void DoUpgrade(std::unique_ptr<engine::io::RwBase>&&,
212 engine::io::Sockaddr&& peer_name) const;
213 /// @endcond
214
215 private:
216 HttpRequestImpl& impl_;
217};
218
219} // namespace server::http
220
221USERVER_NAMESPACE_END