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 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
72 map['socket'] = '$socket'; | 72 map['socket'] = '$socket'; |
73 return map; | 73 return map; |
74 } | 74 } |
75 } | 75 } |
76 | 76 |
77 | 77 |
78 class HttpRequestClient extends Client { | 78 class HttpRequestClient extends Client { |
79 static ContentType jsonContentType = | 79 static ContentType jsonContentType = |
80 new ContentType("application", "json", charset: "utf-8"); | 80 new ContentType("application", "json", charset: "utf-8"); |
81 final HttpRequest request; | 81 final HttpRequest request; |
82 final List<String> _allowedOrigins; | |
82 | 83 |
83 HttpRequestClient(this.request, VMService service) | 84 HttpRequestClient(this.request, VMService service, this._allowedOrigins) |
84 : super(service, sendEvents:false); | 85 : super(service, sendEvents:false); |
85 | 86 |
86 disconnect() { | 87 disconnect() { |
87 request.response.close(); | 88 request.response.close(); |
88 close(); | 89 close(); |
89 } | 90 } |
90 | 91 |
91 void post(dynamic result) { | 92 void post(dynamic result) { |
92 if (result == null) { | 93 if (result == null) { |
93 close(); | 94 close(); |
94 return; | 95 return; |
95 } | 96 } |
96 HttpResponse response = request.response; | 97 HttpResponse response = request.response; |
97 response.headers.contentType = jsonContentType; | 98 response.headers.contentType = jsonContentType; |
99 final uri = Uri.parse(request.headers['Origin']?.single ?? 'http://localhost '); | |
rmacnak
2016/09/02 16:42:01
Let's put this in a separate CL.
| |
100 final noPortOrigin = new Uri(host: uri.host, scheme: uri.scheme).origin; | |
101 if (_allowedOrigins.contains(noPortOrigin)) { | |
102 response.headers.add('Access-Control-Allow-Origin', uri.origin); | |
103 } | |
98 if (result is String) { | 104 if (result is String) { |
99 response.write(result); | 105 response.write(result); |
100 } else { | 106 } else { |
101 assert(result is List); | 107 assert(result is List); |
102 Uint8List cstring = result[0]; // Already in UTF-8. | 108 Uint8List cstring = result[0]; // Already in UTF-8. |
103 response.add(cstring); | 109 response.add(cstring); |
104 } | 110 } |
105 response.close(); | 111 response.close(); |
106 close(); | 112 close(); |
107 } | 113 } |
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
232 if (asset != null) { | 238 if (asset != null) { |
233 // Serving up a static asset (e.g. .css, .html, .png). | 239 // Serving up a static asset (e.g. .css, .html, .png). |
234 request.response.headers.contentType = | 240 request.response.headers.contentType = |
235 ContentType.parse(asset.mimeType); | 241 ContentType.parse(asset.mimeType); |
236 request.response.add(asset.data); | 242 request.response.add(asset.data); |
237 request.response.close(); | 243 request.response.close(); |
238 return; | 244 return; |
239 } | 245 } |
240 // HTTP based service request. | 246 // HTTP based service request. |
241 try { | 247 try { |
242 var client = new HttpRequestClient(request, _service); | 248 var client = new HttpRequestClient(request, _service, _allowedOrigins); |
243 var message = new Message.fromUri(client, request.uri); | 249 var message = new Message.fromUri(client, request.uri); |
244 client.onMessage(null, message); | 250 client.onMessage(null, message); |
245 } catch (e) { | 251 } catch (e) { |
246 print('Unexpected error processing HTTP request uri: ' | 252 print('Unexpected error processing HTTP request uri: ' |
247 '${request.uri}\n$e\n'); | 253 '${request.uri}\n$e\n'); |
248 rethrow; | 254 rethrow; |
249 } | 255 } |
250 } | 256 } |
251 | 257 |
252 Future startup() { | 258 Future startup() { |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
320 _notifyServerState("", 0); | 326 _notifyServerState("", 0); |
321 onServerAddressChange(null); | 327 onServerAddressChange(null); |
322 return this; | 328 return this; |
323 }); | 329 }); |
324 } | 330 } |
325 | 331 |
326 } | 332 } |
327 | 333 |
328 void _notifyServerState(String ip, int port) | 334 void _notifyServerState(String ip, int port) |
329 native "VMServiceIO_NotifyServerState"; | 335 native "VMServiceIO_NotifyServerState"; |
OLD | NEW |