From 3c1221ff6473c8d21365e1ac4d19fe2f10477eaf Mon Sep 17 00:00:00 2001 From: CarloToso <105941898+CarloToso@users.noreply.github.com> Date: Thu, 16 Mar 2023 23:41:12 +0100 Subject: [PATCH 01/37] content helper nullable --- .../WebCmdlet/Common/ContentHelper.Common.cs | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/ContentHelper.Common.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/ContentHelper.Common.cs index 41af8ebc6dc..b5e8f0ec597 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/ContentHelper.Common.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/ContentHelper.Common.cs @@ -1,6 +1,8 @@ // Copyright (c) Microsoft Corporation. // Licensed under the MIT License. +#nullable enable + using System; using System.Management.Automation; using System.Net.Http; @@ -16,7 +18,7 @@ internal static class ContentHelper #region Internal Methods // ContentType may not exist in response header. Return null if not. - internal static string GetContentType(HttpResponseMessage response) => response.Content.Headers.ContentType?.MediaType; + internal static string? GetContentType(HttpResponseMessage response) => response.Content.Headers.ContentType?.MediaType; internal static Encoding GetDefaultEncoding() => Encoding.UTF8; @@ -35,7 +37,7 @@ internal static StringBuilder GetRawContentHeader(HttpResponseMessage response) HttpHeaders[] headerCollections = { response.Headers, - response.Content?.Headers + response.Content.Headers }; foreach (var headerCollection in headerCollections) @@ -118,19 +120,19 @@ private static bool CheckIsText(string contentType) if (Platform.IsWindows && !isText) { // Media types registered with Windows as having a perceived type of text, are text - using (RegistryKey contentTypeKey = Registry.ClassesRoot.OpenSubKey(@"MIME\Database\Content Type\" + contentType)) + using (RegistryKey? contentTypeKey = Registry.ClassesRoot.OpenSubKey(@"MIME\Database\Content Type\" + contentType)) { if (contentTypeKey != null) { - string extension = contentTypeKey.GetValue("Extension") as string; + string? extension = contentTypeKey.GetValue("Extension") as string; if (extension != null) { - using (RegistryKey extensionKey = Registry.ClassesRoot.OpenSubKey(extension)) + using (RegistryKey? extensionKey = Registry.ClassesRoot.OpenSubKey(extension)) { if (extensionKey != null) { - string perceivedType = extensionKey.GetValue("PerceivedType") as string; - isText = (perceivedType == "text"); + string? perceivedType = extensionKey.GetValue("PerceivedType") as string; + isText = perceivedType == "text"; } } } @@ -161,7 +163,7 @@ private static string GetContentTypeSignature(string contentType) { if (string.IsNullOrEmpty(contentType)) { - return null; + return null!; } string sig = contentType.Split(';', 2)[0].ToUpperInvariant(); From e59425b76c84e7680f107bcf39537d7108ed7214 Mon Sep 17 00:00:00 2001 From: CarloToso <105941898+CarloToso@users.noreply.github.com> Date: Thu, 16 Mar 2023 23:41:31 +0100 Subject: [PATCH 02/37] webresponseobject nullable --- .../Common/WebResponseObject.Common.cs | 36 ++++++++----------- 1 file changed, 15 insertions(+), 21 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 5a55ccc2e73..55af7cdfa23 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 @@ -1,9 +1,10 @@ // Copyright (c) Microsoft Corporation. // Licensed under the MIT License. +#nullable enable + using System; using System.Collections.Generic; -using System.Diagnostics.CodeAnalysis; using System.IO; using System.Net.Http; using System.Text; @@ -20,19 +21,19 @@ public class WebResponseObject /// /// Gets or sets the BaseResponse property. /// - public HttpResponseMessage BaseResponse { get; set; } + public HttpResponseMessage? BaseResponse { get; set; } /// /// Gets or protected sets the response body content. /// - public byte[] Content { get; protected set; } + public byte[]? Content { get; protected set; } /// /// Gets the Headers property. /// public Dictionary> Headers => _headers ??= WebResponseHelper.GetHeadersDictionary(BaseResponse); - private Dictionary> _headers = null; + private Dictionary>? _headers; /// /// Gets or protected sets the full response content. @@ -40,7 +41,7 @@ public class WebResponseObject /// /// Full response content, including the HTTP status line, headers, and body. /// - public string RawContent { get; protected set; } + public string? RawContent { get; protected set; } /// /// Gets the length (in bytes) of . @@ -50,12 +51,12 @@ public class WebResponseObject /// /// Gets or protected sets the response body content as a . /// - public MemoryStream RawContentStream { get; protected set; } + public MemoryStream? RawContentStream { get; protected set; } /// /// Gets the RelationLink property. /// - public Dictionary RelationLink { get; internal set; } + public Dictionary? RelationLink { get; internal set; } /// /// Gets the response status code. @@ -71,13 +72,6 @@ public class WebResponseObject #region Constructors - /// - /// Initializes a new instance of the class. - /// - /// - public WebResponseObject(HttpResponseMessage response) : this(response, null) - { } - /// /// Initializes a new instance of the class /// with the specified . @@ -100,7 +94,7 @@ public WebResponseObject(HttpResponseMessage response, Stream contentStream) /// private void InitializeContent() { - Content = RawContentStream.ToArray(); + Content = RawContentStream?.ToArray(); } private void InitializeRawContent(HttpResponseMessage baseResponse) @@ -108,9 +102,9 @@ private void InitializeRawContent(HttpResponseMessage baseResponse) StringBuilder raw = ContentHelper.GetRawContentHeader(baseResponse); // Use ASCII encoding for the RawContent visual view of the content. - if (Content.Length > 0) + if (Content?.Length > 0) { - raw.Append(this.ToString()); + raw.Append(ToString()); } RawContent = raw.ToString(); @@ -128,7 +122,7 @@ private void SetResponse(HttpResponseMessage response, Stream contentStream) BaseResponse = response; - MemoryStream ms = contentStream as MemoryStream; + MemoryStream? ms = contentStream as MemoryStream; if (ms is not null) { RawContentStream = ms; @@ -141,7 +135,7 @@ private void SetResponse(HttpResponseMessage response, Stream contentStream) st = StreamHelper.GetResponseStream(response); } - long contentLength = response.Content.Headers.ContentLength.Value; + long contentLength = response.Content.Headers.ContentLength.GetValueOrDefault(); if (contentLength <= 0) { contentLength = StreamHelper.DefaultReadBuffer; @@ -161,8 +155,8 @@ private void SetResponse(HttpResponseMessage response, Stream contentStream) /// 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++) + char[]? stringContent = Content is null ? null : Encoding.ASCII.GetChars(Content); + for (int counter = 0; counter < stringContent?.Length; counter++) { if (!IsPrintable(stringContent[counter])) { From 68fc5442328fd8781ba08e0702bdfbfbf9a781ed Mon Sep 17 00:00:00 2001 From: CarloToso <105941898+CarloToso@users.noreply.github.com> Date: Thu, 16 Mar 2023 23:51:39 +0100 Subject: [PATCH 03/37] stream helper nullable --- .../commands/utility/WebCmdlet/StreamHelper.cs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/StreamHelper.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/StreamHelper.cs index 1ddaf5a5794..3f17765e5f6 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/StreamHelper.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/StreamHelper.cs @@ -1,6 +1,8 @@ // Copyright (c) Microsoft Corporation. // Licensed under the MIT License. +#nullable enable + using System; using System.IO; using System.Management.Automation; @@ -433,7 +435,7 @@ internal static bool TryGetEncoding(string characterSet, out Encoding encoding) } catch (ArgumentException) { - encoding = null; + encoding = null!; } return result; From d72776397703251098efcbd39b4f63da266df8af Mon Sep 17 00:00:00 2001 From: CarloToso <105941898+CarloToso@users.noreply.github.com> Date: Thu, 16 Mar 2023 23:56:52 +0100 Subject: [PATCH 04/37] webproxy nullable --- .../commands/utility/WebCmdlet/CoreCLR/WebProxy.cs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/CoreCLR/WebProxy.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/CoreCLR/WebProxy.cs index 6890599b51b..6e9f6bdf98e 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/CoreCLR/WebProxy.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/CoreCLR/WebProxy.cs @@ -1,6 +1,8 @@ // Copyright (c) Microsoft Corporation. // Licensed under the MIT License. +#nullable enable + using System; using System.Net; @@ -8,7 +10,7 @@ namespace Microsoft.PowerShell.Commands { internal class WebProxy : IWebProxy, IEquatable { - private ICredentials _credentials; + private ICredentials? _credentials; private readonly Uri _proxyAddress; internal WebProxy(Uri address) @@ -18,11 +20,11 @@ internal WebProxy(Uri address) _proxyAddress = address; } - public override bool Equals(object obj) => Equals(obj as WebProxy); + public override bool Equals(object? obj) => Equals(obj as WebProxy); public override int GetHashCode() => HashCode.Combine(_proxyAddress, _credentials, BypassProxyOnLocal); - public bool Equals(WebProxy other) + public bool Equals(WebProxy? other) { if (other is null) { @@ -35,7 +37,7 @@ public bool Equals(WebProxy other) && BypassProxyOnLocal == other.BypassProxyOnLocal; } - public ICredentials Credentials + public ICredentials? Credentials { get => _credentials; From 8b558e39680f61aec391a930e2614a821ca73dae Mon Sep 17 00:00:00 2001 From: CarloToso <105941898+CarloToso@users.noreply.github.com> Date: Fri, 17 Mar 2023 00:01:10 +0100 Subject: [PATCH 05/37] fixes --- .../commands/utility/WebCmdlet/StreamHelper.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/StreamHelper.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/StreamHelper.cs index 3f17765e5f6..f8d3a8b7fcd 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/StreamHelper.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/StreamHelper.cs @@ -28,7 +28,7 @@ internal class WebResponseContentMemoryStream : MemoryStream private readonly long? _contentLength; private readonly Stream _originalStreamToProxy; private bool _isInitialized = false; - private readonly Cmdlet _ownerCmdlet; + private readonly Cmdlet? _ownerCmdlet; #endregion Data @@ -40,7 +40,7 @@ internal class WebResponseContentMemoryStream : MemoryStream /// /// Owner cmdlet if any. /// Expected download size in Bytes. - internal WebResponseContentMemoryStream(Stream stream, int initialCapacity, Cmdlet cmdlet, long? contentLength) : base(initialCapacity) + internal WebResponseContentMemoryStream(Stream stream, int initialCapacity, Cmdlet? cmdlet, long? contentLength) : base(initialCapacity) { this._contentLength = contentLength; _originalStreamToProxy = stream; From e8fefea7fbeaf495b6339d4b606c8ebdc6c7ecb8 Mon Sep 17 00:00:00 2001 From: CarloToso <105941898+CarloToso@users.noreply.github.com> Date: Fri, 17 Mar 2023 00:16:43 +0100 Subject: [PATCH 06/37] nullable headernames --- .../commands/utility/WebCmdlet/CoreCLR/HttpKnownHeaderNames.cs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/CoreCLR/HttpKnownHeaderNames.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/CoreCLR/HttpKnownHeaderNames.cs index e0a9b3760ef..6571696e5bf 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/CoreCLR/HttpKnownHeaderNames.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/CoreCLR/HttpKnownHeaderNames.cs @@ -1,6 +1,8 @@ // Copyright (c) Microsoft Corporation. // Licensed under the MIT License. +#nullable enable + using System; using System.Collections.Generic; From a4b11036e9d7cb1937c3a6ff3d75058650fc7c3d Mon Sep 17 00:00:00 2001 From: CarloToso <105941898+CarloToso@users.noreply.github.com> Date: Fri, 17 Mar 2023 00:23:14 +0100 Subject: [PATCH 07/37] nullable invokerestmethod --- .../Common/InvokeRestMethodCommand.Common.cs | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/InvokeRestMethodCommand.Common.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/InvokeRestMethodCommand.Common.cs index 0c97211faea..0d27c38e327 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/InvokeRestMethodCommand.Common.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/InvokeRestMethodCommand.Common.cs @@ -1,6 +1,8 @@ // Copyright (c) Microsoft Corporation. // Licensed under the MIT License. +#nullable enable + using System; using System.IO; using System.Management.Automation; @@ -55,13 +57,13 @@ public int MaximumFollowRelLink /// [Parameter] [Alias("RHV")] - public string ResponseHeadersVariable { get; set; } + public string? ResponseHeadersVariable { get; set; } /// /// Gets or sets the variable name to use for storing the status code from the response. /// [Parameter] - public string StatusCodeVariable { get; set; } + public string? StatusCodeVariable { get; set; } #endregion Parameters @@ -97,8 +99,8 @@ internal override void ProcessResponse(HttpResponseMessage response) string str = StreamHelper.DecodeStream(responseStream, charSet, out Encoding encoding); - object obj = null; - Exception ex = null; + object? obj = null; + Exception? ex = null; string encodingVerboseName; try @@ -161,7 +163,7 @@ private static RestReturnType CheckReturnType(HttpResponseMessage response) ArgumentNullException.ThrowIfNull(response); RestReturnType rt = RestReturnType.Detect; - string contentType = ContentHelper.GetContentType(response); + string? contentType = ContentHelper.GetContentType(response); if (string.IsNullOrEmpty(contentType)) { rt = RestReturnType.Detect; @@ -217,7 +219,7 @@ private bool TryProcessFeedStream(Stream responseStream) ) { // This one will do reader.Read() internally - XmlNode result = workingDocument.ReadNode(reader); + XmlNode? result = workingDocument.ReadNode(reader); WriteObject(result); } else @@ -256,7 +258,7 @@ private static XmlReaderSettings GetSecureXmlReaderSettings() return xrs; } - private static bool TryConvertToXml(string xml, out object doc, ref Exception exRef) + private static bool TryConvertToXml(string xml, out object? doc, ref Exception? exRef) { try { @@ -278,7 +280,7 @@ private static bool TryConvertToXml(string xml, out object doc, ref Exception ex return doc != null; } - private static bool TryConvertToJson(string json, out object obj, ref Exception exRef) + private static bool TryConvertToJson(string json, out object? obj, ref Exception? exRef) { bool converted = false; try From 07f1104e689f9bb4945ef3d572fb6e2b4f051970 Mon Sep 17 00:00:00 2001 From: CarloToso <105941898+CarloToso@users.noreply.github.com> Date: Fri, 17 Mar 2023 00:25:25 +0100 Subject: [PATCH 08/37] nullable webcmdletelementcollection --- .../utility/WebCmdlet/WebCmdletElementCollection.cs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/WebCmdletElementCollection.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/WebCmdletElementCollection.cs index 8336249efb5..99326898d9f 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/WebCmdletElementCollection.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/WebCmdletElementCollection.cs @@ -1,6 +1,8 @@ // Copyright (c) Microsoft Corporation. // Licensed under the MIT License. +#nullable enable + using System.Collections.Generic; using System.Collections.ObjectModel; using System.Management.Automation; @@ -21,23 +23,23 @@ internal WebCmdletElementCollection(IList list) : base(list) /// /// /// Found element as PSObject. - public PSObject Find(string nameOrId) => FindById(nameOrId) ?? FindByName(nameOrId); + public PSObject? Find(string nameOrId) => FindById(nameOrId) ?? FindByName(nameOrId); /// /// Finds the element by id. /// /// /// Found element as PSObject. - public PSObject FindById(string id) => Find(id, findById: true); + public PSObject? FindById(string id) => Find(id, findById: true); /// /// Finds the element by name. /// /// /// Found element as PSObject. - public PSObject FindByName(string name) => Find(name, findById: false); + public PSObject? FindByName(string name) => Find(name, findById: false); - private PSObject Find(string nameOrId, bool findById) + private PSObject? Find(string nameOrId, bool findById) { foreach (PSObject candidate in this) { From fe375e88d17262c7949a2ff78a0a4dc8df997a3e Mon Sep 17 00:00:00 2001 From: CarloToso <105941898+CarloToso@users.noreply.github.com> Date: Fri, 17 Mar 2023 01:07:29 +0100 Subject: [PATCH 09/37] fixes --- .../WebCmdlet/Common/ContentHelper.Common.cs | 16 ++++++++-------- .../commands/utility/WebCmdlet/StreamHelper.cs | 15 +++++---------- 2 files changed, 13 insertions(+), 18 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/ContentHelper.Common.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/ContentHelper.Common.cs index b5e8f0ec597..7a09b5da98f 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/ContentHelper.Common.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/ContentHelper.Common.cs @@ -61,19 +61,19 @@ internal static StringBuilder GetRawContentHeader(HttpResponseMessage response) return raw; } - internal static bool IsJson(string contentType) + internal static bool IsJson(string? contentType) { contentType = GetContentTypeSignature(contentType); return CheckIsJson(contentType); } - internal static bool IsText(string contentType) + internal static bool IsText(string? contentType) { contentType = GetContentTypeSignature(contentType); return CheckIsText(contentType); } - internal static bool IsXml(string contentType) + internal static bool IsXml(string? contentType) { contentType = GetContentTypeSignature(contentType); return CheckIsXml(contentType); @@ -83,7 +83,7 @@ internal static bool IsXml(string contentType) #region Private Helper Methods - private static bool CheckIsJson(string contentType) + private static bool CheckIsJson(string? contentType) { if (string.IsNullOrEmpty(contentType)) { @@ -104,7 +104,7 @@ private static bool CheckIsJson(string contentType) return isJson; } - private static bool CheckIsText(string contentType) + private static bool CheckIsText(string? contentType) { if (string.IsNullOrEmpty(contentType)) { @@ -143,7 +143,7 @@ private static bool CheckIsText(string contentType) return isText; } - private static bool CheckIsXml(string contentType) + private static bool CheckIsXml(string? contentType) { if (string.IsNullOrEmpty(contentType)) { @@ -159,11 +159,11 @@ private static bool CheckIsXml(string contentType) return isXml; } - private static string GetContentTypeSignature(string contentType) + private static string? GetContentTypeSignature(string? contentType) { if (string.IsNullOrEmpty(contentType)) { - return null!; + return null; } string sig = contentType.Split(';', 2)[0].ToUpperInvariant(); diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/StreamHelper.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/StreamHelper.cs index f8d3a8b7fcd..d79d178cadb 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/StreamHelper.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/StreamHelper.cs @@ -357,7 +357,7 @@ private static string StreamToString(Stream stream, Encoding encoding) while (!completed) { // If this is the last input data, flush the decoder's internal buffer and state. - bool flush = (bytesRead == 0); + bool flush = bytesRead is 0; decoder.Convert(bytes, byteIndex, bytesRead - byteIndex, chars, 0, useBufferSize, flush, out int bytesUsed, out int charsUsed, out completed); @@ -385,13 +385,7 @@ private static string StreamToString(Stream stream, Encoding encoding) internal static string DecodeStream(Stream stream, string characterSet, out Encoding encoding) { - bool isDefaultEncoding = false; - if (!TryGetEncoding(characterSet, out encoding)) - { - // Use the default encoding if one wasn't provided - encoding = ContentHelper.GetDefaultEncoding(); - isDefaultEncoding = true; - } + bool isDefaultEncoding = TryGetEncoding(characterSet, out encoding); string content = StreamToString(stream, encoding); if (isDefaultEncoding) @@ -416,7 +410,7 @@ internal static string DecodeStream(Stream stream, string characterSet, out Enco if (TryGetEncoding(characterSet, out Encoding localEncoding)) { stream.Seek(0, SeekOrigin.Begin); - content = StreamToString(stream, localEncoding); + content = StreamToString(stream, localEncoding!); encoding = localEncoding; } } @@ -435,7 +429,8 @@ internal static bool TryGetEncoding(string characterSet, out Encoding encoding) } catch (ArgumentException) { - encoding = null!; + // Use the default encoding if one wasn't provided + encoding = ContentHelper.GetDefaultEncoding(); } return result; From 815e673a8e824f732646afb41d1389feeb37edb4 Mon Sep 17 00:00:00 2001 From: CarloToso <105941898+CarloToso@users.noreply.github.com> Date: Fri, 17 Mar 2023 01:13:19 +0100 Subject: [PATCH 10/37] psuseragent nullable --- .../commands/utility/WebCmdlet/PSUserAgent.cs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/PSUserAgent.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/PSUserAgent.cs index e4312e74975..1a19d6b0457 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/PSUserAgent.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/PSUserAgent.cs @@ -1,6 +1,8 @@ // Copyright (c) Microsoft Corporation. // Licensed under the MIT License. +#nullable enable + using System; using System.Globalization; using System.Management.Automation; @@ -14,7 +16,7 @@ namespace Microsoft.PowerShell.Commands /// public static class PSUserAgent { - private static string s_windowsUserAgent; + private static string? s_windowsUserAgent; // Format the user-agent string from the various component parts internal static string UserAgent => string.Create(CultureInfo.InvariantCulture, $"{Compatibility} ({PlatformName}; {OS}; {Culture}) {App}"); From 19016a747e22e2c5343377abb6310317badb6b1d Mon Sep 17 00:00:00 2001 From: CarloToso <105941898+CarloToso@users.noreply.github.com> Date: Fri, 17 Mar 2023 01:14:21 +0100 Subject: [PATCH 11/37] webresponseobjectfactory nullable --- .../WebCmdlet/CoreCLR/WebResponseObjectFactory.CoreClr.cs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/CoreCLR/WebResponseObjectFactory.CoreClr.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/CoreCLR/WebResponseObjectFactory.CoreClr.cs index 4c8f1c40321..1323f537d84 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/CoreCLR/WebResponseObjectFactory.CoreClr.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/CoreCLR/WebResponseObjectFactory.CoreClr.cs @@ -1,6 +1,8 @@ // Copyright (c) Microsoft Corporation. // Licensed under the MIT License. +#nullable enable + using System.IO; using System.Management.Automation; using System.Net.Http; From cf1790cefb37b937a07b5a87ec6e109ba90b6813 Mon Sep 17 00:00:00 2001 From: CarloToso <105941898+CarloToso@users.noreply.github.com> Date: Fri, 17 Mar 2023 01:50:54 +0100 Subject: [PATCH 12/37] formobject nullable --- .../commands/utility/WebCmdlet/FormObject.cs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/FormObject.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/FormObject.cs index b496a120329..872c1ddc0ac 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/FormObject.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/FormObject.cs @@ -1,6 +1,8 @@ // Copyright (c) Microsoft Corporation. // Licensed under the MIT License. +#nullable enable + using System.Collections.Generic; namespace Microsoft.PowerShell.Commands @@ -46,7 +48,7 @@ public FormObject(string id, string method, string action) internal void AddField(string key, string value) { - if (key is not null && !Fields.TryGetValue(key, out string test)) + if (key is not null && !Fields.TryGetValue(key, out string? test)) { Fields[key] = value; } From 522c63099691ae997659924e87d98dc7a0334e63 Mon Sep 17 00:00:00 2001 From: CarloToso <105941898+CarloToso@users.noreply.github.com> Date: Fri, 17 Mar 2023 01:52:59 +0100 Subject: [PATCH 13/37] formobjectcollection nullable --- .../commands/utility/WebCmdlet/FormObjectCollection.cs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/FormObjectCollection.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/FormObjectCollection.cs index f435b92e484..5f923558185 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/FormObjectCollection.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/FormObjectCollection.cs @@ -1,6 +1,8 @@ // Copyright (c) Microsoft Corporation. // Licensed under the MIT License. +#nullable enable + using System; using System.Collections.ObjectModel; @@ -16,11 +18,11 @@ public class FormObjectCollection : Collection /// /// /// - public FormObject this[string key] + public FormObject? this[string key] { get { - FormObject form = null; + FormObject? form = null; foreach (FormObject f in this) { if (string.Equals(key, f.Id, StringComparison.OrdinalIgnoreCase)) From ea7462978f45c6cec3c5753e49bd9836656fc20f Mon Sep 17 00:00:00 2001 From: CarloToso <105941898+CarloToso@users.noreply.github.com> Date: Fri, 17 Mar 2023 01:57:46 +0100 Subject: [PATCH 14/37] small changes --- .../WebCmdlet/CoreCLR/InvokeWebRequestCommand.CoreClr.cs | 4 ++-- .../commands/utility/WebCmdlet/WebRequestMethod.cs | 2 ++ 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/CoreCLR/InvokeWebRequestCommand.CoreClr.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/CoreCLR/InvokeWebRequestCommand.CoreClr.cs index e101bcc65b7..f54170282ef 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/CoreCLR/InvokeWebRequestCommand.CoreClr.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/CoreCLR/InvokeWebRequestCommand.CoreClr.cs @@ -37,7 +37,7 @@ internal override void ProcessResponse(HttpResponseMessage response) Stream responseStream = StreamHelper.GetResponseStream(response); if (ShouldWriteToPipeline) { - // creating a MemoryStream wrapper to response stream here to support IsStopping. + // Creating a MemoryStream wrapper to response stream here to support IsStopping. responseStream = new WebResponseContentMemoryStream( responseStream, StreamHelper.ChunkSize, @@ -47,7 +47,7 @@ internal override void ProcessResponse(HttpResponseMessage response) ro.RelationLink = _relationLink; WriteObject(ro); - // use the rawcontent stream from WebResponseObject for further + // Use the rawcontent stream from WebResponseObject for further // processing of the stream. This is need because WebResponse's // stream can be used only once. responseStream = ro.RawContentStream; diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/WebRequestMethod.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/WebRequestMethod.cs index aa7067f1c23..9b90115a1d5 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/WebRequestMethod.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/WebRequestMethod.cs @@ -1,6 +1,8 @@ // Copyright (c) Microsoft Corporation. // Licensed under the MIT License. +#nullable enable + namespace Microsoft.PowerShell.Commands { /// From ec5af71b13ece38bd12902c8aff4a3f295d61cc6 Mon Sep 17 00:00:00 2001 From: CarloToso <105941898+CarloToso@users.noreply.github.com> Date: Fri, 17 Mar 2023 02:26:25 +0100 Subject: [PATCH 15/37] basichtmlresponseobject nullable WIP --- .../BasicHtmlWebResponseObject.Common.cs | 48 +++++++++---------- .../WebCmdlet/Common/ContentHelper.Common.cs | 2 +- 2 files changed, 23 insertions(+), 27 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/BasicHtmlWebResponseObject.Common.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/BasicHtmlWebResponseObject.Common.cs index 8e8c099c661..6995ec7bab2 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/BasicHtmlWebResponseObject.Common.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/BasicHtmlWebResponseObject.Common.cs @@ -1,6 +1,8 @@ // Copyright (c) Microsoft Corporation. // Licensed under the MIT License. +#nullable enable + using System.Collections.Generic; using System.Diagnostics; using System.IO; @@ -18,23 +20,17 @@ public class BasicHtmlWebResponseObject : WebResponseObject { #region Private Fields - private static Regex s_attribNameValueRegex; - private static Regex s_attribsRegex; - private static Regex s_imageRegex; - private static Regex s_inputFieldRegex; - private static Regex s_linkRegex; - private static Regex s_tagRegex; + private static Regex? s_attribNameValueRegex; + private static Regex? s_attribsRegex; + private static Regex? s_imageRegex; + private static Regex? s_inputFieldRegex; + private static Regex? s_linkRegex; + private static Regex? s_tagRegex; #endregion Private Fields #region Constructors - /// - /// Initializes a new instance of the class. - /// - /// - public BasicHtmlWebResponseObject(HttpResponseMessage response) : this(response, null) { } - /// /// Initializes a new instance of the class /// with the specified . @@ -60,7 +56,7 @@ public BasicHtmlWebResponseObject(HttpResponseMessage response, Stream contentSt /// if the Content-Type response header is a recognized text /// type. Otherwise . /// - public new string Content { get; private set; } + public new string? Content { get; private set; } /// /// Gets the encoding of the text body content of this response. @@ -69,9 +65,9 @@ public BasicHtmlWebResponseObject(HttpResponseMessage response, Stream contentSt /// Encoding of the response body from the Content-Type header, /// or if the encoding could not be determined. /// - public Encoding Encoding { get; private set; } + public Encoding? Encoding { get; private set; } - private WebCmdletElementCollection _inputFields; + private WebCmdletElementCollection? _inputFields; /// /// Gets the HTML input field elements parsed from . @@ -85,7 +81,7 @@ public WebCmdletElementCollection InputFields EnsureHtmlParser(); List parsedFields = new(); - MatchCollection fieldMatch = s_inputFieldRegex.Matches(Content); + MatchCollection fieldMatch = s_inputFieldRegex!.Matches(Content!); foreach (Match field in fieldMatch) { parsedFields.Add(CreateHtmlObject(field.Value, "INPUT")); @@ -98,7 +94,7 @@ public WebCmdletElementCollection InputFields } } - private WebCmdletElementCollection _links; + private WebCmdletElementCollection? _links; /// /// Gets the HTML a link elements parsed from . @@ -112,7 +108,7 @@ public WebCmdletElementCollection Links EnsureHtmlParser(); List parsedLinks = new(); - MatchCollection linkMatch = s_linkRegex.Matches(Content); + MatchCollection linkMatch = s_linkRegex!.Matches(Content!); foreach (Match link in linkMatch) { parsedLinks.Add(CreateHtmlObject(link.Value, "A")); @@ -125,7 +121,7 @@ public WebCmdletElementCollection Links } } - private WebCmdletElementCollection _images; + private WebCmdletElementCollection? _images; /// /// Gets the HTML img elements parsed from . @@ -139,7 +135,7 @@ public WebCmdletElementCollection Images EnsureHtmlParser(); List parsedImages = new(); - MatchCollection imageMatch = s_imageRegex.Matches(Content); + MatchCollection imageMatch = s_imageRegex!.Matches(Content!); foreach (Match image in imageMatch) { parsedImages.Add(CreateHtmlObject(image.Value, "IMG")); @@ -161,13 +157,13 @@ public WebCmdletElementCollection Images /// protected void InitializeContent() { - string contentType = ContentHelper.GetContentType(BaseResponse); + string? contentType = ContentHelper.GetContentType(BaseResponse); if (ContentHelper.IsText(contentType)) { // Fill the Content buffer string characterSet = WebResponseHelper.GetCharacterSet(BaseResponse); - Content = StreamHelper.DecodeStream(RawContentStream, characterSet, out Encoding encoding); + Content = StreamHelper.DecodeStream(RawContentStream!, characterSet, out Encoding encoding); Encoding = encoding; } else @@ -223,23 +219,23 @@ private static void ParseAttributes(string outerHtml, PSObject elementObject) { // Extract just the opening tag of the HTML element (omitting the closing tag and any contents, // including contained HTML elements) - Match match = s_tagRegex.Match(outerHtml); + Match match = s_tagRegex!.Match(outerHtml); // Extract all the attribute specifications within the HTML element opening tag - MatchCollection attribMatches = s_attribsRegex.Matches(match.Value); + MatchCollection attribMatches = s_attribsRegex!.Matches(match.Value); foreach (Match attribMatch in attribMatches) { // Extract the name and value for this attribute (allowing for variations like single/double/no // quotes, and no value at all) - Match nvMatches = s_attribNameValueRegex.Match(attribMatch.Value); + Match nvMatches = s_attribNameValueRegex!.Match(attribMatch.Value); Debug.Assert(nvMatches.Groups.Count == 5); // Name is always captured by group #1 string name = nvMatches.Groups[1].Value; // The value (if any) is captured by group #2, #3, or #4, depending on quoting or lack thereof - string value = null; + string? value = null; if (nvMatches.Groups[2].Success) { value = nvMatches.Groups[2].Value; diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/ContentHelper.Common.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/ContentHelper.Common.cs index 7a09b5da98f..51ef37f710c 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/ContentHelper.Common.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/ContentHelper.Common.cs @@ -18,7 +18,7 @@ internal static class ContentHelper #region Internal Methods // ContentType may not exist in response header. Return null if not. - internal static string? GetContentType(HttpResponseMessage response) => response.Content.Headers.ContentType?.MediaType; + internal static string? GetContentType(HttpResponseMessage? response) => response?.Content.Headers.ContentType?.MediaType; internal static Encoding GetDefaultEncoding() => Encoding.UTF8; From c329321e99b125e0300a30b2ad6b30d298572624 Mon Sep 17 00:00:00 2001 From: CarloToso <105941898+CarloToso@users.noreply.github.com> Date: Fri, 17 Mar 2023 03:13:27 +0100 Subject: [PATCH 16/37] !TryGetEncoding(characterSet, out encoding); --- .../commands/utility/WebCmdlet/StreamHelper.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/StreamHelper.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/StreamHelper.cs index d79d178cadb..bfceb14a26a 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/StreamHelper.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/StreamHelper.cs @@ -385,7 +385,7 @@ private static string StreamToString(Stream stream, Encoding encoding) internal static string DecodeStream(Stream stream, string characterSet, out Encoding encoding) { - bool isDefaultEncoding = TryGetEncoding(characterSet, out encoding); + bool isDefaultEncoding = !TryGetEncoding(characterSet, out encoding); string content = StreamToString(stream, encoding); if (isDefaultEncoding) From cfcd17f36b4e8dbbc944bbbda3f9088eab13ea0b Mon Sep 17 00:00:00 2001 From: CarloToso <105941898+CarloToso@users.noreply.github.com> Date: Sat, 18 Mar 2023 19:44:43 +0100 Subject: [PATCH 17/37] Update src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/ContentHelper.Common.cs Co-authored-by: Ilya --- .../commands/utility/WebCmdlet/Common/ContentHelper.Common.cs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/ContentHelper.Common.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/ContentHelper.Common.cs index 51ef37f710c..3e2cf36a644 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/ContentHelper.Common.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/ContentHelper.Common.cs @@ -124,8 +124,7 @@ private static bool CheckIsText(string? contentType) { if (contentTypeKey != null) { - string? extension = contentTypeKey.GetValue("Extension") as string; - if (extension != null) + if (contentTypeKey.GetValue("Extension") is string extension) { using (RegistryKey? extensionKey = Registry.ClassesRoot.OpenSubKey(extension)) { From 6a0846dbf19e859a635c2da8a1eb727d47431264 Mon Sep 17 00:00:00 2001 From: CarloToso <105941898+CarloToso@users.noreply.github.com> Date: Sat, 18 Mar 2023 20:24:54 +0100 Subject: [PATCH 18/37] Update src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/FormObject.cs Co-authored-by: Ilya --- .../commands/utility/WebCmdlet/FormObject.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/FormObject.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/FormObject.cs index 872c1ddc0ac..5ac4adfbb64 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/FormObject.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/FormObject.cs @@ -48,7 +48,7 @@ public FormObject(string id, string method, string action) internal void AddField(string key, string value) { - if (key is not null && !Fields.TryGetValue(key, out string? test)) + if (key is not null && !Fields.TryGetValue(key, out string? _)) { Fields[key] = value; } From 39668e2c48bf1e37630e994de1fe22ffd3ae73bb Mon Sep 17 00:00:00 2001 From: CarloToso <105941898+CarloToso@users.noreply.github.com> Date: Sat, 18 Mar 2023 23:35:29 +0100 Subject: [PATCH 19/37] [MemberNotNull]; [NotNullWhen] --- .../Common/BasicHtmlWebResponseObject.Common.cs | 12 +++++++----- .../utility/WebCmdlet/Common/ContentHelper.Common.cs | 7 ++++--- .../WebCmdlet/Common/WebResponseObject.Common.cs | 6 ++++-- 3 files changed, 15 insertions(+), 10 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/BasicHtmlWebResponseObject.Common.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/BasicHtmlWebResponseObject.Common.cs index a797de240e9..deb75b8f6ad 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/BasicHtmlWebResponseObject.Common.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/BasicHtmlWebResponseObject.Common.cs @@ -5,6 +5,7 @@ using System.Collections.Generic; using System.Diagnostics; +using System.Diagnostics.CodeAnalysis; using System.IO; using System.Management.Automation; using System.Net.Http; @@ -44,7 +45,7 @@ public BasicHtmlWebResponseObject(HttpResponseMessage response, Stream contentSt /// if the Content-Type response header is a recognized text /// type. Otherwise . /// - public new string? Content { get; private set; } + public new string Content { get; private set; } /// /// Gets the encoding of the text body content of this response. @@ -67,7 +68,7 @@ public WebCmdletElementCollection InputFields if (_inputFields == null) { List parsedFields = new(); - MatchCollection fieldMatch = HtmlParser.InputFieldRegex.Matches(Content!); + MatchCollection fieldMatch = HtmlParser.InputFieldRegex.Matches(Content); foreach (Match field in fieldMatch) { parsedFields.Add(CreateHtmlObject(field.Value, "INPUT")); @@ -92,7 +93,7 @@ public WebCmdletElementCollection Links if (_links == null) { List parsedLinks = new(); - MatchCollection linkMatch = HtmlParser.LinkRegex.Matches(Content!); + MatchCollection linkMatch = HtmlParser.LinkRegex.Matches(Content); foreach (Match link in linkMatch) { parsedLinks.Add(CreateHtmlObject(link.Value, "A")); @@ -117,7 +118,7 @@ public WebCmdletElementCollection Images if (_images == null) { List parsedImages = new(); - MatchCollection imageMatch = HtmlParser.ImageRegex.Matches(Content!); + MatchCollection imageMatch = HtmlParser.ImageRegex.Matches(Content); foreach (Match image in imageMatch) { parsedImages.Add(CreateHtmlObject(image.Value, "IMG")); @@ -137,6 +138,7 @@ public WebCmdletElementCollection Images /// /// Reads the response content from the web response. /// + [MemberNotNull(nameof(Content))] protected void InitializeContent() { string? contentType = ContentHelper.GetContentType(BaseResponse); @@ -145,7 +147,7 @@ protected void InitializeContent() // Fill the Content buffer string characterSet = WebResponseHelper.GetCharacterSet(BaseResponse); - Content = StreamHelper.DecodeStream(RawContentStream!, characterSet, out Encoding encoding); + Content = StreamHelper.DecodeStream(RawContentStream, characterSet, out Encoding encoding); Encoding = encoding; } else diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/ContentHelper.Common.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/ContentHelper.Common.cs index 3e2cf36a644..1649cf79bcc 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/ContentHelper.Common.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/ContentHelper.Common.cs @@ -4,6 +4,7 @@ #nullable enable using System; +using System.Diagnostics.CodeAnalysis; using System.Management.Automation; using System.Net.Http; using System.Net.Http.Headers; @@ -61,19 +62,19 @@ internal static StringBuilder GetRawContentHeader(HttpResponseMessage response) return raw; } - internal static bool IsJson(string? contentType) + internal static bool IsJson([NotNullWhen(true)] string? contentType) { contentType = GetContentTypeSignature(contentType); return CheckIsJson(contentType); } - internal static bool IsText(string? contentType) + internal static bool IsText([NotNullWhen(true)] string? contentType) { contentType = GetContentTypeSignature(contentType); return CheckIsText(contentType); } - internal static bool IsXml(string? contentType) + internal static bool IsXml([NotNullWhen(true)] string? contentType) { contentType = GetContentTypeSignature(contentType); return CheckIsXml(contentType); 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 55af7cdfa23..871a887fd2c 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 @@ -5,6 +5,7 @@ using System; using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; using System.IO; using System.Net.Http; using System.Text; @@ -51,7 +52,7 @@ public class WebResponseObject /// /// Gets or protected sets the response body content as a . /// - public MemoryStream? RawContentStream { get; protected set; } + public MemoryStream RawContentStream { get; protected set; } /// /// Gets the RelationLink property. @@ -94,7 +95,7 @@ public WebResponseObject(HttpResponseMessage response, Stream contentStream) /// private void InitializeContent() { - Content = RawContentStream?.ToArray(); + Content = RawContentStream.ToArray(); } private void InitializeRawContent(HttpResponseMessage baseResponse) @@ -116,6 +117,7 @@ private static bool IsPrintable(char c) => char.IsLetterOrDigit(c) || char.IsSymbol(c) || char.IsWhiteSpace(c); + [MemberNotNull(nameof(RawContentStream))] private void SetResponse(HttpResponseMessage response, Stream contentStream) { ArgumentNullException.ThrowIfNull(response); From 5df97cc9be5d4b14adbebad18d467c8f2e19aa01 Mon Sep 17 00:00:00 2001 From: CarloToso <105941898+CarloToso@users.noreply.github.com> Date: Sun, 19 Mar 2023 00:06:21 +0100 Subject: [PATCH 20/37] add back BasicHtmlWebResponseObject and WebResponseObject --- .../Common/BasicHtmlWebResponseObject.Common.cs | 8 +++++++- .../WebCmdlet/Common/WebResponseObject.Common.cs | 15 ++++++++------- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/BasicHtmlWebResponseObject.Common.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/BasicHtmlWebResponseObject.Common.cs index deb75b8f6ad..40b65694feb 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/BasicHtmlWebResponseObject.Common.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/BasicHtmlWebResponseObject.Common.cs @@ -21,13 +21,19 @@ public class BasicHtmlWebResponseObject : WebResponseObject { #region Constructors + /// + /// Initializes a new instance of the class. + /// + /// + public BasicHtmlWebResponseObject(HttpResponseMessage response) : this(response, null) { } + /// /// Initializes a new instance of the class /// with the specified . /// /// /// - public BasicHtmlWebResponseObject(HttpResponseMessage response, Stream contentStream) : base(response, contentStream) + public BasicHtmlWebResponseObject(HttpResponseMessage response, Stream? contentStream) : base(response, contentStream) { InitializeContent(); InitializeRawContent(response); 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 871a887fd2c..b53d13e5b90 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 @@ -73,13 +73,18 @@ public class WebResponseObject #region Constructors + /// + /// + /// + public WebResponseObject(HttpResponseMessage response) : this(response, null) { } + /// /// Initializes a new instance of the class /// with the specified . /// /// /// - public WebResponseObject(HttpResponseMessage response, Stream contentStream) + public WebResponseObject(HttpResponseMessage response, Stream? contentStream) { SetResponse(response, contentStream); InitializeContent(); @@ -118,7 +123,7 @@ private static bool IsPrintable(char c) => char.IsLetterOrDigit(c) || char.IsWhiteSpace(c); [MemberNotNull(nameof(RawContentStream))] - private void SetResponse(HttpResponseMessage response, Stream contentStream) + private void SetResponse(HttpResponseMessage response, Stream? contentStream) { ArgumentNullException.ThrowIfNull(response); @@ -131,11 +136,7 @@ private void SetResponse(HttpResponseMessage response, Stream contentStream) } else { - Stream st = contentStream; - if (contentStream is null) - { - st = StreamHelper.GetResponseStream(response); - } + Stream st = contentStream ?? StreamHelper.GetResponseStream(response); long contentLength = response.Content.Headers.ContentLength.GetValueOrDefault(); if (contentLength <= 0) From 02539dd3d04259b0ef7776ab31713dfe4b04838a Mon Sep 17 00:00:00 2001 From: CarloToso <105941898+CarloToso@users.noreply.github.com> Date: Sun, 19 Mar 2023 11:31:03 +0100 Subject: [PATCH 21/37] [NotNullWhen(true)] --- .../utility/WebCmdlet/Common/ContentHelper.Common.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/ContentHelper.Common.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/ContentHelper.Common.cs index 1649cf79bcc..0e83e74f591 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/ContentHelper.Common.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/ContentHelper.Common.cs @@ -84,7 +84,7 @@ internal static bool IsXml([NotNullWhen(true)] string? contentType) #region Private Helper Methods - private static bool CheckIsJson(string? contentType) + private static bool CheckIsJson([NotNullWhen(true)] string? contentType) { if (string.IsNullOrEmpty(contentType)) { @@ -105,7 +105,7 @@ private static bool CheckIsJson(string? contentType) return isJson; } - private static bool CheckIsText(string? contentType) + private static bool CheckIsText([NotNullWhen(true)] string? contentType) { if (string.IsNullOrEmpty(contentType)) { @@ -143,7 +143,7 @@ private static bool CheckIsText(string? contentType) return isText; } - private static bool CheckIsXml(string? contentType) + private static bool CheckIsXml([NotNullWhen(true)] string? contentType) { if (string.IsNullOrEmpty(contentType)) { From ba590b9aa6871ec6a365e94a5ae7ec98e44052b6 Mon Sep 17 00:00:00 2001 From: CarloToso <105941898+CarloToso@users.noreply.github.com> Date: Mon, 20 Mar 2023 19:39:30 +0100 Subject: [PATCH 22/37] [NotNullWhen(true)] --- .../Common/InvokeRestMethodCommand.Common.cs | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/InvokeRestMethodCommand.Common.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/InvokeRestMethodCommand.Common.cs index 0d27c38e327..0f50096364f 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/InvokeRestMethodCommand.Common.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/InvokeRestMethodCommand.Common.cs @@ -4,6 +4,7 @@ #nullable enable using System; +using System.Diagnostics.CodeAnalysis; using System.IO; using System.Management.Automation; using System.Net.Http; @@ -258,7 +259,7 @@ private static XmlReaderSettings GetSecureXmlReaderSettings() return xrs; } - private static bool TryConvertToXml(string xml, out object? doc, ref Exception? exRef) + private static bool TryConvertToXml(string xml, [NotNullWhen(true)] out object? doc, ref Exception? exRef) { try { @@ -280,13 +281,11 @@ private static bool TryConvertToXml(string xml, out object? doc, ref Exception? return doc != null; } - private static bool TryConvertToJson(string json, out object? obj, ref Exception? exRef) + private static bool TryConvertToJson(string json, [NotNullWhen(true)] out object? obj, ref Exception? exRef) { - bool converted = false; try { - ErrorRecord error; - obj = JsonObject.ConvertFromJson(json, out error); + obj = JsonObject.ConvertFromJson(json, out ErrorRecord error); if (obj == null) { @@ -300,10 +299,6 @@ private static bool TryConvertToJson(string json, out object? obj, ref Exception exRef = error.Exception; obj = null; } - else - { - converted = true; - } } catch (Exception ex) when (ex is ArgumentException || ex is InvalidOperationException) { @@ -317,7 +312,7 @@ private static bool TryConvertToJson(string json, out object? obj, ref Exception obj = null; } - return converted; + return obj != null; } #endregion Helper Methods From 1d2d715aa7a48bd0a7c487ad722564f990f1e8ad Mon Sep 17 00:00:00 2001 From: CarloToso <105941898+CarloToso@users.noreply.github.com> Date: Mon, 20 Mar 2023 20:11:34 +0100 Subject: [PATCH 23/37] revert --- .../WebCmdlet/Common/InvokeRestMethodCommand.Common.cs | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/InvokeRestMethodCommand.Common.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/InvokeRestMethodCommand.Common.cs index 0f50096364f..8c021b47ca6 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/InvokeRestMethodCommand.Common.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/InvokeRestMethodCommand.Common.cs @@ -281,8 +281,9 @@ private static bool TryConvertToXml(string xml, [NotNullWhen(true)] out object? return doc != null; } - private static bool TryConvertToJson(string json, [NotNullWhen(true)] out object? obj, ref Exception? exRef) + private static bool TryConvertToJson(string json, out object? obj, ref Exception? exRef) { + bool converted = false; try { obj = JsonObject.ConvertFromJson(json, out ErrorRecord error); @@ -299,6 +300,10 @@ private static bool TryConvertToJson(string json, [NotNullWhen(true)] out object exRef = error.Exception; obj = null; } + else + { + converted = true; + } } catch (Exception ex) when (ex is ArgumentException || ex is InvalidOperationException) { @@ -312,7 +317,7 @@ private static bool TryConvertToJson(string json, [NotNullWhen(true)] out object obj = null; } - return obj != null; + return converted; } #endregion Helper Methods From 91dccf808ecce1ae1be72cc02f235d4c5a86f29f Mon Sep 17 00:00:00 2001 From: CarloToso <105941898+CarloToso@users.noreply.github.com> Date: Mon, 20 Mar 2023 20:35:25 +0100 Subject: [PATCH 24/37] [SuppressMessage] --- .../utility/WebCmdlet/Common/InvokeRestMethodCommand.Common.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/InvokeRestMethodCommand.Common.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/InvokeRestMethodCommand.Common.cs index 8c021b47ca6..8c0a090493b 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/InvokeRestMethodCommand.Common.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/InvokeRestMethodCommand.Common.cs @@ -336,7 +336,7 @@ public enum RestReturnType /// /// Json return type. /// - [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly")] + [SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly")] Json, /// From 6414309fe158018ab8c8ff417e72c46c603beb48 Mon Sep 17 00:00:00 2001 From: CarloToso <105941898+CarloToso@users.noreply.github.com> Date: Tue, 21 Mar 2023 00:00:07 +0100 Subject: [PATCH 25/37] InvokeWebRequestCommand --- .../WebCmdlet/CoreCLR/InvokeWebRequestCommand.CoreClr.cs | 2 ++ .../commands/utility/WebCmdlet/StreamHelper.cs | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/CoreCLR/InvokeWebRequestCommand.CoreClr.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/CoreCLR/InvokeWebRequestCommand.CoreClr.cs index f54170282ef..fc5014cc29b 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/CoreCLR/InvokeWebRequestCommand.CoreClr.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/CoreCLR/InvokeWebRequestCommand.CoreClr.cs @@ -1,6 +1,8 @@ // Copyright (c) Microsoft Corporation. // Licensed under the MIT License. +#nullable enable + using System; using System.IO; using System.Management.Automation; diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/StreamHelper.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/StreamHelper.cs index bfceb14a26a..369aa823d6b 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/StreamHelper.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/StreamHelper.cs @@ -410,7 +410,7 @@ internal static string DecodeStream(Stream stream, string characterSet, out Enco if (TryGetEncoding(characterSet, out Encoding localEncoding)) { stream.Seek(0, SeekOrigin.Begin); - content = StreamToString(stream, localEncoding!); + content = StreamToString(stream, localEncoding); encoding = localEncoding; } } From 48ae8df2d2de4d50d228fab2e3dc6e25ff325f1e Mon Sep 17 00:00:00 2001 From: CarloToso <105941898+CarloToso@users.noreply.github.com> Date: Tue, 21 Mar 2023 12:28:49 +0100 Subject: [PATCH 26/37] Update src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebResponseObject.Common.cs Co-authored-by: Ilya --- .../utility/WebCmdlet/Common/WebResponseObject.Common.cs | 9 +++++++-- 1 file changed, 7 insertions(+), 2 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 b53d13e5b90..092b605a8e1 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 @@ -158,8 +158,13 @@ private void SetResponse(HttpResponseMessage response, Stream? contentStream) /// The string representation of this web response. public sealed override string ToString() { - char[]? stringContent = Content is null ? null : Encoding.ASCII.GetChars(Content); - for (int counter = 0; counter < stringContent?.Length; counter++) + if (Content is null) + { + return string.Empty; + } + + char[] stringContent = Encoding.ASCII.GetChars(Content); + for (int counter = 0; counter < stringContent.Length; counter++) { if (!IsPrintable(stringContent[counter])) { From af1add51beb89b5aecb38c452d3beed1a76feec7 Mon Sep 17 00:00:00 2001 From: CarloToso <105941898+CarloToso@users.noreply.github.com> Date: Tue, 21 Mar 2023 19:03:38 +0100 Subject: [PATCH 27/37] merge --- .../commands/utility/WebCmdlet/FormObject.cs | 57 ------------------- .../utility/WebCmdlet/FormObjectCollection.cs | 39 ------------- 2 files changed, 96 deletions(-) delete mode 100644 src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/FormObject.cs delete mode 100644 src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/FormObjectCollection.cs diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/FormObject.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/FormObject.cs deleted file mode 100644 index 5ac4adfbb64..00000000000 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/FormObject.cs +++ /dev/null @@ -1,57 +0,0 @@ -// Copyright (c) Microsoft Corporation. -// Licensed under the MIT License. - -#nullable enable - -using System.Collections.Generic; - -namespace Microsoft.PowerShell.Commands -{ - /// - /// FormObject used in HtmlWebResponseObject. - /// - public class FormObject - { - /// - /// Gets the Id property. - /// - public string Id { get; } - - /// - /// Gets the Method property. - /// - public string Method { get; } - - /// - /// Gets the Action property. - /// - public string Action { get; } - - /// - /// Gets the Fields property. - /// - public Dictionary Fields { get; } - - /// - /// Initializes a new instance of the class. - /// - /// - /// - /// - public FormObject(string id, string method, string action) - { - Id = id; - Method = method; - Action = action; - Fields = new Dictionary(); - } - - internal void AddField(string key, string value) - { - if (key is not null && !Fields.TryGetValue(key, out string? _)) - { - Fields[key] = value; - } - } - } -} diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/FormObjectCollection.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/FormObjectCollection.cs deleted file mode 100644 index 5f923558185..00000000000 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/FormObjectCollection.cs +++ /dev/null @@ -1,39 +0,0 @@ -// Copyright (c) Microsoft Corporation. -// Licensed under the MIT License. - -#nullable enable - -using System; -using System.Collections.ObjectModel; - -namespace Microsoft.PowerShell.Commands -{ - /// - /// FormObjectCollection used in HtmlWebResponseObject. - /// - public class FormObjectCollection : Collection - { - /// - /// Gets the FormObject from the key. - /// - /// - /// - public FormObject? this[string key] - { - get - { - FormObject? form = null; - foreach (FormObject f in this) - { - if (string.Equals(key, f.Id, StringComparison.OrdinalIgnoreCase)) - { - form = f; - break; - } - } - - return form; - } - } - } -} From c7e4b731994a287851d77f65daaddea0382f164e Mon Sep 17 00:00:00 2001 From: CarloToso <105941898+CarloToso@users.noreply.github.com> Date: Fri, 24 Mar 2023 09:59:19 +0100 Subject: [PATCH 28/37] merge --- .../WebResponseObjectFactory.CoreClr.cs | 21 ------------------- 1 file changed, 21 deletions(-) delete mode 100644 src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/CoreCLR/WebResponseObjectFactory.CoreClr.cs diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/CoreCLR/WebResponseObjectFactory.CoreClr.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/CoreCLR/WebResponseObjectFactory.CoreClr.cs deleted file mode 100644 index 1323f537d84..00000000000 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/CoreCLR/WebResponseObjectFactory.CoreClr.cs +++ /dev/null @@ -1,21 +0,0 @@ -// Copyright (c) Microsoft Corporation. -// Licensed under the MIT License. - -#nullable enable - -using System.IO; -using System.Management.Automation; -using System.Net.Http; - -namespace Microsoft.PowerShell.Commands -{ - internal static class WebResponseObjectFactory - { - internal static WebResponseObject GetResponseObject(HttpResponseMessage response, Stream responseStream, ExecutionContext executionContext) - { - WebResponseObject output = WebResponseHelper.IsText(response) ? new BasicHtmlWebResponseObject(response, responseStream) : new WebResponseObject(response, responseStream); - - return output; - } - } -} From 027bb41358b713c16ef83e48d2ab8e354e326c09 Mon Sep 17 00:00:00 2001 From: CarloToso <105941898+CarloToso@users.noreply.github.com> Date: Wed, 29 Mar 2023 12:10:15 +0200 Subject: [PATCH 29/37] add formobject --- .../commands/utility/WebCmdlet/FormObject.cs | 57 +++++++++++++++++++ .../utility/WebCmdlet/FormObjectCollection.cs | 39 +++++++++++++ 2 files changed, 96 insertions(+) create mode 100644 src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/FormObject.cs create mode 100644 src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/FormObjectCollection.cs diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/FormObject.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/FormObject.cs new file mode 100644 index 00000000000..5ac4adfbb64 --- /dev/null +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/FormObject.cs @@ -0,0 +1,57 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT License. + +#nullable enable + +using System.Collections.Generic; + +namespace Microsoft.PowerShell.Commands +{ + /// + /// FormObject used in HtmlWebResponseObject. + /// + public class FormObject + { + /// + /// Gets the Id property. + /// + public string Id { get; } + + /// + /// Gets the Method property. + /// + public string Method { get; } + + /// + /// Gets the Action property. + /// + public string Action { get; } + + /// + /// Gets the Fields property. + /// + public Dictionary Fields { get; } + + /// + /// Initializes a new instance of the class. + /// + /// + /// + /// + public FormObject(string id, string method, string action) + { + Id = id; + Method = method; + Action = action; + Fields = new Dictionary(); + } + + internal void AddField(string key, string value) + { + if (key is not null && !Fields.TryGetValue(key, out string? _)) + { + Fields[key] = value; + } + } + } +} diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/FormObjectCollection.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/FormObjectCollection.cs new file mode 100644 index 00000000000..5f923558185 --- /dev/null +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/FormObjectCollection.cs @@ -0,0 +1,39 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT License. + +#nullable enable + +using System; +using System.Collections.ObjectModel; + +namespace Microsoft.PowerShell.Commands +{ + /// + /// FormObjectCollection used in HtmlWebResponseObject. + /// + public class FormObjectCollection : Collection + { + /// + /// Gets the FormObject from the key. + /// + /// + /// + public FormObject? this[string key] + { + get + { + FormObject? form = null; + foreach (FormObject f in this) + { + if (string.Equals(key, f.Id, StringComparison.OrdinalIgnoreCase)) + { + form = f; + break; + } + } + + return form; + } + } + } +} From 5ee06968cafbbaee858814b11077cfa693d5cd0e Mon Sep 17 00:00:00 2001 From: CarloToso <105941898+CarloToso@users.noreply.github.com> Date: Thu, 30 Mar 2023 00:19:50 +0200 Subject: [PATCH 30/37] new syntax --- .../utility/WebCmdlet/Common/WebResponseObject.Common.cs | 3 +-- 1 file changed, 1 insertion(+), 2 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 092b605a8e1..5cd584ebf8f 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 @@ -129,8 +129,7 @@ private void SetResponse(HttpResponseMessage response, Stream? contentStream) BaseResponse = response; - MemoryStream? ms = contentStream as MemoryStream; - if (ms is not null) + if (contentStream is MemoryStream ms) { RawContentStream = ms; } From 106286477003e640bbcfe1e97e4179ff3a58bc97 Mon Sep 17 00:00:00 2001 From: CarloToso <105941898+CarloToso@users.noreply.github.com> Date: Thu, 30 Mar 2023 00:25:24 +0200 Subject: [PATCH 31/37] [NotNullWhen(true)] --- .../utility/WebCmdlet/Common/InvokeRestMethodCommand.Common.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/InvokeRestMethodCommand.Common.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/InvokeRestMethodCommand.Common.cs index e4a9906e3ff..8aaa83e1860 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/InvokeRestMethodCommand.Common.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/InvokeRestMethodCommand.Common.cs @@ -285,7 +285,7 @@ private static bool TryConvertToXml(string xml, [NotNullWhen(true)] out object? return doc != null; } - private static bool TryConvertToJson(string json, out object? obj, ref Exception? exRef) + private static bool TryConvertToJson(string json, [NotNullWhen(true)] out object? obj, ref Exception? exRef) { bool converted = false; try From 8652eaf93de4951fc34cb5a69211be6eb264588e Mon Sep 17 00:00:00 2001 From: CarloToso <105941898+CarloToso@users.noreply.github.com> Date: Thu, 30 Mar 2023 00:33:24 +0200 Subject: [PATCH 32/37] follow suggestions --- .../commands/utility/WebCmdlet/Common/ContentHelper.Common.cs | 2 +- .../utility/WebCmdlet/Common/WebResponseObject.Common.cs | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/ContentHelper.Common.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/ContentHelper.Common.cs index f69c3c692e9..bb31366f794 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/ContentHelper.Common.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/ContentHelper.Common.cs @@ -19,7 +19,7 @@ internal static class ContentHelper #region Internal Methods // ContentType may not exist in response header. Return null if not. - internal static string? GetContentType(HttpResponseMessage? response) => response?.Content.Headers.ContentType?.MediaType; + internal static string? GetContentType(HttpResponseMessage response) => response.Content.Headers.ContentType?.MediaType; internal static Encoding GetDefaultEncoding() => Encoding.UTF8; 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 5cd584ebf8f..8d4cc7ffb29 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 @@ -22,7 +22,7 @@ public class WebResponseObject /// /// Gets or sets the BaseResponse property. /// - public HttpResponseMessage? BaseResponse { get; set; } + public HttpResponseMessage BaseResponse { get; set; } /// /// Gets or protected sets the response body content. @@ -123,6 +123,7 @@ private static bool IsPrintable(char c) => char.IsLetterOrDigit(c) || char.IsWhiteSpace(c); [MemberNotNull(nameof(RawContentStream))] + [MemberNotNull(nameof(BaseResponse))] private void SetResponse(HttpResponseMessage response, Stream? contentStream) { ArgumentNullException.ThrowIfNull(response); From 585d7e0bbe518c0cd80c01b4ce40c7226570439c Mon Sep 17 00:00:00 2001 From: CarloToso <105941898+CarloToso@users.noreply.github.com> Date: Thu, 30 Mar 2023 01:17:21 +0200 Subject: [PATCH 33/37] WebResponseHelper.CoreClr nullable enable --- .../WebCmdlet/Common/BasicHtmlWebResponseObject.Common.cs | 2 +- .../WebCmdlet/Common/InvokeRestMethodCommand.Common.cs | 2 +- .../WebCmdlet/CoreCLR/WebResponseHelper.CoreClr.cs | 8 +++++--- .../commands/utility/WebCmdlet/StreamHelper.cs | 6 ++++-- 4 files changed, 11 insertions(+), 7 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/BasicHtmlWebResponseObject.Common.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/BasicHtmlWebResponseObject.Common.cs index 40b65694feb..8de62d07320 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/BasicHtmlWebResponseObject.Common.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/BasicHtmlWebResponseObject.Common.cs @@ -151,7 +151,7 @@ protected void InitializeContent() if (ContentHelper.IsText(contentType)) { // Fill the Content buffer - string characterSet = WebResponseHelper.GetCharacterSet(BaseResponse); + string? characterSet = WebResponseHelper.GetCharacterSet(BaseResponse); Content = StreamHelper.DecodeStream(RawContentStream, characterSet, out Encoding encoding); Encoding = encoding; diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/InvokeRestMethodCommand.Common.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/InvokeRestMethodCommand.Common.cs index 8aaa83e1860..acb1fcdb840 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/InvokeRestMethodCommand.Common.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/InvokeRestMethodCommand.Common.cs @@ -96,7 +96,7 @@ internal override void ProcessResponse(HttpResponseMessage response) RestReturnType returnType = CheckReturnType(response); // Try to get the response encoding from the ContentType header. - string charSet = WebResponseHelper.GetCharacterSet(response); + string? charSet = WebResponseHelper.GetCharacterSet(response); string str = StreamHelper.DecodeStream(responseStream, charSet, out Encoding encoding); 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 eff0d658c81..6e24629548b 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 @@ -1,6 +1,8 @@ // Copyright (c) Microsoft Corporation. // Licensed under the MIT License. +#nullable enable + using System; using System.Collections.Generic; using System.Globalization; @@ -11,7 +13,7 @@ namespace Microsoft.PowerShell.Commands { internal static class WebResponseHelper { - internal static string GetCharacterSet(HttpResponseMessage response) => response.Content.Headers.ContentType?.CharSet; + internal static string? GetCharacterSet(HttpResponseMessage response) => response.Content.Headers.ContentType?.CharSet; internal static Dictionary> GetHeadersDictionary(HttpResponseMessage response) { @@ -38,7 +40,7 @@ internal static Dictionary> GetHeadersDictionary(Htt internal static string GetOutFilePath(HttpResponseMessage response, string _qualifiedOutFile) { // Get file name from last segment of Uri - string lastUriSegment = System.Net.WebUtility.UrlDecode(response.RequestMessage.RequestUri.Segments[^1]); + string? lastUriSegment = System.Net.WebUtility.UrlDecode(response.RequestMessage?.RequestUri?.Segments[^1]); return Directory.Exists(_qualifiedOutFile) ? Path.Join(_qualifiedOutFile, lastUriSegment) : _qualifiedOutFile; } @@ -52,7 +54,7 @@ internal static string GetOutFilePath(HttpResponseMessage response, string _qual internal static bool IsText(HttpResponseMessage response) { // ContentType may not exist in response header. - string contentType = ContentHelper.GetContentType(response); + string? contentType = ContentHelper.GetContentType(response); return ContentHelper.IsText(contentType); } } diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/StreamHelper.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/StreamHelper.cs index 369aa823d6b..28272a7daaf 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/StreamHelper.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/StreamHelper.cs @@ -383,7 +383,7 @@ private static string StreamToString(Stream stream, Encoding encoding) return result.ToString(); } - internal static string DecodeStream(Stream stream, string characterSet, out Encoding encoding) + internal static string DecodeStream(Stream stream, string? characterSet, out Encoding encoding) { bool isDefaultEncoding = !TryGetEncoding(characterSet, out encoding); @@ -419,11 +419,13 @@ internal static string DecodeStream(Stream stream, string characterSet, out Enco return content; } - internal static bool TryGetEncoding(string characterSet, out Encoding encoding) + internal static bool TryGetEncoding(string? characterSet, out Encoding encoding) { bool result = false; try { + ArgumentException.ThrowIfNullOrEmpty(characterSet); + encoding = Encoding.GetEncoding(characterSet); result = true; } From 64af5c8c456e27491932af325bcf73dc1cecbbc8 Mon Sep 17 00:00:00 2001 From: CarloToso <105941898+CarloToso@users.noreply.github.com> Date: Sat, 1 Apr 2023 00:33:04 +0200 Subject: [PATCH 34/37] remove ArgumentException.ThrowIfNullOrEmpty(characterSet); --- .../WebCmdlet/Common/BasicHtmlWebResponseObject.Common.cs | 2 +- .../WebCmdlet/Common/InvokeRestMethodCommand.Common.cs | 4 ++-- .../utility/WebCmdlet/CoreCLR/WebResponseHelper.CoreClr.cs | 2 +- .../commands/utility/WebCmdlet/StreamHelper.cs | 6 ++---- 4 files changed, 6 insertions(+), 8 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/BasicHtmlWebResponseObject.Common.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/BasicHtmlWebResponseObject.Common.cs index 8de62d07320..40b65694feb 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/BasicHtmlWebResponseObject.Common.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/BasicHtmlWebResponseObject.Common.cs @@ -151,7 +151,7 @@ protected void InitializeContent() if (ContentHelper.IsText(contentType)) { // Fill the Content buffer - string? characterSet = WebResponseHelper.GetCharacterSet(BaseResponse); + string characterSet = WebResponseHelper.GetCharacterSet(BaseResponse); Content = StreamHelper.DecodeStream(RawContentStream, characterSet, out Encoding encoding); Encoding = encoding; diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/InvokeRestMethodCommand.Common.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/InvokeRestMethodCommand.Common.cs index acb1fcdb840..e09c01fb025 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/InvokeRestMethodCommand.Common.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/InvokeRestMethodCommand.Common.cs @@ -96,9 +96,9 @@ internal override void ProcessResponse(HttpResponseMessage response) RestReturnType returnType = CheckReturnType(response); // Try to get the response encoding from the ContentType header. - string? charSet = WebResponseHelper.GetCharacterSet(response); + string characterSet = WebResponseHelper.GetCharacterSet(response); - string str = StreamHelper.DecodeStream(responseStream, charSet, out Encoding encoding); + string str = StreamHelper.DecodeStream(responseStream, characterSet, out Encoding encoding); object? obj = null; Exception? ex = null; 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 6e24629548b..cf040dd0d76 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 @@ -13,7 +13,7 @@ namespace Microsoft.PowerShell.Commands { internal static class WebResponseHelper { - internal static string? GetCharacterSet(HttpResponseMessage response) => response.Content.Headers.ContentType?.CharSet; + internal static string GetCharacterSet(HttpResponseMessage response) => response.Content.Headers.ContentType?.CharSet ?? string.Empty; internal static Dictionary> GetHeadersDictionary(HttpResponseMessage response) { diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/StreamHelper.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/StreamHelper.cs index 28272a7daaf..369aa823d6b 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/StreamHelper.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/StreamHelper.cs @@ -383,7 +383,7 @@ private static string StreamToString(Stream stream, Encoding encoding) return result.ToString(); } - internal static string DecodeStream(Stream stream, string? characterSet, out Encoding encoding) + internal static string DecodeStream(Stream stream, string characterSet, out Encoding encoding) { bool isDefaultEncoding = !TryGetEncoding(characterSet, out encoding); @@ -419,13 +419,11 @@ internal static string DecodeStream(Stream stream, string? characterSet, out Enc return content; } - internal static bool TryGetEncoding(string? characterSet, out Encoding encoding) + internal static bool TryGetEncoding(string characterSet, out Encoding encoding) { bool result = false; try { - ArgumentException.ThrowIfNullOrEmpty(characterSet); - encoding = Encoding.GetEncoding(characterSet); result = true; } From b33f28ab29e5ff840376e7d888ea378bfa2ab977 Mon Sep 17 00:00:00 2001 From: CarloToso <105941898+CarloToso@users.noreply.github.com> Date: Sat, 1 Apr 2023 14:07:23 +0200 Subject: [PATCH 35/37] Encoding.GetEncoding(characterSet!) --- .../WebCmdlet/Common/BasicHtmlWebResponseObject.Common.cs | 2 +- .../WebCmdlet/Common/InvokeRestMethodCommand.Common.cs | 2 +- .../utility/WebCmdlet/CoreCLR/WebResponseHelper.CoreClr.cs | 3 +-- .../commands/utility/WebCmdlet/StreamHelper.cs | 6 +++--- 4 files changed, 6 insertions(+), 7 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/BasicHtmlWebResponseObject.Common.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/BasicHtmlWebResponseObject.Common.cs index 40b65694feb..8de62d07320 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/BasicHtmlWebResponseObject.Common.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/BasicHtmlWebResponseObject.Common.cs @@ -151,7 +151,7 @@ protected void InitializeContent() if (ContentHelper.IsText(contentType)) { // Fill the Content buffer - string characterSet = WebResponseHelper.GetCharacterSet(BaseResponse); + string? characterSet = WebResponseHelper.GetCharacterSet(BaseResponse); Content = StreamHelper.DecodeStream(RawContentStream, characterSet, out Encoding encoding); Encoding = encoding; diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/InvokeRestMethodCommand.Common.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/InvokeRestMethodCommand.Common.cs index e09c01fb025..64edaa06fbb 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/InvokeRestMethodCommand.Common.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/InvokeRestMethodCommand.Common.cs @@ -96,7 +96,7 @@ internal override void ProcessResponse(HttpResponseMessage response) RestReturnType returnType = CheckReturnType(response); // Try to get the response encoding from the ContentType header. - string characterSet = WebResponseHelper.GetCharacterSet(response); + string? characterSet = WebResponseHelper.GetCharacterSet(response); string str = StreamHelper.DecodeStream(responseStream, characterSet, out Encoding encoding); 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 cf040dd0d76..d1238516ea2 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 @@ -13,8 +13,7 @@ namespace Microsoft.PowerShell.Commands { internal static class WebResponseHelper { - internal static string GetCharacterSet(HttpResponseMessage response) => response.Content.Headers.ContentType?.CharSet ?? string.Empty; - + internal static string? GetCharacterSet(HttpResponseMessage response) => response.Content.Headers.ContentType?.CharSet; internal static Dictionary> GetHeadersDictionary(HttpResponseMessage response) { var headers = new Dictionary>(StringComparer.OrdinalIgnoreCase); diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/StreamHelper.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/StreamHelper.cs index 369aa823d6b..8c490ee3009 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/StreamHelper.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/StreamHelper.cs @@ -383,7 +383,7 @@ private static string StreamToString(Stream stream, Encoding encoding) return result.ToString(); } - internal static string DecodeStream(Stream stream, string characterSet, out Encoding encoding) + internal static string DecodeStream(Stream stream, string? characterSet, out Encoding encoding) { bool isDefaultEncoding = !TryGetEncoding(characterSet, out encoding); @@ -419,12 +419,12 @@ internal static string DecodeStream(Stream stream, string characterSet, out Enco return content; } - internal static bool TryGetEncoding(string characterSet, out Encoding encoding) + internal static bool TryGetEncoding(string? characterSet, out Encoding encoding) { bool result = false; try { - encoding = Encoding.GetEncoding(characterSet); + encoding = Encoding.GetEncoding(characterSet!); result = true; } catch (ArgumentException) From 0070ddc61bbf66eb72df8cd171a37684cece9021 Mon Sep 17 00:00:00 2001 From: CarloToso <105941898+CarloToso@users.noreply.github.com> Date: Sat, 1 Apr 2023 14:13:40 +0200 Subject: [PATCH 36/37] fix --- .../utility/WebCmdlet/CoreCLR/WebResponseHelper.CoreClr.cs | 1 + 1 file changed, 1 insertion(+) 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 d1238516ea2..6e24629548b 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 @@ -14,6 +14,7 @@ namespace Microsoft.PowerShell.Commands internal static class WebResponseHelper { internal static string? GetCharacterSet(HttpResponseMessage response) => response.Content.Headers.ContentType?.CharSet; + internal static Dictionary> GetHeadersDictionary(HttpResponseMessage response) { var headers = new Dictionary>(StringComparer.OrdinalIgnoreCase); From 0546067a08f0ca76c6cab447aa72604412fad1e8 Mon Sep 17 00:00:00 2001 From: CarloToso <105941898+CarloToso@users.noreply.github.com> Date: Sun, 23 Apr 2023 11:33:16 +0200 Subject: [PATCH 37/37] fix codefactor --- .../WebCmdlet/Common/BasicHtmlWebResponseObject.Common.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/BasicHtmlWebResponseObject.Common.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/BasicHtmlWebResponseObject.Common.cs index bce46d98c89..75775428bc9 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/BasicHtmlWebResponseObject.Common.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/BasicHtmlWebResponseObject.Common.cs @@ -150,7 +150,6 @@ public WebCmdletElementCollection Images /// The cancellation token. [MemberNotNull(nameof(Content))] protected void InitializeContent(CancellationToken cancellationToken) - { string? contentType = ContentHelper.GetContentType(BaseResponse); if (ContentHelper.IsText(contentType))