Пример #1
0
        private void ReadMask(PipeStream stream)
        {
            IndexOfResult result = stream.IndexOf((int)this.Length);
            int           index  = 0;

            if (result.Start.ID == result.End.ID)
            {
                index = MarkBytes(result.Start.Bytes, result.StartPostion, result.EndPostion, index);
            }
            else
            {
                index = MarkBytes(result.Start.Bytes, result.StartPostion, result.Start.Length - 1, index);
                IMemoryBlock next = result.Start.NextMemory;
                while (next != null)
                {
                    if (next.ID == result.End.ID)
                    {
                        index = MarkBytes(next.Bytes, 0, result.EndPostion, index);
                        break;
                    }
                    else
                    {
                        index = MarkBytes(next.Bytes, 0, next.Length - 1, index);
                    }
                    next = result.Start.NextMemory;
                }
            }
        }
Пример #2
0
        public bool Read(PipeStream stream, Cookies cookies)
        {
            IndexOfResult index = stream.IndexOf(HeaderType.LINE_BYTES);

            while (index.End != null)
            {
                if (index.Length == 2)
                {
                    stream.ReadFree(2);
                    return(true);
                }
                else
                {
                    ReadOnlySpan <Char> line = HttpParse.ReadCharLine(index);
                    stream.ReadFree(index.Length);
                    if (line[0] == 'C' && line[5] == 'e' && line[1] == 'o' && line[2] == 'o' && line[3] == 'k' && line[4] == 'i')
                    {
                        HttpParse.AnalyzeCookie(line.Slice(8, line.Length - 8), cookies);
                    }
                    else
                    {
                        Tuple <string, string> result = HttpParse.AnalyzeHeader(line);
                        Add(result.Item1, result.Item2);
                    }
                }
                index = stream.IndexOf(HeaderType.LINE_BYTES);
            }
            return(false);
        }
Пример #3
0
        /** 查找一组字符串中,任意一个出现的位置(详细) */
        private static IndexOfResult indexOfEx(string str, string[] arr, int fromIndex)
        {
            IndexOfResult result = new IndexOfResult();

            int temp;

            for (int i = 0; i < arr.Length; i++)
            {
                temp = str.IndexOf(arr[i], fromIndex);

                if (temp != -1)
                {
                    if (result.index == -1)
                    {
                        result.index = temp;
                        //					result.contentInex=i;
                        result.content = arr[i];
                    }
                    else
                    {
                        if (temp < result.index)
                        {
                            result.index = temp;
                            //						result.contentInex=i;
                            result.content = arr[i];
                        }
                    }
                }
            }

            return(result);
        }
Пример #4
0
        public static ReadOnlySpan <char> ReadCharLine(IndexOfResult result)
        {
            int offset = 0;

            char[]       data   = HttpParse.GetCharBuffer();
            IMemoryBlock memory = result.Start;

            for (int i = result.StartPostion; i < memory.Length; i++)
            {
                data[offset] = (char)result.Start.Bytes[i];
                offset++;
                if (offset == result.Length)
                {
                    break;
                }
            }
            if (offset < result.Length)
            {
Next:
                memory = result.Start.NextMemory;

                int count;
                if (memory.ID == result.End.ID)
                {
                    count = result.EndPostion + 1;
                }
                else
                {
                    count = memory.Length;
                }
                for (int i = 0; i < count; i++)
                {
                    data[offset] = (char)memory.Bytes[i];
                    offset++;
                    if (offset == result.Length)
                    {
                        break;
                    }
                }
                if (offset < result.Length)
                {
                    goto Next;
                }
            }
            return(new ReadOnlySpan <char>(data, 0, result.Length - 2));
        }
Пример #5
0
        /** 找到另一个对应字符的位置(次数计数的) */
        public static int getAnotherIndex(string str, string left, string right, int fromIndex)
        {
            int re = -1;

            int start = str.IndexOf(left, fromIndex);

            int lastIndex = start + left.Length;

            int num = 1;

            string[] arr = new string[] { left, right };

            while (true)
            {
                IndexOfResult temp = indexOfEx(str, arr, lastIndex);

                if (temp.index != -1)
                {
                    if (temp.content == left)
                    {
                        ++num;
                    }
                    else if (temp.content == right)
                    {
                        --num;
                    }

                    lastIndex = temp.index + temp.content.Length;

                    if (num == 0)
                    {
                        re = temp.index;
                        break;
                    }
                }
                else
                {
                    break;
                }
            }

            return(re);
        }
Пример #6
0
 private void LoadMethod(PipeStream stream)
 {
     if (mState == LoadedState.None)
     {
         IndexOfResult index = stream.IndexOf(HeaderType.LINE_BYTES);
         if (index.End != null)
         {
             ReadOnlySpan <Char> line = HttpParse.ReadCharLine(index);
             stream.ReadFree(index.Length);
             Tuple <string, string, string> result = HttpParse.AnalyzeRequestLine(line);
             Method      = result.Item1;
             Url         = result.Item2;
             BaseUrl     = HttpParse.GetBaseUrlToLower(Url);
             Ext         = HttpParse.GetBaseUrlExt(BaseUrl);
             HttpVersion = result.Item3;
             HttpParse.AnalyzeQueryString(Url, mQueryString);
             mState = LoadedState.Method;
         }
     }
 }
Пример #7
0
 public void Load(IDataContext dataContext, HttpRequest request)
 {
     if (!string.IsNullOrEmpty(mStartBoundary))
     {
         var stream = request.Stream;
         if (stream.ReadLine() == mStartBoundary)
         {
             string name = null, filename = null, contentType = null;
             while (stream.TryReadWith(HeaderTypeFactory.LINE_BYTES, out string headerLine))
             {
                 if (string.IsNullOrEmpty(headerLine))
                 {
                     System.IO.MemoryStream memoryStream = new System.IO.MemoryStream();
                     while (true)
                     {
                         IndexOfResult indexOf = stream.IndexOf(HeaderTypeFactory.LINE_BYTES);
                         if (indexOf.End == null)
                         {
                             break;
                         }
                         if (indexOf.Length == mEndBoundary.Length + 2)
                         {
                             byte[] buffer = HttpParse.GetByteBuffer();
                             stream.Read(buffer, 0, indexOf.Length);
                             string line = Encoding.UTF8.GetString(buffer, 0, indexOf.Length - 2);
                             if (line == mEndBoundary)
                             {
                                 CreateParameter(dataContext, request, name, filename, contentType, memoryStream);
                                 name = filename = contentType = null;
                                 return;
                             }
                             else
                             {
                                 memoryStream.Write(buffer, 0, indexOf.Length - 2);
                             }
                         }
                         else if (indexOf.Length == mStartBoundary.Length + 2)
                         {
                             byte[] buffer = HttpParse.GetByteBuffer();
                             stream.Read(buffer, 0, indexOf.Length);
                             string line = Encoding.UTF8.GetString(buffer, 0, indexOf.Length - 2);
                             if (line == mStartBoundary)
                             {
                                 CreateParameter(dataContext, request, name, filename, contentType, memoryStream);
                                 name = filename = contentType = null;
                                 break;
                             }
                             else
                             {
                                 memoryStream.Write(buffer, 0, indexOf.Length - 2);
                             }
                         }
                         else
                         {
                             IMemoryBlock block = indexOf.Start;
                             while (block != null)
                             {
                                 int offset = 0;
                                 int length = block.Length;
                                 if (block.ID == indexOf.Start.ID)
                                 {
                                     offset = indexOf.StartPostion;
                                 }
                                 if (block.ID == indexOf.End.ID)
                                 {
                                     length = indexOf.EndPostion + 1;
                                 }
                                 memoryStream.Write(block.Data, offset, length - offset);
                                 if (block.ID == indexOf.End.ID)
                                 {
                                     stream.ReadFree(indexOf.Length);
                                     break;
                                 }
                                 else
                                 {
                                     block = block.NextMemory;
                                 }
                             }
                         }
                     }
                 }
                 else
                 {
                     var result = HttpParse.AnalyzeContentHeader(headerLine);
                     if (result.Name == ContentDisposition)
                     {
                         if (result.Properties != null)
                         {
                             for (int i = 0; i < result.Properties.Length; i++)
                             {
                                 if (result.Properties[i].Name == "name")
                                 {
                                     name = result.Properties[i].Value;
                                 }
                                 else if (result.Properties[i].Name == "filename")
                                 {
                                     filename = result.Properties[i].Value;
                                 }
                             }
                         }
                     }
                     else if (result.Name == ContentType)
                     {
                         contentType = result.Value;
                     }
                 }
             }
         }
     }
 }