Exemplo n.º 1
0
 protected int ReadChunkLength(Stream stream)
 {
     return(int.Parse(HTTPResponse.ReadTo(stream, 10).Split(new char[]
     {
         ';'
     })[0], NumberStyles.AllowHexSpecifier));
 }
Exemplo n.º 2
0
        protected void ReadChunked(Stream stream)
        {
            this.BeginReceiveStreamFragments();
            using (MemoryStream memoryStream = new MemoryStream())
            {
                int    num   = this.ReadChunkLength(stream);
                byte[] array = new byte[num];
                int    num2  = 0;
                this.baseRequest.DownloadLength          = num;
                this.baseRequest.DownloadProgressChanged = (this.IsSuccess || this.IsFromCache);
                while (num != 0)
                {
                    if (array.Length < num)
                    {
                        Array.Resize <byte>(ref array, num);
                    }
                    int num3 = 0;
                    this.WaitWhileHasFragments();
                    do
                    {
                        int num4 = stream.Read(array, num3, num - num3);
                        if (num4 == 0)
                        {
                            goto Block_5;
                        }
                        num3 += num4;
                    }while (num3 < num);
                    if (this.baseRequest.UseStreaming)
                    {
                        this.FeedStreamFragment(array, 0, num3);
                    }
                    else
                    {
                        memoryStream.Write(array, 0, num3);
                    }
                    HTTPResponse.ReadTo(stream, 10);
                    num2 += num3;
                    num   = this.ReadChunkLength(stream);
                    this.baseRequest.DownloadLength         += num;
                    this.baseRequest.Downloaded              = num2;
                    this.baseRequest.DownloadProgressChanged = (this.IsSuccess || this.IsFromCache);
                    continue;
Block_5:
                    throw new Exception("The remote server closed the connection unexpectedly!");
                }
                if (this.baseRequest.UseStreaming)
                {
                    this.FlushRemainingFragmentBuffer();
                }
                this.ReadHeaders(stream);
                if (!this.baseRequest.UseStreaming)
                {
                    this.Data = this.DecodeStream(memoryStream);
                }
            }
        }
Exemplo n.º 3
0
        protected void ReadHeaders(Stream stream)
        {
            string text = HTTPResponse.ReadTo(stream, 58, 10).Trim();

            while (text != string.Empty)
            {
                string value = HTTPResponse.ReadTo(stream, 10);
                this.AddHeader(text, value);
                text = HTTPResponse.ReadTo(stream, 58, 10);
            }
        }
Exemplo n.º 4
0
        internal virtual bool Receive(int forceReadRawContentLength = -1, bool readPayloadData = true)
        {
            string text = string.Empty;

            try
            {
                text = HTTPResponse.ReadTo(this.Stream, 32);
            }
            catch
            {
                if (!this.baseRequest.DisableRetry)
                {
                    bool result = false;
                    return(result);
                }
                throw;
            }
            if (!this.baseRequest.DisableRetry && string.IsNullOrEmpty(text))
            {
                return(false);
            }
            string[] array = text.Split(new char[]
            {
                '/',
                '.'
            });
            this.VersionMajor = int.Parse(array[1]);
            this.VersionMinor = int.Parse(array[2]);
            string text2 = HTTPResponse.NoTrimReadTo(this.Stream, 32, 10);
            int    statusCode;

            if (this.baseRequest.DisableRetry)
            {
                statusCode = int.Parse(text2);
            }
            else if (!int.TryParse(text2, out statusCode))
            {
                return(false);
            }
            this.StatusCode = statusCode;
            if (text2.Length > 0 && (byte)text2[text2.Length - 1] != 10 && (byte)text2[text2.Length - 1] != 13)
            {
                this.Message = HTTPResponse.ReadTo(this.Stream, 10);
            }
            else
            {
                this.Message = string.Empty;
            }
            this.ReadHeaders(this.Stream);
            this.IsUpgraded = (this.StatusCode == 101 && (this.HasHeaderWithValue("connection", "upgrade") || this.HasHeader("upgrade")));
            if (!readPayloadData)
            {
                return(true);
            }
            if (forceReadRawContentLength != -1)
            {
                this.IsFromCache = true;
                this.ReadRaw(this.Stream, forceReadRawContentLength);
                return(true);
            }
            if ((this.StatusCode >= 100 && this.StatusCode < 200) || this.StatusCode == 204 || this.StatusCode == 304 || this.baseRequest.MethodType == HTTPMethods.Head)
            {
                return(true);
            }
            if (this.HasHeaderWithValue("transfer-encoding", "chunked"))
            {
                this.ReadChunked(this.Stream);
            }
            else
            {
                List <string> headerValues  = this.GetHeaderValues("content-length");
                List <string> headerValues2 = this.GetHeaderValues("content-range");
                if (headerValues != null && headerValues2 == null)
                {
                    this.ReadRaw(this.Stream, int.Parse(headerValues[0]));
                }
                else if (headerValues2 != null)
                {
                    HTTPRange range = this.GetRange();
                    this.ReadRaw(this.Stream, range.LastBytePos - range.FirstBytePos + 1);
                }
                else
                {
                    this.ReadUnknownSize(this.Stream);
                }
            }
            return(true);
        }