| 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'; |
| 11 import 'package:mime/mime.dart'; | 11 import 'package:mime/mime.dart'; |
| 12 import 'package:path/path.dart' as path; | 12 import 'package:path/path.dart' as path; |
| 13 import 'package:stack_trace/stack_trace.dart'; | 13 import 'package:stack_trace/stack_trace.dart'; |
| 14 | 14 |
| 15 import '../barback.dart'; | 15 import '../barback.dart'; |
| 16 import '../log.dart' as log; | 16 import '../log.dart' as log; |
| 17 import '../utils.dart'; | 17 import '../utils.dart'; |
| 18 import 'base_server.dart'; | 18 import 'base_server.dart'; |
| 19 import 'build_environment.dart'; | 19 import 'build_environment.dart'; |
| 20 import 'old_web_socket_api.dart'; | |
| 21 | 20 |
| 22 /// Callback for determining if an asset with [id] should be served or not. | 21 /// Callback for determining if an asset with [id] should be served or not. |
| 23 typedef bool AllowAsset(AssetId id); | 22 typedef bool AllowAsset(AssetId id); |
| 24 | 23 |
| 25 /// A server that serves assets transformed by barback. | 24 /// A server that serves assets transformed by barback. |
| 26 class BarbackServer extends BaseServer<BarbackServerResult> { | 25 class BarbackServer extends BaseServer<BarbackServerResult> { |
| 27 /// The directory in the root which will serve as the root of this server as | 26 /// The directory in the root which will serve as the root of this server as |
| 28 /// a native platform path. | 27 /// a native platform path. |
| 29 /// | 28 /// |
| 30 /// This may be `null` in which case no files in the root package can be | 29 /// This may be `null` in which case no files in the root package can be |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 88 | 87 |
| 89 // Strip the leading "/" from the URL. | 88 // Strip the leading "/" from the URL. |
| 90 if (parts.isNotEmpty && parts.first == "/") parts = parts.skip(1); | 89 if (parts.isNotEmpty && parts.first == "/") parts = parts.skip(1); |
| 91 | 90 |
| 92 var relativePath = path.url.join(rootDirectory, path.url.joinAll(parts)); | 91 var relativePath = path.url.join(rootDirectory, path.url.joinAll(parts)); |
| 93 return new AssetId(environment.rootPackage.name, relativePath); | 92 return new AssetId(environment.rootPackage.name, relativePath); |
| 94 } | 93 } |
| 95 | 94 |
| 96 /// Handles an HTTP request. | 95 /// Handles an HTTP request. |
| 97 void handleRequest(HttpRequest request) { | 96 void handleRequest(HttpRequest request) { |
| 98 // TODO(rnystrom): Remove this when the Editor is using the admin server. | |
| 99 // port. See #17640. | |
| 100 if (WebSocketTransformer.isUpgradeRequest(request)) { | |
| 101 _handleWebSocket(request); | |
| 102 return; | |
| 103 } | |
| 104 | |
| 105 if (request.method != "GET" && request.method != "HEAD") { | 97 if (request.method != "GET" && request.method != "HEAD") { |
| 106 methodNotAllowed(request); | 98 methodNotAllowed(request); |
| 107 return; | 99 return; |
| 108 } | 100 } |
| 109 | 101 |
| 110 var id; | 102 var id; |
| 111 try { | 103 try { |
| 112 id = urlToId(request.uri); | 104 id = urlToId(request.uri); |
| 113 } on FormatException catch (ex) { | 105 } on FormatException catch (ex) { |
| 114 // If we got here, we had a path like "/packages" which is a special | 106 // If we got here, we had a path like "/packages" which is a special |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 155 addError(error, trace); | 147 addError(error, trace); |
| 156 close(); | 148 close(); |
| 157 return; | 149 return; |
| 158 } | 150 } |
| 159 | 151 |
| 160 addResult(new BarbackServerResult._failure(request.uri, id, error)); | 152 addResult(new BarbackServerResult._failure(request.uri, id, error)); |
| 161 notFound(request, asset: id); | 153 notFound(request, asset: id); |
| 162 }); | 154 }); |
| 163 } | 155 } |
| 164 | 156 |
| 165 // TODO(rnystrom): Remove this when the Editor is using the admin server. | |
| 166 // port. See #17640. | |
| 167 /// Creates a web socket for [request] which should be an upgrade request. | |
| 168 void _handleWebSocket(HttpRequest request) { | |
| 169 Chain.track(WebSocketTransformer.upgrade(request)).then((socket) { | |
| 170 _webSockets.add(socket); | |
| 171 var api = new OldWebSocketApi(socket, environment); | |
| 172 | |
| 173 return api.listen().whenComplete(() { | |
| 174 _webSockets.remove(api); | |
| 175 }); | |
| 176 }).catchError(addError); | |
| 177 } | |
| 178 | |
| 179 /// Serves the body of [asset] on [request]'s response stream. | 157 /// Serves the body of [asset] on [request]'s response stream. |
| 180 /// | 158 /// |
| 181 /// Returns a future that completes when the response has been succesfully | 159 /// Returns a future that completes when the response has been succesfully |
| 182 /// written. | 160 /// written. |
| 183 Future _serveAsset(HttpRequest request, Asset asset) { | 161 Future _serveAsset(HttpRequest request, Asset asset) { |
| 184 return validateStream(asset.read()).then((stream) { | 162 return validateStream(asset.read()).then((stream) { |
| 185 addResult(new BarbackServerResult._success(request.uri, asset.id)); | 163 addResult(new BarbackServerResult._success(request.uri, asset.id)); |
| 186 var mimeType = lookupMimeType(asset.id.path); | 164 var mimeType = lookupMimeType(asset.id.path); |
| 187 if (mimeType != null) { | 165 if (mimeType != null) { |
| 188 request.response.headers.add('content-type', mimeType); | 166 request.response.headers.add('content-type', mimeType); |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 238 bool get isSuccess => error == null; | 216 bool get isSuccess => error == null; |
| 239 | 217 |
| 240 /// Whether the request was served unsuccessfully. | 218 /// Whether the request was served unsuccessfully. |
| 241 bool get isFailure => !isSuccess; | 219 bool get isFailure => !isSuccess; |
| 242 | 220 |
| 243 BarbackServerResult._success(this.url, this.id) | 221 BarbackServerResult._success(this.url, this.id) |
| 244 : error = null; | 222 : error = null; |
| 245 | 223 |
| 246 BarbackServerResult._failure(this.url, this.id, this.error); | 224 BarbackServerResult._failure(this.url, this.id, this.error); |
| 247 } | 225 } |
| OLD | NEW |