azure-core
raw_response.hpp
Go to the documentation of this file.
1 // Copyright (c) Microsoft Corporation. All rights reserved.
2 // SPDX-License-Identifier: MIT
3 
9 #pragma once
10 
14 
15 #include <memory>
16 #include <string>
17 #include <vector>
18 
19 namespace 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)
73  : RawResponse(
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 
96  RawResponse& operator=(RawResponse const&) = delete;
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 
146 
151  std::string const& GetReasonPhrase() const;
152 
157  CaseInsensitiveMap const& GetHeaders() const;
158 
163  std::unique_ptr<Azure::Core::IO::BodyStream> ExtractBodyStream()
164  {
165  // If m_bodyStream was moved before. nullptr is returned
166  return std::move(this->m_bodyStream);
167  }
168 
173  std::vector<uint8_t>& GetBody() { return this->m_body; }
174 
179  std::vector<uint8_t> const& GetBody() const { return this->m_body; }
180  };
181 }}} // namespace Azure::Core::Http
body_stream.hpp
BodyStream is used to read data to/from a service.
Azure::Core::Http::RawResponse::operator=
RawResponse & operator=(RawResponse const &)=delete
RawResponse cannot be assigned.
Azure::Core::Http::RawResponse::operator=
RawResponse & operator=(RawResponse &&)=delete
RawResponse cannot be moved into.
Azure::Core::Http::RawResponse::RawResponse
RawResponse(RawResponse &&response)=default
Constructs RawResponse by moving in another instance.
Azure::Core::Http::RawResponse::SetBodyStream
void SetBodyStream(std::unique_ptr< Azure::Core::IO::BodyStream > stream)
Set Azure::Core::IO::BodyStream for this HTTP response.
Definition: raw_response.cpp:27
Azure::Core::Http::RawResponse::~RawResponse
~RawResponse()=default
Destructs RawResponse.
Azure::Core::Http::RawResponse::ExtractBodyStream
std::unique_ptr< Azure::Core::IO::BodyStream > ExtractBodyStream()
Get HTTP response body as Azure::Core::IO::BodyStream.
Definition: raw_response.hpp:163
Azure::Core::Http::RawResponse::RawResponse
RawResponse(RawResponse const &response)
Constructs RawResponse from another.
Definition: raw_response.hpp:72
http_status_code.hpp
HTTP status code definition.
Azure::Core::Http::RawResponse
After receiving and interpreting a request message, a server responds with an HTTP response message.
Definition: raw_response.hpp:24
Azure::Core::Http::RawResponse::GetBody
std::vector< uint8_t > & GetBody()
Get HTTP response body as vector of bytes.
Definition: raw_response.hpp:173
Azure::Core::Http::RawResponse::GetBody
std::vector< uint8_t > const & GetBody() const
Get HTTP response body as vector of bytes.
Definition: raw_response.hpp:179
Azure
Azure SDK abstractions.
Definition: azure_assert.hpp:55
Azure::Core::Http::RawResponse::RawResponse
RawResponse(int32_t majorVersion, int32_t minorVersion, HttpStatusCode statusCode, std::string const &reasonPhrase)
Constructs RawResponse.
Definition: raw_response.hpp:56
Azure::Core::Http::RawResponse::SetHeader
void SetHeader(std::string const &name, std::string const &value)
Set an HTTP header to the #RawResponse.
Definition: raw_response.cpp:22
Azure::Core::CaseInsensitiveMap
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
case_insensitive_containers.hpp
A map<string, string> with case-insensitive key comparison.
Azure::Core::Http::RawResponse::GetHeaders
CaseInsensitiveMap const & GetHeaders() const
Get HTTP response headers.
Definition: raw_response.cpp:20
Azure::Core::Http::RawResponse::GetReasonPhrase
std::string const & GetReasonPhrase() const
Get HTTP reason phrase code of the HTTP response.
Definition: raw_response.cpp:18
Azure::Core::Http::RawResponse::GetStatusCode
HttpStatusCode GetStatusCode() const
Get HTTP status code of the HTTP response.
Definition: raw_response.cpp:16
Azure::Core::Http::RawResponse::SetBody
void SetBody(std::vector< uint8_t > body)
Set HTTP response body for this HTTP response.
Definition: raw_response.hpp:136
Azure::Core::Http::HttpStatusCode
HttpStatusCode
Defines the possible HTTP status codes.
Definition: http_status_code.hpp:18