| 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 defaultPath = '/index.html'; | 83 String observatoryPath = '/index.html'; |
| 84 final String ip; | 84 final String ip; |
| 85 int port; | 85 int port; |
| 86 | 86 |
| 87 final VMService service; | 87 final VMService service; |
| 88 HttpServer _server; | 88 HttpServer _server; |
| 89 | 89 |
| 90 Server(this.service, this.ip, this.port); | 90 Server(this.service, this.ip, this.port); |
| 91 | 91 |
| 92 bool _shouldServeObservatory(HttpRequest request) { |
| 93 if (request.headers['Observatory-Version'] != null) { |
| 94 // Request is already coming from Observatory. |
| 95 return false; |
| 96 } |
| 97 // TODO(johnmccutchan): Test with obscure browsers. |
| 98 if (request.headers.value(HttpHeaders.USER_AGENT).contains('Mozilla')) { |
| 99 // Request is coming from a browser but not Observatory application. |
| 100 // Serve Observatory and let the Observatory make the real request. |
| 101 return true; |
| 102 } |
| 103 // All other user agents are assumed to be textual. |
| 104 return false; |
| 105 } |
| 106 |
| 92 void _requestHandler(HttpRequest request) { | 107 void _requestHandler(HttpRequest request) { |
| 93 // Allow cross origin requests. | 108 // Allow cross origin requests with 'observatory' header. |
| 94 request.response.headers.add('Access-Control-Allow-Origin', '*'); | 109 request.response.headers.add('Access-Control-Allow-Origin', '*'); |
| 110 request.response.headers.add('Access-Control-Allow-Headers', |
| 111 'Observatory-Version'); |
| 95 | 112 |
| 96 final String path = | 113 if (request.method != 'GET') { |
| 97 request.uri.path == '/' ? defaultPath : request.uri.path; | 114 // Not a GET request. Do nothing. |
| 98 | |
| 99 var resource = Resource.resources[path]; | |
| 100 if (resource != null) { | |
| 101 // Serving up a static resource (e.g. .css, .html, .png). | |
| 102 request.response.headers.contentType = | |
| 103 ContentType.parse(resource.mimeType); | |
| 104 request.response.add(resource.data); | |
| 105 request.response.close(); | 115 request.response.close(); |
| 106 return; | 116 return; |
| 107 } | 117 } |
| 108 | 118 |
| 119 final String path = |
| 120 request.uri.path == '/' ? observatoryPath : request.uri.path; |
| 121 |
| 109 if (path == WEBSOCKET_PATH) { | 122 if (path == WEBSOCKET_PATH) { |
| 110 WebSocketTransformer.upgrade(request).then((WebSocket webSocket) { | 123 WebSocketTransformer.upgrade(request).then((WebSocket webSocket) { |
| 111 new WebSocketClient(webSocket, service); | 124 new WebSocketClient(webSocket, service); |
| 112 }); | 125 }); |
| 113 return; | 126 return; |
| 114 } | 127 } |
| 115 | 128 |
| 129 var resource = Resource.resources[path]; |
| 130 if (resource == null && _shouldServeObservatory(request)) { |
| 131 resource = Resource.resources[observatoryPath]; |
| 132 assert(resource != null); |
| 133 } |
| 134 if (resource != null) { |
| 135 // Serving up a static resource (e.g. .css, .html, .png). |
| 136 request.response.headers.contentType = |
| 137 ContentType.parse(resource.mimeType); |
| 138 request.response.add(resource.data); |
| 139 request.response.close(); |
| 140 return; |
| 141 } |
| 116 var message = new Message.fromUri(request.uri); | 142 var message = new Message.fromUri(request.uri); |
| 117 var client = new HttpRequestClient(request, service); | 143 var client = new HttpRequestClient(request, service); |
| 118 client.onMessage(null, message); | 144 client.onMessage(null, message); |
| 119 } | 145 } |
| 120 | 146 |
| 121 Future startServer() { | 147 Future startServer() { |
| 122 return HttpServer.bind(ip, port).then((s) { | 148 return HttpServer.bind(ip, port).then((s) { |
| 123 // Only display message when port is automatically selected. | 149 // Only display message when port is automatically selected. |
| 124 var display_message = (ip != '127.0.0.1' || port != 8181); | 150 var display_message = (ip != '127.0.0.1' || port != 8181); |
| 125 // Retrieve port. | 151 // Retrieve port. |
| 126 port = s.port; | 152 port = s.port; |
| 127 _server = s; | 153 _server = s; |
| 128 _server.listen(_requestHandler); | 154 _server.listen(_requestHandler); |
| 129 if (display_message) { | 155 if (display_message) { |
| 130 print('Observatory listening on http://$ip:$port'); | 156 print('Observatory listening on http://$ip:$port'); |
| 131 } | 157 } |
| 132 return s; | 158 return s; |
| 133 }); | 159 }); |
| 134 } | 160 } |
| 135 } | 161 } |
| OLD | NEW |