Beispiel #1
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.Data[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.Data[i];
                    offset++;
                    if (offset == result.Length)
                    {
                        break;
                    }
                }
                if (offset < result.Length)
                {
                    goto Next;
                }
            }
            return(new ReadOnlySpan <char>(data, 0, result.Length - 2));
        }
Beispiel #2
0
        public LoadedState Load(PipeStream stream)
        {
            string line;

            if (mState == LoadedState.None)
            {
                if (stream.TryReadWith(HeaderTypeFactory.LINE_BYTES, out line))
                {
                    HttpParse.AnalyzeResponseLine(line.AsSpan(), this);
                    mState = LoadedState.Method;
                }
            }
            if (mState == LoadedState.Method)
            {
                if (Header.Read(stream, Cookies))
                {
                    mState = LoadedState.Header;
                }
            }
            if (mState == LoadedState.Header)
            {
                if (string.Compare(Header[HeaderTypeFactory.CONNECTION], "close", true) == 0)
                {
                    this.KeepAlive = false;
                }
                if (string.Compare(Header[HeaderTypeFactory.TRANSFER_ENCODING], "chunked", true) == 0)
                {
                    Chunked = true;
                }
                else
                {
                    string lenstr = Header[HeaderTypeFactory.CONTENT_LENGTH];
                    int    length = 0;
                    if (lenstr != null)
                    {
                        int.TryParse(lenstr, out length);
                    }
                    Length = length;
                }
                mState = LoadedState.Completed;
            }
            return(mState);
        }
Beispiel #3
0
        public bool Read(PipeStream stream, Cookies cookies)
        {
            string lineData;

            while (stream.TryReadLine(out lineData))
            {
                if (lineData.Length == 0)
                {
                    return(true);
                }
                else
                {
                    ReadOnlySpan <Char>    line   = lineData.AsSpan();
                    Tuple <string, string> result = HttpParse.AnalyzeHeader(line);
                    this[result.Item1] = result.Item2;
                    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);
                    }
                }
            }
            return(false);
        }
Beispiel #4
0
        internal void Execute(PipeStream stream)
        {
            try
            {
                var buffer = HttpParse.GetByteBuffer();
                int offset = 0;
                offset        += Encoding.ASCII.GetBytes(Method, 0, Method.Length, buffer, offset);
                buffer[offset] = HeaderTypeFactory._SPACE_BYTE;
                offset++;
                offset += Encoding.ASCII.GetBytes(Url, 0, Url.Length, buffer, offset);
                if (QuestryString != null && QuestryString.Count > 0)
                {
                    int i = 0;
                    foreach (var item in this.QuestryString)
                    {
                        string key   = item.Key;
                        string value = item.Value;
                        if (string.IsNullOrEmpty(value))
                        {
                            continue;
                        }
                        value = System.Net.WebUtility.UrlEncode(value);
                        if (i == 0)
                        {
                            buffer[offset] = HeaderTypeFactory._QMARK;
                            offset++;
                        }
                        else
                        {
                            buffer[offset] = HeaderTypeFactory._AND;
                            offset++;
                        }
                        offset        += Encoding.ASCII.GetBytes(key, 0, key.Length, buffer, offset);
                        buffer[offset] = HeaderTypeFactory._EQ;
                        offset++;
                        offset += Encoding.ASCII.GetBytes(value, 0, value.Length, buffer, offset);
                        i++;
                    }
                }
                buffer[offset] = HeaderTypeFactory._SPACE_BYTE;
                offset++;
                offset        += Encoding.ASCII.GetBytes(HttpProtocol, 0, HttpProtocol.Length, buffer, offset);
                buffer[offset] = HeaderTypeFactory._LINE_R;
                offset++;
                buffer[offset] = HeaderTypeFactory._LINE_N;
                offset++;
                stream.Write(buffer, 0, offset);

                Formater?.Setting(this);
                if (Header != null)
                {
                    Header.Write(stream);
                }

                if (Method == POST || Method == PUT)
                {
                    if (Body != null)
                    {
                        stream.Write(HeaderTypeFactory.CONTENT_LENGTH_BYTES, 0, 16);
                        MemoryBlockCollection contentLength = stream.Allocate(10);
                        stream.Write(HeaderTypeFactory.TOW_LINE_BYTES, 0, 4);
                        int len = stream.CacheLength;
                        Formater.Serialization(this, Body, stream);
                        int count = stream.CacheLength - len;
                        contentLength.Full(count.ToString().PadRight(10), stream.Encoding);
                    }
                    else
                    {
                        stream.Write(HeaderTypeFactory.NULL_CONTENT_LENGTH_BYTES, 0, HeaderTypeFactory.NULL_CONTENT_LENGTH_BYTES.Length);
                        stream.Write(HeaderTypeFactory.LINE_BYTES, 0, 2);
                    }
                }
                else
                {
                    stream.Write(HeaderTypeFactory.LINE_BYTES, 0, 2);
                }
            }
            catch (Exception e)
            {
                Client?.DisConnect();
                throw new HttpClientException($"http client write data error {e.Message}", e);
            }
        }
        private void loadChunkedData(PipeStream stream)
        {

        Next:
            string line;
            if (chunkeLength > 0)
            {
                if (pipeStream == null)
                    pipeStream = new PipeStream();
                while (true)
                {
                    byte[] buffer = HttpParse.GetByteBuffer();
                    int count = buffer.Length;
                    if (count > chunkeLength)
                        count = chunkeLength;
                    int read = stream.Read(buffer, 0, count);
                    if (read == 0)
                        return;
                    pipeStream.Write(buffer, 0, read);
                    chunkeLength -= read;
                    if (chunkeLength == 0)
                    {
                        chunkeLength = 0;
                        break;
                    }
                }
            }
            else
            {
                if (!stream.TryReadWith(HeaderTypeFactory.LINE_BYTES, out line))
                    return;
                if (string.IsNullOrEmpty(line))
                {
                    if (end)
                    {
                        var item = response;
                        pipeStream.Flush();
                        item.Stream = pipeStream;
                        response = null;
                        pipeStream = null;
                        end = false;
                        Completed?.Invoke(Client, item);
                        return;
                    }
                    else
                    {
                        goto Next;
                    }
                }
                else
                {
                    try
                    {
                        chunkeLength = int.Parse(line, System.Globalization.NumberStyles.HexNumber);
                        if (chunkeLength == 0)
                        {
                            end = true;
                        }
                        else
                            response.Length += chunkeLength;
                    }
                    catch (Exception e_)
                    {
                        throw e_;
                    }
                }
            }
            if (stream.Length > 0)
                goto Next;
        }