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) 2022 Alan de Freitas (alandefreitas@gmail.com)
3  
// Copyright (c) 2022 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  

11  

12  
#include <boost/url/detail/config.hpp>
12  
#include <boost/url/detail/config.hpp>
13  
#include <boost/url/rfc/ipv6_address_rule.hpp>
13  
#include <boost/url/rfc/ipv6_address_rule.hpp>
14  
#include "ip_literal_rule.hpp"
14  
#include "ip_literal_rule.hpp"
15  
#include "ipv6_addrz_rule.hpp"
15  
#include "ipv6_addrz_rule.hpp"
16  
#include <boost/url/grammar/delim_rule.hpp>
16  
#include <boost/url/grammar/delim_rule.hpp>
17  
#include <boost/url/grammar/parse.hpp>
17  
#include <boost/url/grammar/parse.hpp>
18  
#include <boost/url/grammar/tuple_rule.hpp>
18  
#include <boost/url/grammar/tuple_rule.hpp>
19  
#include "ipvfuture_rule.hpp"
19  
#include "ipvfuture_rule.hpp"
20  

20  

21  
namespace boost {
21  
namespace boost {
22  
namespace urls {
22  
namespace urls {
23  
namespace detail {
23  
namespace detail {
24  

24  

25  
auto
25  
auto
26  
ip_literal_rule_t::
26  
ip_literal_rule_t::
27  
parse(
27  
parse(
28  
    char const*& it,
28  
    char const*& it,
29  
    char const* const end
29  
    char const* const end
30  
        ) const noexcept ->
30  
        ) const noexcept ->
31  
    system::result<value_type>
31  
    system::result<value_type>
32  
{
32  
{
33  
    value_type t;
33  
    value_type t;
34  

34  

35  
    // '['
35  
    // '['
36  
    {
36  
    {
37  
        auto rv = grammar::parse(
37  
        auto rv = grammar::parse(
38  
            it, end, grammar::delim_rule('['));
38  
            it, end, grammar::delim_rule('['));
39  
        if(! rv)
39  
        if(! rv)
40  
            return rv.error();
40  
            return rv.error();
41  
    }
41  
    }
42  
    if(it == end)
42  
    if(it == end)
43  
    {
43  
    {
44  
        // end
44  
        // end
45  
        BOOST_URL_RETURN_EC(
45  
        BOOST_URL_RETURN_EC(
46  
            grammar::error::invalid);
46  
            grammar::error::invalid);
47  
    }
47  
    }
48  
    if(*it != 'v')
48  
    if(*it != 'v')
49  
    {
49  
    {
50  
        // IPv6address
50  
        // IPv6address
51  
        auto it0 = it;
51  
        auto it0 = it;
52  
        auto rv = grammar::parse(
52  
        auto rv = grammar::parse(
53  
            it, end,
53  
            it, end,
54  
            grammar::tuple_rule(
54  
            grammar::tuple_rule(
55  
                ipv6_address_rule,
55  
                ipv6_address_rule,
56  
                grammar::squelch(
56  
                grammar::squelch(
57  
                    grammar::delim_rule(']'))));
57  
                    grammar::delim_rule(']'))));
58  
        if(! rv)
58  
        if(! rv)
59  
        {
59  
        {
60  
            // IPv6addrz: https://datatracker.ietf.org/doc/html/rfc6874
60  
            // IPv6addrz: https://datatracker.ietf.org/doc/html/rfc6874
61  
            it = it0;
61  
            it = it0;
62  
            auto rv2 = grammar::parse(
62  
            auto rv2 = grammar::parse(
63  
                it, end,
63  
                it, end,
64  
                grammar::tuple_rule(
64  
                grammar::tuple_rule(
65  
                    ipv6_addrz_rule,
65  
                    ipv6_addrz_rule,
66  
                    grammar::squelch(
66  
                    grammar::squelch(
67  
                        grammar::delim_rule(']'))));
67  
                        grammar::delim_rule(']'))));
68  
            if(! rv2)
68  
            if(! rv2)
69  
                return rv2.error();
69  
                return rv2.error();
70  
            t.ipv6 = rv2->ipv6;
70  
            t.ipv6 = rv2->ipv6;
71  
            t.is_ipv6 = true;
71  
            t.is_ipv6 = true;
72  
            return t;
72  
            return t;
73  
        }
73  
        }
74  
        t.ipv6 = *rv;
74  
        t.ipv6 = *rv;
75  
        t.is_ipv6 = true;
75  
        t.is_ipv6 = true;
76  
        return t;
76  
        return t;
77  
    }
77  
    }
78  
    {
78  
    {
79  
        // IPvFuture
79  
        // IPvFuture
80  
        auto rv = grammar::parse(
80  
        auto rv = grammar::parse(
81  
            it, end, 
81  
            it, end, 
82  
            grammar::tuple_rule(
82  
            grammar::tuple_rule(
83  
                ipvfuture_rule,
83  
                ipvfuture_rule,
84  
                grammar::squelch(
84  
                grammar::squelch(
85  
                    grammar::delim_rule(']'))));
85  
                    grammar::delim_rule(']'))));
86  
        if(! rv)
86  
        if(! rv)
87  
            return rv.error();
87  
            return rv.error();
88  
        t.is_ipv6 = false;
88  
        t.is_ipv6 = false;
89  
        t.ipvfuture = rv->str;
89  
        t.ipvfuture = rv->str;
90  
        return t;
90  
        return t;
91  
    }
91  
    }
92  
}
92  
}
93  

93  

94  
} // detail
94  
} // detail
95  
} // urls
95  
} // urls
96  
} // boost
96  
} // boost
97  

97