1  
//
1  
//
2  
// Copyright (c) 2019 Vinnie Falco (vinnie.falco@gmail.com)
2  
// Copyright (c) 2019 Vinnie Falco (vinnie.falco@gmail.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_IMPL_SCHEME_HPP
11  
#ifndef BOOST_URL_IMPL_SCHEME_HPP
12  
#define BOOST_URL_IMPL_SCHEME_HPP
12  
#define BOOST_URL_IMPL_SCHEME_HPP
13  

13  

14  
#include <boost/url/detail/config.hpp>
14  
#include <boost/url/detail/config.hpp>
15  

15  

16  
#include <boost/url/grammar/ci_string.hpp>
16  
#include <boost/url/grammar/ci_string.hpp>
17  

17  

18  
namespace boost {
18  
namespace boost {
19  
namespace urls {
19  
namespace urls {
20  

20  

21  
BOOST_URL_CXX20_CONSTEXPR_OR_INLINE
21  
BOOST_URL_CXX20_CONSTEXPR_OR_INLINE
22  
scheme
22  
scheme
23  
string_to_scheme(
23  
string_to_scheme(
24  
    core::string_view s) noexcept
24  
    core::string_view s) noexcept
25  
{
25  
{
26  
    using grammar::to_lower;
26  
    using grammar::to_lower;
27  
    switch(s.size())
27  
    switch(s.size())
28  
    {
28  
    {
29  
    case 0: // none
29  
    case 0: // none
30  
        return scheme::none;
30  
        return scheme::none;
31  

31  

32  
    case 2: // ws
32  
    case 2: // ws
33  
        if( to_lower(s[0]) == 'w' &&
33  
        if( to_lower(s[0]) == 'w' &&
34  
            to_lower(s[1]) == 's')
34  
            to_lower(s[1]) == 's')
35  
            return scheme::ws;
35  
            return scheme::ws;
36  
        break;
36  
        break;
37  

37  

38  
    case 3:
38  
    case 3:
39  
        switch(to_lower(s[0]))
39  
        switch(to_lower(s[0]))
40  
        {
40  
        {
41  
        case 'w': // wss
41  
        case 'w': // wss
42  
            if( to_lower(s[1]) == 's' &&
42  
            if( to_lower(s[1]) == 's' &&
43  
                to_lower(s[2]) == 's')
43  
                to_lower(s[2]) == 's')
44  
                return scheme::wss;
44  
                return scheme::wss;
45  
            break;
45  
            break;
46  

46  

47  
        case 'f': // ftp
47  
        case 'f': // ftp
48  
            if( to_lower(s[1]) == 't' &&
48  
            if( to_lower(s[1]) == 't' &&
49  
                to_lower(s[2]) == 'p')
49  
                to_lower(s[2]) == 'p')
50  
                return scheme::ftp;
50  
                return scheme::ftp;
51  
            break;
51  
            break;
52  

52  

53  
        default:
53  
        default:
54  
            break;
54  
            break;
55  
        }
55  
        }
56  
        break;
56  
        break;
57  

57  

58  
    case 4:
58  
    case 4:
59  
        switch(to_lower(s[0]))
59  
        switch(to_lower(s[0]))
60  
        {
60  
        {
61  
        case 'f': // file
61  
        case 'f': // file
62  
            if( to_lower(s[1]) == 'i' &&
62  
            if( to_lower(s[1]) == 'i' &&
63  
                to_lower(s[2]) == 'l' &&
63  
                to_lower(s[2]) == 'l' &&
64  
                to_lower(s[3]) == 'e')
64  
                to_lower(s[3]) == 'e')
65  
                return scheme::file;
65  
                return scheme::file;
66  
            break;
66  
            break;
67  

67  

68  
        case 'h': // http
68  
        case 'h': // http
69  
            if( to_lower(s[1]) == 't' &&
69  
            if( to_lower(s[1]) == 't' &&
70  
                to_lower(s[2]) == 't' &&
70  
                to_lower(s[2]) == 't' &&
71  
                to_lower(s[3]) == 'p')
71  
                to_lower(s[3]) == 'p')
72  
                return scheme::http;
72  
                return scheme::http;
73  
            break;
73  
            break;
74  

74  

75  
        default:
75  
        default:
76  
            break;
76  
            break;
77  
        }
77  
        }
78  
        break;
78  
        break;
79  

79  

80  
    case 5: // https
80  
    case 5: // https
81  
        if( to_lower(s[0]) == 'h' &&
81  
        if( to_lower(s[0]) == 'h' &&
82  
            to_lower(s[1]) == 't' &&
82  
            to_lower(s[1]) == 't' &&
83  
            to_lower(s[2]) == 't' &&
83  
            to_lower(s[2]) == 't' &&
84  
            to_lower(s[3]) == 'p' &&
84  
            to_lower(s[3]) == 'p' &&
85  
            to_lower(s[4]) == 's')
85  
            to_lower(s[4]) == 's')
86  
            return scheme::https;
86  
            return scheme::https;
87  
        break;
87  
        break;
88  

88  

89  
    default:
89  
    default:
90  
        break;
90  
        break;
91  
    }
91  
    }
92  
    return scheme::unknown;
92  
    return scheme::unknown;
93  
}
93  
}
94  

94  

95  
inline
95  
inline
96  
core::string_view
96  
core::string_view
97  
to_string(scheme s) noexcept
97  
to_string(scheme s) noexcept
98  
{
98  
{
99  
    switch(s)
99  
    switch(s)
100  
    {
100  
    {
101  
    case scheme::ftp:   return "ftp";
101  
    case scheme::ftp:   return "ftp";
102  
    case scheme::file:  return "file";
102  
    case scheme::file:  return "file";
103  
    case scheme::http:  return "http";
103  
    case scheme::http:  return "http";
104  
    case scheme::https: return "https";
104  
    case scheme::https: return "https";
105  
    case scheme::ws:    return "ws";
105  
    case scheme::ws:    return "ws";
106  
    case scheme::wss:   return "wss";
106  
    case scheme::wss:   return "wss";
107  
    case scheme::none:  return {};
107  
    case scheme::none:  return {};
108  
    default:
108  
    default:
109  
        break;
109  
        break;
110  
    }
110  
    }
111  
    return "<unknown>";
111  
    return "<unknown>";
112  
}
112  
}
113  

113  

114  
inline
114  
inline
115  
std::uint16_t
115  
std::uint16_t
116  
default_port(scheme s) noexcept
116  
default_port(scheme s) noexcept
117  
{
117  
{
118  
    switch(s)
118  
    switch(s)
119  
    {
119  
    {
120  
    case scheme::ftp:
120  
    case scheme::ftp:
121  
        return 21;
121  
        return 21;
122  
    case scheme::http:
122  
    case scheme::http:
123  
    case scheme::ws:
123  
    case scheme::ws:
124  
        return 80;
124  
        return 80;
125  
    case scheme::https:
125  
    case scheme::https:
126  
    case scheme::wss:
126  
    case scheme::wss:
127  
        return 443;
127  
        return 443;
128  
    default:
128  
    default:
129  
        break;
129  
        break;
130  
    }
130  
    }
131  
    return 0;
131  
    return 0;
132  
}
132  
}
133  

133  

134  
} // urls
134  
} // urls
135  
} // boost
135  
} // boost
136  

136  

137  
#endif
137  
#endif