Changed header byte check to use up to 25 bytes for the check, chaned arg to be named header bytes instead of magic bytes
diff --git a/lib/src/static_handler.dart b/lib/src/static_handler.dart
index d231226..2f5cdbf 100644
--- a/lib/src/static_handler.dart
+++ b/lib/src/static_handler.dart
@@ -29,7 +29,7 @@
/// handler produces a listing of the directory.
Handler createStaticHandler(String fileSystemPath,
{bool serveFilesOutsidePath: false, String defaultDocument,
- bool listDirectories: false, bool useMagicBytesForContentType: false}) {
+ bool listDirectories: false, bool useHeaderBytesForContentType: false}) {
var rootDir = new Directory(fileSystemPath);
if (!rootDir.existsSync()) {
throw new ArgumentError('A directory corresponding to fileSystemPath '
@@ -102,8 +102,12 @@
var contentType;
- if(useMagicBytesForContentType) {
- List<int> magicBytes = await file.openRead(0,2).single;
+ if(useHeaderBytesForContentType) {
+ int length = 25; // The longest file header identifier
+ int file_length =file.lengthSync();
+ if(file_length<length)
+ length = file_length;
+ List<int> magicBytes = await file.openRead(0,length).single;
contentType = mime.lookupMimeType(file.path, headerBytes: magicBytes);
} else {
contentType = mime.lookupMimeType(file.path);
diff --git a/test/basic_file_test.dart b/test/basic_file_test.dart
index 10e1a04..3de28f1 100644
--- a/test/basic_file_test.dart
+++ b/test/basic_file_test.dart
@@ -25,7 +25,7 @@
d.file('index.html', '<html></html>').create();
d.file('root.txt', 'root txt').create();
d.file('random.unknown', 'no clue').create();
- d.binaryFile('magic_bytes_test_image', BASE64.decode(r"iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAABmJLR0QA/wD/AP+gvaeTAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAB3RJTUUH4AYRETkSXaxBzQAAAB1pVFh0Q29tbWVudAAAAAAAQ3JlYXRlZCB3aXRoIEdJTVBkLmUHAAAAbUlEQVQI1wXBvwpBYRwA0HO/kjBKJmXRLWXxJ4PsnsMTeAEPILvNZrybF7B4A6XvQW6k+DkHwqgM1TnMpoEoDMtwOJE7pB/VXmF3CdseucmjxaAruR41Pl9p/Gbyoq5B9FeL2OR7zJ+3aC/X8QdQCyIArPsHkQAAAABJRU5ErkJggg==")).create();
+ d.binaryFile('header_bytes_test_image', BASE64.decode(r"iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAABmJLR0QA/wD/AP+gvaeTAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAB3RJTUUH4AYRETkSXaxBzQAAAB1pVFh0Q29tbWVudAAAAAAAQ3JlYXRlZCB3aXRoIEdJTVBkLmUHAAAAbUlEQVQI1wXBvwpBYRwA0HO/kjBKJmXRLWXxJ4PsnsMTeAEPILvNZrybF7B4A6XvQW6k+DkHwqgM1TnMpoEoDMtwOJE7pB/VXmF3CdseucmjxaAruR41Pl9p/Gbyoq5B9FeL2OR7zJ+3aC/X8QdQCyIArPsHkQAAAABJRU5ErkJggg==")).create();
d
.dir('files', [
d.file('test.txt', 'test txt content'),
@@ -202,9 +202,9 @@
test('magic_bytes_test_image should be image/png', () {
schedule(() {
- var handler = createStaticHandler(d.defaultRoot, useMagicBytesForContentType: true);
+ var handler = createStaticHandler(d.defaultRoot, useHeaderBytesForContentType: true);
- return makeRequest(handler, '/magic_bytes_test_image').then((response) {
+ return makeRequest(handler, '/header_bytes_test_image').then((response) {
expect(response.mimeType, "image/png");
});
});