diff --git a/CHANGELOG.md b/CHANGELOG.md index 701b3155..329b1625 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,6 @@ +# 2.1.2 +* Patch support for range+streaming in Caching server. + # 2.1.1 * URI-encode paths in directory listing. This produces correct URL's, always. diff --git a/example/main.dart b/example/main.dart index ca11c5b8..b923181c 100644 --- a/example/main.dart +++ b/example/main.dart @@ -4,14 +4,17 @@ import 'package:angel_static/angel_static.dart'; import 'package:file/local.dart'; import 'package:logging/logging.dart'; -main() async { +main(List args) async { var app = new Angel(); var http = new AngelHttp(app); var fs = const LocalFileSystem(); - var vDir = new CachingVirtualDirectory(app, fs, - allowDirectoryListing: true, - source: fs.currentDirectory, - maxAge: const Duration(days: 24).inSeconds); + var vDir = new CachingVirtualDirectory( + app, + fs, + allowDirectoryListing: true, + source: args.isEmpty ? fs.currentDirectory : fs.directory(args[0]), + maxAge: const Duration(days: 24).inSeconds, + ); app.mimeTypeResolver ..addExtension('', 'text/plain') @@ -31,5 +34,6 @@ main() async { app.fallback(vDir.handleRequest); var server = await http.startServer('127.0.0.1', 3000); + print('Serving from ${vDir.source.path}'); print('Listening at http://${server.address.address}:${server.port}'); } diff --git a/lib/src/cache.dart b/lib/src/cache.dart index 3cede6cf..2669d321 100644 --- a/lib/src/cache.dart +++ b/lib/src/cache.dart @@ -1,16 +1,9 @@ import 'dart:async'; import 'dart:io' show HttpDate; import 'package:angel_framework/angel_framework.dart'; -import 'package:convert/convert.dart'; -import 'package:crypto/crypto.dart'; import 'package:file/file.dart'; import 'virtual_directory.dart'; -/// Generates an MD5 ETag from the given buffer. -String md5Etag(List buf) { - return hex.encode(md5.convert(buf).bytes); -} - /// Returns a string representation of the given [CacheAccessLevel]. String accessLevelToString(CacheAccessLevel accessLevel) { switch (accessLevel) { @@ -114,8 +107,7 @@ class CachingVirtualDirectory extends VirtualDirectory { return new Future.value(false); } else if (ifRange) { - // Return 200, just send the whole thing. - return res.streamFile(file).then((_) => false); + return super.serveFile(file, stat, req, res); } } catch (_) { throw new AngelHttpException.badRequest( @@ -154,28 +146,18 @@ class CachingVirtualDirectory extends VirtualDirectory { return new Future.value(false); } } else { - if (!hasBeenModified) { - // Continue serving like a regular range... - return super.serveFile(file, stat, req, res); - } else { - // Otherwise, send the whole thing. - return res.streamFile(file).then((_) => false); - } + return super.serveFile(file, stat, req, res); } } } - return file.readAsBytes().then((buf) { + return file.lastModified().then((stamp) { if (useEtags) { - res.headers['ETag'] = _etags[file.absolute.path] = md5Etag(buf); + res.headers['ETag'] = _etags[file.absolute.path] = stamp.millisecondsSinceEpoch.toString(); } - //res.statusCode = 200; - res.headers - ..['content-type'] = res.app.mimeTypeResolver.lookup(file.path) ?? - 'application/octet-stream'; + setCachedHeaders(stat.modified, req, res); - res.add(buf); - return false; + return res.streamFile(file).then((_) => false); }); } } diff --git a/lib/src/virtual_directory.dart b/lib/src/virtual_directory.dart index a4fa4b93..bc24dc2a 100644 --- a/lib/src/virtual_directory.dart +++ b/lib/src/virtual_directory.dart @@ -146,13 +146,11 @@ class VirtualDirectory { final index = fileSystem.file(directory.absolute.uri.resolve(indexFileName)); if (await index.exists()) { - if (req.method == 'HEAD') return false; return await serveFile(index, stat, req, res); } } if (allowDirectoryListing == true) { - if (req.method == 'HEAD') return false; res.contentType = new MediaType('text', 'html'); res ..write('') @@ -225,10 +223,7 @@ class VirtualDirectory { /// Writes the contents of a file to a response. Future serveFile( File file, FileStat stat, RequestContext req, ResponseContext res) async { - if (req.method == 'HEAD') { - res.headers['accept-ranges'] = 'bytes'; - return false; - } + res.headers['accept-ranges'] = 'bytes'; if (callback != null) { return await req.app.executeHandler( diff --git a/pubspec.yaml b/pubspec.yaml index d51c1082..d81c9611 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -4,7 +4,7 @@ environment: sdk: ">=1.8.0 <3.0.0" homepage: https://github.com/angel-dart/static author: Tobe O -version: 2.1.1 +version: 2.1.2 dependencies: angel_framework: ^2.0.0-alpha convert: ^2.0.0