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

Add span-based Uri {Try}{Un}EscapeDataString overloads #98074

Merged
merged 4 commits into from
Feb 13, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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,30 +28,61 @@ private static byte[] GetContentByteArray(IEnumerable<KeyValuePair<string?, stri
ArgumentNullException.ThrowIfNull(nameValueCollection);

// Encode and concatenate data
StringBuilder builder = new StringBuilder();
var builder = new ValueStringBuilder(stackalloc char[256]);

foreach (KeyValuePair<string?, string?> pair in nameValueCollection)
{
if (builder.Length > 0)
{
builder.Append('&');
}

builder.Append(Encode(pair.Key));
Encode(ref builder, pair.Key);
builder.Append('=');
builder.Append(Encode(pair.Value));
Encode(ref builder, pair.Value);
}

return HttpRuleParser.DefaultHttpEncoding.GetBytes(builder.ToString());
// We know the encoded length because the input is all ASCII.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How do we know the input is ASCII?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The input is a bunch of concatenated Uri.EscapeDataString results, which are always ASCII, and the encoding is Latin1.
I clarified it in the comment now.

MihaZupan marked this conversation as resolved.
Show resolved Hide resolved
byte[] bytes = new byte[builder.Length];
HttpRuleParser.DefaultHttpEncoding.GetBytes(builder.AsSpan(), bytes);
builder.Dispose();
return bytes;
}

private static string Encode(string? data)
private static void Encode(ref ValueStringBuilder builder, string? data)
{
if (string.IsNullOrEmpty(data))
if (!string.IsNullOrEmpty(data))
{
return string.Empty;
int charsWritten;
while (!Uri.TryEscapeDataString(data, builder.RawChars.Slice(builder.Length), out charsWritten))
{
builder.EnsureCapacity(builder.Capacity + 1);
}

// Escape spaces as '+'.
if (data.Contains(' '))
{
ReadOnlySpan<char> escapedChars = builder.RawChars.Slice(builder.Length, charsWritten);

while (true)
{
int indexOfEscapedSpace = escapedChars.IndexOf("%20", StringComparison.Ordinal);
if (indexOfEscapedSpace < 0)
{
builder.Append(escapedChars);
break;
}

builder.Append(escapedChars.Slice(0, indexOfEscapedSpace));
builder.Append('+');
escapedChars = escapedChars.Slice(indexOfEscapedSpace + 3); // Skip "%20"
}
}
else
{
builder.Length += charsWritten;
}
}
// Escape spaces as '+'.
return Uri.EscapeDataString(data).Replace("%20", "+");
}

protected override Task SerializeToStreamAsync(Stream stream, TransportContext? context, CancellationToken cancellationToken) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,16 +170,9 @@ private HttpEnvironmentProxy(Uri? httpProxy, Uri? httpsProxy, string? bypassList
int separatorIndex = value.LastIndexOf('@');
if (separatorIndex != -1)
{
string auth = value.Substring(0, separatorIndex);

// The User and password may or may not be URL encoded.
// Curl seems to accept both. To match that,
// we do opportunistic decode and we use original string if it fails.
try
{
auth = Uri.UnescapeDataString(auth);
}
catch { };
// Curl seems to accept both. To match that, we also decode the value.
string auth = Uri.UnescapeDataString(value.AsSpan(0, separatorIndex));

value = value.Substring(separatorIndex + 1);
separatorIndex = auth.IndexOf(':');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -495,17 +495,17 @@ internal FtpWebRequest(Uri uri)
NetworkCredential? networkCredential = null;
_uri = uri;
_methodInfo = FtpMethodInfo.GetMethodInfo(WebRequestMethods.Ftp.DownloadFile);
if (!string.IsNullOrEmpty(_uri.UserInfo))

if (_uri.UserInfo is { Length: > 0 } userInfo)
{
string userInfo = _uri.UserInfo;
string username = userInfo;
string password = "";
int index = userInfo.IndexOf(':');
if (index != -1)
{
username = Uri.UnescapeDataString(userInfo.Substring(0, index));
username = Uri.UnescapeDataString(userInfo.AsSpan(0, index));
index++; // skip ':'
password = Uri.UnescapeDataString(userInfo.Substring(index));
password = Uri.UnescapeDataString(userInfo.AsSpan(index));
}
networkCredential = new NetworkCredential(username, password);
}
Expand Down
Loading