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  
//
4  
//
4  
// Distributed under the Boost Software License, Version 1.0. (See accompanying
5  
// 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)
6  
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6  
//
7  
//
7  
// Official repository: https://github.com/boostorg/url
8  
// Official repository: https://github.com/boostorg/url
8  
//
9  
//
9  

10  

10  

11  

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

15  

15  
namespace boost {
16  
namespace boost {
16 -

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

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

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

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

 
48 -
        default:
 
49 -
            break;
 
50 -
        }
 
51 -
        break;
 
52 -

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

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

 
70 -
        default:
 
71 -
            break;
 
72 -
        }
 
73 -
        break;
 
74 -

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

 
84 -
    default:
 
85 -
        break;
 
86 -
    }
 
87 -
    return scheme::unknown;
 
88 -
}
 
89  
namespace urls {
17  
namespace urls {
90  

18  

91  
core::string_view
19  
core::string_view
92  
to_string(scheme s) noexcept
20  
to_string(scheme s) noexcept
93  
{
21  
{
94  
    switch(s)
22  
    switch(s)
95  
    {
23  
    {
96  
    case scheme::ftp:   return "ftp";
24  
    case scheme::ftp:   return "ftp";
97  
    case scheme::file:  return "file";
25  
    case scheme::file:  return "file";
98  
    case scheme::http:  return "http";
26  
    case scheme::http:  return "http";
99  
    case scheme::https: return "https";
27  
    case scheme::https: return "https";
100  
    case scheme::ws:    return "ws";
28  
    case scheme::ws:    return "ws";
101  
    case scheme::wss:   return "wss";
29  
    case scheme::wss:   return "wss";
102  
    case scheme::none:  return {};
30  
    case scheme::none:  return {};
103  
    default:
31  
    default:
104  
        break;
32  
        break;
105  
    }
33  
    }
106  
    return "<unknown>";
34  
    return "<unknown>";
107  
}
35  
}
108  

36  

109  
std::uint16_t
37  
std::uint16_t
110  
default_port(scheme s) noexcept
38  
default_port(scheme s) noexcept
111  
{
39  
{
112  
    switch(s)
40  
    switch(s)
113  
    {
41  
    {
114  
    case scheme::ftp:
42  
    case scheme::ftp:
115  
        return 21;
43  
        return 21;
116  
    case scheme::http:
44  
    case scheme::http:
117  
    case scheme::ws:
45  
    case scheme::ws:
118  
        return 80;
46  
        return 80;
119  
    case scheme::https:
47  
    case scheme::https:
120  
    case scheme::wss:
48  
    case scheme::wss:
121  
        return 443;
49  
        return 443;
122  
    default:
50  
    default:
123  
        break;
51  
        break;
124  
    }
52  
    }
125  
    return 0;
53  
    return 0;
126  
}
54  
}
127  

55  

128  
} // urls
56  
} // urls
129  
} // boost
57  
} // boost
130  

58