forked from PowerShell/PowerShell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHttpClientPackageRepository.cs
More file actions
461 lines (379 loc) · 18 KB
/
Copy pathHttpClientPackageRepository.cs
File metadata and controls
461 lines (379 loc) · 18 KB
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
namespace Microsoft.PackageManagement.NuGetProvider {
using System;
using System.Net;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Xml.Linq;
using Resources;
using System.Collections.Concurrent;
using System.Globalization;
using System.Security;
using Microsoft.PackageManagement.Provider.Utility;
/// <summary>
/// Package repository for downloading data from remote galleries
/// </summary>
internal class HttpClientPackageRepository : IPackageRepository
{
private readonly string _nugetFindPackageIdQueryFormat;
private readonly string _queryUri;
/// <summary>
/// Ctor's
/// </summary>
/// <param name="request">The nuget request object</param>
/// <param name="queryUrl">Packagesource location</param>
internal HttpClientPackageRepository(string queryUrl, NuGetRequest request)
{
// Validate the url
Uri newUri;
Uri validatedUri = null;
if (Uri.TryCreate(queryUrl, UriKind.Absolute, out newUri))
{
validatedUri = NuGetPathUtility.ValidateUri(newUri, request);
}
if (validatedUri == null)
{
throw new ArgumentException(String.Format(CultureInfo.CurrentCulture, Messages.InvalidQueryUrl, queryUrl));
}
queryUrl = validatedUri.AbsoluteUri;
// if a query is http://www.nuget.org/api/v2 then we add / to the end
if (!queryUrl.EndsWith("/", StringComparison.OrdinalIgnoreCase))
{
queryUrl = String.Concat(queryUrl, "/");
}
_queryUri = queryUrl;
//we are constructing the url query like http://www.nuget.org/api/v2/FindPackagesById()?id='JQuery'
_nugetFindPackageIdQueryFormat = PathUtility.UriCombine(_queryUri, NuGetConstant.FindPackagesById);
}
/// <summary>
/// Package source location
/// </summary>
public string Source {
get {
// Package source Uri
return _queryUri;
}
}
/// <summary>
/// True if the packagesource is a file repository
/// </summary>
public bool IsFile
{
get
{
//false because this is not a local file repository
return false;
}
}
/// <summary>
/// Find-Package
/// </summary>
/// <param name="packageId">package Id</param>
/// <param name="version">package version</param>
/// <param name="request"></param>
/// <returns></returns>
public IPackage FindPackage(string packageId, SemanticVersion version, NuGetRequest request)
{
if (string.IsNullOrWhiteSpace(packageId)) {
return null;
}
request.Debug(Messages.DebugInfoCallMethod3, "HttpClientPackageRepository", "FindPackage", packageId);
var query = packageId.MakeFindPackageByIdQuery(_nugetFindPackageIdQueryFormat);
var packages = NuGetClient.FindPackage(query, request);
//Usually versions has a limited number, ToArray should be ok.
var versions = version.GetComparableVersionStrings().ToArray();
//Will only enumerate packages once
return packages.FirstOrDefault(package => packageId.Equals(package.Id, StringComparison.OrdinalIgnoreCase) && versions.Contains(package.Version,StringComparer.OrdinalIgnoreCase));
}
/// <summary>
/// Find-Package bases on the given package Id
/// </summary>
/// <param name="packageId">Package Id</param>
/// <param name="request"></param>
/// <returns></returns>
public IEnumerable<IPackage> FindPackagesById(string packageId, NuGetRequest request){
request.Debug(Messages.DebugInfoCallMethod3, "HttpClientPackageRepository", "FindPackagesById", packageId);
var query = packageId.MakeFindPackageByIdQuery(_nugetFindPackageIdQueryFormat);
//request.Verbose(query.ToString());
return NuGetClient.FindPackage(query, request);
}
/// <summary>
/// Search the entire repository for the case when a user does not provider package name or uses wildcards in the name.
/// </summary>
/// <param name="searchTerm"></param>
/// <param name="nugetRequest"></param>
/// <returns></returns>
public IEnumerable<IPackage> Search(string searchTerm, NuGetRequest nugetRequest)
{
if (nugetRequest == null)
{
yield break;
}
nugetRequest.Debug(Messages.DebugInfoCallMethod3, "HttpClientPackageRepository", "Search", searchTerm);
var searchQuery = searchTerm.MakeSearchQuery(_queryUri, nugetRequest.AllowPrereleaseVersions.Value, nugetRequest.AllVersions.Value);
foreach (var pkg in SendRequest(searchQuery, nugetRequest))
{
yield return pkg;
}
}
/// <summary>
/// Send the request to the server with buffer size to account for the case where there are more data
/// that we need to fetch
/// </summary>
/// <param name="query"></param>
/// <param name="request"></param>
/// <returns></returns>
public static IEnumerable<PackageBase> SendRequest(string query, NuGetRequest request)
{
const int bufferSize = 40;
// number of threads sending the requests
const int numberOfSenders = 4;
var startPoint = 0;
var tasks = new List<Task<Stream>>();
bool stopSending = false;
object stopLock = new Object();
// Send one request first
// this initial query is of the form http://www.nuget.org/api/v2/FindPackagesById()?id='jquery'&$skip={0}&$top={1}
UriBuilder initialQuery = new UriBuilder(query.InsertSkipAndTop());
PackageBase firstPackage = null;
// Send out an initial request
// we send out 1 initial request first to check for redirection and check whether repository supports odata
using (Stream stream = NuGetClient.InitialDownloadDataToStream(initialQuery, startPoint, bufferSize, request))
{
if (stream == null)
{
yield break;
}
XDocument document = XmlUtility.LoadSafe(stream, ignoreWhiteSpace: true);
var entries = document.Root.ElementsNoNamespace("entry").ToList();
// If the initial request has different number of entries than the buffer size, return it because this means the server
// does not understand odata request or there is no more data. in the former case, we have to stop to prevent infinite loop
if (entries.Count != bufferSize)
{
request.Debug(Messages.PackagesReceived, entries.Count);
stopSending = true;
}
foreach (XElement entry in entries)
{
var package = new PackageBase();
// set the first package of the request. this is used later to verify that the case when the number of packages in the repository
// is the same as the buffer size and the repository does not support odata query. in that case, we want to check whether the first package
// exists anywhere in the second call. if it is, then we cancel the request (this is to prevent infinite loop)
if (firstPackage == null)
{
firstPackage = package;
}
PackageUtility.ReadEntryElement(ref package, entry);
yield return package;
}
}
if (stopSending || request.IsCanceled)
{
yield break;
}
// To avoid more redirection (for example, if the initial query is nuget.org, it will be changed to www.nuget.org
query = initialQuery.Uri.ToString();
// Sending the initial requests
for (var i = 0; i < numberOfSenders; i++)
{
// Update the start point to fetch the packages
startPoint += bufferSize;
// Get the query
var newQuery = string.Format(query, startPoint, bufferSize);
// Send it
tasks.Add(Task.Factory.StartNew(() =>
{
Stream items = NuGetClient.DownloadDataToStream(newQuery, request);
return items;
}));
}
//Wait for the responses, parse the data, and send to the user
while (tasks.Count > 0)
{
//Cast because the compiler warning: Co-variant array conversion from Task[] to Task[] can cause run-time exception on write operation.
var index = Task.WaitAny(tasks.Cast<Task>().ToArray());
using (Stream stream = tasks[index].Result)
{
if (stream == null)
{
yield break;
}
XDocument document = XmlUtility.LoadSafe(stream, ignoreWhiteSpace: true);
var entries = document.Root.ElementsNoNamespace("entry").ToList();
if (entries.Count < bufferSize)
{
request.Debug(Messages.PackagesReceived, entries.Count);
lock (stopLock)
{
stopSending = true;
}
}
foreach (XElement entry in entries)
{
var package = new PackageBase();
PackageUtility.ReadEntryElement(ref package, entry);
if (firstPackage != null)
{
// check whether first package in the first request exists anywhere in the second request
if (string.Equals(firstPackage.GetFullName(), package.GetFullName(), StringComparison.OrdinalIgnoreCase)
&& string.Equals(firstPackage.Version, package.Version, StringComparison.OrdinalIgnoreCase))
{
lock (stopLock)
{
stopSending = true;
}
break;
}
}
yield return package;
}
// we only needs to check for the existence of the first package in the second request. don't need to do for subsequent request
if (firstPackage != null)
{
firstPackage = null;
}
}
// checks whether we should stop sending requests
if (!stopSending && !request.IsCanceled)
{
// Make sure nobody else is updating the startPoint
lock (stopLock)
{
// update the startPoint
startPoint += bufferSize;
}
// Make a new request with the new startPoint
var newQuery = string.Format(query, startPoint, bufferSize);
//Keep sending a request
tasks[index] = (Task.Factory.StartNew(searchQuery =>
{
var items = NuGetClient.DownloadDataToStream(searchQuery.ToStringSafe(), request);
return items;
}, newQuery));
}
else
{
if (request.IsCanceled)
{
request.Warning(Messages.RequestCanceled, "HttpClientPackageRepository", "SendRequest");
//stop sending request to the remote server
stopSending = true;
}
tasks.RemoveAt(index);
}
}
}
#region SkipTokenCode
// This code is commented out because powershellgallery has a bug with skip token. We can uncomment it once that is fixed.
///// <summary>
///// Create a new task that will automatically create a task and add it to taskCollection
///// if there are more links to be downloaded. Otherwise, it will signal to the taskCollection that
///// we are not expecting any more results.
///// The task will also add any packages that it produced to packageCollection
///// </summary>
///// <param name="query"></param>
///// <param name="taskCollection"></param>
///// <param name="packageCollection"></param>
///// <returns></returns>
//private static Task<Stream> CreateDownloadTask(string query, BlockingCollection<Task<Stream>> taskCollection, BlockingCollection<PackageBase> packageCollection,Request request)
//{
// Task<Stream> taskStream = Task.Factory.StartNew(searchQuery =>
// {
// var items = NuGetClient.DownloadDataToStream(searchQuery.ToStringSafe(), request);
// return items;
// }, query);
// // After the task is done, we check whether we should create a new task
// taskStream.ContinueWith(streamTask =>
// {
// using (Stream stream = streamTask.Result)
// {
// XDocument document = XmlUtility.LoadSafe(stream, ignoreWhiteSpace: true);
// // find the xelement of the form <link rel="next" href="<next link>">
// XElement next = document.Root.Elements().FirstOrDefault(e => String.Equals(e.Name.LocalName, "link", StringComparison.OrdinalIgnoreCase)
// && e.Attribute("rel") != null
// && String.Equals(e.Attribute("rel").Value, "next", StringComparison.OrdinalIgnoreCase)
// && e.Attribute("href") != null);
// // If there is no next link or the request is cancelled, stop sending the request
// if (next == null || request.IsCanceled)
// {
// // call completeadding to signal that there won't be anymore request
// taskCollection.CompleteAdding();
// }
// else
// {
// var newQuery = next.Attribute("href").Value;
// taskCollection.Add(CreateDownloadTask(newQuery, taskCollection, packageCollection, request));
// }
// foreach (XElement entry in document.Root.ElementsNoNamespace("entry"))
// {
// var package = new PackageBase();
// PackageUtility.ReadEntryElement(ref package, entry);
// packageCollection.Add(package);
// }
// if (next == null || request.IsCanceled)
// {
// packageCollection.CompleteAdding();
// }
// }
// });
// return taskStream;
//}
///// <summary>
///// Send the request to the server. We check whether the response has a next link
///// to account for the case where there are more data that we need to fetch
///// </summary>
///// <param name="query"></param>
///// <param name="request"></param>
///// <returns></returns>
//public static IEnumerable<PackageBase> SendRequest(string query, Request request)
//{
// var tasks = new List<Task<Stream>>();
// // A blocking collection of task stream.
// BlockingCollection<Task<Stream>> taskCollection = new BlockingCollection<Task<Stream>>();
// // A blocking collection of packages.
// BlockingCollection<PackageBase> packageCollection = new BlockingCollection<PackageBase>();
// // Populate the first task
// Task<Stream> firstTask = CreateDownloadTask(query, taskCollection, packageCollection, request);
// while (!taskCollection.IsCompleted)
// {
// Task<Stream> streamTask = null;
// try
// {
// streamTask = taskCollection.Take();
// }
// catch (InvalidOperationException) { }
// // Try to yield package from packageCollection
// while (!packageCollection.IsCompleted)
// {
// PackageBase package = null;
// try
// {
// package = packageCollection.Take();
// }
// catch (InvalidOperationException) { }
// if (package != null)
// {
// yield return package;
// }
// }
// }
// // There may be packages in packageCollection
// // Try to yield package from packageCollection
// while (!packageCollection.IsCompleted)
// {
// PackageBase package = null;
// try
// {
// package = packageCollection.Take();
// }
// catch (InvalidOperationException) { }
// if (package != null)
// {
// yield return package;
// }
// }
//}
#endregion
}
}