Uri.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /**
  2. * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
  3. * SPDX-License-Identifier: Apache-2.0.
  4. */
  5. #include <aws/crt/io/Uri.h>
  6. namespace Aws
  7. {
  8. namespace Crt
  9. {
  10. namespace Io
  11. {
  12. Uri::Uri() noexcept : m_lastError(AWS_ERROR_SUCCESS), m_isInit(false) { AWS_ZERO_STRUCT(m_uri); }
  13. Uri::~Uri()
  14. {
  15. if (m_isInit)
  16. {
  17. aws_uri_clean_up(&m_uri);
  18. m_isInit = false;
  19. }
  20. }
  21. Uri::Uri(const ByteCursor &cursor, Allocator *allocator) noexcept
  22. : m_lastError(AWS_ERROR_SUCCESS), m_isInit(false)
  23. {
  24. if (!aws_uri_init_parse(&m_uri, allocator, &cursor))
  25. {
  26. m_isInit = true;
  27. }
  28. else
  29. {
  30. m_lastError = aws_last_error();
  31. }
  32. }
  33. Uri::Uri(aws_uri_builder_options &builderOptions, Allocator *allocator) noexcept
  34. : m_lastError(AWS_ERROR_SUCCESS), m_isInit(false)
  35. {
  36. if (!aws_uri_init_from_builder_options(&m_uri, allocator, &builderOptions))
  37. {
  38. m_isInit = true;
  39. }
  40. else
  41. {
  42. m_lastError = aws_last_error();
  43. }
  44. }
  45. Uri::Uri(const Uri &other) : m_lastError(AWS_ERROR_SUCCESS), m_isInit(false)
  46. {
  47. if (other.m_isInit)
  48. {
  49. ByteCursor uriCursor = other.GetFullUri();
  50. if (!aws_uri_init_parse(&m_uri, other.m_uri.allocator, &uriCursor))
  51. {
  52. m_isInit = true;
  53. }
  54. else
  55. {
  56. m_lastError = aws_last_error();
  57. }
  58. }
  59. }
  60. Uri &Uri::operator=(const Uri &other)
  61. {
  62. if (this != &other)
  63. {
  64. m_isInit = false;
  65. m_lastError = AWS_ERROR_SUCCESS;
  66. if (other.m_isInit)
  67. {
  68. ByteCursor uriCursor = other.GetFullUri();
  69. if (!aws_uri_init_parse(&m_uri, other.m_uri.allocator, &uriCursor))
  70. {
  71. m_isInit = true;
  72. }
  73. else
  74. {
  75. m_lastError = aws_last_error();
  76. }
  77. }
  78. }
  79. return *this;
  80. }
  81. Uri::Uri(Uri &&uri) noexcept : m_lastError(AWS_ERROR_SUCCESS), m_isInit(uri.m_isInit)
  82. {
  83. if (uri.m_isInit)
  84. {
  85. m_uri = uri.m_uri;
  86. AWS_ZERO_STRUCT(uri.m_uri);
  87. uri.m_isInit = false;
  88. }
  89. }
  90. Uri &Uri::operator=(Uri &&uri) noexcept
  91. {
  92. if (this != &uri)
  93. {
  94. if (m_isInit)
  95. {
  96. aws_uri_clean_up(&m_uri);
  97. }
  98. if (uri.m_isInit)
  99. {
  100. m_uri = uri.m_uri;
  101. AWS_ZERO_STRUCT(uri.m_uri);
  102. uri.m_isInit = false;
  103. m_isInit = true;
  104. m_lastError = AWS_ERROR_SUCCESS;
  105. }
  106. else
  107. {
  108. m_lastError = uri.m_lastError;
  109. }
  110. }
  111. return *this;
  112. }
  113. ByteCursor Uri::GetScheme() const noexcept { return m_uri.scheme; }
  114. ByteCursor Uri::GetAuthority() const noexcept { return m_uri.authority; }
  115. ByteCursor Uri::GetPath() const noexcept { return m_uri.path; }
  116. ByteCursor Uri::GetQueryString() const noexcept { return m_uri.query_string; }
  117. ByteCursor Uri::GetHostName() const noexcept { return m_uri.host_name; }
  118. uint16_t Uri::GetPort() const noexcept { return m_uri.port; }
  119. ByteCursor Uri::GetPathAndQuery() const noexcept { return m_uri.path_and_query; }
  120. ByteCursor Uri::GetFullUri() const noexcept { return ByteCursorFromByteBuf(m_uri.uri_str); }
  121. } // namespace Io
  122. } // namespace Crt
  123. } // namespace Aws