Remove charset of binary files

This commit is contained in:
Adrien Jalabert 2021-12-14 21:58:55 +01:00
parent 3a36aec87e
commit 961065ef3a
1 changed files with 19 additions and 4 deletions

View File

@ -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);
}
}