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

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

Issue 17589012: Add error-page support 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 | « no previous file | pkg/http_server/test/utils.dart » ('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 39e42e00680b2bdad2c5bbbddef205c5e6eb9903..6ce667b687dfbb97f858d13d2acb69f91e1801a6 100644
--- a/pkg/http_server/lib/src/virtual_directory.dart
+++ b/pkg/http_server/lib/src/virtual_directory.dart
@@ -49,7 +49,7 @@ abstract class VirtualDirectory {
* Set the [callback] to override the error page handler. When [callback] is
* invoked, the `statusCode` property of the response is set.
*/
- void setErrorPageHandler(void callback(HttpResponse response));
+ void setErrorPageHandler(void callback(HttpRequest request));
}
class _VirtualDirectory implements VirtualDirectory {
@@ -58,6 +58,8 @@ class _VirtualDirectory implements VirtualDirectory {
bool allowDirectoryListing = false;
bool followLinks = true;
+ Function _errorCallback;
+
_VirtualDirectory(this.root);
void serve(Stream<HttpRequest> requests) {
@@ -85,6 +87,10 @@ class _VirtualDirectory implements VirtualDirectory {
});
}
+ void setErrorPageHandler(void callback(HttpRequest request)) {
+ _errorCallback = callback;
+ }
+
Future<FileSystemEntity> _locateResource(Path path,
Iterable<String> segments) {
Path fullPath() => new Path(root).join(path);
@@ -192,8 +198,32 @@ class _VirtualDirectory implements VirtualDirectory {
}
void _serveErrorPage(int error, HttpRequest request) {
- request.response.statusCode = error;
- request.response.close();
+ var response = request.response;
+ response.statusCode = error;
+ if (_errorCallback != null) {
+ _errorCallback(request);
+ return;
+ }
+ // Default error page.
+ var path = request.uri.path;
+ var reason = response.reasonPhrase;
+ response.write(
+ '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"\n');
+ response.writeln(
+ '"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">');
+ response.writeln('<html xmlns="http://www.w3.org/1999/xhtml">');
+ response.writeln('<head>');
+ response.writeln('<title>$reason: $path</title>');
+ response.writeln('</head>');
+ response.writeln('<body>');
+ response.writeln('<h1>Error $error at \'$path\': $reason</h1>');
+ var server = response.headers.value(HttpHeaders.SERVER);
+ if (server != null) {
+ response.writeln(server);
+ }
+ response.writeln('</body>');
+ response.writeln('</html>');
+ response.close();
}
}
« no previous file with comments | « no previous file | pkg/http_server/test/utils.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698