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'; |
(...skipping 21 matching lines...) Expand all Loading... |
32 /** | 32 /** |
33 * The name of the option used to specify the port to which the server will | 33 * The name of the option used to specify the port to which the server will |
34 * connect. | 34 * connect. |
35 */ | 35 */ |
36 static const String PORT_OPTION = "port"; | 36 static const String PORT_OPTION = "port"; |
37 | 37 |
38 /** | 38 /** |
39 * The analysis server that was created when an UPGRADE request was received, | 39 * The analysis server that was created when an UPGRADE request was received, |
40 * or `null` if no such request has yet been received. | 40 * or `null` if no such request has yet been received. |
41 */ | 41 */ |
42 AnalysisServer server; | 42 AnalysisServer analysisServer; |
43 | 43 |
44 /** | 44 /** |
45 * An object that can handle GET requests. | 45 * An object that can handle GET requests. |
46 */ | 46 */ |
47 GetHandler getHandler; | 47 GetHandler getHandler; |
48 | 48 |
49 /** | 49 /** |
50 * Initialize a newly created HTTP server. | 50 * Initialize a newly created HTTP server. |
51 */ | 51 */ |
52 HttpAnalysisServer(); | 52 HttpAnalysisServer(); |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
87 print(''); | 87 print(''); |
88 _printUsage(parser); | 88 _printUsage(parser); |
89 exitCode = 1; | 89 exitCode = 1; |
90 return; | 90 return; |
91 } | 91 } |
92 } | 92 } |
93 | 93 |
94 /** | 94 /** |
95 * Attach a listener to a newly created HTTP server. | 95 * Attach a listener to a newly created HTTP server. |
96 */ | 96 */ |
97 void _handleServer(HttpServer server) { | 97 void _handleServer(HttpServer httServer) { |
98 server.listen((HttpRequest request) { | 98 httServer.listen((HttpRequest request) { |
99 List<String> updateValues = request.headers[HttpHeaders.UPGRADE]; | 99 List<String> updateValues = request.headers[HttpHeaders.UPGRADE]; |
100 if (updateValues != null && updateValues.indexOf('websocket') >= 0) { | 100 if (updateValues != null && updateValues.indexOf('websocket') >= 0) { |
101 if (server != null) { | 101 if (analysisServer != null) { |
102 _returnServerAlreadyStarted(request); | 102 _returnServerAlreadyStarted(request); |
103 return; | 103 return; |
104 } | 104 } |
105 WebSocketTransformer.upgrade(request).then((WebSocket websocket) { | 105 WebSocketTransformer.upgrade(request).then((WebSocket websocket) { |
106 _handleWebSocket(websocket); | 106 _handleWebSocket(websocket); |
107 }); | 107 }); |
108 } else if (request.method == 'GET') { | 108 } else if (request.method == 'GET') { |
109 _handleGetRequest(request); | 109 _handleGetRequest(request); |
110 } else { | 110 } else { |
111 _returnUnknownRequest(request); | 111 _returnUnknownRequest(request); |
112 } | 112 } |
113 }); | 113 }); |
114 } | 114 } |
115 | 115 |
116 /** | 116 /** |
117 * Handle a GET request received by the HTTP server. | 117 * Handle a GET request received by the HTTP server. |
118 */ | 118 */ |
119 void _handleGetRequest(HttpRequest request) { | 119 void _handleGetRequest(HttpRequest request) { |
120 if (getHandler == null) { | 120 if (getHandler == null) { |
121 getHandler = new GetHandler(); | 121 getHandler = new GetHandler(); |
122 getHandler.server = server; | 122 getHandler.server = analysisServer; |
123 } | 123 } |
124 getHandler.handleGetRequest(request); | 124 getHandler.handleGetRequest(request); |
125 } | 125 } |
126 | 126 |
127 /** | 127 /** |
128 * Handle an UPGRADE request received by the HTTP server by creating and | 128 * Handle an UPGRADE request received by the HTTP server by creating and |
129 * running an analysis server on a [WebSocket]-based communication channel. | 129 * running an analysis server on a [WebSocket]-based communication channel. |
130 */ | 130 */ |
131 void _handleWebSocket(WebSocket socket) { | 131 void _handleWebSocket(WebSocket socket) { |
132 server = new AnalysisServer(new WebSocketChannel(socket)); | 132 analysisServer = new AnalysisServer(new WebSocketChannel(socket)); |
133 _initializeHandlers(server); | 133 _initializeHandlers(analysisServer); |
134 if (getHandler != null) { | 134 if (getHandler != null) { |
135 getHandler.server = server; | 135 getHandler.server = analysisServer; |
136 } | 136 } |
137 server.run(); | 137 analysisServer.run(); |
138 } | 138 } |
139 | 139 |
140 /** | 140 /** |
141 * Initialize the handlers to be used by the given [server]. | 141 * Initialize the handlers to be used by the given [server]. |
142 */ | 142 */ |
143 void _initializeHandlers(AnalysisServer server) { | 143 void _initializeHandlers(AnalysisServer server) { |
144 server.handlers = [ | 144 server.handlers = [ |
145 new ServerDomainHandler(server), | 145 new ServerDomainHandler(server), |
146 new ContextDomainHandler(server), | 146 new ContextDomainHandler(server), |
147 ]; | 147 ]; |
(...skipping 26 matching lines...) Expand all Loading... |
174 * server. | 174 * server. |
175 */ | 175 */ |
176 void _returnUnknownRequest(HttpRequest request) { | 176 void _returnUnknownRequest(HttpRequest request) { |
177 HttpResponse response = request.response; | 177 HttpResponse response = request.response; |
178 response.statusCode = HttpStatus.NOT_FOUND; | 178 response.statusCode = HttpStatus.NOT_FOUND; |
179 response.headers.add(HttpHeaders.CONTENT_TYPE, "text/plain"); | 179 response.headers.add(HttpHeaders.CONTENT_TYPE, "text/plain"); |
180 response.write('Not found'); | 180 response.write('Not found'); |
181 response.close(); | 181 response.close(); |
182 } | 182 } |
183 } | 183 } |
OLD | NEW |