azure-core
Loading...
Searching...
No Matches
raw_response.hpp
Go to the documentation of this file.
1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
9#pragma once
10
14
15#include <memory>
16#include <string>
17#include <vector>
18
19namespace Azure { namespace Core { namespace Http {
24 class RawResponse final {
25
26 private:
27 int32_t m_majorVersion;
28 int32_t m_minorVersion;
29 HttpStatusCode m_statusCode;
30 std::string m_reasonPhrase;
31 CaseInsensitiveMap m_headers;
32
33 std::unique_ptr<Azure::Core::IO::BodyStream> m_bodyStream;
34 std::vector<uint8_t> m_body;
35
36 explicit RawResponse(
37 int32_t majorVersion,
38 int32_t minorVersion,
39 HttpStatusCode statusCode,
40 std::string const& reasonPhrase,
41 std::unique_ptr<Azure::Core::IO::BodyStream> BodyStream)
42 : m_majorVersion(majorVersion), m_minorVersion(minorVersion), m_statusCode(statusCode),
43 m_reasonPhrase(reasonPhrase), m_bodyStream(std::move(BodyStream))
44 {
45 }
46
47 public:
56 explicit RawResponse(
57 int32_t majorVersion,
58 int32_t minorVersion,
59 HttpStatusCode statusCode,
60 std::string const& reasonPhrase)
61 : RawResponse(majorVersion, minorVersion, statusCode, reasonPhrase, nullptr)
62 {
63 }
64
72 RawResponse(RawResponse const& response)
74 response.m_majorVersion,
75 response.m_minorVersion,
76 response.m_statusCode,
77 response.m_reasonPhrase)
78 {
79 AZURE_ASSERT(m_bodyStream == nullptr);
80 // Copy body
81 m_body = response.GetBody();
82 }
83
90 RawResponse(RawResponse&& response) = default;
91
97
103
108 ~RawResponse() = default;
109
110 // ===== Methods used to build HTTP response =====
111
122 void SetHeader(std::string const& name, std::string const& value);
123
129 void SetBodyStream(std::unique_ptr<Azure::Core::IO::BodyStream> stream);
130
136 void SetBody(std::vector<uint8_t> body) { this->m_body = std::move(body); }
137
138 // adding getters for version and stream body. Clang will complain on macOS if we have unused
139 // fields in a class
140
145 int32_t GetMajorVersion() const { return m_majorVersion; }
146
151 int32_t GetMinorVersion() const { return m_minorVersion; }
152
158
163 std::string const& GetReasonPhrase() const;
164
169 CaseInsensitiveMap const& GetHeaders() const;
170
175 std::unique_ptr<Azure::Core::IO::BodyStream> ExtractBodyStream()
176 {
177 // If m_bodyStream was moved before. nullptr is returned
178 return std::move(this->m_bodyStream);
179 }
180
185 std::vector<uint8_t>& GetBody() { return this->m_body; }
186
191 std::vector<uint8_t> const& GetBody() const { return this->m_body; }
192 };
193}}} // namespace Azure::Core::Http
#define AZURE_ASSERT(exp)
Azure specific assert macro.
Definition azure_assert.hpp:51
BodyStream is used to read data to/from a service.
A map<string, string> with case-insensitive key comparison.
std::map< std::string, std::string, _internal::StringExtensions::CaseInsensitiveComparator > CaseInsensitiveMap
A type alias of std::map<std::string, std::string> with case-insensitive key comparison.
Definition case_insensitive_containers.hpp:24
After receiving and interpreting a request message, a server responds with an HTTP response message.
Definition raw_response.hpp:24
std::vector< uint8_t > & GetBody()
Get HTTP response body as vector of bytes.
Definition raw_response.hpp:185
HttpStatusCode GetStatusCode() const
Get HTTP status code of the HTTP response.
Definition raw_response.cpp:11
void SetBody(std::vector< uint8_t > body)
Set HTTP response body for this HTTP response.
Definition raw_response.hpp:136
void SetHeader(std::string const &name, std::string const &value)
Set an HTTP header to the Azure::Core::Http::RawResponse.
Definition raw_response.cpp:17
RawResponse(RawResponse &&response)=default
Constructs RawResponse by moving in another instance.
std::unique_ptr< Azure::Core::IO::BodyStream > ExtractBodyStream()
Get HTTP response body as Azure::Core::IO::BodyStream.
Definition raw_response.hpp:175
std::string const & GetReasonPhrase() const
Get HTTP reason phrase code of the HTTP response.
Definition raw_response.cpp:13
~RawResponse()=default
Destructs RawResponse.
CaseInsensitiveMap const & GetHeaders() const
Get HTTP response headers.
Definition raw_response.cpp:15
RawResponse(RawResponse const &response)
Constructs RawResponse from another.
Definition raw_response.hpp:72
RawResponse(int32_t majorVersion, int32_t minorVersion, HttpStatusCode statusCode, std::string const &reasonPhrase)
Constructs RawResponse.
Definition raw_response.hpp:56
int32_t GetMajorVersion() const
Get HTTP protocol major version.
Definition raw_response.hpp:145
int32_t GetMinorVersion() const
Get HTTP protocol minor version.
Definition raw_response.hpp:151
std::vector< uint8_t > const & GetBody() const
Get HTTP response body as vector of bytes.
Definition raw_response.hpp:191
void SetBodyStream(std::unique_ptr< Azure::Core::IO::BodyStream > stream)
Set Azure::Core::IO::BodyStream for this HTTP response.
Definition raw_response.cpp:22
RawResponse & operator=(RawResponse const &)=delete
RawResponse cannot be assigned.
RawResponse & operator=(RawResponse &&)=delete
RawResponse cannot be moved into.
HTTP status code definition.
HttpStatusCode
Defines the possible HTTP status codes.
Definition http_status_code.hpp:18
Compute the hash value for the input binary data, using SHA256, SHA384 and SHA512.
Definition azure_assert.hpp:57