Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 57 * Serve a single [HttpRequest], in this [VirtualDirectory]. | 57 * Serve a single [HttpRequest], in this [VirtualDirectory]. |
| 58 */ | 58 */ |
| 59 void serveRequest(HttpRequest request) { | 59 void serveRequest(HttpRequest request) { |
| 60 _locateResource('.', request.uri.pathSegments.iterator..moveNext()) | 60 _locateResource('.', request.uri.pathSegments.iterator..moveNext()) |
| 61 .then((entity) { | 61 .then((entity) { |
| 62 if (entity == null) { | 62 if (entity == null) { |
| 63 _serveErrorPage(HttpStatus.NOT_FOUND, request); | 63 _serveErrorPage(HttpStatus.NOT_FOUND, request); |
| 64 return; | 64 return; |
| 65 } | 65 } |
| 66 if (entity is File) { | 66 if (entity is File) { |
| 67 _serveFile(entity, request); | 67 serveFile(entity, request); |
| 68 } else if (entity is Directory) { | 68 } else if (entity is Directory) { |
| 69 _serveDirectory(entity, request); | 69 _serveDirectory(entity, request); |
| 70 } else { | 70 } else { |
| 71 _serveErrorPage(HttpStatus.NOT_FOUND, request); | 71 _serveErrorPage(HttpStatus.NOT_FOUND, request); |
| 72 } | 72 } |
| 73 }); | 73 }); |
| 74 } | 74 } |
| 75 | 75 |
| 76 /** | 76 /** |
| 77 * Set the [callback] to override the default directory listing. The | 77 * Set the [callback] to override the default directory listing. The |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 134 } | 134 } |
| 135 }); | 135 }); |
| 136 } | 136 } |
| 137 break; | 137 break; |
| 138 } | 138 } |
| 139 // Return `null` on fall-through, to indicate NOT_FOUND. | 139 // Return `null` on fall-through, to indicate NOT_FOUND. |
| 140 return null; | 140 return null; |
| 141 }); | 141 }); |
| 142 } | 142 } |
| 143 | 143 |
| 144 void _serveFile(File file, HttpRequest request) { | 144 /** |
| 145 * Serve the content of [file] to [request]. | |
| 146 * | |
| 147 * This is usefull when e.g. overriding [directoryHandler] to redirect to | |
| 148 * some index file. | |
| 149 * | |
|
Søren Gjesse
2013/10/29 07:50:01
Please mention that this will handle the If-Modifi
Anders Johnsen
2013/10/29 10:17:22
Done.
| |
| 150 * Note that if it was unabled to read from [file], the [request]s response | |
| 151 * is closed with an appropriate error-code. | |
|
Søren Gjesse
2013/10/29 07:50:01
Currently this seems to be always NOT_FOUND (404)
Anders Johnsen
2013/10/29 10:17:22
Done.
| |
| 152 */ | |
| 153 void serveFile(File file, HttpRequest request) { | |
| 145 var response = request.response; | 154 var response = request.response; |
| 146 // TODO(ajohnsen): Set up Zone support for these errors. | 155 // TODO(ajohnsen): Set up Zone support for these errors. |
| 147 file.lastModified().then((lastModified) { | 156 file.lastModified().then((lastModified) { |
| 148 if (request.headers.ifModifiedSince != null && | 157 if (request.headers.ifModifiedSince != null && |
| 149 !lastModified.isAfter(request.headers.ifModifiedSince)) { | 158 !lastModified.isAfter(request.headers.ifModifiedSince)) { |
| 150 response.statusCode = HttpStatus.NOT_MODIFIED; | 159 response.statusCode = HttpStatus.NOT_MODIFIED; |
| 151 response.close(); | 160 response.close(); |
| 152 return; | 161 return; |
| 153 } | 162 } |
| 154 | 163 |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 194 .catchError((_) {}); | 203 .catchError((_) {}); |
| 195 return; | 204 return; |
| 196 } | 205 } |
| 197 } | 206 } |
| 198 | 207 |
| 199 file.openRead() | 208 file.openRead() |
| 200 .pipe(new _VirtualDirectoryFileStream(response, file.path)) | 209 .pipe(new _VirtualDirectoryFileStream(response, file.path)) |
| 201 .catchError((_) {}); | 210 .catchError((_) {}); |
| 202 }); | 211 }); |
| 203 }).catchError((_) { | 212 }).catchError((_) { |
| 213 response.statusCode = HttpStatus.NOT_FOUND; | |
| 204 response.close(); | 214 response.close(); |
| 205 }); | 215 }); |
| 206 } | 216 } |
| 207 | 217 |
| 208 void _serveDirectory(Directory dir, HttpRequest request) { | 218 void _serveDirectory(Directory dir, HttpRequest request) { |
| 209 if (_dirCallback != null) { | 219 if (_dirCallback != null) { |
| 210 _dirCallback(dir, request); | 220 _dirCallback(dir, request); |
| 211 return; | 221 return; |
| 212 } | 222 } |
| 213 var response = request.response; | 223 var response = request.response; |
| (...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 363 | 373 |
| 364 Future close() => new Future.value(); | 374 Future close() => new Future.value(); |
| 365 | 375 |
| 366 void setMimeType(var bytes) { | 376 void setMimeType(var bytes) { |
| 367 var mimeType = lookupMimeType(path, headerBytes: bytes); | 377 var mimeType = lookupMimeType(path, headerBytes: bytes); |
| 368 if (mimeType != null) { | 378 if (mimeType != null) { |
| 369 response.headers.contentType = ContentType.parse(mimeType); | 379 response.headers.contentType = ContentType.parse(mimeType); |
| 370 } | 380 } |
| 371 } | 381 } |
| 372 } | 382 } |
| OLD | NEW |