2.1.2
This commit is contained in:
parent
9a038ded48
commit
6a3309c987
5 changed files with 20 additions and 36 deletions
|
@ -1,3 +1,6 @@
|
||||||
|
# 2.1.2
|
||||||
|
* Patch support for range+streaming in Caching server.
|
||||||
|
|
||||||
# 2.1.1
|
# 2.1.1
|
||||||
* URI-encode paths in directory listing. This produces correct URL's, always.
|
* URI-encode paths in directory listing. This produces correct URL's, always.
|
||||||
|
|
||||||
|
|
|
@ -4,14 +4,17 @@ import 'package:angel_static/angel_static.dart';
|
||||||
import 'package:file/local.dart';
|
import 'package:file/local.dart';
|
||||||
import 'package:logging/logging.dart';
|
import 'package:logging/logging.dart';
|
||||||
|
|
||||||
main() async {
|
main(List<String> args) async {
|
||||||
var app = new Angel();
|
var app = new Angel();
|
||||||
var http = new AngelHttp(app);
|
var http = new AngelHttp(app);
|
||||||
var fs = const LocalFileSystem();
|
var fs = const LocalFileSystem();
|
||||||
var vDir = new CachingVirtualDirectory(app, fs,
|
var vDir = new CachingVirtualDirectory(
|
||||||
allowDirectoryListing: true,
|
app,
|
||||||
source: fs.currentDirectory,
|
fs,
|
||||||
maxAge: const Duration(days: 24).inSeconds);
|
allowDirectoryListing: true,
|
||||||
|
source: args.isEmpty ? fs.currentDirectory : fs.directory(args[0]),
|
||||||
|
maxAge: const Duration(days: 24).inSeconds,
|
||||||
|
);
|
||||||
|
|
||||||
app.mimeTypeResolver
|
app.mimeTypeResolver
|
||||||
..addExtension('', 'text/plain')
|
..addExtension('', 'text/plain')
|
||||||
|
@ -31,5 +34,6 @@ main() async {
|
||||||
app.fallback(vDir.handleRequest);
|
app.fallback(vDir.handleRequest);
|
||||||
|
|
||||||
var server = await http.startServer('127.0.0.1', 3000);
|
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}');
|
print('Listening at http://${server.address.address}:${server.port}');
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,16 +1,9 @@
|
||||||
import 'dart:async';
|
import 'dart:async';
|
||||||
import 'dart:io' show HttpDate;
|
import 'dart:io' show HttpDate;
|
||||||
import 'package:angel_framework/angel_framework.dart';
|
import 'package:angel_framework/angel_framework.dart';
|
||||||
import 'package:convert/convert.dart';
|
|
||||||
import 'package:crypto/crypto.dart';
|
|
||||||
import 'package:file/file.dart';
|
import 'package:file/file.dart';
|
||||||
import 'virtual_directory.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].
|
/// Returns a string representation of the given [CacheAccessLevel].
|
||||||
String accessLevelToString(CacheAccessLevel accessLevel) {
|
String accessLevelToString(CacheAccessLevel accessLevel) {
|
||||||
switch (accessLevel) {
|
switch (accessLevel) {
|
||||||
|
@ -114,8 +107,7 @@ class CachingVirtualDirectory extends VirtualDirectory {
|
||||||
|
|
||||||
return new Future.value(false);
|
return new Future.value(false);
|
||||||
} else if (ifRange) {
|
} else if (ifRange) {
|
||||||
// Return 200, just send the whole thing.
|
return super.serveFile(file, stat, req, res);
|
||||||
return res.streamFile(file).then((_) => false);
|
|
||||||
}
|
}
|
||||||
} catch (_) {
|
} catch (_) {
|
||||||
throw new AngelHttpException.badRequest(
|
throw new AngelHttpException.badRequest(
|
||||||
|
@ -154,28 +146,18 @@ class CachingVirtualDirectory extends VirtualDirectory {
|
||||||
return new Future.value(false);
|
return new Future.value(false);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (!hasBeenModified) {
|
return super.serveFile(file, stat, req, res);
|
||||||
// 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 file.readAsBytes().then((buf) {
|
return file.lastModified().then((stamp) {
|
||||||
if (useEtags) {
|
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);
|
setCachedHeaders(stat.modified, req, res);
|
||||||
res.add(buf);
|
return res.streamFile(file).then((_) => false);
|
||||||
return false;
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -146,13 +146,11 @@ class VirtualDirectory {
|
||||||
final index =
|
final index =
|
||||||
fileSystem.file(directory.absolute.uri.resolve(indexFileName));
|
fileSystem.file(directory.absolute.uri.resolve(indexFileName));
|
||||||
if (await index.exists()) {
|
if (await index.exists()) {
|
||||||
if (req.method == 'HEAD') return false;
|
|
||||||
return await serveFile(index, stat, req, res);
|
return await serveFile(index, stat, req, res);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (allowDirectoryListing == true) {
|
if (allowDirectoryListing == true) {
|
||||||
if (req.method == 'HEAD') return false;
|
|
||||||
res.contentType = new MediaType('text', 'html');
|
res.contentType = new MediaType('text', 'html');
|
||||||
res
|
res
|
||||||
..write('<!DOCTYPE html>')
|
..write('<!DOCTYPE html>')
|
||||||
|
@ -225,10 +223,7 @@ class VirtualDirectory {
|
||||||
/// Writes the contents of a file to a response.
|
/// Writes the contents of a file to a response.
|
||||||
Future<bool> serveFile(
|
Future<bool> serveFile(
|
||||||
File file, FileStat stat, RequestContext req, ResponseContext res) async {
|
File file, FileStat stat, RequestContext req, ResponseContext res) async {
|
||||||
if (req.method == 'HEAD') {
|
res.headers['accept-ranges'] = 'bytes';
|
||||||
res.headers['accept-ranges'] = 'bytes';
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (callback != null) {
|
if (callback != null) {
|
||||||
return await req.app.executeHandler(
|
return await req.app.executeHandler(
|
||||||
|
|
|
@ -4,7 +4,7 @@ environment:
|
||||||
sdk: ">=1.8.0 <3.0.0"
|
sdk: ">=1.8.0 <3.0.0"
|
||||||
homepage: https://github.com/angel-dart/static
|
homepage: https://github.com/angel-dart/static
|
||||||
author: Tobe O <thosakwe@gmail.com>
|
author: Tobe O <thosakwe@gmail.com>
|
||||||
version: 2.1.1
|
version: 2.1.2
|
||||||
dependencies:
|
dependencies:
|
||||||
angel_framework: ^2.0.0-alpha
|
angel_framework: ^2.0.0-alpha
|
||||||
convert: ^2.0.0
|
convert: ^2.0.0
|
||||||
|
|
Loading…
Reference in a new issue