Merge branch 'httpc' into online

This commit is contained in:
PabloMK7 2023-10-05 16:13:28 +02:00
commit 9a12f84ffa

View File

@ -68,18 +68,55 @@ const ResultCode ERROR_CERT_ALREADY_SET = // 0xD8A0A03D
// Splits URL into its components. Example: https://citra-emu.org:443/index.html
// is_https: true; host: citra-emu.org; port: 443; path: /index.html
static URLInfo SplitUrl(const std::string& url) {
constexpr u16 default_http_port = 80;
constexpr u16 default_https_port = 443;
const std::string prefix = "://";
constexpr int default_http_port = 80;
constexpr int default_https_port = 443;
const auto result = boost::urls::parse_uri(url);
const bool is_https = result->scheme_id() == boost::urls::scheme::https;
const auto port = result->port_number();
const auto default_port = is_https ? default_https_port : default_http_port;
std::string host;
int port = -1;
std::string path;
const auto scheme_end = url.find(prefix);
const auto prefix_end = scheme_end == std::string::npos ? 0 : scheme_end + prefix.length();
bool is_https = scheme_end != std::string::npos && url.starts_with("https");
const auto path_index = url.find("/", prefix_end);
if (path_index == std::string::npos) {
// If no path is specified after the host, set it to "/"
host = url.substr(prefix_end);
const auto port_start = host.find(":");
if (port_start != std::string::npos) {
std::string port_str = host.substr(port_start + 1);
host = host.substr(0, port_start);
char* pEnd = NULL;
port = std::strtol(port_str.c_str(), &pEnd, 10);
if (*pEnd) {
port = -1;
}
}
path = "/";
} else {
host = url.substr(prefix_end, path_index - prefix_end);
const auto port_start = host.find(":");
if (port_start != std::string::npos) {
std::string port_str = host.substr(port_start + 1);
host = host.substr(0, port_start);
char* pEnd = NULL;
port = std::strtol(port_str.c_str(), &pEnd, 10);
if (*pEnd) {
port = -1;
}
}
path = url.substr(path_index);
}
if (port == -1) {
port = is_https ? default_https_port : default_http_port;
}
return URLInfo{
.is_https = is_https,
.host = std::string{result->encoded_host()},
.port = port == 0 ? default_port : port,
.path = std::string{result->encoded_path()},
.host = host,
.port = port,
.path = path,
};
}