100.00% Lines (59/59) 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   6731 host_rule_t:: 27   6745 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   6731 value_type t; 34   6745 value_type t;
35   35  
HITCBC 36   6731 if(it == end) 36   6745 if(it == end)
37   { 37   {
38   // empty host 38   // empty host
HITCBC 39   228 t.host_type = 39   230 t.host_type =
40   urls::host_type::name; 40   urls::host_type::name;
HITCBC 41   228 return t; 41   230 return t;
42   } 42   }
43   43  
HITCBC 44   6503 auto const it0 = it; 44   6515 auto const it0 = it;
HITCBC 45   6503 if(*it == '[') 45   6515 if(*it == '[')
46   { 46   {
47   // IP-literal 47   // IP-literal
HITCBC 48   92 auto rv = grammar::parse( 48   92 auto rv = grammar::parse(
49   it, end, 49   it, end,
50   detail::ip_literal_rule); 50   detail::ip_literal_rule);
HITCBC 51   92 if(! rv) 51   92 if(! rv)
HITCBC 52   26 return rv.error(); 52   26 return rv.error();
HITCBC 53   66 auto v = *rv; 53   66 auto v = *rv;
HITCBC 54   66 if(v.is_ipv6) 54   66 if(v.is_ipv6)
55   { 55   {
56   // IPv6address 56   // IPv6address
57   auto const b = 57   auto const b =
HITCBC 58   61 v.ipv6.to_bytes(); 58   61 v.ipv6.to_bytes();
HITCBC 59   122 std::memcpy( 59   122 std::memcpy(
60   t.addr, 60   t.addr,
HITCBC 61   61 b.data(), 61   61 b.data(),
62   b.size()); 62   b.size());
HITCBC 63   61 t.host_type = 63   61 t.host_type =
64   urls::host_type::ipv6; 64   urls::host_type::ipv6;
65   // "[" IPv6address [ "%25" ZoneID ] "]" 65   // "[" IPv6address [ "%25" ZoneID ] "]"
66   // decodes to 66   // decodes to
67   // "[" IPv6address [ "%" zone_id ] "]" 67   // "[" IPv6address [ "%" zone_id ] "]"
HITCBC 68   61 auto dn = static_cast< 68   61 auto dn = static_cast<
HITCBC 69   61 std::size_t>(it - it0); 69   61 std::size_t>(it - it0);
HITCBC 70   61 if(! v.zone_id.empty()) 70   61 if(! v.zone_id.empty())
HITCBC 71   21 dn = dn 71   21 dn = dn
HITCBC 72   21 - (3 + v.zone_id.size()) 72   21 - (3 + v.zone_id.size())
HITCBC 73   21 + (1 + v.zone_id.decoded_size()); 73   21 + (1 + v.zone_id.decoded_size());
HITCBC 74   61 t.match = make_pct_string_view_unsafe( 74   61 t.match = make_pct_string_view_unsafe(
HITCBC 75   61 it0, it - it0, dn); 75   61 it0, it - it0, dn);
HITCBC 76   61 return t; 76   61 return t;
77   } 77   }
78   78  
79   // IPvFuture 79   // IPvFuture
HITCBC 80   5 t.host_type = 80   5 t.host_type =
81   urls::host_type::ipvfuture; 81   urls::host_type::ipvfuture;
HITCBC 82   5 t.match = make_pct_string_view_unsafe( 82   5 t.match = make_pct_string_view_unsafe(
HITCBC 83   5 it0, it - it0, it - it0); 83   5 it0, it - it0, it - it0);
HITCBC 84   5 return t; 84   5 return t;
85   } 85   }
86   86  
87   // IPv4address 87   // IPv4address
88   { 88   {
HITCBC 89   6411 auto rv = grammar::parse( 89   6423 auto rv = grammar::parse(
90   it, end, ipv4_address_rule); 90   it, end, ipv4_address_rule);
HITCBC 91   6411 if( rv ) 91   6423 if( rv )
92   { 92   {
HITCBC 93   108 auto it02 = it; 93   108 auto it02 = it;
HITCBC 94   108 auto rv2 = grammar::parse( 94   108 auto rv2 = grammar::parse(
95   it, end, 95   it, end,
96   detail::reg_name_rule); 96   detail::reg_name_rule);
HITCBC 97   215 if (rv2.has_value() && 97   215 if (rv2.has_value() &&
HITCBC 98   107 !rv2->empty()) 98   107 !rv2->empty())
99   { 99   {
HITCBC 100   6 auto dn = static_cast< 100   6 auto dn = static_cast<
HITCBC 101   6 std::size_t>(it02 - it0) + 101   6 std::size_t>(it02 - it0) +
HITCBC 102   6 rv2->decoded_size(); 102   6 rv2->decoded_size();
HITCBC 103   6 t.name = make_pct_string_view_unsafe( 103   6 t.name = make_pct_string_view_unsafe(
HITCBC 104   6 it0, it - it0, dn); 104   6 it0, it - it0, dn);
HITCBC 105   6 t.host_type = 105   6 t.host_type =
106   urls::host_type::name; 106   urls::host_type::name;
HITCBC 107   6 t.match = t.name; 107   6 t.match = t.name;
HITCBC 108   6 return t; 108   6 return t;
109   } 109   }
HITCBC 110   102 it = it02; 110   102 it = it02;
111   auto const b = 111   auto const b =
HITCBC 112   102 rv->to_bytes(); 112   102 rv->to_bytes();
HITCBC 113   204 std::memcpy( 113   204 std::memcpy(
114   t.addr, 114   t.addr,
HITCBC 115   102 b.data(), 115   102 b.data(),
116   b.size()); 116   b.size());
HITCBC 117   102 t.host_type = 117   102 t.host_type =
118   urls::host_type::ipv4; 118   urls::host_type::ipv4;
HITCBC 119   102 t.match = make_pct_string_view_unsafe( 119   102 t.match = make_pct_string_view_unsafe(
HITCBC 120   102 it0, it - it0, it - it0); 120   102 it0, it - it0, it - it0);
HITCBC 121   102 return t; 121   102 return t;
122   } 122   }
123   123  
HITCBC 124   6303 it = it0; // rewind 124   6315 it = it0; // rewind
125   } 125   }
126   126  
127   // reg-name 127   // reg-name
128   { 128   {
HITCBC 129   6303 auto rv = grammar::parse( 129   6315 auto rv = grammar::parse(
130   it, end, 130   it, end,
131   detail::reg_name_rule); 131   detail::reg_name_rule);
HITCBC 132   6303 if(! rv) 132   6315 if(! rv)
HITCBC 133   7 return rv.error(); 133   7 return rv.error();
HITCBC 134   6296 t.name = *rv; 134   6308 t.name = *rv;
HITCBC 135   6296 t.host_type = 135   6308 t.host_type =
136   urls::host_type::name; 136   urls::host_type::name;
HITCBC 137   6296 t.match = *rv; 137   6308 t.match = *rv;
HITCBC 138   6296 return t; 138   6308 return t;
139   } 139   }
140   } 140   }
141   141  
142   } // detail 142   } // detail
143   } // urls 143   } // urls
144   } // boost 144   } // boost
145   145  
146   146  
147   #endif 147   #endif