Debian Codegraph @

LspCpp/third_party/uri/src/detail/uri_parse_authority.cpp

raw
1 // Copyright 2016 Glyn Matthews.
2 // Distributed under the Boost Software License, Version 1.0.
3 // (See accompanying file LICENSE_1_0.txt or copy at
4 // http://www.boost.org/LICENSE_1_0.txt)
5
6 #include "uri_parse_authority.hpp"
7 #include <cstdlib>
8 #include <iterator>
9 #include <limits>
10 #include "grammar.hpp"
11
12 namespace network {
13 namespace detail {
14 namespace {
15 enum class authority_state { user_info, host, host_ipv6, port };
16 } // namespace
17
18 bool parse_authority(string_view::const_iterator &it,
19 string_view::const_iterator last,
20 optional<uri_part> &user_info, optional<uri_part> &host,
21 optional<uri_part> &port) {
22 auto first = it;
23
24 auto state = authority_state::user_info;
25 while (it != last) {
26 if (state == authority_state::user_info) {
27 if (is_in(first, last, "@:")) {
28 return false;
29 }
30
31 if (*it == '@') {
32 user_info = uri_part(first, it);
33 state = authority_state::host;
34 ++it;
35 first = it;
36 continue;
37 } else if (*it == '[') {
38 // this is an IPv6 address
39 state = authority_state::host_ipv6;
40 first = it;
41 continue;
42 } else if (*it == ':') {
43 // this is actually the host, and the next part is expected to be the
44 // port
45 host = uri_part(first, it);
46 state = authority_state::port;
47 ++it;
48 first = it;
49 continue;
50 }
51 } else if (state == authority_state::host) {
52 if (*first == ':') {
53 return false;
54 }
55
56 if (*it == ':') {
57 host = uri_part(first, it);
58 state = authority_state::port;
59 ++it;
60 first = it;
61 continue;
62 }
63 } else if (state == authority_state::host_ipv6) {
64 if (*first != '[') {
65 return false;
66 }
67
68 if (*it == ']') {
69 host = uri_part(first, it);
70 ++it;
71 // Then test if the next part is a host, part, or the end of the file
72 if (it == last) {
73 break;
74 } else if (*it == ':') {
75 host = uri_part(first, it);
76 state = authority_state::port;
77 ++it;
78 first = it;
79 }
80 }
81 } else if (state == authority_state::port) {
82 if (*first == '/') {
83 // the port is empty, but valid
84 port = uri_part(first, it);
85 if (!is_valid_port(std::begin(*port))) {
86 return false;
87 }
88
89 continue;
90 }
91
92 if (!isdigit(it, last)) {
93 return false;
94 }
95 }
96
97 ++it;
98 }
99
100 if (state == authority_state::user_info) {
101 host = uri_part(first, last);
102 } else if (state == authority_state::host) {
103 host = uri_part(first, last);
104 } else if (state == authority_state::host_ipv6) {
105 host = uri_part(first, last);
106 } else if (state == authority_state::port) {
107 port = uri_part(first, last);
108 if (!is_valid_port(std::begin(*port))) {
109 return false;
110 }
111 }
112
113 return true;
114 }
115 } // namespace detail
116 } // namespace network