Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Delete duplicative code in WebClient #75896

Merged
merged 1 commit into from
Sep 20, 2022
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 @@ -28,6 +28,7 @@
<Reference Include="System.Collections.Specialized" />
<Reference Include="System.ComponentModel.EventBasedAsync" />
<Reference Include="System.ComponentModel.Primitives" />
<Reference Include="System.Memory" />
<Reference Include="System.Net.Primitives" />
<Reference Include="System.Net.Requests" />
<Reference Include="System.Net.WebHeaderCollection" />
Expand Down
116 changes: 9 additions & 107 deletions src/libraries/System.Net.WebClient/src/System/Net/WebClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1076,24 +1076,6 @@ private async void UploadBitsAsync(
}
}

private static bool ByteArrayHasPrefix(byte[] prefix, byte[] byteArray)
{
if (prefix == null || byteArray == null || prefix.Length > byteArray.Length)
{
return false;
}

for (int i = 0; i < prefix.Length; i++)
{
if (prefix[i] != byteArray[i])
{
return false;
}
}

return true;
}

private static readonly char[] s_parseContentTypeSeparators = new char[] { ';', '=', ' ' };
private static readonly Encoding[] s_knownEncodings = { Encoding.UTF8, Encoding.UTF32, Encoding.Unicode, Encoding.BigEndianUnicode };

Expand Down Expand Up @@ -1147,13 +1129,12 @@ private string GetStringUsingEncoding(WebRequest request, byte[] data)
if (enc == null)
{
// UTF32 must be tested before Unicode because it's BOM is the same but longer.
Encoding[] encodings = s_knownEncodings;
for (int i = 0; i < encodings.Length; i++)
foreach (Encoding encoding in s_knownEncodings)
{
byte[] preamble = encodings[i].GetPreamble();
if (ByteArrayHasPrefix(preamble, data))
ReadOnlySpan<byte> preamble = encoding.Preamble;
if (data.AsSpan().StartsWith(preamble))
{
enc = encodings[i];
enc = encoding;
bomLengthInData = preamble.Length;
break;
}
Expand All @@ -1166,8 +1147,8 @@ private string GetStringUsingEncoding(WebRequest request, byte[] data)
// Calculate BOM length based on encoding guess. Then check for it in the data.
if (bomLengthInData == -1)
{
byte[] preamble = enc.GetPreamble();
bomLengthInData = ByteArrayHasPrefix(preamble, data) ? preamble.Length : 0;
ReadOnlySpan<byte> preamble = enc.Preamble;
bomLengthInData = data.AsSpan().StartsWith(preamble) ? preamble.Length : 0;
}

// Convert byte array to string stripping off any BOM before calling Format().
Expand All @@ -1187,88 +1168,9 @@ private string MapToDefaultMethod(Uri address)
}

[return: NotNullIfNotNull(nameof(str))]
private static string? UrlEncode(string? str)
{
if (str == null)
return null;
byte[] bytes = Encoding.UTF8.GetBytes(str);
return Encoding.ASCII.GetString(UrlEncodeBytesToBytesInternal(bytes, 0, bytes.Length, false));
}

private static byte[] UrlEncodeBytesToBytesInternal(byte[] bytes, int offset, int count, bool alwaysCreateReturnValue)
{
int cSpaces = 0;
int cUnsafe = 0;

// Count them first.
for (int i = 0; i < count; i++)
{
char ch = (char)bytes[offset + i];

if (ch == ' ')
{
cSpaces++;
}
else if (!IsSafe(ch))
{
cUnsafe++;
}
}

// If nothing to expand.
if (!alwaysCreateReturnValue && cSpaces == 0 && cUnsafe == 0)
return bytes;

// Expand not 'safe' characters into %XX, spaces to +.
byte[] expandedBytes = new byte[count + cUnsafe * 2];
int pos = 0;

for (int i = 0; i < count; i++)
{
byte b = bytes[offset + i];
char ch = (char)b;

if (IsSafe(ch))
{
expandedBytes[pos++] = b;
}
else if (ch == ' ')
{
expandedBytes[pos++] = (byte)'+';
}
else
{
expandedBytes[pos++] = (byte)'%';
expandedBytes[pos++] = (byte)HexConverter.ToCharLower(b >> 4);
expandedBytes[pos++] = (byte)HexConverter.ToCharLower(b);
}
}

return expandedBytes;
}

private static bool IsSafe(char ch)
{
if (char.IsAsciiLetterOrDigit(ch))
{
return true;
}

switch (ch)
{
case '-':
case '_':
case '.':
case '!':
case '*':
case '\'':
case '(':
case ')':
return true;
}

return false;
}
private static string? UrlEncode(string? str) =>
str is null ? null :
WebUtility.UrlEncode(str);

private void InvokeOperationCompleted(AsyncOperation asyncOp, SendOrPostCallback callback, AsyncCompletedEventArgs eventArgs)
{
Expand Down