From 3b757a64ce115e36e23e075b1ae8d4e1eee211f4 Mon Sep 17 00:00:00 2001 From: thosakwe Date: Mon, 10 Jul 2017 18:31:17 -0400 Subject: [PATCH] 1.2.4 --- CHANGELOG.md | 5 +++ lib/src/cache.dart | 2 +- lib/src/file_info.dart | 4 +-- lib/src/virtual_directory.dart | 57 ++++++++++++++-------------------- pubspec.yaml | 2 +- 5 files changed, 33 insertions(+), 37 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index dc6ee309..34e3b15a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,8 @@ +# 1.2.4 +Fixes https://github.com/angel-dart/angel/issues/44. +* MIME types will now default to `application/octet-stream`. +* When `streamToIO` is `true`, the body will only be sent gzipped if the request explicitly allows it. + # 1.2.3 Fixed #40 and #41, which dealt with paths being improperly served when using a `publicPath`. \ No newline at end of file diff --git a/lib/src/cache.dart b/lib/src/cache.dart index 599ebf0f..8be15fbc 100644 --- a/lib/src/cache.dart +++ b/lib/src/cache.dart @@ -154,7 +154,7 @@ class CachingVirtualDirectory extends VirtualDirectory { generateEtag(buf, weak: useWeakEtags != false, hash: hash); res.headers ..[HttpHeaders.ETAG] = etag - ..[HttpHeaders.CONTENT_TYPE] = lookupMimeType(file.path); + ..[HttpHeaders.CONTENT_TYPE] = lookupMimeType(file.path) ?? 'application/octet-stream'; setCachedHeaders(stat.modified, req, res); if (useWeakEtags == false) { diff --git a/lib/src/file_info.dart b/lib/src/file_info.dart index 095bd198..9405907c 100644 --- a/lib/src/file_info.dart +++ b/lib/src/file_info.dart @@ -26,13 +26,13 @@ abstract class FileInfo { factory FileInfo.fromFile(File file) => new _FileInfoImpl( () => file.openRead(), file.absolute.path, - lookupMimeType(file.path) ?? 'application/octet-stream', + lookupMimeType(file.path) ?? 'application/octet-stream' ?? 'application/octet-stream', file.statSync().modified); /// Creates a [FileInfo] describing a file that might not even exists to begin with. factory FileInfo.hypothetical(String hypotheticalFileName) => new _FileInfoImpl(null, hypotheticalFileName, - lookupMimeType(hypotheticalFileName), null); + lookupMimeType(hypotheticalFileName) ?? 'application/octet-stream', null); /// Returns an identical instance, but with a different filename. FileInfo changeFilename(String newFilename); diff --git a/lib/src/virtual_directory.dart b/lib/src/virtual_directory.dart index 94c08a92..2a524720 100644 --- a/lib/src/virtual_directory.dart +++ b/lib/src/virtual_directory.dart @@ -235,31 +235,9 @@ class VirtualDirectory implements AngelPlugin { return true; } - Future serveFileOld( - File file, FileStat stat, RequestContext req, ResponseContext res) async { - // _printDebug('Sending file ${file.absolute.path}...'); - // _printDebug('MIME type for ${file.path}: ${lookupMimeType(file.path)}'); - res.statusCode = 200; - - if (callback != null) { - var r = callback(file, req, res); - r = r is Future ? await r : r; - if (r != null && r != true) return r; - } - - res.headers[HttpHeaders.CONTENT_TYPE] = lookupMimeType(file.path); - - if (streamToIO == true) { - res - ..io.headers.set(HttpHeaders.CONTENT_TYPE, lookupMimeType(file.path)) - ..io.headers.set(HttpHeaders.CONTENT_ENCODING, 'gzip') - ..end() - ..willCloseItself = true; - - await file.openRead().transform(GZIP.encoder).pipe(res.io); - } else - await res.sendFile(file); - return false; + bool _acceptsGzip(RequestContext req) { + var h = req.headers.value(HttpHeaders.ACCEPT)?.toLowerCase(); + return h?.contains('gzip') == true; } void _ensureContentTypeAllowed(String mimeType, RequestContext req) { @@ -279,7 +257,7 @@ class VirtualDirectory implements AngelPlugin { Future serveFile( File file, FileStat stat, RequestContext req, ResponseContext res) async { // _printDebug('Sending file ${file.absolute.path}...'); - // _printDebug('MIME type for ${file.path}: ${lookupMimeType(file.path)}'); + // _printDebug('MIME type for ${file.path}: ${lookupMimeType(file.path) ?? 'application/octet-stream'}'); res.statusCode = 200; if (callback != null) { @@ -288,18 +266,24 @@ class VirtualDirectory implements AngelPlugin { if (r != null && r != true) return r; } - var type = lookupMimeType(file.path); + var type = lookupMimeType(file.path) ?? 'application/octet-stream'; _ensureContentTypeAllowed(type, req); res.headers[HttpHeaders.CONTENT_TYPE] = type; if (streamToIO == true) { res - ..io.headers.set(HttpHeaders.CONTENT_TYPE, lookupMimeType(file.path)) - ..io.headers.set(HttpHeaders.CONTENT_ENCODING, 'gzip') + ..io.headers.set(HttpHeaders.CONTENT_TYPE, + lookupMimeType(file.path) ?? 'application/octet-stream') ..end() ..willCloseItself = true; - await file.openRead().transform(GZIP.encoder).pipe(res.io); + if (_acceptsGzip(req)) + res.io.headers.set(HttpHeaders.CONTENT_ENCODING, 'gzip'); + + Stream> stream = _acceptsGzip(req) + ? file.openRead().transform(GZIP.encoder) + : file.openRead(); + await stream.pipe(res.io); } else await res.sendFile(file); return false; @@ -316,11 +300,18 @@ class VirtualDirectory implements AngelPlugin { if (streamToIO == true) { res ..statusCode = 200 - ..io.headers.set(HttpHeaders.CONTENT_TYPE, file.mimeType) - ..io.headers.set(HttpHeaders.CONTENT_ENCODING, 'gzip') + ..io.headers.set(HttpHeaders.CONTENT_TYPE, + lookupMimeType(file.filename) ?? 'application/octet-stream') ..end() ..willCloseItself = true; - await file.content.transform(GZIP.encoder).pipe(res.io); + + if (_acceptsGzip(req)) + res.io.headers.set(HttpHeaders.CONTENT_ENCODING, 'gzip'); + + Stream> stream = _acceptsGzip(req) + ? file.content.transform(GZIP.encoder) + : file.content; + await stream.pipe(res.io); } else { await file.content.forEach(res.buffer.add); } diff --git a/pubspec.yaml b/pubspec.yaml index e6e1fed1..b125d051 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -4,7 +4,7 @@ environment: sdk: ">=1.19.0" homepage: https://github.com/angel-dart/static author: Tobe O -version: 1.2.3 +version: 1.2.4 dependencies: angel_framework: ^1.0.0-dev cli_util: ^0.1.1