This commit is contained in:
Tobe O 2019-01-27 17:14:54 -05:00
parent 9a038ded48
commit 6a3309c987
5 changed files with 20 additions and 36 deletions

View file

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

View file

@ -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<String> 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}');
}

View file

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

View file

@ -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('<!DOCTYPE html>')
@ -225,10 +223,7 @@ class VirtualDirectory {
/// Writes the contents of a file to a response.
Future<bool> 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(

View file

@ -4,7 +4,7 @@ environment:
sdk: ">=1.8.0 <3.0.0"
homepage: https://github.com/angel-dart/static
author: Tobe O <thosakwe@gmail.com>
version: 2.1.1
version: 2.1.2
dependencies:
angel_framework: ^2.0.0-alpha
convert: ^2.0.0