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