20
20
using System . Text ;
21
21
using System . Security . Cryptography ;
22
22
using System . Text . Json ;
23
- using System . Runtime . InteropServices . WindowsRuntime ;
24
23
25
24
namespace Microsoft . PowerShell . PSResourceGet
26
25
{
@@ -48,8 +47,6 @@ internal class ContainerRegistryServerAPICalls : ServerApiCall
48
47
const string containerRegistryStartUploadTemplate = "https://{0}/v2/{1}/blobs/uploads/" ; // 0 - registry, 1 - packagename
49
48
const string containerRegistryEndUploadTemplate = "https://{0}{1}&digest=sha256:{2}" ; // 0 - registry, 1 - location, 2 - digest
50
49
51
- private static readonly HttpClient s_client = new HttpClient ( ) ;
52
-
53
50
#endregion
54
51
55
52
#region Constructor
@@ -65,6 +62,7 @@ public ContainerRegistryServerAPICalls(PSRepositoryInfo repository, PSCmdlet cmd
65
62
} ;
66
63
67
64
_sessionClient = new HttpClient ( handler ) ;
65
+ _sessionClient . Timeout = TimeSpan . FromMinutes ( 10 ) ;
68
66
_sessionClient . DefaultRequestHeaders . TryAddWithoutValidation ( "User-Agent" , userAgentString ) ;
69
67
}
70
68
@@ -444,7 +442,7 @@ internal bool IsContainerRegistryUnauthenticated(string containerRegistyUrl, out
444
442
HttpResponseMessage response ;
445
443
try
446
444
{
447
- response = s_client . SendAsync ( new HttpRequestMessage ( HttpMethod . Head , endpoint ) ) . Result ;
445
+ response = _sessionClient . SendAsync ( new HttpRequestMessage ( HttpMethod . Head , endpoint ) ) . Result ;
448
446
}
449
447
catch ( Exception e )
450
448
{
@@ -965,7 +963,7 @@ internal JObject GetHttpResponseJObjectUsingContentHeaders(string url, HttpMetho
965
963
/// <summary>
966
964
/// Get response headers.
967
965
/// </summary>
968
- internal static async Task < HttpResponseHeaders > GetHttpResponseHeader ( string url , HttpMethod method , Collection < KeyValuePair < string , string > > defaultHeaders )
966
+ internal async Task < HttpResponseHeaders > GetHttpResponseHeader ( string url , HttpMethod method , Collection < KeyValuePair < string , string > > defaultHeaders )
969
967
{
970
968
try
971
969
{
@@ -982,24 +980,24 @@ internal static async Task<HttpResponseHeaders> GetHttpResponseHeader(string url
982
980
/// <summary>
983
981
/// Set default headers for HttpClient.
984
982
/// </summary>
985
- private static void SetDefaultHeaders ( Collection < KeyValuePair < string , string > > defaultHeaders )
983
+ private void SetDefaultHeaders ( Collection < KeyValuePair < string , string > > defaultHeaders )
986
984
{
987
- s_client . DefaultRequestHeaders . Clear ( ) ;
985
+ _sessionClient . DefaultRequestHeaders . Clear ( ) ;
988
986
if ( defaultHeaders != null )
989
987
{
990
988
foreach ( var header in defaultHeaders )
991
989
{
992
990
if ( string . Equals ( header . Key , "Authorization" , StringComparison . OrdinalIgnoreCase ) )
993
991
{
994
- s_client . DefaultRequestHeaders . Authorization = new AuthenticationHeaderValue ( "Bearer" , header . Value ) ;
992
+ _sessionClient . DefaultRequestHeaders . Authorization = new AuthenticationHeaderValue ( "Bearer" , header . Value ) ;
995
993
}
996
994
else if ( string . Equals ( header . Key , "Accept" , StringComparison . OrdinalIgnoreCase ) )
997
995
{
998
- s_client . DefaultRequestHeaders . Accept . Add ( new MediaTypeWithQualityHeaderValue ( header . Value ) ) ;
996
+ _sessionClient . DefaultRequestHeaders . Accept . Add ( new MediaTypeWithQualityHeaderValue ( header . Value ) ) ;
999
997
}
1000
998
else
1001
999
{
1002
- s_client . DefaultRequestHeaders . Add ( header . Key , header . Value ) ;
1000
+ _sessionClient . DefaultRequestHeaders . Add ( header . Key , header . Value ) ;
1003
1001
}
1004
1002
}
1005
1003
}
@@ -1008,11 +1006,11 @@ private static void SetDefaultHeaders(Collection<KeyValuePair<string, string>> d
1008
1006
/// <summary>
1009
1007
/// Sends request for content.
1010
1008
/// </summary>
1011
- private static async Task < HttpContent > SendContentRequestAsync ( HttpRequestMessage message )
1009
+ private async Task < HttpContent > SendContentRequestAsync ( HttpRequestMessage message )
1012
1010
{
1013
1011
try
1014
1012
{
1015
- HttpResponseMessage response = await s_client . SendAsync ( message ) ;
1013
+ HttpResponseMessage response = await _sessionClient . SendAsync ( message ) ;
1016
1014
response . EnsureSuccessStatusCode ( ) ;
1017
1015
return response . Content ;
1018
1016
}
@@ -1025,12 +1023,12 @@ private static async Task<HttpContent> SendContentRequestAsync(HttpRequestMessag
1025
1023
/// <summary>
1026
1024
/// Sends HTTP request.
1027
1025
/// </summary>
1028
- private static async Task < JObject > SendRequestAsync ( HttpRequestMessage message )
1026
+ private async Task < JObject > SendRequestAsync ( HttpRequestMessage message )
1029
1027
{
1030
1028
HttpResponseMessage response ;
1031
1029
try
1032
1030
{
1033
- response = await s_client . SendAsync ( message ) ;
1031
+ response = await _sessionClient . SendAsync ( message ) ;
1034
1032
}
1035
1033
catch ( Exception e )
1036
1034
{
@@ -1058,11 +1056,11 @@ private static async Task<JObject> SendRequestAsync(HttpRequestMessage message)
1058
1056
/// <summary>
1059
1057
/// Send request to get response headers.
1060
1058
/// </summary>
1061
- private static async Task < HttpResponseHeaders > SendRequestHeaderAsync ( HttpRequestMessage message )
1059
+ private async Task < HttpResponseHeaders > SendRequestHeaderAsync ( HttpRequestMessage message )
1062
1060
{
1063
1061
try
1064
1062
{
1065
- HttpResponseMessage response = await s_client . SendAsync ( message ) ;
1063
+ HttpResponseMessage response = await _sessionClient . SendAsync ( message ) ;
1066
1064
response . EnsureSuccessStatusCode ( ) ;
1067
1065
return response . Headers ;
1068
1066
}
@@ -1075,7 +1073,7 @@ private static async Task<HttpResponseHeaders> SendRequestHeaderAsync(HttpReques
1075
1073
/// <summary>
1076
1074
/// Sends a PUT request, used for publishing to container registry.
1077
1075
/// </summary>
1078
- private static async Task < HttpResponseMessage > PutRequestAsync ( string url , string filePath , bool isManifest , Collection < KeyValuePair < string , string > > contentHeaders )
1076
+ private async Task < HttpResponseMessage > PutRequestAsync ( string url , string filePath , bool isManifest , Collection < KeyValuePair < string , string > > contentHeaders )
1079
1077
{
1080
1078
try
1081
1079
{
@@ -1094,7 +1092,7 @@ private static async Task<HttpResponseMessage> PutRequestAsync(string url, strin
1094
1092
httpContent . Headers . Add ( "Content-Type" , "application/octet-stream" ) ;
1095
1093
}
1096
1094
1097
- return await s_client . PutAsync ( url , httpContent ) ;
1095
+ return await _sessionClient . PutAsync ( url , httpContent ) ;
1098
1096
}
1099
1097
}
1100
1098
catch ( Exception e )
0 commit comments