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

Side by Side Diff: pkg/http_server/lib/src/virtual_directory.dart

Issue 47513008: Expose 'serveFile' on VirtualDirectory. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Expand documentation Created 7 years, 1 month 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/virtual_directory_test.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 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
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
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 *
150 * In the request contains the [HttpStatus.IF_MODIFIED_SINCE] header,
151 * [serveFile] will send a [HttpStatus.NOT_MODIFIED] response if the file
152 * was not changed.
153 *
154 * Note that if it was unabled to read from [file], the [request]s response
155 * is closed with error-code [HttpStatus.NOT_FOUND].
156 */
157 void serveFile(File file, HttpRequest request) {
145 var response = request.response; 158 var response = request.response;
146 // TODO(ajohnsen): Set up Zone support for these errors. 159 // TODO(ajohnsen): Set up Zone support for these errors.
147 file.lastModified().then((lastModified) { 160 file.lastModified().then((lastModified) {
148 if (request.headers.ifModifiedSince != null && 161 if (request.headers.ifModifiedSince != null &&
149 !lastModified.isAfter(request.headers.ifModifiedSince)) { 162 !lastModified.isAfter(request.headers.ifModifiedSince)) {
150 response.statusCode = HttpStatus.NOT_MODIFIED; 163 response.statusCode = HttpStatus.NOT_MODIFIED;
151 response.close(); 164 response.close();
152 return; 165 return;
153 } 166 }
154 167
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
194 .catchError((_) {}); 207 .catchError((_) {});
195 return; 208 return;
196 } 209 }
197 } 210 }
198 211
199 file.openRead() 212 file.openRead()
200 .pipe(new _VirtualDirectoryFileStream(response, file.path)) 213 .pipe(new _VirtualDirectoryFileStream(response, file.path))
201 .catchError((_) {}); 214 .catchError((_) {});
202 }); 215 });
203 }).catchError((_) { 216 }).catchError((_) {
217 response.statusCode = HttpStatus.NOT_FOUND;
204 response.close(); 218 response.close();
205 }); 219 });
206 } 220 }
207 221
208 void _serveDirectory(Directory dir, HttpRequest request) { 222 void _serveDirectory(Directory dir, HttpRequest request) {
209 if (_dirCallback != null) { 223 if (_dirCallback != null) {
210 _dirCallback(dir, request); 224 _dirCallback(dir, request);
211 return; 225 return;
212 } 226 }
213 var response = request.response; 227 var response = request.response;
(...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after
363 377
364 Future close() => new Future.value(); 378 Future close() => new Future.value();
365 379
366 void setMimeType(var bytes) { 380 void setMimeType(var bytes) {
367 var mimeType = lookupMimeType(path, headerBytes: bytes); 381 var mimeType = lookupMimeType(path, headerBytes: bytes);
368 if (mimeType != null) { 382 if (mimeType != null) {
369 response.headers.contentType = ContentType.parse(mimeType); 383 response.headers.contentType = ContentType.parse(mimeType);
370 } 384 }
371 } 385 }
372 } 386 }
OLDNEW
« no previous file with comments | « no previous file | pkg/http_server/test/virtual_directory_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698