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 = const bool.fromEnvironment('SILENT_OBSERVATORY'); |
8 | 8 |
9 void serverPrint(String s) { | 9 void serverPrint(String s) { |
10 if (silentObservatory) { | 10 if (silentObservatory) { |
(...skipping 27 matching lines...) Expand all Loading... |
38 try { | 38 try { |
39 map = JSON.decode(message); | 39 map = JSON.decode(message); |
40 } catch (e) { | 40 } catch (e) { |
41 socket.close(PARSE_ERROR_CODE, 'Message parse error: $e'); | 41 socket.close(PARSE_ERROR_CODE, 'Message parse error: $e'); |
42 return; | 42 return; |
43 } | 43 } |
44 if (map is! Map) { | 44 if (map is! Map) { |
45 socket.close(NOT_MAP_ERROR_CODE, 'Message must be a JSON map.'); | 45 socket.close(NOT_MAP_ERROR_CODE, 'Message must be a JSON map.'); |
46 return; | 46 return; |
47 } | 47 } |
48 var serial = map['id']; | 48 try { |
49 if (serial != null && serial is! num && serial is! String) { | 49 final rpc = new Message.fromJsonRpc(this, map); |
50 socket.close(ID_ERROR_CODE, '"id" must be a number, string, or null.'); | 50 switch (rpc.type) { |
| 51 case MessageType.Request: |
| 52 onRequest(rpc); |
| 53 break; |
| 54 case MessageType.Notification: |
| 55 onNotification(rpc); |
| 56 break; |
| 57 case MessageType.Response: |
| 58 onResponse(rpc); |
| 59 break; |
| 60 } |
| 61 } catch (e) { |
| 62 socket.close(ID_ERROR_CODE, e.message); |
51 } | 63 } |
52 onMessage(serial, new Message.fromJsonRpc(this, map)); | |
53 } else { | 64 } else { |
54 socket.close(BINARY_MESSAGE_ERROR_CODE, 'Message must be a string.'); | 65 socket.close(BINARY_MESSAGE_ERROR_CODE, 'Message must be a string.'); |
55 } | 66 } |
56 } | 67 } |
57 | 68 |
58 void post(dynamic result) { | 69 void post(dynamic result) { |
59 if (result == null) { | 70 if (result == null) { |
60 // Do nothing. | 71 // Do nothing. |
61 return; | 72 return; |
62 } | 73 } |
63 try { | 74 try { |
64 if (result is String || result is Uint8List) { | 75 if (result is String || result is Uint8List) { |
65 socket.add(result); // String or binary message. | 76 socket.add(result); // String or binary message. |
66 } else { | 77 } else { |
67 // String message as external Uint8List. | 78 // String message as external Uint8List. |
68 assert(result is List); | 79 assert(result is List); |
69 Uint8List cstring = result[0]; | 80 Uint8List cstring = result[0]; |
70 socket.addUtf8Text(cstring); | 81 socket.addUtf8Text(cstring); |
71 } | 82 } |
72 } catch (e, st) { | 83 } catch (e, st) { |
73 serverPrint("Ignoring error posting over WebSocket."); | 84 serverPrint("Ignoring error posting over WebSocket."); |
74 serverPrint(e); | 85 serverPrint(e.toString()); |
75 serverPrint(st); | 86 serverPrint(st.toString()); |
76 } | 87 } |
77 } | 88 } |
78 | 89 |
79 dynamic toJson() { | 90 dynamic toJson() { |
80 Map map = super.toJson(); | 91 Map map = super.toJson(); |
81 map['type'] = 'WebSocketClient'; | 92 map['type'] = 'WebSocketClient'; |
82 map['socket'] = '$socket'; | 93 map['socket'] = '$socket'; |
83 return map; | 94 return map; |
84 } | 95 } |
85 } | 96 } |
(...skipping 218 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
304 } | 315 } |
305 Asset asset = assets[path]; | 316 Asset asset = assets[path]; |
306 if (asset != null) { | 317 if (asset != null) { |
307 // Serving up a static asset (e.g. .css, .html, .png). | 318 // Serving up a static asset (e.g. .css, .html, .png). |
308 request.response.headers.contentType = ContentType.parse(asset.mimeType); | 319 request.response.headers.contentType = ContentType.parse(asset.mimeType); |
309 request.response.add(asset.data); | 320 request.response.add(asset.data); |
310 request.response.close(); | 321 request.response.close(); |
311 return; | 322 return; |
312 } | 323 } |
313 // HTTP based service request. | 324 // HTTP based service request. |
314 try { | 325 final client = new HttpRequestClient(request, _service); |
315 var client = new HttpRequestClient(request, _service); | 326 final message = new Message.fromUri(client, request.uri); |
316 var message = new Message.fromUri(client, request.uri); | 327 client.onRequest(message); // exception free, no need to try catch |
317 client.onMessage(null, message); | |
318 } catch (e) { | |
319 serverPrint('Unexpected error processing HTTP request uri: ' | |
320 '${request.uri}\n$e\n'); | |
321 rethrow; | |
322 } | |
323 } | 328 } |
324 | 329 |
325 Future startup() async { | 330 Future startup() async { |
326 if (_server != null) { | 331 if (_server != null) { |
327 // Already running. | 332 // Already running. |
328 return this; | 333 return this; |
329 } | 334 } |
330 | 335 |
331 // Startup HTTP server. | 336 // Startup HTTP server. |
332 var pollError; | 337 var pollError; |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
415 _server = null; | 420 _server = null; |
416 serverPrint('Could not shutdown Observatory HTTP server:\n$e\n$st\n'); | 421 serverPrint('Could not shutdown Observatory HTTP server:\n$e\n$st\n'); |
417 _notifyServerState(""); | 422 _notifyServerState(""); |
418 onServerAddressChange(null); | 423 onServerAddressChange(null); |
419 return this; | 424 return this; |
420 }); | 425 }); |
421 } | 426 } |
422 } | 427 } |
423 | 428 |
424 void _notifyServerState(String uri) native "VMServiceIO_NotifyServerState"; | 429 void _notifyServerState(String uri) native "VMServiceIO_NotifyServerState"; |
OLD | NEW |