Stop sending X-Pub headers (#4764)
diff --git a/lib/src/command/lish.dart b/lib/src/command/lish.dart index abd1bfc..a69fdb3 100644 --- a/lib/src/command/lish.dart +++ b/lib/src/command/lish.dart
@@ -149,7 +149,6 @@ host.resolve('api/packages/versions/new'), ); request.attachPubApiHeaders(); - request.attachMetadataHeaders(); return await client.fetch(request); }, ); @@ -197,7 +196,6 @@ () async { final request = http.Request('GET', Uri.parse(location)); request.attachPubApiHeaders(); - request.attachMetadataHeaders(); return await client.fetch(request); }, );
diff --git a/lib/src/http.dart b/lib/src/http.dart index 51f2297..ccecab0 100644 --- a/lib/src/http.dart +++ b/lib/src/http.dart
@@ -14,11 +14,8 @@ import 'package:http/http.dart' as http; import 'package:pool/pool.dart'; -import 'command.dart'; import 'log.dart' as log; -import 'pubspec.dart'; import 'sdk.dart'; -import 'source/hosted.dart'; import 'utils.dart'; /// Headers and field names that should be censored in the log output. @@ -31,9 +28,6 @@ /// it's not supported. const pubApiHeaders = {'Accept': 'application/vnd.pub.v2+json'}; -/// A unique ID to identify this particular invocation of pub. -final _sessionId = createUuid(); - /// An HTTP client that transforms 40* errors and socket exceptions into more /// user-friendly error messages. class _PubHttpClient extends http.BaseClient { @@ -172,46 +166,11 @@ http.Client get innerHttpClient => _pubClient._inner; set innerHttpClient(http.Client client) => _pubClient._inner = client; -/// Runs [callback] in a zone where all HTTP requests sent to `pub.dev` -/// will indicate the [type] of the relationship between the root package and -/// the package being requested. -/// -/// If [type] is [DependencyType.none], no extra metadata is added. -Future<T> withDependencyType<T>( - DependencyType type, - Future<T> Function() callback, -) { - return runZoned(callback, zoneValues: {#_dependencyType: type}); -} - extension AttachHeaders on http.Request { /// Adds headers required for pub.dev API requests. void attachPubApiHeaders() { headers.addAll(pubApiHeaders); } - - /// Adds request metadata headers about the Pub tool's environment and the - /// currently running command if the request URL indicates the destination is - /// a Hosted Pub Repository. - void attachMetadataHeaders() { - if (!HostedSource.shouldSendAdditionalMetadataFor(url)) { - return; - } - - headers['X-Pub-OS'] = Platform.operatingSystem; - headers['X-Pub-Command'] = PubCommand.command; - headers['X-Pub-Session-ID'] = _sessionId; - - final environment = Platform.environment['PUB_ENVIRONMENT']; - if (environment != null) { - headers['X-Pub-Environment'] = environment; - } - - final type = Zone.current[#_dependencyType]; - if (type != null && type != DependencyType.none) { - headers['X-Pub-Reason'] = type.toString(); - } - } } /// Handles a successful JSON-formatted response from pub.dev.
diff --git a/lib/src/pubspec.dart b/lib/src/pubspec.dart index 62eaeb9..0170c77 100644 --- a/lib/src/pubspec.dart +++ b/lib/src/pubspec.dart
@@ -560,14 +560,7 @@ } /// The type of dependency from one package to another. -enum DependencyType { - direct, - dev, - none; - - @override - String toString() => name; -} +enum DependencyType { direct, dev, none } /// Parses the dependency field named [field], and returns the corresponding /// map of dependency names to dependencies.
diff --git a/lib/src/solver/package_lister.dart b/lib/src/solver/package_lister.dart index 56aa960..554df7a 100644 --- a/lib/src/solver/package_lister.dart +++ b/lib/src/solver/package_lister.dart
@@ -9,7 +9,6 @@ import 'package:pub_semver/pub_semver.dart'; import '../exceptions.dart'; -import '../http.dart'; import '../log.dart' as log; import '../package.dart'; import '../package_name.dart'; @@ -48,9 +47,6 @@ final SystemCache _systemCache; - /// The type of the dependency from the root package onto [_ref]. - final DependencyType _dependencyType; - /// The set of packages that were overridden by the root package. final Set<String> _overriddenPackages; @@ -94,12 +90,9 @@ ResolvedRootDescription(_ref.description as RootDescription), ), ] - : (await withDependencyType( - _dependencyType, - () => _systemCache.getVersions( - _ref, - allowedRetractedVersion: _allowedRetractedVersion, - ), + : (await _systemCache.getVersions( + _ref, + allowedRetractedVersion: _allowedRetractedVersion, )) ..sort((id1, id2) => id1.version.compareTo(id2.version)); _cachedVersions = cachedVersions; @@ -118,7 +111,6 @@ this._systemCache, this._ref, this._locked, - this._dependencyType, this._overriddenPackages, this._allowedRetractedVersion, { bool downgrade = false, @@ -137,7 +129,6 @@ // boundaries of various constraints, which is useless for the root // package. _locked = PackageId.root(package), - _dependencyType = DependencyType.none, _overriddenPackages = overriddenPackages, _isDowngrade = false, _allowedRetractedVersion = null, @@ -218,10 +209,7 @@ pubspec = _rootPackage!.pubspec; } else { try { - pubspec = await withDependencyType( - _dependencyType, - () => _systemCache.describe(id), - ); + pubspec = await _systemCache.describe(id); } on SourceSpanApplicationException catch (error) { // The lockfile for the pubspec couldn't be parsed, log.fine('Failed to parse pubspec for $id:\n$error'); @@ -472,10 +460,7 @@ /// keeping the actual error handling in a central location. Future<Pubspec> _describeSafe(PackageId id) async { try { - return await withDependencyType( - _dependencyType, - () => _systemCache.describe(id), - ); + return await _systemCache.describe(id); } catch (_) { return Pubspec(id.name, version: id.version); }
diff --git a/lib/src/solver/result.dart b/lib/src/solver/result.dart index 7c88666..3990f78 100644 --- a/lib/src/solver/result.dart +++ b/lib/src/solver/result.dart
@@ -5,7 +5,6 @@ import 'package:collection/collection.dart'; import 'package:pub_semver/pub_semver.dart'; -import '../http.dart'; import '../lock_file.dart'; import '../log.dart'; import '../package.dart'; @@ -66,12 +65,7 @@ return await Future.wait( packages.map((id) async { if (id.source is CachedSource) { - return await withDependencyType( - _root.pubspec.dependencyType(id.name), - () async { - return (await cache.downloadPackage(id)).packageId; - }, - ); + return (await cache.downloadPackage(id)).packageId; } return id; }),
diff --git a/lib/src/solver/version_solver.dart b/lib/src/solver/version_solver.dart index 0d0f119..1293b9a 100644 --- a/lib/src/solver/version_solver.dart +++ b/lib/src/solver/version_solver.dart
@@ -561,7 +561,6 @@ _systemCache, ref, locked, - _root.pubspec.dependencyType(package.name), overridden, _getAllowedRetracted(ref.name), downgrade: _type == SolveType.downgrade,
diff --git a/lib/src/source/hosted.dart b/lib/src/source/hosted.dart index 6b58d92..25fe27f 100644 --- a/lib/src/source/hosted.dart +++ b/lib/src/source/hosted.dart
@@ -184,25 +184,6 @@ } }(); - /// Whether extra metadata headers should be sent for HTTP requests to a given - /// [url]. - static bool shouldSendAdditionalMetadataFor(Uri url) { - if (runningFromTest && Platform.environment.containsKey('PUB_HOSTED_URL')) { - if (url.origin != Platform.environment['PUB_HOSTED_URL']) { - return false; - } - } else { - if (!HostedSource.isPubDevUrl(url.toString())) return false; - } - - if (Platform.environment.containsKey('CI') && - Platform.environment['CI'] != 'false') { - return false; - } - - return true; - } - /// Returns a reference to a hosted package named [name]. /// /// If [url] is passed, it's the URL of the pub server from which the package @@ -499,7 +480,6 @@ () async { final request = http.Request('GET', url); request.attachPubApiHeaders(); - request.attachMetadataHeaders(); final response = await client.fetch(request); return response.body; }, @@ -547,15 +527,13 @@ final latestVersion = maxBy<_VersionInfo, Version>(listing, (e) => e.version)!; final dependencies = latestVersion.pubspec.dependencies.values; - unawaited( - withDependencyType(DependencyType.none, () async { - for (final packageRange in dependencies) { - if (packageRange.source is HostedSource) { - preschedule!(_RefAndCache(packageRange.toRef(), cache)); - } + unawaited(() async { + for (final packageRange in dependencies) { + if (packageRange.source is HostedSource) { + preschedule!(_RefAndCache(packageRange.toRef(), cache)); } - }), - ); + } + }()); } final cache = refAndCache.cache; @@ -608,7 +586,6 @@ () async { final request = http.Request('GET', url); request.attachPubApiHeaders(); - request.attachMetadataHeaders(); final response = await client.fetch(request); return response.body; }, @@ -1561,7 +1538,6 @@ // [PubHttpException]. await retryForHttp('downloading "$archiveUrl"', () async { final request = http.Request('GET', archiveUrl); - request.attachMetadataHeaders(); final response = await client.fetchAsStream(request); Stream<List<int>> stream = response.stream;
diff --git a/test/cache/add/adds_latest_matching_version_test.dart b/test/cache/add/adds_latest_matching_version_test.dart index 70b7b48..b2e1f14 100644 --- a/test/cache/add/adds_latest_matching_version_test.dart +++ b/test/cache/add/adds_latest_matching_version_test.dart
@@ -2,8 +2,6 @@ // for details. All rights reserved. Use of this source code is governed by a // BSD-style license that can be found in the LICENSE file. -import 'dart:io'; - import 'package:test/test.dart'; import '../../descriptor.dart' as d; @@ -20,14 +18,7 @@ await runPub( args: ['cache', 'add', 'foo', '-v', '>=1.0.0 <2.0.0'], - silent: allOf([ - contains('Downloading foo 1.2.3...'), - contains('X-Pub-OS: ${Platform.operatingSystem}'), - contains('X-Pub-Command: cache add'), - contains('X-Pub-Session-ID:'), - contains('X-Pub-Environment: test-environment'), - isNot(contains('X-Pub-Reason')), - ]), + silent: allOf([contains('Downloading foo 1.2.3...')]), ); await d.cacheDir({'foo': '1.2.3'}).validate();
diff --git a/test/cache/repair/hosted.dart b/test/cache/repair/hosted.dart index abe9927..92a4b96 100644 --- a/test/cache/repair/hosted.dart +++ b/test/cache/repair/hosted.dart
@@ -2,8 +2,6 @@ // for details. All rights reserved. Use of this source code is governed by a // BSD-style license that can be found in the LICENSE file. -import 'dart:io'; - import 'package:pub/src/exit_codes.dart' as exit_codes; import 'package:test/test.dart'; @@ -200,11 +198,6 @@ contains('Downloading bar 1.2.4...'), contains('Downloading foo 1.2.3...'), contains('Downloading foo 1.2.5...'), - contains('X-Pub-OS: ${Platform.operatingSystem}'), - contains('X-Pub-Command: cache repair'), - contains('X-Pub-Session-ID:'), - contains('X-Pub-Environment: test-environment'), - isNot(contains('X-Pub-Reason')), ]), );
diff --git a/test/embedding/embedding_test.dart b/test/embedding/embedding_test.dart index 772cd5a..8718c89 100644 --- a/test/embedding/embedding_test.dart +++ b/test/embedding/embedding_test.dart
@@ -429,10 +429,6 @@ RegExp(r'Generated by pub on (.*)$', multiLine: true), r'Generated by pub on $TIME', ) - .replaceAll( - RegExp(r'X-Pub-Session-ID(.*)$', multiLine: true), - r'X-Pub-Session-ID: $ID', - ) .replaceAll(RegExp(r'took (.*)$', multiLine: true), r'took: $TIME') .replaceAll(RegExp(r'date: (.*)$', multiLine: true), r'date: $TIME') .replaceAll(
diff --git a/test/hosted/metadata_test.dart b/test/hosted/metadata_test.dart deleted file mode 100644 index 7764fb9..0000000 --- a/test/hosted/metadata_test.dart +++ /dev/null
@@ -1,125 +0,0 @@ -// Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file -// for details. All rights reserved. Use of this source code is governed by a -// BSD-style license that can be found in the LICENSE file. - -import 'dart:io'; - -import 'package:test/test.dart'; - -import '../descriptor.dart' as d; -import '../test_pub.dart'; - -void main() { - forBothPubGetAndUpgrade((command) { - test('sends metadata headers for a direct dependency', () async { - final server = await servePackages(); - server.serve('foo', '1.0.0'); - - await d.appDir(dependencies: {'foo': '1.0.0'}).create(); - - await pubCommand( - command, - silent: allOf([ - contains('X-Pub-OS: ${Platform.operatingSystem}'), - contains('X-Pub-Command: ${command.name}'), - contains('X-Pub-Session-ID:'), - contains('X-Pub-Environment: test-environment'), - - // We should send the reason when we request the pubspec and when we - // request the tarball. - matchesMultiple('X-Pub-Reason: direct', 2), - isNot(contains('X-Pub-Reason: dev')), - ]), - ); - }); - - test('sends metadata headers for a dev dependency', () async { - final server = await servePackages(); - server.serve('foo', '1.0.0'); - - await d.dir(appPath, [ - d.pubspec({ - 'name': 'myapp', - 'dev_dependencies': {'foo': '1.0.0'}, - }), - ]).create(); - - await pubCommand( - command, - silent: allOf([ - contains('X-Pub-OS: ${Platform.operatingSystem}'), - contains('X-Pub-Command: ${command.name}'), - contains('X-Pub-Session-ID:'), - contains('X-Pub-Environment: test-environment'), - - // We should send the reason when we request the pubspec and when we - // request the tarball. - matchesMultiple('X-Pub-Reason: dev', 2), - isNot(contains('X-Pub-Reason: direct')), - ]), - ); - }); - - test('sends metadata headers for a transitive dependency', () async { - final server = await servePackages(); - server.serve('bar', '1.0.0'); - - await d - .appDir( - dependencies: { - 'foo': {'path': '../foo'}, - }, - ) - .create(); - - await d.dir('foo', [ - d.libPubspec('foo', '1.0.0', deps: {'bar': '1.0.0'}), - ]).create(); - - await pubCommand( - command, - silent: allOf([ - contains('X-Pub-OS: ${Platform.operatingSystem}'), - contains('X-Pub-Command: ${command.name}'), - contains('X-Pub-Session-ID:'), - contains('X-Pub-Environment: test-environment'), - isNot(contains('X-Pub-Reason:')), - ]), - ); - }); - - test("doesn't send metadata headers to a foreign server", () async { - final server = - await startPackageServer() - ..serve('foo', '1.0.0'); - - await d - .appDir( - dependencies: { - 'foo': { - 'version': '1.0.0', - 'hosted': { - 'name': 'foo', - 'url': 'http://localhost:${server.port}', - }, - }, - }, - ) - .create(); - - await pubCommand(command, silent: isNot(contains('X-Pub-'))); - }); - - test("doesn't send metadata headers when CI=true", () async { - (await servePackages()).serve('foo', '1.0.0'); - - await d.appDir(dependencies: {'foo': '1.0.0'}).create(); - - await pubCommand( - command, - silent: isNot(contains('X-Pub-')), - environment: {'CI': 'true'}, - ); - }); - }); -}
diff --git a/test/testdata/goldens/embedding/embedding_test/logfile is written with --verbose and on unexpected exceptions.txt b/test/testdata/goldens/embedding/embedding_test/logfile is written with --verbose and on unexpected exceptions.txt index 21b203d..9615a0f 100644 --- a/test/testdata/goldens/embedding/embedding_test/logfile is written with --verbose and on unexpected exceptions.txt +++ b/test/testdata/goldens/embedding/embedding_test/logfile is written with --verbose and on unexpected exceptions.txt
@@ -15,11 +15,6 @@ [E] IO : Get versions from http://localhost:$PORT/api/packages/foo. [E] IO : HTTP GET http://localhost:$PORT/api/packages/foo [E] | Accept: application/vnd.pub.v2+json -[E] | X-Pub-OS: $OS -[E] | X-Pub-Command: get -[E] | X-Pub-Session-ID: $ID -[E] | X-Pub-Environment: test-environment -[E] | X-Pub-Reason: direct [E] | user-agent: Dart pub 3.1.2+3 [E] IO : HTTP response 200 OK for GET http://localhost:$PORT/api/packages/foo [E] | took: $TIME @@ -41,11 +36,6 @@ [E] FINE: Downloading foo 1.0.0... [E] IO : Created temp directory $DIR [E] IO : HTTP GET http://localhost:$PORT/packages/foo/versions/1.0.0.tar.gz -[E] | X-Pub-OS: $OS -[E] | X-Pub-Command: get -[E] | X-Pub-Session-ID: $ID -[E] | X-Pub-Environment: test-environment -[E] | X-Pub-Reason: direct [E] | user-agent: Dart pub 3.1.2+3 [E] IO : HTTP response 200 OK for GET http://localhost:$PORT/packages/foo/versions/1.0.0.tar.gz [E] | took: $TIME @@ -201,11 +191,6 @@ IO : Get versions from http://localhost:$PORT/api/packages/foo. IO : HTTP GET http://localhost:$PORT/api/packages/foo | Accept: application/vnd.pub.v2+json - | X-Pub-OS: $OS - | X-Pub-Command: get - | X-Pub-Session-ID: $ID - | X-Pub-Environment: test-environment - | X-Pub-Reason: direct | user-agent: Dart pub 3.1.2+3 IO : HTTP response 200 OK for GET http://localhost:$PORT/api/packages/foo | took: $TIME @@ -228,11 +213,6 @@ FINE: Downloading foo 1.0.0... IO : Created temp directory $DIR IO : HTTP GET http://localhost:$PORT/packages/foo/versions/1.0.0.tar.gz - | X-Pub-OS: $OS - | X-Pub-Command: get - | X-Pub-Session-ID: $ID - | X-Pub-Environment: test-environment - | X-Pub-Reason: direct | user-agent: Dart pub 3.1.2+3 IO : HTTP response 200 OK for GET http://localhost:$PORT/packages/foo/versions/1.0.0.tar.gz | took: $TIME
diff --git a/tool/extract_all_pub_dev.dart b/tool/extract_all_pub_dev.dart index 8ad1adf..1fd6516 100644 --- a/tool/extract_all_pub_dev.dart +++ b/tool/extract_all_pub_dev.dart
@@ -22,7 +22,6 @@ Future<List<String>> allPackageNames() async { final nextUrl = Uri.https('pub.dev', 'api/packages', {'compact': '1'}); final request = http.Request('GET', nextUrl); - request.attachMetadataHeaders(); final response = await globalHttpClient.fetch(request); final result = json.decode(response.body); return List<String>.from((result as Map)['packages'] as List); @@ -31,7 +30,6 @@ Future<List<String>> versionArchiveUrls(String packageName) async { final url = Uri.https('pub.dev', 'api/packages/$packageName'); final request = http.Request('GET', url); - request.attachMetadataHeaders(); final response = await globalHttpClient.fetch(request); final result = json.decode(response.body) as Map; return (result['versions'] as List) @@ -96,7 +94,6 @@ try { final archiveUri = Uri.parse(archiveUrl); final request = http.Request('GET', archiveUri); - request.attachMetadataHeaders(); response = await globalHttpClient.fetchAsStream(request); await extractTarGz(response.stream, tempDir); log.message('Extracted $archiveUrl');