Skip to content

Commit

Permalink
Add libcurl & Go compression handling too
Browse files Browse the repository at this point in the history
  • Loading branch information
pimterry committed Apr 8, 2024
1 parent d63c0c1 commit 8432fac
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 4 deletions.
10 changes: 8 additions & 2 deletions src/targets/c/libcurl.js
Original file line number Diff line number Diff line change
@@ -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()
Expand All @@ -26,16 +27,21 @@ 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) {
code.blank()
.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);')

Expand Down
6 changes: 6 additions & 0 deletions src/targets/go/native.js
Original file line number Diff line number Diff line change
Expand Up @@ -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!
Expand Down Expand Up @@ -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])
Expand Down
2 changes: 2 additions & 0 deletions test/fixtures/output/c/libcurl/compression.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
2 changes: 0 additions & 2 deletions test/fixtures/output/go/native/compression.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 8432fac

Please sign in to comment.