Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add protection against empty response from server #1525

Merged
merged 2 commits into from
Jan 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -437,25 +437,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 @@ -486,25 +486,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 @@ -571,9 +571,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 @@ -607,9 +607,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 @@ -638,9 +638,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 @@ -674,9 +674,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 @@ -786,16 +786,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 @@ -805,15 +815,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 @@ -835,9 +855,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 @@ -892,7 +912,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
Loading