This is an automated email from the ASF dual-hosted git repository. alexey pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/kudu.git
commit 9050941663e532db29bd25cb7170712a80d945ea Author: Alexey Serbin <[email protected]> AuthorDate: Wed Feb 5 19:59:50 2020 -0800 [util] add CURLOPT_FAILONERROR for instance metadata requests By default, libcurl doesn't return an error if the server responds with HTTP response >= 400. It makes sense for handling auth-related codes such as 401 and 407. In case of retrieving cloud instance metadata it's more convenient to turn HTTP response codes >= 400 into CURLcode errors. Change-Id: I10ca30b13323cdd4cad65259dda78e0ee3c7d647 Reviewed-on: http://gerrit.cloudera.org:8080/15172 Reviewed-by: Adar Dembo <[email protected]> Tested-by: Alexey Serbin <[email protected]> --- src/kudu/util/cloud/instance_metadata.cc | 1 + src/kudu/util/curl_util.cc | 3 +++ src/kudu/util/curl_util.h | 11 +++++++++++ 3 files changed, 15 insertions(+) diff --git a/src/kudu/util/cloud/instance_metadata.cc b/src/kudu/util/cloud/instance_metadata.cc index b47fe3f..cc26419 100644 --- a/src/kudu/util/cloud/instance_metadata.cc +++ b/src/kudu/util/cloud/instance_metadata.cc @@ -153,6 +153,7 @@ Status InstanceMetadata::Fetch(const string& url, } EasyCurl curl; curl.set_timeout(timeout); + curl.set_fail_on_http_error(true); faststring resp; RETURN_NOT_OK(curl.FetchURL(url, &resp, headers)); if (out) { diff --git a/src/kudu/util/curl_util.cc b/src/kudu/util/curl_util.cc index d75cc54..82b0efc 100644 --- a/src/kudu/util/curl_util.cc +++ b/src/kudu/util/curl_util.cc @@ -132,6 +132,9 @@ Status EasyCurl::DoRequest(const string& url, if (verbose_) { CURL_RETURN_NOT_OK(curl_easy_setopt(curl_, CURLOPT_VERBOSE, 1)); } + if (fail_on_http_error_) { + CURL_RETURN_NOT_OK(curl_easy_setopt(curl_, CURLOPT_FAILONERROR, 1)); + } // Add headers if specified. struct curl_slist* curl_headers = nullptr; diff --git a/src/kudu/util/curl_util.h b/src/kudu/util/curl_util.h index 4ed07a0..c1ddcc2 100644 --- a/src/kudu/util/curl_util.h +++ b/src/kudu/util/curl_util.h @@ -84,6 +84,14 @@ class EasyCurl { custom_method_ = std::move(m); } + // Whether to return an error if server responds with HTTP code >= 400. + // By default, curl returns the returned content and the response code + // since it's handy in case of auth-related HTTP response codes such as + // 401 and 407. See 'man CURLOPT_FAILONERROR' for details. + void set_fail_on_http_error(bool fail_on_http_error) { + fail_on_http_error_ = fail_on_http_error; + } + // Returns the number of new connections created to achieve the previous transfer. int num_connects() const { return num_connects_; @@ -113,6 +121,9 @@ class EasyCurl { bool verbose_ = false; + // The default setting for CURLOPT_FAILONERROR in libcurl is 0 (false). + bool fail_on_http_error_ = false; + MonoDelta timeout_; int num_connects_ = 0;
