Skip to content

Commit 8a78e49

Browse files
committed
Merge branch 'fix/network-log-oom' of https://github.com/Instabug/Instabug-React-Native into fix/network-log-oom
2 parents defc7fa + 2def7fe commit 8a78e49

File tree

3 files changed

+24
-3
lines changed

3 files changed

+24
-3
lines changed

src/modules/NetworkLogger.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,24 @@ export const setEnabled = (isEnabled: boolean) => {
2727
network = await _networkDataObfuscationHandler(network);
2828
}
2929

30-
if (isContentTypeNotAllowed(network.requestContentType)) {
30+
if (network.requestBodySize > InstabugConstants.MAX_NETWORK_BODY_SIZE_IN_BYTES) {
31+
network.requestBody = InstabugConstants.MAX_REQUEST_BODY_SIZE_EXCEEDED_MESSAGE;
32+
console.warn('IBG-RN:', InstabugConstants.MAX_REQUEST_BODY_SIZE_EXCEEDED_MESSAGE);
33+
}
34+
35+
if (network.responseBodySize > InstabugConstants.MAX_NETWORK_BODY_SIZE_IN_BYTES) {
36+
network.responseBody = InstabugConstants.MAX_RESPONSE_BODY_SIZE_EXCEEDED_MESSAGE;
37+
console.warn('IBG-RN:', InstabugConstants.MAX_RESPONSE_BODY_SIZE_EXCEEDED_MESSAGE);
38+
}
39+
40+
if (network.requestBody && isContentTypeNotAllowed(network.requestContentType)) {
3141
network.requestBody = `Body is omitted because content type ${network.requestContentType} isn't supported`;
3242
console.warn(
3343
`IBG-RN: The request body for the network request with URL ${network.url} has been omitted because the content type ${network.requestContentType} isn't supported.`,
3444
);
3545
}
3646

37-
if (isContentTypeNotAllowed(network.contentType)) {
47+
if (network.responseBody && isContentTypeNotAllowed(network.contentType)) {
3848
network.responseBody = `Body is omitted because content type ${network.contentType} isn't supported`;
3949
console.warn(
4050
`IBG-RN: The response body for the network request with URL ${network.url} has been omitted because the content type ${network.contentType} isn't supported.`,

src/utils/InstabugConstants.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
enum InstabugConstants {
22
GRAPHQL_HEADER = 'ibg-graphql-header',
3+
4+
// TODO: dyanmically get the max size from the native SDK and update the error message to reflect the dynamic size.
5+
MAX_NETWORK_BODY_SIZE_IN_BYTES = 1024 * 10, // 10 KB
6+
MAX_RESPONSE_BODY_SIZE_EXCEEDED_MESSAGE = 'The response body has not been logged because it exceeds the maximum size of 10 Kb',
7+
MAX_REQUEST_BODY_SIZE_EXCEEDED_MESSAGE = 'The request body has not been logged because it exceeds the maximum size of 10 Kb',
38
}
49

510
export default InstabugConstants;

src/utils/XhrNetworkInterceptor.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import InstabugConstants from './InstabugConstants';
2+
import { stringifyIfNotString } from './InstabugUtils';
23

34
export type ProgressCallback = (totalBytesSent: number, totalBytesExpectedToSend: number) => void;
45
export type NetworkDataCallback = (data: NetworkData) => void;
@@ -80,7 +81,12 @@ export default {
8081
};
8182

8283
XMLHttpRequest.prototype.setRequestHeader = function (header, value) {
83-
network.requestHeaders[header] = typeof value === 'string' ? value : JSON.stringify(value);
84+
// According to the HTTP RFC, headers are case-insensitive, so we convert
85+
// them to lower-case to make accessing headers predictable.
86+
// This avoid issues like failing to get the Content-Type header for a request
87+
// because the header is set as 'Content-Type' instead of 'content-type'.
88+
const key = header.toLowerCase();
89+
network.requestHeaders[key] = stringifyIfNotString(value);
8490
originalXHRSetRequestHeader.apply(this, [header, value]);
8591
};
8692

0 commit comments

Comments
 (0)