From 3a36aec87e9e618ad1ff78a0e45646c1402a1f4a Mon Sep 17 00:00:00 2001 From: Adrien Jalabert Date: Mon, 13 Dec 2021 17:56:34 +0100 Subject: [PATCH 1/2] Add wasm mime type --- lib/src/mime_type_resolver.dart | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/src/mime_type_resolver.dart b/lib/src/mime_type_resolver.dart index 00732168..a01fe4b1 100644 --- a/lib/src/mime_type_resolver.dart +++ b/lib/src/mime_type_resolver.dart @@ -580,6 +580,7 @@ class MimeTypeResolver { "zirz": "application/vnd.zul", "zaz": "application/vnd.zzazz.deck+xml", "vxml": "application/voicexml+xml", + "wasm": "application/wasm", "wgt": "application/widget", "hlp": "application/winhlp", "wsdl": "application/wsdl+xml", From 961065ef3a304a0a3770c144ccb953149df4c0b5 Mon Sep 17 00:00:00 2001 From: Adrien Jalabert Date: Tue, 14 Dec 2021 21:58:55 +0100 Subject: [PATCH 2/2] Remove charset of binary files --- lib/src/in_app_localhost_server.dart | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/lib/src/in_app_localhost_server.dart b/lib/src/in_app_localhost_server.dart index 7bde49e6..2c7d2c5d 100755 --- a/lib/src/in_app_localhost_server.dart +++ b/lib/src/in_app_localhost_server.dart @@ -56,17 +56,16 @@ class InAppLocalhostServer { return; } - var contentType = ['text', 'html']; + var contentType = ContentType('text', 'html', charset: 'utf-8'); if (!request.requestedUri.path.endsWith('/') && request.requestedUri.pathSegments.isNotEmpty) { var mimeType = MimeTypeResolver.lookup(request.requestedUri.path); if (mimeType != null) { - contentType = mimeType.split('/'); + contentType = _getContentTypeFromMimeType(mimeType); } } - request.response.headers.contentType = - ContentType(contentType[0], contentType[1], charset: 'utf-8'); + request.response.headers.contentType = contentType; request.response.add(body); request.response.close(); }); @@ -93,4 +92,20 @@ class InAppLocalhostServer { bool isRunning() { return this._server != null; } + + ContentType _getContentTypeFromMimeType(String mimeType) { + final contentType = mimeType.split('/'); + String? charset; + + if (_isTextFile(mimeType)) { + charset = 'utf-8'; + } + + return ContentType(contentType[0], contentType[1], charset: charset); + } + + bool _isTextFile(String mimeType) { + final textFile = RegExp(r'^text\/|^application\/(javascript|json)'); + return textFile.hasMatch(mimeType); + } }