public override async Task <Tuple <bool, INuGetResource> > TryCreate(
            SourceRepository source,
            CancellationToken token)
        {
            ListResource resource = null;

            var serviceIndex = await source.GetResourceAsync <ServiceIndexResourceV3>(token);

            if (serviceIndex != null)
            {
                var baseUrl = serviceIndex.GetServiceEntryUri(ServiceTypes.LegacyGallery);
                if (baseUrl != null)
                {
                    var httpSource = await source.GetResourceAsync <HttpSourceResource>(token);

                    var serviceDocument =
                        await ODataServiceDocumentUtils.CreateODataServiceDocumentResourceV2(
                            baseUrl.AbsoluteUri, httpSource.HttpSource, DateTime.UtcNow, NullLogger.Instance, token);

                    var parser = new V2FeedParser(httpSource.HttpSource, serviceDocument.BaseAddress, source.PackageSource.Source);
                    var feedCapabilityResource = new LegacyFeedCapabilityResourceV2Feed(parser, serviceDocument.BaseAddress);
                    resource = new V2FeedListResource(parser, feedCapabilityResource, serviceDocument.BaseAddress);
                }
            }

            var result = new Tuple <bool, INuGetResource>(resource != null, resource);

            return(result);
        }
        private static async Task <IEnumerable <VersionInfo> > GetVersions(
            V2FeedPackageInfo package,
            MetadataReferenceCache metadataCache,
            SearchFilter filter,
            V2FeedParser feedParser,
            Common.ILogger log,
            CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();
            // apply the filters to the version list returned
            var packages = await feedParser.FindPackagesByIdAsync(
                package.Id,
                filter.IncludeDelisted,
                filter.IncludePrerelease,
                log,
                cancellationToken);

            var uniqueVersions = new HashSet <NuGetVersion>();
            var results        = new List <VersionInfo>();

            foreach (var versionPackage in packages.OrderByDescending(p => p.Version))
            {
                if (uniqueVersions.Add(versionPackage.Version))
                {
                    var versionInfo = new VersionInfo(versionPackage.Version, versionPackage.DownloadCount)
                    {
                        PackageSearchMetadata = new PackageSearchMetadataV2Feed(versionPackage, metadataCache)
                    };

                    results.Add(versionInfo);
                }
            }
            return(results);
        }
 public LegacyFeedCapabilityResourceV2Feed(V2FeedParser feedParser, string baseAddress)
 {
     _feedParser  = feedParser;
     _metadataUri = string.Format(
         CultureInfo.InvariantCulture,
         MetadataUriFormat,
         baseAddress);
 }
Exemple #4
0
        public DownloadResourceV2Feed(V2FeedParser feedParser)
        {
            if (feedParser == null)
            {
                throw new ArgumentNullException(nameof(feedParser));
            }

            _feedParser = feedParser;
        }
Exemple #5
0
        public DependencyInfoResourceV2Feed(V2FeedParser feedParser, SourceRepository source)
        {
            if (feedParser == null)
            {
                throw new ArgumentNullException(nameof(feedParser));
            }

            _feedParser = feedParser;
            _source     = source;
        }
Exemple #6
0
 public static string GetBaseAddress(Stream stream)
 {
     try
     {
         XDocument serviceDocumentXml = V2FeedParser.LoadXml(stream);
         return(serviceDocumentXml.Document.Root.Attribute(_xnameXmlBase)?.Value);
     }
     catch (XmlException)
     {
         return(null);
     }
 }
        public static IPackageSearchMetadata CreatePackageSearchResult(
            V2FeedPackageInfo package,
            SearchFilter filter,
            V2FeedParser feedParser,
            Common.ILogger log,
            CancellationToken cancellationToken)
        {
            var metadata = new PackageSearchMetadataV2Feed(package);

            return(metadata
                   .WithVersions(() => GetVersions(package, filter, feedParser, log, cancellationToken)));
        }
        public PackageSearchResourceV2Feed(HttpSourceResource httpSourceResource, string baseAddress, Configuration.PackageSource packageSource)
        {
            if (httpSourceResource == null)
            {
                throw new ArgumentNullException(nameof(httpSourceResource));
            }

            if (packageSource == null)
            {
                throw new ArgumentNullException(nameof(packageSource));
            }

            _httpSource    = httpSourceResource.HttpSource;
            _packageSource = packageSource;
            _feedParser    = new V2FeedParser(_httpSource, baseAddress, packageSource);
        }
        public override async Task <Tuple <bool, INuGetResource> > TryCreate(SourceRepository source, CancellationToken token)
        {
            DownloadResource resource = null;

            if (await source.GetFeedType(token) == FeedType.HttpV2)
            {
                var serviceDocument = await source.GetResourceAsync <ODataServiceDocumentResourceV2>(token);

                var httpSource = await source.GetResourceAsync <HttpSourceResource>(token);

                var parser = new V2FeedParser(httpSource.HttpSource, serviceDocument.BaseAddress, source.PackageSource.Source);

                resource = new DownloadResourceV2Feed(parser, source.PackageSource.Source);
            }

            return(new Tuple <bool, INuGetResource>(resource != null, resource));
        }
Exemple #10
0
        private static async Task <IEnumerable <VersionInfo> > GetVersions(
            V2FeedPackageInfo package,
            MetadataReferenceCache metadataCache,
            SearchFilter filter,
            V2FeedParser feedParser,
            Common.ILogger log,
            CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();

            using (var sourceCacheContext = new SourceCacheContext())
            {
                // Update http source cache context MaxAge so that it can always go online to fetch
                // latest version of packages.
                sourceCacheContext.MaxAge = DateTimeOffset.UtcNow;

                // apply the filters to the version list returned
                var packages = await feedParser.FindPackagesByIdAsync(
                    package.Id,
                    filter.IncludeDelisted,
                    filter.IncludePrerelease,
                    sourceCacheContext,
                    log,
                    cancellationToken);

                var uniqueVersions = new HashSet <NuGetVersion>();
                var results        = new List <VersionInfo>();

                foreach (var versionPackage in packages.OrderByDescending(p => p.Version))
                {
                    if (uniqueVersions.Add(versionPackage.Version))
                    {
                        var versionInfo = new VersionInfo(versionPackage.Version, versionPackage.DownloadCount)
                        {
                            PackageSearchMetadata = new PackageSearchMetadataV2Feed(versionPackage, metadataCache)
                        };

                        results.Add(versionInfo);
                    }
                }
                return(results);
            }
        }
        private async Task <IEnumerable <PackageInfo> > FindPackagesByIdAsyncCore(
            string id,
            SourceCacheContext cacheContext,
            ILogger logger,
            CancellationToken cancellationToken)
        {
            for (var retry = 0; retry < 3; ++retry)
            {
                var uri = _baseUri + "FindPackagesById()?id='" + id + "'";
                var httpSourceCacheContext = HttpSourceCacheContext.Create(cacheContext, retry);

                try
                {
                    var results = new List <PackageInfo>();
                    var uris    = new HashSet <string>(StringComparer.OrdinalIgnoreCase);

                    uris.Add(uri);
                    var page   = 1;
                    var paging = true;
                    while (paging)
                    {
                        // TODO: Pages for a package ID are cached separately.
                        // So we will get inaccurate data when a page shrinks.
                        // However, (1) In most cases the pages grow rather than shrink;
                        // (2) cache for pages is valid for only 30 min.
                        // So we decide to leave current logic and observe.
                        paging = await _httpSource.GetAsync(
                            new HttpSourceCachedRequest(
                                uri,
                                $"list_{id.ToLowerInvariant()}_page{page}",
                                httpSourceCacheContext)
                        {
                            AcceptHeaderValues =
                            {
                                new MediaTypeWithQualityHeaderValue("application/atom+xml"),
                                new MediaTypeWithQualityHeaderValue("application/xml")
                            },
                            EnsureValidContents = stream => HttpStreamValidation.ValidateXml(uri, stream),
                            MaxTries            = 1
                        },
                            async httpSourceResult =>
                        {
                            if (httpSourceResult.Status == HttpSourceResultStatus.NoContent)
                            {
                                // Team city returns 204 when no versions of the package exist
                                // This should result in an empty list and we should not try to
                                // read the stream as xml.
                                return(false);
                            }

                            var doc = await V2FeedParser.LoadXmlAsync(httpSourceResult.Stream);

                            var result = doc.Root
                                         .Elements(_xnameEntry)
                                         .Select(x => BuildModel(id, x))
                                         .Where(x => x != null);

                            results.AddRange(result);

                            // Find the next url for continuation
                            var nextUri = V2FeedParser.GetNextUrl(doc);

                            // Stop if there's nothing else to GET
                            if (string.IsNullOrEmpty(nextUri))
                            {
                                return(false);
                            }

                            // check for any duplicate url and error out
                            if (!uris.Add(nextUri))
                            {
                                throw new FatalProtocolException(string.Format(
                                                                     CultureInfo.CurrentCulture,
                                                                     Strings.Protocol_duplicateUri,
                                                                     nextUri));
                            }

                            uri = nextUri;
                            page++;

                            return(true);
                        },
                            logger,
                            cancellationToken);
                    }

                    return(results);
                }
                catch (Exception ex) when(retry < 2)
                {
                    var message = string.Format(CultureInfo.CurrentCulture, Strings.Log_RetryingFindPackagesById, nameof(FindPackagesByIdAsyncCore), uri)
                                  + Environment.NewLine
                                  + ExceptionUtilities.DisplayMessage(ex);

                    logger.LogMinimal(message);
                }
                catch (Exception ex) when(retry == 2)
                {
                    var message = string.Format(
                        CultureInfo.CurrentCulture,
                        Strings.Log_FailedToRetrievePackage,
                        id,
                        uri);

                    throw new FatalProtocolException(message, ex);
                }
            }

            return(null);
        }
 public DownloadResourceV2FeedPrivate(V2FeedParser parser, HttpSource source)
 {
     _parser = parser;
     _source = source;
 }
Exemple #13
0
 public DownloadResourceV2Feed(V2FeedParser feedParser)
     : this(feedParser, source : null)
 {
 }