| 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 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 73 dynamic toJson() { | 73 dynamic toJson() { |
| 74 Map map = super.toJson(); | 74 Map map = super.toJson(); |
| 75 map['type'] = 'HttpRequestClient'; | 75 map['type'] = 'HttpRequestClient'; |
| 76 map['request'] = '$request'; | 76 map['request'] = '$request'; |
| 77 return map; | 77 return map; |
| 78 } | 78 } |
| 79 } | 79 } |
| 80 | 80 |
| 81 class Server { | 81 class Server { |
| 82 static const WEBSOCKET_PATH = '/ws'; | 82 static const WEBSOCKET_PATH = '/ws'; |
| 83 String observatoryPath = '/index.html'; | 83 static const ROOT_REDIRECT_PATH = '/index.html'; |
| 84 final String ip; | |
| 85 int port; | |
| 86 | 84 |
| 87 final VMService service; | 85 final VMService _service; |
| 86 final String _ip; |
| 87 final int _port; |
| 88 |
| 88 HttpServer _server; | 89 HttpServer _server; |
| 90 bool get running => _server != null; |
| 91 bool _displayMessages = false; |
| 89 | 92 |
| 90 Server(this.service, this.ip, this.port); | 93 Server(this._service, this._ip, this._port) { |
| 94 _displayMessages = (_ip != '127.0.0.1' || _port != 8181); |
| 95 } |
| 91 | 96 |
| 92 bool _shouldServeObservatory(HttpRequest request) { | 97 bool _shouldServeObservatory(HttpRequest request) { |
| 93 if (request.headers['Observatory-Version'] != null) { | 98 if (request.headers['Observatory-Version'] != null) { |
| 94 // Request is already coming from Observatory. | 99 // Request is already coming from Observatory. |
| 95 return false; | 100 return false; |
| 96 } | 101 } |
| 97 // TODO(johnmccutchan): Test with obscure browsers. | 102 // TODO(johnmccutchan): Test with obscure browsers. |
| 98 if (request.headers.value(HttpHeaders.USER_AGENT).contains('Mozilla')) { | 103 if (request.headers.value(HttpHeaders.USER_AGENT).contains('Mozilla')) { |
| 99 // Request is coming from a browser but not Observatory application. | 104 // Request is coming from a browser but not Observatory application. |
| 100 // Serve Observatory and let the Observatory make the real request. | 105 // Serve Observatory and let the Observatory make the real request. |
| 101 return true; | 106 return true; |
| 102 } | 107 } |
| 103 // All other user agents are assumed to be textual. | 108 // All other user agents are assumed to be textual. |
| 104 return false; | 109 return false; |
| 105 } | 110 } |
| 106 | 111 |
| 107 void _requestHandler(HttpRequest request) { | 112 void _requestHandler(HttpRequest request) { |
| 108 // Allow cross origin requests with 'observatory' header. | 113 // Allow cross origin requests with 'observatory' header. |
| 109 request.response.headers.add('Access-Control-Allow-Origin', '*'); | 114 request.response.headers.add('Access-Control-Allow-Origin', '*'); |
| 110 request.response.headers.add('Access-Control-Allow-Headers', | 115 request.response.headers.add('Access-Control-Allow-Headers', |
| 111 'Observatory-Version'); | 116 'Observatory-Version'); |
| 112 | 117 |
| 113 if (request.method != 'GET') { | 118 if (request.method != 'GET') { |
| 114 // Not a GET request. Do nothing. | 119 // Not a GET request. Do nothing. |
| 115 request.response.close(); | 120 request.response.close(); |
| 116 return; | 121 return; |
| 117 } | 122 } |
| 118 | 123 |
| 119 final String path = | 124 final String path = |
| 120 request.uri.path == '/' ? observatoryPath : request.uri.path; | 125 request.uri.path == '/' ? ROOT_REDIRECT_PATH : request.uri.path; |
| 121 | 126 |
| 122 if (path == WEBSOCKET_PATH) { | 127 if (path == WEBSOCKET_PATH) { |
| 123 WebSocketTransformer.upgrade(request).then((WebSocket webSocket) { | 128 WebSocketTransformer.upgrade(request).then((WebSocket webSocket) { |
| 124 new WebSocketClient(webSocket, service); | 129 new WebSocketClient(webSocket, _service); |
| 125 }); | 130 }); |
| 126 return; | 131 return; |
| 127 } | 132 } |
| 128 | 133 |
| 129 var resource = Resource.resources[path]; | 134 var resource = Resource.resources[path]; |
| 130 if (resource == null && _shouldServeObservatory(request)) { | 135 if (resource == null && _shouldServeObservatory(request)) { |
| 131 resource = Resource.resources[observatoryPath]; | 136 resource = Resource.resources[ROOT_REDIRECT_PATH]; |
| 132 assert(resource != null); | 137 assert(resource != null); |
| 133 } | 138 } |
| 134 if (resource != null) { | 139 if (resource != null) { |
| 135 // Serving up a static resource (e.g. .css, .html, .png). | 140 // Serving up a static resource (e.g. .css, .html, .png). |
| 136 request.response.headers.contentType = | 141 request.response.headers.contentType = |
| 137 ContentType.parse(resource.mimeType); | 142 ContentType.parse(resource.mimeType); |
| 138 request.response.add(resource.data); | 143 request.response.add(resource.data); |
| 139 request.response.close(); | 144 request.response.close(); |
| 140 return; | 145 return; |
| 141 } | 146 } |
| 142 var message = new Message.fromUri(request.uri); | 147 var message = new Message.fromUri(request.uri); |
| 143 var client = new HttpRequestClient(request, service); | 148 var client = new HttpRequestClient(request, _service); |
| 144 client.onMessage(null, message); | 149 client.onMessage(null, message); |
| 145 } | 150 } |
| 146 | 151 |
| 147 Future startServer() { | 152 Future startup() { |
| 148 return HttpServer.bind(ip, port).then((s) { | 153 if (_server != null) { |
| 149 // Only display message when port is automatically selected. | 154 // Already running. |
| 150 var display_message = (ip != '127.0.0.1' || port != 8181); | 155 return new Future.value(this); |
| 151 // Retrieve port. | 156 } |
| 152 port = s.port; | 157 |
| 158 // Startup HTTP server. |
| 159 return HttpServer.bind(_ip, _port).then((s) { |
| 153 _server = s; | 160 _server = s; |
| 154 _server.listen(_requestHandler); | 161 _server.listen(_requestHandler); |
| 155 if (display_message) { | 162 if (_displayMessages) { |
| 163 var ip = _server.address.address.toString(); |
| 164 var port = _server.port.toString(); |
| 156 print('Observatory listening on http://$ip:$port'); | 165 print('Observatory listening on http://$ip:$port'); |
| 157 } | 166 } |
| 158 return s; | 167 // Server is up and running. |
| 168 return this; |
| 169 }).catchError((e, st) { |
| 170 print('Could not start Observatory HTTP server:\n$e\n$st\n'); |
| 171 return this; |
| 159 }); | 172 }); |
| 160 } | 173 } |
| 174 |
| 175 Future shutdown(bool forced) { |
| 176 if (_server == null) { |
| 177 // Not started. |
| 178 return new Future.value(this); |
| 179 } |
| 180 |
| 181 // Force displaying of status messages if we are forcibly shutdown. |
| 182 _displayMessages = _displayMessages || forced; |
| 183 |
| 184 // Shutdown HTTP server and subscription. |
| 185 var ip = _server.address.address.toString(); |
| 186 var port = _server.port.toString(); |
| 187 return _server.close(force: forced).then((_) { |
| 188 if (_displayMessages) { |
| 189 print('Observatory no longer listening on http://$ip:$port'); |
| 190 } |
| 191 _server = null; |
| 192 return this; |
| 193 }).catchError((e, st) { |
| 194 _server = null; |
| 195 print('Could not shutdown Observatory HTTP server:\n$e\n$st\n'); |
| 196 return this; |
| 197 }); |
| 198 } |
| 199 |
| 161 } | 200 } |
| OLD | NEW |