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