OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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 part of vmservice_io; | 5 part of vmservice_io; |
6 | 6 |
7 class WebSocketClient extends Client { | 7 class WebSocketClient extends Client { |
8 static const int PARSE_ERROR_CODE = 4000; | 8 static const int PARSE_ERROR_CODE = 4000; |
9 static const int BINARY_MESSAGE_ERROR_CODE = 4001; | 9 static const int BINARY_MESSAGE_ERROR_CODE = 4001; |
10 static const int NOT_MAP_ERROR_CODE = 4002; | 10 static const int NOT_MAP_ERROR_CODE = 4002; |
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
121 static const ROOT_REDIRECT_PATH = '/index.html'; | 121 static const ROOT_REDIRECT_PATH = '/index.html'; |
122 | 122 |
123 final VMService _service; | 123 final VMService _service; |
124 final String _ip; | 124 final String _ip; |
125 final int _port; | 125 final int _port; |
126 final bool _originCheckDisabled; | 126 final bool _originCheckDisabled; |
127 HttpServer _server; | 127 HttpServer _server; |
128 bool get running => _server != null; | 128 bool get running => _server != null; |
129 bool _displayMessages = false; | 129 bool _displayMessages = false; |
130 | 130 |
| 131 /// Returns the server address including the auth token. |
| 132 Uri get serverAddress { |
| 133 if (!running) { |
| 134 return null; |
| 135 } |
| 136 var ip = _server.address.address; |
| 137 var port = _server.port; |
| 138 return Uri.parse('http://$ip:$port/$serviceAuthToken/'); |
| 139 } |
| 140 |
131 Server(this._service, this._ip, this._port, this._originCheckDisabled) { | 141 Server(this._service, this._ip, this._port, this._originCheckDisabled) { |
132 _displayMessages = (_ip != '127.0.0.1' || _port != 8181); | 142 _displayMessages = (_ip != '127.0.0.1' || _port != 8181); |
133 } | 143 } |
134 | 144 |
135 bool _isAllowedOrigin(String origin) { | 145 bool _isAllowedOrigin(String origin) { |
136 Uri uri; | 146 Uri uri; |
137 try { | 147 try { |
138 uri = Uri.parse(origin); | 148 uri = Uri.parse(origin); |
139 } catch (_) { | 149 } catch (_) { |
140 return false; | 150 return false; |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
224 } | 234 } |
225 request.response.close(); | 235 request.response.close(); |
226 return; | 236 return; |
227 } | 237 } |
228 if (request.method != 'GET') { | 238 if (request.method != 'GET') { |
229 // Not a GET request. Do nothing. | 239 // Not a GET request. Do nothing. |
230 request.response.close(); | 240 request.response.close(); |
231 return; | 241 return; |
232 } | 242 } |
233 | 243 |
| 244 final List<String> requestPathSegments = request.uri.pathSegments; |
| 245 if (requestPathSegments.length < 1) { |
| 246 // Malformed. |
| 247 request.response.close(); |
| 248 return; |
| 249 } |
| 250 // Check that we were given the auth token. |
| 251 final String authToken = requestPathSegments[0]; |
| 252 if (authToken != serviceAuthToken) { |
| 253 // Malformed. |
| 254 request.response.close(); |
| 255 return; |
| 256 } |
| 257 // Construct the actual request path by chopping off the auth token. |
234 final String path = | 258 final String path = |
235 request.uri.path == '/' ? ROOT_REDIRECT_PATH : request.uri.path; | 259 (requestPathSegments.length == 1) ? |
| 260 ROOT_REDIRECT_PATH : '/${requestPathSegments.sublist(1).join('/')}'; |
236 | 261 |
237 if (path == WEBSOCKET_PATH) { | 262 if (path == WEBSOCKET_PATH) { |
238 WebSocketTransformer.upgrade(request).then( | 263 WebSocketTransformer.upgrade(request).then( |
239 (WebSocket webSocket) { | 264 (WebSocket webSocket) { |
240 new WebSocketClient(webSocket, _service); | 265 new WebSocketClient(webSocket, _service); |
241 }); | 266 }); |
242 return; | 267 return; |
243 } | 268 } |
244 | 269 |
245 Asset asset = assets[path]; | 270 Asset asset = assets[path]; |
(...skipping 21 matching lines...) Expand all Loading... |
267 if (_server != null) { | 292 if (_server != null) { |
268 // Already running. | 293 // Already running. |
269 return new Future.value(this); | 294 return new Future.value(this); |
270 } | 295 } |
271 | 296 |
272 var address = new InternetAddress(_ip); | 297 var address = new InternetAddress(_ip); |
273 // Startup HTTP server. | 298 // Startup HTTP server. |
274 return HttpServer.bind(address, _port).then((s) { | 299 return HttpServer.bind(address, _port).then((s) { |
275 _server = s; | 300 _server = s; |
276 _server.listen(_requestHandler, cancelOnError: true); | 301 _server.listen(_requestHandler, cancelOnError: true); |
277 var ip = _server.address.address; | |
278 var port = _server.port; | |
279 if (_displayMessages) { | 302 if (_displayMessages) { |
280 print('Observatory listening on http://$ip:$port'); | 303 print('Observatory listening on $serverAddress'); |
281 } | 304 } |
282 // Server is up and running. | 305 // Server is up and running. |
283 _notifyServerState(ip, _server.port); | 306 _notifyServerState(serverAddress.toString()); |
284 onServerAddressChange('http://$ip:$port'); | 307 onServerAddressChange('$serverAddress'); |
285 return this; | 308 return this; |
286 }).catchError((e, st) { | 309 }).catchError((e, st) { |
287 print('Could not start Observatory HTTP server:\n$e\n$st\n'); | 310 print('Could not start Observatory HTTP server:\n$e\n$st\n'); |
288 _notifyServerState("", 0); | 311 _notifyServerState(""); |
289 onServerAddressChange(null); | 312 onServerAddressChange(null); |
290 return this; | 313 return this; |
291 }); | 314 }); |
292 } | 315 } |
293 | 316 |
294 Future cleanup(bool force) { | 317 Future cleanup(bool force) { |
295 if (_server == null) { | 318 if (_server == null) { |
296 return new Future.value(null); | 319 return new Future.value(null); |
297 } | 320 } |
298 return _server.close(force: force); | 321 return _server.close(force: force); |
299 } | 322 } |
300 | 323 |
301 Future shutdown(bool forced) { | 324 Future shutdown(bool forced) { |
302 if (_server == null) { | 325 if (_server == null) { |
303 // Not started. | 326 // Not started. |
304 return new Future.value(this); | 327 return new Future.value(this); |
305 } | 328 } |
306 | 329 |
307 // Force displaying of status messages if we are forcibly shutdown. | 330 // Force displaying of status messages if we are forcibly shutdown. |
308 _displayMessages = _displayMessages || forced; | 331 _displayMessages = _displayMessages || forced; |
309 | 332 |
310 // Shutdown HTTP server and subscription. | 333 // Shutdown HTTP server and subscription. |
311 var ip = _server.address.address.toString(); | 334 String oldServerAddress = serverAddress; |
312 var port = _server.port.toString(); | |
313 return cleanup(forced).then((_) { | 335 return cleanup(forced).then((_) { |
314 if (_displayMessages) { | 336 if (_displayMessages) { |
315 print('Observatory no longer listening on http://$ip:$port'); | 337 print('Observatory no longer listening on $oldServerAddress'); |
316 } | 338 } |
317 _server = null; | 339 _server = null; |
318 _notifyServerState("", 0); | 340 _notifyServerState(""); |
319 onServerAddressChange(null); | 341 onServerAddressChange(null); |
320 return this; | 342 return this; |
321 }).catchError((e, st) { | 343 }).catchError((e, st) { |
322 _server = null; | 344 _server = null; |
323 print('Could not shutdown Observatory HTTP server:\n$e\n$st\n'); | 345 print('Could not shutdown Observatory HTTP server:\n$e\n$st\n'); |
324 _notifyServerState("", 0); | 346 _notifyServerState(""); |
325 onServerAddressChange(null); | 347 onServerAddressChange(null); |
326 return this; | 348 return this; |
327 }); | 349 }); |
328 } | 350 } |
329 | 351 |
330 } | 352 } |
331 | 353 |
332 void _notifyServerState(String ip, int port) | 354 void _notifyServerState(String uri) |
333 native "VMServiceIO_NotifyServerState"; | 355 native "VMServiceIO_NotifyServerState"; |
OLD | NEW |