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
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.
82 /// Arguments are extracted from:
83 /// - query part of the URL,
84 /// - the HTTP body (only if `parse_args_from_body: true` for handler is set).
85 const std::string& GetArg(std::string_view arg_name) const;
86
87 /// @return Argument values with name `arg_name` or an empty vector 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).
93
94 /// @return true if argument with name arg_name exists, false otherwise.
95 /// Arguments are extracted from:
96 /// - query part of the URL,
97 /// - the HTTP body (only if `parse_args_from_body: true` for handler is set).
98 bool HasArg(std::string_view arg_name) const;
99
100 /// @return Count of arguments.
101 /// Arguments are extracted from:
102 /// - query part of the URL,
103 /// - the HTTP body (only if `parse_args_from_body: true` for handler is set).
105
106 /// @return List of names of arguments.
107 /// Arguments are extracted from:
108 /// - query part of the URL,
109 /// - the HTTP body (only if `parse_args_from_body: true` for handler is set).
111
112 /// @return First argument value with name arg_name from multipart/form-data
113 /// request or an empty FormDataArg if no such argument.
114 const FormDataArg& GetFormDataArg(std::string_view arg_name) const;
115
116 /// @return Argument values with name arg_name from multipart/form-data
117 /// request or an empty FormDataArg if no such argument.
119 std::string_view arg_name) const;
120
121 /// @return true if argument with name arg_name exists in multipart/form-data
122 /// request, false otherwise.
123 bool HasFormDataArg(std::string_view arg_name) const;
124
125 /// @return Count of multipart/form-data arguments.
127
128 /// @return List of names of multipart/form-data arguments.
130
131 /// @return Named argument from URL path with wildcards.
132 const std::string& GetPathArg(std::string_view arg_name) const;
133
134 /// @return Argument from URL path with wildcards by its 0-based index
135 const std::string& GetPathArg(size_t index) const;
136
137 /// @return true if named argument from URL path with wildcards exists, false
138 /// otherwise.
139 bool HasPathArg(std::string_view arg_name) const;
140
141 /// @return true if argument with index from URL path with wildcards exists,
142 /// false otherwise.
143 bool HasPathArg(size_t index) const;
144
145 /// @return Number of wildcard arguments in URL path.
147
148 /// @return Value of the header with case insensitive name header_name, or an
149 /// empty string if no such header.
150 const std::string& GetHeader(std::string_view header_name) const;
151 /// @overload
152 const std::string& GetHeader(
153 const USERVER_NAMESPACE::http::headers::PredefinedHeader& header_name)
154 const;
155 const HeadersMap& GetHeaders() const;
156
157 /// @return true if header with case insensitive name header_name exists,
158 /// false otherwise.
159 bool HasHeader(std::string_view header_name) const;
160 /// @overload
161 bool HasHeader(const USERVER_NAMESPACE::http::headers::PredefinedHeader&
162 header_name) const;
163
164 /// @return Number of headers.
166
167 /// @return List of headers names.
169
170 /// Removes the header with case insensitive name header_name.
171 void RemoveHeader(std::string_view header_name);
172 /// @overload
174 const USERVER_NAMESPACE::http::headers::PredefinedHeader& header_name);
175
176 /// @return Value of the cookie with case sensitive name cookie_name, or an
177 /// empty string if no such cookie exists.
178 const std::string& GetCookie(const std::string& cookie_name) const;
179
180 /// @return true if cookie with case sensitive name cookie_name exists, false
181 /// otherwise.
182 bool HasCookie(const std::string& cookie_name) const;
183
184 /// @return Number of cookies.
186
187 /// @return List of cookies names.
189
190 /// @return HTTP body.
191 const std::string& RequestBody() const;
192
193 /// @return HTTP headers.
194 const HeadersMap& RequestHeaders() const;
195
196 /// @return HTTP cookies.
198
199 /// @cond
200 void SetRequestBody(std::string body);
201 void ParseArgsFromBody();
202
203 std::chrono::steady_clock::time_point GetStartTime() const;
204 /// @endcond
205
206 /// @brief Set the response status code.
207 ///
208 /// Equivalent to this->GetHttpResponse().SetStatus(status).
209 void SetResponseStatus(HttpStatus status) const;
210
211 /// @return true if the body of the request was compressed
212 bool IsBodyCompressed() const;
213
214 /// @cond
215 void SetUpgradeWebsocket(
216 std::function<void(std::unique_ptr<engine::io::RwBase>&&,
217 engine::io::Sockaddr&&)>
218 cb) const;
219 void DoUpgrade(std::unique_ptr<engine::io::RwBase>&&,
220 engine::io::Sockaddr&& peer_name) const;
221 /// @endcond
222
223 private:
224 HttpRequestImpl& impl_;
225};
226
227} // namespace server::http
228
229USERVER_NAMESPACE_END