| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551 |
- //
- // Copyright (c) 2019 Vinnie Falco (vinnie.falco@gmail.com)
- //
- // Distributed under the Boost Software License, Version 1.0. (See accompanying
- // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
- //
- // Official repository: https://github.com/boostorg/url
- //
- #ifndef BOOST_URL_AUTHORITY_VIEW_HPP
- #define BOOST_URL_AUTHORITY_VIEW_HPP
- #include <boost/url/detail/config.hpp>
- #include <boost/url/host_type.hpp>
- #include <boost/url/ipv4_address.hpp>
- #include <boost/url/ipv6_address.hpp>
- #include <boost/url/pct_string_view.hpp>
- #include <boost/url/detail/except.hpp>
- #include <boost/url/detail/url_impl.hpp>
- #include <boost/assert.hpp>
- #include <cstddef>
- #include <iosfwd>
- #include <utility>
- namespace boost {
- namespace urls {
- /** A non-owning reference to a valid authority
- Objects of this type represent valid authority
- strings constructed from a parsed, external
- character buffer whose storage is managed
- by the caller. That is, it acts like a
- `core::string_view` in terms of ownership.
- The caller is responsible for ensuring
- that the lifetime of the underlying
- character buffer extends until it is no
- longer referenced.
- @par Example 1
- Construction from a string parses the input
- as an <em>authority</em> and throws an
- exception on error. Upon success, the
- constructed object points to the passed
- character buffer; ownership is not
- transferred.
- @code
- authority_view a( "user:pass@www.example.com:8080" );
- @endcode
- @par Example 2
- The parsing function @ref parse_authority returns
- a `boost::system::result` containing either a valid
- @ref authority_view upon success, otherwise it
- contains an error. The error can be converted to
- an exception by the caller if desired:
- @code
- system::result< authority_view > rv = parse_authority( "user:pass@www.example.com:8080" );
- @endcode
- @par BNF
- @code
- authority = [ userinfo "@" ] host [ ":" port ]
- userinfo = user [ ":" [ password ] ]
- user = *( unreserved / pct-encoded / sub-delims )
- password = *( unreserved / pct-encoded / sub-delims / ":" )
- host = IP-literal / IPv4address / reg-name
- port = *DIGIT
- @endcode
- @par Specification
- @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-3.2"
- >3.2. Authority (rfc3986)</a>
- @see
- @ref parse_authority.
- */
- class BOOST_URL_DECL
- authority_view
- : private detail::parts_base
- {
- detail::url_impl u_;
- friend struct detail::url_impl;
- explicit
- authority_view(
- detail::url_impl const& u) noexcept;
- public:
- //--------------------------------------------
- //
- // Special Members
- //
- //--------------------------------------------
- /** Destructor
- */
- virtual
- ~authority_view();
- /** Constructor
- Default constructed authorities
- refer to a string with zero length,
- which is always valid. This matches
- the grammar for a zero-length host.
- @par Exception Safety
- Throws nothing.
- @par Specification
- */
- authority_view() noexcept;
- /** Construct from a string.
- This function attempts to construct
- an authority from the string `s`,
- which must be a valid authority or
- else an exception is thrown. Upon
- successful construction, the view
- refers to the characters in the
- buffer pointed to by `s`.
- Ownership is not transferred; the
- caller is responsible for ensuring
- that the lifetime of the buffer
- extends until the view is destroyed.
- @param s The string to parse
- @par BNF
- @code
- authority = [ userinfo "@" ] host [ ":" port ]
- userinfo = user [ ":" [ password ] ]
- user = *( unreserved / pct-encoded / sub-delims )
- password = *( unreserved / pct-encoded / sub-delims / ":" )
- host = IP-literal / IPv4address / reg-name
- port = *DIGIT
- @endcode
- @par Specification
- @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-3.2"
- >3.2. Authority (rfc3986)</a>
- @see
- @ref parse_authority.
- */
- explicit
- authority_view(core::string_view s);
- /** Constructor
- */
- authority_view(
- authority_view const&) noexcept;
- /** Assignment
- This function assigns the contents of
- `other` to this object.
- @param other The object to assign
- @return A reference to this object
- @par Exception Safety
- Throws nothing.
- */
- authority_view&
- operator=(
- authority_view const& other) noexcept;
- //--------------------------------------------
- //
- // Observers
- //
- //--------------------------------------------
- /** Return the number of characters in the authority
- This function returns the number of
- characters in the authority.
- @return The number of characters in the authority
- @par Example
- @code
- assert( authority_view( "user:pass@www.example.com:8080" ).size() == 30 );
- @endcode
- @par Exception Safety
- Throws nothing.
- */
- std::size_t
- size() const noexcept
- {
- return u_.offset(id_end);
- }
- /** Return true if the authority is empty
- An empty authority has an empty host,
- no userinfo, and no port.
- @return `true` if the authority is empty
- @par Example
- @code
- assert( authority_view( "" ).empty() );
- @endcode
- @par Exception Safety
- Throws nothing.
- */
- bool
- empty() const noexcept
- {
- return size() == 0;
- }
- /** Return a pointer to the first character
- This function returns a pointer to the
- beginning of the view, which is not
- guaranteed to be null-terminated.
- @return A pointer to the first character
- @par Exception Safety
- Throws nothing.
- */
- char const*
- data() const noexcept
- {
- return u_.cs_;
- }
- /** Return the complete authority
- This function returns the authority
- as a percent-encoded string.
- @return The complete authority
- @par Example
- @code
- assert( parse_authority( "www.example.com" ).value().buffer() == "www.example.com" );
- @endcode
- @par BNF
- @code
- authority = [ userinfo "@" ] host [ ":" port ]
- @endcode
- @par Exception Safety
- Throws nothing.
- @par Specification
- @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-3.2"
- >3.2. Authority (rfc3986)</a>
- */
- core::string_view
- buffer() const noexcept
- {
- return core::string_view(data(), size());
- }
- //--------------------------------------------
- //
- // Userinfo
- //
- //--------------------------------------------
- /** Return true if a userinfo is present
- This function returns true if this
- contains a userinfo.
- @return `true` if a userinfo is present
- @par Example
- @code
- assert( url_view( "http://jane%2Ddoe:pass@example.com" ).has_userinfo() );
- @endcode
- @par Complexity
- Constant.
- @par Exception Safety
- Throws nothing.
- @par BNF
- @code
- userinfo = user [ ":" [ password ] ]
- authority = [ userinfo "@" ] host [ ":" port ]
- @endcode
- @par Specification
- @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-3.2.1"
- >3.2.1. User Information (rfc3986)</a>
- @see
- @ref has_password,
- @ref encoded_password,
- @ref encoded_user,
- @ref encoded_userinfo,
- @ref password,
- @ref user,
- @ref userinfo.
- */
- bool
- has_userinfo() const noexcept;
- /** Return the userinfo
- If present, this function returns a
- string representing the userinfo (which
- may be empty).
- Otherwise it returns an empty string.
- Any percent-escapes in the string are
- decoded first.
- @param token A string token to receive the result.
- @return The userinfo
- @par Example
- @code
- assert( url_view( "http://jane%2Ddoe:pass@example.com" ).userinfo() == "jane-doe:pass" );
- @endcode
- @par Complexity
- Linear in `this->userinfo().size()`.
- @par Exception Safety
- Calls to allocate may throw.
- @par BNF
- @code
- userinfo = user [ ":" [ password ] ]
- authority = [ userinfo "@" ] host [ ":" port ]
- @endcode
- @par Specification
- @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-3.2.1"
- >3.2.1. User Information (rfc3986)</a>
- @see
- @ref has_password,
- @ref has_userinfo,
- @ref encoded_password,
- @ref encoded_user,
- @ref encoded_userinfo,
- @ref password,
- @ref user.
- */
- template<BOOST_URL_STRTOK_TPARAM>
- BOOST_URL_STRTOK_RETURN
- userinfo(
- BOOST_URL_STRTOK_ARG(token)) const
- {
- encoding_opts opt;
- opt.space_as_plus = false;
- return encoded_userinfo().decode(
- opt, std::move(token));
- }
- /** Return the userinfo
- If present, this function returns a
- string representing the userinfo (which
- may be empty).
- Otherwise it returns an empty string.
- The returned string may contain
- percent escapes.
- @return The userinfo
- @par Example
- @code
- assert( url_view( "http://jane%2Ddoe:pass@example.com" ).encoded_userinfo() == "jane%2Ddoe:pass" );
- @endcode
- @par Complexity
- Constant.
- @par Exception Safety
- Throws nothing
- @par BNF
- @code
- userinfo = user [ ":" [ password ] ]
- authority = [ userinfo "@" ] host [ ":" port ]
- @endcode
- @par Specification
- @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-3.2.1"
- >3.2.1. User Information (rfc3986)</a>
- @see
- @ref has_password,
- @ref has_userinfo,
- @ref encoded_password,
- @ref encoded_user,
- @ref password,
- @ref user,
- @ref userinfo.
- */
- pct_string_view
- encoded_userinfo() const noexcept;
- //--------------------------------------------
- /** Return the user
- If present, this function returns a
- string representing the user (which
- may be empty).
- Otherwise it returns an empty string.
- Any percent-escapes in the string are
- decoded first.
- @param token A string token to receive the result.
- @return The user
- @par Example
- @code
- assert( url_view( "http://jane%2Ddoe:pass@example.com" ).user() == "jane-doe" );
- @endcode
- @par Complexity
- Linear in `this->user().size()`.
- @par Exception Safety
- Calls to allocate may throw.
- @par BNF
- @code
- userinfo = user [ ":" [ password ] ]
- user = *( unreserved / pct-encoded / sub-delims )
- password = *( unreserved / pct-encoded / sub-delims / ":" )
- @endcode
- @par Specification
- @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-3.2.1"
- >3.2.1. User Information (rfc3986)</a>
- @see
- @ref has_password,
- @ref has_userinfo,
- @ref encoded_password,
- @ref encoded_user,
- @ref encoded_userinfo,
- @ref password,
- @ref userinfo.
- */
- template<BOOST_URL_STRTOK_TPARAM>
- BOOST_URL_STRTOK_RETURN
- user(
- BOOST_URL_STRTOK_ARG(token)) const
- {
- encoding_opts opt;
- opt.space_as_plus = false;
- return encoded_user().decode(
- opt, std::move(token));
- }
- /** Return the user
- If present, this function returns a
- string representing the user (which
- may be empty).
- Otherwise it returns an empty string.
- The returned string may contain
- percent escapes.
- @return The user
- @par Example
- @code
- assert( url_view( "http://jane%2Ddoe:pass@example.com" ).encoded_user() == "jane%2Ddoe" );
- @endcode
- @par Complexity
- Constant.
- @par Exception Safety
- Throws nothing.
- @par BNF
- @code
- userinfo = user [ ":" [ password ] ]
- user = *( unreserved / pct-encoded / sub-delims )
- password = *( unreserved / pct-encoded / sub-delims / ":" )
- @endcode
- @par Specification
- @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-3.2.1"
- >3.2.1. User Information (rfc3986)</a>
- @see
- @ref has_password,
- @ref has_userinfo,
- @ref encoded_password,
- @ref encoded_userinfo,
- @ref password,
- @ref user,
- @ref userinfo.
- */
- pct_string_view
- encoded_user() const noexcept;
- /** Return true if a password is present
- This function returns true if the
- userinfo is present and contains
- a password.
- @return `true` if a password is present
- @par Example
- @code
- assert( url_view( "http://jane%2Ddoe:pass@example.com" ).has_password() );
- @endcode
- @par Complexity
- Constant.
- @par Exception Safety
- Throws nothing.
- @par BNF
- @code
- userinfo = user [ ":" [ password ] ]
- user = *( unreserved / pct-encoded / sub-delims )
- password = *( unreserved / pct-encoded / sub-delims / ":" )
- @endcode
- @par Specification
- @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-3.2.1"
- >3.2.1. User Information (rfc3986)</a>
- @see
- @ref has_userinfo,
- @ref encoded_password,
- @ref encoded_user,
- @ref encoded_userinfo,
- @ref password,
- @ref user,
- @ref userinfo.
- */
- bool
- has_password() const noexcept;
- /** Return the password
- If present, this function returns a
- string representing the password (which
- may be an empty string).
- Otherwise it returns an empty string.
- Any percent-escapes in the string are
- decoded first.
- @param token A string token to receive the result.
- @return The password
- @par Example
- @code
- assert( url_view( "http://jane%2Ddoe:pass@example.com" ).password() == "pass" );
- @endcode
- @par Complexity
- Linear in `this->password().size()`.
- @par Exception Safety
- Calls to allocate may throw.
- @par BNF
- @code
- userinfo = user [ ":" [ password ] ]
- user = *( unreserved / pct-encoded / sub-delims )
- password = *( unreserved / pct-encoded / sub-delims / ":" )
- @endcode
- @par Specification
- @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-3.2.1"
- >3.2.1. User Information (rfc3986)</a>
- @see
- @ref has_password,
- @ref has_userinfo,
- @ref encoded_password,
- @ref encoded_user,
- @ref encoded_userinfo,
- @ref user,
- @ref userinfo.
- */
- template<BOOST_URL_STRTOK_TPARAM>
- BOOST_URL_STRTOK_RETURN
- password(
- BOOST_URL_STRTOK_ARG(token)) const
- {
- encoding_opts opt;
- opt.space_as_plus = false;
- return encoded_password().decode(
- opt, std::move(token));
- }
- /** Return the password
- This function returns the password portion
- of the userinfo as a percent-encoded string.
- @return The password
- @par Example
- @code
- assert( url_view( "http://jane%2Ddoe:pass@example.com" ).encoded_password() == "pass" );
- @endcode
- @par Complexity
- Constant.
- @par Exception Safety
- Throws nothing.
- @par BNF
- @code
- userinfo = user [ ":" [ password ] ]
- user = *( unreserved / pct-encoded / sub-delims )
- password = *( unreserved / pct-encoded / sub-delims / ":" )
- @endcode
- @par Specification
- @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-3.2.1"
- >3.2.1. User Information (rfc3986)</a>
- @see
- @ref has_password,
- @ref has_userinfo,
- @ref encoded_user,
- @ref encoded_userinfo,
- @ref password,
- @ref user,
- @ref userinfo.
- */
- pct_string_view
- encoded_password() const noexcept;
- //--------------------------------------------
- //
- // Host
- //
- //--------------------------------------------
- /** Return the host type
- This function returns one of the
- following constants representing the
- type of host present.
- @li @ref host_type::ipv4
- @li @ref host_type::ipv6
- @li @ref host_type::ipvfuture
- @li @ref host_type::name
- @return The host type
- @par Example
- @code
- assert( url_view( "https://192.168.0.1/local.htm" ).host_type() == host_type::ipv4 );
- @endcode
- @par Complexity
- Constant.
- @par Exception Safety
- Throws nothing.
- @par Specification
- @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-3.2.2"
- >3.2.2. Host (rfc3986)</a>
- */
- urls::host_type
- host_type() const noexcept
- {
- return u_.host_type_;
- }
- /** Return the host
- This function returns the host portion
- of the authority as a string, or the
- empty string if there is no authority.
- Any percent-escapes in the string are
- decoded first.
- @param token A string token to receive the result.
- @return The host
- @par Example
- @code
- assert( url_view( "https://www%2droot.example.com/" ).host() == "www-root.example.com" );
- @endcode
- @par Complexity
- Linear in `this->host().size()`.
- @par Exception Safety
- Calls to allocate may throw.
- @par BNF
- @code
- host = IP-literal / IPv4address / reg-name
- IP-literal = "[" ( IPv6address / IPvFuture ) "]"
- reg-name = *( unreserved / pct-encoded / "-" / ".")
- @endcode
- @par Specification
- @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-3.2.2"
- >3.2.2. Host (rfc3986)</a>
- */
- template<BOOST_URL_STRTOK_TPARAM>
- BOOST_URL_STRTOK_RETURN
- host(
- BOOST_URL_STRTOK_ARG(token)) const
- {
- encoding_opts opt;
- opt.space_as_plus = false;
- return encoded_host().decode(
- opt, std::move(token));
- }
- /** Return the host
- This function returns the host portion
- of the authority as a string, or the
- empty string if there is no authority.
- The returned string may contain
- percent escapes.
- @return The host
- @par Example
- @code
- assert( url_view( "https://www%2droot.example.com/" ).encoded_host() == "www%2droot.example.com" );
- @endcode
- @par Complexity
- Constant.
- @par Exception Safety
- Throws nothing.
- @par BNF
- @code
- host = IP-literal / IPv4address / reg-name
- IP-literal = "[" ( IPv6address / IPvFuture ) "]"
- reg-name = *( unreserved / pct-encoded / "-" / ".")
- @endcode
- @par Specification
- @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-3.2.2"
- >3.2.2. Host (rfc3986)</a>
- */
- pct_string_view
- encoded_host() const noexcept;
- /** Return the host
- The value returned by this function
- depends on the type of host returned
- from the function @ref host_type.
- @li If the type is @ref host_type::ipv4,
- then the IPv4 address string is returned.
- @li If the type is @ref host_type::ipv6,
- then the IPv6 address string is returned,
- without any enclosing brackets.
- @li If the type is @ref host_type::ipvfuture,
- then the IPvFuture address string is returned,
- without any enclosing brackets.
- @li If the type is @ref host_type::name,
- then the host name string is returned.
- Any percent-escapes in the string are
- decoded first.
- @li If the type is @ref host_type::none,
- then an empty string is returned.
- @param token A string token to receive the result.
- @return The host address
- @par Example
- @code
- assert( url_view( "https://[1::6:c0a8:1]/" ).host_address() == "1::6:c0a8:1" );
- @endcode
- @par Complexity
- Linear in `this->host_address().size()`.
- @par Exception Safety
- Calls to allocate may throw.
- @par BNF
- @code
- host = IP-literal / IPv4address / reg-name
- IP-literal = "[" ( IPv6address / IPvFuture ) "]"
- reg-name = *( unreserved / pct-encoded / "-" / ".")
- @endcode
- @par Specification
- @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-3.2.2"
- >3.2.2. Host (rfc3986)</a>
- */
- template<BOOST_URL_STRTOK_TPARAM>
- BOOST_URL_STRTOK_RETURN
- host_address(
- BOOST_URL_STRTOK_ARG(token)) const
- {
- encoding_opts opt;
- opt.space_as_plus = false;
- return encoded_host_address().decode(
- opt, std::move(token));
- }
- /** Return the host
- The value returned by this function
- depends on the type of host returned
- from the function @ref host_type.
- @li If the type is @ref host_type::ipv4,
- then the IPv4 address string is returned.
- @li If the type is @ref host_type::ipv6,
- then the IPv6 address string is returned,
- without any enclosing brackets.
- @li If the type is @ref host_type::ipvfuture,
- then the IPvFuture address string is returned,
- without any enclosing brackets.
- @li If the type is @ref host_type::name,
- then the host name string is returned.
- Any percent-escapes in the string are
- decoded first.
- @li If the type is @ref host_type::none,
- then an empty string is returned.
- The returned string may contain
- percent escapes.
- @return The host address
- @par Example
- @code
- assert( url_view( "https://www%2droot.example.com/" ).encoded_host_address() == "www%2droot.example.com" );
- @endcode
- @par Complexity
- Constant.
- @par Exception Safety
- Throws nothing.
- @par BNF
- @code
- host = IP-literal / IPv4address / reg-name
- IP-literal = "[" ( IPv6address / IPvFuture ) "]"
- reg-name = *( unreserved / pct-encoded / "-" / ".")
- @endcode
- @par Specification
- @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-3.2.2"
- >3.2.2. Host (rfc3986)</a>
- */
- pct_string_view
- encoded_host_address() const noexcept;
- /** Return the host IPv4 address
- If the host type is @ref host_type::ipv4,
- this function returns the address as
- a value of type @ref ipv4_address.
- Otherwise, if the host type is not an IPv4
- address, it returns a default-constructed
- value which is equal to the unspecified
- address "0.0.0.0".
- @return The host IPv4 address
- @par Example
- @code
- assert( url_view( "http://127.0.0.1/index.htm?user=win95" ).host_ipv4_address() == ipv4_address( "127.0.0.1" ) );
- @endcode
- @par Complexity
- Constant.
- @par Exception Safety
- Throws nothing.
- @par BNF
- @code
- IPv4address = dec-octet "." dec-octet "." dec-octet "." dec-octet
- dec-octet = DIGIT ; 0-9
- / %x31-39 DIGIT ; 10-99
- / "1" 2DIGIT ; 100-199
- / "2" %x30-34 DIGIT ; 200-249
- / "25" %x30-35 ; 250-255
- @endcode
- @par Specification
- @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-3.2.2"
- >3.2.2. Host (rfc3986)</a>
- */
- ipv4_address
- host_ipv4_address() const noexcept;
- /** Return the host IPv6 address
- If the host type is @ref host_type::ipv6,
- this function returns the address as
- a value of type @ref ipv6_address.
- Otherwise, if the host type is not an IPv6
- address, it returns a default-constructed
- value which is equal to the unspecified
- address "0:0:0:0:0:0:0:0".
- @return The host IPv6 address
- @par Example
- @code
- assert( url_view( "ftp://[::1]/" ).host_ipv6_address() == ipv6_address( "::1" ) );
- @endcode
- @par Complexity
- Constant.
- @par Exception Safety
- Throws nothing.
- @par BNF
- @code
- IPv6address = 6( h16 ":" ) ls32
- / "::" 5( h16 ":" ) ls32
- / [ h16 ] "::" 4( h16 ":" ) ls32
- / [ *1( h16 ":" ) h16 ] "::" 3( h16 ":" ) ls32
- / [ *2( h16 ":" ) h16 ] "::" 2( h16 ":" ) ls32
- / [ *3( h16 ":" ) h16 ] "::" h16 ":" ls32
- / [ *4( h16 ":" ) h16 ] "::" ls32
- / [ *5( h16 ":" ) h16 ] "::" h16
- / [ *6( h16 ":" ) h16 ] "::"
- ls32 = ( h16 ":" h16 ) / IPv4address
- ; least-significant 32 bits of address
- h16 = 1*4HEXDIG
- ; 16 bits of address represented in hexadecimal
- @endcode
- @par Specification
- @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-3.2.2"
- >3.2.2. Host (rfc3986)</a>
- */
- ipv6_address
- host_ipv6_address() const noexcept;
- /** Return the host IPvFuture address
- If the host type is @ref host_type::ipvfuture,
- this function returns the address as
- a string.
- Otherwise, if the host type is not an
- IPvFuture address, it returns an
- empty string.
- @return The host IPvFuture address
- @par Example
- @code
- assert( url_view( "http://[v1fe.d:9]/index.htm" ).host_ipvfuture() == "v1fe.d:9" );
- @endcode
- @par Complexity
- Constant.
- @par Exception Safety
- Throws nothing.
- @par BNF
- @code
- IPvFuture = "v" 1*HEXDIG "." 1*( unreserved / sub-delims / ":" )
- @endcode
- @par Specification
- @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-3.2.2"
- >3.2.2. Host (rfc3986)</a>
- */
- core::string_view
- host_ipvfuture() const noexcept;
- /** Return the host name
- If the host type is @ref host_type::name,
- this function returns the name as
- a string.
- Otherwise, if the host type is not a
- name, it returns an empty string.
- Any percent-escapes in the string are
- decoded first.
- @param token A string token to receive the result
- @return The host name
- @par Example
- @code
- assert( url_view( "https://www%2droot.example.com/" ).host_name() == "www-root.example.com" );
- @endcode
- @par Complexity
- Linear in `this->host_name().size()`.
- @par Exception Safety
- Calls to allocate may throw.
- @par BNF
- @code
- host = IP-literal / IPv4address / reg-name
- IP-literal = "[" ( IPv6address / IPvFuture ) "]"
- reg-name = *( unreserved / pct-encoded / "-" / ".")
- @endcode
- @par Specification
- @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-3.2.2"
- >3.2.2. Host (rfc3986)</a>
- */
- template<BOOST_URL_STRTOK_TPARAM>
- BOOST_URL_STRTOK_RETURN
- host_name(
- BOOST_URL_STRTOK_ARG(token)) const
- {
- encoding_opts opt;
- opt.space_as_plus = false;
- return encoded_host_name().decode(
- opt, std::move(token));
- }
- /** Return the host name
- If the host type is @ref host_type::name,
- this function returns the name as
- a string.
- Otherwise, if the host type is not an
- name, it returns an empty string.
- The returned string may contain
- percent escapes.
- @return The host name
- @par Example
- @code
- assert( url_view( "https://www%2droot.example.com/" ).encoded_host_name() == "www%2droot.example.com" );
- @endcode
- @par Complexity
- Constant.
- @par Exception Safety
- Throws nothing.
- @par BNF
- @code
- host = IP-literal / IPv4address / reg-name
- IP-literal = "[" ( IPv6address / IPvFuture ) "]"
- reg-name = *( unreserved / pct-encoded / "-" / ".")
- @endcode
- @par Specification
- @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-3.2.2"
- >3.2.2. Host (rfc3986)</a>
- */
- pct_string_view
- encoded_host_name() const noexcept;
- //--------------------------------------------
- //
- // Port
- //
- //--------------------------------------------
- /** Return true if a port is present
- This function returns true if an
- authority is present and contains a port.
- @return `true` if a port is present, otherwise `false`
- @par Example
- @code
- assert( url_view( "wss://www.example.com:443" ).has_port() );
- @endcode
- @par Complexity
- Constant.
- @par Exception Safety
- Throws nothing.
- @par BNF
- @code
- authority = [ userinfo "@" ] host [ ":" port ]
- port = *DIGIT
- @endcode
- @par Specification
- @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-3.2.3"
- >3.2.3. Port (rfc3986)</a>
- @see
- @ref encoded_host_and_port,
- @ref port,
- @ref port_number.
- */
- bool
- has_port() const noexcept;
- /** Return the port
- If present, this function returns a
- string representing the port (which
- may be empty).
- Otherwise it returns an empty string.
- @return The port as a string
- @par Example
- @code
- assert( url_view( "http://localhost.com:8080" ).port() == "8080" );
- @endcode
- @par Complexity
- Constant.
- @par Exception Safety
- Throws nothing.
- @par BNF
- @code
- port = *DIGIT
- @endcode
- @par Specification
- @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-3.2.3"
- >3.2.3. Port (rfc3986)</a>
- @see
- @ref encoded_host_and_port,
- @ref has_port,
- @ref port_number.
- */
- core::string_view
- port() const noexcept;
- /** Return the port
- If a port is present and the numerical
- value is representable, it is returned
- as an unsigned integer. Otherwise, the
- number zero is returned.
- @return The port number
- @par Example
- @code
- assert( url_view( "http://localhost.com:8080" ).port_number() == 8080 );
- @endcode
- @par Complexity
- Constant.
- @par Exception Safety
- Throws nothing.
- @par BNF
- @code
- port = *DIGIT
- @endcode
- @par Specification
- @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-3.2.3"
- >3.2.3. Port (rfc3986)</a>
- @see
- @ref encoded_host_and_port,
- @ref has_port,
- @ref port.
- */
- std::uint16_t
- port_number() const noexcept;
- /** Return the host and port
- If an authority is present, this
- function returns the host and optional
- port as a string, which may be empty.
- Otherwise it returns an empty string.
- The returned string may contain
- percent escapes.
- @par Example
- @code
- assert( url_view( "http://www.example.com:8080/index.htm" ).encoded_host_and_port() == "www.example.com:8080" );
- @endcode
- @par Complexity
- Constant.
- @par Exception Safety
- Throws nothing.
- @par BNF
- @code
- authority = [ userinfo "@" ] host [ ":" port ]
- @endcode
- @par Specification
- @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-3.2.2"
- >3.2.2. Host (rfc3986)</a>
- @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-3.2.3"
- >3.2.3. Port (rfc3986)</a>
- @see
- @ref has_port,
- @ref port,
- @ref port_number.
- @return The host and port
- */
- pct_string_view
- encoded_host_and_port() const noexcept;
- //--------------------------------------------
- //
- // Comparison
- //
- //--------------------------------------------
- /** Return the result of comparing this with another authority
- This function compares two authorities
- according to Syntax-Based comparison
- algorithm.
- @par Exception Safety
- Throws nothing.
- @param other The authority to compare
- @return `-1` if `*this < other`, `0` if
- `this == other`, and 1 if `this > other`.
- @par Specification
- @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-6.2.2"
- >6.2.2 Syntax-Based Normalization (rfc3986)</a>
- */
- int
- compare(authority_view const& other) const noexcept;
- /** Return the result of comparing two authorities.
- The authorities are compared component
- by component as if they were first
- normalized.
- @par Complexity
- Linear in `min( a0.size(), a1.size() )`
- @par Exception Safety
- Throws nothing
- @param a0 The first authority to compare
- @param a1 The second authority to compare
- @return `true` if `a0 == a1`, otherwise `false`
- */
- friend
- bool
- operator==(
- authority_view const& a0,
- authority_view const& a1) noexcept
- {
- return a0.compare(a1) == 0;
- }
- /** Return the result of comparing two authorities.
- The authorities are compared component
- by component as if they were first
- normalized.
- @par Complexity
- Linear in `min( a0.size(), a1.size() )`
- @par Exception Safety
- Throws nothing
- @param a0 The first authority to compare
- @param a1 The second authority to compare
- @return `true` if `a0 != a1`, otherwise `false`
- */
- friend
- bool
- operator!=(
- authority_view const& a0,
- authority_view const& a1) noexcept
- {
- return ! (a0 == a1);
- }
- /** Return the result of comparing two authorities.
- The authorities are compared component
- by component as if they were first
- normalized.
- @par Complexity
- Linear in `min( a0.size(), a1.size() )`
- @par Exception Safety
- Throws nothing
- @param a0 The first authority to compare
- @param a1 The second authority to compare
- @return `true` if `a0 < a1`, otherwise `false`
- */
- friend
- bool
- operator<(
- authority_view const& a0,
- authority_view const& a1) noexcept
- {
- return a0.compare(a1) < 0;
- }
- /** Return the result of comparing two authorities.
- The authorities are compared component
- by component as if they were first
- normalized.
- @par Complexity
- Linear in `min( a0.size(), a1.size() )`
- @par Exception Safety
- Throws nothing
- @param a0 The first authority to compare
- @param a1 The second authority to compare
- @return `true` if `a0 <= a1`, otherwise `false`
- */
- friend
- bool
- operator<=(
- authority_view const& a0,
- authority_view const& a1) noexcept
- {
- return a0.compare(a1) <= 0;
- }
- /** Return the result of comparing two authorities.
- The authorities are compared component
- by component as if they were first
- normalized.
- @par Complexity
- Linear in `min( a0.size(), a1.size() )`
- @par Exception Safety
- Throws nothing
- @param a0 The first authority to compare
- @param a1 The second authority to compare
- @return `true` if `a0 > a1`, otherwise `false`
- */
- friend
- bool
- operator>(
- authority_view const& a0,
- authority_view const& a1) noexcept
- {
- return a0.compare(a1) > 0;
- }
- /** Return the result of comparing two authorities.
- The authorities are compared component
- by component as if they were first
- normalized.
- @par Complexity
- Linear in `min( a0.size(), a1.size() )`
- @par Exception Safety
- Throws nothing
- @param a0 The first authority to compare
- @param a1 The second authority to compare
- @return `true` if `a0 >= a1`, otherwise `false`
- */
- friend
- bool
- operator>=(
- authority_view const& a0,
- authority_view const& a1) noexcept
- {
- return a0.compare(a1) >= 0;
- }
- //--------------------------------------------
- /** Format the encoded authority to the output stream
- This hidden friend function serializes the encoded URL
- to the output stream.
- @par Example
- @code
- authority_view a( "www.example.com" );
- std::cout << a << std::endl;
- @endcode
- @return A reference to the output stream, for chaining
- @param os The output stream to write to
- @param a The URL to write
- */
- friend
- std::ostream&
- operator<<(
- std::ostream& os,
- authority_view const& a)
- {
- return os << a.buffer();
- }
- };
- /** Format the encoded authority to the output stream
- This function serializes the encoded URL
- to the output stream.
- @par Example
- @code
- authority_view a( "www.example.com" );
- std::cout << a << std::endl;
- @endcode
- @return A reference to the output stream, for chaining
- @param os The output stream to write to
- @param a The URL to write
- */
- std::ostream&
- operator<<(
- std::ostream& os,
- authority_view const& a);
- //------------------------------------------------
- /** Parse an authority
- This function parses a string according to
- the authority grammar below, and returns an
- @ref authority_view referencing the string.
- Ownership of the string is not transferred;
- the caller is responsible for ensuring that
- the lifetime of the string extends until the
- view is no longer being accessed.
- @par BNF
- @code
- authority = [ userinfo "@" ] host [ ":" port ]
- userinfo = user [ ":" [ password ] ]
- user = *( unreserved / pct-encoded / sub-delims )
- password = *( unreserved / pct-encoded / sub-delims / ":" )
- host = IP-literal / IPv4address / reg-name
- port = *DIGIT
- @endcode
- @par Exception Safety
- Throws nothing.
- @return A view to the parsed authority
- @param s The string to parse
- @par Specification
- @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-3.2"
- >3.2. Authority (rfc3986)</a>
- @see
- @ref authority_view.
- */
- BOOST_URL_DECL
- system::result<authority_view>
- parse_authority(
- core::string_view s) noexcept;
- //------------------------------------------------
- } // urls
- } // boost
- #endif
|