Skip to content

Commit 74cbad4

Browse files
authored
Merge pull request #5 from Fr0stM0urne/gzip_decode_fix
Fix for python3 http client when accept-encoding is gzip
2 parents 57515e2 + 8432fac commit 74cbad4

File tree

42 files changed

+442
-12
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+442
-12
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
*.log
22
node_modules
33
coverage*
4+
.vscode

src/targets/c/libcurl.js

Lines changed: 8 additions & 2 deletions
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

Lines changed: 6 additions & 0 deletions
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])

src/targets/python/python3.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,25 @@
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
const code = new CodeBuilder()
18+
1719
// Start Request
1820
code.push('import http.client')
1921

2022
if (options.insecureSkipVerify) {
2123
code.push('import ssl')
2224
}
2325

26+
const mayBeGzipped = helpers.hasHeader(source.allHeaders, 'accept-encoding') &&
27+
helpers.getHeader(source.allHeaders, 'accept-encoding').includes('gzip')
28+
29+
if (mayBeGzipped) {
30+
code.push('import gzip')
31+
}
32+
2433
code.blank()
2534

2635
// Check which protocol to be used for the client connection
@@ -90,7 +99,16 @@ module.exports = function (source, options) {
9099
.push('res = conn.getresponse()')
91100
.push('data = res.read()')
92101
.blank()
93-
.push('print(data.decode("utf-8"))')
102+
103+
// Decode response
104+
if (mayBeGzipped) {
105+
code.push("if res.headers['content-encoding'] == 'gzip':")
106+
code.push(' print(gzip.decompress(data).decode("utf-8"))')
107+
code.push('else:')
108+
code.push(' print(data.decode("utf-8"))')
109+
} else {
110+
code.push('print(data.decode("utf-8"))')
111+
}
94112

95113
return code.join()
96114
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
CURL *hnd = curl_easy_init();
2+
3+
curl_easy_setopt(hnd, CURLOPT_CUSTOMREQUEST, "GET");
4+
curl_easy_setopt(hnd, CURLOPT_URL, "http://mockbin.com/har");
5+
6+
struct curl_slist *headers = NULL;
7+
headers = curl_slist_append(headers, "accept-encoding: deflate, gzip, br");
8+
curl_easy_setopt(hnd, CURLOPT_HTTPHEADER, headers);
9+
10+
curl_easy_setopt(hnd, CURLOPT_ACCEPT_ENCODING, "");
11+
12+
CURLcode ret = curl_easy_perform(hnd);
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
(require '[clj-http.client :as client])
2+
3+
(client/get "http://mockbin.com/har" {:headers {:accept-encoding "deflate, gzip, br"}})
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
var clientHandler = new HttpClientHandler
2+
{
3+
AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip,
4+
};
5+
var client = new HttpClient(clientHandler);
6+
var request = new HttpRequestMessage
7+
{
8+
Method = HttpMethod.Get,
9+
RequestUri = new Uri("http://mockbin.com/har"),
10+
};
11+
using (var response = await client.SendAsync(request))
12+
{
13+
response.EnsureSuccessStatusCode();
14+
var body = await response.Content.ReadAsStringAsync();
15+
Console.WriteLine(body);
16+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
var client = new RestClient("http://mockbin.com/har");
2+
var request = new RestRequest(Method.GET);
3+
request.AddHeader("accept-encoding", "deflate, gzip, br");
4+
IRestResponse response = client.Execute(request);
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"net/http"
6+
"io"
7+
)
8+
9+
func main() {
10+
11+
url := "http://mockbin.com/har"
12+
13+
req, _ := http.NewRequest("GET", url, nil)
14+
15+
res, _ := http.DefaultClient.Do(req)
16+
17+
defer res.Body.Close()
18+
body, _ := io.ReadAll(res.Body)
19+
20+
fmt.Println(res)
21+
fmt.Println(string(body))
22+
23+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
GET /har HTTP/1.1
2+
Accept-Encoding: deflate, gzip, br
3+
Host: mockbin.com

0 commit comments

Comments
 (0)