| 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:io'; | 8 import 'dart:io'; |
| 9 | 9 |
| 10 import 'package:barback/barback.dart'; | 10 import 'package:barback/barback.dart'; |
| (...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 112 error: "Asset $id is not available in this configuration.", | 112 error: "Asset $id is not available in this configuration.", |
| 113 asset: id); | 113 asset: id); |
| 114 } | 114 } |
| 115 | 115 |
| 116 return environment.barback.getAssetById(id).then((result) { | 116 return environment.barback.getAssetById(id).then((result) { |
| 117 return result; | 117 return result; |
| 118 }).then((asset) => _serveAsset(request, asset)).catchError((error, trace) { | 118 }).then((asset) => _serveAsset(request, asset)).catchError((error, trace) { |
| 119 if (error is! AssetNotFoundException) throw error; | 119 if (error is! AssetNotFoundException) throw error; |
| 120 return environment.barback.getAssetById(id.addExtension("/index.html")) | 120 return environment.barback.getAssetById(id.addExtension("/index.html")) |
| 121 .then((asset) { | 121 .then((asset) { |
| 122 if (request.url.path.endsWith('/')) return _serveAsset(request, asset); | 122 if (request.url.path.isEmpty || request.url.path.endsWith('/')) { |
| 123 return _serveAsset(request, asset); |
| 124 } |
| 123 | 125 |
| 124 // We only want to serve index.html if the URL explicitly ends in a | 126 // We only want to serve index.html if the URL explicitly ends in a |
| 125 // slash. For other URLs, we redirect to one with the slash added to | 127 // slash. For other URLs, we redirect to one with the slash added to |
| 126 // implicitly support that too. This follows Apache's behavior. | 128 // implicitly support that too. This follows Apache's behavior. |
| 127 logRequest(request, "302 Redirect to ${request.url}/"); | 129 logRequest(request, "302 Redirect to /${request.url}/"); |
| 128 return new shelf.Response.found('${request.url}/'); | 130 return new shelf.Response.found('/${request.url}/'); |
| 129 }).catchError((newError, newTrace) { | 131 }).catchError((newError, newTrace) { |
| 130 // If we find neither the original file or the index, we should report | 132 // If we find neither the original file or the index, we should report |
| 131 // the error about the original to the user. | 133 // the error about the original to the user. |
| 132 throw newError is AssetNotFoundException ? error : newError; | 134 throw newError is AssetNotFoundException ? error : newError; |
| 133 }); | 135 }); |
| 134 }).catchError((error, trace) { | 136 }).catchError((error, trace) { |
| 135 if (error is! AssetNotFoundException) { | 137 if (error is! AssetNotFoundException) { |
| 136 trace = new Chain.forTrace(trace); | 138 trace = new Chain.forTrace(trace); |
| 137 logRequest(request, "$error\n$trace"); | 139 logRequest(request, "$error\n$trace"); |
| 138 | 140 |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 200 bool get isSuccess => error == null; | 202 bool get isSuccess => error == null; |
| 201 | 203 |
| 202 /// Whether the request was served unsuccessfully. | 204 /// Whether the request was served unsuccessfully. |
| 203 bool get isFailure => !isSuccess; | 205 bool get isFailure => !isSuccess; |
| 204 | 206 |
| 205 BarbackServerResult._success(this.url, this.id) | 207 BarbackServerResult._success(this.url, this.id) |
| 206 : error = null; | 208 : error = null; |
| 207 | 209 |
| 208 BarbackServerResult._failure(this.url, this.id, this.error); | 210 BarbackServerResult._failure(this.url, this.id, this.error); |
| 209 } | 211 } |
| OLD | NEW |