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 8eaa95c224a..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 @@ -1,8 +1,11 @@ // Copyright (c) Microsoft Corporation. // Licensed under the MIT License. +#nullable enable + using System.Collections.Generic; using System.Diagnostics; +using System.Diagnostics.CodeAnalysis; using System.IO; using System.Management.Automation; using System.Net.Http; @@ -33,7 +36,7 @@ public BasicHtmlWebResponseObject(HttpResponseMessage response, CancellationToke /// The response. /// The content stream associated with the response. /// Cancellation token. - public BasicHtmlWebResponseObject(HttpResponseMessage response, Stream contentStream, CancellationToken cancellationToken) : base(response, contentStream, cancellationToken) + public BasicHtmlWebResponseObject(HttpResponseMessage response, Stream? contentStream, CancellationToken cancellationToken) : base(response, contentStream, cancellationToken) { InitializeContent(cancellationToken); InitializeRawContent(response); @@ -60,9 +63,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 . @@ -87,7 +90,7 @@ public WebCmdletElementCollection InputFields } } - private WebCmdletElementCollection _links; + private WebCmdletElementCollection? _links; /// /// Gets the HTML a link elements parsed from . @@ -112,7 +115,7 @@ public WebCmdletElementCollection Links } } - private WebCmdletElementCollection _images; + private WebCmdletElementCollection? _images; /// /// Gets the HTML img elements parsed from . @@ -145,13 +148,14 @@ public WebCmdletElementCollection Images /// Reads the response content from the web response. /// /// The cancellation token. + [MemberNotNull(nameof(Content))] protected void InitializeContent(CancellationToken cancellationToken) { - string contentType = ContentHelper.GetContentType(BaseResponse); + string? contentType = ContentHelper.GetContentType(BaseResponse); 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, cancellationToken); Encoding = encoding; @@ -204,7 +208,7 @@ private static void ParseAttributes(string outerHtml, PSObject elementObject) 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 bf127967ffe..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 @@ -1,7 +1,10 @@ // Copyright (c) Microsoft Corporation. // Licensed under the MIT License. +#nullable enable + using System; +using System.Diagnostics.CodeAnalysis; using System.Management.Automation; using System.Net.Http; using System.Net.Http.Headers; @@ -16,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; @@ -35,7 +38,7 @@ internal static StringBuilder GetRawContentHeader(HttpResponseMessage response) HttpHeaders[] headerCollections = { response.Headers, - response.Content?.Headers + response.Content.Headers }; foreach (var headerCollection in headerCollections) @@ -59,7 +62,7 @@ internal static StringBuilder GetRawContentHeader(HttpResponseMessage response) return raw; } - internal static bool IsJson(string contentType) + internal static bool IsJson([NotNullWhen(true)] string? contentType) { if (string.IsNullOrEmpty(contentType)) { @@ -80,7 +83,7 @@ internal static bool IsJson(string contentType) return isJson; } - internal static bool IsText(string contentType) + internal static bool IsText([NotNullWhen(true)] string? contentType) { if (string.IsNullOrEmpty(contentType)) { @@ -96,19 +99,18 @@ internal static bool IsText(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; - if (extension != null) + if (contentTypeKey.GetValue("Extension") is string extension) { - 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"; } } } @@ -119,7 +121,7 @@ internal static bool IsText(string contentType) return isText; } - internal static bool IsXml(string contentType) + internal static bool IsXml([NotNullWhen(true)] string? contentType) { if (string.IsNullOrEmpty(contentType)) { 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 3fe6e2a91ad..a0ec902fb96 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,7 +1,10 @@ // Copyright (c) Microsoft Corporation. // Licensed under the MIT License. +#nullable enable + using System; +using System.Diagnostics.CodeAnalysis; using System.IO; using System.Management.Automation; using System.Net.Http; @@ -56,13 +59,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 @@ -95,12 +98,12 @@ 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, _cancelToken.Token); + string str = StreamHelper.DecodeStream(responseStream, characterSet, out Encoding encoding, _cancelToken.Token); - object obj = null; - Exception ex = null; + object? obj = null; + Exception? ex = null; string encodingVerboseName; try @@ -167,7 +170,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; @@ -223,7 +226,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 @@ -262,7 +265,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 { @@ -284,13 +287,12 @@ 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, [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) { @@ -340,7 +342,7 @@ public enum RestReturnType /// /// Json return type. /// - [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly")] + [SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly")] Json, /// 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 8bfdeed2da4..5b89a2352fe 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,6 +1,8 @@ // Copyright (c) Microsoft Corporation. // Licensed under the MIT License. +#nullable enable + using System; using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; @@ -26,14 +28,14 @@ public class WebResponseObject /// /// 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. @@ -41,7 +43,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 . @@ -56,7 +58,7 @@ public class WebResponseObject /// /// Gets the RelationLink property. /// - public Dictionary RelationLink { get; internal set; } + public Dictionary? RelationLink { get; internal set; } /// /// Gets the response status code. @@ -77,8 +79,7 @@ public class WebResponseObject /// /// The Http response. /// The cancellation token. - public WebResponseObject(HttpResponseMessage response, CancellationToken cancellationToken) : this(response, null, cancellationToken) - { } + public WebResponseObject(HttpResponseMessage response, CancellationToken cancellationToken) : this(response, null, cancellationToken) { } /// /// Initializes a new instance of the class @@ -87,7 +88,7 @@ public WebResponseObject(HttpResponseMessage response, CancellationToken cancell /// Http response. /// The http content stream. /// The cancellation token. - public WebResponseObject(HttpResponseMessage response, Stream contentStream, CancellationToken cancellationToken) + public WebResponseObject(HttpResponseMessage response, Stream? contentStream, CancellationToken cancellationToken) { SetResponse(response, contentStream, cancellationToken); InitializeContent(); @@ -111,9 +112,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(); @@ -125,26 +126,23 @@ private static bool IsPrintable(char c) => char.IsLetterOrDigit(c) || char.IsSymbol(c) || char.IsWhiteSpace(c); - private void SetResponse(HttpResponseMessage response, Stream contentStream, CancellationToken cancellationToken) + [MemberNotNull(nameof(RawContentStream))] + [MemberNotNull(nameof(BaseResponse))] + private void SetResponse(HttpResponseMessage response, Stream? contentStream, CancellationToken cancellationToken) { ArgumentNullException.ThrowIfNull(response); BaseResponse = response; - MemoryStream ms = contentStream as MemoryStream; - if (ms is not null) + if (contentStream is MemoryStream ms) { RawContentStream = ms; } else { - Stream st = contentStream; - if (contentStream is null) - { - st = StreamHelper.GetResponseStream(response, cancellationToken); - } + Stream st = contentStream ?? StreamHelper.GetResponseStream(response, cancellationToken); - long contentLength = response.Content.Headers.ContentLength.Value; + long contentLength = response.Content.Headers.ContentLength.GetValueOrDefault(); if (contentLength <= 0) { contentLength = StreamHelper.DefaultReadBuffer; @@ -164,7 +162,12 @@ private void SetResponse(HttpResponseMessage response, Stream contentStream, Can /// The string representation of this web response. public sealed override string ToString() { - char[] stringContent = System.Text.Encoding.ASCII.GetChars(Content); + 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])) 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; 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 19ebc294c10..1e66157ef0c 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; @@ -37,7 +39,7 @@ internal override void ProcessResponse(HttpResponseMessage response) Stream responseStream = StreamHelper.GetResponseStream(response, _cancelToken.Token); 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, @@ -48,7 +50,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/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; 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/FormObject.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/FormObject.cs index b496a120329..5ac4adfbb64 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? _)) { 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 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)) 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}"); 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 bec01ee8a1d..6d217cdc909 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; @@ -25,7 +27,7 @@ internal class WebResponseContentMemoryStream : MemoryStream private readonly long? _contentLength; private readonly Stream _originalStreamToProxy; - private readonly Cmdlet _ownerCmdlet; + private readonly Cmdlet? _ownerCmdlet; private readonly CancellationToken _cancellationToken; private bool _isInitialized = false; @@ -40,7 +42,7 @@ internal class WebResponseContentMemoryStream : MemoryStream /// Owner cmdlet if any. /// Expected download size in Bytes. /// Cancellation token. - internal WebResponseContentMemoryStream(Stream stream, int initialCapacity, Cmdlet cmdlet, long? contentLength, CancellationToken cancellationToken) : base(initialCapacity) + internal WebResponseContentMemoryStream(Stream stream, int initialCapacity, Cmdlet? cmdlet, long? contentLength, CancellationToken cancellationToken) : base(initialCapacity) { this._contentLength = contentLength; _originalStreamToProxy = stream; @@ -361,7 +363,7 @@ private static string StreamToString(Stream stream, Encoding encoding, Cancellat 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); // The conversion produced the number of characters indicated by charsUsed. Write that number @@ -386,7 +388,7 @@ private static string StreamToString(Stream stream, Encoding encoding, Cancellat return result.ToString(); } - internal static string DecodeStream(Stream stream, string characterSet, out Encoding encoding, CancellationToken cancellationToken) + internal static string DecodeStream(Stream stream, string? characterSet, out Encoding encoding, CancellationToken cancellationToken) { bool isDefaultEncoding = !TryGetEncoding(characterSet, out encoding); @@ -422,12 +424,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) 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) { 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 { ///