HttpRequestResponse.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /**
  2. * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
  3. * SPDX-License-Identifier: Apache-2.0.
  4. */
  5. #include <aws/crt/http/HttpRequestResponse.h>
  6. #include <aws/crt/io/Stream.h>
  7. #include <aws/http/request_response.h>
  8. #include <aws/io/stream.h>
  9. namespace Aws
  10. {
  11. namespace Crt
  12. {
  13. namespace Http
  14. {
  15. HttpMessage::HttpMessage(Allocator *allocator, struct aws_http_message *message) noexcept
  16. : m_allocator(allocator), m_message(message), m_bodyStream(nullptr)
  17. {
  18. if (message)
  19. {
  20. // Acquire a refcount to keep the message alive until this object dies.
  21. aws_http_message_acquire(this->m_message);
  22. }
  23. }
  24. HttpMessage::~HttpMessage() { m_message = aws_http_message_release(m_message); }
  25. std::shared_ptr<Aws::Crt::Io::InputStream> HttpMessage::GetBody() const noexcept { return m_bodyStream; }
  26. bool HttpMessage::SetBody(const std::shared_ptr<Aws::Crt::Io::IStream> &body) noexcept
  27. {
  28. aws_http_message_set_body_stream(m_message, nullptr);
  29. m_bodyStream = nullptr;
  30. if (body != nullptr)
  31. {
  32. m_bodyStream = MakeShared<Io::StdIOStreamInputStream>(m_allocator, body, m_allocator);
  33. if (m_bodyStream == nullptr || !m_bodyStream)
  34. {
  35. return false;
  36. }
  37. aws_http_message_set_body_stream(m_message, m_bodyStream->GetUnderlyingStream());
  38. }
  39. return true;
  40. }
  41. bool HttpMessage::SetBody(const std::shared_ptr<Aws::Crt::Io::InputStream> &body) noexcept
  42. {
  43. m_bodyStream = body;
  44. aws_http_message_set_body_stream(
  45. m_message, m_bodyStream && *m_bodyStream ? m_bodyStream->GetUnderlyingStream() : nullptr);
  46. return true;
  47. }
  48. size_t HttpMessage::GetHeaderCount() const noexcept { return aws_http_message_get_header_count(m_message); }
  49. Optional<HttpHeader> HttpMessage::GetHeader(size_t index) const noexcept
  50. {
  51. HttpHeader header;
  52. if (aws_http_message_get_header(m_message, &header, index) != AWS_OP_SUCCESS)
  53. {
  54. return Optional<HttpHeader>();
  55. }
  56. return Optional<HttpHeader>(header);
  57. }
  58. bool HttpMessage::AddHeader(const HttpHeader &header) noexcept
  59. {
  60. return aws_http_message_add_header(m_message, header) == AWS_OP_SUCCESS;
  61. }
  62. bool HttpMessage::EraseHeader(size_t index) noexcept
  63. {
  64. return aws_http_message_erase_header(m_message, index) == AWS_OP_SUCCESS;
  65. }
  66. HttpRequest::HttpRequest(Allocator *allocator)
  67. : HttpMessage(allocator, aws_http_message_new_request(allocator))
  68. {
  69. // Releas the refcount as it created, since HttpMessage is taking the ownership
  70. aws_http_message_release(this->m_message);
  71. }
  72. HttpRequest::HttpRequest(Allocator *allocator, struct aws_http_message *message)
  73. : HttpMessage(allocator, message)
  74. {
  75. }
  76. Optional<ByteCursor> HttpRequest::GetMethod() const noexcept
  77. {
  78. ByteCursor method;
  79. if (aws_http_message_get_request_method(m_message, &method) != AWS_OP_SUCCESS)
  80. {
  81. return Optional<ByteCursor>();
  82. }
  83. return Optional<ByteCursor>(method);
  84. }
  85. bool HttpRequest::SetMethod(ByteCursor method) noexcept
  86. {
  87. return aws_http_message_set_request_method(m_message, method) == AWS_OP_SUCCESS;
  88. }
  89. Optional<ByteCursor> HttpRequest::GetPath() const noexcept
  90. {
  91. ByteCursor path;
  92. if (aws_http_message_get_request_path(m_message, &path) != AWS_OP_SUCCESS)
  93. {
  94. return Optional<ByteCursor>();
  95. }
  96. return Optional<ByteCursor>(path);
  97. }
  98. bool HttpRequest::SetPath(ByteCursor path) noexcept
  99. {
  100. return aws_http_message_set_request_path(m_message, path) == AWS_OP_SUCCESS;
  101. }
  102. HttpResponse::HttpResponse(Allocator *allocator)
  103. : HttpMessage(allocator, aws_http_message_new_response(allocator))
  104. {
  105. // Releas the refcount as it created, since HttpMessage is taking the ownership
  106. aws_http_message_release(this->m_message);
  107. }
  108. Optional<int> HttpResponse::GetResponseCode() const noexcept
  109. {
  110. int response = 0;
  111. if (aws_http_message_get_response_status(m_message, &response) != AWS_OP_SUCCESS)
  112. {
  113. return Optional<int>();
  114. }
  115. return response;
  116. }
  117. bool HttpResponse::SetResponseCode(int response) noexcept
  118. {
  119. return aws_http_message_set_response_status(m_message, response) == AWS_OP_SUCCESS;
  120. }
  121. } // namespace Http
  122. } // namespace Crt
  123. } // namespace Aws