System.Net.WebUtility.HtmlEncode C# (CSharp) Method

HtmlEncode() private static method

private static HtmlEncode ( string value, int index, StringBuilder output ) : void
value string
index int
output System.Text.StringBuilder
return void
        private static unsafe void HtmlEncode(string value, int index, StringBuilder output)
        {
            Debug.Assert(value != null);
            Debug.Assert(output != null);
            Debug.Assert(0 <= index && index <= value.Length, "0 <= index && index <= value.Length");

            int cch = value.Length - index;
            fixed (char* str = value)
            {
                char* pch = str;
                while (index-- > 0)
                {
                    output.Append(*pch++);
                }

                for (; cch > 0; cch--, pch++)
                {
                    char ch = *pch;
                    if (ch <= '>')
                    {
                        switch (ch)
                        {
                            case '<':
                                output.Append("&lt;");
                                break;
                            case '>':
                                output.Append("&gt;");
                                break;
                            case '"':
                                output.Append("&quot;");
                                break;
                            case '\'':
                                output.Append("&#39;");
                                break;
                            case '&':
                                output.Append("&amp;");
                                break;
                            default:
                                output.Append(ch);
                                break;
                        }
                    }
                    else
                    {
                        int valueToEncode = -1; // set to >= 0 if needs to be encoded

#if ENTITY_ENCODE_HIGH_ASCII_CHARS
                        if (ch >= 160 && ch < 256)
                        {
                            // The seemingly arbitrary 160 comes from RFC
                            valueToEncode = ch;
                        }
                        else
#endif // ENTITY_ENCODE_HIGH_ASCII_CHARS
                        if (Char.IsSurrogate(ch))
                        {
                            int scalarValue = GetNextUnicodeScalarValueFromUtf16Surrogate(ref pch, ref cch);
                            if (scalarValue >= UNICODE_PLANE01_START)
                            {
                                valueToEncode = scalarValue;
                            }
                            else
                            {
                                // Don't encode BMP characters (like U+FFFD) since they wouldn't have
                                // been encoded if explicitly present in the string anyway.
                                ch = (char)scalarValue;
                            }
                        }

                        if (valueToEncode >= 0)
                        {
                            // value needs to be encoded
                            output.Append("&#");
                            output.Append(valueToEncode.ToString(CultureInfo.InvariantCulture));
                            output.Append(';');
                        }
                        else
                        {
                            // write out the character directly
                            output.Append(ch);
                        }
                    }
                }
            }
        }

Same methods

WebUtility::HtmlEncode ( string value ) : string
WebUtility::HtmlEncode ( string value, TextWriter output ) : void

Usage Example

Beispiel #1
0
        internal void FinishInitialization()
        {
            string host = UserHostName;

            if (_version > HttpVersion.Version10 && (host == null || host.Length == 0))
            {
                _context.ErrorMessage = "Invalid host name";
                return;
            }

            string path;
            Uri    raw_uri = null;

            if (MaybeUri(_raw_url.ToLowerInvariant()) && Uri.TryCreate(_raw_url, UriKind.Absolute, out raw_uri))
            {
                path = raw_uri.PathAndQuery;
            }
            else
            {
                path = _raw_url;
            }

            if ((host == null || host.Length == 0))
            {
                host = UserHostAddress;
            }

            if (raw_uri != null)
            {
                host = raw_uri.Host;
            }

            int colon = host.IndexOf(':');

            if (colon >= 0)
            {
                host = host.Substring(0, colon);
            }

            string base_uri = String.Format("{0}://{1}:{2}",
                                            (IsSecureConnection) ? "https" : "http",
                                            host, LocalEndPoint.Port);

            if (!Uri.TryCreate(base_uri + path, UriKind.Absolute, out _url))
            {
                _context.ErrorMessage = WebUtility.HtmlEncode("Invalid url: " + base_uri + path);
                return;
            }

            CreateQueryString(_url.Query);

            _url = HttpListenerRequestUriBuilder.GetRequestUri(_raw_url, _url.Scheme,
                                                               _url.Authority, _url.LocalPath, _url.Query);

            if (_version >= HttpVersion.Version11)
            {
                string t_encoding = Headers["Transfer-Encoding"];
                _is_chunked = (t_encoding != null && string.Equals(t_encoding, "chunked", StringComparison.OrdinalIgnoreCase));
                // 'identity' is not valid!
                if (t_encoding != null && !_is_chunked)
                {
                    _context.Connection.SendError(null, 501);
                    return;
                }
            }

            if (!_is_chunked && !_cl_set)
            {
                if (string.Equals(_method, "POST", StringComparison.OrdinalIgnoreCase) ||
                    string.Equals(_method, "PUT", StringComparison.OrdinalIgnoreCase))
                {
                    _context.Connection.SendError(null, 411);
                    return;
                }
            }

            if (String.Compare(Headers["Expect"], "100-continue", StringComparison.OrdinalIgnoreCase) == 0)
            {
                HttpResponseStream output = _context.Connection.GetResponseStream();
                output.InternalWrite(s_100continue, 0, s_100continue.Length);
            }
        }