Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(105)

Side by Side Diff: runtime/bin/builtin.dart

Issue 17183008: HTTP loading cleanups (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | runtime/bin/dartutils.h » ('j') | runtime/bin/dartutils.cc » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
114 void _setWorkingDirectory(cwd) {
115 cwd = _sanitizeWindowsPath(cwd);
116 // Ensure we have a trailing slash character.
117 if (cwd.endsWith('/')) {
118 cwd = cwd;
119 } else {
120 cwd = '$cwd/';
121 }
122 _workingDirectoryUri = new Uri(scheme: 'file', path: cwd);
123 _logResolution('# Working Directory: $cwd');
124 }
125
126
127 _setPackageRoot(String packageRoot) {
128 if (!packageRoot.endsWith('/')) {
129 // Ensure we have a trailing slash character.
130 packageRoot = '$packageRoot/';
131 }
Ivan Posva 2013/06/18 22:24:00 How about pulling this out into _enforceTrailingSl
Cutch 2013/06/18 22:28:22 Done.
132 _packageRoot = Uri.parse(packageRoot);
133 }
134
135
136 String _resolveScriptUri(String scriptName) {
137 if (_workingDirectoryUri == null) {
138 throw 'No current working directory set.';
139 }
140 scriptName = _sanitizeWindowsPath(scriptName);
141
76 var scriptUri = Uri.parse(scriptName); 142 var scriptUri = Uri.parse(scriptName);
77 if (scriptUri.scheme == 'http') { 143 if (scriptUri.scheme != '') {
78 _entrypoint = scriptUri; 144 // Script has a scheme, assume that it is fully formed.
79 _logResolution("# Resolved script to: $_entrypoint"); 145 _entryPointScript = scriptUri;
80 return _entrypoint.toString(); 146 } else {
147 // Script does not have a scheme, assume that it is a path,
148 // resolve it against the working directory.
149 _entryPointScript = _workingDirectoryUri.resolve(scriptName);
81 } 150 }
82 _logResolution("# Current working directory: $cwd"); 151 _logResolution('# Resolved entry point to: $_entryPointScript');
83 _logResolution("# ScriptName: $scriptName"); 152 return _entryPointScript.toString();
84 if (isWindows) { 153 }
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 154
108 return _entrypoint.toString();
109 }
110 155
111 String _resolveUri(String base, String userString) { 156 String _resolveUri(String base, String userString) {
112 var baseUri = Uri.parse(base); 157 var baseUri = Uri.parse(base);
113 _logResolution("# Resolving: $userString from $base"); 158 _logResolution('# Resolving: $userString from $base');
114 159
115 var uri = Uri.parse(userString); 160 var uri = Uri.parse(userString);
116 var resolved; 161 var resolved;
117 if ('dart-ext' == uri.scheme) { 162 if ('dart-ext' == uri.scheme) {
118 // Relative URIs with scheme dart-ext should be resolved as if with no 163 // Relative URIs with scheme dart-ext should be resolved as if with no
119 // scheme. 164 // scheme.
120 resolved = baseUri.resolve(uri.path); 165 resolved = baseUri.resolve(uri.path);
121 var path = resolved.path; 166 var path = resolved.path;
122 if (resolved.scheme == 'package') { 167 if (resolved.scheme == 'package') {
123 // If we are resolving relative to a package URI we go directly to the 168 // 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 169 // file path and keep the dart-ext scheme. Otherwise, we will lose the
125 // package URI path part. 170 // package URI path part.
126 path = _filePathFromPackageUri(resolved); 171 path = _filePathFromPackageUri(resolved);
127 } 172 }
128 resolved = new Uri(scheme: "dart-ext", path: path); 173 resolved = new Uri(scheme: 'dart-ext', path: path);
129 } else { 174 } else {
130 resolved = baseUri.resolve(userString); 175 resolved = baseUri.resolve(userString);
131 } 176 }
132 _logResolution("# Resolved to: $resolved"); 177 _logResolution('# Resolved to: $resolved');
133 return resolved.toString(); 178 return resolved.toString();
134 } 179 }
135 180
136 181
137 String _filePathFromUri(String userUri, bool isWindows) { 182 String _filePathFromUri(String userUri) {
138 var uri = Uri.parse(userUri); 183 var uri = Uri.parse(userUri);
139 _logResolution("# Getting file path from: $uri"); 184 _logResolution('# Getting file path from: $uri');
140 185
141 var path; 186 var path;
142 switch (uri.scheme) { 187 switch (uri.scheme) {
143 case 'file': 188 case 'file':
144 path = _filePathFromFileUri(uri); 189 path = _filePathFromFileUri(uri);
145 break; 190 break;
146 case 'dart-ext': 191 case 'dart-ext':
147 path = _filePathFromOtherUri(uri); 192 path = _filePathFromOtherUri(uri);
148 break; 193 break;
149 case 'package': 194 case 'package':
150 path = _filePathFromPackageUri(uri); 195 path = _filePathFromPackageUri(uri);
151 break; 196 break;
152 case 'http': 197 case 'http':
153 path = _filePathFromHttpUri(uri); 198 path = _filePathFromHttpUri(uri);
154 break; 199 break;
155 default: 200 default:
156 // Only handling file and package URIs in standalone binary. 201 // Only handling file and package URIs in standalone binary.
157 _logResolution("# Unknown scheme (${uri.scheme}) in $uri."); 202 _logResolution('# Unknown scheme (${uri.scheme}) in $uri.');
158 throw "Not a known scheme: $uri"; 203 throw 'Not a known scheme: $uri';
159 } 204 }
160 205
161 if (isWindows && path.startsWith("/")) { 206 if (_isWindows && path.startsWith('/')) {
162 // For Windows we need to massage the paths a bit according to 207 // 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 208 // http://blogs.msdn.com/b/ie/archive/2006/12/06/file-uris-in-windows.aspx
164 // 209 //
165 // Drop the leading / before the drive letter. 210 // Drop the leading / before the drive letter.
166 path = path.substring(1); 211 path = path.substring(1);
167 _logResolution("# path: $path"); 212 _logResolution('# Path: Removed leading / -> $path');
168 } 213 }
169 214
170 return path; 215 return path;
171 } 216 }
172 217
218
173 String _filePathFromFileUri(Uri uri) { 219 String _filePathFromFileUri(Uri uri) {
174 if (!uri.host.isEmpty) { 220 if (!uri.host.isEmpty) {
175 throw "URIs using the 'file:' scheme may not contain a host."; 221 throw "URIs using the 'file:' scheme may not contain a host.";
176 } 222 }
177 223
178 _logResolution("# Path: ${uri.path}"); 224 _logResolution('# Path: $uri -> ${uri.path}');
179 return uri.path; 225 return uri.path;
180 } 226 }
181 227
228
182 String _filePathFromOtherUri(Uri uri) { 229 String _filePathFromOtherUri(Uri uri) {
183 if (!uri.host.isEmpty) { 230 if (!uri.host.isEmpty) {
184 throw "URIs whose paths are used as file paths may not contain a host."; 231 throw 'URIs whose paths are used as file paths may not contain a host.';
185 } 232 }
186 233
187 _logResolution("# Path: ${uri.path}"); 234 _logResolution('# Path: $uri -> ${uri.path}');
188 return uri.path; 235 return uri.path;
189 } 236 }
190 237
238
191 String _filePathFromPackageUri(Uri uri) { 239 String _filePathFromPackageUri(Uri uri) {
192 if (!uri.host.isEmpty) { 240 if (!uri.host.isEmpty) {
193 var path = (uri.path != '') ? '${uri.host}${uri.path}' : uri.host; 241 var path = (uri.path != '') ? '${uri.host}${uri.path}' : uri.host;
194 var right = 'package:$path'; 242 var right = 'package:$path';
195 var wrong = 'package://$path'; 243 var wrong = 'package://$path';
196 244
197 throw "URIs using the 'package:' scheme should look like " 245 throw "URIs using the 'package:' scheme should look like "
198 "'$right', not '$wrong'."; 246 "'$right', not '$wrong'.";
199 } 247 }
200 248
249 var packageUri;
201 var path; 250 var path;
202 if (_packageRoot != null) { 251 if (_packageRoot != null) {
203 path = "${_packageRoot}${uri.path}"; 252 // Resolve against package root.
253 packageUri = _packageRoot.resolve(uri.path);
204 } else { 254 } else {
205 if (_entrypoint.scheme == 'http') { 255 // Resolve against working directory.
206 path = _entrypoint.resolve('packages/${uri.path}').toString(); 256 packageUri = _entryPointScript.resolve('packages/${uri.path}');
207 } else {
208 path = _entrypoint.resolve('packages/${uri.path}').path;
209 }
210 } 257 }
211 258
212 _logResolution("# Package: $path"); 259 if (packageUri.scheme == 'file') {
260 path = packageUri.path;
261 } else {
262 path = packageUri.toString();
263 }
264 _logResolution('# Package: $uri -> $path');
213 return path; 265 return path;
214 } 266 }
215 267
268
216 String _filePathFromHttpUri(Uri uri) { 269 String _filePathFromHttpUri(Uri uri) {
217 _logResolution('# Path: $uri'); 270 _logResolution('# Path: $uri -> $uri');
218 return uri.toString(); 271 return uri.toString();
219 } 272 }
OLDNEW
« no previous file with comments | « no previous file | runtime/bin/dartutils.h » ('j') | runtime/bin/dartutils.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698