diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs index a9c5934ecd3..4069a061c2b 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs @@ -859,8 +859,7 @@ internal virtual void PrepareSession() X509Certificate2Collection tbCollection = (X509Certificate2Collection)collection.Find(X509FindType.FindByThumbprint, CertificateThumbprint, false); if (tbCollection.Count == 0) { - CryptographicException ex = new(WebCmdletStrings.ThumbprintNotFound); - throw ex; + throw new CryptographicException(WebCmdletStrings.ThumbprintNotFound); } foreach (X509Certificate2 tbCert in tbCollection) @@ -910,13 +909,13 @@ internal virtual void PrepareSession() { foreach (string key in Headers.Keys) { - var value = Headers[key]; + object value = Headers[key]; // null is not valid value for header. // We silently ignore header if value is null. if (value is not null) { - // add the header value (or overwrite it if already present) + // Add the header value (or overwrite it if already present) WebSession.Headers[key] = value.ToString(); } } @@ -994,7 +993,7 @@ internal virtual HttpRequestMessage GetRequest(Uri uri) HttpMethod httpMethod = string.IsNullOrEmpty(CustomMethod) ? GetHttpMethod(Method) : new HttpMethod(CustomMethod); // Create the base WebRequest object - var request = new HttpRequestMessage(httpMethod, requestUri); + HttpRequestMessage request = new(httpMethod, requestUri); if (HttpVersion is not null) { @@ -1058,7 +1057,7 @@ internal virtual HttpRequestMessage GetRequest(Uri uri) if (TransferEncoding is not null) { request.Headers.TransferEncodingChunked = true; - var headerValue = new TransferCodingHeaderValue(TransferEncoding); + TransferCodingHeaderValue headerValue = new(TransferEncoding); if (!request.Headers.TransferEncoding.Contains(headerValue)) { request.Headers.TransferEncoding.Add(headerValue); @@ -1069,7 +1068,7 @@ internal virtual HttpRequestMessage GetRequest(Uri uri) // If not, create a Range to request the entire file. if (Resume.IsPresent) { - var fileInfo = new FileInfo(QualifiedOutFile); + FileInfo fileInfo = new(QualifiedOutFile); if (fileInfo.Exists) { request.Headers.Range = new RangeHeaderValue(fileInfo.Length, null); @@ -1105,7 +1104,7 @@ internal virtual void FillRequestStream(HttpRequestMessage request) if (Form is not null) { - var formData = new MultipartFormDataContent(); + MultipartFormDataContent formData = new(); foreach (DictionaryEntry formEntry in Form) { // AddMultipartContent will handle PSObject unwrapping, Object type determination and enumerateing top level IEnumerables. @@ -1117,13 +1116,8 @@ internal virtual void FillRequestStream(HttpRequestMessage request) else if (Body is not null) { // Coerce body into a usable form - object content = Body; - // Make sure we're using the base object of the body, not the PSObject wrapper - if (Body is PSObject psBody) - { - content = psBody.BaseObject; - } + object content = Body is PSObject psBody ? psBody.BaseObject : Body; switch (content) { @@ -1179,7 +1173,7 @@ internal virtual void FillRequestStream(HttpRequestMessage request) request.Content.Headers.Clear(); } - foreach (var entry in WebSession.ContentHeaders) + foreach (KeyValuePair entry in WebSession.ContentHeaders) { if (!string.IsNullOrWhiteSpace(entry.Value)) { @@ -1195,7 +1189,7 @@ internal virtual void FillRequestStream(HttpRequestMessage request) } catch (FormatException ex) { - var outerEx = new ValidationMetadataException(WebCmdletStrings.ContentTypeException, ex); + ValidationMetadataException outerEx = new(WebCmdletStrings.ContentTypeException, ex); ErrorRecord er = new(outerEx, "WebCmdletContentTypeException", ErrorCategory.InvalidArgument, ContentType); ThrowTerminatingError(er); } @@ -1379,20 +1373,11 @@ private static Uri CheckProtocol(Uri uri) { ArgumentNullException.ThrowIfNull(uri); - if (!uri.IsAbsoluteUri) - { - uri = new Uri("http://" + uri.OriginalString); - } - - return uri; - } - - private string QualifyFilePath(string path) - { - string resolvedFilePath = PathUtils.ResolveFilePath(filePath: path, command: this, isLiteralPath: true); - return resolvedFilePath; + return uri.IsAbsoluteUri ? uri : new Uri("http://" + uri.OriginalString); } + private string QualifyFilePath(string path) => PathUtils.ResolveFilePath(filePath: path, command: this, isLiteralPath: true); + private static string FormatDictionary(IDictionary content) { ArgumentNullException.ThrowIfNull(content); @@ -1409,11 +1394,7 @@ private static string FormatDictionary(IDictionary content) // URLEncode the key and value string encodedKey = WebUtility.UrlEncode(key); - string encodedValue = string.Empty; - if (value is not null) - { - encodedValue = WebUtility.UrlEncode(value.ToString()); - } + string encodedValue = value is null ? string.Empty : WebUtility.UrlEncode(value.ToString()); bodyBuilder.Append($"{encodedKey}={encodedValue}"); } @@ -1423,22 +1404,20 @@ private static string FormatDictionary(IDictionary content) private ErrorRecord GetValidationError(string msg, string errorId) { - var ex = new ValidationMetadataException(msg); - var error = new ErrorRecord(ex, errorId, ErrorCategory.InvalidArgument, this); - return error; + ValidationMetadataException ex = new(msg); + return new ErrorRecord(ex, errorId, ErrorCategory.InvalidArgument, this); } private ErrorRecord GetValidationError(string msg, string errorId, params object[] args) { msg = string.Format(CultureInfo.InvariantCulture, msg, args); - var ex = new ValidationMetadataException(msg); - var error = new ErrorRecord(ex, errorId, ErrorCategory.InvalidArgument, this); - return error; + ValidationMetadataException ex = new(msg); + return new ErrorRecord(ex, errorId, ErrorCategory.InvalidArgument, this); } private string GetBasicAuthorizationHeader() { - var password = new NetworkCredential(null, Credential.Password).Password; + string password = new NetworkCredential(string.Empty, Credential.Password).Password; string unencoded = string.Create(CultureInfo.InvariantCulture, $"{Credential.UserName}:{password}"); byte[] bytes = Encoding.UTF8.GetBytes(unencoded); return string.Create(CultureInfo.InvariantCulture, $"Basic {Convert.ToBase64String(bytes)}"); @@ -1464,14 +1443,14 @@ private void ProcessAuthentication() Diagnostics.Assert(false, string.Create(CultureInfo.InvariantCulture, $"Unrecognized Authentication value: {Authentication}")); } } - + /// - /// Sets the ContentLength property of the request and writes the specified content to the request's RequestStream. + /// Writes the specified content to the request's RequestStream. /// /// The WebRequest who's content is to be set. /// A byte array containing the content data. /// - /// Because this function sets the request's ContentLength property and writes content data into the request's stream, + /// Because this function writes content data into the request's stream, /// it should be called one time maximum on a given request. /// internal void SetRequestContent(HttpRequestMessage request, byte[] content) @@ -1479,17 +1458,16 @@ internal void SetRequestContent(HttpRequestMessage request, byte[] content) ArgumentNullException.ThrowIfNull(request); ArgumentNullException.ThrowIfNull(content); - ByteArrayContent byteArrayContent = new(content); - request.Content = byteArrayContent; + request.Content = new ByteArrayContent(content); } /// - /// Sets the ContentLength property of the request and writes the specified content to the request's RequestStream. + /// Writes the specified content to the request's RequestStream. /// /// The WebRequest who's content is to be set. /// A String object containing the content data. /// - /// Because this function sets the request's ContentLength property and writes content data into the request's stream, + /// Because this function writes content data into the request's stream, /// it should be called one time maximum on a given request. /// internal void SetRequestContent(HttpRequestMessage request, string content) @@ -1505,7 +1483,7 @@ internal void SetRequestContent(HttpRequestMessage request, string content) // would be used if Charset is not supplied in the Content-Type property. try { - var mediaTypeHeaderValue = MediaTypeHeaderValue.Parse(ContentType); + MediaTypeHeaderValue mediaTypeHeaderValue = MediaTypeHeaderValue.Parse(ContentType); if (!string.IsNullOrEmpty(mediaTypeHeaderValue.CharSet)) { encoding = Encoding.GetEncoding(mediaTypeHeaderValue.CharSet); @@ -1523,8 +1501,7 @@ internal void SetRequestContent(HttpRequestMessage request, string content) } byte[] bytes = StreamHelper.EncodeToBytes(content, encoding); - ByteArrayContent byteArrayContent = new(bytes); - request.Content = byteArrayContent; + request.Content = new ByteArrayContent(bytes); } internal void SetRequestContent(HttpRequestMessage request, XmlNode xmlNode) @@ -1534,9 +1511,8 @@ internal void SetRequestContent(HttpRequestMessage request, XmlNode xmlNode) byte[] bytes = null; XmlDocument doc = xmlNode as XmlDocument; - if (doc?.FirstChild is XmlDeclaration) + if (doc?.FirstChild is XmlDeclaration decl) { - XmlDeclaration decl = doc.FirstChild as XmlDeclaration; Encoding encoding = Encoding.GetEncoding(decl.Encoding); bytes = StreamHelper.EncodeToBytes(doc.OuterXml, encoding); } @@ -1545,18 +1521,16 @@ internal void SetRequestContent(HttpRequestMessage request, XmlNode xmlNode) bytes = StreamHelper.EncodeToBytes(xmlNode.OuterXml, encoding: null); } - ByteArrayContent byteArrayContent = new(bytes); - - request.Content = byteArrayContent; + request.Content = new ByteArrayContent(bytes); } /// - /// Sets the ContentLength property of the request and writes the specified content to the request's RequestStream. + /// Writes the specified content to the request's RequestStream. /// /// The WebRequest who's content is to be set. /// A Stream object containing the content data. /// - /// Because this function sets the request's ContentLength property and writes content data into the request's stream, + /// Because this function writes content data into the request's stream, /// it should be called one time maximum on a given request. /// internal void SetRequestContent(HttpRequestMessage request, Stream contentStream) @@ -1564,17 +1538,16 @@ internal void SetRequestContent(HttpRequestMessage request, Stream contentStream ArgumentNullException.ThrowIfNull(request); ArgumentNullException.ThrowIfNull(contentStream); - StreamContent streamContent = new(contentStream); - request.Content = streamContent; + request.Content = new StreamContent(contentStream); } /// - /// Sets the ContentLength property of the request and writes the specified content to the request's RequestStream. + /// Writes the specified content to the request's RequestStream. /// /// The WebRequest who's content is to be set. /// A MultipartFormDataContent object containing multipart/form-data content. /// - /// Because this function sets the request's ContentLength property and writes content data into the request's stream, + /// Because this function writes content data into the request's stream, /// it should be called one time maximum on a given request. /// internal void SetRequestContent(HttpRequestMessage request, MultipartFormDataContent multipartContent) @@ -1679,7 +1652,7 @@ private void AddMultipartContent(object fieldName, object fieldValue, MultipartF // Treat the value as a collection and enumerate it if enumeration is true if (enumerate && fieldValue is IEnumerable items) { - foreach (var item in items) + foreach (object item in items) { // Recurse, but do not enumerate the next level. IEnumerables will be treated as single values. AddMultipartContent(fieldName: fieldName, fieldValue: item, formData: formData, enumerate: false); @@ -1694,11 +1667,12 @@ private void AddMultipartContent(object fieldName, object fieldValue, MultipartF /// The Field Value to use for the private static StringContent GetMultipartStringContent(object fieldName, object fieldValue) { - var contentDisposition = new ContentDispositionHeaderValue("form-data"); + ContentDispositionHeaderValue contentDisposition = new("form-data"); + // .NET does not enclose field names in quotes, however, modern browsers and curl do. contentDisposition.Name = "\"" + LanguagePrimitives.ConvertTo(fieldName) + "\""; - var result = new StringContent(LanguagePrimitives.ConvertTo(fieldValue)); + StringContent result = new(LanguagePrimitives.ConvertTo(fieldValue)); result.Headers.ContentDisposition = contentDisposition; return result; @@ -1711,11 +1685,12 @@ private static StringContent GetMultipartStringContent(object fieldName, object /// The to use for the private static StreamContent GetMultipartStreamContent(object fieldName, Stream stream) { - var contentDisposition = new ContentDispositionHeaderValue("form-data"); + ContentDispositionHeaderValue contentDisposition = new("form-data"); + // .NET does not enclose field names in quotes, however, modern browsers and curl do. contentDisposition.Name = "\"" + LanguagePrimitives.ConvertTo(fieldName) + "\""; - var result = new StreamContent(stream); + StreamContent result = new(stream); result.Headers.ContentDisposition = contentDisposition; result.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream"); @@ -1729,7 +1704,7 @@ private static StreamContent GetMultipartStreamContent(object fieldName, Stream /// The file to use for the private static StreamContent GetMultipartFileContent(object fieldName, FileInfo file) { - var result = GetMultipartStreamContent(fieldName: fieldName, stream: new FileStream(file.FullName, FileMode.Open)); + StreamContent result = GetMultipartStreamContent(fieldName: fieldName, stream: new FileStream(file.FullName, FileMode.Open)); // .NET does not enclose field names in quotes, however, modern browsers and curl do. result.Headers.ContentDisposition.FileName = "\"" + file.Name + "\""; @@ -1754,9 +1729,8 @@ private static string FormatErrorMessage(string error, string contentType) OmitXmlDeclaration = true }; - if (doc.FirstChild is XmlDeclaration) + if (doc.FirstChild is XmlDeclaration decl) { - XmlDeclaration decl = doc.FirstChild as XmlDeclaration; settings.Encoding = Encoding.GetEncoding(decl.Encoding); }