Ejemplo n.º 1
0
        public async Task ListAvailable(InterceptCallContext context)
        {
            string indexUrl = _listAvailableLatestStableIndex;

            if (!context.Args.IsLatestVersion)
            {
                indexUrl = _listAvailableAllIndex;
            }
            else if (context.Args.IncludePrerelease)
            {
                indexUrl = _listAvailableLatestPrereleaseIndex;
            }

            var index = await FetchJson(context, new Uri(indexUrl));

            var data = GetListAvailableData(context, index);

            // all versions with no pre
            if (!context.Args.IsLatestVersion && !context.Args.IncludePrerelease)
            {
                data = data.Where(p => (new NuGetVersion(p["version"].ToString())).IsPrerelease == false);
            }

            string nextUrl = null;

            // Convert to a list after calling Take to avoid enumerating the list multiple times.

            if (context.Args.Top.HasValue && context.Args.Top.Value > 0)
            {
                data = data.Take(context.Args.Top.Value).ToList();
            }
            else
            {
                data = data.Take(30).ToList();

                if (data.Count() >= 30)
                {
                    var last = data.LastOrDefault();

                    if (last != null)
                    {
                        string argsWithoutSkipToken = String.Join("&", context.Args.Arguments.Where(a => a.Key.ToLowerInvariant() != "$skiptoken")
                                                                  .Select(a => String.Format(CultureInfo.InvariantCulture, "{0}={1}", a.Key, a.Value)));

                        nextUrl = String.Format(CultureInfo.InvariantCulture, "{0}?{1}&$skiptoken='{2}','{2}','{3}'",
                                                context.RequestUri.AbsoluteUri.Split('?')[0],
                                                argsWithoutSkipToken,
                                                last["id"],
                                                last["version"]);
                    }
                }
            }

            XElement feed = InterceptFormatting.MakeFeed(_passThroughAddress, "Packages", data, data.Select(e => e["id"].ToString()).ToArray(), nextUrl);
            await context.WriteResponse(feed);
        }
Ejemplo n.º 2
0
        public async Task Search(InterceptCallContext context, string searchTerm, bool isLatestVersion, string targetFramework, bool includePrerelease, int skip, int take, string feedName)
        {
            context.Log(string.Format(CultureInfo.InvariantCulture, "[V3 CALL] Search: {0} ({1},{2})", searchTerm, skip, take), ConsoleColor.Magenta);

            JObject obj = await FetchJson(context, MakeSearchAddress(searchTerm, isLatestVersion, targetFramework, includePrerelease, skip, take, feedName));

            IEnumerable <JToken> data = (obj != null) ? data = obj["data"] : Enumerable.Empty <JToken>();

            XElement feed = InterceptFormatting.MakeFeedFromSearch(_source, _passThroughAddress, "Packages", data, "");
            await context.WriteResponse(feed);
        }
Ejemplo n.º 3
0
        public async Task GetUpdates(InterceptCallContext context, string[] packageIds, string[] versions, string[] versionConstraints, string[] targetFrameworks, bool includePrerelease, bool includeAllVersions, bool count = false)
        {
            context.Log(string.Format(CultureInfo.InvariantCulture, "[V3 CALL] GetUpdates{1}: {0}", string.Join("|", packageIds), count ? "Count" : string.Empty), ConsoleColor.Magenta);

            var packages = await GetUpdatesCore(context, packageIds, versions, versionConstraints, targetFrameworks, includePrerelease, includeAllVersions);

            if (count)
            {
                await context.WriteResponse(packages.Count);
            }
            else
            {
                XElement feed = InterceptFormatting.MakeFeed(_passThroughAddress, "GetUpdates", packages, packages.Select(p => p["id"].ToString()).ToArray());
                await context.WriteResponse(feed);
            }
        }
Ejemplo n.º 4
0
        public async Task GetPackage(InterceptCallContext context, string id, string version, string feedName)
        {
            context.Log(string.Format(CultureInfo.InvariantCulture, "[V3 CALL] GetPackage: {0} {1}", id, version), ConsoleColor.Magenta);

            var desiredPackage = await GetPackageCore(context, id, version);

            if (desiredPackage == null)
            {
                throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, "unable to find version {0} of package {1}", version, id));
            }

            XElement feed = InterceptFormatting.MakeFeed(_passThroughAddress, "Packages", new List <JToken> {
                desiredPackage
            }, id);
            await context.WriteResponse(feed);
        }
Ejemplo n.º 5
0
        public async Task GetAllPackageVersions(InterceptCallContext context, string id)
        {
            context.Log(string.Format(CultureInfo.InvariantCulture, "[V3 CALL] GetAllPackageVersions: {0}", id), ConsoleColor.Magenta);

            var ids = id.Split(new string[] { " or " }, StringSplitOptions.RemoveEmptyEntries);

            List <JToken> packages = new List <JToken>();

            foreach (var s in ids)
            {
                string curId = s.Trim('\'');

                if (curId.StartsWith("tolower(id) eq '"))
                {
                    curId = curId.Split('\'')[1];
                }

                // TODO: run in parallel
                JObject resolverBlob = await FetchJson(context, MakeResolverAddress(curId));

                if (resolverBlob == null)
                {
                    throw new InvalidOperationException(string.Format("package {0} not found", curId));
                }

                foreach (var p in resolverBlob["packages"])
                {
                    NuGetVersion version = NuGetVersion.Parse(p["version"].ToString());

                    // all versions are returned, filter to only stable if needed
                    if (context.Args.IncludePrerelease || !version.IsPrerelease)
                    {
                        p["id"] = resolverBlob["id"];

                        packages.Add(p);
                    }
                }
            }

            var data = packages.OrderBy(p => p["id"].ToString()).ThenByDescending(p => NuGetVersion.Parse(p["version"].ToString()), VersionComparer.VersionRelease);

            XElement feed = InterceptFormatting.MakeFeed(_passThroughAddress, "Packages", data, data.Select(p => p["id"].ToString()).ToArray());
            await context.WriteResponse(feed);
        }
Ejemplo n.º 6
0
        public override Task WriteResponseAsync(byte[] data)
        {
            return(Task.Run(() =>
            {
                if (IsBatchRequest)
                {
                    // batch requests need to be wrapped
                    ResponseContentType = String.Format(CultureInfo.InvariantCulture, "multipart/mixed;boundary=batchresponse_{0}", _batchBoundaryId);
                    string s = InterceptFormatting.MakeBatchEntry(_batchBoundaryId, data);
                    _data = new MemoryStream(Encoding.UTF8.GetBytes(s));
                }
                else
                {
                    _data = new MemoryStream(data);
                }

                _sem.Set();
            }));
        }
Ejemplo n.º 7
0
        public async Task GetLatestVersionPackage(InterceptCallContext context, string id, bool includePrerelease)
        {
            context.Log(string.Format(CultureInfo.InvariantCulture, "[V3 CALL] GetLatestVersionPackage: {0} {1}", id, includePrerelease ? "[include prerelease]" : ""), ConsoleColor.Magenta);

            JObject resolverBlob = await FetchJson(context, MakeResolverAddress(id));

            if (resolverBlob == null)
            {
                throw new InvalidOperationException(string.Format("package {0} not found", id));
            }

            JToken latest = ExtractLatestVersion(resolverBlob, includePrerelease);

            if (latest == null)
            {
                throw new InvalidOperationException(string.Format("package {0} not found", id));
            }

            XElement feed = InterceptFormatting.MakeFeed(_passThroughAddress, "Packages", new List <JToken> {
                latest
            }, id);
            await context.WriteResponse(feed);
        }