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

+1
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

+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])

src/targets/python/python3.js

+19-1
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
}
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);
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"}})
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+
}
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);
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+
}
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
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
AsyncHttpClient client = new DefaultAsyncHttpClient();
2+
client.prepare("GET", "http://mockbin.com/har")
3+
.setHeader("accept-encoding", "deflate, gzip, br")
4+
.execute()
5+
.toCompletableFuture()
6+
.thenAccept(System.out::println)
7+
.join();
8+
9+
client.close();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
HttpRequest request = HttpRequest.newBuilder()
2+
.uri(URI.create("http://mockbin.com/har"))
3+
.header("accept-encoding", "deflate, gzip, br")
4+
.method("GET", HttpRequest.BodyPublishers.noBody())
5+
.build();
6+
HttpResponse<String> response = HttpClient.newHttpClient().send(request, HttpResponse.BodyHandlers.ofString());
7+
System.out.println(response.body());
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
OkHttpClient client = new OkHttpClient();
2+
3+
Request request = new Request.Builder()
4+
.url("http://mockbin.com/har")
5+
.get()
6+
.addHeader("accept-encoding", "deflate, gzip, br")
7+
.build();
8+
9+
Response response = client.newCall(request).execute();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
HttpResponse<String> response = Unirest.get("http://mockbin.com/har")
2+
.header("accept-encoding", "deflate, gzip, br")
3+
.asString();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import axios from "axios";
2+
3+
const options = {
4+
method: 'GET',
5+
url: 'http://mockbin.com/har',
6+
headers: {'accept-encoding': 'deflate, gzip, br'}
7+
};
8+
9+
axios.request(options).then(function (response) {
10+
console.log(response.data);
11+
}).catch(function (error) {
12+
console.error(error);
13+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
const options = {method: 'GET', headers: {'accept-encoding': 'deflate, gzip, br'}};
2+
3+
fetch('http://mockbin.com/har', options)
4+
.then(response => response.json())
5+
.then(response => console.log(response))
6+
.catch(err => console.error(err));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
const settings = {
2+
"async": true,
3+
"crossDomain": true,
4+
"url": "http://mockbin.com/har",
5+
"method": "GET",
6+
"headers": {
7+
"accept-encoding": "deflate, gzip, br"
8+
}
9+
};
10+
11+
$.ajax(settings).done(function (response) {
12+
console.log(response);
13+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
const data = null;
2+
3+
const xhr = new XMLHttpRequest();
4+
xhr.withCredentials = true;
5+
6+
xhr.addEventListener("readystatechange", function () {
7+
if (this.readyState === this.DONE) {
8+
console.log(this.responseText);
9+
}
10+
});
11+
12+
xhr.open("GET", "http://mockbin.com/har");
13+
xhr.setRequestHeader("accept-encoding", "deflate, gzip, br");
14+
15+
xhr.send(data);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
val client = OkHttpClient()
2+
3+
val request = Request.Builder()
4+
.url("http://mockbin.com/har")
5+
.get()
6+
.addHeader("accept-encoding", "deflate, gzip, br")
7+
.build()
8+
9+
val response = client.newCall(request).execute()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
var axios = require("axios").default;
2+
3+
var options = {
4+
method: 'GET',
5+
url: 'http://mockbin.com/har',
6+
headers: {'accept-encoding': 'deflate, gzip, br'}
7+
};
8+
9+
axios.request(options).then(function (response) {
10+
console.log(response.data);
11+
}).catch(function (error) {
12+
console.error(error);
13+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
const fetch = require('node-fetch');
2+
3+
let url = 'http://mockbin.com/har';
4+
5+
let options = {method: 'GET', headers: {'accept-encoding': 'deflate, gzip, br'}};
6+
7+
fetch(url, options)
8+
.then(res => res.json())
9+
.then(json => console.log(json))
10+
.catch(err => console.error('error:' + err));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
const http = require("http");
2+
3+
const options = {
4+
"method": "GET",
5+
"hostname": "mockbin.com",
6+
"port": null,
7+
"path": "/har",
8+
"headers": {
9+
"accept-encoding": "deflate, gzip, br"
10+
}
11+
};
12+
13+
const req = http.request(options, function (res) {
14+
const chunks = [];
15+
16+
res.on("data", function (chunk) {
17+
chunks.push(chunk);
18+
});
19+
20+
res.on("end", function () {
21+
const body = Buffer.concat(chunks);
22+
console.log(body.toString());
23+
});
24+
});
25+
26+
req.end();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
const request = require('request');
2+
3+
const options = {
4+
method: 'GET',
5+
url: 'http://mockbin.com/har',
6+
headers: {'accept-encoding': 'deflate, gzip, br'}
7+
};
8+
9+
request(options, function (error, response, body) {
10+
if (error) throw new Error(error);
11+
12+
console.log(body);
13+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
const unirest = require("unirest");
2+
3+
const req = unirest("GET", "http://mockbin.com/har");
4+
5+
req.headers({
6+
"accept-encoding": "deflate, gzip, br"
7+
});
8+
9+
req.end(function (res) {
10+
if (res.error) throw new Error(res.error);
11+
12+
console.log(res.body);
13+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#import <Foundation/Foundation.h>
2+
3+
NSDictionary *headers = @{ @"accept-encoding": @"deflate, gzip, br" };
4+
5+
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://mockbin.com/har"]
6+
cachePolicy:NSURLRequestUseProtocolCachePolicy
7+
timeoutInterval:10.0];
8+
[request setHTTPMethod:@"GET"];
9+
[request setAllHTTPHeaderFields:headers];
10+
11+
NSURLSession *session = [NSURLSession sharedSession];
12+
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request
13+
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
14+
if (error) {
15+
NSLog(@"%@", error);
16+
} else {
17+
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;
18+
NSLog(@"%@", httpResponse);
19+
}
20+
}];
21+
[dataTask resume];
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
open Cohttp_lwt_unix
2+
open Cohttp
3+
open Lwt
4+
5+
let uri = Uri.of_string "http://mockbin.com/har" in
6+
let headers = Header.add (Header.init ()) "accept-encoding" "deflate, gzip, br" in
7+
8+
Client.call ~headers `GET uri
9+
>>= fun (res, body_stream) ->
10+
(* Do stuff with the result *)

0 commit comments

Comments
 (0)