Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(53)

Unified Diff: pkg/http_server/lib/src/virtual_directory.dart

Issue 17573011: Add auto-detection of MIME-type in VirtualDirectory. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « pkg/http_server/lib/http_server.dart ('k') | pkg/http_server/pubspec.yaml » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: pkg/http_server/lib/src/virtual_directory.dart
diff --git a/pkg/http_server/lib/src/virtual_directory.dart b/pkg/http_server/lib/src/virtual_directory.dart
index b77df02a3132e78e52e4bc99664b7b459978b69d..39e42e00680b2bdad2c5bbbddef205c5e6eb9903 100644
--- a/pkg/http_server/lib/src/virtual_directory.dart
+++ b/pkg/http_server/lib/src/virtual_directory.dart
@@ -175,12 +175,16 @@ class _VirtualDirectory implements VirtualDirectory {
"bytes $start-${end - 1}/$length");
// Pipe the 'range' of the file.
- file.openRead(start, end).pipe(response).catchError((_) {});
+ file.openRead(start, end)
+ .pipe(new _VirtualDirectoryFileStream(response, file.path))
+ .catchError((_) {});
return;
}
}
- file.openRead().pipe(response).catchError((_) {});
+ file.openRead()
+ .pipe(new _VirtualDirectoryFileStream(response, file.path))
+ .catchError((_) {});
});
}).catchError((_) {
response.close();
@@ -192,3 +196,59 @@ class _VirtualDirectory implements VirtualDirectory {
request.response.close();
}
}
+
+class _VirtualDirectoryFileStream extends StreamConsumer<List<int>> {
+ final HttpResponse response;
+ final String path;
+ var buffer = [];
+
+ _VirtualDirectoryFileStream(HttpResponse this.response, String this.path);
+
+ Future addStream(Stream<List<int>> stream) {
+ stream.listen(
+ (data) {
+ if (buffer == null) {
+ response.add(data);
+ return;
+ }
+ if (buffer.length == 0) {
+ if (data.length >= defaultMagicNumbersMaxLength) {
+ setMimeType(data);
+ response.add(data);
+ buffer = null;
+ } else {
+ buffer.addAll(data);
+ }
+ } else {
+ buffer.addAll(data);
+ if (buffer.length >= defaultMagicNumbersMaxLength) {
+ setMimeType(buffer);
+ response.add(buffer);
+ buffer = null;
+ }
+ }
+ },
+ onDone: () {
+ if (buffer != null) {
+ if (buffer.length == 0) {
+ setMimeType(null);
+ } else {
+ setMimeType(buffer);
+ response.add(buffer);
+ }
+ }
+ response.close();
+ },
+ onError: response.addError);
+ return response.done;
+ }
+
+ Future close() => new Future.value();
+
+ void setMimeType(var bytes) {
+ var mimeType = lookupMimeType(path, headerBytes: bytes);
+ if (mimeType != null) {
+ response.headers.contentType = ContentType.parse(mimeType);
+ }
+ }
+}
« no previous file with comments | « pkg/http_server/lib/http_server.dart ('k') | pkg/http_server/pubspec.yaml » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698