Chromium Code Reviews| 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:async'; |
| 8 import 'dart:io'; | 8 import 'dart:io'; |
| 9 | 9 |
| 10 import 'package:analysis_server/src/channel/web_socket_channel.dart'; | 10 import 'package:analysis_server/src/channel/web_socket_channel.dart'; |
| 11 import 'package:analysis_server/src/get_handler.dart'; | 11 import 'package:analysis_server/src/get_handler.dart'; |
| 12 import 'package:analysis_server/src/socket_server.dart'; | 12 import 'package:analysis_server/src/socket_server.dart'; |
| 13 | 13 |
| 14 /** | 14 /** |
| 15 * Instances of the class [HttpServer] implement a simple HTTP server. The | 15 * Instances of the class [HttpServer] implement a simple HTTP server. The |
| 16 * 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 |
| 17 * to start an analysis server. | 17 * to start an analysis server. |
| 18 */ | 18 */ |
| 19 class HttpAnalysisServer { | 19 class HttpAnalysisServer { |
| 20 /** | 20 /** |
| 21 * Number of lines of print output to capture. | |
| 22 */ | |
| 23 static const int PRINT_BUFFER_LENGTH = 1000; | |
|
danrubel
2014/10/28 21:01:02
MAX_PRINT_BUFFER_MAX_LENGTH ?
Paul Berry
2014/10/28 23:29:13
Done.
| |
| 24 | |
| 25 /** | |
| 21 * An object that can handle either a WebSocket connection or a connection | 26 * An object that can handle either a WebSocket connection or a connection |
| 22 * to the client over stdio. | 27 * to the client over stdio. |
| 23 */ | 28 */ |
| 24 SocketServer socketServer; | 29 SocketServer socketServer; |
| 25 | 30 |
| 26 /** | 31 /** |
| 27 * An object that can handle GET requests. | 32 * An object that can handle GET requests. |
| 28 */ | 33 */ |
| 29 GetHandler getHandler; | 34 GetHandler getHandler; |
| 30 | 35 |
| 31 /** | 36 /** |
| 32 * Initialize a newly created HTTP server. | 37 * Initialize a newly created HTTP server. |
| 33 */ | 38 */ |
| 34 HttpAnalysisServer(this.socketServer); | 39 HttpAnalysisServer(this.socketServer); |
| 35 | 40 |
| 36 /** | 41 /** |
| 37 * Future that is completed with the HTTP server once it is running. | 42 * Future that is completed with the HTTP server once it is running. |
| 38 */ | 43 */ |
| 39 Future<HttpServer> _server; | 44 Future<HttpServer> _server; |
| 40 | 45 |
| 41 /** | 46 /** |
| 47 * Last PRINT_BUFFER_LENGTH lines printed. | |
| 48 */ | |
| 49 List<String> _printBuffer = <String>[]; | |
| 50 | |
| 51 /** | |
| 42 * Attach a listener to a newly created HTTP server. | 52 * Attach a listener to a newly created HTTP server. |
| 43 */ | 53 */ |
| 44 void _handleServer(HttpServer httServer) { | 54 void _handleServer(HttpServer httServer) { |
| 45 httServer.listen((HttpRequest request) { | 55 httServer.listen((HttpRequest request) { |
| 46 List<String> updateValues = request.headers[HttpHeaders.UPGRADE]; | 56 List<String> updateValues = request.headers[HttpHeaders.UPGRADE]; |
| 47 if (updateValues != null && updateValues.indexOf('websocket') >= 0) { | 57 if (updateValues != null && updateValues.indexOf('websocket') >= 0) { |
| 48 WebSocketTransformer.upgrade(request).then((WebSocket websocket) { | 58 WebSocketTransformer.upgrade(request).then((WebSocket websocket) { |
| 49 _handleWebSocket(websocket); | 59 _handleWebSocket(websocket); |
| 50 }); | 60 }); |
| 51 } else if (request.method == 'GET') { | 61 } else if (request.method == 'GET') { |
| 52 _handleGetRequest(request); | 62 _handleGetRequest(request); |
| 53 } else { | 63 } else { |
| 54 _returnUnknownRequest(request); | 64 _returnUnknownRequest(request); |
| 55 } | 65 } |
| 56 }); | 66 }); |
| 57 } | 67 } |
| 58 | 68 |
| 59 /** | 69 /** |
| 60 * Handle a GET request received by the HTTP server. | 70 * Handle a GET request received by the HTTP server. |
| 61 */ | 71 */ |
| 62 void _handleGetRequest(HttpRequest request) { | 72 void _handleGetRequest(HttpRequest request) { |
| 63 if (getHandler == null) { | 73 if (getHandler == null) { |
| 64 getHandler = new GetHandler(socketServer); | 74 getHandler = new GetHandler(socketServer, _printBuffer); |
| 65 } | 75 } |
| 66 getHandler.handleGetRequest(request); | 76 getHandler.handleGetRequest(request); |
| 67 } | 77 } |
| 68 | 78 |
| 69 /** | 79 /** |
| 70 * Handle an UPGRADE request received by the HTTP server by creating and | 80 * Handle an UPGRADE request received by the HTTP server by creating and |
| 71 * running an analysis server on a [WebSocket]-based communication channel. | 81 * running an analysis server on a [WebSocket]-based communication channel. |
| 72 */ | 82 */ |
| 73 void _handleWebSocket(WebSocket socket) { | 83 void _handleWebSocket(WebSocket socket) { |
| 74 socketServer.createAnalysisServer(new WebSocketServerChannel(socket)); | 84 socketServer.createAnalysisServer(new WebSocketServerChannel(socket)); |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 92 void serveHttp(int port) { | 102 void serveHttp(int port) { |
| 93 _server = HttpServer.bind(InternetAddress.LOOPBACK_IP_V4, port); | 103 _server = HttpServer.bind(InternetAddress.LOOPBACK_IP_V4, port); |
| 94 _server.then(_handleServer); | 104 _server.then(_handleServer); |
| 95 } | 105 } |
| 96 | 106 |
| 97 void close() { | 107 void close() { |
| 98 _server.then((HttpServer server) { | 108 _server.then((HttpServer server) { |
| 99 server.close(); | 109 server.close(); |
| 100 }); | 110 }); |
| 101 } | 111 } |
| 112 | |
| 113 /** | |
| 114 * Record that the given line was printed out by the analysis server. | |
| 115 */ | |
| 116 void recordPrint(String line) { | |
| 117 _printBuffer.add(line); | |
| 118 if (_printBuffer.length > PRINT_BUFFER_LENGTH) { | |
| 119 _printBuffer.removeRange(0, _printBuffer.length - PRINT_BUFFER_LENGTH); | |
| 120 } | |
| 121 } | |
| 102 } | 122 } |
| OLD | NEW |