http_client: Handle relative redirects (#6049)

This commit is contained in:
Teddy Reed 2019-11-19 00:22:41 -05:00 committed by GitHub
parent 0f9b15b05b
commit fa8ac48a87
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 6 deletions

View File

@ -377,10 +377,11 @@ Response Client::sendHTTPRequest(Request& req) {
boost::posix_time::seconds(client_options_.timeout_));
}
bool retry_connect = false;
size_t redirect_attempts = 0;
bool init_request = true;
do {
bool create_connection = true;
if (!retry_connect) {
if (init_request) {
create_connection = initHTTPRequest(req);
}
@ -412,14 +413,35 @@ Response Client::sendHTTPRequest(Request& req) {
return Response(resp.release());
}
if (redirect_attempts++ >= 10) {
throw std::runtime_error("Exceeded max of 10 redirects");
}
std::string redir_url = Response(resp.release()).headers()["Location"];
if (!redir_url.size()) {
throw std::runtime_error(
"Location header missing in redirect response");
}
req.uri(redir_url);
VLOG(1) << "HTTP(S) request re-directed to: " << redir_url;
if (redir_url[0] == '/') {
// Relative URI.
if (req.remotePort()) {
redir_url.insert(0, *req.remotePort());
redir_url.insert(0, ":");
}
if (req.remoteHost()) {
redir_url.insert(0, *req.remoteHost());
}
if (req.protocol()) {
redir_url.insert(0, "://");
redir_url.insert(0, *req.protocol());
}
} else {
// Absolute URI.
init_request = true;
}
req.uri(redir_url);
break;
}
default:
@ -427,8 +449,8 @@ Response Client::sendHTTPRequest(Request& req) {
}
} catch (std::exception const& /* e */) {
closeSocket();
if (!retry_connect && ec_ != boost::asio::error::timed_out) {
retry_connect = true;
if (init_request && ec_ != boost::asio::error::timed_out) {
init_request = false;
} else {
ec_.clear();
throw;

View File

@ -79,7 +79,7 @@ QueryData genCurl(QueryContext& context) {
auto status = processRequest(r);
if (!status.ok()) {
LOG(WARNING) << status.getMessage();
LOG(WARNING) << "Error making request: " << status.getMessage();
}
results.push_back(r);