Skip to content

Commit d63c0c1

Browse files
Fr0stM0urnepimterry
authored andcommitted
Added handling of gzip encoding for python3 http client.
1 parent 7231dc8 commit d63c0c1

File tree

40 files changed

+428
-10
lines changed

40 files changed

+428
-10
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/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: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
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+
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: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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+
req.Header.Add("accept-encoding", "deflate, gzip, br")
16+
17+
res, _ := http.DefaultClient.Do(req)
18+
19+
defer res.Body.Close()
20+
body, _ := io.ReadAll(res.Body)
21+
22+
fmt.Println(res)
23+
fmt.Println(string(body))
24+
25+
}
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
Lines changed: 9 additions & 0 deletions
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();
Lines changed: 7 additions & 0 deletions
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());

0 commit comments

Comments
 (0)