From 07be44a50a9a909bf211258bf2d27b29e27a473f Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Mon, 19 Aug 2019 21:19:16 +0200 Subject: [PATCH] lib/curl/Easy: add getter functions --- src/lib/curl/Easy.hxx | 18 ++++++++++++++++++ src/lib/curl/Request.cxx | 2 +- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/lib/curl/Easy.hxx b/src/lib/curl/Easy.hxx index 8d9596041..00d66c27d 100644 --- a/src/lib/curl/Easy.hxx +++ b/src/lib/curl/Easy.hxx @@ -30,6 +30,8 @@ #ifndef CURL_EASY_HXX #define CURL_EASY_HXX +#include "util/Compiler.h" + #include #include @@ -160,6 +162,22 @@ public: SetOption(CURLOPT_HTTPPOST, post); } + template + bool GetInfo(CURLINFO info, T value_r) const noexcept { + return ::curl_easy_getinfo(handle, info, value_r) == CURLE_OK; + } + + /** + * Returns the response body's size, or -1 if that is unknown. + */ + gcc_pure + int64_t GetContentLength() const noexcept { + double value; + return GetInfo(CURLINFO_CONTENT_LENGTH_DOWNLOAD, &value) + ? (int64_t)value + : -1; + } + bool Unpause() noexcept { return ::curl_easy_pause(handle, CURLPAUSE_CONT) == CURLE_OK; } diff --git a/src/lib/curl/Request.cxx b/src/lib/curl/Request.cxx index b08bb2095..809ef6ce4 100644 --- a/src/lib/curl/Request.cxx +++ b/src/lib/curl/Request.cxx @@ -140,7 +140,7 @@ CurlRequest::FinishHeaders() state = State::BODY; long status = 0; - curl_easy_getinfo(easy.Get(), CURLINFO_RESPONSE_CODE, &status); + easy.GetInfo(CURLINFO_RESPONSE_CODE, &status); handler.OnHeaders(status, std::move(headers)); }