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

Side by Side 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 unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | pkg/http_server/test/utils.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 part of http_server; 5 part of http_server;
6 6
7 /** 7 /**
8 * A [VirtualDirectory] can serve files and directory-listing from a root path, 8 * A [VirtualDirectory] can serve files and directory-listing from a root path,
9 * to [HttpRequest]s. 9 * to [HttpRequest]s.
10 * 10 *
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
42 42
43 /** 43 /**
44 * Serve a single [HttpRequest], in this [VirtualDirectory]. 44 * Serve a single [HttpRequest], in this [VirtualDirectory].
45 */ 45 */
46 void serveRequest(HttpRequest request); 46 void serveRequest(HttpRequest request);
47 47
48 /** 48 /**
49 * Set the [callback] to override the error page handler. When [callback] is 49 * Set the [callback] to override the error page handler. When [callback] is
50 * invoked, the `statusCode` property of the response is set. 50 * invoked, the `statusCode` property of the response is set.
51 */ 51 */
52 void setErrorPageHandler(void callback(HttpResponse response)); 52 void setErrorPageHandler(void callback(HttpRequest request));
53 } 53 }
54 54
55 class _VirtualDirectory implements VirtualDirectory { 55 class _VirtualDirectory implements VirtualDirectory {
56 final String root; 56 final String root;
57 57
58 bool allowDirectoryListing = false; 58 bool allowDirectoryListing = false;
59 bool followLinks = true; 59 bool followLinks = true;
60 60
61 Function _errorCallback;
62
61 _VirtualDirectory(this.root); 63 _VirtualDirectory(this.root);
62 64
63 void serve(Stream<HttpRequest> requests) { 65 void serve(Stream<HttpRequest> requests) {
64 requests.listen(serveRequest); 66 requests.listen(serveRequest);
65 } 67 }
66 68
67 void serveRequest(HttpRequest request) { 69 void serveRequest(HttpRequest request) {
68 var path = new Path(request.uri.path).canonicalize(); 70 var path = new Path(request.uri.path).canonicalize();
69 71
70 if (!path.isAbsolute) { 72 if (!path.isAbsolute) {
71 return _serveErrorPage(HttpStatus.NOT_FOUND, request); 73 return _serveErrorPage(HttpStatus.NOT_FOUND, request);
72 } 74 }
73 75
74 _locateResource(new Path('.'), path.segments()) 76 _locateResource(new Path('.'), path.segments())
75 .then((entity) { 77 .then((entity) {
76 if (entity == null) { 78 if (entity == null) {
77 _serveErrorPage(HttpStatus.NOT_FOUND, request); 79 _serveErrorPage(HttpStatus.NOT_FOUND, request);
78 return; 80 return;
79 } 81 }
80 if (entity is File) { 82 if (entity is File) {
81 _serveFile(entity, request); 83 _serveFile(entity, request);
82 } else { 84 } else {
83 _serveErrorPage(HttpStatus.NOT_FOUND, request); 85 _serveErrorPage(HttpStatus.NOT_FOUND, request);
84 } 86 }
85 }); 87 });
86 } 88 }
87 89
90 void setErrorPageHandler(void callback(HttpRequest request)) {
91 _errorCallback = callback;
92 }
93
88 Future<FileSystemEntity> _locateResource(Path path, 94 Future<FileSystemEntity> _locateResource(Path path,
89 Iterable<String> segments) { 95 Iterable<String> segments) {
90 Path fullPath() => new Path(root).join(path); 96 Path fullPath() => new Path(root).join(path);
91 return FileSystemEntity.type(fullPath().toNativePath(), followLinks: false) 97 return FileSystemEntity.type(fullPath().toNativePath(), followLinks: false)
92 .then((type) { 98 .then((type) {
93 switch (type) { 99 switch (type) {
94 case FileSystemEntityType.FILE: 100 case FileSystemEntityType.FILE:
95 if (segments.isEmpty) return new File.fromPath(fullPath()); 101 if (segments.isEmpty) return new File.fromPath(fullPath());
96 break; 102 break;
97 103
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after
185 file.openRead() 191 file.openRead()
186 .pipe(new _VirtualDirectoryFileStream(response, file.path)) 192 .pipe(new _VirtualDirectoryFileStream(response, file.path))
187 .catchError((_) {}); 193 .catchError((_) {});
188 }); 194 });
189 }).catchError((_) { 195 }).catchError((_) {
190 response.close(); 196 response.close();
191 }); 197 });
192 } 198 }
193 199
194 void _serveErrorPage(int error, HttpRequest request) { 200 void _serveErrorPage(int error, HttpRequest request) {
195 request.response.statusCode = error; 201 var response = request.response;
196 request.response.close(); 202 response.statusCode = error;
203 if (_errorCallback != null) {
204 _errorCallback(request);
205 return;
206 }
207 // Default error page.
208 var path = request.uri.path;
209 var reason = response.reasonPhrase;
210 response.write(
211 '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"\n');
212 response.writeln(
213 '"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">');
214 response.writeln('<html xmlns="http://www.w3.org/1999/xhtml">');
215 response.writeln('<head>');
216 response.writeln('<title>$reason: $path</title>');
217 response.writeln('</head>');
218 response.writeln('<body>');
219 response.writeln('<h1>Error $error at \'$path\': $reason</h1>');
220 var server = response.headers.value(HttpHeaders.SERVER);
221 if (server != null) {
222 response.writeln(server);
223 }
224 response.writeln('</body>');
225 response.writeln('</html>');
226 response.close();
197 } 227 }
198 } 228 }
199 229
200 class _VirtualDirectoryFileStream extends StreamConsumer<List<int>> { 230 class _VirtualDirectoryFileStream extends StreamConsumer<List<int>> {
201 final HttpResponse response; 231 final HttpResponse response;
202 final String path; 232 final String path;
203 var buffer = []; 233 var buffer = [];
204 234
205 _VirtualDirectoryFileStream(HttpResponse this.response, String this.path); 235 _VirtualDirectoryFileStream(HttpResponse this.response, String this.path);
206 236
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
245 275
246 Future close() => new Future.value(); 276 Future close() => new Future.value();
247 277
248 void setMimeType(var bytes) { 278 void setMimeType(var bytes) {
249 var mimeType = lookupMimeType(path, headerBytes: bytes); 279 var mimeType = lookupMimeType(path, headerBytes: bytes);
250 if (mimeType != null) { 280 if (mimeType != null) {
251 response.headers.contentType = ContentType.parse(mimeType); 281 response.headers.contentType = ContentType.parse(mimeType);
252 } 282 }
253 } 283 }
254 } 284 }
OLDNEW
« 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