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 library pub.barback.server; | 5 library pub.barback.server; |
6 | 6 |
7 import 'dart:async'; | 7 import 'dart:async'; |
8 import 'dart:convert'; | 8 import 'dart:convert'; |
9 import 'dart:io'; | 9 import 'dart:io'; |
10 | 10 |
11 import 'package:barback/barback.dart'; | 11 import 'package:barback/barback.dart'; |
12 import 'package:mime/mime.dart'; | |
12 import 'package:path/path.dart' as path; | 13 import 'package:path/path.dart' as path; |
13 import 'package:stack_trace/stack_trace.dart'; | 14 import 'package:stack_trace/stack_trace.dart'; |
14 | 15 |
15 import '../barback.dart'; | 16 import '../barback.dart'; |
16 import '../log.dart' as log; | 17 import '../log.dart' as log; |
17 import '../utils.dart'; | 18 import '../utils.dart'; |
18 | 19 |
19 /// Callback for determining if an asset with [id] should be served or not. | 20 /// Callback for determining if an asset with [id] should be served or not. |
20 typedef bool AllowAsset(AssetId id); | 21 typedef bool AllowAsset(AssetId id); |
21 | 22 |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
107 if (allowAsset != null && !allowAsset(id)) { | 108 if (allowAsset != null && !allowAsset(id)) { |
108 _notFound(request, "Asset $id is not available in this configuration."); | 109 _notFound(request, "Asset $id is not available in this configuration."); |
109 return; | 110 return; |
110 } | 111 } |
111 | 112 |
112 _logRequest(request, "Loading $id"); | 113 _logRequest(request, "Loading $id"); |
113 barback.getAssetById(id).then((asset) { | 114 barback.getAssetById(id).then((asset) { |
114 return validateStream(asset.read()).then((stream) { | 115 return validateStream(asset.read()).then((stream) { |
115 _resultsController.add( | 116 _resultsController.add( |
116 new BarbackServerResult._success(request.uri, id)); | 117 new BarbackServerResult._success(request.uri, id)); |
118 request.response.headers.add('content-type', lookupMimeType(id.path)); | |
Bob Nystrom
2014/02/11 19:11:14
Hurray for code reuse!
| |
117 // TODO(rnystrom): Set content-type based on asset type. | 119 // TODO(rnystrom): Set content-type based on asset type. |
118 return Chain.track(request.response.addStream(stream)).then((_) { | 120 return Chain.track(request.response.addStream(stream)).then((_) { |
119 // Log successful requests both so we can provide debugging | 121 // Log successful requests both so we can provide debugging |
120 // information and so scheduled_test knows we haven't timed out while | 122 // information and so scheduled_test knows we haven't timed out while |
121 // loading transformers. | 123 // loading transformers. |
122 _logRequest(request, "Served $id"); | 124 _logRequest(request, "Served $id"); |
123 request.response.close(); | 125 request.response.close(); |
124 }); | 126 }); |
125 }).catchError((error, trace) { | 127 }).catchError((error, trace) { |
126 _resultsController.add( | 128 _resultsController.add( |
(...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
301 bool get isSuccess => error == null; | 303 bool get isSuccess => error == null; |
302 | 304 |
303 /// Whether the request was served unsuccessfully. | 305 /// Whether the request was served unsuccessfully. |
304 bool get isFailure => !isSuccess; | 306 bool get isFailure => !isSuccess; |
305 | 307 |
306 BarbackServerResult._success(this.url, this.id) | 308 BarbackServerResult._success(this.url, this.id) |
307 : error = null; | 309 : error = null; |
308 | 310 |
309 BarbackServerResult._failure(this.url, this.id, this.error); | 311 BarbackServerResult._failure(this.url, this.id, this.error); |
310 } | 312 } |
OLD | NEW |