Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-09-14 08:49:54

0001 //
0002 // Copyright (c) 2019 Vinnie Falco (vinnie.falco@gmail.com)
0003 // Copyright (c) 2022 Alan de Freitas (alandefreitas@gmail.com)
0004 //
0005 // Distributed under the Boost Software License, Version 1.0. (See accompanying
0006 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
0007 //
0008 // Official repository: https://github.com/boostorg/url
0009 //
0010 
0011 #ifndef BOOST_URL_URL_VIEW_HPP
0012 #define BOOST_URL_URL_VIEW_HPP
0013 
0014 #include <boost/url/detail/config.hpp>
0015 #include <boost/url/url_view_base.hpp>
0016 #include <utility>
0017 
0018 namespace boost {
0019 namespace urls {
0020 
0021 /** A non-owning reference to a valid URL 
0022 
0023     Objects of this type represent valid URL
0024     strings constructed from a parsed, external
0025     character buffer whose storage is managed
0026     by the caller. That is, it acts like a
0027     `core::string_view` in terms of ownership.
0028     The caller is responsible for ensuring
0029     that the lifetime of the underlying
0030     character buffer extends until it is no
0031     longer referenced.
0032 
0033     @par Example 1
0034     Construction from a string parses the input
0035     as a <em>URI-reference</em> and throws an
0036     exception on error. Upon success, the
0037     constructed object points to the passed
0038     character buffer; ownership is not
0039     transferred.
0040     @code
0041     url_view u( "https://www.example.com/index.htm?text=none#a1" );
0042     @endcode
0043 
0044     @par Example 2
0045     Parsing functions like @ref parse_uri_reference
0046     return a `boost::system::result` containing either a valid
0047     @ref url_view upon succcess, otherwise they
0048     contain an error. The error can be converted to
0049     an exception by the caller if desired:
0050     @code
0051     system::result< url_view > rv = parse_uri_reference( "https://www.example.com/index.htm?text=none#a1" );
0052     @endcode
0053 
0054     @par BNF
0055     @code
0056     URI-reference = URI / relative-ref
0057 
0058     URI           = scheme ":" hier-part [ "?" query ] [ "#" fragment ]
0059 
0060     relative-ref  = relative-part [ "?" query ] [ "#" fragment ]
0061     @endcode
0062 
0063     @par Specification
0064     @li <a href="https://tools.ietf.org/html/rfc3986"
0065         >Uniform Resource Identifier (URI): Generic Syntax (rfc3986)</a>
0066 
0067     @see
0068         @ref parse_absolute_uri,
0069         @ref parse_origin_form,
0070         @ref parse_relative_ref,
0071         @ref parse_uri,
0072         @ref parse_uri_reference.
0073 */
0074 class BOOST_URL_DECL url_view
0075     : public url_view_base
0076 {
0077     friend std::hash<url_view>;
0078     friend class url_view_base;
0079     friend class params_base;
0080     friend class params_encoded_base;
0081 
0082 #ifndef BOOST_URL_DOCS
0083     // VFALCO docca emits this erroneously
0084     friend struct detail::url_impl;
0085 #endif
0086 
0087     using url_view_base::digest;
0088 
0089     explicit
0090     url_view(
0091         detail::url_impl const& impl) noexcept
0092         : url_view_base(impl)
0093     {
0094     }
0095 
0096 public:
0097     //--------------------------------------------
0098     //
0099     // Special Members
0100     //
0101     //--------------------------------------------
0102 
0103     /** Destructor
0104 
0105         Any params, segments, iterators, or
0106         other views which reference the same
0107         underlying character buffer remain
0108         valid.
0109     */
0110     ~url_view() = default;
0111 
0112     /** Constructor
0113 
0114         Default constructed views refer to
0115         a string with zero length, which
0116         always remains valid. This matches
0117         the grammar for a relative-ref with
0118         an empty path and no query or
0119         fragment.
0120 
0121         @par Example
0122         @code
0123         url_view u;
0124         @endcode
0125 
0126         @par Postconditions
0127         @code
0128         this->empty() == true
0129         @endcode
0130 
0131         @par Complexity
0132         Constant.
0133 
0134         @par Exception Safety
0135         Throws nothing.
0136 
0137         @par BNF
0138         @code
0139         relative-ref  = relative-part [ "?" query ] [ "#" fragment ]
0140         @endcode
0141 
0142         @par Specification
0143         <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-4.2"
0144             >4.2. Relative Reference (rfc3986)</a>
0145     */
0146     url_view() noexcept;
0147 
0148     /** Constructor
0149 
0150         This function constructs a URL from
0151         the string `s`, which must contain a
0152         valid <em>URI</em> or <em>relative-ref</em>
0153         or else an exception is thrown. Upon
0154         successful construction, the view
0155         refers to the characters in the
0156         buffer pointed to by `s`.
0157         Ownership is not transferred; The caller
0158         is responsible for ensuring that the
0159         lifetime of the buffer extends until
0160         it is no longer referenced.
0161 
0162         @par Example
0163         @code
0164         url_view u( "http://www.example.com/index.htm" );
0165         @endcode
0166 
0167         @par Effects
0168         @code
0169         return parse_uri_reference( s ).value();
0170         @endcode
0171 
0172         @par Complexity
0173         Linear in `s.size()`.
0174 
0175         @par Exception Safety
0176         Exceptions thrown on invalid input.
0177 
0178         @throw system_error
0179         The input failed to parse correctly.
0180 
0181         @param s The string to parse.
0182 
0183         @par BNF
0184         @code
0185         URI           = scheme ":" hier-part [ "?" query ] [ "#" fragment ]
0186 
0187         relative-ref  = relative-part [ "?" query ] [ "#" fragment ]
0188         @endcode
0189 
0190         @par Specification
0191         @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-4.1"
0192             >4.1. URI Reference</a>
0193 
0194         @see
0195             @ref parse_uri_reference.
0196     */
0197     url_view(core::string_view s);
0198 
0199     /// @copydoc url_view(core::string_view)
0200     template<
0201         class String
0202 #ifndef BOOST_URL_DOCS
0203         , class = typename std::enable_if<
0204             std::is_convertible<
0205                 String,
0206                 core::string_view
0207                     >::value &&
0208             !std::is_convertible<
0209                 String*,
0210                 url_view_base*
0211                     >::value
0212             >::type
0213 #endif
0214     >
0215     url_view(
0216         String const& s)
0217         : url_view(
0218             detail::to_sv(s))
0219     {
0220     }
0221 
0222     /** Constructor
0223 
0224         After construction, both views
0225         reference the same underlying character
0226         buffer. Ownership is not transferred.
0227 
0228         @par Postconditions
0229         @code
0230         this->buffer().data() == other.buffer().data()
0231         @endcode
0232 
0233         @par Complexity
0234         Constant.
0235 
0236         @par Exception Safety
0237         Throws nothing.
0238 
0239         @param other The other view.
0240     */
0241     url_view(
0242         url_view const& other) noexcept
0243         : url_view(static_cast<
0244             url_view_base const&>(other))
0245     {
0246     }
0247 
0248     /** Constructor
0249 
0250         After construction, both views
0251         reference the same underlying character
0252         buffer. Ownership is not transferred.
0253 
0254         @par Postconditions
0255         @code
0256         this->buffer().data() == other.buffer().data()
0257         @endcode
0258 
0259         @par Complexity
0260         Constant.
0261 
0262         @par Exception Safety
0263         Throws nothing.
0264 
0265         @param other The other view.
0266     */
0267     url_view(
0268         url_view_base const& other) noexcept;
0269 
0270     /** Assignment
0271 
0272         After assignment, both views
0273         reference the same underlying character
0274         buffer. Ownership is not transferred.
0275 
0276         @par Postconditions
0277         @code
0278         this->buffer().data() == other.buffer().data()
0279         @endcode
0280 
0281         @par Complexity
0282         Constant.
0283 
0284         @par Exception Safety
0285         Throws nothing.
0286 
0287         @param other The other view.
0288         @return A reference to this object.
0289     */
0290     url_view&
0291     operator=(
0292         url_view const& other) noexcept
0293     {
0294         if (this != &other)
0295             *this = static_cast<
0296                 url_view_base const&>(other);
0297         return *this;
0298     }
0299 
0300     /** Assignment
0301 
0302         After assignment, both views
0303         reference the same underlying character
0304         buffer. Ownership is not transferred.
0305 
0306         @par Postconditions
0307         @code
0308         this->buffer().data() == other.buffer().data()
0309         @endcode
0310 
0311         @par Complexity
0312         Constant.
0313 
0314         @par Exception Safety
0315         Throws nothing.
0316 
0317         @param other The other view.
0318         @return A reference to this object.
0319     */
0320     url_view& operator=(
0321         url_view_base const& other) noexcept;
0322 
0323     //--------------------------------------------
0324     //
0325     // Observers
0326     //
0327     //--------------------------------------------
0328 
0329     /** Return the maximum number of characters possible
0330 
0331         This represents the largest number of
0332         characters that are possible in a url,
0333         not including any null terminator.
0334 
0335         @par Complexity
0336         Constant.
0337 
0338         @par Exception Safety
0339         Throws nothing.
0340 
0341         @return The maximum number of characters possible.
0342     */
0343     static
0344     constexpr
0345     std::size_t
0346     max_size() noexcept
0347     {
0348         return BOOST_URL_MAX_SIZE;
0349     }
0350 };
0351 
0352 } // urls
0353 } // boost
0354 
0355 //------------------------------------------------
0356 
0357 // std::hash specialization
0358 #ifndef BOOST_URL_DOCS
0359 namespace std {
0360 template<>
0361 struct hash< ::boost::urls::url_view >
0362 {
0363     hash() = default;
0364     hash(hash const&) = default;
0365     hash& operator=(hash const&) = default;
0366 
0367     explicit
0368     hash(std::size_t salt) noexcept
0369         : salt_(salt)
0370     {
0371     }
0372 
0373     std::size_t
0374     operator()(::boost::urls::url_view const& u) const noexcept
0375     {
0376         return u.digest(salt_);
0377     }
0378 
0379 private:
0380     std::size_t salt_ = 0;
0381 };
0382 } // std
0383 #endif
0384 
0385 #endif