fix: main request service for Electron.net requests (#167075)

This commit is contained in:
Robo 2022-11-24 17:48:48 +09:00 committed by GitHub
parent 0306f1e019
commit 0f8089f424
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 2 deletions

View file

@ -15,6 +15,6 @@ function getRawRequest(options: IRequestOptions): IRawRequestFunction {
export class RequestMainService extends NodeRequestService {
override request(options: IRequestOptions, token: CancellationToken): Promise<IRequestContext> {
return super.request({ ...(options || {}), getRawRequest }, token);
return super.request({ ...(options || {}), getRawRequest, isChromiumNetwork: true }, token);
}
}

View file

@ -29,6 +29,7 @@ export interface IRawRequestFunction {
export interface NodeRequestOptions extends IRequestOptions {
agent?: Agent;
strictSSL?: boolean;
isChromiumNetwork?: boolean;
getRawRequest?(options: IRequestOptions): IRawRequestFunction;
}
@ -146,7 +147,12 @@ export class RequestService extends Disposable implements IRequestService {
} else {
let stream: streams.ReadableStreamEvents<Uint8Array> = res;
if (res.headers['content-encoding'] === 'gzip') {
// Responses from Electron net module should be treated as response
// from browser, which will apply gzip filter and decompress the response
// using zlib before passing the result to us. Following step can be bypassed
// in this case and proceed further.
// Refs https://source.chromium.org/chromium/chromium/src/+/main:net/url_request/url_request_http_job.cc;l=1266-1318
if (!options.isChromiumNetwork && res.headers['content-encoding'] === 'gzip') {
stream = res.pipe(createGunzip());
}
@ -160,6 +166,13 @@ export class RequestService extends Disposable implements IRequestService {
req.setTimeout(options.timeout);
}
// Chromium will abort the request if forbidden headers are set.
// Ref https://source.chromium.org/chromium/chromium/src/+/main:services/network/public/cpp/header_util.cc;l=14-48;
// for additional context.
if (options.isChromiumNetwork) {
req.removeHeader('Content-Length');
}
if (options.data) {
if (typeof options.data === 'string') {
req.write(options.data);