Skip to content

Commit 2edd555

Browse files
adityapatwardhanalerickson
authored andcommitted
Add protection against empty response from server (#1525)
1 parent f4680a2 commit 2edd555

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);
@@ -431,25 +431,25 @@ private string HttpRequestCall(string requestUrl, out ErrorRecord errRecord)
431431
catch (HttpRequestException e)
432432
{
433433
errRecord = new ErrorRecord(
434-
exception: e,
435-
"HttpRequestFallFailure",
436-
ErrorCategory.ConnectionError,
434+
exception: e,
435+
"HttpRequestFallFailure",
436+
ErrorCategory.ConnectionError,
437437
this);
438438
}
439439
catch (ArgumentNullException e)
440440
{
441441
errRecord = new ErrorRecord(
442-
exception: e,
443-
"HttpRequestFallFailure",
442+
exception: e,
443+
"HttpRequestFallFailure",
444444
ErrorCategory.ConnectionError,
445445
this);
446446
}
447447
catch (InvalidOperationException e)
448448
{
449449
errRecord = new ErrorRecord(
450-
exception: e,
451-
"HttpRequestFallFailure",
452-
ErrorCategory.ConnectionError,
450+
exception: e,
451+
"HttpRequestFallFailure",
452+
ErrorCategory.ConnectionError,
453453
this);
454454
}
455455

@@ -480,25 +480,25 @@ private HttpContent HttpRequestCallForContent(string requestUrl, out ErrorRecord
480480
catch (HttpRequestException e)
481481
{
482482
errRecord = new ErrorRecord(
483-
exception: e,
484-
"HttpRequestFailure",
485-
ErrorCategory.ConnectionError ,
483+
exception: e,
484+
"HttpRequestFailure",
485+
ErrorCategory.ConnectionError ,
486486
this);
487487
}
488488
catch (ArgumentNullException e)
489489
{
490490
errRecord = new ErrorRecord(
491-
exception: e,
492-
"HttpRequestFailure",
493-
ErrorCategory.InvalidData,
491+
exception: e,
492+
"HttpRequestFailure",
493+
ErrorCategory.InvalidData,
494494
this);
495495
}
496496
catch (InvalidOperationException e)
497497
{
498498
errRecord = new ErrorRecord(
499-
exception: e,
500-
"HttpRequestFailure",
501-
ErrorCategory.InvalidOperation,
499+
exception: e,
500+
"HttpRequestFailure",
501+
ErrorCategory.InvalidOperation,
502502
this);
503503
}
504504

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

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

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

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

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

792-
return responseStream;
791+
if (response is null)
792+
{
793+
errRecord = new ErrorRecord(
794+
new Exception($"No content was returned by repository '{Repository.Name}'"),
795+
"InstallFailureContentNullNuGetServer",
796+
ErrorCategory.InvalidResult,
797+
this);
798+
799+
return null;
800+
}
801+
802+
return response.ReadAsStreamAsync().Result;
793803
}
794804

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

810-
return responseStream;
819+
if (response is null)
820+
{
821+
errRecord = new ErrorRecord(
822+
new Exception($"No content was returned by repository '{Repository.Name}'"),
823+
"InstallFailureContentNullNuGetServer",
824+
ErrorCategory.InvalidResult,
825+
this);
826+
827+
return null;
828+
}
829+
830+
return response.ReadAsStreamAsync().Result;
811831
}
812832

813833
/// <summary>
@@ -829,9 +849,9 @@ public int GetCountFromResponse(string httpResponse, out ErrorRecord errRecord)
829849
catch (XmlException e)
830850
{
831851
errRecord = new ErrorRecord(
832-
exception: e,
833-
"GetCountFromResponse",
834-
ErrorCategory.InvalidData,
852+
exception: e,
853+
"GetCountFromResponse",
854+
ErrorCategory.InvalidData,
835855
this);
836856
}
837857
if (errRecord != null)
@@ -886,7 +906,7 @@ public static async Task<HttpContent> SendRequestForContentAsync(HttpRequestMess
886906
{
887907
HttpResponseMessage response = await s_client.SendAsync(message);
888908
response.EnsureSuccessStatusCode();
889-
909+
890910
return response.Content;
891911
}
892912
catch (HttpRequestException e)

0 commit comments

Comments
 (0)