This commit is contained in:
thosakwe 2017-07-10 18:31:17 -04:00
parent a55f97c871
commit 3b757a64ce
5 changed files with 33 additions and 37 deletions

View file

@ -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`.

View file

@ -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) {

View file

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

View file

@ -235,31 +235,9 @@ class VirtualDirectory implements AngelPlugin {
return true;
}
Future<bool> 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<bool> 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<List<int>> 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<List<int>> stream = _acceptsGzip(req)
? file.content.transform(GZIP.encoder)
: file.content;
await stream.pipe(res.io);
} else {
await file.content.forEach(res.buffer.add);
}

View file

@ -4,7 +4,7 @@ environment:
sdk: ">=1.19.0"
homepage: https://github.com/angel-dart/static
author: Tobe O <thosakwe@gmail.com>
version: 1.2.3
version: 1.2.4
dependencies:
angel_framework: ^1.0.0-dev
cli_util: ^0.1.1