Example #1
0
        // Encode a string using RFC 5987 encoding
        // encoding'lang'PercentEncodedSpecials
        private string Encode5987(StringSegment input)
        {
            var builder = new StringBuilder("UTF-8\'\'");

            for (int i = 0; i < input.Length; i++)
            {
                var c = input[i];
                // attr-char = ALPHA / DIGIT / "!" / "#" / "$" / "&" / "+" / "-" / "." / "^" / "_" / "`" / "|" / "~"
                //      ; token except ( "*" / "'" / "%" )
                if (c > 0x7F) // Encodes as multiple utf-8 bytes
                {
                    var bytes = Encoding.UTF8.GetBytes(c.ToString());
                    foreach (byte b in bytes)
                    {
                        HexEscape(builder, (char)b);
                    }
                }
                else if (!HttpRuleParser.IsTokenChar(c) || c == '*' || c == '\'' || c == '%')
                {
                    // ASCII - Only one encoded byte
                    HexEscape(builder, c);
                }
                else
                {
                    builder.Append(c);
                }
            }
            return(builder.ToString());
        }