1  
//
1  
//
2  
// Copyright (c) 2019 Vinnie Falco (vinnie.falco@gmail.com)
2  
// Copyright (c) 2019 Vinnie Falco (vinnie.falco@gmail.com)
3  
//
3  
//
4  
// Distributed under the Boost Software License, Version 1.0. (See accompanying
4  
// Distributed under the Boost Software License, Version 1.0. (See accompanying
5  
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
5  
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6  
//
6  
//
7  
// Official repository: https://github.com/boostorg/url
7  
// Official repository: https://github.com/boostorg/url
8  
//
8  
//
9  

9  

10  

10  

11  
#include <boost/url/detail/config.hpp>
11  
#include <boost/url/detail/config.hpp>
12  
#include <boost/url/scheme.hpp>
12  
#include <boost/url/scheme.hpp>
13  
#include <boost/url/grammar/ci_string.hpp>
13  
#include <boost/url/grammar/ci_string.hpp>
14  

14  

15  
namespace boost {
15  
namespace boost {
16  
namespace urls {
16  
namespace urls {
17  

17  

18  
scheme
18  
scheme
19  
string_to_scheme(
19  
string_to_scheme(
20  
    core::string_view s) noexcept
20  
    core::string_view s) noexcept
21  
{
21  
{
22  
    using grammar::to_lower;
22  
    using grammar::to_lower;
23  
    switch(s.size())
23  
    switch(s.size())
24  
    {
24  
    {
25  
    case 0: // none
25  
    case 0: // none
26  
        return scheme::none;
26  
        return scheme::none;
27  

27  

28  
    case 2: // ws
28  
    case 2: // ws
29  
        if( to_lower(s[0]) == 'w' &&
29  
        if( to_lower(s[0]) == 'w' &&
30  
            to_lower(s[1]) == 's')
30  
            to_lower(s[1]) == 's')
31  
            return scheme::ws;
31  
            return scheme::ws;
32  
        break;
32  
        break;
33  

33  

34  
    case 3:
34  
    case 3:
35  
        switch(to_lower(s[0]))
35  
        switch(to_lower(s[0]))
36  
        {
36  
        {
37  
        case 'w': // wss
37  
        case 'w': // wss
38  
            if( to_lower(s[1]) == 's' &&
38  
            if( to_lower(s[1]) == 's' &&
39  
                to_lower(s[2]) == 's')
39  
                to_lower(s[2]) == 's')
40  
                return scheme::wss;
40  
                return scheme::wss;
41  
            break;
41  
            break;
42  

42  

43  
        case 'f': // ftp
43  
        case 'f': // ftp
44  
            if( to_lower(s[1]) == 't' &&
44  
            if( to_lower(s[1]) == 't' &&
45  
                to_lower(s[2]) == 'p')
45  
                to_lower(s[2]) == 'p')
46  
                return scheme::ftp;
46  
                return scheme::ftp;
47  
            break;
47  
            break;
48  

48  

49  
        default:
49  
        default:
50  
            break;
50  
            break;
51  
        }
51  
        }
52  
        break;
52  
        break;
53  

53  

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

63  

64  
        case 'h': // http
64  
        case 'h': // http
65  
            if( to_lower(s[1]) == 't' &&
65  
            if( to_lower(s[1]) == 't' &&
66  
                to_lower(s[2]) == 't' &&
66  
                to_lower(s[2]) == 't' &&
67  
                to_lower(s[3]) == 'p')
67  
                to_lower(s[3]) == 'p')
68  
                return scheme::http;
68  
                return scheme::http;
69  
            break;
69  
            break;
70  

70  

71  
        default:
71  
        default:
72  
            break;
72  
            break;
73  
        }
73  
        }
74  
        break;
74  
        break;
75  

75  

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

84  

85  
    default:
85  
    default:
86  
        break;
86  
        break;
87  
    }
87  
    }
88  
    return scheme::unknown;
88  
    return scheme::unknown;
89  
}
89  
}
90  

90  

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

108  

109  
std::uint16_t
109  
std::uint16_t
110  
default_port(scheme s) noexcept
110  
default_port(scheme s) noexcept
111  
{
111  
{
112  
    switch(s)
112  
    switch(s)
113  
    {
113  
    {
114  
    case scheme::ftp:
114  
    case scheme::ftp:
115  
        return 21;
115  
        return 21;
116  
    case scheme::http:
116  
    case scheme::http:
117  
    case scheme::ws:
117  
    case scheme::ws:
118  
        return 80;
118  
        return 80;
119  
    case scheme::https:
119  
    case scheme::https:
120  
    case scheme::wss:
120  
    case scheme::wss:
121  
        return 443;
121  
        return 443;
122  
    default:
122  
    default:
123  
        break;
123  
        break;
124  
    }
124  
    }
125  
    return 0;
125  
    return 0;
126  
}
126  
}
127  

127  

128  
} // urls
128  
} // urls
129  
} // boost
129  
} // boost
130  

130