userver: userver/clients/http/response.hpp Source File
Loading...
Searching...
No Matches
response.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/clients/http/response.hpp
4/// @brief @copybrief clients::http::Response
5
6#include <iosfwd>
7#include <string>
8#include <unordered_map>
9
10#include <userver/clients/http/error.hpp>
11#include <userver/clients/http/local_stats.hpp>
12#include <userver/http/header_map.hpp>
13#include <userver/server/http/http_response_cookie.hpp>
14#include <userver/utils/str_icase.hpp>
15
16USERVER_NAMESPACE_BEGIN
17
18namespace clients::http {
19
20/// https://en.wikipedia.org/wiki/List_of_HTTP_status_codes
22 Invalid = 0,
23
24 // 1xx informational response
25 Continue = 100,
26 SwitchingProtocols = 101,
27 Processing = 102,
28 EarlyHints = 103,
29
30 // 2xx success
31 OK = 200,
32 Created = 201,
33 Accepted = 202,
34 NonAuthoritativeInformation = 203,
35 NoContent = 204,
36 ResetContent = 205,
37 PartialContent = 206,
38 MultiStatus = 207,
39 AlreadyReported = 208,
40 ThisIsFine = 218,
41 IMUsed = 226,
42
43 // 3xx redirection
44 MultipleChoices = 300,
45 MovedPermanently = 301,
46 Found = 302,
47 SeeOther = 303,
48 NotModified = 304,
49 UseProxy = 305,
50 SwitchProxy = 306,
51 TemporaryRedirect = 307,
52 PermanentRedirect = 308,
53
54 // 4xx client errors
55 BadRequest = 400,
56 Unauthorized = 401,
57 PaymentRequired = 402,
58 Forbidden = 403,
59 NotFound = 404,
60 MethodNotAllowed = 405,
61 NotAcceptable = 406,
62 ProxyAuthenticationRequired = 407,
63 RequestTimeout = 408,
64 Conflict = 409,
65 Gone = 410,
66 LengthRequired = 411,
67 PreconditionFailed = 412,
68 PayloadTooLarge = 413,
69 UriTooLong = 414,
70 UnsupportedMediaType = 415,
71 RangeNotSatisfiable = 416,
72 ExpectationFailed = 417,
73 ImATeapot = 418,
74 PageExpired = 419,
75 MethodFailure = 420,
76 MisdirectedRequest = 421,
77 UnprocessableEntity = 422,
78 Locked = 423,
79 FailedDependency = 424,
80 TooEarly = 425,
81 UpgradeRequired = 426,
82 PreconditionRequired = 428,
83 TooManyRequests = 429,
84 RequestHeaderFieldsTooLarge = 431,
85 NginxNoResponse = 444,
86 UnavailableForLegalReasons = 451,
87 NginxRequestHeaderTooLarge = 494,
88 NginxSSLCertificateError = 495,
89 NginxSSLCertificateRequired = 496,
90 NginxHTTPRequestSenttoHTTPSPort = 497,
91 NginxClientClosedRequest = 499,
92
93 // 5xx server errors
94 InternalServerError = 500,
95 NotImplemented = 501,
96 BadGateway = 502,
97 ServiceUnavailable = 503,
98 GatewayTimeout = 504,
99 HttpVersionNotSupported = 505,
100 VariantAlsoNegotiates = 506,
101 InsufficientStorage = 507,
102 LoopDetected = 508,
103 BandwidthLimitExceeded = 509,
104 NotExtended = 510,
105 NetworkAuthenticationRequired = 511,
106 WebServerIsDown = 520,
107 ConnectionTimedOut = 522,
108 OriginIsUnreachable = 523,
109 TimeoutOccurred = 524,
110 SslHandshakeFailed = 525,
111 InvalidSslCertificate = 526,
112};
113
114std::ostream& operator<<(std::ostream& os, Status s);
115
116/// Headers container type
117using Headers = USERVER_NAMESPACE::http::headers::HeaderMap;
118
119/// Class that will be returned for successful request
120class Response final {
121 public:
122 using CookiesMap = server::http::Cookie::CookiesMap;
123
124 Response() = default;
125
126 /// response string
127 std::string& sink_string() { return response_; }
128
129 /// body as string
130 std::string body() const& { return response_; }
131 std::string&& body() && { return std::move(response_); }
132
133 /// body as string_view
134 std::string_view body_view() const { return response_; }
135
136 /// return reference to headers
137 const Headers& headers() const { return headers_; }
138 Headers& headers() { return headers_; }
139 const CookiesMap& cookies() const { return cookies_; }
140 CookiesMap& cookies() { return cookies_; }
141
142 /// status_code
144 /// check status code
145 bool IsOk() const { return status_code() == Status::OK; }
146
147 static void RaiseForStatus(int code, const LocalStats& stats);
148
149 void raise_for_status() const;
150
151 /// returns statistics on request execution like count of opened sockets,
152 /// connect time...
153 LocalStats GetStats() const;
154
155 void SetStats(const LocalStats& stats) { stats_ = stats; }
156 void SetStatusCode(Status status_code) { status_code_ = status_code; }
157
158 private:
159 Headers headers_;
160 CookiesMap cookies_;
161 std::string response_;
162 Status status_code_{Status::Invalid};
163 LocalStats stats_;
164};
165
166} // namespace clients::http
167
168USERVER_NAMESPACE_END