| OLD | NEW |
| 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, 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:io'; | 8 import 'dart:io'; |
| 8 | 9 |
| 9 import 'package:analysis_server/src/channel.dart'; | 10 import 'package:analysis_server/src/channel.dart'; |
| 10 import 'package:analysis_server/src/get_handler.dart'; | 11 import 'package:analysis_server/src/get_handler.dart'; |
| 11 import 'package:analysis_server/src/socket_server.dart'; | 12 import 'package:analysis_server/src/socket_server.dart'; |
| 12 | 13 |
| 13 /** | 14 /** |
| 14 * Instances of the class [HttpServer] implement a simple HTTP server. The | 15 * Instances of the class [HttpServer] implement a simple HTTP server. The |
| 15 * primary responsibility of this server is to listen for an UPGRADE request and | 16 * primary responsibility of this server is to listen for an UPGRADE request and |
| 16 * to start an analysis server. | 17 * to start an analysis server. |
| 17 */ | 18 */ |
| 18 class HttpAnalysisServer { | 19 class HttpAnalysisServer { |
| 19 /** | 20 /** |
| 20 * An object that can handle either a WebSocket connection or a connection | 21 * An object that can handle either a WebSocket connection or a connection |
| 21 * to the client over stdio. | 22 * to the client over stdio. |
| 22 */ | 23 */ |
| 23 SocketServer socketServer; | 24 SocketServer socketServer; |
| 24 | 25 |
| 25 /** | 26 /** |
| 26 * An object that can handle GET requests. | 27 * An object that can handle GET requests. |
| 27 */ | 28 */ |
| 28 GetHandler getHandler; | 29 GetHandler getHandler; |
| 29 | 30 |
| 30 /** | 31 /** |
| 31 * Initialize a newly created HTTP server. | 32 * Initialize a newly created HTTP server. |
| 32 */ | 33 */ |
| 33 HttpAnalysisServer(this.socketServer); | 34 HttpAnalysisServer(this.socketServer); |
| 34 | 35 |
| 35 /** | 36 /** |
| 37 * Future that is completed with the HTTP server once it is running. |
| 38 */ |
| 39 Future<HttpServer> _server; |
| 40 |
| 41 /** |
| 36 * Attach a listener to a newly created HTTP server. | 42 * Attach a listener to a newly created HTTP server. |
| 37 */ | 43 */ |
| 38 void _handleServer(HttpServer httServer) { | 44 void _handleServer(HttpServer httServer) { |
| 39 httServer.listen((HttpRequest request) { | 45 httServer.listen((HttpRequest request) { |
| 40 List<String> updateValues = request.headers[HttpHeaders.UPGRADE]; | 46 List<String> updateValues = request.headers[HttpHeaders.UPGRADE]; |
| 41 if (updateValues != null && updateValues.indexOf('websocket') >= 0) { | 47 if (updateValues != null && updateValues.indexOf('websocket') >= 0) { |
| 42 WebSocketTransformer.upgrade(request).then((WebSocket websocket) { | 48 WebSocketTransformer.upgrade(request).then((WebSocket websocket) { |
| 43 _handleWebSocket(websocket); | 49 _handleWebSocket(websocket); |
| 44 }); | 50 }); |
| 45 } else if (request.method == 'GET') { | 51 } else if (request.method == 'GET') { |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 77 response.statusCode = HttpStatus.NOT_FOUND; | 83 response.statusCode = HttpStatus.NOT_FOUND; |
| 78 response.headers.add(HttpHeaders.CONTENT_TYPE, "text/plain"); | 84 response.headers.add(HttpHeaders.CONTENT_TYPE, "text/plain"); |
| 79 response.write('Not found'); | 85 response.write('Not found'); |
| 80 response.close(); | 86 response.close(); |
| 81 } | 87 } |
| 82 | 88 |
| 83 /** | 89 /** |
| 84 * Begin serving HTTP requests over the given port. | 90 * Begin serving HTTP requests over the given port. |
| 85 */ | 91 */ |
| 86 void serveHttp(int port) { | 92 void serveHttp(int port) { |
| 87 HttpServer.bind(InternetAddress.LOOPBACK_IP_V4, port).then(_handleServer); | 93 _server = HttpServer.bind(InternetAddress.LOOPBACK_IP_V4, port); |
| 94 _server.then(_handleServer); |
| 95 } |
| 96 |
| 97 void close() { |
| 98 _server.then((HttpServer server) { |
| 99 server.close(); |
| 100 }); |
| 88 } | 101 } |
| 89 } | 102 } |
| OLD | NEW |