Esempio n. 1
0
        public void Write(PipeStream stream)
        {
            byte[] buffer = HttpParse.GetByteBuffer();
            int    count  = Type.Bytes.Length;

            System.Buffer.BlockCopy(Type.Bytes, 0, buffer, 0, count);
            count             = count + Encoding.UTF8.GetBytes(Value, 0, Value.Length, buffer, count);
            buffer[count]     = HeaderTypeFactory._LINE_R;
            buffer[count + 1] = HeaderTypeFactory._LINE_N;
            stream.Write(buffer, 0, count + 2);
        }
Esempio n. 2
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);
            }
        }
Esempio n. 3
0
        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;
        }