From 4c25e7898ec2583946537d8f5336f732effd0993 Mon Sep 17 00:00:00 2001 From: Julian Smith Date: Sun, 28 Feb 2021 22:25:16 +0000 Subject: [PATCH] simgear/io/HTTP*: added support for enabling compression when downloading. Uses underlying curl library's CURLOPT_ACCEPT_ENCODING. --- simgear/io/HTTPClient.cxx | 6 ++++++ simgear/io/HTTPRequest.cxx | 18 ++++++++++++++++++ simgear/io/HTTPRequest.hxx | 13 +++++++++++++ 3 files changed, 37 insertions(+) diff --git a/simgear/io/HTTPClient.cxx b/simgear/io/HTTPClient.cxx index d0f414b7..8acfb688 100644 --- a/simgear/io/HTTPClient.cxx +++ b/simgear/io/HTTPClient.cxx @@ -304,6 +304,12 @@ void Client::makeRequest(const Request_ptr& r) curl_easy_setopt(curlRequest, CURLOPT_RANGE, range.c_str()); SG_LOG(SG_GENERAL, SG_DEBUG, "Have added CURLOPT_RANGE: '" << range << "'"); } + + const char* enc = r->getAcceptEncoding(); + if (enc) { + curl_easy_setopt(curlRequest, CURLOPT_ACCEPT_ENCODING, const_cast(enc)); + SG_LOG(SG_GENERAL, SG_ALERT, "Have added CURLOPT_ACCEPT_ENCODING: '" << enc << "'"); + } const std::string method = strutils::lowercase (r->method()); if (method == "get") { diff --git a/simgear/io/HTTPRequest.cxx b/simgear/io/HTTPRequest.cxx index b9e6c6e1..de57f248 100644 --- a/simgear/io/HTTPRequest.cxx +++ b/simgear/io/HTTPRequest.cxx @@ -133,6 +133,24 @@ void Request::setRange(const std::string& range) _range = range; } +//------------------------------------------------------------------------------ +void Request::setAcceptEncoding(const char* enc) +{ + if (enc) { + _enc_set = true; + _enc = enc; + } + else { + _enc_set = false; + } +} + +//------------------------------------------------------------------------------ +const char* Request::getAcceptEncoding() +{ + return (_enc_set) ? _enc.c_str() : nullptr; +} + //------------------------------------------------------------------------------ void Request::requestStart() { diff --git a/simgear/io/HTTPRequest.hxx b/simgear/io/HTTPRequest.hxx index d9742323..27b2acab 100644 --- a/simgear/io/HTTPRequest.hxx +++ b/simgear/io/HTTPRequest.hxx @@ -138,6 +138,17 @@ public: * if we are updating a file that was fully downloaded previously. */ virtual void setRange(const std::string& range); + + /* + * Control underlying curl library's automatic decompression support. + * is passed directly to curl_easy_setopt(CURLOPT_ACCEPT_ENCODING); see + * CURLOPT_ACCEPT_ENCODING(3) for details. + * + * E.g. pass env="" to allow any available compression algorithm. + */ + virtual void setAcceptEncoding(const char* enc); + + const char* getAcceptEncoding(); virtual std::string method() const { return _method; } @@ -269,6 +280,8 @@ public: std::string _method; std::string _url; std::string _range; + bool _enc_set = false; + std::string _enc; StringMap _request_headers; std::string _request_data; std::string _request_media_type;