Skip to content

Commit

Permalink
Switch to UTF8 as the default encoding. (#1768)
Browse files Browse the repository at this point in the history
  • Loading branch information
peombwa authored Jan 26, 2023
1 parent 88fcc7f commit e6fe006
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 70 deletions.
36 changes: 2 additions & 34 deletions src/Authentication/Authentication/Helpers/ContentHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,16 @@
// Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License. See License in the project root for license information.
// ------------------------------------------------------------------------------

using Microsoft.Graph.PowerShell.Authentication.Models;
using Microsoft.Win32;
using System;
using System.Globalization;
using System.Net.Http;
using System.Text;

using Microsoft.Graph.PowerShell.Authentication.Models;
using Microsoft.Win32;

namespace Microsoft.Graph.PowerShell.Authentication.Helpers
{
internal static class ContentHelper
{
#region Constants

// default codepage encoding for web content. See RFC 2616.
private const string DefaultCodePage = "ISO-8859-1";

#endregion Constants

#region Fields
internal static RestReturnType CheckReturnType(this HttpResponseMessage response)
{
Expand Down Expand Up @@ -104,29 +95,6 @@ internal static string GetContentType(this HttpResponseMessage response)
return response.Content.Headers.ContentType?.MediaType;
}

internal static Encoding GetDefaultEncoding()
{
return GetEncodingOrDefault(null);
}

internal static Encoding GetEncodingOrDefault(string characterSet)
{
// get the name of the codepage to use for response content
var codepage = string.IsNullOrEmpty(characterSet) ? DefaultCodePage : characterSet;
Encoding encoding;
try
{
encoding = Encoding.GetEncoding(codepage);
}
catch (ArgumentException)
{
// 0, default code page
encoding = Encoding.GetEncoding(0);
}

return encoding;
}

internal static bool IsJson(string contentType)
{
contentType = GetContentTypeSignature(contentType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -754,7 +754,7 @@ private void WaitOurTurn()
private void WaitForCompletion()
{
// wait for the result (or cancellation!)
WaitHandle.WaitAny(new[] {CancellationToken.WaitHandle, _completed?.WaitHandle});
WaitHandle.WaitAny(new[] { CancellationToken.WaitHandle, _completed?.WaitHandle });

// let go of the semaphore
_semaphore?.Release();
Expand Down
33 changes: 3 additions & 30 deletions src/Authentication/Authentication/Helpers/StreamHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
// Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License. See License in the project root for license information.
// ------------------------------------------------------------------------------

using Microsoft.Graph.PowerShell.Authentication.Cmdlets;
using Microsoft.Graph.PowerShell.Authentication.Properties;
using System;
using System.IO;
using System.Management.Automation;
using System.Net.Http;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Graph.PowerShell.Authentication.Cmdlets;
using Microsoft.Graph.PowerShell.Authentication.Properties;

namespace Microsoft.Graph.PowerShell.Authentication.Helpers
{
Expand All @@ -31,7 +31,7 @@ internal static byte[] EncodeToBytes(this string str, Encoding encoding)
if (encoding == null)
{
// just use the default encoding if one wasn't provided
encoding = ContentHelper.GetDefaultEncoding();
encoding = Encoding.UTF8;
}

return encoding.GetBytes(str);
Expand All @@ -43,33 +43,6 @@ internal static Stream GetResponseStream(this HttpResponseMessage response)
return responseStream;
}

internal static bool TryGetEncoding(this string characterSet, out Encoding encoding)
{
var result = false;
try
{
encoding = Encoding.GetEncoding(characterSet);
result = true;
}
catch (ArgumentException)
{
encoding = null;
}

return result;
}

internal static string DecodeStream(this BufferingStreamReader responseStream, ref Encoding encoding)
{
if (encoding == null)
{
// Use the default encoding if one wasn't provided
encoding = ContentHelper.GetDefaultEncoding();
}
var content = responseStream.StreamToString(encoding);
return content;
}

internal static string StreamToString(this Stream stream, Encoding encoding)
{
using (var reader = new StreamReader(stream, encoding))
Expand Down
8 changes: 3 additions & 5 deletions src/Authentication/Authentication/Helpers/StringUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
// Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License. See License in the project root for license information.
// ------------------------------------------------------------------------------

using Microsoft.Graph.PowerShell.Authentication.Properties;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections;
using System.Collections.Generic;
Expand All @@ -12,11 +15,6 @@
using System.Text;
using System.Text.RegularExpressions;

using Microsoft.Graph.PowerShell.Authentication.Properties;

using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace Microsoft.Graph.PowerShell.Authentication.Helpers
{
public static class StringUtil
Expand Down

0 comments on commit e6fe006

Please sign in to comment.