public abstract Warning ( string messageText ) : bool | ||
messageText | string | |
return | bool |
/// <summary> /// A delegate used for finding the package. /// </summary> /// <param name="path">File repository path</param> /// <param name="request"></param> /// <returns></returns> private static IPackage OpenPackage(string path, Request request) { request.Debug(Resources.Messages.DebugInfoCallMethod3, "LocalPackageRepository", "OpenPackage", path); if (!File.Exists(path)) { request.Warning(Resources.Messages.FileNotFound, path, "LocalPackageRepository::OpenPackage"); return(null); } //deal with .nupkg if (string.Equals(Path.GetExtension(path), NuGetConstant.PackageExtension, StringComparison.OrdinalIgnoreCase)) { PackageBase package; try { package = ProcessZipPackage(path); } catch (Exception ex) { request.Verbose(ex.Message); throw; } // Set the last modified date on the package package.Published = FileUtility.GetLastModified(path); // We assume local files in the local repository are all latest version package.IsAbsoluteLatestVersion = true; package.IsLatestVersion = true; package.FullFilePath = path; return(package); } return(null); }
/// <summary> /// Returns dynamic option definitions to the HOST /// /// example response: /// request.YieldDynamicOption( "MySwitch", OptionType.String.ToString(), false); /// /// </summary> /// <param name="category">The category of dynamic options that the HOST is interested in</param> /// <param name="request">An object passed in from the PackageManagement that contains functions that can be used to interact with its Provider</param> public void GetDynamicOptions(string category, Request request) { if (request == null) { throw new ArgumentNullException("request"); } if (category == null) { request.Warning(Resources.Messages.UnkownCategory, PackageProviderName, "GetDynamicOptions", category); return; } switch ((category ?? string.Empty).ToLowerInvariant()) { case "install": // put any options required for install/uninstall/getinstalledpackages request.YieldDynamicOption("Destination", Constants.OptionType.Folder, false); request.YieldDynamicOption("ExcludeVersion", Constants.OptionType.Switch, false); request.YieldDynamicOption("Scope", Constants.OptionType.String, false, new[] { Constants.CurrentUser, Constants.AllUsers }); //request.YieldDynamicOption("SkipDependencies", Constants.OptionType.Switch, false); //request.YieldDynamicOption("ContinueOnFailure", Constants.OptionType.Switch, false); break; case "source": // put any options for package sources request.YieldDynamicOption("ConfigFile", Constants.OptionType.String, false); request.YieldDynamicOption("SkipValidate", Constants.OptionType.Switch, false); break; case "package": // put any options used when searching for packages request.YieldDynamicOption("Headers", Constants.OptionType.StringArray, false); request.YieldDynamicOption("FilterOnTag", Constants.OptionType.StringArray, false); request.YieldDynamicOption("Contains", Constants.OptionType.String, false); request.YieldDynamicOption("AllowPrereleaseVersions", Constants.OptionType.Switch, false); break; default: request.Warning(Resources.Messages.UnkownCategory, PackageProviderName, "GetDynamicOptions", category); break; } }
/// <summary> /// Returns dynamic option definitions to the HOST /// /// example response: /// request.YieldDynamicOption( "MySwitch", OptionType.String.ToString(), false); /// /// </summary> /// <param name="category">The category of dynamic options that the HOST is interested in</param> /// <param name="request">An object passed in from the PackageManagement that contains functions that can be used to interact with its Provider</param> public void GetDynamicOptions(string category, Request request) { if (request == null){ throw new ArgumentNullException("request"); } if (category == null) { request.Warning(Resources.Messages.UnkownCategory, PackageProviderName, "GetDynamicOptions", category); return; } switch ((category ?? string.Empty).ToLowerInvariant()) { case "install": // put any options required for install/uninstall/getinstalledpackages request.YieldDynamicOption("Destination", Constants.OptionType.Folder, false); request.YieldDynamicOption("ExcludeVersion", Constants.OptionType.Switch, false); request.YieldDynamicOption("Scope", Constants.OptionType.String, false, new[] { Constants.CurrentUser, Constants.AllUsers }); //request.YieldDynamicOption("SkipDependencies", Constants.OptionType.Switch, false); //request.YieldDynamicOption("ContinueOnFailure", Constants.OptionType.Switch, false); break; case "source": // put any options for package sources request.YieldDynamicOption("ConfigFile", Constants.OptionType.String, false); request.YieldDynamicOption("SkipValidate", Constants.OptionType.Switch, false); break; case "package": // put any options used when searching for packages request.YieldDynamicOption("Headers", Constants.OptionType.StringArray, false); request.YieldDynamicOption("FilterOnTag", Constants.OptionType.StringArray, false); request.YieldDynamicOption("Contains", Constants.OptionType.String, false); request.YieldDynamicOption("AllowPrereleaseVersions", Constants.OptionType.Switch, false); break; default: request.Warning(Resources.Messages.UnkownCategory, PackageProviderName, "GetDynamicOptions", category); break; } }
/// <summary> /// A delegate used for finding the package. /// </summary> /// <param name="path">File repository path</param> /// <param name="request"></param> /// <returns></returns> private static IPackage OpenPackage(string path, Request request) { request.Debug(Resources.Messages.DebugInfoCallMethod3, "LocalPackageRepository", "OpenPackage", path); if (!File.Exists(path)) { request.Warning(Resources.Messages.FileNotFound, path,"LocalPackageRepository::OpenPackage"); return null; } //deal with .nupkg if (string.Equals(Path.GetExtension(path), NuGetConstant.PackageExtension, StringComparison.OrdinalIgnoreCase)) { PackageBase package; try { package = ProcessZipPackage(path); } catch (Exception ex) { request.Verbose(ex.Message); throw; } // Set the last modified date on the package package.Published = FileUtility.GetLastModified(path); // We assume local files in the local repository are all latest version package.IsAbsoluteLatestVersion = true; package.IsLatestVersion = true; package.FullFilePath = path; return package; } return null; }
/// <summary> /// Send an initial request to download data from the server. /// From the initial request, we may change the host of subsequent calls (if a redirection happens in this initial request) /// Also, if the initial request sends us less data than the amount we request, then we do not /// need to issue more requests /// </summary> /// <param name="query"></param> /// <param name="startPoint"></param> /// <param name="bufferSize"></param> /// <param name="request"></param> /// <returns></returns> internal static Stream InitialDownloadDataToStream(UriBuilder query, int startPoint, int bufferSize, Request request) { var uri = String.Format(CultureInfo.CurrentCulture, query.Uri.ToString(), startPoint, bufferSize); request.Debug(Messages.DownloadingPackage, uri); var client = GetHttpClient(request); var response = PathUtility.GetHttpResponse(client, uri, request); // Check that response was successful or write error if (response == null || !response.IsSuccessStatusCode) { request.Warning(Resources.Messages.CouldNotGetResponseFromQuery, uri); return null; } // Read response and write out a stream var stream = GetStreamBasedOnEncoding(response); request.Debug(Messages.CompletedDownload, uri); // If the host from the response is different, change the host of the original query if (!String.Equals(response.RequestMessage.RequestUri.Host, query.Host, StringComparison.OrdinalIgnoreCase)) { query.Host = response.RequestMessage.RequestUri.Host; } return stream; }
/// <summary> /// Download data from remote via uri query. /// </summary> /// <param name="query">Uri query</param> /// <param name="request">An object passed in from the PackageManagement platform that contains APIs that can be used to interact with it </param> /// <returns></returns> internal static Stream DownloadDataToStream(string query, Request request) { request.Debug(Messages.DownloadingPackage, query); var client = GetHttpClient(request); var response = PathUtility.GetHttpResponse(client, query, request); // Check that response was successful or throw exception if (response == null || !response.IsSuccessStatusCode) { request.Warning(Resources.Messages.CouldNotGetResponseFromQuery, query); return null; } // Read response and write out a stream var stream = GetStreamBasedOnEncoding(response); request.Debug(Messages.CompletedDownload, query); return stream; }
/// <summary> /// Returns a httpclient with the headers set. /// </summary> /// <returns></returns> private static HttpClient GetHttpClient(Request request) { HttpClient client = new HttpClient(new HttpClientHandler() { UseDefaultCredentials = true }); client.DefaultRequestHeaders.TryAddWithoutValidation("Accept-Charset", "UTF-8"); // Request for gzip and deflate encoding to make the response lighter. client.DefaultRequestHeaders.TryAddWithoutValidation("Accept-Encoding", "gzip,deflate"); client.DefaultRequestHeaders.TryAddWithoutValidation("User-Agent", "NuGet"); if (request is NuGetRequest) { var nuGetRequest = request as NuGetRequest; if (nuGetRequest != null && nuGetRequest.Headers != null) { foreach (var header in nuGetRequest.Headers.Value) { // header is in the format "A=B" because OneGet doesn't support Dictionary parameters if (!String.IsNullOrEmpty(header)) { var headerSplit = header.Split(new string[] { "=" }, 2, StringSplitOptions.RemoveEmptyEntries); // ignore wrong entries if (headerSplit.Count() == 2) { client.DefaultRequestHeaders.TryAddWithoutValidation(headerSplit[0], headerSplit[1]); } else { request.Warning(Messages.HeaderIgnored, header); } } } } } return client; }
/// <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, Request 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()); // Send out an initial request 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 less entries than the buffer size, return it because there won't be any more data if (entries.Count < bufferSize) { request.Debug(Messages.PackagesReceived, entries.Count); stopSending = true; } foreach (XElement entry in entries) { var package = new PackageBase(); 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); yield return package; } } // 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); } } }