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

Side by Side 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 unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « pkg/http_server/lib/http_server.dart ('k') | pkg/http_server/pubspec.yaml » ('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 157 matching lines...) Expand 10 before | Expand all | Expand 10 after
168 168
169 // Override Content-Length with the actual bytes sent. 169 // Override Content-Length with the actual bytes sent.
170 response.headers.set(HttpHeaders.CONTENT_LENGTH, end - start); 170 response.headers.set(HttpHeaders.CONTENT_LENGTH, end - start);
171 171
172 // Set 'Partial Content' status code. 172 // Set 'Partial Content' status code.
173 response.statusCode = HttpStatus.PARTIAL_CONTENT; 173 response.statusCode = HttpStatus.PARTIAL_CONTENT;
174 response.headers.set(HttpHeaders.CONTENT_RANGE, 174 response.headers.set(HttpHeaders.CONTENT_RANGE,
175 "bytes $start-${end - 1}/$length"); 175 "bytes $start-${end - 1}/$length");
176 176
177 // Pipe the 'range' of the file. 177 // Pipe the 'range' of the file.
178 file.openRead(start, end).pipe(response).catchError((_) {}); 178 file.openRead(start, end)
179 .pipe(new _VirtualDirectoryFileStream(response, file.path))
180 .catchError((_) {});
179 return; 181 return;
180 } 182 }
181 } 183 }
182 184
183 file.openRead().pipe(response).catchError((_) {}); 185 file.openRead()
186 .pipe(new _VirtualDirectoryFileStream(response, file.path))
187 .catchError((_) {});
184 }); 188 });
185 }).catchError((_) { 189 }).catchError((_) {
186 response.close(); 190 response.close();
187 }); 191 });
188 } 192 }
189 193
190 void _serveErrorPage(int error, HttpRequest request) { 194 void _serveErrorPage(int error, HttpRequest request) {
191 request.response.statusCode = error; 195 request.response.statusCode = error;
192 request.response.close(); 196 request.response.close();
193 } 197 }
194 } 198 }
199
200 class _VirtualDirectoryFileStream extends StreamConsumer<List<int>> {
201 final HttpResponse response;
202 final String path;
203 var buffer = [];
204
205 _VirtualDirectoryFileStream(HttpResponse this.response, String this.path);
206
207 Future addStream(Stream<List<int>> stream) {
208 stream.listen(
209 (data) {
210 if (buffer == null) {
211 response.add(data);
212 return;
213 }
214 if (buffer.length == 0) {
215 if (data.length >= defaultMagicNumbersMaxLength) {
216 setMimeType(data);
217 response.add(data);
218 buffer = null;
219 } else {
220 buffer.addAll(data);
221 }
222 } else {
223 buffer.addAll(data);
224 if (buffer.length >= defaultMagicNumbersMaxLength) {
225 setMimeType(buffer);
226 response.add(buffer);
227 buffer = null;
228 }
229 }
230 },
231 onDone: () {
232 if (buffer != null) {
233 if (buffer.length == 0) {
234 setMimeType(null);
235 } else {
236 setMimeType(buffer);
237 response.add(buffer);
238 }
239 }
240 response.close();
241 },
242 onError: response.addError);
243 return response.done;
244 }
245
246 Future close() => new Future.value();
247
248 void setMimeType(var bytes) {
249 var mimeType = lookupMimeType(path, headerBytes: bytes);
250 if (mimeType != null) {
251 response.headers.contentType = ContentType.parse(mimeType);
252 }
253 }
254 }
OLDNEW
« 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