| OLD | NEW |
| 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, 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 var _httpClient; | 7 var _httpClient; |
| 8 | 8 |
| 9 void _loadHttp(sendPort, uri) { | 9 void _loadHttp(sendPort, uri) { |
| 10 if (_httpClient == null) { | 10 if (_httpClient == null) { |
| (...skipping 29 matching lines...) Expand all Loading... |
| 40 void _loadFile(sendPort, path) { | 40 void _loadFile(sendPort, path) { |
| 41 var sourceFile = new File(path); | 41 var sourceFile = new File(path); |
| 42 sourceFile.readAsBytes().then((data) { | 42 sourceFile.readAsBytes().then((data) { |
| 43 sendPort.send(data); | 43 sendPort.send(data); |
| 44 }, | 44 }, |
| 45 onError: (e) { | 45 onError: (e) { |
| 46 sendPort.send(e.toString()); | 46 sendPort.send(e.toString()); |
| 47 }); | 47 }); |
| 48 } | 48 } |
| 49 | 49 |
| 50 var dataUriRegex = new RegExp( |
| 51 r"data:([\w-]+/[\w-]+)?(;charset=([\w-]+))?(;base64)?,(.*)"); |
| 52 |
| 53 void _loadDataUri(sendPort, uri) { |
| 54 try { |
| 55 var match = dataUriRegex.firstMatch(uri.toString()); |
| 56 if (match == null) throw "Malformed data uri"; |
| 57 |
| 58 var mimeType = match.group(1); |
| 59 var encoding = match.group(3); |
| 60 var maybeBase64 = match.group(4); |
| 61 var encodedData = match.group(5); |
| 62 |
| 63 if (mimeType != "application/dart") { |
| 64 throw "MIME-type must be application/dart"; |
| 65 } |
| 66 if (encoding != "utf-8") { |
| 67 // Default is ASCII. The C++ portion of the embedder assumes UTF-8. |
| 68 throw "Only utf-8 encoding is supported"; |
| 69 } |
| 70 if (maybeBase64 != null) { |
| 71 throw "Only percent encoding is supported"; |
| 72 } |
| 73 |
| 74 var data = UTF8.encode(Uri.decodeComponent(encodedData)); |
| 75 sendPort.send(data); |
| 76 } catch (e) { |
| 77 sendPort.send("Invalid data uri ($uri) $e"); |
| 78 } |
| 79 } |
| 80 |
| 50 _processLoadRequest(request) { | 81 _processLoadRequest(request) { |
| 51 var sp = request[0]; | 82 var sp = request[0]; |
| 52 var uri = Uri.parse(request[1]); | 83 var uri = Uri.parse(request[1]); |
| 53 if (uri.scheme == 'file') { | 84 if (uri.scheme == 'file') { |
| 54 _loadFile(sp, uri.toFilePath()); | 85 _loadFile(sp, uri.toFilePath()); |
| 55 } else if ((uri.scheme == 'http') || (uri.scheme == 'https')) { | 86 } else if ((uri.scheme == 'http') || (uri.scheme == 'https')) { |
| 56 _loadHttp(sp, uri); | 87 _loadHttp(sp, uri); |
| 88 } else if ((uri.scheme == 'data')) { |
| 89 _loadDataUri(sp, uri); |
| 57 } else { | 90 } else { |
| 58 sp.send('Unknown scheme for $uri'); | 91 sp.send('Unknown scheme for $uri'); |
| 59 } | 92 } |
| 60 } | 93 } |
| OLD | NEW |