From 5e24352177916e52ed2454d2da45493e3fcc984d Mon Sep 17 00:00:00 2001 From: Mark Kraus Date: Thu, 21 Sep 2017 03:41:57 -0500 Subject: [PATCH] Initialize Headers Dictionary Only Once (#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. --- .../CoreCLR/WebResponseHelper.CoreClr.cs | 24 +++++++++++++++++++ .../CoreCLR/WebResponseObject.CoreClr.cs | 16 ++++--------- 2 files changed, 29 insertions(+), 11 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/CoreCLR/WebResponseHelper.CoreClr.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/CoreCLR/WebResponseHelper.CoreClr.cs index ec5708763..fbcbdd801 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/CoreCLR/WebResponseHelper.CoreClr.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/CoreCLR/WebResponseHelper.CoreClr.cs @@ -4,6 +4,8 @@ Copyright (c) Microsoft Corporation. All rights reserved. --********************************************************************/ +using System; +using System.Collections.Generic; using System.Net.Http; using System.Globalization; @@ -16,6 +18,28 @@ namespace Microsoft.PowerShell.Commands string characterSet = response.Content.Headers.ContentType.CharSet; return characterSet; } + + internal static Dictionary> GetHeadersDictionary(HttpResponseMessage response) + { + var headers = new Dictionary>(StringComparer.OrdinalIgnoreCase); + foreach (var entry in response.Headers) + { + headers[entry.Key] = entry.Value; + } + // In CoreFX, HttpResponseMessage separates content related headers, such as Content-Type to + // HttpResponseMessage.Content.Headers. The remaining headers are in HttpResponseMessage.Headers. + // The keys in both should be unique with no duplicates between them. + // Added for backwards compatibility with PowerShell 5.1 and earlier. + if (response.Content != null) + { + foreach (var entry in response.Content.Headers) + { + headers[entry.Key] = entry.Value; + } + } + + return headers; + } internal static string GetProtocol(HttpResponseMessage response) { diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/CoreCLR/WebResponseObject.CoreClr.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/CoreCLR/WebResponseObject.CoreClr.cs index 9e4b52bfd..d683a833b 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/CoreCLR/WebResponseObject.CoreClr.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/CoreCLR/WebResponseObject.CoreClr.cs @@ -31,23 +31,17 @@ namespace Microsoft.PowerShell.Commands { get { - var headers = new Dictionary>(StringComparer.OrdinalIgnoreCase); - foreach (var entry in BaseResponse.Headers) + if(_headers == null) { - headers[entry.Key] = entry.Value; - } - if (BaseResponse.Content != null) - { - foreach (var entry in BaseResponse.Content.Headers) - { - headers[entry.Key] = entry.Value; - } + _headers = WebResponseHelper.GetHeadersDictionary(BaseResponse); } - return headers; + return _headers; } } + private Dictionary> _headers = null; + /// /// gets the RelationLink property ///