snitch: Check http response codes to be OK
Several snitch drivers make http requests to get region/dc/zone/rack/whatever from the cloud provider. They blindly rely on the response being successfull and read response body to parse the data they need from. That's not nice, add checks for requests finish with http OK statuses. refs: #12185 Signed-off-by: Pavel Emelyanov <xemul@scylladb.com> Closes #12287 Signed-off-by: Pavel Emelyanov <xemul@scylladb.com>
This commit is contained in:
@@ -15,6 +15,7 @@
|
||||
#include <seastar/core/coroutine.hh>
|
||||
#include <seastar/core/seastar.hh>
|
||||
#include <seastar/http/response_parser.hh>
|
||||
#include <seastar/http/reply.hh>
|
||||
#include <seastar/net/api.hh>
|
||||
#include <seastar/net/dns.hh>
|
||||
|
||||
@@ -90,6 +91,10 @@ future<sstring> azure_snitch::azure_api_call(sstring path) {
|
||||
|
||||
// Read HTTP response header first
|
||||
auto rsp = parser.get_parsed_response();
|
||||
if (rsp->_status_code != static_cast<int>(httpd::reply::status_type::ok)) {
|
||||
throw std::runtime_error(format("Error: HTTP response status {}", rsp->_status_code));
|
||||
}
|
||||
|
||||
auto it = rsp->_headers.find("Content-Length");
|
||||
if (it == rsp->_headers.end()) {
|
||||
throw std::runtime_error("Error: HTTP response does not contain: Content-Length\n");
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
#include <seastar/core/seastar.hh>
|
||||
#include <seastar/core/sleep.hh>
|
||||
#include <seastar/core/do_with.hh>
|
||||
#include <seastar/http/reply.hh>
|
||||
|
||||
#include <boost/algorithm/string/classification.hpp>
|
||||
#include <boost/algorithm/string/split.hpp>
|
||||
@@ -114,6 +115,9 @@ future<sstring> ec2_snitch::aws_api_call_once(sstring addr, uint16_t port, sstri
|
||||
|
||||
// Read HTTP response header first
|
||||
auto _rsp = _parser.get_parsed_response();
|
||||
if (_rsp->_status_code != static_cast<int>(httpd::reply::status_type::ok)) {
|
||||
return make_exception_future<sstring>(std::runtime_error(format("Error: HTTP response status {}", _rsp->_status_code)));
|
||||
}
|
||||
auto it = _rsp->_headers.find("Content-Length");
|
||||
if (it == _rsp->_headers.end()) {
|
||||
return make_exception_future<sstring>("Error: HTTP response does not contain: Content-Length\n");
|
||||
|
||||
@@ -14,6 +14,7 @@
|
||||
#include <seastar/net/dns.hh>
|
||||
#include <seastar/core/seastar.hh>
|
||||
#include "locator/gce_snitch.hh"
|
||||
#include <seastar/http/reply.hh>
|
||||
|
||||
#include <boost/algorithm/string/split.hpp>
|
||||
#include <boost/algorithm/string/classification.hpp>
|
||||
@@ -106,6 +107,10 @@ future<sstring> gce_snitch::gce_api_call(sstring addr, sstring cmd) {
|
||||
|
||||
// Read HTTP response header first
|
||||
auto rsp = parser.get_parsed_response();
|
||||
if (rsp->_status_code != static_cast<int>(httpd::reply::status_type::ok)) {
|
||||
throw std::runtime_error(format("Error: HTTP response status {}", rsp->_status_code));
|
||||
}
|
||||
|
||||
auto it = rsp->_headers.find("Content-Length");
|
||||
if (it == rsp->_headers.end()) {
|
||||
throw std::runtime_error("Error: HTTP response does not contain: Content-Length\n");
|
||||
|
||||
Reference in New Issue
Block a user