Unverified Commit 2a4e573d authored by Sergey Fedotov's avatar Sergey Fedotov Committed by GitHub

Serve assets with correct content-type header value (#94357)

parent c1872c32
......@@ -455,14 +455,11 @@ class WebAssetServer implements AssetReader {
// Attempt to determine the file's mime type. if this is not provided some
// browsers will refuse to render images/show video etc. If the tool
// cannot determine a mime type, fall back to application/octet-stream.
String mimeType;
if (length >= 12) {
mimeType = mime.lookupMimeType(
final String mimeType = mime.lookupMimeType(
file.path,
headerBytes: await file.openRead(0, 12).first,
);
}
mimeType ??= _kDefaultMimeType;
headerBytes: await file.openRead(0, mime.defaultMagicNumbersMaxLength).first,
) ?? _kDefaultMimeType;
headers[HttpHeaders.contentLengthHeader] = length.toString();
headers[HttpHeaders.contentTypeHeader] = mimeType;
headers[HttpHeaders.etagHeader] = etag;
......
......@@ -539,31 +539,30 @@ void main() {
expect((await response.read().toList()).first, source.readAsBytesSync());
}));
test('serves asset files files from in filesystem with unknown mime type and length > 12', () => testbed.run(() async {
final File source = globals.fs.file(globals.fs.path.join('build', 'flutter_assets', 'foo'))
..createSync(recursive: true)
..writeAsBytesSync(List<int>.filled(100, 0));
test('serves asset files from in filesystem with known mime type and empty content', () => testbed.run(() async {
final File source = globals.fs.file(globals.fs.path.join('web', 'foo.js'))
..createSync(recursive: true);
final Response response = await webAssetServer
.handleRequest(Request('GET', Uri.parse('http://foobar/assets/foo')));
.handleRequest(Request('GET', Uri.parse('http://foobar/foo.js')));
expect(response.headers, allOf(<Matcher>[
containsPair(HttpHeaders.contentLengthHeader, '100'),
containsPair(HttpHeaders.contentTypeHeader, 'application/octet-stream'),
containsPair(HttpHeaders.contentLengthHeader, '0'),
containsPair(HttpHeaders.contentTypeHeader, 'application/javascript'),
]));
expect((await response.read().toList()).first, source.readAsBytesSync());
}));
test('serves asset files files from in filesystem with unknown mime type and length < 12', () => testbed.run(() async {
test('serves asset files files from in filesystem with unknown mime type', () => testbed.run(() async {
final File source = globals.fs.file(globals.fs.path.join('build', 'flutter_assets', 'foo'))
..createSync(recursive: true)
..writeAsBytesSync(<int>[1, 2, 3]);
..writeAsBytesSync(List<int>.filled(100, 0));
final Response response = await webAssetServer
.handleRequest(Request('GET', Uri.parse('http://foobar/assets/foo')));
expect(response.headers, allOf(<Matcher>[
containsPair(HttpHeaders.contentLengthHeader, '3'),
containsPair(HttpHeaders.contentLengthHeader, '100'),
containsPair(HttpHeaders.contentTypeHeader, 'application/octet-stream'),
]));
expect((await response.read().toList()).first, source.readAsBytesSync());
......@@ -594,7 +593,7 @@ void main() {
expect(response.headers, allOf(<Matcher>[
containsPair(HttpHeaders.contentLengthHeader, '3'),
containsPair(HttpHeaders.contentTypeHeader, 'application/octet-stream'),
containsPair(HttpHeaders.contentTypeHeader, 'text/x-dart'),
]));
expect((await response.read().toList()).first, source.readAsBytesSync());
}));
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment