Skip to content

Commit

Permalink
Add protection against empty response from server (#1525)
Browse files Browse the repository at this point in the history
  • Loading branch information
adityapatwardhan authored and alerickson committed Feb 5, 2024
1 parent f4680a2 commit 2edd555
Show file tree
Hide file tree
Showing 3 changed files with 282 additions and 242 deletions.
104 changes: 62 additions & 42 deletions src/code/NuGetServerAPICalls.cs
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,9 @@ public override FindResults FindTags(string[] tags, bool includePrerelease, Reso
public override FindResults FindCommandOrDscResource(string[] tags, bool includePrerelease, bool isSearchingForCommands, out ErrorRecord errRecord)
{
errRecord = new ErrorRecord(
new InvalidOperationException($"Find by CommandName or DSCResource is not supported for the repository '{Repository.Name}'"),
"FindCommandOrDscResourceFailure",
ErrorCategory.InvalidOperation,
new InvalidOperationException($"Find by CommandName or DSCResource is not supported for the repository '{Repository.Name}'"),
"FindCommandOrDscResourceFailure",
ErrorCategory.InvalidOperation,
this);

return new FindResults(stringResponse: Utils.EmptyStrArray, hashtableResponse: emptyHashResponses, responseType: FindResponseType);
Expand Down Expand Up @@ -431,25 +431,25 @@ private string HttpRequestCall(string requestUrl, out ErrorRecord errRecord)
catch (HttpRequestException e)
{
errRecord = new ErrorRecord(
exception: e,
"HttpRequestFallFailure",
ErrorCategory.ConnectionError,
exception: e,
"HttpRequestFallFailure",
ErrorCategory.ConnectionError,
this);
}
catch (ArgumentNullException e)
{
errRecord = new ErrorRecord(
exception: e,
"HttpRequestFallFailure",
exception: e,
"HttpRequestFallFailure",
ErrorCategory.ConnectionError,
this);
}
catch (InvalidOperationException e)
{
errRecord = new ErrorRecord(
exception: e,
"HttpRequestFallFailure",
ErrorCategory.ConnectionError,
exception: e,
"HttpRequestFallFailure",
ErrorCategory.ConnectionError,
this);
}

Expand Down Expand Up @@ -480,25 +480,25 @@ private HttpContent HttpRequestCallForContent(string requestUrl, out ErrorRecord
catch (HttpRequestException e)
{
errRecord = new ErrorRecord(
exception: e,
"HttpRequestFailure",
ErrorCategory.ConnectionError ,
exception: e,
"HttpRequestFailure",
ErrorCategory.ConnectionError ,
this);
}
catch (ArgumentNullException e)
{
errRecord = new ErrorRecord(
exception: e,
"HttpRequestFailure",
ErrorCategory.InvalidData,
exception: e,
"HttpRequestFailure",
ErrorCategory.InvalidData,
this);
}
catch (InvalidOperationException e)
{
errRecord = new ErrorRecord(
exception: e,
"HttpRequestFailure",
ErrorCategory.InvalidOperation,
exception: e,
"HttpRequestFailure",
ErrorCategory.InvalidOperation,
this);
}

Expand Down Expand Up @@ -565,9 +565,9 @@ private string FindNameGlobbing(string packageName, bool includePrerelease, int
if (names.Length == 0)
{
errRecord = new ErrorRecord(
new ArgumentException("-Name '*' for NuGet.Server hosted feed repository is not supported"),
"FindNameGlobbingFailure",
ErrorCategory.InvalidArgument,
new ArgumentException("-Name '*' for NuGet.Server hosted feed repository is not supported"),
"FindNameGlobbingFailure",
ErrorCategory.InvalidArgument,
this);

return string.Empty;
Expand Down Expand Up @@ -601,9 +601,9 @@ private string FindNameGlobbing(string packageName, bool includePrerelease, int
else
{
errRecord = new ErrorRecord(
new ArgumentException("-Name with wildcards is only supported for scenarios similar to the following examples: PowerShell*, *ShellGet, *Shell*."),
"FindNameGlobbingFailure",
ErrorCategory.InvalidArgument,
new ArgumentException("-Name with wildcards is only supported for scenarios similar to the following examples: PowerShell*, *ShellGet, *Shell*."),
"FindNameGlobbingFailure",
ErrorCategory.InvalidArgument,
this);

return string.Empty;
Expand Down Expand Up @@ -632,9 +632,9 @@ private string FindNameGlobbingWithTag(string packageName, string[] tags, bool i
if (names.Length == 0)
{
errRecord = new ErrorRecord(
new ArgumentException("-Name '*' for NuGet.Server hosted feed repository is not supported"),
"FindNameGlobbingFailure",
ErrorCategory.InvalidArgument,
new ArgumentException("-Name '*' for NuGet.Server hosted feed repository is not supported"),
"FindNameGlobbingFailure",
ErrorCategory.InvalidArgument,
this);

return string.Empty;
Expand Down Expand Up @@ -668,9 +668,9 @@ private string FindNameGlobbingWithTag(string packageName, string[] tags, bool i
else
{
errRecord = new ErrorRecord(
new ArgumentException("-Name with wildcards is only supported for scenarios similar to the following examples: PowerShell*, *ShellGet, *Shell*."),
"FindNameGlobbing",
ErrorCategory.InvalidArgument,
new ArgumentException("-Name with wildcards is only supported for scenarios similar to the following examples: PowerShell*, *ShellGet, *Shell*."),
"FindNameGlobbing",
ErrorCategory.InvalidArgument,
this);

return string.Empty;
Expand Down Expand Up @@ -780,16 +780,26 @@ private string FindVersionGlobbing(string packageName, VersionRange versionRange
/// Name: no wildcard support.
/// Examples: Install "PowerShellGet"
/// Implementation Note: {repoUri}/Packages(Id='test_local_mod')/Download
/// if prerelease, call into InstallVersion instead.
/// if prerelease, call into InstallVersion instead.
/// </summary>
private Stream InstallName(string packageName, out ErrorRecord errRecord)
{
_cmdletPassedIn.WriteDebug("In NuGetServerAPICalls::InstallName()");
var requestUrl = $"{Repository.Uri}/Packages/(Id='{packageName}')/Download";
var response = HttpRequestCallForContent(requestUrl, out errRecord);
var responseStream = response.ReadAsStreamAsync().Result;

return responseStream;
if (response is null)
{
errRecord = new ErrorRecord(
new Exception($"No content was returned by repository '{Repository.Name}'"),
"InstallFailureContentNullNuGetServer",
ErrorCategory.InvalidResult,
this);

return null;
}

return response.ReadAsStreamAsync().Result;
}

/// <summary>
Expand All @@ -799,15 +809,25 @@ private Stream InstallName(string packageName, out ErrorRecord errRecord)
/// Examples: Install "PowerShellGet" -Version "3.0.0.0"
/// Install "PowerShellGet" -Version "3.0.0-beta16"
/// API Call: {repoUri}/Packages(Id='Castle.Core',Version='5.1.1')/Download
/// </summary>
/// </summary>
private Stream InstallVersion(string packageName, string version, out ErrorRecord errRecord)
{
_cmdletPassedIn.WriteDebug("In NuGetServerAPICalls::InstallVersion()");
var requestUrl = $"{Repository.Uri}/Packages(Id='{packageName}',Version='{version}')/Download";
var response = HttpRequestCallForContent(requestUrl, out errRecord);
var responseStream = response.ReadAsStreamAsync().Result;

return responseStream;
if (response is null)
{
errRecord = new ErrorRecord(
new Exception($"No content was returned by repository '{Repository.Name}'"),
"InstallFailureContentNullNuGetServer",
ErrorCategory.InvalidResult,
this);

return null;
}

return response.ReadAsStreamAsync().Result;
}

/// <summary>
Expand All @@ -829,9 +849,9 @@ public int GetCountFromResponse(string httpResponse, out ErrorRecord errRecord)
catch (XmlException e)
{
errRecord = new ErrorRecord(
exception: e,
"GetCountFromResponse",
ErrorCategory.InvalidData,
exception: e,
"GetCountFromResponse",
ErrorCategory.InvalidData,
this);
}
if (errRecord != null)
Expand Down Expand Up @@ -886,7 +906,7 @@ public static async Task<HttpContent> SendRequestForContentAsync(HttpRequestMess
{
HttpResponseMessage response = await s_client.SendAsync(message);
response.EnsureSuccessStatusCode();

return response.Content;
}
catch (HttpRequestException e)
Expand Down
Loading

0 comments on commit 2edd555

Please sign in to comment.