fix network stackoverflow #215 (#474)

Summary:
fixed issue: https://github.com/facebook/flipper/issues/215

## Changelog

1. use pako to inflate direct to unicode string to avoid 'stack size limit exceed' problem
Pull Request resolved: https://github.com/facebook/flipper/pull/474

Test Plan:
Tested with Meetup trace that crashes reproducibly without this fix:
{F165395994}

Tested gzipped body with fb4a:

{F165396053}

Reviewed By: jknoxville

Differential Revision: D16090209

Pulled By: passy

fbshipit-source-id: 64b8a61fa9d80d9e8b62105b3f1dc3289a29cc07
This commit is contained in:
henry_zheng
2019-07-04 05:12:10 -07:00
committed by Facebook Github Bot
parent 3ba71cea9c
commit 1b3ae77c40

View File

@@ -24,17 +24,17 @@ export function decodeBody(container: Request | Response): string {
}
const b64Decoded = atob(container.data);
const body =
getHeaderValue(container.headers, 'Content-Encoding') === 'gzip'
? decompress(b64Decoded)
: b64Decoded;
// Data is transferred as base64 encoded bytes to support unicode characters,
// we need to decode the bytes here to display the correct unicode characters.
try {
return decodeURIComponent(escape(body));
if (getHeaderValue(container.headers, 'Content-Encoding') === 'gzip') {
// for gzip, use pako to decompress directly to unicode string
return decompress(b64Decoded);
}
// Data is transferred as base64 encoded bytes to support unicode characters,
// we need to decode the bytes here to display the correct unicode characters.
return decodeURIComponent(escape(b64Decoded));
} catch (e) {
console.warn('Discarding malformed body:', escape(body));
console.warn('Discarding malformed body:', escape(b64Decoded));
return '';
}
}
@@ -44,20 +44,18 @@ function decompress(body: string): string {
const byteArray = new Uint8Array(charArray);
let data;
try {
if (body) {
data = pako.inflate(byteArray);
return pako.inflate(byteArray, {to: 'string'});
} else {
return body;
}
} catch (e) {
// Sometimes Content-Encoding is 'gzip' but the body is already decompressed.
// Assume this is the case when decompression fails.
return body;
}
return String.fromCharCode.apply(null, new Uint8Array(data));
return body;
}
export function convertRequestToCurlCommand(request: Request): string {