Skip to content

added mime checks, minor logic improvements in recording #9134

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

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,9 @@ class HarDataEntry {
};
}).toList();

final isBinary = !isTextMimeType(e.type);
final responseBodyBytes = e.encodedResponse;

return <String, Object?>{
NetworkEventKeys.startedDateTime.name:
e.startTimestamp.toUtc().toIso8601String(),
Expand Down Expand Up @@ -155,8 +158,14 @@ class HarDataEntry {
NetworkEventKeys.content.name: <String, Object?>{
NetworkEventKeys.size.name: e.responseBody?.length,
NetworkEventKeys.mimeType.name: e.type,
NetworkEventKeys.text.name: e.responseBody,
if (responseBodyBytes != null && isBinary) ...{
NetworkEventKeys.text.name: base64.encode(responseBodyBytes),
'encoding': 'base64',
} else if (e.responseBody != null) ...{
NetworkEventKeys.text.name: e.responseBody,
},
},

NetworkEventKeys.redirectURL.name: '',
NetworkEventKeys.headersSize.name: calculateHeadersSize(
e.responseHeaders,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,8 @@ class NetworkController extends DevToolsScreenController
debugPrint('No valid request data to export');
return '';
}

// Build the HAR object
try {
// Build the HAR object
final har = HarNetworkData(_httpRequests!);
return ExportController().downloadFile(
json.encode(har.toJson()),
Expand Down Expand Up @@ -205,8 +204,11 @@ class NetworkController extends DevToolsScreenController
shouldLoad: (data) => !data.isEmpty,
loadData: (data) => loadOfflineData(data),
);
}
if (serviceConnection.serviceManager.connectedState.value.connected) {
} else if (serviceConnection
.serviceManager
.connectedState
.value
.connected) {
await startRecording();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,27 @@ class DartIOHttpRequestData extends NetworkRequest {
);
_request = updated;
final fullRequest = _request as HttpProfileRequest;
_responseBody = utf8.decode(fullRequest.responseBody!);
_requestBody = utf8.decode(fullRequest.requestBody!);
final responseMime =
responseHeaders?['content-type']?.toString().split(';').first;
final requestMime =
requestHeaders?['content-type']?.toString().split(';').first;

if (fullRequest.responseBody != null) {
if (isTextMimeType(responseMime)) {
_responseBody = utf8.decode(fullRequest.responseBody!);
} else {
_responseBody = base64.encode(fullRequest.responseBody!);
}
}

if (fullRequest.requestBody != null) {
if (isTextMimeType(requestMime)) {
_requestBody = utf8.decode(fullRequest.requestBody!);
} else {
_requestBody = base64.encode(fullRequest.requestBody!);
}
}

notifyListeners();
}
} finally {
Expand Down
15 changes: 15 additions & 0 deletions packages/devtools_app/lib/src/shared/primitives/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1126,3 +1126,18 @@ String devtoolsAssetsBasePath({required String origin, required String path}) {
pathParts.removeLast();
return '$trimmedOrigin${pathParts.join(separator)}';
}

/// Returns `true` if the given [mimeType] is considered textual and can be
/// safely decoded as UTF-8 without base64 encoding.
///
/// This function is useful for determining whether the content of an HTTP
/// request or response can be directly included in a HAR or JSON file as
/// human-readable text.
bool isTextMimeType(String? mimeType) {
if (mimeType == null) return false;
return mimeType.startsWith('text/') ||
mimeType == 'application/json' ||
mimeType == 'application/javascript' ||
mimeType == 'application/xml' ||
mimeType == 'application/x-www-form-urlencoded';
}