uri.cpp

main 24 lines · 1 KB Raw
Alex Alex Commit Client Upload Part 2 19/10/2026 00:06
1#include <boost/algorithm/string.hpp>
2#include <regex>
3
4#include "uri.h"
5
6ParsedURI parseURI(const std::string& url)
7{
8 ParsedURI result;
9 auto value_or = [](const std::string& value, std::string&& deflt) -> std::string {
10 return (value.empty() ? deflt : value);
11 };
12 // Note: only "http", "https", "ws", and "wss" protocols are supported
13 static const std::regex PARSE_URL{ R"((([httpsw]{2,5})://)?([^/ :]+)(:(\d+))?(/(.+)?))",
14 std::regex_constants::ECMAScript | std::regex_constants::icase };
15 std::smatch match;
16 if (std::regex_match(url, match, PARSE_URL) && match.size() == 8) {
17 result.protocol = value_or(boost::algorithm::to_lower_copy(std::string(match[2])), "http");
18 result.domain = match[3];
19 const bool is_sequre_protocol = (result.protocol == "https" || result.protocol == "wss");
20 result.port = value_or(match[5], (is_sequre_protocol) ? "443" : "80");
21 result.query = value_or(match[6], "/");
22 }
23 return result;
24}
Top