Skip to content

Commit 2df06f2

Browse files
authored
Small cleanup common code of webcmdlets (PowerShell#18946)
1 parent 9ffa9a2 commit 2df06f2

File tree

10 files changed

+57
-191
lines changed

10 files changed

+57
-191
lines changed

src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/BasicHtmlWebResponseObject.Common.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,18 +33,15 @@ public class BasicHtmlWebResponseObject : WebResponseObject
3333
/// Initializes a new instance of the <see cref="BasicHtmlWebResponseObject"/> class.
3434
/// </summary>
3535
/// <param name="response"></param>
36-
public BasicHtmlWebResponseObject(HttpResponseMessage response)
37-
: this(response, null)
38-
{ }
36+
public BasicHtmlWebResponseObject(HttpResponseMessage response) : this(response, null) { }
3937

4038
/// <summary>
4139
/// Initializes a new instance of the <see cref="BasicHtmlWebResponseObject"/> class
4240
/// with the specified <paramref name="contentStream"/>.
4341
/// </summary>
4442
/// <param name="response"></param>
4543
/// <param name="contentStream"></param>
46-
public BasicHtmlWebResponseObject(HttpResponseMessage response, Stream contentStream)
47-
: base(response, contentStream)
44+
public BasicHtmlWebResponseObject(HttpResponseMessage response, Stream contentStream) : base(response, contentStream)
4845
{
4946
EnsureHtmlParser();
5047
InitializeContent();

src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/ContentHelper.Common.cs

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,7 @@ internal static string GetContentType(HttpResponseMessage response)
2121
return response.Content.Headers.ContentType?.MediaType;
2222
}
2323

24-
internal static Encoding GetDefaultEncoding()
25-
{
26-
Encoding encoding = Encoding.UTF8;
27-
return encoding;
28-
}
24+
internal static Encoding GetDefaultEncoding() => Encoding.UTF8;
2925

3026
internal static StringBuilder GetRawContentHeader(HttpResponseMessage response)
3127
{
@@ -95,28 +91,32 @@ internal static bool IsXml(string contentType)
9591
private static bool CheckIsJson(string contentType)
9692
{
9793
if (string.IsNullOrEmpty(contentType))
94+
{
9895
return false;
96+
}
9997

100-
// the correct type for JSON content, as specified in RFC 4627
98+
// The correct type for JSON content, as specified in RFC 4627
10199
bool isJson = contentType.Equals("application/json", StringComparison.OrdinalIgnoreCase);
102100

103-
// add in these other "javascript" related types that
101+
// Add in these other "javascript" related types that
104102
// sometimes get sent down as the mime type for JSON content
105103
isJson |= contentType.Equals("text/json", StringComparison.OrdinalIgnoreCase)
106104
|| contentType.Equals("application/x-javascript", StringComparison.OrdinalIgnoreCase)
107105
|| contentType.Equals("text/x-javascript", StringComparison.OrdinalIgnoreCase)
108106
|| contentType.Equals("application/javascript", StringComparison.OrdinalIgnoreCase)
109107
|| contentType.Equals("text/javascript", StringComparison.OrdinalIgnoreCase);
110108

111-
return (isJson);
109+
return isJson;
112110
}
113111

114112
private static bool CheckIsText(string contentType)
115113
{
116114
if (string.IsNullOrEmpty(contentType))
115+
{
117116
return false;
117+
}
118118

119-
// any text, xml or json types are text
119+
// Any text, xml or json types are text
120120
bool isText = contentType.StartsWith("text/", StringComparison.OrdinalIgnoreCase)
121121
|| CheckIsXml(contentType)
122122
|| CheckIsJson(contentType);
@@ -145,30 +145,34 @@ private static bool CheckIsText(string contentType)
145145
}
146146
}
147147

148-
return (isText);
148+
return isText;
149149
}
150150

151151
private static bool CheckIsXml(string contentType)
152152
{
153153
if (string.IsNullOrEmpty(contentType))
154+
{
154155
return false;
156+
}
155157

156158
// RFC 3023: Media types with the suffix "+xml" are XML
157159
bool isXml = (contentType.Equals("application/xml", StringComparison.OrdinalIgnoreCase)
158160
|| contentType.Equals("application/xml-external-parsed-entity", StringComparison.OrdinalIgnoreCase)
159161
|| contentType.Equals("application/xml-dtd", StringComparison.OrdinalIgnoreCase));
160162

161163
isXml |= contentType.EndsWith("+xml", StringComparison.OrdinalIgnoreCase);
162-
return (isXml);
164+
return isXml;
163165
}
164166

165167
private static string GetContentTypeSignature(string contentType)
166168
{
167169
if (string.IsNullOrEmpty(contentType))
170+
{
168171
return null;
172+
}
169173

170174
string sig = contentType.Split(';', 2)[0].ToUpperInvariant();
171-
return (sig);
175+
return sig;
172176
}
173177

174178
#endregion Private Helper Methods

src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/InvokeRestMethodCommand.Common.cs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ internal override void ProcessResponse(HttpResponseMessage response)
142142
}
143143

144144
// NOTE: Tests use this verbose output to verify the encoding.
145-
WriteVerbose(string.Format(System.Globalization.CultureInfo.InvariantCulture, "Content encoding: {0}", encodingVerboseName));
145+
WriteVerbose(string.Format(System.Globalization.CultureInfo.InvariantCulture, $"Content encoding: {encodingVerboseName}"));
146146

147147
bool convertSuccess = false;
148148

@@ -331,12 +331,7 @@ private static bool TryConvertToJson(string json, out object obj, ref Exception
331331
converted = true;
332332
}
333333
}
334-
catch (ArgumentException ex)
335-
{
336-
exRef = ex;
337-
obj = null;
338-
}
339-
catch (InvalidOperationException ex)
334+
catch (Exception ex) when (ex is ArgumentException || ex is InvalidOperationException)
340335
{
341336
exRef = ex;
342337
obj = null;

src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -776,14 +776,14 @@ private ErrorRecord GetValidationError(string msg, string errorId, params object
776776
private string GetBasicAuthorizationHeader()
777777
{
778778
var password = new NetworkCredential(null, Credential.Password).Password;
779-
string unencoded = string.Format("{0}:{1}", Credential.UserName, password);
779+
string unencoded = string.Format($"{Credential.UserName}:{password}");
780780
byte[] bytes = Encoding.UTF8.GetBytes(unencoded);
781-
return string.Format("Basic {0}", Convert.ToBase64String(bytes));
781+
return string.Format($"Basic {Convert.ToBase64String(bytes)}");
782782
}
783783

784784
private string GetBearerAuthorizationHeader()
785785
{
786-
return string.Format("Bearer {0}", new NetworkCredential(string.Empty, Token).Password);
786+
return string.Format($"Bearer {new NetworkCredential(string.Empty, Token).Password}");
787787
}
788788

789789
private void ProcessAuthentication()
@@ -798,7 +798,7 @@ private void ProcessAuthentication()
798798
}
799799
else
800800
{
801-
Diagnostics.Assert(false, string.Format("Unrecognized Authentication value: {0}", Authentication));
801+
Diagnostics.Assert(false, string.Format($"Unrecognized Authentication value: {Authentication}"));
802802
}
803803
}
804804

@@ -1170,10 +1170,7 @@ internal virtual void FillRequestStream(HttpRequestMessage request)
11701170
}
11711171
catch (UnauthorizedAccessException)
11721172
{
1173-
string msg = string.Format(
1174-
CultureInfo.InvariantCulture,
1175-
WebCmdletStrings.AccessDenied,
1176-
_originalFilePath);
1173+
string msg = string.Format(CultureInfo.InvariantCulture, WebCmdletStrings.AccessDenied, _originalFilePath);
11771174

11781175
throw new UnauthorizedAccessException(msg);
11791176
}

src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/CoreCLR/WebProxy.cs

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,27 +20,18 @@ internal WebProxy(Uri address)
2020

2121
public ICredentials Credentials
2222
{
23-
get { return _credentials; }
23+
get => _credentials;
2424

25-
set { _credentials = value; }
25+
set => _credentials = value;
2626
}
2727

28-
internal bool BypassProxyOnLocal
29-
{
30-
get; set;
31-
}
28+
internal bool BypassProxyOnLocal { get; set; }
3229

3330
internal bool UseDefaultCredentials
3431
{
35-
get
36-
{
37-
return _credentials == CredentialCache.DefaultCredentials;
38-
}
32+
get => _credentials == CredentialCache.DefaultCredentials;
3933

40-
set
41-
{
42-
_credentials = value ? CredentialCache.DefaultCredentials : null;
43-
}
34+
set => _credentials = value ? CredentialCache.DefaultCredentials : null;
4435
}
4536

4637
public Uri GetProxy(Uri destination)
@@ -55,9 +46,6 @@ public Uri GetProxy(Uri destination)
5546
return _proxyAddress;
5647
}
5748

58-
public bool IsBypassed(Uri host)
59-
{
60-
return host.IsLoopback;
61-
}
49+
public bool IsBypassed(Uri host) => host.IsLoopback;
6250
}
6351
}

src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/CoreCLR/WebResponseHelper.CoreClr.cs

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,7 @@ namespace Microsoft.PowerShell.Commands
1010
{
1111
internal static class WebResponseHelper
1212
{
13-
internal static string GetCharacterSet(HttpResponseMessage response)
14-
{
15-
string characterSet = response.Content.Headers.ContentType.CharSet;
16-
return characterSet;
17-
}
13+
internal static string GetCharacterSet(HttpResponseMessage response) => response.Content.Headers.ContentType.CharSet;
1814

1915
internal static Dictionary<string, IEnumerable<string>> GetHeadersDictionary(HttpResponseMessage response)
2016
{
@@ -27,7 +23,7 @@ internal static Dictionary<string, IEnumerable<string>> GetHeadersDictionary(Htt
2723
// HttpResponseMessage.Content.Headers. The remaining headers are in HttpResponseMessage.Headers.
2824
// The keys in both should be unique with no duplicates between them.
2925
// Added for backwards compatibility with PowerShell 5.1 and earlier.
30-
if (response.Content != null)
26+
if (response.Content is not null)
3127
{
3228
foreach (var entry in response.Content.Headers)
3329
{
@@ -38,24 +34,11 @@ internal static Dictionary<string, IEnumerable<string>> GetHeadersDictionary(Htt
3834
return headers;
3935
}
4036

41-
internal static string GetProtocol(HttpResponseMessage response)
42-
{
43-
string protocol = string.Format(CultureInfo.InvariantCulture,
44-
"HTTP/{0}", response.Version);
45-
return protocol;
46-
}
37+
internal static string GetProtocol(HttpResponseMessage response) => string.Format(CultureInfo.InvariantCulture, $"HTTP/{response.Version}");
4738

48-
internal static int GetStatusCode(HttpResponseMessage response)
49-
{
50-
int statusCode = (int)response.StatusCode;
51-
return statusCode;
52-
}
39+
internal static int GetStatusCode(HttpResponseMessage response) => (int)response.StatusCode;
5340

54-
internal static string GetStatusDescription(HttpResponseMessage response)
55-
{
56-
string statusDescription = response.StatusCode.ToString();
57-
return statusDescription;
58-
}
41+
internal static string GetStatusDescription(HttpResponseMessage response) => response.StatusCode.ToString();
5942

6043
internal static bool IsText(HttpResponseMessage response)
6144
{

src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/CoreCLR/WebResponseObjectFactory.CoreClr.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ internal static WebResponseObject GetResponseObject(HttpResponseMessage response
2121
output = new WebResponseObject(response, responseStream);
2222
}
2323

24-
return (output);
24+
return output;
2525
}
2626
}
2727
}

src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/FormObjectCollection.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public FormObject this[string key]
3030
}
3131
}
3232

33-
return (form);
33+
return form;
3434
}
3535
}
3636
}

0 commit comments

Comments
 (0)