libcpr / cpr

C++ Requests: Curl for People, a spiritual port of Python Requests.
https://docs.libcpr.org/
Other
6.49k stars 922 forks source link

cpr::Download failed to decode downloaded content of gzip encoding #1008

Closed amolvmisal closed 8 months ago

amolvmisal commented 9 months ago

Description

cpr::AcceptEncoding parameter does not work when downloading with cpr::Download causing it to fail to decode the downloaded file which is a gzip encoded.

Example/How to Reproduce

cpr::Url url{"http://anyurl/file.ext"}; // Make sure response should be with 'Content-Encoding: gzip' in it
cpr::Response r = cpr::Download(of, url);
std::cout << r.header["Content-Encoding"] << std::endl; // should be 'gzip'

After downloading you can see cpr::Download failed to decode the downloaded file.

Possible Fix

Following piece of code in Session::prepareCommonDownload solved the issue;

#if LIBCURL_VERSION_NUM >= 0x072100
    if (acceptEncoding_.empty()) {
        // Enable all supported built-in compressions
        curl_easy_setopt(curl_->handle, CURLOPT_ACCEPT_ENCODING, "");
    } else if (acceptEncoding_.disabled()) {
        // Disable curl adding the 'Accept-Encoding' header
        curl_easy_setopt(curl_->handle, CURLOPT_ACCEPT_ENCODING, nullptr);
    } else {
        curl_easy_setopt(curl_->handle, CURLOPT_ACCEPT_ENCODING, acceptEncoding_.getString().c_str());
    }
#endif

This piece of code is same as in Session::prepareCommon

Where did you get it from?

GitHub (branch e.g. master)

Additional Context/Your Environment

COM8 commented 8 months ago

Thanks @amolvmisal for reporting! I will look into it tomorrow.

COM8 commented 8 months ago

Workaroud for while I'm working on a PR for it:

cpr::Session session;
session.SetHeader(cpr::Header{{"Accept-Encoding", "gzip"}});
COM8 commented 8 months ago

A new PR to fix this exists: https://github.com/libcpr/cpr/pull/1010

Could you please verify, this fixes your issue?

amolvmisal commented 8 months ago

@COM8 my apology for late reply and thanks for quick response. I can confirm #1010 fixes the issue