url_impl.hpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. //
  2. // Copyright (c) 2022 Vinnie Falco ([email protected])
  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_right(int first, int last, std::size_t n) noexcept;
  66. void adjust_left(int first, int last, std::size_t n) noexcept;
  67. void collapse(int, int, std::size_t) noexcept;
  68. void apply_scheme(core::string_view) noexcept;
  69. void apply_userinfo(pct_string_view const&,
  70. pct_string_view const*) noexcept;
  71. void apply_host(host_type, pct_string_view,
  72. unsigned char const*) noexcept;
  73. void apply_port(core::string_view, unsigned short) noexcept;
  74. void apply_authority(authority_view const&) noexcept;
  75. void apply_path(pct_string_view, std::size_t) noexcept;
  76. void apply_query(pct_string_view, std::size_t) noexcept;
  77. void apply_frag(pct_string_view) noexcept;
  78. };
  79. //------------------------------------------------
  80. // this allows a path to come from a
  81. // url_impl or a separate core::string_view
  82. class path_ref
  83. : private parts_base
  84. {
  85. url_impl const* impl_ = nullptr;
  86. char const* data_ = nullptr;
  87. std::size_t size_ = 0;
  88. std::size_t nseg_ = 0;
  89. std::size_t dn_ = 0;
  90. public:
  91. path_ref() = default;
  92. path_ref(url_impl const& impl) noexcept;
  93. path_ref(core::string_view,
  94. std::size_t, std::size_t) noexcept;
  95. pct_string_view buffer() const noexcept;
  96. std::size_t size() const noexcept;
  97. char const* data() const noexcept;
  98. char const* end() const noexcept;
  99. std::size_t nseg() const noexcept;
  100. bool
  101. alias_of(
  102. url_impl const& impl) const noexcept
  103. {
  104. return impl_ == &impl;
  105. }
  106. bool
  107. alias_of(
  108. path_ref const& ref) const noexcept
  109. {
  110. if(impl_)
  111. return impl_ == ref.impl_;
  112. BOOST_ASSERT(data_ != ref.data_ || (
  113. size_ == ref.size_ &&
  114. nseg_ == ref.nseg_ &&
  115. dn_ == ref.dn_));
  116. return data_ == ref.data_;
  117. }
  118. };
  119. //------------------------------------------------
  120. // this allows a params to come from a
  121. // url_impl or a separate core::string_view
  122. class BOOST_URL_DECL query_ref
  123. : private parts_base
  124. {
  125. url_impl const* impl_ = nullptr;
  126. char const* data_ = nullptr;
  127. std::size_t size_ = 0;
  128. std::size_t nparam_ = 0;
  129. std::size_t dn_ = 0;
  130. bool question_mark_ = false;
  131. public:
  132. query_ref(
  133. core::string_view s, // buffer, no '?'
  134. std::size_t dn, // decoded size
  135. std::size_t nparam
  136. ) noexcept;
  137. query_ref() = default;
  138. query_ref(url_impl const& impl) noexcept;
  139. pct_string_view buffer() const noexcept;
  140. std::size_t size() const noexcept; // with '?'
  141. char const* begin() const noexcept; // no '?'
  142. char const* end() const noexcept;
  143. std::size_t nparam() const noexcept;
  144. bool
  145. alias_of(
  146. url_impl const& impl) const noexcept
  147. {
  148. return impl_ == &impl;
  149. }
  150. bool
  151. alias_of(
  152. query_ref const& ref) const noexcept
  153. {
  154. if(impl_)
  155. return impl_ == ref.impl_;
  156. BOOST_ASSERT(data_ != ref.data_ || (
  157. size_ == ref.size_ &&
  158. nparam_ == ref.nparam_ &&
  159. dn_ == ref.dn_));
  160. return data_ == ref.data_;
  161. }
  162. };
  163. } // detail
  164. } // urls
  165. } // boost
  166. #endif