1
0
mirror of synced 2025-04-21 22:25:55 +03:00

Fixed regex problem for recirect location

This commit is contained in:
yhirose 2020-04-21 21:18:29 -04:00
parent 129e2f00b8
commit 240cc85ccb

View File

@ -3885,7 +3885,7 @@ inline bool Client::redirect(const Request &req, Response &res) {
if (location.empty()) { return false; } if (location.empty()) { return false; }
const static std::regex re( const static std::regex re(
R"(^(?:([^:/?#]+):)?(?://([^/?#]*))?([^?#]*(?:\?[^#]*)?)(?:#.*)?)"); R"(^(?:(https?):)?(?://([^/?#]*)(?:(:\d+))?)?([^?#]*(?:\?[^#]*)?)(?:#.*)?)");
std::smatch m; std::smatch m;
if (!regex_match(location, m, re)) { return false; } if (!regex_match(location, m, re)) { return false; }
@ -3894,25 +3894,33 @@ inline bool Client::redirect(const Request &req, Response &res) {
auto next_scheme = m[1].str(); auto next_scheme = m[1].str();
auto next_host = m[2].str(); auto next_host = m[2].str();
auto next_path = m[3].str(); auto port_str = m[3].str();
if (next_scheme.empty()) { next_scheme = scheme; } auto next_path = m[4].str();
auto next_port = port_;
if (!port_str.empty()) {
next_port = std::stoi(port_str);
} else if (!next_scheme.empty()) {
next_port = next_scheme == "https" ? 443 : 80;
}
if (next_scheme.empty()) { next_scheme = scheme; } if (next_scheme.empty()) { next_scheme = scheme; }
if (next_host.empty()) { next_host = host_; } if (next_host.empty()) { next_host = host_; }
if (next_path.empty()) { next_path = "/"; } if (next_path.empty()) { next_path = "/"; }
if (next_scheme == scheme && next_host == host_) { if (next_scheme == scheme && next_host == host_ && next_port == port_) {
return detail::redirect(*this, req, res, next_path); return detail::redirect(*this, req, res, next_path);
} else { } else {
if (next_scheme == "https") { if (next_scheme == "https") {
#ifdef CPPHTTPLIB_OPENSSL_SUPPORT #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
SSLClient cli(next_host.c_str()); SSLClient cli(next_host.c_str(), next_port);
cli.copy_settings(*this); cli.copy_settings(*this);
return detail::redirect(cli, req, res, next_path); return detail::redirect(cli, req, res, next_path);
#else #else
return false; return false;
#endif #endif
} else { } else {
Client cli(next_host.c_str()); Client cli(next_host.c_str(), next_port);
cli.copy_settings(*this); cli.copy_settings(*this);
return detail::redirect(cli, req, res, next_path); return detail::redirect(cli, req, res, next_path);
} }