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

Side by Side Diff: utils/pub/hosted_source.dart

Issue 12328021: Make sure package names are URL encoded. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: URL encode version too. Created 7 years, 10 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 | utils/pub/validator/name.dart » ('j') | no next file with comments »
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 hosted_source; 5 library hosted_source;
6 6
7 import 'dart:async'; 7 import 'dart:async';
8 import 'dart:io' as io; 8 import 'dart:io' as io;
9 import 'dart:json' as json; 9 import 'dart:json' as json;
10 import 'dart:uri'; 10 import 'dart:uri';
(...skipping 11 matching lines...) Expand all
22 import 'source_registry.dart'; 22 import 'source_registry.dart';
23 import 'utils.dart'; 23 import 'utils.dart';
24 import 'version.dart'; 24 import 'version.dart';
25 25
26 /// A package source that installs packages from a package hosting site that 26 /// A package source that installs packages from a package hosting site that
27 /// uses the same API as pub.dartlang.org. 27 /// uses the same API as pub.dartlang.org.
28 class HostedSource extends Source { 28 class HostedSource extends Source {
29 final name = "hosted"; 29 final name = "hosted";
30 final shouldCache = true; 30 final shouldCache = true;
31 31
32 /// The URL of the default package repository.
33 static final defaultUrl = "https://pub.dartlang.org";
34
35 /// Downloads a list of all versions of a package that are available from the 32 /// Downloads a list of all versions of a package that are available from the
36 /// site. 33 /// site.
37 Future<List<Version>> getVersions(String name, description) { 34 Future<List<Version>> getVersions(String name, description) {
38 var parsed = _parseDescription(description); 35 var url = _makeUrl(description,
39 var fullUrl = "${parsed.last}/packages/${parsed.first}.json"; 36 (server, package) => "$server/packages/$package.json");
40 37
41 return httpClient.read(fullUrl).then((body) { 38 log.io("Get versions from $url.");
39 return httpClient.read(url).then((body) {
42 var doc = json.parse(body); 40 var doc = json.parse(body);
43 return doc['versions'] 41 return doc['versions']
44 .map((version) => new Version.parse(version)) 42 .map((version) => new Version.parse(version))
45 .toList(); 43 .toList();
46 }).catchError((ex) { 44 }).catchError((ex) {
45 var parsed = _parseDescription(description);
47 _throwFriendlyError(ex, parsed.first, parsed.last); 46 _throwFriendlyError(ex, parsed.first, parsed.last);
48 }); 47 });
49 } 48 }
50 49
51 /// Downloads and parses the pubspec for a specific version of a package that 50 /// Downloads and parses the pubspec for a specific version of a package that
52 /// is available from the site. 51 /// is available from the site.
53 Future<Pubspec> describe(PackageId id) { 52 Future<Pubspec> describe(PackageId id) {
54 var parsed = _parseDescription(id.description); 53 var url = _makeVersionUrl(id, (server, package, version) =>
55 var fullUrl = "${parsed.last}/packages/${parsed.first}/versions/" 54 "$server/packages/$package/versions/$version.yaml");
56 "${id.version}.yaml";
57 55
58 return httpClient.read(fullUrl).then((yaml) { 56 log.io("Describe package at $url.");
57 return httpClient.read(url).then((yaml) {
59 return new Pubspec.parse(null, yaml, systemCache.sources); 58 return new Pubspec.parse(null, yaml, systemCache.sources);
60 }).catchError((ex) { 59 }).catchError((ex) {
60 var parsed = _parseDescription(id.description);
61 _throwFriendlyError(ex, id, parsed.last); 61 _throwFriendlyError(ex, id, parsed.last);
62 }); 62 });
63 } 63 }
64 64
65 /// Downloads a package from the site and unpacks it. 65 /// Downloads a package from the site and unpacks it.
66 Future<bool> install(PackageId id, String destPath) { 66 Future<bool> install(PackageId id, String destPath) {
67 return defer(() { 67 return defer(() {
68 var parsedDescription = _parseDescription(id.description); 68 var url = _makeVersionUrl(id, (server, package, version) =>
69 var name = parsedDescription.first; 69 "$server/packages/$package/versions/$version.tar.gz");
70 var url = parsedDescription.last; 70 log.io("Install package from $url.");
71
72 var fullUrl = "$url/packages/$name/versions/${id.version}.tar.gz";
73 71
74 log.message('Downloading $id...'); 72 log.message('Downloading $id...');
75 73
76 // Download and extract the archive to a temp directory. 74 // Download and extract the archive to a temp directory.
77 var tempDir = systemCache.createTempDir(); 75 var tempDir = systemCache.createTempDir();
78 return httpClient.send(new http.Request("GET", Uri.parse(fullUrl))) 76 return httpClient.send(new http.Request("GET", url))
79 .then((response) => response.stream) 77 .then((response) => response.stream)
80 .then((stream) { 78 .then((stream) {
81 return timeout(extractTarGz(stream, tempDir), HTTP_TIMEOUT, 79 return timeout(extractTarGz(stream, tempDir), HTTP_TIMEOUT,
82 'fetching URL "$fullUrl"'); 80 'fetching URL "$url"');
83 }).then((_) { 81 }).then((_) {
84 // Now that the install has succeeded, move it to the real location in 82 // Now that the install has succeeded, move it to the real location in
85 // the cache. This ensures that we don't leave half-busted ghost 83 // the cache. This ensures that we don't leave half-busted ghost
86 // directories in the user's pub cache if an install fails. 84 // directories in the user's pub cache if an install fails.
87 return renameDir(tempDir, destPath); 85 return renameDir(tempDir, destPath);
88 }).then((_) => true); 86 }).then((_) => true);
89 }); 87 });
90 } 88 }
91 89
92 /// The system cache directory for the hosted source contains subdirectories 90 /// The system cache directory for the hosted source contains subdirectories
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
135 133
136 if (asyncError.error is io.SocketIOException) { 134 if (asyncError.error is io.SocketIOException) {
137 throw 'Got socket error trying to find package "$package" at $url.\n' 135 throw 'Got socket error trying to find package "$package" at $url.\n'
138 '${asyncError.error.osError}'; 136 '${asyncError.error.osError}';
139 } 137 }
140 138
141 // Otherwise re-throw the original exception. 139 // Otherwise re-throw the original exception.
142 throw asyncError; 140 throw asyncError;
143 } 141 }
144 142
145 /// Parses the description for a package. 143 }
146 ///
147 /// If the package parses correctly, this returns a (name, url) pair. If not,
148 /// this throws a descriptive FormatException.
149 Pair<String, String> _parseDescription(description) {
150 if (description is String) {
151 return new Pair<String, String>(description, defaultUrl);
152 }
153 144
154 if (description is! Map) { 145 /// The URL of the default package repository.
155 throw new FormatException( 146 final _defaultUrl = "https://pub.dartlang.org";
156 "The description must be a package name or map.");
157 }
158 147
159 if (!description.containsKey("name")) { 148 /// Parses [description] into its server and package name components, then
160 throw new FormatException( 149 /// converts that to a Uri given [pattern]. Ensures the package name is
161 "The description map must contain a 'name' key."); 150 /// properly URL encoded.
162 } 151 Uri _makeUrl(description, String pattern(String server, String package)) {
152 var parsed = _parseDescription(description);
153 var server = parsed.last;
154 var package = encodeUriComponent(parsed.first);
155 return new Uri(pattern(server, package));
156 }
163 157
164 var name = description["name"]; 158 /// Parses [id] into its server, package name, and version components, then
165 if (name is! String) { 159 /// converts that to a Uri given [pattern]. Ensures the package name is
166 throw new FormatException("The 'name' key must have a string value."); 160 /// properly URL encoded.
167 } 161 Uri _makeVersionUrl(PackageId id,
162 String pattern(String server, String package, String version)) {
163 var parsed = _parseDescription(id.description);
164 var server = parsed.last;
165 var package = encodeUriComponent(parsed.first);
166 var version = encodeUriComponent(id.version.toString());
167 return new Uri(pattern(server, package, version));
168 }
168 169
169 var url = description.containsKey("url") ? description["url"] : defaultUrl; 170 /// Parses the description for a package.
170 return new Pair<String, String>(name, url); 171 ///
172 /// If the package parses correctly, this returns a (name, url) pair. If not,
173 /// this throws a descriptive FormatException.
174 Pair<String, String> _parseDescription(description) {
175 if (description is String) {
176 return new Pair<String, String>(description, _defaultUrl);
171 } 177 }
178
179 if (description is! Map) {
180 throw new FormatException(
181 "The description must be a package name or map.");
182 }
183
184 if (!description.containsKey("name")) {
185 throw new FormatException(
186 "The description map must contain a 'name' key.");
187 }
188
189 var name = description["name"];
190 if (name is! String) {
191 throw new FormatException("The 'name' key must have a string value.");
192 }
193
194 var url = description.containsKey("url") ? description["url"] : _defaultUrl;
195 return new Pair<String, String>(name, url);
172 } 196 }
OLDNEW
« no previous file with comments | « no previous file | utils/pub/validator/name.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698