Exemplo n.º 1
0
        public virtual async Task <ActionResult> ListPackages(string q, int page = 1)
        {
            if (page < 1)
            {
                page = 1;
            }

            q = (q ?? "").Trim();

            var searchFilter = SearchAdaptor.GetSearchFilter(q, page, sortOrder: null, context: SearchFilter.UISearchContext);
            var results      = await _searchService.Search(searchFilter);

            int totalHits = results.Hits;

            if (page == 1 && !results.Data.Any())
            {
                // In the event the index wasn't updated, we may get an incorrect count.
                totalHits = 0;
            }

            var viewModel = new PackageListViewModel(
                results.Data,
                results.IndexTimestampUtc,
                q,
                totalHits,
                page - 1,
                Constants.DefaultPackageListPageSize,
                Url);

            ViewBag.SearchTerm = q;

            return(View(viewModel));
        }
Exemplo n.º 2
0
            public void ReturnsDefaultSearchFilter()
            {
                const string query             = "someQuery";
                const int    page              = 1;
                const bool   includePrerelease = true;
                const string packageType       = "";
                const string sortOrder         = GalleryConstants.SearchSortNames.Relevance;
                const string context           = "someContext";
                const string semVerLevel       = "someSemVer";

                var searchFilter = SearchAdaptor.GetSearchFilter(
                    query,
                    page,
                    includePrerelease,
                    packageType,
                    sortOrder,
                    context,
                    semVerLevel);

                Assert.Equal(query, searchFilter.SearchTerm);
                Assert.Equal((page - 1) * GalleryConstants.DefaultPackageListPageSize, searchFilter.Skip);
                Assert.Equal(GalleryConstants.DefaultPackageListPageSize, searchFilter.Take);
                Assert.Equal(includePrerelease, searchFilter.IncludePrerelease);
                Assert.Equal(context, searchFilter.Context);
                Assert.Equal(semVerLevel, searchFilter.SemVerLevel);
                Assert.Equal(string.Empty, searchFilter.PackageType);
                Assert.Equal(SortOrder.Relevance, searchFilter.SortOrder);
            }
Exemplo n.º 3
0
        public virtual async Task <ActionResult> ListPackages(string curatedFeedName, string q, int page = 1, bool prerel = true)
        {
            if (page < 1)
            {
                page = 1;
            }

            q = (q ?? string.Empty).Trim();

            var searchFilter = SearchAdaptor.GetSearchFilter(
                q,
                page,
                includePrerelease: prerel,
                sortOrder: null,
                context: SearchFilter.UISearchContext,
                semVerLevel: SemVerLevelKey.SemVerLevel2);

            searchFilter.CuratedFeed = CuratedFeedService.GetFeedByName(curatedFeedName, includePackages: false);
            if (searchFilter.CuratedFeed == null)
            {
                return(HttpNotFound());
            }

            SearchResults results = await SearchService.Search(searchFilter);

            int totalHits = results.Hits;

            if (page == 1 && !results.Data.Any())
            {
                // In the event the index wasn't updated, we may get an incorrect count.
                totalHits = 0;
            }

            var viewModel = new PackageListViewModel(
                results.Data,
                GetCurrentUser(),
                results.IndexTimestampUtc,
                q,
                totalHits,
                page - 1,
                Constants.DefaultPackageListPageSize,
                Url,
                curatedFeedName,
                includePrerelease: prerel);

            ViewBag.SearchTerm = q;

            return(View("ListPackages", viewModel));
        }
Exemplo n.º 4
0
        private async Task <SearchResults> SearchAsync(ISearchService searchService, string searchTerm)
        {
            await Task.Yield();

            var searchFilter = SearchAdaptor.GetSearchFilter(
                searchTerm,
                page: 1,
                includePrerelease: true,
                sortOrder: null,
                context: SearchFilter.UISearchContext,
                semVerLevel: SemVerLevelKey.SemVerLevel2);

            searchFilter.Take = 10;

            return(await searchService.Search(searchFilter));
        }
Exemplo n.º 5
0
        public virtual ActionResult ListPackages(string curatedFeedName, string q, string sortOrder = null, int page = 1, bool prerelease = false)
        {
            if (page < 1)
            {
                page = 1;
            }

            q = (q ?? "").Trim();

            if (String.IsNullOrEmpty(sortOrder))
            {
                // Determine the default sort order. If no query string is specified, then the sortOrder is DownloadCount
                // If we are searching for something, sort by relevance.
                sortOrder = q.IsEmpty() ? Constants.PopularitySortOrder : Constants.RelevanceSortOrder;
            }

            var searchFilter = SearchAdaptor.GetSearchFilter(q, sortOrder, page, prerelease);

            searchFilter.CuratedFeedKey = CuratedFeedService.GetKey(curatedFeedName);
            if (searchFilter.CuratedFeedKey == 0)
            {
                return(HttpNotFound());
            }

            int totalHits;
            IQueryable <Package> packageVersions = SearchService.Search(searchFilter, out totalHits);

            if (page == 1 && !packageVersions.Any())
            {
                // In the event the index wasn't updated, we may get an incorrect count.
                totalHits = 0;
            }

            var viewModel = new PackageListViewModel(
                packageVersions,
                q,
                sortOrder,
                totalHits,
                page - 1,
                Constants.DefaultPackageListPageSize,
                Url,
                prerelease);

            ViewBag.SearchTerm = q;

            return(View("~/Views/Packages/ListPackages.cshtml", viewModel));
        }
Exemplo n.º 6
0
            public void MapsAllSortOrders(SortOrder sortOrder)
            {
                var searchFilter = SearchAdaptor.GetSearchFilter(
                    q: string.Empty,
                    page: 1,
                    includePrerelease: true,
                    packageType: "Dependency",
                    sortOrder: SortNames[sortOrder],
                    context: string.Empty,
                    semVerLevel: "SomeSemVer");

                Assert.Equal(string.Empty, searchFilter.SearchTerm);
                Assert.Equal(0, searchFilter.Skip);
                Assert.Equal(GalleryConstants.DefaultPackageListPageSize, searchFilter.Take);
                Assert.Equal(true, searchFilter.IncludePrerelease);
                Assert.Equal(string.Empty, searchFilter.Context);
                Assert.Equal("SomeSemVer", searchFilter.SemVerLevel);
                Assert.Equal("Dependency", searchFilter.PackageType);
                Assert.Equal(sortOrder, searchFilter.SortOrder);
            }
Exemplo n.º 7
0
            public void ReturnsDefaultSearchFilterOnNull()
            {
                var searchFilter = SearchAdaptor.GetSearchFilter(
                    q: null,
                    page: -1,
                    includePrerelease: true,
                    packageType: null,
                    sortOrder: null,
                    context: null,
                    semVerLevel: null);

                Assert.Null(searchFilter.SearchTerm);
                Assert.Equal(0, searchFilter.Skip);
                Assert.Equal(GalleryConstants.DefaultPackageListPageSize, searchFilter.Take);
                Assert.True(searchFilter.IncludePrerelease);
                Assert.Null(searchFilter.Context);
                Assert.Null(searchFilter.SemVerLevel);
                Assert.Equal(string.Empty, searchFilter.PackageType);
                Assert.Equal(SortOrder.Relevance, searchFilter.SortOrder);
            }
Exemplo n.º 8
0
        public virtual async Task <ActionResult> ListPackages(string q, int page = 1)
        {
            if (page < 1)
            {
                page = 1;
            }

            q = (q ?? string.Empty).Trim();

            // We are not going to SQL here anyway, but our request logs do show some attempts to SQL injection.
            // The below code just fails out those requests early.
            if (q.ToLowerInvariant().Contains("char(") ||
                q.ToLowerInvariant().Contains("union select") ||
                q.ToLowerInvariant().Contains("/*") ||
                q.ToLowerInvariant().Contains("--"))
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }

            SearchResults results;

            // fetch most common query from cache to relieve load on the search service
            if (string.IsNullOrEmpty(q) && page == 1)
            {
                var cachedResults = HttpContext.Cache.Get("DefaultSearchResults");
                if (cachedResults == null)
                {
                    var searchFilter = SearchAdaptor.GetSearchFilter(q, page, null, SearchFilter.UISearchContext);
                    results = await _searchService.Search(searchFilter);

                    // note: this is a per instance cache
                    HttpContext.Cache.Add(
                        "DefaultSearchResults",
                        results,
                        null,
                        DateTime.UtcNow.AddMinutes(10),
                        Cache.NoSlidingExpiration,
                        CacheItemPriority.Default, null);
                }
                else
                {
                    // default for /packages view
                    results = (SearchResults)cachedResults;
                }
            }
            else
            {
                var searchFilter = SearchAdaptor.GetSearchFilter(q, page, null, SearchFilter.UISearchContext);
                results = await _searchService.Search(searchFilter);
            }

            int totalHits = results.Hits;

            if (page == 1 && !results.Data.Any())
            {
                // In the event the index wasn't updated, we may get an incorrect count.
                totalHits = 0;
            }

            var viewModel = new PackageListViewModel(
                results.Data,
                results.IndexTimestampUtc,
                q,
                totalHits,
                page - 1,
                Constants.DefaultPackageListPageSize,
                Url);

            ViewBag.SearchTerm = q;

            return(View(viewModel));
        }
Exemplo n.º 9
0
        public virtual async Task <ActionResult> DisplayPackage(string id, string version)
        {
            string normalized = NuGetVersionNormalizer.Normalize(version);

            if (!string.Equals(version, normalized))
            {
                // Permanent redirect to the normalized one (to avoid multiple URLs for the same content)
                return(RedirectToActionPermanent("DisplayPackage", new { id = id, version = normalized }));
            }

            var package = _packageService.FindPackageByIdAndVersion(id, version);

            if (package == null)
            {
                return(HttpNotFound());
            }

            var packageHistory = package.PackageRegistration.Packages.ToList()
                                 .OrderByDescending(p => new NuGetVersion(p.Version));

            var model = new DisplayPackageViewModel(package, packageHistory);

            if (package.IsOwner(User))
            {
                // Tell logged-in package owners not to cache the package page,
                // so they won't be confused about the state of pending edits.
                Response.Cache.SetCacheability(HttpCacheability.NoCache);
                Response.Cache.SetNoStore();
                Response.Cache.SetMaxAge(TimeSpan.Zero);
                Response.Cache.SetRevalidation(HttpCacheRevalidation.AllCaches);

                var pendingMetadata = _editPackageService.GetPendingMetadata(package);
                if (pendingMetadata != null)
                {
                    model.SetPendingMetadata(pendingMetadata);
                }
            }

            var externalSearchService = _searchService as ExternalSearchService;

            if (_searchService.ContainsAllVersions && externalSearchService != null)
            {
                var isIndexedCacheKey = $"IsIndexed_{package.PackageRegistration.Id}_{package.Version}";
                var isIndexed         = HttpContext.Cache.Get(isIndexedCacheKey) as bool?;
                if (!isIndexed.HasValue)
                {
                    var searchFilter = SearchAdaptor.GetSearchFilter(
                        "id:\"" + package.PackageRegistration.Id + "\" AND version:\"" + package.Version + "\"",
                        1, null, SearchFilter.ODataSearchContext);
                    var results = await externalSearchService.RawSearch(searchFilter);

                    isIndexed = results.Hits > 0;

                    var expiration = Cache.NoAbsoluteExpiration;
                    if (!isIndexed.Value)
                    {
                        expiration = DateTime.UtcNow.Add(TimeSpan.FromSeconds(30));
                    }

                    HttpContext.Cache.Add(isIndexedCacheKey,
                                          isIndexed,
                                          null,
                                          expiration,
                                          Cache.NoSlidingExpiration,
                                          CacheItemPriority.Default, null);
                }

                model.IsIndexed = isIndexed;
            }

            ViewBag.FacebookAppID = _config.FacebookAppId;
            return(View(model));
        }
Exemplo n.º 10
0
        public virtual async Task <ActionResult> ListPackages(string q, int page = 1)
        {
            if (page < 1)
            {
                page = 1;
            }

            q = (q ?? string.Empty).Trim();

            SearchResults results;

            // fetch most common query from cache to relieve load on the search service
            if (string.IsNullOrEmpty(q) && page == 1)
            {
                var cachedResults = HttpContext.Cache.Get("DefaultSearchResults");
                if (cachedResults == null)
                {
                    var searchFilter = SearchAdaptor.GetSearchFilter(q, page, null, SearchFilter.UISearchContext);
                    results = await _searchService.Search(searchFilter);

                    // note: this is a per instance cache
                    HttpContext.Cache.Add(
                        "DefaultSearchResults",
                        results,
                        null,
                        DateTime.UtcNow.AddMinutes(10),
                        Cache.NoSlidingExpiration,
                        CacheItemPriority.Default, null);
                }
                else
                {
                    // default for /packages view
                    results = (SearchResults)cachedResults;
                }
            }
            else
            {
                var searchFilter = SearchAdaptor.GetSearchFilter(q, page, null, SearchFilter.UISearchContext);
                results = await _searchService.Search(searchFilter);
            }

            int totalHits = results.Hits;

            if (page == 1 && !results.Data.Any())
            {
                // In the event the index wasn't updated, we may get an incorrect count.
                totalHits = 0;
            }

            var viewModel = new PackageListViewModel(
                results.Data,
                results.IndexTimestampUtc,
                q,
                totalHits,
                page - 1,
                Constants.DefaultPackageListPageSize,
                Url);

            ViewBag.SearchTerm = q;

            return(View(viewModel));
        }