Kurlyk
Loading...
Searching...
No Matches
url_utils.hpp
Go to the documentation of this file.
1#pragma once
2#ifndef _KURLYK_UTILS_URL_UTILS_HPP_INCLUDED
3#define _KURLYK_UTILS_URL_UTILS_HPP_INCLUDED
4
7
8namespace kurlyk::utils {
9
13 inline std::string extract_protocol(const std::string& url) {
14 std::string protocol;
15 std::size_t pos = url.find("://");
16
17 if (pos != std::string::npos) {
18 protocol = url.substr(0, pos);
19 }
20
21 return protocol;
22 }
23
27 std::string remove_ws_prefix(const std::string& url) {
28 const std::string wss_prefix = "wss://";
29 const std::string ws_prefix = "ws://";
30
31 std::string modified_url = url;
32
33 // Find the position of "wss://" or "ws://"
34 std::size_t pos = modified_url.find(wss_prefix);
35 if (pos == std::string::npos) {
36 pos = modified_url.find(ws_prefix);
37 }
38
39 // If found, erase the substring
40 if (pos != std::string::npos) {
41 if (modified_url.compare(pos, wss_prefix.length(), wss_prefix) == 0) {
42 modified_url.erase(pos, wss_prefix.length());
43 } else if (modified_url.compare(pos, ws_prefix.length(), ws_prefix) == 0) {
44 modified_url.erase(pos, ws_prefix.length());
45 }
46 }
47
48 return modified_url;
49 }
50
55 bool is_valid_scheme(const std::string& url, const std::string& scheme) {
56 return url.compare(0, scheme.length(), scheme) == 0;
57 }
58
62 bool is_valid_domain(const std::string& domain) {
63 size_t dot_pos = domain.find('.');
64
65 if (dot_pos == std::string::npos ||
66 dot_pos == 0 ||
67 dot_pos == domain.length() - 1) {
68 return false;
69 }
70
71 for (char ch : domain) {
72 if (!isalnum(ch) && ch != '-' && ch != '.') {
73 return false;
74 }
75 }
76
77 std::string tld = domain.substr(dot_pos + 1);
78 for (char ch : tld) {
79 if (!isalpha(ch)) {
80 return false;
81 }
82 }
83 return true;
84 }
85
89 bool is_valid_path(const std::string& path) {
90 if (path.empty() || path[0] != '/') return false;
91 for (char ch : path) {
92 if (!isalnum(ch) && ch != '/' && ch != '-' && ch != '_') {
93 return false;
94 }
95 }
96 return true;
97 }
98
102 bool is_valid_query(const std::string& query) {
103 if (query.empty() || query[0] != '?') {
104 return false;
105 }
106
107 size_t pos = 1;
108 while (pos < query.length()) {
109 size_t equalPos = query.find('=', pos);
110 size_t ampPos = query.find('&', pos);
111
112 if (equalPos == std::string::npos || (ampPos != std::string::npos && ampPos < equalPos)) {
113 return false;
114 }
115
116 pos = (ampPos == std::string::npos) ? query.length() : ampPos + 1;
117 }
118
119 return true;
120 }
121
126 bool is_valid_url(const std::string& url, const std::vector<std::string>& protocol) {
127 size_t scheme_end = url.find("://");
128 if (scheme_end == std::string::npos) {
129 return false;
130 }
131
132 const std::string scheme = url.substr(0, scheme_end);
133 const auto it = std::find(protocol.begin(), protocol.end(), scheme);
134 if (it == protocol.end()) return false;
135
136 const size_t domain_start = scheme_end + 3;
137 const size_t path_start = url.find('/', domain_start);
138 const size_t query_start = url.find('?', domain_start);
139
140 // Определяем конец домена
141 size_t domain_end = (path_start != std::string::npos) ? path_start : query_start;
142 if (domain_end == std::string::npos) {
143 domain_end = url.length();
144 }
145
146 const std::string domain = url.substr(domain_start, domain_end - domain_start);
147 if (!is_valid_domain(domain)) {
148 return false;
149 }
150
151 // Проверяем путь, если он есть
152 if (path_start != std::string::npos) {
153 std::string path = (query_start != std::string::npos) ? url.substr(path_start, query_start - path_start) : url.substr(path_start);
154 if (!is_valid_path(path)) {
155 return false;
156 }
157 }
158
159 // Проверяем query, если он есть
160 if (query_start != std::string::npos) {
161 std::string query = url.substr(query_start);
162 if (!is_valid_query(query)) {
163 return false;
164 }
165 }
166
167 return true;
168 }
169
170} // namespace kurlyk::utils
171
172#endif // _KURLYK_UTILS_URL_UTILS_HPP_INCLUDED
bool is_valid_scheme(const std::string &url, const std::string &scheme)
Checks if a given URL starts with a specified scheme.
Definition url_utils.hpp:55
bool is_valid_url(const std::string &url, const std::vector< std::string > &protocol)
Validates if a URL is correctly formatted.
std::string extract_protocol(const std::string &url)
Extracts the protocol from a URL.
Definition url_utils.hpp:13
bool is_valid_query(const std::string &query)
Validates if a query string is correctly formatted.
std::string remove_ws_prefix(const std::string &url)
Removes the first occurrence of "wss://" or "ws://" from the given URL.
Definition url_utils.hpp:27
bool is_valid_path(const std::string &path)
Checks if a path is correctly formatted.
Definition url_utils.hpp:89
bool is_valid_domain(const std::string &domain)
Validates if a domain name is correctly formatted.
Definition url_utils.hpp:62