Chromium Code Reviews| Index: sdk/lib/_internal/pub/lib/src/barback/server.dart |
| diff --git a/sdk/lib/_internal/pub/lib/src/barback/server.dart b/sdk/lib/_internal/pub/lib/src/barback/server.dart |
| index d285cfad3e978b42c3cf1a08422ee316f8ee9203..331d153fa8b0197e715c4cd4f125572a4eca2c25 100644 |
| --- a/sdk/lib/_internal/pub/lib/src/barback/server.dart |
| +++ b/sdk/lib/_internal/pub/lib/src/barback/server.dart |
| @@ -110,37 +110,18 @@ class BarbackServer { |
| } |
| _logRequest(request, "Loading $id"); |
| - barback.getAssetById(id).then((asset) { |
| - return validateStream(asset.read()).then((stream) { |
| - _resultsController.add( |
| - new BarbackServerResult._success(request.uri, id)); |
| - // TODO(rnystrom): Set content-type based on asset type. |
| - return Chain.track(request.response.addStream(stream)).then((_) { |
| - // Log successful requests both so we can provide debugging |
| - // information and so scheduled_test knows we haven't timed out while |
| - // loading transformers. |
| - _logRequest(request, "Served $id"); |
| - request.response.close(); |
| - }); |
| - }).catchError((error, trace) { |
| - _resultsController.add( |
| - new BarbackServerResult._failure(request.uri, id, error)); |
| - |
| - // If we couldn't read the asset, handle the error gracefully. |
| - if (error is FileSystemException) { |
| - // Assume this means the asset was a file-backed source asset |
| - // and we couldn't read it, so treat it like a missing asset. |
| - _notFound(request, error); |
| - return; |
| - } |
| - |
| - trace = new Chain.forTrace(trace); |
| - _logRequest(request, "$error\n$trace"); |
| - |
| - // Otherwise, it's some internal error. |
| - request.response.statusCode = 500; |
| - request.response.reasonPhrase = "Internal Error"; |
| - request.response.write(error); |
| + barback.getAssetById(id) |
| + .then((asset) => _serveAsset(request, asset)) |
| + .catchError((error, trace) { |
| + if (error is! AssetNotFoundException) throw error; |
| + return barback.getAssetById(id.addExtension("/index.html")).then((asset) { |
|
Bob Nystrom
2014/02/11 19:18:17
It conveniently does the right thing, but I think
|
| + if (request.uri.path.endsWith('/')) return _serveAsset(request, asset); |
| + |
| + // Follow Apache's index.html behavior by redirecting to ensure |
| + // index.html is only served when the URL has a trailing slash. |
|
Bob Nystrom
2014/02/11 19:18:17
Move this comment up a line. Also, it took my a wh
nweiz
2014/02/11 22:13:41
Done.
|
| + _logRequest(request, "302 Redirect to ${request.uri}/"); |
| + request.response.statusCode = 302; |
| + request.response.headers.add('location', '${request.uri}/'); |
| request.response.close(); |
| }); |
| }).catchError((error, trace) { |
| @@ -159,6 +140,45 @@ class BarbackServer { |
| }); |
| } |
| + /// Serves the body of [asset] on [request]'s response stream. |
| + /// |
| + /// Returns a future that completes when the response has been succesfully |
| + /// written. |
| + Future _serveAsset(HttpRequest request, Asset asset) { |
| + return validateStream(asset.read()).then((stream) { |
| + _resultsController.add( |
| + new BarbackServerResult._success(request.uri, asset.id)); |
| + // TODO(rnystrom): Set content-type based on asset type. |
| + return Chain.track(request.response.addStream(stream)).then((_) { |
| + // Log successful requests both so we can provide debugging |
| + // information and so scheduled_test knows we haven't timed out while |
| + // loading transformers. |
| + _logRequest(request, "Served ${asset.id}"); |
| + request.response.close(); |
| + }); |
| + }).catchError((error, trace) { |
| + _resultsController.add( |
| + new BarbackServerResult._failure(request.uri, asset.id, error)); |
| + |
| + // If we couldn't read the asset, handle the error gracefully. |
| + if (error is FileSystemException) { |
| + // Assume this means the asset was a file-backed source asset |
| + // and we couldn't read it, so treat it like a missing asset. |
| + _notFound(request, error); |
| + return; |
| + } |
| + |
| + trace = new Chain.forTrace(trace); |
| + _logRequest(request, "$error\n$trace"); |
| + |
| + // Otherwise, it's some internal error. |
| + request.response.statusCode = 500; |
| + request.response.reasonPhrase = "Internal Error"; |
| + request.response.write(error); |
| + request.response.close(); |
| + }); |
| + } |
| + |
| /// Creates a web socket for [request] which should be an upgrade request. |
| void _handleWebSocket(HttpRequest request) { |
| Chain.track(WebSocketTransformer.upgrade(request)).then((socket) { |