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

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

+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,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);
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,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+
}
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 *)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
$curl = curl_init();
4+
5+
curl_setopt_array($curl, [
6+
CURLOPT_URL => "http://mockbin.com/har",
7+
CURLOPT_RETURNTRANSFER => true,
8+
CURLOPT_ENCODING => "",
9+
CURLOPT_MAXREDIRS => 10,
10+
CURLOPT_TIMEOUT => 30,
11+
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
12+
CURLOPT_CUSTOMREQUEST => "GET",
13+
CURLOPT_HTTPHEADER => [
14+
"accept-encoding: deflate, gzip, br"
15+
],
16+
]);
17+
18+
$response = curl_exec($curl);
19+
$err = curl_error($curl);
20+
21+
curl_close($curl);
22+
23+
if ($err) {
24+
echo "cURL Error #:" . $err;
25+
} else {
26+
echo $response;
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
$request = new HttpRequest();
4+
$request->setUrl('http://mockbin.com/har');
5+
$request->setMethod(HTTP_METH_GET);
6+
7+
$request->setHeaders([
8+
'accept-encoding' => 'deflate, gzip, br'
9+
]);
10+
11+
try {
12+
$response = $request->send();
13+
14+
echo $response->getBody();
15+
} catch (HttpException $ex) {
16+
echo $ex;
17+
}

0 commit comments

Comments
 (0)