Esempio n. 1
0
        private void _appendEscapedString(TextWriter writer, string primStr)
        {
            int escapeForwardSlash = builderPolicy.EscapeForwardSlash;

            if (!string.ReferenceEquals(primStr, null) && primStr.Length > 0)
            {
                char[] primChars = primStr.ToCharArray();
                char   prevEc    = (char)0;
                foreach (char ec in primChars)
                {
                    if (Symbols.IsEscapedChar(ec))
                    {
                        if (prevEc == '<' && ec == '/')
                        {
                            if (escapeForwardSlash >= 0)
                            {
                                writer.Write("\\/");
                            }
                            else
                            {
                                writer.Write("/");
                            }
                            // } else if(prevEc == '\\' && ec == '\\') {
                            //     // Already escaped... ????
                            //     // Skip ???
                        }
                        else
                        {
                            string str = Symbols.GetEscapedCharString(ec, escapeForwardSlash > 0 ? true : false);
                            if (!string.ReferenceEquals(str, null))
                            {
                                writer.Write(str);
                            }
                            else
                            {
                                // ???
                                writer.Write(ec);
                            }
                        }
                    }
                    else if (CharUtil.IsISOControl(ec))
                    {
                        char[] uc = UnicodeUtil.GetUnicodeHexCodeFromChar(ec);
                        writer.Write(uc);
                    }
                    else
                    {
                        writer.Write(ec);
                    }
                    prevEc = ec;
                }
            }
        }
Esempio n. 2
0
        private static bool IsWhiteSpaceOrISOControl(byte c)
        {
            switch (c)
            {
            case HttpConstants.HorizontalSpace:
            case HttpConstants.HorizontalTab:
            case HttpConstants.CarriageReturn:
                return(true);

            default:
                return(CharUtil.IsISOControl(c));
            }
        }
Esempio n. 3
0
        static int GetChunkSize(AsciiString hex)
        {
            hex = hex.Trim();
            for (int i = hex.Offset; i < hex.Count; i++)
            {
                byte c = hex.Array[i];
                if (c == ';' || IsWhiteSpace(c) || CharUtil.IsISOControl(c))
                {
                    hex = (AsciiString)hex.SubSequence(0, i);
                    break;
                }
            }

            return(hex.ParseInt(16));
        }
Esempio n. 4
0
        private string _escapeString(string primStr)
        {
            StringBuilder sb = new StringBuilder();

            if (!string.ReferenceEquals(primStr, null) && primStr.Length > 0)
            {
                char[] primChars = primStr.ToCharArray();
                char   prevEc    = (char)0;
                foreach (char ec in primChars)
                {
                    if (Symbols.IsEscapedChar(ec))
                    {
                        if (prevEc == '<' && ec == '/')
                        {
                            sb.Append("\\/");
                        }
                        else
                        {
                            string str = Symbols.GetEscapedCharString(ec, false);
                            if (!string.ReferenceEquals(str, null))
                            {
                                sb.Append(str);
                            }
                            else
                            {
                                // ???
                                sb.Append(ec);
                            }
                        }
                    }
                    else if (CharUtil.IsISOControl(ec))
                    {
                        char[] uc = UnicodeUtil.GetUnicodeHexCodeFromChar(ec);
                        sb.Append(uc);
                    }
                    else
                    {
                        sb.Append(ec);
                    }
                    prevEc = ec;
                }
            }
            return(sb.ToString());
        }
Esempio n. 5
0
        static bool SkipControlCharacters(IByteBuffer buffer)
        {
            bool skiped = false;
            int  wIdx   = buffer.WriterIndex;
            int  rIdx   = buffer.ReaderIndex;

            while (wIdx > rIdx)
            {
                byte c = buffer.GetByte(rIdx++);
                if (!CharUtil.IsISOControl(c) && !IsWhiteSpace(c))
                {
                    rIdx--;
                    skiped = true;
                    break;
                }
            }
            buffer.SetReaderIndex(rIdx);
            return(skiped);
        }
Esempio n. 6
0
 // private static readonly int escapeForwardSlash = -1;
 private async void _AppendEscapedString(TextWriter writer, string primStr)
 {
     if (primStr != null && primStr.Length > 0)
     {
         char[] primChars = primStr.ToCharArray();
         // char prevEc = 0;
         foreach (char ec in primChars)
         {
             if (Symbols.IsEscapedChar(ec))
             {
                 // if(prevEc == '<' && ec == '/') {
                 //     // if(escapeForwardSlash >= 0) {
                 //     //     await writer.WriteAsync("\\/");
                 //     // } else {
                 //         await writer.WriteAsync("/");
                 //     // }
                 // } else {
                 // string str = Symbols.GetEscapedCharString(ec, escapeForwardSlash > 0 ? true : false);
                 string str = Symbols.GetEscapedCharString(ec, false);
                 if (str != null)
                 {
                     await writer.WriteAsync(str);
                 }
                 else
                 {
                     // ???
                     await writer.WriteAsync(ec);
                 }
                 // }
             }
             else if (CharUtil.IsISOControl(ec))
             {
                 char[] uc = UnicodeUtil.GetUnicodeHexCodeFromChar(ec);
                 await writer.WriteAsync(uc);
             }
             else
             {
                 await writer.WriteAsync(ec);
             }
             // prevEc = ec;
         }
     }
 }
Esempio n. 7
0
        // Creates a new HTTP method with the specified name.  You will not need to
        // create a new method unless you are implementing a protocol derived from
        // HTTP, such as
        // http://en.wikipedia.org/wiki/Real_Time_Streaming_Protocol and
        // http://en.wikipedia.org/wiki/Internet_Content_Adaptation_Protocol
        //
        public HttpMethod(string name)
        {
            Contract.Requires(name != null);

            name = name.Trim();
            if (string.IsNullOrEmpty(name))
            {
                throw new ArgumentException(nameof(name));
            }

            for (int i = 0; i < name.Length; i++)
            {
                char c = name[i];
                if (CharUtil.IsISOControl(c) || char.IsWhiteSpace(c))
                {
                    throw new ArgumentException($"Invalid character '{c}' in {nameof(name)}");
                }
            }

            this.name = AsciiString.Cached(name);
        }
        HttpVersion(string protocolName, int majorVersion, int minorVersion, bool keepAliveDefault, bool bytes)
        {
            if (protocolName == null)
            {
                throw new ArgumentException(nameof(protocolName));
            }

            protocolName = protocolName.Trim().ToUpper();
            if (string.IsNullOrEmpty(protocolName))
            {
                throw new ArgumentException("empty protocolName");
            }

            // ReSharper disable once ForCanBeConvertedToForeach
            for (int i = 0; i < protocolName.Length; i++)
            {
                char c = protocolName[i];
                if (CharUtil.IsISOControl(c) || char.IsWhiteSpace(c))
                {
                    throw new ArgumentException($"invalid character {c} in protocolName");
                }
            }

            if (majorVersion < 0)
            {
                throw new ArgumentException("negative majorVersion");
            }
            if (minorVersion < 0)
            {
                throw new ArgumentException("negative minorVersion");
            }

            this.protocolName     = protocolName;
            this.majorVersion     = majorVersion;
            this.minorVersion     = minorVersion;
            this.text             = new AsciiString(protocolName + '/' + majorVersion + '.' + minorVersion);
            this.keepAliveDefault = keepAliveDefault;

            this.bytes = bytes ? this.text.Array : null;
        }
Esempio n. 9
0
        /// <summary>
        /// Creates a new HTTP method with the specified name.  You will not need to
        /// create a new method unless you are implementing a protocol derived from
        /// HTTP, such as
        /// http://en.wikipedia.org/wiki/Real_Time_Streaming_Protocol and
        /// http://en.wikipedia.org/wiki/Internet_Content_Adaptation_Protocol
        /// </summary>
        /// <param name="name"></param>
        public HttpMethod(string name)
        {
            if (name is null)
            {
                ThrowHelper.ThrowArgumentNullException(ExceptionArgument.name);
            }

            name = name.Trim();
            if (string.IsNullOrEmpty(name))
            {
                ThrowHelper.ThrowArgumentNullException(ExceptionArgument.name);
            }

            for (int i = 0; i < name.Length; i++)
            {
                char c = name[i];
                if (CharUtil.IsISOControl(c) || char.IsWhiteSpace(c))
                {
                    ThrowHelper.ThrowArgumentException_InvalidMethodName(c, name);
                }
            }

            this.name = AsciiString.Cached(name);
        }