Beispiel #1
0
        public IPackage FindPackage(NuGetSearchContext findContext, NuGetRequest request)
        {
            request.Debug(Messages.DebugInfoCallMethod3, "NuGetPackageRepository", "FindPackage", findContext.PackageInfo.Id);
            NuGetSearchResult result = this.ResourceProvider.PackagesFeed.Find(findContext, request);

            if (result.Result != null)
            {
                return(result.Result.FirstOrDefault());
            }
            else
            {
                return(null);
            }
        }
Beispiel #2
0
        public NuGetSearchResult Find(NuGetSearchContext findContext, RequestWrapper request)
        {
            request.Debug(Messages.DebugInfoCallMethod3, "NuGetPackageFeed3", "Find", findContext.PackageInfo.Id);
            if (System.Management.Automation.WildcardPattern.ContainsWildcardCharacters(findContext.PackageInfo.Id))
            {
                // Short circuit when there's wildcards - this will never work
                return(findContext.MakeResult(new List <IPackage>()));
            }

            NuGetSearchResult result = findContext.MakeResult(FindImpl(findContext, request), versionPostFilterRequired: false);

            request.Debug(Messages.DebugInfoReturnCall, "NuGetPackageFeed3", "Find");
            return(result);
        }
Beispiel #3
0
        /// <summary>
        /// Find packages when the registration URL is already known.
        /// </summary>
        /// <param name="registrationUrl"></param>
        /// <param name="request"></param>
        /// <returns></returns>
        internal IEnumerable <PackageBase> Find(string registrationUrl, NuGetSearchContext context, RequestWrapper request, bool finalAttempt)
        {
            request.Debug(Messages.DebugInfoCallMethod3, "NuGetPackageFeed3", "Find", registrationUrl);
            List <PackageBase> packages = null;
            PackageBase        cachedPackage;

            if (!registrationUrl.Contains("index.json") && ConcurrentInMemoryCache.Instance.TryGet <PackageBase>(registrationUrl, out cachedPackage))
            {
                if (cachedPackage == null)
                {
                    return(packages);
                }
                else
                {
                    packages = new List <PackageBase>()
                    {
                        cachedPackage
                    };
                    return(packages);
                }
            }

            Stream s = NuGetClient.DownloadDataToStream(registrationUrl, request, ignoreNullResponse: true, tries: 1);

            if (s != null)
            {
                packages = new List <PackageBase>();
                // Now we can get the catalog entry
                dynamic root = DynamicJsonParser.Parse(new StreamReader(s).ReadToEnd());
                if (root.Metadata.HasProperty("type"))
                {
                    // First check if this is a Package or PackageRegistration type
                    // Package is from packageId + version
                    // PackageRegistration is from packageId
                    bool isRegistrationType = false;
                    foreach (string t in root.Metadata.type)
                    {
                        if (t.Equals("PackageRegistration", StringComparison.OrdinalIgnoreCase))
                        {
                            isRegistrationType = true;
                            break;
                        }
                    }

                    if (context.PackageInfo.AllVersions.Count == 0)
                    {
                        // Only when the version list is restricted is this method usually faster
                        this.ResourcesCollection.FilesFeed.GetVersionInfo(context.PackageInfo, request);
                    }

                    HashSet <SemanticVersion> packageSemanticVersions = null;
                    if (context.PackageInfo.AllVersions.Count > 0)
                    {
                        packageSemanticVersions = FilterVersionsByRequirements(context, context.PackageInfo);
                    }

                    if (isRegistrationType)
                    {
                        // This is a registration index, like: "https://api.nuget.org/v3/registration3/json/index.json"
                        // Get all versions from files service
                        // In addition, when DeepMetadataBypass is enabled, we MUST use the registration index to get package info
                        // If a call to -Name -RequiredVersion is done, DeepMetadataBypass will never be enabled (for now)
                        // If we wanted, we could enable this by checking if !isRegistrationType && context.EnableDeepMetadataBypass, then call into Find with the registration index URL
                        if (!context.AllVersions && packageSemanticVersions != null && !context.EnableDeepMetadataBypass)
                        {
                            foreach (SemanticVersion packageVersion in packageSemanticVersions)
                            {
                                NuGetSearchResult result = this.ResourcesCollection.PackagesFeed.Find(new NuGetSearchContext()
                                {
                                    PackageInfo              = context.PackageInfo,
                                    RequiredVersion          = packageVersion,
                                    EnableDeepMetadataBypass = context.EnableDeepMetadataBypass
                                }, request);
                                PackageBase package = result.Result == null ? null : result.Result.FirstOrDefault() as PackageBase;
                                if (package != null)
                                {
                                    packages.Add(package);
                                }
                            }
                        }
                        else
                        {
                            // Going to collect versions from the registration index in here
                            // Map of package version -> either PackageBase (if context.EnableDeepMetadataBypass) or catalog URL
                            Dictionary <SemanticVersion, object> catalogObjects = new Dictionary <SemanticVersion, object>();
                            // If the version list hasn't been built yet, we can build it from the registration page instead of using FilesFeed
                            bool buildPackageInfoVersions = context.PackageInfo.AllVersions.Count == 0;
                            // Fallback to catalog crawling in these cases:
                            //      - Bypass deep metadata
                            //      - Getting version list failed
                            //      - All versions required
                            foreach (dynamic catalogPage in root.items)
                            {
                                dynamic actualCatalogPage = catalogPage;
                                if (!actualCatalogPage.HasProperty("items"))
                                {
                                    // Sometimes the catalog page on the PackageRegistration entry doesn't have the actual page contents
                                    // In this case, the ID metadata tag points to the full catalog entry
                                    Stream fullCatalogPageResponseStream = NuGetClient.DownloadDataToStream(actualCatalogPage.Metadata.id, request);
                                    if (fullCatalogPageResponseStream != null)
                                    {
                                        actualCatalogPage = DynamicJsonParser.Parse(new StreamReader(fullCatalogPageResponseStream).ReadToEnd());
                                    }
                                }
                                foreach (dynamic packageEntry in actualCatalogPage.items)
                                {
                                    SemanticVersion version = new SemanticVersion(packageEntry.catalogentry.version);
                                    if (buildPackageInfoVersions)
                                    {
                                        context.PackageInfo.AddVersion(version);
                                    }

                                    bool hasCatalogEntry = packageEntry.HasProperty("catalogentry");
                                    if (context.EnableDeepMetadataBypass || !hasCatalogEntry)
                                    {
                                        // Bypass retrieving "deep" (but required) metadata like packageHash
                                        // Also do this if there's no catalog entry
                                        PackageBase pb = null;
                                        if (packageEntry.HasProperty("catalogentry"))
                                        {
                                            pb = this.ResourcesCollection.PackageConverter.Make(packageEntry.catalogentry, context.PackageInfo);
                                        }
                                        else
                                        {
                                            // In some implementations (lookin' at you MyGet) there's no catalogEntry object
                                            pb = this.ResourcesCollection.PackageConverter.Make(packageEntry, context.PackageInfo);
                                        }

                                        if (pb != null)
                                        {
                                            catalogObjects[version] = pb;
                                        }
                                    }
                                    else
                                    {
                                        catalogObjects[version] = this.ResourcesCollection.CatalogUrlConverter.Make(packageEntry);
                                    }
                                }
                            }

                            packageSemanticVersions = FilterVersionsByRequirements(context, context.PackageInfo);
                            foreach (SemanticVersion version in packageSemanticVersions)
                            {
                                if (!catalogObjects.ContainsKey(version))
                                {
                                    continue;
                                }

                                if (context.EnableDeepMetadataBypass)
                                {
                                    packages.Add((PackageBase)catalogObjects[version]);
                                }
                                else
                                {
                                    PackageBase pb = GetPackageFromCatalogUrl((string)catalogObjects[version], request, packageSemanticVersions, context);
                                    if (pb != null)
                                    {
                                        packages.Add(pb);
                                    }
                                }
                            }
                        }
                    }
                    else
                    {
                        // In some implementations (lookin' at you MyGet) there's no catalogEntry object

                        PackageBase pb = ConcurrentInMemoryCache.Instance.GetOrAdd <PackageBase>(registrationUrl, () =>
                        {
                            if (!root.HasProperty("catalogentry"))
                            {
                                if ((packageSemanticVersions == null || packageSemanticVersions.Contains(new SemanticVersion(root.version))))
                                {
                                    return(this.ResourcesCollection.PackageConverter.Make(root));
                                }
                                else
                                {
                                    return(null);
                                }
                            }
                            else
                            {
                                return(GetPackageFromCatalogUrl(this.ResourcesCollection.CatalogUrlConverter.Make(root), request, packageSemanticVersions, context));
                            }
                        });
                        if (pb != null)
                        {
                            packages.Add(pb);
                        }
                    }
                }
                else
                {
                    request.Warning(Messages.JsonSchemaMismatch, "type");
                    request.Debug(Messages.JsonObjectDump, DynamicJsonParser.Serialize(root));
                }

                if (context.RequiredVersion == null && context.MinimumVersion == null && context.MaximumVersion == null && packages != null)
                {
                    PackageBase absoluteLatestPackage = packages.Where(p => p.IsPrerelease).OrderByDescending(pb => ((IPackage)pb).Version).FirstOrDefault();
                    if (absoluteLatestPackage != null)
                    {
                        absoluteLatestPackage.IsAbsoluteLatestVersion = true;
                    }

                    PackageBase latestPackage = packages.Where(p => !p.IsPrerelease).OrderByDescending(pb => ((IPackage)pb).Version).FirstOrDefault();
                    if (latestPackage != null)
                    {
                        latestPackage.IsLatestVersion = true;
                    }
                }
            }
            else if (finalAttempt)
            {
                // This is the last retry of this URL. It's definitely not a good one.
                ConcurrentInMemoryCache.Instance.GetOrAdd <PackageBase>(registrationUrl, () => null);
            }

            return(packages);
        }
        private IEnumerable <PackageBase> GetPackagesForPackageEntry(dynamic packageEntry, NuGetSearchContext searchContext, RequestWrapper request)
        {
            PackageEntryInfo packageEntryInfo = new PackageEntryInfo(packageEntry.id);

            if (!PackageFilterUtility.IsValidByName(packageEntryInfo, searchContext))
            {
                yield break;
            }

            // This will help us take packageEntryInfo.LatestVersion and packageEntryInfo.AbsoluteLatestVersion and get the matching package easily
            // We're not setting isLatestVersion here so we don't have to deal with "is it latest or absolute latest"
            Dictionary <SemanticVersion, PackageBase> versionToPackageTable = new Dictionary <SemanticVersion, PackageBase>();
            NuGetSearchContext individualPackageSearchContext = new NuGetSearchContext()
            {
                PackageInfo              = packageEntryInfo,
                AllVersions              = searchContext.AllVersions,
                AllowPrerelease          = searchContext.AllowPrerelease,
                RequiredVersion          = searchContext.RequiredVersion,
                MinimumVersion           = searchContext.MinimumVersion,
                MaximumVersion           = searchContext.MaximumVersion,
                EnableDeepMetadataBypass = searchContext.EnableDeepMetadataBypass
            };
            bool latestVersionRequired = !searchContext.AllVersions && searchContext.RequiredVersion == null && searchContext.MinimumVersion == null && searchContext.MaximumVersion == null;

            if (searchContext.EnableDeepMetadataBypass)
            {
                if (latestVersionRequired)
                {
                    // Use the search result page to get the metadata for the latest version
                    SemanticVersion individualPackageVersion = new SemanticVersion(packageEntry.version);
                    packageEntryInfo.AddVersion(individualPackageVersion);
                    PackageBase pb = this.ResourcesCollection.PackageConverter.Make(packageEntry);
                    if (pb != null)
                    {
                        yield return(pb);
                    }
                }
                else
                {
                    // Go to the registration index of this package first. This allows us to bypass "deep" (but required) metadata in certain cases.
                    NuGetPackageFeed3 packageFeed3 = (NuGetPackageFeed3)this.ResourcesCollection.PackagesFeed;
                    NuGetSearchResult result       = packageFeed3.Find(individualPackageSearchContext, request);
                    foreach (PackageBase pb in result.Result.Cast <PackageBase>())
                    {
                        yield return(pb);
                    }
                }
            }
            else
            {
                // Either we want a specific version or we want all metadata for any packages
                foreach (dynamic packageVersionEntry in packageEntry.versions)
                {
                    if (packageEntry.version.Equals(packageVersionEntry.version) || searchContext.AllVersions)
                    {
                        if (packageVersionEntry.Metadata.HasProperty("id"))
                        {
                            // Collect all versions from the search results so we can manually set isLatestVersion and isAbsoluteLatestVersion later
                            SemanticVersion individualPackageVersion = new SemanticVersion(packageVersionEntry.version);
                            packageEntryInfo.AddVersion(individualPackageVersion);

                            // Skip prerelease versions if AllowPrereleaseVersions is not specified
                            if (!String.IsNullOrEmpty(individualPackageVersion.SpecialVersion) && !searchContext.AllowPrerelease)
                            {
                                continue;
                            }

                            long?versionDownloadCount = null;
                            if (packageVersionEntry.HasProperty("downloads"))
                            {
                                versionDownloadCount = packageVersionEntry.downloads;
                            }

                            string registrationUrl = packageVersionEntry.Metadata.id;
                            // This should be PackageFeed3
                            // There should be a better way to reuse this function
                            NuGetPackageFeed3 packageFeed3          = (NuGetPackageFeed3)this.ResourcesCollection.PackagesFeed;
                            PackageBase       packageVersionPackage = packageFeed3.Find(registrationUrl, individualPackageSearchContext, request, true).FirstOrDefault();
                            if (packageVersionPackage != null)
                            {
                                if (versionDownloadCount.HasValue)
                                {
                                    packageVersionPackage.VersionDownloadCount = versionDownloadCount.Value;
                                }

                                // Reset these so we haven't collected all versions yet, so this is wrong
                                packageVersionPackage.IsLatestVersion         = false;
                                packageVersionPackage.IsAbsoluteLatestVersion = false;

                                versionToPackageTable[individualPackageVersion] = packageVersionPackage;
                            }
                        }
                    }
                }

                // Now manually set the latest versions
                if (packageEntryInfo.LatestVersion != null && versionToPackageTable.ContainsKey(packageEntryInfo.LatestVersion))
                {
                    versionToPackageTable[packageEntryInfo.LatestVersion].IsLatestVersion = true;
                }

                if (packageEntryInfo.AbsoluteLatestVersion != null && versionToPackageTable.ContainsKey(packageEntryInfo.AbsoluteLatestVersion))
                {
                    versionToPackageTable[packageEntryInfo.AbsoluteLatestVersion].IsAbsoluteLatestVersion = true;
                }

                // I think this is the best we can do for enumeration (reads all versions of a package before yielding anything)
                foreach (PackageBase package in versionToPackageTable.Values)
                {
                    yield return(package);
                }
            }
        }