#include #include #include namespace Void::Net { std::optional Url::parse(std::string_view url) { namespace urls = boost::urls; auto parsed = urls::parse_uri(url); if (!parsed) { // Some callers pass scheme-relative or relative refs; try the // looser reference parser before giving up. parsed = urls::parse_uri_reference(url); if (!parsed) { return std::nullopt; } } const auto& uri = *parsed; Url out; out.scheme = std::string{uri.scheme()}; out.host = std::string{uri.host()}; out.is_tls = (out.scheme == "https" || out.scheme == "wss"); if (uri.has_port()) { out.port = std::string{uri.port()}; } else { out.port = out.is_tls ? "443" : "80"; } out.path = std::string{uri.encoded_path()}; if (out.path.empty()) { out.path = "/"; } out.target = out.path; if (uri.has_query()) { out.query = std::string{uri.encoded_query()}; out.target += "?"; out.target += out.query; } return out; } } // namespace Void::Net