Skip to content

Commit 5e24352

Browse files
markekrausiSazonov
authored andcommitted
Initialize Headers Dictionary Only Once (PowerShell#4853)
Switch the WebResponseObject.Headers Dictionary to initialize once instead of creating a new dictionary on every get. Moved logic to WebResponseHelper.GetHeadersDictionary() as this code will be reused outside of WebResponseObject . * [Feature] Create Headers Only Once and Only If Called * {feature] headers -> _headers * [Feature] Move logic to WebResponseObject for reuse This code has potential reuse in InvokeRestMethodCommand. Moving it to WebResponseHelper * Add comment to address PR Feedback.
1 parent b81aeb4 commit 5e24352

File tree

2 files changed

+29
-11
lines changed

2 files changed

+29
-11
lines changed

src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/CoreCLR/WebResponseHelper.CoreClr.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
Copyright (c) Microsoft Corporation. All rights reserved.
55
--********************************************************************/
66

7+
using System;
8+
using System.Collections.Generic;
79
using System.Net.Http;
810
using System.Globalization;
911

@@ -16,6 +18,28 @@ internal static string GetCharacterSet(HttpResponseMessage response)
1618
string characterSet = response.Content.Headers.ContentType.CharSet;
1719
return characterSet;
1820
}
21+
22+
internal static Dictionary<string, IEnumerable<string>> GetHeadersDictionary(HttpResponseMessage response)
23+
{
24+
var headers = new Dictionary<string, IEnumerable<string>>(StringComparer.OrdinalIgnoreCase);
25+
foreach (var entry in response.Headers)
26+
{
27+
headers[entry.Key] = entry.Value;
28+
}
29+
// In CoreFX, HttpResponseMessage separates content related headers, such as Content-Type to
30+
// HttpResponseMessage.Content.Headers. The remaining headers are in HttpResponseMessage.Headers.
31+
// The keys in both should be unique with no duplicates between them.
32+
// Added for backwards compatibility with PowerShell 5.1 and earlier.
33+
if (response.Content != null)
34+
{
35+
foreach (var entry in response.Content.Headers)
36+
{
37+
headers[entry.Key] = entry.Value;
38+
}
39+
}
40+
41+
return headers;
42+
}
1943

2044
internal static string GetProtocol(HttpResponseMessage response)
2145
{

src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/CoreCLR/WebResponseObject.CoreClr.cs

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,23 +31,17 @@ public Dictionary<string, IEnumerable<string>> Headers
3131
{
3232
get
3333
{
34-
var headers = new Dictionary<string, IEnumerable<string>>(StringComparer.OrdinalIgnoreCase);
35-
foreach (var entry in BaseResponse.Headers)
34+
if(_headers == null)
3635
{
37-
headers[entry.Key] = entry.Value;
38-
}
39-
if (BaseResponse.Content != null)
40-
{
41-
foreach (var entry in BaseResponse.Content.Headers)
42-
{
43-
headers[entry.Key] = entry.Value;
44-
}
36+
_headers = WebResponseHelper.GetHeadersDictionary(BaseResponse);
4537
}
4638

47-
return headers;
39+
return _headers;
4840
}
4941
}
5042

43+
private Dictionary<string, IEnumerable<string>> _headers = null;
44+
5145
/// <summary>
5246
/// gets the RelationLink property
5347
/// </summary>

0 commit comments

Comments
 (0)