From c3a080caf9c46c2a742b032e4a85d05c21129fe4 Mon Sep 17 00:00:00 2001 From: CarloToso <105941898+CarloToso@users.noreply.github.com> Date: Tue, 13 Dec 2022 14:18:16 +0100 Subject: [PATCH] WebResponseObject.Common merge partials atomic commits (#18703) --- .../Common/WebResponseObject.Common.cs | 186 +++++++----------- 1 file changed, 75 insertions(+), 111 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebResponseObject.Common.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebResponseObject.Common.cs index 6f9c14638..f92296905 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebResponseObject.Common.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebResponseObject.Common.cs @@ -13,101 +13,7 @@ namespace Microsoft.PowerShell.Commands /// /// WebResponseObject. /// - public partial class WebResponseObject - { - #region Properties - - /// - /// Gets or protected sets the response body content. - /// - [SuppressMessage("Microsoft.Performance", "CA1819:PropertiesShouldNotReturnArrays")] - public byte[] Content { get; protected set; } - - /// - /// Gets the response status code. - /// - public int StatusCode - { - get { return (WebResponseHelper.GetStatusCode(BaseResponse)); } - } - - /// - /// Gets the response status description. - /// - public string StatusDescription - { - get { return (WebResponseHelper.GetStatusDescription(BaseResponse)); } - } - - private MemoryStream _rawContentStream; - /// - /// Gets the response body content as a . - /// - public MemoryStream RawContentStream - { - get { return (_rawContentStream); } - } - - /// - /// Gets the length (in bytes) of . - /// - public long RawContentLength - { - get { return (RawContentStream == null ? -1 : RawContentStream.Length); } - } - - /// - /// Gets or protected sets the full response content. - /// - /// - /// Full response content, including the HTTP status line, headers, and body. - /// - public string RawContent { get; protected set; } - - #endregion Properties - - #region Methods - - /// - /// Reads the response content from the web response. - /// - private void InitializeContent() - { - this.Content = this.RawContentStream.ToArray(); - } - - private static bool IsPrintable(char c) - { - return (char.IsLetterOrDigit(c) || char.IsPunctuation(c) || char.IsSeparator(c) || char.IsSymbol(c) || char.IsWhiteSpace(c)); - } - - /// - /// Returns the string representation of this web response. - /// - /// The string representation of this web response. - public sealed override string ToString() - { - char[] stringContent = System.Text.Encoding.ASCII.GetChars(Content); - for (int counter = 0; counter < stringContent.Length; counter++) - { - if (!IsPrintable(stringContent[counter])) - { - stringContent[counter] = '.'; - } - } - - return new string(stringContent); - } - - #endregion Methods - } - - // TODO: Merge Partials - - /// - /// WebResponseObject. - /// - public partial class WebResponseObject + public class WebResponseObject { #region Properties @@ -116,27 +22,52 @@ namespace Microsoft.PowerShell.Commands /// public HttpResponseMessage BaseResponse { get; set; } + /// + /// Gets or protected sets the response body content. + /// + public byte[] Content { get; protected set; } + /// /// Gets the Headers property. /// - public Dictionary> Headers - { - get - { - _headers ??= WebResponseHelper.GetHeadersDictionary(BaseResponse); - - return _headers; - } - } + public Dictionary> Headers => _headers ??= WebResponseHelper.GetHeadersDictionary(BaseResponse); private Dictionary> _headers = null; + /// + /// Gets or protected sets the full response content. + /// + /// + /// Full response content, including the HTTP status line, headers, and body. + /// + public string RawContent { get; protected set; } + + /// + /// Gets the length (in bytes) of . + /// + public long RawContentLength => RawContentStream is null ? -1 : RawContentStream.Length; + + /// + /// Gets or protected sets the response body content as a . + /// + public MemoryStream RawContentStream { get; protected set; } + /// /// Gets the RelationLink property. /// public Dictionary RelationLink { get; internal set; } - #endregion + /// + /// Gets the response status code. + /// + public int StatusCode => WebResponseHelper.GetStatusCode(BaseResponse); + + /// + /// Gets the response status description. + /// + public string StatusDescription => WebResponseHelper.GetStatusDescription(BaseResponse); + + #endregion Properties #region Constructors @@ -165,6 +96,14 @@ namespace Microsoft.PowerShell.Commands #region Methods + /// + /// Reads the response content from the web response. + /// + private void InitializeContent() + { + this.Content = this.RawContentStream.ToArray(); + } + private void InitializeRawContent(HttpResponseMessage baseResponse) { StringBuilder raw = ContentHelper.GetRawContentHeader(baseResponse); @@ -178,21 +117,27 @@ namespace Microsoft.PowerShell.Commands this.RawContent = raw.ToString(); } + private static bool IsPrintable(char c) => char.IsLetterOrDigit(c) + || char.IsPunctuation(c) + || char.IsSeparator(c) + || char.IsSymbol(c) + || char.IsWhiteSpace(c); + private void SetResponse(HttpResponseMessage response, Stream contentStream) { - if (response == null) { throw new ArgumentNullException(nameof(response)); } + if (response is null) { throw new ArgumentNullException(nameof(response)); } BaseResponse = response; MemoryStream ms = contentStream as MemoryStream; if (ms != null) { - _rawContentStream = ms; + RawContentStream = ms; } else { Stream st = contentStream; - if (contentStream == null) + if (contentStream is null) { st = StreamHelper.GetResponseStream(response); } @@ -204,11 +149,30 @@ namespace Microsoft.PowerShell.Commands } int initialCapacity = (int)Math.Min(contentLength, StreamHelper.DefaultReadBuffer); - _rawContentStream = new WebResponseContentMemoryStream(st, initialCapacity, cmdlet: null, response.Content.Headers.ContentLength.GetValueOrDefault()); + RawContentStream = new WebResponseContentMemoryStream(st, initialCapacity, cmdlet: null, response.Content.Headers.ContentLength.GetValueOrDefault()); } // set the position of the content stream to the beginning - _rawContentStream.Position = 0; + RawContentStream.Position = 0; } - #endregion + + /// + /// Returns the string representation of this web response. + /// + /// The string representation of this web response. + public sealed override string ToString() + { + char[] stringContent = System.Text.Encoding.ASCII.GetChars(Content); + for (int counter = 0; counter < stringContent.Length; counter++) + { + if (!IsPrintable(stringContent[counter])) + { + stringContent[counter] = '.'; + } + } + + return new string(stringContent); + } + + #endregion Methods } }