Beispiel #1
0
        /// <summary>
        /// Returns the value properly formatted and encoded for use in a URI in a HTTP header.
        /// Any Unicode is converted to punycode. IPv6 addresses will have brackets added if they are missing.
        /// </summary>
        /// <returns>The <see cref="HostString"/> value formated for use in a URI or HTTP header.</returns>
        public string ToUriComponent()
        {
            if (string.IsNullOrEmpty(_value))
            {
                return(string.Empty);
            }

            int i;

            for (i = 0; i < _value.Length; ++i)
            {
                if (!HostStringHelper.IsSafeHostStringChar(_value[i]))
                {
                    break;
                }
            }

            if (i != _value.Length)
            {
                GetParts(_value, out var host, out var port);

                var mapping = new IdnMapping();
                var encoded = mapping.GetAscii(host.Buffer, host.Offset, host.Length);

                return(StringSegment.IsNullOrEmpty(port)
                    ? encoded
                    : string.Concat(encoded, ":", port.ToString()));
            }

            return(_value);
        }
Beispiel #2
0
        /// <summary>
        /// Returns the value properly formatted and encoded for use in a URI in a HTTP header.
        /// Any Unicode is converted to punycode. IPv6 addresses will have brackets added if they are missing.
        /// </summary>
        /// <returns></returns>
        public string ToUriComponent()
        {
            if (string.IsNullOrEmpty(_value))
            {
                return(string.Empty);
            }

            int i;

            for (i = 0; i < _value.Length; ++i)
            {
                if (!HostStringHelper.IsSafeHostStringChar(_value[i]))
                {
                    break;
                }
            }

            if (i != _value.Length)
            {
                string host, port;
                GetParts(out host, out port);

                var mapping = new IdnMapping();
                host = mapping.GetAscii(host);

                return(string.IsNullOrEmpty(port)
                    ? host
                    : string.Concat(host, ":", port));
            }

            return(_value);
        }