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:io'; | 7 import 'dart:io'; |
8 | 8 |
9 import 'package:analysis_server/src/analysis_server.dart'; | 9 import 'package:analysis_server/src/analysis_server.dart'; |
10 import 'package:analysis_server/src/channel.dart'; | 10 import 'package:analysis_server/src/channel.dart'; |
11 import 'package:analysis_server/src/domain_context.dart'; | 11 import 'package:analysis_server/src/domain_context.dart'; |
12 import 'package:analysis_server/src/domain_server.dart'; | 12 import 'package:analysis_server/src/domain_server.dart'; |
13 import 'package:analysis_server/src/get_handler.dart'; | 13 import 'package:analysis_server/src/get_handler.dart'; |
14 import 'package:args/args.dart'; | 14 import 'package:args/args.dart'; |
15 import 'package:analysis_server/src/protocol.dart'; | |
Brian Wilkerson
2014/04/14 21:25:43
nit: I believe that we're generally keeping the im
| |
15 | 16 |
16 /** | 17 /** |
17 * Instances of the class [HttpServer] implement a simple HTTP server. The | 18 * Instances of the class [HttpServer] implement a simple HTTP server. The |
18 * primary responsibility of this server is to listen for an UPGRADE request and | 19 * primary responsibility of this server is to listen for an UPGRADE request and |
19 * to start an analysis server. | 20 * to start an analysis server. |
20 */ | 21 */ |
21 class HttpAnalysisServer { | 22 class HttpAnalysisServer { |
22 /** | 23 /** |
23 * The name of the application that is used to start a server. | 24 * The name of the application that is used to start a server. |
24 */ | 25 */ |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
91 } | 92 } |
92 } | 93 } |
93 | 94 |
94 /** | 95 /** |
95 * Attach a listener to a newly created HTTP server. | 96 * Attach a listener to a newly created HTTP server. |
96 */ | 97 */ |
97 void _handleServer(HttpServer httServer) { | 98 void _handleServer(HttpServer httServer) { |
98 httServer.listen((HttpRequest request) { | 99 httServer.listen((HttpRequest request) { |
99 List<String> updateValues = request.headers[HttpHeaders.UPGRADE]; | 100 List<String> updateValues = request.headers[HttpHeaders.UPGRADE]; |
100 if (updateValues != null && updateValues.indexOf('websocket') >= 0) { | 101 if (updateValues != null && updateValues.indexOf('websocket') >= 0) { |
101 if (analysisServer != null) { | |
102 _returnServerAlreadyStarted(request); | |
103 return; | |
104 } | |
105 WebSocketTransformer.upgrade(request).then((WebSocket websocket) { | 102 WebSocketTransformer.upgrade(request).then((WebSocket websocket) { |
106 _handleWebSocket(websocket); | 103 _handleWebSocket(websocket); |
107 }); | 104 }); |
108 } else if (request.method == 'GET') { | 105 } else if (request.method == 'GET') { |
109 _handleGetRequest(request); | 106 _handleGetRequest(request); |
110 } else { | 107 } else { |
111 _returnUnknownRequest(request); | 108 _returnUnknownRequest(request); |
112 } | 109 } |
113 }); | 110 }); |
114 } | 111 } |
115 | 112 |
116 /** | 113 /** |
117 * Handle a GET request received by the HTTP server. | 114 * Handle a GET request received by the HTTP server. |
118 */ | 115 */ |
119 void _handleGetRequest(HttpRequest request) { | 116 void _handleGetRequest(HttpRequest request) { |
120 if (getHandler == null) { | 117 if (getHandler == null) { |
121 getHandler = new GetHandler(); | 118 getHandler = new GetHandler(); |
122 getHandler.server = analysisServer; | 119 getHandler.server = analysisServer; |
123 } | 120 } |
124 getHandler.handleGetRequest(request); | 121 getHandler.handleGetRequest(request); |
125 } | 122 } |
126 | 123 |
127 /** | 124 /** |
128 * Handle an UPGRADE request received by the HTTP server by creating and | 125 * Handle an UPGRADE request received by the HTTP server by creating and |
129 * running an analysis server on a [WebSocket]-based communication channel. | 126 * running an analysis server on a [WebSocket]-based communication channel. |
130 */ | 127 */ |
131 void _handleWebSocket(WebSocket socket) { | 128 void _handleWebSocket(WebSocket socket) { |
129 ServerCommunicationChannel serverChannel = new WebSocketServerChannel(socket ); | |
130 if (analysisServer != null) { | |
131 // TODO(paulberry): add a message to the protocol so that the server can | |
132 // inform the client of a successful connection. | |
133 var error = new RequestError.serverAlreadyStarted(); | |
134 serverChannel.sendResponse(new Response('', error)); | |
135 serverChannel.listen((Request request) { | |
136 serverChannel.sendResponse(new Response(request.id, error)); | |
137 }); | |
138 return; | |
139 } | |
132 analysisServer = new AnalysisServer(new WebSocketServerChannel(socket)); | 140 analysisServer = new AnalysisServer(new WebSocketServerChannel(socket)); |
133 _initializeHandlers(analysisServer); | 141 _initializeHandlers(analysisServer); |
134 if (getHandler != null) { | 142 if (getHandler != null) { |
135 getHandler.server = analysisServer; | 143 getHandler.server = analysisServer; |
136 } | 144 } |
137 analysisServer.run(); | 145 analysisServer.run(); |
138 } | 146 } |
139 | 147 |
140 /** | 148 /** |
141 * Initialize the handlers to be used by the given [server]. | 149 * Initialize the handlers to be used by the given [server]. |
142 */ | 150 */ |
143 void _initializeHandlers(AnalysisServer server) { | 151 void _initializeHandlers(AnalysisServer server) { |
144 server.handlers = [ | 152 server.handlers = [ |
145 new ServerDomainHandler(server), | 153 new ServerDomainHandler(server), |
146 new ContextDomainHandler(server), | 154 new ContextDomainHandler(server), |
147 ]; | 155 ]; |
148 } | 156 } |
149 | 157 |
150 /** | 158 /** |
151 * Print information about how to use the server. | 159 * Print information about how to use the server. |
152 */ | 160 */ |
153 void _printUsage(ArgParser parser) { | 161 void _printUsage(ArgParser parser) { |
154 print('Usage: $BINARY_NAME [flags]'); | 162 print('Usage: $BINARY_NAME [flags]'); |
155 print(''); | 163 print(''); |
156 print('Supported flags are:'); | 164 print('Supported flags are:'); |
157 print(parser.getUsage()); | 165 print(parser.getUsage()); |
158 } | 166 } |
159 | 167 |
160 /** | 168 /** |
161 * Return an error in response to an UPGRADE request received after the server | |
162 * has already been started by a previous UPGRADE request. | |
163 */ | |
164 void _returnServerAlreadyStarted(HttpRequest request) { | |
165 HttpResponse response = request.response; | |
166 response.statusCode = HttpStatus.SERVICE_UNAVAILABLE; | |
167 response.headers.add(HttpHeaders.CONTENT_TYPE, "text/plain"); | |
168 response.write('The server has already been started'); | |
169 response.close(); | |
170 } | |
171 | |
172 /** | |
173 * Return an error in response to an unrecognized request received by the HTTP | 169 * Return an error in response to an unrecognized request received by the HTTP |
174 * server. | 170 * server. |
175 */ | 171 */ |
176 void _returnUnknownRequest(HttpRequest request) { | 172 void _returnUnknownRequest(HttpRequest request) { |
177 HttpResponse response = request.response; | 173 HttpResponse response = request.response; |
178 response.statusCode = HttpStatus.NOT_FOUND; | 174 response.statusCode = HttpStatus.NOT_FOUND; |
179 response.headers.add(HttpHeaders.CONTENT_TYPE, "text/plain"); | 175 response.headers.add(HttpHeaders.CONTENT_TYPE, "text/plain"); |
180 response.write('Not found'); | 176 response.write('Not found'); |
181 response.close(); | 177 response.close(); |
182 } | 178 } |
183 } | 179 } |
OLD | NEW |