| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 library builtin; | 5 library builtin; |
| 6 import 'dart:io'; |
| 7 |
| 8 int _httpRequestResponseCode = 0; |
| 9 String _httpRequestStatusString; |
| 10 var _httpRequestResponse; |
| 11 |
| 12 void _requestCompleted(HttpClientResponseBody body) { |
| 13 _httpRequestResponseCode = body.statusCode; |
| 14 _httpRequestStatusString = '${body.statusCode} ${body.reasonPhrase}'; |
| 15 _httpRequestResponse = null; |
| 16 if (body.statusCode != 200 || body.type == "json") { |
| 17 return; |
| 18 } |
| 19 _httpRequestResponse = body.body; |
| 20 } |
| 21 |
| 22 void _requestFailed(error) { |
| 23 _httpRequestResponseCode = 0; |
| 24 _httpRequestStatusString = error.toString(); |
| 25 _httpRequestResponse = null; |
| 26 } |
| 27 |
| 28 HttpClient _client = new HttpClient(); |
| 29 void _makeHttpRequest(String uri) { |
| 30 _httpRequestResponseCode = 0; |
| 31 _httpRequestStatusString = null; |
| 32 _httpRequestResponse = null; |
| 33 Uri requestUri = Uri.parse(uri); |
| 34 _client.getUrl(requestUri) |
| 35 .then((HttpClientRequest request) => request.close()) |
| 36 .then(HttpBodyHandler.processResponse) |
| 37 .then((HttpClientResponseBody body) { |
| 38 _requestCompleted(body); |
| 39 }).catchError((error) { |
| 40 _requestFailed(error); |
| 41 }); |
| 42 } |
| 6 | 43 |
| 7 // Corelib 'print' implementation. | 44 // Corelib 'print' implementation. |
| 8 void _print(arg) { | 45 void _print(arg) { |
| 9 _Logger._printString(arg.toString()); | 46 _Logger._printString(arg.toString()); |
| 10 } | 47 } |
| 11 | 48 |
| 12 | |
| 13 class _Logger { | 49 class _Logger { |
| 14 static void _printString(String s) native "Logger_PrintString"; | 50 static void _printString(String s) native "Logger_PrintString"; |
| 15 } | 51 } |
| 16 | 52 |
| 17 | |
| 18 _getPrintClosure() => _print; | 53 _getPrintClosure() => _print; |
| 19 | 54 |
| 55 // The URI that the entrypoint script was loaded from. Remembered so that |
| 56 // package imports can be resolved relative to it. |
| 57 var _entrypoint; |
| 58 |
| 59 // The directory to look in to resolve "package:" scheme URIs. |
| 60 var _packageRoot; |
| 20 | 61 |
| 21 void _logResolution(String msg) { | 62 void _logResolution(String msg) { |
| 22 final enabled = false; | 63 final enabled = false; |
| 23 if (enabled) { | 64 if (enabled) { |
| 24 _Logger._printString(msg); | 65 _Logger._printString(msg); |
| 25 } | 66 } |
| 26 } | 67 } |
| 27 | 68 |
| 28 | 69 _setPackageRoot(String packageRoot) { |
| 29 // Are we running on Windows? | 70 // TODO(mattsh) - refactor windows drive and path handling code |
| 30 var _isWindows = false; | 71 // so it can be used here if needed. |
| 31 // The current working directory | 72 _packageRoot = packageRoot; |
| 32 var _workingDirectoryUri; | |
| 33 // The URI that the entry point script was loaded from. Remembered so that | |
| 34 // package imports can be resolved relative to it. | |
| 35 var _entryPointScript; | |
| 36 // The directory to look in to resolve "package:" scheme URIs. | |
| 37 var _packageRoot; | |
| 38 | |
| 39 | |
| 40 void _setWindows() { | |
| 41 _isWindows = true; | |
| 42 } | 73 } |
| 43 | 74 |
| 44 | 75 String _resolveScriptUri(String cwd, String scriptName, bool isWindows) { |
| 45 void _setWorkingDirectory(cwd) { | 76 var scriptUri = Uri.parse(scriptName); |
| 46 if (_isWindows) { | 77 if (scriptUri.scheme == 'http') { |
| 78 _entrypoint = scriptUri; |
| 79 _logResolution("# Resolved script to: $_entrypoint"); |
| 80 return _entrypoint.toString(); |
| 81 } |
| 82 _logResolution("# Current working directory: $cwd"); |
| 83 _logResolution("# ScriptName: $scriptName"); |
| 84 if (isWindows) { |
| 47 // For Windows we need to massage the paths a bit according to | 85 // For Windows we need to massage the paths a bit according to |
| 48 // http://blogs.msdn.com/b/ie/archive/2006/12/06/file-uris-in-windows.aspx | 86 // http://blogs.msdn.com/b/ie/archive/2006/12/06/file-uris-in-windows.aspx |
| 49 // | 87 // |
| 50 // Convert | 88 // Convert |
| 51 // C:\one\two\three | 89 // C:\one\two\three |
| 52 // to | 90 // to |
| 53 // /C:/one/two/three | 91 // /C:/one/two/three |
| 54 cwd = "/${cwd.replaceAll('\\', '/')}"; | 92 cwd = "/${cwd.replaceAll('\\', '/')}"; |
| 93 _logResolution("## cwd: $cwd"); |
| 94 if ((scriptName.length > 2) && (scriptName[1] == ":")) { |
| 95 // This is an absolute path. |
| 96 scriptName = "/${scriptName.replaceAll('\\', '/')}"; |
| 97 } else { |
| 98 scriptName = scriptName.replaceAll('\\', '/'); |
| 99 } |
| 100 _logResolution("## scriptName: $scriptName"); |
| 55 } | 101 } |
| 102 var base = |
| 103 new Uri(scheme: "file", |
| 104 path: cwd.endsWith("/") ? cwd : "$cwd/"); |
| 105 _entrypoint = base.resolve(scriptName); |
| 106 _logResolution("# Resolved script to: $_entrypoint"); |
| 56 | 107 |
| 57 // Ensure we have a trailing slash character. | 108 return _entrypoint.toString(); |
| 58 if (cwd.endsWith('/')) { | |
| 59 cwd = cwd; | |
| 60 } else { | |
| 61 cwd = '$cwd/'; | |
| 62 } | |
| 63 _workingDirectoryUri = new Uri(scheme: 'file', path: cwd); | |
| 64 _logResolution('# Working Directory: $cwd'); | |
| 65 } | 109 } |
| 66 | 110 |
| 67 | |
| 68 _setPackageRoot(String packageRoot) { | |
| 69 if (!packageRoot.endsWith('/')) { | |
| 70 // Ensure we have a trailing slash character. | |
| 71 packageRoot = '$packageRoot/'; | |
| 72 } | |
| 73 _packageRoot = Uri.parse(packageRoot); | |
| 74 } | |
| 75 | |
| 76 | |
| 77 String _resolveScriptUri(String scriptName) { | |
| 78 if (_workingDirectoryUri == null) { | |
| 79 throw 'No current working directory set.'; | |
| 80 } | |
| 81 var scriptUri = Uri.parse(scriptName); | |
| 82 if (scriptUri.scheme != '') { | |
| 83 // Script has a scheme, assume that it is fully formed. | |
| 84 _entryPointScript = scriptUri; | |
| 85 } else { | |
| 86 // Script does not have a scheme, assume that it is a path, | |
| 87 // resolve it against the working directory. | |
| 88 _entryPointScript = _workingDirectoryUri.resolve(scriptName); | |
| 89 } | |
| 90 _logResolution('# Resolved entry point to: $_entryPointScript'); | |
| 91 return _entryPointScript.toString(); | |
| 92 } | |
| 93 | |
| 94 | |
| 95 String _resolveUri(String base, String userString) { | 111 String _resolveUri(String base, String userString) { |
| 96 var baseUri = Uri.parse(base); | 112 var baseUri = Uri.parse(base); |
| 97 _logResolution('# Resolving: $userString from $base'); | 113 _logResolution("# Resolving: $userString from $base"); |
| 98 | 114 |
| 99 var uri = Uri.parse(userString); | 115 var uri = Uri.parse(userString); |
| 100 var resolved; | 116 var resolved; |
| 101 if ('dart-ext' == uri.scheme) { | 117 if ('dart-ext' == uri.scheme) { |
| 102 // Relative URIs with scheme dart-ext should be resolved as if with no | 118 // Relative URIs with scheme dart-ext should be resolved as if with no |
| 103 // scheme. | 119 // scheme. |
| 104 resolved = baseUri.resolve(uri.path); | 120 resolved = baseUri.resolve(uri.path); |
| 105 var path = resolved.path; | 121 var path = resolved.path; |
| 106 if (resolved.scheme == 'package') { | 122 if (resolved.scheme == 'package') { |
| 107 // If we are resolving relative to a package URI we go directly to the | 123 // If we are resolving relative to a package URI we go directly to the |
| 108 // file path and keep the dart-ext scheme. Otherwise, we will lose the | 124 // file path and keep the dart-ext scheme. Otherwise, we will lose the |
| 109 // package URI path part. | 125 // package URI path part. |
| 110 path = _filePathFromPackageUri(resolved); | 126 path = _filePathFromPackageUri(resolved); |
| 111 } | 127 } |
| 112 resolved = new Uri(scheme: 'dart-ext', path: path); | 128 resolved = new Uri(scheme: "dart-ext", path: path); |
| 113 } else { | 129 } else { |
| 114 resolved = baseUri.resolve(userString); | 130 resolved = baseUri.resolve(userString); |
| 115 } | 131 } |
| 116 _logResolution('# Resolved to: $resolved'); | 132 _logResolution("# Resolved to: $resolved"); |
| 117 return resolved.toString(); | 133 return resolved.toString(); |
| 118 } | 134 } |
| 119 | 135 |
| 120 | 136 |
| 121 String _filePathFromUri(String userUri) { | 137 String _filePathFromUri(String userUri, bool isWindows) { |
| 122 var uri = Uri.parse(userUri); | 138 var uri = Uri.parse(userUri); |
| 123 _logResolution('# Getting file path from: $uri'); | 139 _logResolution("# Getting file path from: $uri"); |
| 124 | 140 |
| 125 var path; | 141 var path; |
| 126 switch (uri.scheme) { | 142 switch (uri.scheme) { |
| 127 case 'file': | 143 case 'file': |
| 128 path = _filePathFromFileUri(uri); | 144 path = _filePathFromFileUri(uri); |
| 129 break; | 145 break; |
| 130 case 'dart-ext': | 146 case 'dart-ext': |
| 131 path = _filePathFromOtherUri(uri); | 147 path = _filePathFromOtherUri(uri); |
| 132 break; | 148 break; |
| 133 case 'package': | 149 case 'package': |
| 134 path = _filePathFromPackageUri(uri); | 150 path = _filePathFromPackageUri(uri); |
| 135 break; | 151 break; |
| 136 case 'http': | 152 case 'http': |
| 137 path = _filePathFromHttpUri(uri); | 153 path = _filePathFromHttpUri(uri); |
| 138 break; | 154 break; |
| 139 default: | 155 default: |
| 140 // Only handling file and package URIs in standalone binary. | 156 // Only handling file and package URIs in standalone binary. |
| 141 _logResolution('# Unknown scheme (${uri.scheme}) in $uri.'); | 157 _logResolution("# Unknown scheme (${uri.scheme}) in $uri."); |
| 142 throw 'Not a known scheme: $uri'; | 158 throw "Not a known scheme: $uri"; |
| 143 } | 159 } |
| 144 | 160 |
| 145 if (_isWindows && path.startsWith('/')) { | 161 if (isWindows && path.startsWith("/")) { |
| 146 // For Windows we need to massage the paths a bit according to | 162 // For Windows we need to massage the paths a bit according to |
| 147 // http://blogs.msdn.com/b/ie/archive/2006/12/06/file-uris-in-windows.aspx | 163 // http://blogs.msdn.com/b/ie/archive/2006/12/06/file-uris-in-windows.aspx |
| 148 // | 164 // |
| 149 // Drop the leading / before the drive letter. | 165 // Drop the leading / before the drive letter. |
| 150 path = path.substring(1); | 166 path = path.substring(1); |
| 151 _logResolution('# Path: Removed leading / -> $path'); | 167 _logResolution("# path: $path"); |
| 152 } | 168 } |
| 153 | 169 |
| 154 return path; | 170 return path; |
| 155 } | 171 } |
| 156 | 172 |
| 157 | |
| 158 String _filePathFromFileUri(Uri uri) { | 173 String _filePathFromFileUri(Uri uri) { |
| 159 if (!uri.host.isEmpty) { | 174 if (!uri.host.isEmpty) { |
| 160 throw "URIs using the 'file:' scheme may not contain a host."; | 175 throw "URIs using the 'file:' scheme may not contain a host."; |
| 161 } | 176 } |
| 162 | 177 |
| 163 _logResolution('# Path: $uri -> ${uri.path}'); | 178 _logResolution("# Path: ${uri.path}"); |
| 164 return uri.path; | 179 return uri.path; |
| 165 } | 180 } |
| 166 | 181 |
| 167 | |
| 168 String _filePathFromOtherUri(Uri uri) { | 182 String _filePathFromOtherUri(Uri uri) { |
| 169 if (!uri.host.isEmpty) { | 183 if (!uri.host.isEmpty) { |
| 170 throw 'URIs whose paths are used as file paths may not contain a host.'; | 184 throw "URIs whose paths are used as file paths may not contain a host."; |
| 171 } | 185 } |
| 172 | 186 |
| 173 _logResolution('# Path: $uri -> ${uri.path}'); | 187 _logResolution("# Path: ${uri.path}"); |
| 174 return uri.path; | 188 return uri.path; |
| 175 } | 189 } |
| 176 | 190 |
| 177 | |
| 178 String _filePathFromPackageUri(Uri uri) { | 191 String _filePathFromPackageUri(Uri uri) { |
| 179 if (!uri.host.isEmpty) { | 192 if (!uri.host.isEmpty) { |
| 180 var path = (uri.path != '') ? '${uri.host}${uri.path}' : uri.host; | 193 var path = (uri.path != '') ? '${uri.host}${uri.path}' : uri.host; |
| 181 var right = 'package:$path'; | 194 var right = 'package:$path'; |
| 182 var wrong = 'package://$path'; | 195 var wrong = 'package://$path'; |
| 183 | 196 |
| 184 throw "URIs using the 'package:' scheme should look like " | 197 throw "URIs using the 'package:' scheme should look like " |
| 185 "'$right', not '$wrong'."; | 198 "'$right', not '$wrong'."; |
| 186 } | 199 } |
| 187 | 200 |
| 188 var packageUri; | |
| 189 var path; | 201 var path; |
| 190 if (_packageRoot != null) { | 202 if (_packageRoot != null) { |
| 191 // Resolve against package root. | 203 path = "${_packageRoot}${uri.path}"; |
| 192 packageUri = _packageRoot.resolve(uri.path); | |
| 193 } else { | 204 } else { |
| 194 // Resolve against working directory. | 205 if (_entrypoint.scheme == 'http') { |
| 195 packageUri = _entryPointScript.resolve('packages/${uri.path}'); | 206 path = _entrypoint.resolve('packages/${uri.path}').toString(); |
| 207 } else { |
| 208 path = _entrypoint.resolve('packages/${uri.path}').path; |
| 209 } |
| 196 } | 210 } |
| 197 | 211 |
| 198 if (packageUri.scheme == 'file') { | 212 _logResolution("# Package: $path"); |
| 199 path = packageUri.path; | |
| 200 } else { | |
| 201 path = packageUri.toString(); | |
| 202 } | |
| 203 _logResolution('# Package: $uri -> $path'); | |
| 204 return path; | 213 return path; |
| 205 } | 214 } |
| 206 | 215 |
| 207 | |
| 208 String _filePathFromHttpUri(Uri uri) { | 216 String _filePathFromHttpUri(Uri uri) { |
| 209 _logResolution('# Path: $uri -> $uri'); | 217 _logResolution('# Path: $uri'); |
| 210 return uri.toString(); | 218 return uri.toString(); |
| 211 } | 219 } |
| 212 | |
| 213 | |
| 214 String _pathFromHttpUri(String userUri) { | |
| 215 var uri = Uri.parse(userUri); | |
| 216 return uri.path; | |
| 217 } | |
| 218 | |
| 219 | |
| 220 String _hostFromHttpUri(String userUri) { | |
| 221 var uri = Uri.parse(userUri); | |
| 222 return uri.host; | |
| 223 } | |
| 224 | |
| 225 | |
| 226 int _portFromHttpUri(String userUri) { | |
| 227 var uri = Uri.parse(userUri); | |
| 228 return uri.port == 0 ? 80 : uri.port; | |
| 229 } | |
| OLD | NEW |