Skip to content

Commit 8432fac

Browse files
committed
Add libcurl & Go compression handling too
1 parent d63c0c1 commit 8432fac

File tree

4 files changed

+16
-4
lines changed

4 files changed

+16
-4
lines changed

src/targets/c/libcurl.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict'
22

33
const CodeBuilder = require('../../helpers/code-builder')
4+
const helpers = require('../../helpers/headers')
45

56
module.exports = function (source, options) {
67
const code = new CodeBuilder()
@@ -26,16 +27,21 @@ module.exports = function (source, options) {
2627
}
2728

2829
// construct cookies
29-
if (source.allHeaders.cookie) {
30+
if (helpers.hasHeader(source.allHeaders, 'cookie')) {
3031
code.blank()
31-
.push('curl_easy_setopt(hnd, CURLOPT_COOKIE, "%s");', source.allHeaders.cookie)
32+
.push('curl_easy_setopt(hnd, CURLOPT_COOKIE, "%s");', helpers.getHeader(source.allHeaders, 'cookie'))
3233
}
3334

3435
if (source.postData.text) {
3536
code.blank()
3637
.push('curl_easy_setopt(hnd, CURLOPT_POSTFIELDS, %s);', JSON.stringify(source.postData.text))
3738
}
3839

40+
if (helpers.hasHeader(source.allHeaders, 'accept-encoding')) {
41+
code.blank()
42+
.push('curl_easy_setopt(hnd, CURLOPT_ACCEPT_ENCODING, "");')
43+
}
44+
3945
code.blank()
4046
.push('CURLcode ret = curl_easy_perform(hnd);')
4147

src/targets/go/native.js

+6
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
'use strict'
1212

1313
const CodeBuilder = require('../../helpers/code-builder')
14+
const helpers = require('../../helpers/headers')
1415

1516
module.exports = function (source, options) {
1617
// Let's Go!
@@ -110,6 +111,11 @@ module.exports = function (source, options) {
110111
errorCheck()
111112

112113
// Add headers
114+
115+
// Go automatically adds this and handles decompression, as long as we don't try to
116+
// manually add it ourselves:
117+
delete source.allHeaders[helpers.getHeaderName(source.allHeaders, 'accept-encoding')]
118+
113119
if (Object.keys(source.allHeaders).length) {
114120
Object.keys(source.allHeaders).forEach(function (key) {
115121
code.push(indent, 'req.Header.Add("%s", "%qd")', key, source.allHeaders[key])

test/fixtures/output/c/libcurl/compression.c

+2
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,6 @@ struct curl_slist *headers = NULL;
77
headers = curl_slist_append(headers, "accept-encoding: deflate, gzip, br");
88
curl_easy_setopt(hnd, CURLOPT_HTTPHEADER, headers);
99

10+
curl_easy_setopt(hnd, CURLOPT_ACCEPT_ENCODING, "");
11+
1012
CURLcode ret = curl_easy_perform(hnd);

test/fixtures/output/go/native/compression.go

-2
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ func main() {
1212

1313
req, _ := http.NewRequest("GET", url, nil)
1414

15-
req.Header.Add("accept-encoding", "deflate, gzip, br")
16-
1715
res, _ := http.DefaultClient.Do(req)
1816

1917
defer res.Body.Close()

0 commit comments

Comments
 (0)