| 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 final bool silentObservatory = | 7 final bool silentObservatory = const bool.fromEnvironment('SILENT_OBSERVATORY'); |
| 8 const bool.fromEnvironment('SILENT_OBSERVATORY'); | |
| 9 | 8 |
| 10 void serverPrint(String s) { | 9 void serverPrint(String s) { |
| 11 if (silentObservatory) { | 10 if (silentObservatory) { |
| 12 // We've been requested to be silent. | 11 // We've been requested to be silent. |
| 13 return; | 12 return; |
| 14 } | 13 } |
| 15 print(s); | 14 print(s); |
| 16 } | 15 } |
| 17 | 16 |
| 18 class WebSocketClient extends Client { | 17 class WebSocketClient extends Client { |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 56 } | 55 } |
| 57 } | 56 } |
| 58 | 57 |
| 59 void post(dynamic result) { | 58 void post(dynamic result) { |
| 60 if (result == null) { | 59 if (result == null) { |
| 61 // Do nothing. | 60 // Do nothing. |
| 62 return; | 61 return; |
| 63 } | 62 } |
| 64 try { | 63 try { |
| 65 if (result is String || result is Uint8List) { | 64 if (result is String || result is Uint8List) { |
| 66 socket.add(result); // String or binary message. | 65 socket.add(result); // String or binary message. |
| 67 } else { | 66 } else { |
| 68 // String message as external Uint8List. | 67 // String message as external Uint8List. |
| 69 assert(result is List); | 68 assert(result is List); |
| 70 Uint8List cstring = result[0]; | 69 Uint8List cstring = result[0]; |
| 71 socket.addUtf8Text(cstring); | 70 socket.addUtf8Text(cstring); |
| 72 } | 71 } |
| 73 } catch (e, st) { | 72 } catch (e, st) { |
| 74 serverPrint("Ignoring error posting over WebSocket."); | 73 serverPrint("Ignoring error posting over WebSocket."); |
| 75 serverPrint(e); | 74 serverPrint(e); |
| 76 serverPrint(st); | 75 serverPrint(st); |
| 77 } | 76 } |
| 78 } | 77 } |
| 79 | 78 |
| 80 dynamic toJson() { | 79 dynamic toJson() { |
| 81 Map map = super.toJson(); | 80 Map map = super.toJson(); |
| 82 map['type'] = 'WebSocketClient'; | 81 map['type'] = 'WebSocketClient'; |
| 83 map['socket'] = '$socket'; | 82 map['socket'] = '$socket'; |
| 84 return map; | 83 return map; |
| 85 } | 84 } |
| 86 } | 85 } |
| 87 | 86 |
| 88 | |
| 89 class HttpRequestClient extends Client { | 87 class HttpRequestClient extends Client { |
| 90 static ContentType jsonContentType = | 88 static ContentType jsonContentType = |
| 91 new ContentType("application", "json", charset: "utf-8"); | 89 new ContentType("application", "json", charset: "utf-8"); |
| 92 final HttpRequest request; | 90 final HttpRequest request; |
| 93 | 91 |
| 94 HttpRequestClient(this.request, VMService service) | 92 HttpRequestClient(this.request, VMService service) |
| 95 : super(service, sendEvents:false); | 93 : super(service, sendEvents: false); |
| 96 | 94 |
| 97 disconnect() { | 95 disconnect() { |
| 98 request.response.close(); | 96 request.response.close(); |
| 99 close(); | 97 close(); |
| 100 } | 98 } |
| 101 | 99 |
| 102 void post(dynamic result) { | 100 void post(dynamic result) { |
| 103 if (result == null) { | 101 if (result == null) { |
| 104 close(); | 102 close(); |
| 105 return; | 103 return; |
| 106 } | 104 } |
| 107 HttpResponse response = request.response; | 105 HttpResponse response = request.response; |
| 108 // We closed the connection for bad origins earlier. | 106 // We closed the connection for bad origins earlier. |
| 109 response.headers.add('Access-Control-Allow-Origin', '*'); | 107 response.headers.add('Access-Control-Allow-Origin', '*'); |
| 110 response.headers.contentType = jsonContentType; | 108 response.headers.contentType = jsonContentType; |
| 111 if (result is String) { | 109 if (result is String) { |
| 112 response.write(result); | 110 response.write(result); |
| 113 } else { | 111 } else { |
| 114 assert(result is List); | 112 assert(result is List); |
| 115 Uint8List cstring = result[0]; // Already in UTF-8. | 113 Uint8List cstring = result[0]; // Already in UTF-8. |
| 116 response.add(cstring); | 114 response.add(cstring); |
| 117 } | 115 } |
| 118 response.close(); | 116 response.close(); |
| 119 close(); | 117 close(); |
| 120 } | 118 } |
| 121 | 119 |
| 122 dynamic toJson() { | 120 dynamic toJson() { |
| 123 Map map = super.toJson(); | 121 Map map = super.toJson(); |
| 124 map['type'] = 'HttpRequestClient'; | 122 map['type'] = 'HttpRequestClient'; |
| 125 map['request'] = '$request'; | 123 map['request'] = '$request'; |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 162 // Explicitly add localhost and 127.0.0.1 on any port (necessary for | 160 // Explicitly add localhost and 127.0.0.1 on any port (necessary for |
| 163 // adb port forwarding). | 161 // adb port forwarding). |
| 164 if ((uri.host == 'localhost') || | 162 if ((uri.host == 'localhost') || |
| 165 (uri.host == '::1') || | 163 (uri.host == '::1') || |
| 166 (uri.host == '127.0.0.1')) { | 164 (uri.host == '127.0.0.1')) { |
| 167 return true; | 165 return true; |
| 168 } | 166 } |
| 169 | 167 |
| 170 if ((uri.port == _server.port) && | 168 if ((uri.port == _server.port) && |
| 171 ((uri.host == _server.address.address) || | 169 ((uri.host == _server.address.address) || |
| 172 (uri.host == _server.address.host))) { | 170 (uri.host == _server.address.host))) { |
| 173 return true; | 171 return true; |
| 174 } | 172 } |
| 175 | 173 |
| 176 return false; | 174 return false; |
| 177 } | 175 } |
| 178 | 176 |
| 179 bool _originCheck(HttpRequest request) { | 177 bool _originCheck(HttpRequest request) { |
| 180 if (_originCheckDisabled || Platform.isFuchsia) { | 178 if (_originCheckDisabled || Platform.isFuchsia) { |
| 181 // Always allow. | 179 // Always allow. |
| 182 return true; | 180 return true; |
| (...skipping 27 matching lines...) Expand all Loading... |
| 210 // Malformed. | 208 // Malformed. |
| 211 return null; | 209 return null; |
| 212 } | 210 } |
| 213 // Check that we were given the auth token. | 211 // Check that we were given the auth token. |
| 214 final String authToken = requestPathSegments[0]; | 212 final String authToken = requestPathSegments[0]; |
| 215 if (authToken != serviceAuthToken) { | 213 if (authToken != serviceAuthToken) { |
| 216 // Malformed. | 214 // Malformed. |
| 217 return null; | 215 return null; |
| 218 } | 216 } |
| 219 // Construct the actual request path by chopping off the auth token. | 217 // Construct the actual request path by chopping off the auth token. |
| 220 return (requestPathSegments[1] == '') ? | 218 return (requestPathSegments[1] == '') |
| 221 ROOT_REDIRECT_PATH : '/${requestPathSegments.sublist(1).join('/')}'; | 219 ? ROOT_REDIRECT_PATH |
| 220 : '/${requestPathSegments.sublist(1).join('/')}'; |
| 222 } | 221 } |
| 223 | 222 |
| 224 Future _requestHandler(HttpRequest request) async { | 223 Future _requestHandler(HttpRequest request) async { |
| 225 if (!_originCheck(request)) { | 224 if (!_originCheck(request)) { |
| 226 // This is a cross origin attempt to connect | 225 // This is a cross origin attempt to connect |
| 227 request.response.close(); | 226 request.response.close(); |
| 228 return; | 227 return; |
| 229 } | 228 } |
| 230 if (request.method == 'PUT') { | 229 if (request.method == 'PUT') { |
| 231 // PUT requests are forwarded to DevFS for processing. | 230 // PUT requests are forwarded to DevFS for processing. |
| (...skipping 22 matching lines...) Expand all Loading... |
| 254 if (fsUri == null) { | 253 if (fsUri == null) { |
| 255 fsPathList = request.headers['dev_fs_path']; | 254 fsPathList = request.headers['dev_fs_path']; |
| 256 fsPathBase64List = request.headers['dev_fs_path_b64']; | 255 fsPathBase64List = request.headers['dev_fs_path_b64']; |
| 257 // If the 'dev_fs_path_b64' header field was sent, use that instead. | 256 // If the 'dev_fs_path_b64' header field was sent, use that instead. |
| 258 if ((fsPathBase64List != null) && (fsPathBase64List.length > 0)) { | 257 if ((fsPathBase64List != null) && (fsPathBase64List.length > 0)) { |
| 259 fsPath = UTF8.decode(BASE64.decode(fsPathBase64List[0])); | 258 fsPath = UTF8.decode(BASE64.decode(fsPathBase64List[0])); |
| 260 } else { | 259 } else { |
| 261 fsPath = fsPathList[0]; | 260 fsPath = fsPathList[0]; |
| 262 } | 261 } |
| 263 } | 262 } |
| 264 } catch (e) { /* ignore */ } | 263 } catch (e) {/* ignore */} |
| 265 | 264 |
| 266 String result; | 265 String result; |
| 267 try { | 266 try { |
| 268 result = await _service.devfs.handlePutStream( | 267 result = await _service.devfs.handlePutStream( |
| 269 fsName, | 268 fsName, fsPath, fsUri, request.transform(GZIP.decoder)); |
| 270 fsPath, | 269 } catch (e) {/* ignore */} |
| 271 fsUri, | |
| 272 request.transform(GZIP.decoder)); | |
| 273 } catch (e) { /* ignore */ } | |
| 274 | 270 |
| 275 if (result != null) { | 271 if (result != null) { |
| 276 request.response.headers.contentType = | 272 request.response.headers.contentType = |
| 277 HttpRequestClient.jsonContentType; | 273 HttpRequestClient.jsonContentType; |
| 278 request.response.write(result); | 274 request.response.write(result); |
| 279 } | 275 } |
| 280 request.response.close(); | 276 request.response.close(); |
| 281 return; | 277 return; |
| 282 } | 278 } |
| 283 if (request.method != 'GET') { | 279 if (request.method != 'GET') { |
| 284 // Not a GET request. Do nothing. | 280 // Not a GET request. Do nothing. |
| 285 request.response.close(); | 281 request.response.close(); |
| 286 return; | 282 return; |
| 287 } | 283 } |
| 288 | 284 |
| 289 final String path = _checkAuthTokenAndGetPath(request.uri); | 285 final String path = _checkAuthTokenAndGetPath(request.uri); |
| 290 if (path == null) { | 286 if (path == null) { |
| 291 // Malformed. | 287 // Malformed. |
| 292 request.response.close(); | 288 request.response.close(); |
| 293 return; | 289 return; |
| 294 } | 290 } |
| 295 | 291 |
| 296 if (path == WEBSOCKET_PATH) { | 292 if (path == WEBSOCKET_PATH) { |
| 297 WebSocketTransformer.upgrade(request).then( | 293 WebSocketTransformer.upgrade(request).then((WebSocket webSocket) { |
| 298 (WebSocket webSocket) { | |
| 299 new WebSocketClient(webSocket, _service); | 294 new WebSocketClient(webSocket, _service); |
| 300 }); | 295 }); |
| 301 return; | 296 return; |
| 302 } | 297 } |
| 303 | 298 |
| 304 Asset asset = assets[path]; | 299 Asset asset = assets[path]; |
| 305 if (asset != null) { | 300 if (asset != null) { |
| 306 // Serving up a static asset (e.g. .css, .html, .png). | 301 // Serving up a static asset (e.g. .css, .html, .png). |
| 307 request.response.headers.contentType = | 302 request.response.headers.contentType = ContentType.parse(asset.mimeType); |
| 308 ContentType.parse(asset.mimeType); | |
| 309 request.response.add(asset.data); | 303 request.response.add(asset.data); |
| 310 request.response.close(); | 304 request.response.close(); |
| 311 return; | 305 return; |
| 312 } | 306 } |
| 313 // HTTP based service request. | 307 // HTTP based service request. |
| 314 try { | 308 try { |
| 315 var client = new HttpRequestClient(request, _service); | 309 var client = new HttpRequestClient(request, _service); |
| 316 var message = new Message.fromUri(client, request.uri); | 310 var message = new Message.fromUri(client, request.uri); |
| 317 client.onMessage(null, message); | 311 client.onMessage(null, message); |
| 318 } catch (e) { | 312 } catch (e) { |
| 319 serverPrint('Unexpected error processing HTTP request uri: ' | 313 serverPrint('Unexpected error processing HTTP request uri: ' |
| 320 '${request.uri}\n$e\n'); | 314 '${request.uri}\n$e\n'); |
| 321 rethrow; | 315 rethrow; |
| 322 } | 316 } |
| 323 } | 317 } |
| 324 | 318 |
| 325 Future startup() async { | 319 Future startup() async { |
| 326 if (_server != null) { | 320 if (_server != null) { |
| 327 // Already running. | 321 // Already running. |
| 328 return this; | 322 return this; |
| 329 } | 323 } |
| 330 | 324 |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 392 onServerAddressChange(null); | 386 onServerAddressChange(null); |
| 393 return this; | 387 return this; |
| 394 }).catchError((e, st) { | 388 }).catchError((e, st) { |
| 395 _server = null; | 389 _server = null; |
| 396 serverPrint('Could not shutdown Observatory HTTP server:\n$e\n$st\n'); | 390 serverPrint('Could not shutdown Observatory HTTP server:\n$e\n$st\n'); |
| 397 _notifyServerState(""); | 391 _notifyServerState(""); |
| 398 onServerAddressChange(null); | 392 onServerAddressChange(null); |
| 399 return this; | 393 return this; |
| 400 }); | 394 }); |
| 401 } | 395 } |
| 402 | |
| 403 } | 396 } |
| 404 | 397 |
| 405 void _notifyServerState(String uri) | 398 void _notifyServerState(String uri) native "VMServiceIO_NotifyServerState"; |
| 406 native "VMServiceIO_NotifyServerState"; | |
| OLD | NEW |