Chromium Code Reviews| 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 http_server; | 5 library http_server; |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 import 'dart:io'; | 8 import 'dart:io'; |
| 9 import 'dart:isolate'; | 9 import 'dart:isolate'; |
| 10 import 'dart:uri'; | 10 import 'dart:uri'; |
| (...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 125 for (var server in _serverList) { | 125 for (var server in _serverList) { |
| 126 server.close(); | 126 server.close(); |
| 127 } | 127 } |
| 128 } | 128 } |
| 129 | 129 |
| 130 Future _startHttpServer(String host, {int port: 0, int allowedPort: -1}) { | 130 Future _startHttpServer(String host, {int port: 0, int allowedPort: -1}) { |
| 131 return HttpServer.bind(host, port).then((HttpServer httpServer) { | 131 return HttpServer.bind(host, port).then((HttpServer httpServer) { |
| 132 httpServer.listen((HttpRequest request) { | 132 httpServer.listen((HttpRequest request) { |
| 133 if (request.uri.path == "/echo") { | 133 if (request.uri.path == "/echo") { |
| 134 _handleEchoRequest(request, request.response); | 134 _handleEchoRequest(request, request.response); |
| 135 } else if (request.uri.path == '/ws') { | |
|
kustermann
2013/03/19 12:13:43
Please add '/ws' to the description of the http se
blois
2013/03/20 21:05:17
Done.
| |
| 136 _handleWebSocketRequest(request); | |
| 135 } else { | 137 } else { |
| 136 _handleFileOrDirectoryRequest( | 138 _handleFileOrDirectoryRequest( |
| 137 request, request.response, allowedPort); | 139 request, request.response, allowedPort); |
| 138 } | 140 } |
| 139 }, | 141 }, |
| 140 onError: (e) { | 142 onError: (e) { |
| 141 DebugLogger.error('HttpServer: an error occured: $e'); | 143 DebugLogger.error('HttpServer: an error occured: $e'); |
| 142 }); | 144 }); |
| 143 _serverList.add(httpServer); | 145 _serverList.add(httpServer); |
| 144 }); | 146 }); |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 179 } | 181 } |
| 180 | 182 |
| 181 void _handleEchoRequest(HttpRequest request, HttpResponse response) { | 183 void _handleEchoRequest(HttpRequest request, HttpResponse response) { |
| 182 response.headers.set("Access-Control-Allow-Origin", "*"); | 184 response.headers.set("Access-Control-Allow-Origin", "*"); |
| 183 request.pipe(response).catchError((e) { | 185 request.pipe(response).catchError((e) { |
| 184 DebugLogger.warning( | 186 DebugLogger.warning( |
| 185 'HttpServer: error while closing the response stream: $e'); | 187 'HttpServer: error while closing the response stream: $e'); |
| 186 }); | 188 }); |
| 187 } | 189 } |
| 188 | 190 |
| 191 void _handleWebSocketRequest(HttpRequest request) { | |
| 192 WebSocketTransformer.upgrade(request).then((websocket) { | |
|
Søren Gjesse
2013/03/19 12:31:13
There should be a catchError (with some print stat
blois
2013/03/20 21:05:17
Done.
| |
| 193 websocket.listen((data) { | |
| 194 websocket.send(data); | |
| 195 websocket.close(); | |
|
Søren Gjesse
2013/03/19 12:31:13
There should also be an onError handler here to av
blois
2013/03/20 21:05:17
Done.
| |
| 196 }); | |
| 197 }); | |
| 198 } | |
| 199 | |
| 189 Path _getFilePathFromRequestPath(String urlRequestPath) { | 200 Path _getFilePathFromRequestPath(String urlRequestPath) { |
| 190 // Go to the top of the file to see an explanation of the URL path scheme. | 201 // Go to the top of the file to see an explanation of the URL path scheme. |
| 191 var requestPath = new Path(urlRequestPath.substring(1)).canonicalize(); | 202 var requestPath = new Path(urlRequestPath.substring(1)).canonicalize(); |
| 192 var pathSegments = requestPath.segments(); | 203 var pathSegments = requestPath.segments(); |
| 193 if (pathSegments.length > 0) { | 204 if (pathSegments.length > 0) { |
| 194 var basePath; | 205 var basePath; |
| 195 var relativePath; | 206 var relativePath; |
| 196 if (pathSegments[0] == PREFIX_BUILDDIR) { | 207 if (pathSegments[0] == PREFIX_BUILDDIR) { |
| 197 basePath = _buildDirectory; | 208 basePath = _buildDirectory; |
| 198 relativePath = new Path( | 209 relativePath = new Path( |
| (...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 342 class _Entry { | 353 class _Entry { |
| 343 final String name; | 354 final String name; |
| 344 final String displayName; | 355 final String displayName; |
| 345 | 356 |
| 346 _Entry(this.name, this.displayName); | 357 _Entry(this.name, this.displayName); |
| 347 | 358 |
| 348 int compareTo(_Entry other) { | 359 int compareTo(_Entry other) { |
| 349 return name.compareTo(other.name); | 360 return name.compareTo(other.name); |
| 350 } | 361 } |
| 351 } | 362 } |
| OLD | NEW |