From 8432facd6e15e8ff0046f0d9604dcf779ba7866a Mon Sep 17 00:00:00 2001 From: Tim Perry Date: Mon, 8 Apr 2024 14:05:59 +0200 Subject: [PATCH] Add libcurl & Go compression handling too --- src/targets/c/libcurl.js | 10 ++++++++-- src/targets/go/native.js | 6 ++++++ test/fixtures/output/c/libcurl/compression.c | 2 ++ test/fixtures/output/go/native/compression.go | 2 -- 4 files changed, 16 insertions(+), 4 deletions(-) diff --git a/src/targets/c/libcurl.js b/src/targets/c/libcurl.js index be3a218..729bbec 100644 --- a/src/targets/c/libcurl.js +++ b/src/targets/c/libcurl.js @@ -1,6 +1,7 @@ 'use strict' const CodeBuilder = require('../../helpers/code-builder') +const helpers = require('../../helpers/headers') module.exports = function (source, options) { const code = new CodeBuilder() @@ -26,9 +27,9 @@ module.exports = function (source, options) { } // construct cookies - if (source.allHeaders.cookie) { + if (helpers.hasHeader(source.allHeaders, 'cookie')) { code.blank() - .push('curl_easy_setopt(hnd, CURLOPT_COOKIE, "%s");', source.allHeaders.cookie) + .push('curl_easy_setopt(hnd, CURLOPT_COOKIE, "%s");', helpers.getHeader(source.allHeaders, 'cookie')) } if (source.postData.text) { @@ -36,6 +37,11 @@ module.exports = function (source, options) { .push('curl_easy_setopt(hnd, CURLOPT_POSTFIELDS, %s);', JSON.stringify(source.postData.text)) } + if (helpers.hasHeader(source.allHeaders, 'accept-encoding')) { + code.blank() + .push('curl_easy_setopt(hnd, CURLOPT_ACCEPT_ENCODING, "");') + } + code.blank() .push('CURLcode ret = curl_easy_perform(hnd);') diff --git a/src/targets/go/native.js b/src/targets/go/native.js index 9a6feee..af038d5 100644 --- a/src/targets/go/native.js +++ b/src/targets/go/native.js @@ -11,6 +11,7 @@ 'use strict' const CodeBuilder = require('../../helpers/code-builder') +const helpers = require('../../helpers/headers') module.exports = function (source, options) { // Let's Go! @@ -110,6 +111,11 @@ module.exports = function (source, options) { errorCheck() // Add headers + + // Go automatically adds this and handles decompression, as long as we don't try to + // manually add it ourselves: + delete source.allHeaders[helpers.getHeaderName(source.allHeaders, 'accept-encoding')] + if (Object.keys(source.allHeaders).length) { Object.keys(source.allHeaders).forEach(function (key) { code.push(indent, 'req.Header.Add("%s", "%qd")', key, source.allHeaders[key]) diff --git a/test/fixtures/output/c/libcurl/compression.c b/test/fixtures/output/c/libcurl/compression.c index dd0bff6..c008a40 100644 --- a/test/fixtures/output/c/libcurl/compression.c +++ b/test/fixtures/output/c/libcurl/compression.c @@ -7,4 +7,6 @@ struct curl_slist *headers = NULL; headers = curl_slist_append(headers, "accept-encoding: deflate, gzip, br"); curl_easy_setopt(hnd, CURLOPT_HTTPHEADER, headers); +curl_easy_setopt(hnd, CURLOPT_ACCEPT_ENCODING, ""); + CURLcode ret = curl_easy_perform(hnd); \ No newline at end of file diff --git a/test/fixtures/output/go/native/compression.go b/test/fixtures/output/go/native/compression.go index 11a1717..fae06af 100644 --- a/test/fixtures/output/go/native/compression.go +++ b/test/fixtures/output/go/native/compression.go @@ -12,8 +12,6 @@ func main() { req, _ := http.NewRequest("GET", url, nil) - req.Header.Add("accept-encoding", "deflate, gzip, br") - res, _ := http.DefaultClient.Do(req) defer res.Body.Close()