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