// Encode a string using RFC 5987 encoding. // encoding'lang'PercentEncodedSpecials private string Encode5987(string input) { StringBuilder builder = new StringBuilder("utf-8\'\'"); foreach (char c in input) { // attr-char = ALPHA / DIGIT / "!" / "#" / "$" / "&" / "+" / "-" / "." / "^" / "_" / "`" / "|" / "~" // ; token except ( "*" / "'" / "%" ) if (c > 0x7F) // Encodes as multiple utf-8 bytes { byte[] bytes = Encoding.UTF8.GetBytes(c.ToString()); foreach (byte b in bytes) { builder.Append(UriShim.HexEscape((char)b)); } } else if (!HttpRuleParser.IsTokenChar(c) || c == '*' || c == '\'' || c == '%') { // ASCII - Only one encoded byte. builder.Append(UriShim.HexEscape(c)); } else { builder.Append(c); } } return(builder.ToString()); }
internal static string Encode5987(string input) { StringBuilder stringBuilder = StringBuilderCache.Acquire(16); byte[] numArray = ArrayPool <byte> .Shared.Rent(Encoding.UTF8.GetMaxByteCount(input.Length)); int bytes = Encoding.UTF8.GetBytes(input, 0, input.Length, numArray, 0); stringBuilder.Append("utf-8''"); for (int index = 0; index < bytes; ++index) { byte c = numArray[index]; if (c > (byte)127) { HeaderUtilities.AddHexEscaped(c, stringBuilder); } else if (!HttpRuleParser.IsTokenChar((char)c) || c == (byte)42 || (c == (byte)39 || c == (byte)37)) { HeaderUtilities.AddHexEscaped(c, stringBuilder); } else { stringBuilder.Append((char)c); } } Array.Clear((Array)numArray, 0, bytes); ArrayPool <byte> .Shared.Return(numArray, false); return(StringBuilderCache.GetStringAndRelease(stringBuilder)); }
public void IsTokenChar_IterateArrayWithAllValidTokenChars_AllCharsConsideredValid() { for (int i = 0; i < ValidTokenChars.Length; i++) { // TODO: This test should be a [Theory] Assert.True(HttpRuleParser.IsTokenChar(ValidTokenChars[i])); } }
public void IsTokenChar_IterateArrayWithAllInvalidTokenChars_AllCharsConsideredInvalid() { // All octets not in 'validTokenChars' must be considered invalid characters. for (int i = 0; i < 256; i++) { if (ValidTokenChars.IndexOf((char)i) == -1) { // TODO: This test should be a [Theory] Assert.False(HttpRuleParser.IsTokenChar((char)i)); } } }
// Encode a string using RFC 5987 encoding. // encoding'lang'PercentEncodedSpecials internal static string Encode5987(string input) { // Encode a string using RFC 5987 encoding. // encoding'lang'PercentEncodedSpecials var builder = new ValueStringBuilder(stackalloc char[256]); byte[] utf8bytes = ArrayPool <byte> .Shared.Rent(Encoding.UTF8.GetMaxByteCount(input.Length)); int utf8length = Encoding.UTF8.GetBytes(input, 0, input.Length, utf8bytes, 0); builder.Append("utf-8\'\'"); for (int i = 0; i < utf8length; i++) { byte utf8byte = utf8bytes[i]; // attr-char = ALPHA / DIGIT / "!" / "#" / "$" / "&" / "+" / "-" / "." / "^" / "_" / "`" / "|" / "~" // ; token except ( "*" / "'" / "%" ) if (utf8byte > 0x7F) // Encodes as multiple utf-8 bytes { AddHexEscaped(utf8byte, ref builder); } else if (!HttpRuleParser.IsTokenChar((char)utf8byte) || utf8byte == '*' || utf8byte == '\'' || utf8byte == '%') { // ASCII - Only one encoded byte. AddHexEscaped(utf8byte, ref builder); } else { builder.Append((char)utf8byte); } } Array.Clear(utf8bytes, 0, utf8length); ArrayPool <byte> .Shared.Return(utf8bytes); return(builder.ToString()); }
internal static bool IsInputEncoded5987(string input, out string output) { // Encode a string using RFC 5987 encoding. // encoding'lang'PercentEncodedSpecials bool wasEncoded = false; StringBuilder builder = StringBuilderCache.Acquire(); builder.Append("utf-8\'\'"); foreach (char c in input) { // attr-char = ALPHA / DIGIT / "!" / "#" / "$" / "&" / "+" / "-" / "." / "^" / "_" / "`" / "|" / "~" // ; token except ( "*" / "'" / "%" ) if (c > 0x7F) // Encodes as multiple utf-8 bytes { byte[] bytes = Encoding.UTF8.GetBytes(c.ToString()); foreach (byte b in bytes) { AddHexEscaped((char)b, builder); wasEncoded = true; } } else if (!HttpRuleParser.IsTokenChar(c) || c == '*' || c == '\'' || c == '%') { // ASCII - Only one encoded byte. AddHexEscaped(c, builder); wasEncoded = true; } else { builder.Append(c); } } output = StringBuilderCache.GetStringAndRelease(builder); return(wasEncoded); }
public void IsTokenChar_InvalidTokenChars_ConsideredInvalid(char token) { Assert.False(HttpRuleParser.IsTokenChar(token)); }
public void IsTokenChar_ValidTokenChars_ConsideredValid(char token) { Assert.True(HttpRuleParser.IsTokenChar(token)); }