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

Add responseHeaders extension on XMLHttpRequest #298

Merged
merged 8 commits into from
Sep 4, 2024
Merged
Changes from 1 commit
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
27 changes: 27 additions & 0 deletions web/lib/src/helpers/extensions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,30 @@ extension TouchListConvert on TouchList {
@Deprecated('Use JSImmutableListWrapper<TouchList, Touch> directly instead.')
List<Touch> toList() => JSImmutableListWrapper<TouchList, Touch>(this);
}

extension XMLHttpRequestGlue on XMLHttpRequest {
Map<String, String> get responseHeaders {
mosuem marked this conversation as resolved.
Show resolved Hide resolved
// from Closure's goog.net.Xhrio.getResponseHeaders.
final headers = <String, String>{};
final headersString = getAllResponseHeaders();
final headersList = headersString.split('\r\n');
mosuem marked this conversation as resolved.
Show resolved Hide resolved
for (final header in headersList) {
if (header.isEmpty) {
continue;
}

final splitIdx = header.indexOf(': ');
if (splitIdx == -1) {
continue;
}
final key = header.substring(0, splitIdx).toLowerCase();
final value = header.substring(splitIdx + 2);
if (headers.containsKey(key)) {
headers[key] = '${headers[key]}, $value';
} else {
headers[key] = value;
}
}
return headers;
}
}