| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 library http.server; | |
| 6 | |
| 7 import 'dart:async'; | |
| 8 import 'dart:io'; | |
| 9 | |
| 10 import 'package:analysis_server/src/channel/web_socket_channel.dart'; | |
| 11 import 'package:analysis_server/src/get_handler.dart'; | |
| 12 import 'package:analysis_server/src/socket_server.dart'; | |
| 13 | |
| 14 /** | |
| 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 | |
| 17 * to start an analysis server. | |
| 18 */ | |
| 19 class HttpAnalysisServer { | |
| 20 /** | |
| 21 * Number of lines of print output to capture. | |
| 22 */ | |
| 23 static const int MAX_PRINT_BUFFER_LENGTH = 1000; | |
| 24 | |
| 25 /** | |
| 26 * An object that can handle either a WebSocket connection or a connection | |
| 27 * to the client over stdio. | |
| 28 */ | |
| 29 SocketServer socketServer; | |
| 30 | |
| 31 /** | |
| 32 * An object that can handle GET requests. | |
| 33 */ | |
| 34 GetHandler getHandler; | |
| 35 | |
| 36 /** | |
| 37 * Future that is completed with the HTTP server once it is running. | |
| 38 */ | |
| 39 Future<HttpServer> _server; | |
| 40 | |
| 41 /** | |
| 42 * Last PRINT_BUFFER_LENGTH lines printed. | |
| 43 */ | |
| 44 List<String> _printBuffer = <String>[]; | |
| 45 | |
| 46 /** | |
| 47 * Initialize a newly created HTTP server. | |
| 48 */ | |
| 49 HttpAnalysisServer(this.socketServer); | |
| 50 | |
| 51 void close() { | |
| 52 _server.then((HttpServer server) { | |
| 53 server.close(); | |
| 54 }); | |
| 55 } | |
| 56 | |
| 57 /** | |
| 58 * Record that the given line was printed out by the analysis server. | |
| 59 */ | |
| 60 void recordPrint(String line) { | |
| 61 _printBuffer.add(line); | |
| 62 if (_printBuffer.length > MAX_PRINT_BUFFER_LENGTH) { | |
| 63 _printBuffer.removeRange( | |
| 64 0, | |
| 65 _printBuffer.length - MAX_PRINT_BUFFER_LENGTH); | |
| 66 } | |
| 67 } | |
| 68 | |
| 69 /** | |
| 70 * Begin serving HTTP requests over the given port. | |
| 71 */ | |
| 72 void serveHttp(int port) { | |
| 73 _server = HttpServer.bind(InternetAddress.LOOPBACK_IP_V4, port); | |
| 74 _server.then(_handleServer); | |
| 75 } | |
| 76 | |
| 77 /** | |
| 78 * Handle a GET request received by the HTTP server. | |
| 79 */ | |
| 80 void _handleGetRequest(HttpRequest request) { | |
| 81 if (getHandler == null) { | |
| 82 getHandler = new GetHandler(socketServer, _printBuffer); | |
| 83 } | |
| 84 getHandler.handleGetRequest(request); | |
| 85 } | |
| 86 | |
| 87 /** | |
| 88 * Attach a listener to a newly created HTTP server. | |
| 89 */ | |
| 90 void _handleServer(HttpServer httServer) { | |
| 91 httServer.listen((HttpRequest request) { | |
| 92 List<String> updateValues = request.headers[HttpHeaders.UPGRADE]; | |
| 93 if (updateValues != null && updateValues.indexOf('websocket') >= 0) { | |
| 94 WebSocketTransformer.upgrade(request).then((WebSocket websocket) { | |
| 95 _handleWebSocket(websocket); | |
| 96 }); | |
| 97 } else if (request.method == 'GET') { | |
| 98 _handleGetRequest(request); | |
| 99 } else { | |
| 100 _returnUnknownRequest(request); | |
| 101 } | |
| 102 }); | |
| 103 } | |
| 104 | |
| 105 /** | |
| 106 * Handle an UPGRADE request received by the HTTP server by creating and | |
| 107 * running an analysis server on a [WebSocket]-based communication channel. | |
| 108 */ | |
| 109 void _handleWebSocket(WebSocket socket) { | |
| 110 socketServer.createAnalysisServer( | |
| 111 new WebSocketServerChannel(socket, socketServer.instrumentationService))
; | |
| 112 } | |
| 113 | |
| 114 /** | |
| 115 * Return an error in response to an unrecognized request received by the HTTP | |
| 116 * server. | |
| 117 */ | |
| 118 void _returnUnknownRequest(HttpRequest request) { | |
| 119 HttpResponse response = request.response; | |
| 120 response.statusCode = HttpStatus.NOT_FOUND; | |
| 121 response.headers.add(HttpHeaders.CONTENT_TYPE, "text/plain"); | |
| 122 response.write('Not found'); | |
| 123 response.close(); | |
| 124 } | |
| 125 } | |
| OLD | NEW |