url_impl.hpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. //
  2. // Copyright (c) 2022 Vinnie Falco (vinnie.falco@gmail.com)
  3. //
  4. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. //
  7. // Official repository: https://github.com/boostorg/url
  8. //
  9. #ifndef BOOST_URL_DETAIL_URL_IMPL_HPP
  10. #define BOOST_URL_DETAIL_URL_IMPL_HPP
  11. #include <boost/url/host_type.hpp>
  12. #include <boost/url/pct_string_view.hpp>
  13. #include <boost/url/scheme.hpp>
  14. #include <boost/core/detail/string_view.hpp>
  15. #include <boost/url/detail/parts_base.hpp>
  16. #include <boost/assert.hpp>
  17. #include <cstdint>
  18. namespace boost {
  19. namespace urls {
  20. class url_view;
  21. class authority_view;
  22. namespace detail {
  23. constexpr char const* const empty_c_str_ = "";
  24. // This is the private 'guts' of a
  25. // url_view, exposed so different parts
  26. // of the implementation can work on it.
  27. struct BOOST_URL_DECL url_impl : parts_base
  28. {
  29. static
  30. constexpr
  31. std::size_t const zero_ = 0;
  32. // never nullptr
  33. char const* cs_ = empty_c_str_;
  34. std::size_t offset_[id_end + 1] = {};
  35. std::size_t decoded_[id_end] = {};
  36. std::size_t nseg_ = 0;
  37. std::size_t nparam_ = 0;
  38. unsigned char ip_addr_[16] = {};
  39. // VFALCO don't we need a bool?
  40. std::uint16_t port_number_ = 0;
  41. host_type host_type_ =
  42. urls::host_type::none;
  43. scheme scheme_ =
  44. urls::scheme::none;
  45. from from_ = from::string;
  46. url_impl(
  47. from b) noexcept
  48. : from_(b)
  49. {
  50. }
  51. // in url_view.ipp
  52. url_view construct() const noexcept;
  53. // in authority_view.ipp
  54. authority_view
  55. construct_authority() const noexcept;
  56. std::size_t len(int, int) const noexcept;
  57. std::size_t len(int) const noexcept;
  58. std::size_t offset(int) const noexcept;
  59. core::string_view get(int) const noexcept;
  60. core::string_view get(int, int) const noexcept;
  61. pct_string_view pct_get(int) const noexcept;
  62. pct_string_view pct_get(int, int) const noexcept;
  63. void set_size(int, std::size_t) noexcept;
  64. void split(int, std::size_t) noexcept;
  65. void adjust(int, int, std::size_t) noexcept;
  66. void collapse(int, int, std::size_t) noexcept;
  67. void apply_scheme(core::string_view) noexcept;
  68. void apply_userinfo(pct_string_view const&,
  69. pct_string_view const*) noexcept;
  70. void apply_host(host_type, pct_string_view,
  71. unsigned char const*) noexcept;
  72. void apply_port(core::string_view, unsigned short) noexcept;
  73. void apply_authority(authority_view const&) noexcept;
  74. void apply_path(pct_string_view, std::size_t) noexcept;
  75. void apply_query(pct_string_view, std::size_t) noexcept;
  76. void apply_frag(pct_string_view) noexcept;
  77. };
  78. //------------------------------------------------
  79. // this allows a path to come from a
  80. // url_impl or a separate core::string_view
  81. class path_ref
  82. : private parts_base
  83. {
  84. url_impl const* impl_ = nullptr;
  85. char const* data_ = nullptr;
  86. std::size_t size_ = 0;
  87. std::size_t nseg_ = 0;
  88. std::size_t dn_ = 0;
  89. public:
  90. path_ref() = default;
  91. path_ref(url_impl const& impl) noexcept;
  92. path_ref(core::string_view,
  93. std::size_t, std::size_t) noexcept;
  94. pct_string_view buffer() const noexcept;
  95. std::size_t size() const noexcept;
  96. char const* data() const noexcept;
  97. char const* end() const noexcept;
  98. std::size_t nseg() const noexcept;
  99. bool
  100. alias_of(
  101. url_impl const& impl) const noexcept
  102. {
  103. return impl_ == &impl;
  104. }
  105. bool
  106. alias_of(
  107. path_ref const& ref) const noexcept
  108. {
  109. if(impl_)
  110. return impl_ == ref.impl_;
  111. BOOST_ASSERT(data_ != ref.data_ || (
  112. size_ == ref.size_ &&
  113. nseg_ == ref.nseg_ &&
  114. dn_ == ref.dn_));
  115. return data_ == ref.data_;
  116. }
  117. };
  118. //------------------------------------------------
  119. // this allows a params to come from a
  120. // url_impl or a separate core::string_view
  121. class BOOST_URL_DECL query_ref
  122. : private parts_base
  123. {
  124. url_impl const* impl_ = nullptr;
  125. char const* data_ = nullptr;
  126. std::size_t size_ = 0;
  127. std::size_t nparam_ = 0;
  128. std::size_t dn_ = 0;
  129. bool question_mark_ = false;
  130. public:
  131. query_ref(
  132. core::string_view s, // buffer, no '?'
  133. std::size_t dn, // decoded size
  134. std::size_t nparam
  135. ) noexcept;
  136. query_ref() = default;
  137. query_ref(url_impl const& impl) noexcept;
  138. pct_string_view buffer() const noexcept;
  139. std::size_t size() const noexcept; // with '?'
  140. char const* begin() const noexcept; // no '?'
  141. char const* end() const noexcept;
  142. std::size_t nparam() const noexcept;
  143. bool
  144. alias_of(
  145. url_impl const& impl) const noexcept
  146. {
  147. return impl_ == &impl;
  148. }
  149. bool
  150. alias_of(
  151. query_ref const& ref) const noexcept
  152. {
  153. if(impl_)
  154. return impl_ == ref.impl_;
  155. BOOST_ASSERT(data_ != ref.data_ || (
  156. size_ == ref.size_ &&
  157. nparam_ == ref.nparam_ &&
  158. dn_ == ref.dn_));
  159. return data_ == ref.data_;
  160. }
  161. };
  162. } // detail
  163. } // urls
  164. } // boost
  165. #endif