Skip to content

Commit d0bf74e

Browse files
Add protection against empty response from server (#1525)
1 parent 56428a4 commit d0bf74e

File tree

3 files changed

+282
-242
lines changed

3 files changed

+282
-242
lines changed

src/code/NuGetServerAPICalls.cs

Lines changed: 62 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -143,9 +143,9 @@ public override FindResults FindTags(string[] tags, bool includePrerelease, Reso
143143
public override FindResults FindCommandOrDscResource(string[] tags, bool includePrerelease, bool isSearchingForCommands, out ErrorRecord errRecord)
144144
{
145145
errRecord = new ErrorRecord(
146-
new InvalidOperationException($"Find by CommandName or DSCResource is not supported for the repository '{Repository.Name}'"),
147-
"FindCommandOrDscResourceFailure",
148-
ErrorCategory.InvalidOperation,
146+
new InvalidOperationException($"Find by CommandName or DSCResource is not supported for the repository '{Repository.Name}'"),
147+
"FindCommandOrDscResourceFailure",
148+
ErrorCategory.InvalidOperation,
149149
this);
150150

151151
return new FindResults(stringResponse: Utils.EmptyStrArray, hashtableResponse: emptyHashResponses, responseType: FindResponseType);
@@ -437,25 +437,25 @@ private string HttpRequestCall(string requestUrl, out ErrorRecord errRecord)
437437
catch (HttpRequestException e)
438438
{
439439
errRecord = new ErrorRecord(
440-
exception: e,
441-
"HttpRequestFallFailure",
442-
ErrorCategory.ConnectionError,
440+
exception: e,
441+
"HttpRequestFallFailure",
442+
ErrorCategory.ConnectionError,
443443
this);
444444
}
445445
catch (ArgumentNullException e)
446446
{
447447
errRecord = new ErrorRecord(
448-
exception: e,
449-
"HttpRequestFallFailure",
448+
exception: e,
449+
"HttpRequestFallFailure",
450450
ErrorCategory.ConnectionError,
451451
this);
452452
}
453453
catch (InvalidOperationException e)
454454
{
455455
errRecord = new ErrorRecord(
456-
exception: e,
457-
"HttpRequestFallFailure",
458-
ErrorCategory.ConnectionError,
456+
exception: e,
457+
"HttpRequestFallFailure",
458+
ErrorCategory.ConnectionError,
459459
this);
460460
}
461461

@@ -486,25 +486,25 @@ private HttpContent HttpRequestCallForContent(string requestUrl, out ErrorRecord
486486
catch (HttpRequestException e)
487487
{
488488
errRecord = new ErrorRecord(
489-
exception: e,
490-
"HttpRequestFailure",
491-
ErrorCategory.ConnectionError ,
489+
exception: e,
490+
"HttpRequestFailure",
491+
ErrorCategory.ConnectionError ,
492492
this);
493493
}
494494
catch (ArgumentNullException e)
495495
{
496496
errRecord = new ErrorRecord(
497-
exception: e,
498-
"HttpRequestFailure",
499-
ErrorCategory.InvalidData,
497+
exception: e,
498+
"HttpRequestFailure",
499+
ErrorCategory.InvalidData,
500500
this);
501501
}
502502
catch (InvalidOperationException e)
503503
{
504504
errRecord = new ErrorRecord(
505-
exception: e,
506-
"HttpRequestFailure",
507-
ErrorCategory.InvalidOperation,
505+
exception: e,
506+
"HttpRequestFailure",
507+
ErrorCategory.InvalidOperation,
508508
this);
509509
}
510510

@@ -571,9 +571,9 @@ private string FindNameGlobbing(string packageName, bool includePrerelease, int
571571
if (names.Length == 0)
572572
{
573573
errRecord = new ErrorRecord(
574-
new ArgumentException("-Name '*' for NuGet.Server hosted feed repository is not supported"),
575-
"FindNameGlobbingFailure",
576-
ErrorCategory.InvalidArgument,
574+
new ArgumentException("-Name '*' for NuGet.Server hosted feed repository is not supported"),
575+
"FindNameGlobbingFailure",
576+
ErrorCategory.InvalidArgument,
577577
this);
578578

579579
return string.Empty;
@@ -607,9 +607,9 @@ private string FindNameGlobbing(string packageName, bool includePrerelease, int
607607
else
608608
{
609609
errRecord = new ErrorRecord(
610-
new ArgumentException("-Name with wildcards is only supported for scenarios similar to the following examples: PowerShell*, *ShellGet, *Shell*."),
611-
"FindNameGlobbingFailure",
612-
ErrorCategory.InvalidArgument,
610+
new ArgumentException("-Name with wildcards is only supported for scenarios similar to the following examples: PowerShell*, *ShellGet, *Shell*."),
611+
"FindNameGlobbingFailure",
612+
ErrorCategory.InvalidArgument,
613613
this);
614614

615615
return string.Empty;
@@ -638,9 +638,9 @@ private string FindNameGlobbingWithTag(string packageName, string[] tags, bool i
638638
if (names.Length == 0)
639639
{
640640
errRecord = new ErrorRecord(
641-
new ArgumentException("-Name '*' for NuGet.Server hosted feed repository is not supported"),
642-
"FindNameGlobbingFailure",
643-
ErrorCategory.InvalidArgument,
641+
new ArgumentException("-Name '*' for NuGet.Server hosted feed repository is not supported"),
642+
"FindNameGlobbingFailure",
643+
ErrorCategory.InvalidArgument,
644644
this);
645645

646646
return string.Empty;
@@ -674,9 +674,9 @@ private string FindNameGlobbingWithTag(string packageName, string[] tags, bool i
674674
else
675675
{
676676
errRecord = new ErrorRecord(
677-
new ArgumentException("-Name with wildcards is only supported for scenarios similar to the following examples: PowerShell*, *ShellGet, *Shell*."),
678-
"FindNameGlobbing",
679-
ErrorCategory.InvalidArgument,
677+
new ArgumentException("-Name with wildcards is only supported for scenarios similar to the following examples: PowerShell*, *ShellGet, *Shell*."),
678+
"FindNameGlobbing",
679+
ErrorCategory.InvalidArgument,
680680
this);
681681

682682
return string.Empty;
@@ -786,16 +786,26 @@ private string FindVersionGlobbing(string packageName, VersionRange versionRange
786786
/// Name: no wildcard support.
787787
/// Examples: Install "PowerShellGet"
788788
/// Implementation Note: {repoUri}/Packages(Id='test_local_mod')/Download
789-
/// if prerelease, call into InstallVersion instead.
789+
/// if prerelease, call into InstallVersion instead.
790790
/// </summary>
791791
private Stream InstallName(string packageName, out ErrorRecord errRecord)
792792
{
793793
_cmdletPassedIn.WriteDebug("In NuGetServerAPICalls::InstallName()");
794794
var requestUrl = $"{Repository.Uri}/Packages/(Id='{packageName}')/Download";
795795
var response = HttpRequestCallForContent(requestUrl, out errRecord);
796-
var responseStream = response.ReadAsStreamAsync().Result;
797796

798-
return responseStream;
797+
if (response is null)
798+
{
799+
errRecord = new ErrorRecord(
800+
new Exception($"No content was returned by repository '{Repository.Name}'"),
801+
"InstallFailureContentNullNuGetServer",
802+
ErrorCategory.InvalidResult,
803+
this);
804+
805+
return null;
806+
}
807+
808+
return response.ReadAsStreamAsync().Result;
799809
}
800810

801811
/// <summary>
@@ -805,15 +815,25 @@ private Stream InstallName(string packageName, out ErrorRecord errRecord)
805815
/// Examples: Install "PowerShellGet" -Version "3.0.0.0"
806816
/// Install "PowerShellGet" -Version "3.0.0-beta16"
807817
/// API Call: {repoUri}/Packages(Id='Castle.Core',Version='5.1.1')/Download
808-
/// </summary>
818+
/// </summary>
809819
private Stream InstallVersion(string packageName, string version, out ErrorRecord errRecord)
810820
{
811821
_cmdletPassedIn.WriteDebug("In NuGetServerAPICalls::InstallVersion()");
812822
var requestUrl = $"{Repository.Uri}/Packages(Id='{packageName}',Version='{version}')/Download";
813823
var response = HttpRequestCallForContent(requestUrl, out errRecord);
814-
var responseStream = response.ReadAsStreamAsync().Result;
815824

816-
return responseStream;
825+
if (response is null)
826+
{
827+
errRecord = new ErrorRecord(
828+
new Exception($"No content was returned by repository '{Repository.Name}'"),
829+
"InstallFailureContentNullNuGetServer",
830+
ErrorCategory.InvalidResult,
831+
this);
832+
833+
return null;
834+
}
835+
836+
return response.ReadAsStreamAsync().Result;
817837
}
818838

819839
/// <summary>
@@ -835,9 +855,9 @@ public int GetCountFromResponse(string httpResponse, out ErrorRecord errRecord)
835855
catch (XmlException e)
836856
{
837857
errRecord = new ErrorRecord(
838-
exception: e,
839-
"GetCountFromResponse",
840-
ErrorCategory.InvalidData,
858+
exception: e,
859+
"GetCountFromResponse",
860+
ErrorCategory.InvalidData,
841861
this);
842862
}
843863
if (errRecord != null)
@@ -892,7 +912,7 @@ public static async Task<HttpContent> SendRequestForContentAsync(HttpRequestMess
892912
{
893913
HttpResponseMessage response = await s_client.SendAsync(message);
894914
response.EnsureSuccessStatusCode();
895-
915+
896916
return response.Content;
897917
}
898918
catch (HttpRequestException e)

0 commit comments

Comments
 (0)