100.00% Lines (58/58) 100.00% Functions (1/1)
TLA Baseline Branch
Line Hits Code Line Hits Code
1   // 1   //
2   // Copyright (c) 2016-2019 Vinnie Falco (vinnie dot falco at gmail dot com) 2   // Copyright (c) 2016-2019 Vinnie Falco (vinnie dot falco at gmail dot com)
3   // Copyright (c) 2023 Alan de Freitas (alandefreitas@gmail.com) 3   // Copyright (c) 2023 Alan de Freitas (alandefreitas@gmail.com)
4   // 4   //
5   // Distributed under the Boost Software License, Version 1.0. (See accompanying 5   // Distributed under the Boost Software License, Version 1.0. (See accompanying
6   // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 6   // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7   // 7   //
8   // Official repository: https://github.com/boostorg/url 8   // Official repository: https://github.com/boostorg/url
9   // 9   //
10   10  
11   #ifndef BOOST_URL_RFC_DETAIL_IMPL_HOST_RULE_HPP 11   #ifndef BOOST_URL_RFC_DETAIL_IMPL_HOST_RULE_HPP
12   #define BOOST_URL_RFC_DETAIL_IMPL_HOST_RULE_HPP 12   #define BOOST_URL_RFC_DETAIL_IMPL_HOST_RULE_HPP
13   13  
14   #include <boost/url/detail/config.hpp> 14   #include <boost/url/detail/config.hpp>
15   #include <boost/url/rfc/ipv4_address_rule.hpp> 15   #include <boost/url/rfc/ipv4_address_rule.hpp>
16   #include <boost/url/rfc/detail/ip_literal_rule.hpp> 16   #include <boost/url/rfc/detail/ip_literal_rule.hpp>
17   #include <boost/url/rfc/detail/reg_name_rule.hpp> 17   #include <boost/url/rfc/detail/reg_name_rule.hpp>
18   #include <boost/url/grammar/parse.hpp> 18   #include <boost/url/grammar/parse.hpp>
19   #include <cstring> 19   #include <cstring>
20   20  
21   namespace boost { 21   namespace boost {
22   namespace urls { 22   namespace urls {
23   namespace detail { 23   namespace detail {
24   24  
25   BOOST_URL_CXX20_CONSTEXPR_OR_INLINE 25   BOOST_URL_CXX20_CONSTEXPR_OR_INLINE
26   auto 26   auto
HITCBC 27   6703 host_rule_t:: 27   6703 host_rule_t::
28   parse( 28   parse(
29   char const*& it, 29   char const*& it,
30   char const* const end 30   char const* const end
31   ) const noexcept -> 31   ) const noexcept ->
32   system::result<value_type> 32   system::result<value_type>
33   { 33   {
HITCBC 34   6703 value_type t; 34   6703 value_type t;
35   35  
HITCBC 36   6703 if(it == end) 36   6703 if(it == end)
37   { 37   {
38   // empty host 38   // empty host
HITCBC 39   228 t.host_type = 39   228 t.host_type =
40   urls::host_type::name; 40   urls::host_type::name;
HITCBC 41   228 return t; 41   228 return t;
42   } 42   }
43   43  
HITCBC 44   6475 auto const it0 = it; 44   6475 auto const it0 = it;
HITCBC 45   6475 if(*it == '[') 45   6475 if(*it == '[')
46   { 46   {
47   // IP-literal 47   // IP-literal
HITCBC 48   73 auto rv = grammar::parse( 48   73 auto rv = grammar::parse(
49   it, end, 49   it, end,
50   detail::ip_literal_rule); 50   detail::ip_literal_rule);
HITCBC 51   73 if(! rv) 51   73 if(! rv)
HITCBC 52   26 return rv.error(); 52   26 return rv.error();
HITCBC 53   47 auto v = *rv; 53   47 auto v = *rv;
HITCBC 54   47 if(v.is_ipv6) 54   47 if(v.is_ipv6)
55   { 55   {
56   // IPv6address 56   // IPv6address
57   auto const b = 57   auto const b =
HITCBC 58   42 v.ipv6.to_bytes(); 58   42 v.ipv6.to_bytes();
HITCBC 59   84 std::memcpy( 59   84 std::memcpy(
60   t.addr, 60   t.addr,
HITCBC 61   42 b.data(), 61   42 b.data(),
62   b.size()); 62   b.size());
HITCBC 63   42 t.host_type = 63   42 t.host_type =
64   urls::host_type::ipv6; 64   urls::host_type::ipv6;
HITGNC   65 + 42 auto dn = static_cast<
HITGNC   66 + 42 std::size_t>(it - it0);
HITGNC   67 + 42 if( !v.zone_id.empty())
  68 + // Account for '%25' -> '%' and recalculate the decoded zone_id size.
HITGNC   69 + 10 dn -= 2 + v.zone_id.size() -
HITGNC   70 + 5 v.zone_id.decoded_size();
HITCBC 65   42 t.match = make_pct_string_view_unsafe( 71   42 t.match = make_pct_string_view_unsafe(
HITCBC 66 - 42 it0, it - it0, it - it0); 72 + 42 it0, it - it0, dn);
HITCBC 67   42 return t; 73   42 return t;
68   } 74   }
69   75  
70   // IPvFuture 76   // IPvFuture
HITCBC 71   5 t.host_type = 77   5 t.host_type =
72   urls::host_type::ipvfuture; 78   urls::host_type::ipvfuture;
HITCBC 73   5 t.match = make_pct_string_view_unsafe( 79   5 t.match = make_pct_string_view_unsafe(
HITCBC 74   5 it0, it - it0, it - it0); 80   5 it0, it - it0, it - it0);
HITCBC 75   5 return t; 81   5 return t;
76   } 82   }
77   83  
78   // IPv4address 84   // IPv4address
79   { 85   {
HITCBC 80   6402 auto rv = grammar::parse( 86   6402 auto rv = grammar::parse(
81   it, end, ipv4_address_rule); 87   it, end, ipv4_address_rule);
HITCBC 82   6402 if( rv ) 88   6402 if( rv )
83   { 89   {
HITCBC 84   108 auto it02 = it; 90   108 auto it02 = it;
HITCBC 85   108 auto rv2 = grammar::parse( 91   108 auto rv2 = grammar::parse(
86   it, end, 92   it, end,
87   detail::reg_name_rule); 93   detail::reg_name_rule);
HITCBC 88   215 if (rv2.has_value() && 94   215 if (rv2.has_value() &&
HITCBC 89   107 !rv2->empty()) 95   107 !rv2->empty())
90   { 96   {
HITCBC 91   6 auto dn = static_cast< 97   6 auto dn = static_cast<
HITCBC 92   6 std::size_t>(it02 - it0) + 98   6 std::size_t>(it02 - it0) +
HITCBC 93   6 rv2->decoded_size(); 99   6 rv2->decoded_size();
HITCBC 94   6 t.name = make_pct_string_view_unsafe( 100   6 t.name = make_pct_string_view_unsafe(
HITCBC 95   6 it0, it - it0, dn); 101   6 it0, it - it0, dn);
HITCBC 96   6 t.host_type = 102   6 t.host_type =
97   urls::host_type::name; 103   urls::host_type::name;
HITCBC 98   6 t.match = t.name; 104   6 t.match = t.name;
HITCBC 99   6 return t; 105   6 return t;
100   } 106   }
HITCBC 101   102 it = it02; 107   102 it = it02;
102   auto const b = 108   auto const b =
HITCBC 103   102 rv->to_bytes(); 109   102 rv->to_bytes();
HITCBC 104   204 std::memcpy( 110   204 std::memcpy(
105   t.addr, 111   t.addr,
HITCBC 106   102 b.data(), 112   102 b.data(),
107   b.size()); 113   b.size());
HITCBC 108   102 t.host_type = 114   102 t.host_type =
109   urls::host_type::ipv4; 115   urls::host_type::ipv4;
HITCBC 110   102 t.match = make_pct_string_view_unsafe( 116   102 t.match = make_pct_string_view_unsafe(
HITCBC 111   102 it0, it - it0, it - it0); 117   102 it0, it - it0, it - it0);
HITCBC 112   102 return t; 118   102 return t;
113   } 119   }
114   120  
HITCBC 115   6294 it = it0; // rewind 121   6294 it = it0; // rewind
116   } 122   }
117   123  
118   // reg-name 124   // reg-name
119   { 125   {
HITCBC 120   6294 auto rv = grammar::parse( 126   6294 auto rv = grammar::parse(
121   it, end, 127   it, end,
122   detail::reg_name_rule); 128   detail::reg_name_rule);
HITCBC 123   6294 if(! rv) 129   6294 if(! rv)
HITCBC 124   7 return rv.error(); 130   7 return rv.error();
HITCBC 125   6287 t.name = *rv; 131   6287 t.name = *rv;
HITCBC 126   6287 t.host_type = 132   6287 t.host_type =
127   urls::host_type::name; 133   urls::host_type::name;
HITCBC 128   6287 t.match = *rv; 134   6287 t.match = *rv;
HITCBC 129   6287 return t; 135   6287 return t;
130   } 136   }
131   } 137   }
132   138  
133   } // detail 139   } // detail
134   } // urls 140   } // urls
135   } // boost 141   } // boost
136   142  
137   143  
138   #endif 144   #endif