Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Render no-standard content-encoding response [INS-4910] #8341

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 20 additions & 4 deletions packages/insomnia/src/main/network/libcurl-promise.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ export interface CurlRequestOptions {
caCertficatePath: string | null;
socketPath?: string;
authHeader?: { name: string; value: string };
// make libcurl not decompress the response content
noDecompress?: boolean;
}
interface RequestUsedHere {
headers: any;
Expand Down Expand Up @@ -102,7 +104,7 @@ export const curlRequest = (options: CurlRequestOptions) => new Promise<CurlRequ
await fs.promises.mkdir(responsesDir, { recursive: true });
const responseBodyPath = path.join(responsesDir, uuidv4() + '.response');

const { requestId, req, finalUrl, settings, certificates, caCertficatePath, socketPath, authHeader } = options;
const { requestId, req, finalUrl, settings, certificates, caCertficatePath, socketPath, authHeader, noDecompress = false } = options;
const caCert = (caCertficatePath && (await fs.promises.readFile(caCertficatePath)).toString());

const { curl, debugTimeline } = createConfiguredCurlInstance({
Expand All @@ -112,6 +114,7 @@ export const curlRequest = (options: CurlRequestOptions) => new Promise<CurlRequ
caCert,
certificates,
socketPath,
noDecompress,
});
const { method, body } = req;
// Only set CURLOPT_CUSTOMREQUEST if not HEAD or GET.
Expand Down Expand Up @@ -225,6 +228,12 @@ export const curlRequest = (options: CurlRequestOptions) => new Promise<CurlRequ
curl.isOpen && curl.close();
await waitForStreamToFinish(responseBodyWriteStream);

// If libcurl can't decompress the response, retry without decompression
if (code === CurlCode.CURLE_BAD_CONTENT_ENCODING) {
resolve(curlRequest({ ...options, noDecompress: true }));
return;
}

let error = err + '';
let statusMessage = 'Error';

Expand Down Expand Up @@ -260,22 +269,29 @@ export const createConfiguredCurlInstance = ({
caCert,
certificates,
socketPath,
noDecompress = false,
}: {
req: RequestUsedHere;
finalUrl: string;
settings: SettingsUsedHere;
certificates: ClientCertificate[];
caCert: string | null;
socketPath?: string;
noDecompress?: boolean;
}) => {
const debugTimeline: ResponseTimelineEntry[] = [];
const curl = new Curl();
curl.setOpt(Curl.option.URL, finalUrl);
socketPath && curl.setOpt(Curl.option.UNIX_SOCKET_PATH, socketPath);

curl.setOpt(Curl.option.VERBOSE, true); // Set all the basic options
curl.setOpt(Curl.option.NOPROGRESS, true); // True so debug function works
curl.setOpt(Curl.option.ACCEPT_ENCODING, ''); // True so curl doesn't print progress
// Set all the basic options

// True so debug function works
curl.setOpt(Curl.option.VERBOSE, true);
// True so curl doesn't print progress
curl.setOpt(Curl.option.NOPROGRESS, true);
// whether to decompress response content
curl.setOpt(Curl.option.ACCEPT_ENCODING, noDecompress ? null : '');
// fallback to root certificates or leave unset to use keychain on macOS
if (caCert) {
curl.setOpt(Curl.option.CAINFO_BLOB, caCert);
Expand Down
Loading