Ejemplo n.º 1
0
        public CacheIndexModule(BaGetCompatibilityOptions compat)
        {
            Func <HttpRequest, HttpResponse, RouteData, Task> indexHandler = async(req, res, routeData) =>
            {
                await res.AsJson(new
                {
                    Version   = "3.0.0",
                    Resources =
                        ServiceWithAliases("PackagePublish", req.PackagePublish(prefix), "2.0.0") // api.nuget.org returns this too.
                        .Concat(ServiceWithAliases("RegistrationsBaseUrl", req.RegistrationsBase(prefix), "", "3.0.0-rc", "3.0.0-beta"))
                        .Concat(ServiceWithAliases("PackageBaseAddress", req.PackageBase(prefix), "3.0.0"))
                        .ToList()
                });
            };

            this.Get(prefix + "/v3/index.json", indexHandler);
            if (compat != null && compat.Enabled)
            {
                this.Get("/cache/v3/index.json", indexHandler);
            }
        }
Ejemplo n.º 2
0
        public IndexModule(BaGetCompatibilityOptions compat)
        {
            this._compat = compat;
            Func <HttpRequest, HttpResponse, RouteData, Task> indexHandler = async(req, res, routeData) =>
            {
                await res.AsJson(new
                {
                    Version   = "3.0.0",
                    Resources =
                        ServiceWithAliases("PackagePublish", req.PackagePublish("api"), "2.0.0")                                  // api.nuget.org returns this too.
                        .Concat(ServiceWithAliases("SearchQueryService", req.PackageSearch("api"), "", "3.0.0-beta", "3.0.0-rc")) // each version is an alias of others
                        .Concat(ServiceWithAliases("RegistrationsBaseUrl", req.RegistrationsBase("api"), "", "3.0.0-rc", "3.0.0-beta"))
                        .Concat(ServiceWithAliases("PackageBaseAddress", req.PackageBase("api"), "3.0.0"))
                        .Concat(ServiceWithAliases("SearchAutocompleteService", req.PackageAutocomplete("api"), "", "3.0.0-rc", "3.0.0-beta"))
                        .ToList()
                });
            };

            this.Get("/api/v3/index.json", indexHandler);
            if (_compat != null && _compat.Enabled)
            {
                this.Get("/v3/index.json", indexHandler);
            }
        }
Ejemplo n.º 3
0
        public PackagesV2Module(IODataPackageSerializer serializer, IPackageService packageService,
                                IPackageStorageService storage, IEdmModel odataModel,
                                ILogger <PackagesV2Module> logger, BaGetCompatibilityOptions compat)
        {
            this._compat         = compat;
            this._packageService = packageService;
            this._storage        = storage;
            this._log            = logger;
            this.GetCompat("/api/v2/contents/{id}/{version}", async(req, res, routeData) => {
                string id      = routeData.As <string>("id");
                string version = routeData.As <string>("version");

                if (!NuGetVersion.TryParse(version, out var nugetVersion))
                {
                    res.StatusCode = 400;
                    return;
                }

                var identity = new PackageIdentity(id, nugetVersion);

                if (!await packageService.IncrementDownloadCountAsync(identity))
                {
                    res.StatusCode = 404;
                    return;
                }
                var packageStream = await _storage.GetPackageStreamAsync(identity);

                await res.FromStream(packageStream, "application/zip");
            });

            Func <HttpRequest, HttpResponse, RouteData, Task> indexHandler = async(req, res, routeData) => {
                var    serviceUrl = GetServiceUrl(req);
                string text       = $@"<?xml version=""1.0"" encoding=""utf-8""?>
<service xml:base=""{serviceUrl}"" xmlns=""http://www.w3.org/2007/app"" xmlns:atom=""http://www.w3.org/2005/Atom""><workspace>
<atom:title type=""text"">Default</atom:title><collection href=""Packages""><atom:title type=""text"">Packages</atom:title></collection></workspace>
</service>";
                res.StatusCode  = 200;
                res.ContentType = "application/xml; charset=utf-8";
                await res.WriteAsync(text, new UTF8Encoding(false));
            };

            this.GetCompat("/api/v2/", indexHandler);

            this.GetCompat(@"/api/v2/FindPackagesById{query}", async(req, res, routeData) => {
                CancellationToken ct = CancellationToken.None;
                try {
                    var serviceUrl = GetServiceUrl(req);
                    var uriParser  = new ODataUriParser(odataModel, new Uri(serviceUrl), req.GetUri());
                    var path       = uriParser.ParsePath();
                    if (path.FirstSegment.Identifier == "FindPackagesById")
                    {
                        var idOrNull = uriParser.CustomQueryOptions.FirstOrDefault(o => o.Key.ToLowerInvariant() == "id").Value;
                        string id    = idOrNull.TrimStart('\'').TrimEnd('\'');
                        _log.LogDebug("Request to FindPackagesById id={0}", id);
                        var found = await _packageService.FindAsync(id, false, true);
                        var odata = new ODataResponse <IEnumerable <PackageWithUrls> >(serviceUrl, found.Select(f => ToPackageWithUrls(req, f)));
                        using (var ms = new MemoryStream()) {
                            serializer.Serialize(ms, odata.Entity, odata.ServiceBaseUrl);
                            ms.Seek(0, SeekOrigin.Begin);
                            await res.FromStream(ms, atomXmlContentType);
                        }
                    }
                    else
                    {
                        res.StatusCode = 400;
                    }
                }
                catch (ODataException odataPathError) {
                    _log.LogError("Bad odata query", odataPathError);
                    res.StatusCode = 400;
                }
            });

            this.GetCompat(@"/api/v2/Packages{query}", async(req, res, routeData) => {
                try {
                    var serviceUrl = GetServiceUrl(req);
                    var uriParser  = new ODataUriParser(odataModel, new Uri(serviceUrl), req.GetUri());
                    var path       = uriParser.ParsePath();
                    if (path.FirstSegment.Identifier == "Packages")
                    {
                        if (path.Count == 2 && path.LastSegment is KeySegment)
                        {
                            KeySegment queryParams = (KeySegment)path.LastSegment;
                            string id      = queryParams.Keys.First(k => k.Key == "Id").Value as string;
                            string version = queryParams.Keys.First(k => k.Key == "Version").Value as string;
                            _log.LogDebug("Request to find package by id={0} and version={1}", id, version);
                            var identity = new PackageIdentity(id, NuGetVersion.Parse(version));
                            var found    = await _packageService.FindOrNullAsync(identity, false, true);
                            if (found == null)
                            {
                                res.StatusCode = 404;
                                return;
                            }
                            var odataPackage = new ODataResponse <PackageWithUrls>(serviceUrl, ToPackageWithUrls(req, found));
                            using (var ms = new MemoryStream()) {
                                serializer.Serialize(ms, odataPackage.Entity.Pkg,
                                                     odataPackage.ServiceBaseUrl, odataPackage.Entity.ResourceIdUrl, odataPackage.Entity.PackageContentUrl);
                                ms.Seek(0, SeekOrigin.Begin);
                                await res.FromStream(ms, atomXmlContentType);
                            }
                        }
                        else
                        {
                            res.StatusCode = 400;
                        }
                    }
                    else
                    {
                        res.StatusCode = 400;
                    }
                }
                catch (ODataException odataPathError) {
                    _log.LogError("Bad odata query", odataPathError);
                    res.StatusCode = 400;
                }
            });
        }