Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,15 @@ public class BasicHtmlWebResponseObject : WebResponseObject
/// Initializes a new instance of the <see cref="BasicHtmlWebResponseObject"/> class.
/// </summary>
/// <param name="response"></param>
public BasicHtmlWebResponseObject(HttpResponseMessage response)
: this(response, null)
{ }
public BasicHtmlWebResponseObject(HttpResponseMessage response) : this(response, null) { }

/// <summary>
/// Initializes a new instance of the <see cref="BasicHtmlWebResponseObject"/> class
/// with the specified <paramref name="contentStream"/>.
/// </summary>
/// <param name="response"></param>
/// <param name="contentStream"></param>
public BasicHtmlWebResponseObject(HttpResponseMessage response, Stream contentStream)
: base(response, contentStream)
public BasicHtmlWebResponseObject(HttpResponseMessage response, Stream contentStream) : base(response, contentStream)
{
EnsureHtmlParser();
InitializeContent();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,7 @@ internal static string GetContentType(HttpResponseMessage response)
return response.Content.Headers.ContentType?.MediaType;
}

internal static Encoding GetDefaultEncoding()
{
Encoding encoding = Encoding.UTF8;
return encoding;
}
internal static Encoding GetDefaultEncoding() => Encoding.UTF8;

internal static StringBuilder GetRawContentHeader(HttpResponseMessage response)
{
Expand Down Expand Up @@ -95,28 +91,32 @@ internal static bool IsXml(string contentType)
private static bool CheckIsJson(string contentType)
{
if (string.IsNullOrEmpty(contentType))
{
return false;
}

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

// add in these other "javascript" related types that
// Add in these other "javascript" related types that
// sometimes get sent down as the mime type for JSON content
isJson |= contentType.Equals("text/json", StringComparison.OrdinalIgnoreCase)
|| contentType.Equals("application/x-javascript", StringComparison.OrdinalIgnoreCase)
|| contentType.Equals("text/x-javascript", StringComparison.OrdinalIgnoreCase)
|| contentType.Equals("application/javascript", StringComparison.OrdinalIgnoreCase)
|| contentType.Equals("text/javascript", StringComparison.OrdinalIgnoreCase);

return (isJson);
return isJson;
}

private static bool CheckIsText(string contentType)
{
if (string.IsNullOrEmpty(contentType))
{
return false;
}

// any text, xml or json types are text
// Any text, xml or json types are text
bool isText = contentType.StartsWith("text/", StringComparison.OrdinalIgnoreCase)
|| CheckIsXml(contentType)
|| CheckIsJson(contentType);
Expand Down Expand Up @@ -145,30 +145,34 @@ private static bool CheckIsText(string contentType)
}
}

return (isText);
return isText;
}

private static bool CheckIsXml(string contentType)
{
if (string.IsNullOrEmpty(contentType))
{
return false;
}

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

isXml |= contentType.EndsWith("+xml", StringComparison.OrdinalIgnoreCase);
return (isXml);
return isXml;
}

private static string GetContentTypeSignature(string contentType)
{
if (string.IsNullOrEmpty(contentType))
{
return null;
}

string sig = contentType.Split(';', 2)[0].ToUpperInvariant();
return (sig);
return sig;
}

#endregion Private Helper Methods
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ internal override void ProcessResponse(HttpResponseMessage response)
}

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

bool convertSuccess = false;

Expand Down Expand Up @@ -331,12 +331,7 @@ private static bool TryConvertToJson(string json, out object obj, ref Exception
converted = true;
}
}
catch (ArgumentException ex)
{
exRef = ex;
obj = null;
}
catch (InvalidOperationException ex)
catch (Exception ex) when (ex is ArgumentException || ex is InvalidOperationException)
{
exRef = ex;
obj = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -776,14 +776,14 @@ private ErrorRecord GetValidationError(string msg, string errorId, params object
private string GetBasicAuthorizationHeader()
{
var password = new NetworkCredential(null, Credential.Password).Password;
string unencoded = string.Format("{0}:{1}", Credential.UserName, password);
string unencoded = string.Format($"{Credential.UserName}:{password}");
byte[] bytes = Encoding.UTF8.GetBytes(unencoded);
return string.Format("Basic {0}", Convert.ToBase64String(bytes));
return string.Format($"Basic {Convert.ToBase64String(bytes)}");
}

private string GetBearerAuthorizationHeader()
{
return string.Format("Bearer {0}", new NetworkCredential(string.Empty, Token).Password);
return string.Format($"Bearer {new NetworkCredential(string.Empty, Token).Password}");
}

private void ProcessAuthentication()
Expand All @@ -798,7 +798,7 @@ private void ProcessAuthentication()
}
else
{
Diagnostics.Assert(false, string.Format("Unrecognized Authentication value: {0}", Authentication));
Diagnostics.Assert(false, string.Format($"Unrecognized Authentication value: {Authentication}"));
}
}

Expand Down Expand Up @@ -1170,10 +1170,7 @@ internal virtual void FillRequestStream(HttpRequestMessage request)
}
catch (UnauthorizedAccessException)
{
string msg = string.Format(
CultureInfo.InvariantCulture,
WebCmdletStrings.AccessDenied,
_originalFilePath);
string msg = string.Format(CultureInfo.InvariantCulture, WebCmdletStrings.AccessDenied, _originalFilePath);

throw new UnauthorizedAccessException(msg);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,27 +20,18 @@ internal WebProxy(Uri address)

public ICredentials Credentials
{
get { return _credentials; }
get => _credentials;

set { _credentials = value; }
set => _credentials = value;
}

internal bool BypassProxyOnLocal
{
get; set;
}
internal bool BypassProxyOnLocal { get; set; }

internal bool UseDefaultCredentials
{
get
{
return _credentials == CredentialCache.DefaultCredentials;
}
get => _credentials == CredentialCache.DefaultCredentials;

set
{
_credentials = value ? CredentialCache.DefaultCredentials : null;
}
set => _credentials = value ? CredentialCache.DefaultCredentials : null;
}

public Uri GetProxy(Uri destination)
Expand All @@ -55,9 +46,6 @@ public Uri GetProxy(Uri destination)
return _proxyAddress;
}

public bool IsBypassed(Uri host)
{
return host.IsLoopback;
}
public bool IsBypassed(Uri host) => host.IsLoopback;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,7 @@ namespace Microsoft.PowerShell.Commands
{
internal static class WebResponseHelper
{
internal static string GetCharacterSet(HttpResponseMessage response)
{
string characterSet = response.Content.Headers.ContentType.CharSet;
return characterSet;
}
internal static string GetCharacterSet(HttpResponseMessage response) => response.Content.Headers.ContentType.CharSet;

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

internal static string GetProtocol(HttpResponseMessage response)
{
string protocol = string.Format(CultureInfo.InvariantCulture,
"HTTP/{0}", response.Version);
return protocol;
}
internal static string GetProtocol(HttpResponseMessage response) => string.Format(CultureInfo.InvariantCulture, $"HTTP/{response.Version}");

internal static int GetStatusCode(HttpResponseMessage response)
{
int statusCode = (int)response.StatusCode;
return statusCode;
}
internal static int GetStatusCode(HttpResponseMessage response) => (int)response.StatusCode;

internal static string GetStatusDescription(HttpResponseMessage response)
{
string statusDescription = response.StatusCode.ToString();
return statusDescription;
}
internal static string GetStatusDescription(HttpResponseMessage response) => response.StatusCode.ToString();

internal static bool IsText(HttpResponseMessage response)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ internal static WebResponseObject GetResponseObject(HttpResponseMessage response
output = new WebResponseObject(response, responseStream);
}

return (output);
return output;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public FormObject this[string key]
}
}

return (form);
return form;
}
}
}
Expand Down
Loading