100.00% Lines (53/53) 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   6702 host_rule_t:: 27   6702 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   6702 value_type t; 34   6702 value_type t;
35   35  
HITCBC 36   6702 if(it == end) 36   6702 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   6474 auto const it0 = it; 44   6474 auto const it0 = it;
HITCBC 45   6474 if(*it == '[') 45   6474 if(*it == '[')
46   { 46   {
47   // IP-literal 47   // IP-literal
HITCBC 48   72 auto rv = grammar::parse( 48   72 auto rv = grammar::parse(
49   it, end, 49   it, end,
50   detail::ip_literal_rule); 50   detail::ip_literal_rule);
HITCBC 51   72 if(! rv) 51   72 if(! rv)
HITCBC 52   25 return rv.error(); 52   25 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;
HITCBC 65   42 t.match = make_pct_string_view_unsafe( 65   42 t.match = make_pct_string_view_unsafe(
HITCBC 66   42 it0, it - it0, it - it0); 66   42 it0, it - it0, it - it0);
HITCBC 67   42 return t; 67   42 return t;
68   } 68   }
69   69  
70   // IPvFuture 70   // IPvFuture
HITCBC 71   5 t.host_type = 71   5 t.host_type =
72   urls::host_type::ipvfuture; 72   urls::host_type::ipvfuture;
HITCBC 73   5 t.match = make_pct_string_view_unsafe( 73   5 t.match = make_pct_string_view_unsafe(
HITCBC 74   5 it0, it - it0, it - it0); 74   5 it0, it - it0, it - it0);
HITCBC 75   5 return t; 75   5 return t;
76   } 76   }
77   77  
78   // IPv4address 78   // IPv4address
79   { 79   {
HITCBC 80   6402 auto rv = grammar::parse( 80   6402 auto rv = grammar::parse(
81   it, end, ipv4_address_rule); 81   it, end, ipv4_address_rule);
HITCBC 82   6402 if( rv ) 82   6402 if( rv )
83   { 83   {
HITCBC 84   108 auto it02 = it; 84   108 auto it02 = it;
HITCBC 85   108 auto rv2 = grammar::parse( 85   108 auto rv2 = grammar::parse(
86   it, end, 86   it, end,
87   detail::reg_name_rule); 87   detail::reg_name_rule);
HITCBC 88   215 if (rv2.has_value() && 88   215 if (rv2.has_value() &&
HITCBC 89   107 !rv2->empty()) 89   107 !rv2->empty())
90   { 90   {
HITCBC 91   6 auto dn = static_cast< 91   6 auto dn = static_cast<
HITCBC 92   6 std::size_t>(it02 - it0) + 92   6 std::size_t>(it02 - it0) +
HITCBC 93   6 rv2->decoded_size(); 93   6 rv2->decoded_size();
HITCBC 94   6 t.name = make_pct_string_view_unsafe( 94   6 t.name = make_pct_string_view_unsafe(
HITCBC 95   6 it0, it - it0, dn); 95   6 it0, it - it0, dn);
HITCBC 96   6 t.host_type = 96   6 t.host_type =
97   urls::host_type::name; 97   urls::host_type::name;
HITCBC 98   6 t.match = t.name; 98   6 t.match = t.name;
HITCBC 99   6 return t; 99   6 return t;
100   } 100   }
HITCBC 101   102 it = it02; 101   102 it = it02;
102   auto const b = 102   auto const b =
HITCBC 103   102 rv->to_bytes(); 103   102 rv->to_bytes();
HITCBC 104   204 std::memcpy( 104   204 std::memcpy(
105   t.addr, 105   t.addr,
HITCBC 106   102 b.data(), 106   102 b.data(),
107   b.size()); 107   b.size());
HITCBC 108   102 t.host_type = 108   102 t.host_type =
109   urls::host_type::ipv4; 109   urls::host_type::ipv4;
HITCBC 110   102 t.match = make_pct_string_view_unsafe( 110   102 t.match = make_pct_string_view_unsafe(
HITCBC 111   102 it0, it - it0, it - it0); 111   102 it0, it - it0, it - it0);
HITCBC 112   102 return t; 112   102 return t;
113   } 113   }
114   114  
HITCBC 115   6294 it = it0; // rewind 115   6294 it = it0; // rewind
116   } 116   }
117   117  
118   // reg-name 118   // reg-name
119   { 119   {
HITCBC 120   6294 auto rv = grammar::parse( 120   6294 auto rv = grammar::parse(
121   it, end, 121   it, end,
122   detail::reg_name_rule); 122   detail::reg_name_rule);
HITCBC 123   6294 if(! rv) 123   6294 if(! rv)
HITCBC 124   7 return rv.error(); 124   7 return rv.error();
HITCBC 125   6287 t.name = *rv; 125   6287 t.name = *rv;
HITCBC 126   6287 t.host_type = 126   6287 t.host_type =
127   urls::host_type::name; 127   urls::host_type::name;
HITCBC 128   6287 t.match = *rv; 128   6287 t.match = *rv;
HITCBC 129   6287 return t; 129   6287 return t;
130   } 130   }
131   } 131   }
132   132  
133   } // detail 133   } // detail
134   } // urls 134   } // urls
135   } // boost 135   } // boost
136   136  
137   137  
138   #endif 138   #endif