/
V2Feed.svc.cs
185 lines (161 loc) · 8.33 KB
/
V2Feed.svc.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Services;
using System.Linq;
using System.Runtime.Versioning;
using System.ServiceModel.Web;
using System.Web.Mvc;
using System.Web.Routing;
using NuGet;
using NuGetGallery.Helpers;
namespace NuGetGallery
{
public class V2Feed : FeedServiceBase<V2FeedPackage>
{
private const int FeedVersion = 2;
public V2Feed()
{
}
public V2Feed(IEntitiesContext entities, IEntityRepository<Package> repo, IConfiguration configuration, ISearchService searchService)
: base(entities, repo, configuration, searchService)
{
}
protected override FeedContext<V2FeedPackage> CreateDataSource()
{
return new FeedContext<V2FeedPackage>
{
Packages = PackageRepository.GetAll()
.WithoutVersionSort()
.ToV2FeedPackageQuery(Configuration.GetSiteRoot(UseHttps()))
};
}
public static void InitializeService(DataServiceConfiguration config)
{
InitializeServiceBase(config);
config.SetServiceOperationAccessRule("GetUpdates", ServiceOperationRights.AllRead);
}
[WebGet]
public IQueryable<V2FeedPackage> Search(string searchTerm, string targetFramework, bool includePrerelease)
{
var packages = PackageRepository.GetAll()
.Include(p => p.PackageRegistration)
.Include(p => p.PackageRegistration.Owners)
.Where(p => p.Listed);
return SearchAdaptor.SearchCore(SearchService, HttpContext.Request, SiteRoot, packages, searchTerm, targetFramework, includePrerelease).ToV2FeedPackageQuery(GetSiteRoot());
}
[WebGet]
public IQueryable<V2FeedPackage> FindPackagesById(string id)
{
return PackageRepository.GetAll().Include(p => p.PackageRegistration)
.Where(p => p.PackageRegistration.Id.Equals(id, StringComparison.OrdinalIgnoreCase))
.ToV2FeedPackageQuery(GetSiteRoot());
}
[WebGet]
public IQueryable<V2FeedPackage> GetUpdates(
string packageIds, string versions, bool includePrerelease, bool includeAllVersions, string targetFrameworks, string versionConstraints)
{
if (String.IsNullOrEmpty(packageIds) || String.IsNullOrEmpty(versions))
{
return Enumerable.Empty<V2FeedPackage>().AsQueryable();
}
// Workaround https://github.com/NuGet/NuGetGallery/issues/674 for NuGet 2.1 client. Can probably eventually be retired (when nobody uses 2.1 anymore...)
// Note - it was URI un-escaping converting + to ' ', undoing that is actually a pretty conservative substitution because
// space characters are never acepted as valid by VersionUtility.ParseFrameworkName.
if (!string.IsNullOrEmpty(targetFrameworks))
{
targetFrameworks = targetFrameworks.Replace(' ', '+');
}
var idValues = packageIds.Trim().Split(new[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
var versionValues = versions.Trim().Split(new[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
var targetFrameworkValues = String.IsNullOrEmpty(targetFrameworks)
? null
: targetFrameworks.Split('|').Select(VersionUtility.ParseFrameworkName).ToList();
var versionConstraintValues = String.IsNullOrEmpty(versionConstraints)
? new string[idValues.Length]
: versionConstraints.Split('|');
if (idValues.Length == 0 || idValues.Length != versionValues.Length || idValues.Length != versionConstraintValues.Length)
{
// Exit early if the request looks invalid
return Enumerable.Empty<V2FeedPackage>().AsQueryable();
}
var versionLookup = new Dictionary<string, Tuple<SemanticVersion, IVersionSpec>>(idValues.Length, StringComparer.OrdinalIgnoreCase);
for (int i = 0; i < idValues.Length; i++)
{
var id = idValues[i];
if (versionLookup.ContainsKey(id))
{
// Exit early if the request contains duplicate ids
return Enumerable.Empty<V2FeedPackage>().AsQueryable();
}
SemanticVersion currentVersion = null;
if (SemanticVersion.TryParse(versionValues[i], out currentVersion))
{
IVersionSpec versionConstraint = null;
if (versionConstraintValues[i] != null)
{
if (!VersionUtility.TryParseVersionSpec(versionConstraintValues[i], out versionConstraint))
{
versionConstraint = null;
}
}
versionLookup.Add(id, Tuple.Create(currentVersion, versionConstraint));
}
}
var packages = PackageRepository.GetAll()
.Include(p => p.PackageRegistration)
.Include(p => p.SupportedFrameworks)
.Where(p => p.Listed && (includePrerelease || !p.IsPrerelease) && idValues.Contains(p.PackageRegistration.Id))
.OrderBy(p => p.PackageRegistration.Id);
return GetUpdates(packages, versionLookup, targetFrameworkValues, includeAllVersions).AsQueryable()
.ToV2FeedPackageQuery(GetSiteRoot());
}
private static IEnumerable<Package> GetUpdates(
IEnumerable<Package> packages,
Dictionary<string, Tuple<SemanticVersion, IVersionSpec>> versionLookup,
IEnumerable<FrameworkName> targetFrameworkValues,
bool includeAllVersions)
{
var updates = packages.AsEnumerable()
.Where(
p =>
{
// For each package, if the version is higher than the client version and we satisty the target framework, return it.
// TODO: We could optimize for the includeAllVersions case here by short circuiting the operation once we've encountered the highest version
// for a given Id
var version = SemanticVersion.Parse(p.Version);
Tuple<SemanticVersion, IVersionSpec> versionTuple;
if (versionLookup.TryGetValue(p.PackageRegistration.Id, out versionTuple))
{
SemanticVersion clientVersion = versionTuple.Item1;
var supportedPackageFrameworks = p.SupportedFrameworks.Select(f => f.FrameworkName);
IVersionSpec versionConstraint = versionTuple.Item2;
return (version > clientVersion) &&
(targetFrameworkValues == null ||
targetFrameworkValues.Any(s => VersionUtility.IsCompatible(s, supportedPackageFrameworks))) &&
(versionConstraint == null || versionConstraint.Satisfies(version));
}
return false;
});
if (!includeAllVersions)
{
return updates.GroupBy(p => p.PackageRegistration.Id)
.Select(g => g.OrderByDescending(p => SemanticVersion.Parse(p.Version)).First());
}
return updates;
}
public override Uri GetReadStreamUri(
object entity,
DataServiceOperationContext operationContext)
{
var package = (V2FeedPackage)entity;
var urlHelper = new UrlHelper(new RequestContext(HttpContext, new RouteData()));
string url = urlHelper.PackageDownload(FeedVersion, package.Id, package.Version);
return new Uri(url, UriKind.Absolute);
}
private string GetSiteRoot()
{
return Configuration.GetSiteRoot(UseHttps());
}
}
}