Example #1
0
        void OnRead(IAsyncResult base_ares)
        {
            ReadBufferState       rb   = (ReadBufferState)base_ares.AsyncState;
            HttpStreamAsyncResult ares = rb.Ares;

            try {
                int nread = base.EndRead(base_ares);
                decoder.Write(ares.Buffer, ares.Offset, nread);
                nread      = decoder.Read(rb.Buffer, rb.Offset, rb.Count);
                rb.Offset += nread;
                rb.Count  -= nread;
                if (rb.Count == 0 || !decoder.WantMore || nread == 0)
                {
                    no_more_data = !decoder.WantMore && nread == 0;
                    ares.Count   = rb.InitialCount - rb.Count;
                    ares.Complete();
                    return;
                }
                ares.Offset = 0;
                ares.Count  = Math.Min(8192, decoder.ChunkLeft + 6);
                base.BeginRead(ares.Buffer, ares.Offset, ares.Count, OnRead, rb);
            } catch (Exception e) {
                context.Connection.SendError(e.Message, 400);
                ares.Complete(e);
            }
        }
Example #2
0
        public override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback callback, object state)
        {
            if (this._disposed)
            {
                throw new ObjectDisposedException(base.GetType().ToString());
            }
            int num = this.fillFromBuffer(buffer, offset, count);

            if (num > 0 || num == -1)
            {
                HttpStreamAsyncResult httpStreamAsyncResult = new HttpStreamAsyncResult();
                httpStreamAsyncResult.Buffer   = buffer;
                httpStreamAsyncResult.Offset   = offset;
                httpStreamAsyncResult.Count    = count;
                httpStreamAsyncResult.Callback = callback;
                httpStreamAsyncResult.State    = state;
                httpStreamAsyncResult.SyncRead = num;
                httpStreamAsyncResult.Complete();
                return(httpStreamAsyncResult);
            }
            if (this._remainingBody >= 0L && this._remainingBody < (long)count)
            {
                count = (int)this._remainingBody;
            }
            return(this._stream.BeginRead(buffer, offset, count, callback, state));
        }
Example #3
0
        public override int EndRead(IAsyncResult ares)
        {
            if (disposed)
            {
                throw new ObjectDisposedException(typeof(RequestStream).ToString());
            }

            if (ares == null)
            {
                throw new ArgumentNullException("async_result");
            }

            if (ares is HttpStreamAsyncResult)
            {
                HttpStreamAsyncResult r = (HttpStreamAsyncResult)ares;
                if (!ares.IsCompleted)
                {
                    ares.AsyncWaitHandle.WaitOne();
                }
                return(r.SynchRead);
            }

            // Close on exception?
            int nread = stream.EndRead(ares);

            if (remaining_body > 0 && nread > 0)
            {
                remaining_body -= nread;
            }
            return(nread);
        }
Example #4
0
        public override int EndRead(IAsyncResult ares)
        {
            if (disposed)
            {
                throw new ObjectDisposedException(GetType().ToString());
            }

            HttpStreamAsyncResult my_ares = ares as HttpStreamAsyncResult;

            if (ares == null)
            {
                throw new ArgumentException("Invalid IAsyncResult", "ares");
            }

            if (!ares.IsCompleted)
            {
                ares.AsyncWaitHandle.WaitOne();
            }

            if (my_ares.Error != null)
            {
                throw new HttpListenerException(400, "I/O operation aborted.");
            }

            return(my_ares.Count);
        }
        public override IAsyncResult BeginRead(
            byte[] buffer, int offset, int count, AsyncCallback callback, object state)
        {
            if (_disposed)
            {
                throw new ObjectDisposedException(GetType().ToString());
            }

            var nread = fillFromBuffer(buffer, offset, count);

            if (nread > 0 || nread == -1)
            {
                var ares = new HttpStreamAsyncResult(callback, state);
                ares.Buffer   = buffer;
                ares.Offset   = offset;
                ares.Count    = count;
                ares.SyncRead = nread > 0 ? nread : 0;
                ares.Complete();

                return(ares);
            }

            // Avoid reading past the end of the request to allow for HTTP pipelining.
            if (_bodyLeft >= 0 && count > _bodyLeft)
            {
                count = (int)_bodyLeft;
            }

            return(_stream.BeginRead(buffer, offset, count, callback, state));
        }
        public override int EndRead(IAsyncResult asyncResult)
        {
            if (this._disposed)
            {
                throw new ObjectDisposedException(base.GetType().ToString());
            }
            if (asyncResult == null)
            {
                throw new ArgumentNullException("asyncResult");
            }
            HttpStreamAsyncResult httpStreamAsyncResult = asyncResult as HttpStreamAsyncResult;

            if (httpStreamAsyncResult == null)
            {
                throw new ArgumentException("A wrong IAsyncResult.", "asyncResult");
            }
            if (!httpStreamAsyncResult.IsCompleted)
            {
                httpStreamAsyncResult.AsyncWaitHandle.WaitOne();
            }
            if (httpStreamAsyncResult.HasException)
            {
                throw new HttpListenerException(400, "I/O operation aborted.");
            }
            return(httpStreamAsyncResult.Count);
        }
Example #7
0
        public override IAsyncResult BeginRead(byte [] buffer, int offset, int count,
                                               AsyncCallback cback, object state)
        {
            if (disposed)
            {
                throw new ObjectDisposedException(typeof(RequestStream).ToString());
            }

            int nread = FillFromBuffer(buffer, offset, count);

            if (nread > 0 || nread == -1)
            {
                HttpStreamAsyncResult ares = new HttpStreamAsyncResult();
                ares.Buffer    = buffer;
                ares.Offset    = offset;
                ares.Count     = count;
                ares.Callback  = cback;
                ares.State     = state;
                ares.SynchRead = nread;
                ares.Complete();
                return(ares);
            }

            // Avoid reading past the end of the request to allow
            // for HTTP pipelining
            if (remaining_body >= 0 && count > remaining_body)
            {
                count = (int)Math.Min(Int32.MaxValue, remaining_body);
            }
            return(stream.BeginRead(buffer, offset, count, cback, state));
        }
        private void onRead(IAsyncResult asyncResult)
        {
            ReadBufferState       asyncState   = (ReadBufferState)asyncResult.AsyncState;
            HttpStreamAsyncResult initialCount = asyncState.AsyncResult;

            try
            {
                int num = base.EndRead(asyncResult);
                this._decoder.Write(initialCount.Buffer, initialCount.Offset, num);
                num = this._decoder.Read(asyncState.Buffer, asyncState.Offset, asyncState.Count);
                ReadBufferState offset = asyncState;
                offset.Offset = offset.Offset + num;
                ReadBufferState count = asyncState;
                count.Count = count.Count - num;
                if ((asyncState.Count == 0 || !this._decoder.WantMore ? false : num != 0))
                {
                    initialCount.Offset = 0;
                    initialCount.Count  = Math.Min(8192, this._decoder.ChunkLeft + 6);
                    base.BeginRead(initialCount.Buffer, initialCount.Offset, initialCount.Count, new AsyncCallback(this.onRead), asyncState);
                }
                else
                {
                    this._noMoreData   = (this._decoder.WantMore ? false : num == 0);
                    initialCount.Count = asyncState.InitialCount - asyncState.Count;
                    initialCount.Complete();
                }
            }
            catch (Exception exception1)
            {
                Exception exception = exception1;
                this._context.Connection.SendError(exception.Message, 400);
                initialCount.Complete(exception);
            }
        }
Example #9
0
        public override int EndRead(IAsyncResult asyncResult)
        {
            if (this._disposed)
            {
                throw new ObjectDisposedException(base.GetType().ToString());
            }
            if (asyncResult == null)
            {
                throw new ArgumentNullException("asyncResult");
            }
            if (asyncResult is HttpStreamAsyncResult)
            {
                HttpStreamAsyncResult result = (HttpStreamAsyncResult)asyncResult;
                if (!result.IsCompleted)
                {
                    result.AsyncWaitHandle.WaitOne();
                }
                return(result.SyncRead);
            }
            int num = this._stream.EndRead(asyncResult);

            if ((num > 0) && (this._bodyLeft > 0L))
            {
                this._bodyLeft -= num;
            }
            return(num);
        }
Example #10
0
        public override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback callback, object state)
        {
            if (this._disposed)
            {
                throw new ObjectDisposedException(base.GetType().ToString());
            }
            int num = this.fillFromBuffer(buffer, offset, count);

            if ((num <= 0) && (num != -1))
            {
                if ((this._bodyLeft >= 0L) && (count > this._bodyLeft))
                {
                    count = (int)this._bodyLeft;
                }
                return(this._stream.BeginRead(buffer, offset, count, callback, state));
            }
            HttpStreamAsyncResult result = new HttpStreamAsyncResult(callback, state)
            {
                Buffer   = buffer,
                Offset   = offset,
                Count    = count,
                SyncRead = (num <= 0) ? 0 : num
            };

            result.Complete();
            return(result);
        }
Example #11
0
        public override int EndRead(IAsyncResult asyncResult)
        {
            if (this._disposed)
            {
                throw new ObjectDisposedException(base.GetType().ToString());
            }
            if (asyncResult == null)
            {
                throw new ArgumentNullException("asyncResult");
            }
            if (asyncResult is HttpStreamAsyncResult)
            {
                HttpStreamAsyncResult httpStreamAsyncResult = (HttpStreamAsyncResult)asyncResult;
                if (!httpStreamAsyncResult.IsCompleted)
                {
                    httpStreamAsyncResult.AsyncWaitHandle.WaitOne();
                }
                return(httpStreamAsyncResult.SyncRead);
            }
            int num = this._stream.EndRead(asyncResult);

            if (num > 0 && this._remainingBody > 0L)
            {
                this._remainingBody -= (long)num;
            }
            return(num);
        }
 public ReadBufferState(byte[] buffer, int offset, int count, HttpStreamAsyncResult asyncResult)
 {
     this._buffer       = buffer;
     this._offset       = offset;
     this._count        = count;
     this._initialCount = count;
     this._asyncResult  = asyncResult;
 }
 public ReadBufferState(byte[] buffer, int offset, int count, HttpStreamAsyncResult ares)
 {
     this.Buffer       = buffer;
     this.Offset       = offset;
     this.Count        = count;
     this.InitialCount = count;
     this.Ares         = ares;
 }
        public override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback callback, object state)
        {
            if (_disposed)
            {
                throw new ObjectDisposedException(GetType().ToString());
            }
            if (buffer == null)
            {
                throw new ArgumentNullException("buffer");
            }
            if (offset < 0)
            {
                throw new ArgumentOutOfRangeException("offset", "A negative value.");
            }
            if (count < 0)
            {
                throw new ArgumentOutOfRangeException("count", "A negative value.");
            }
            int num = buffer.Length;

            if (offset + count > num)
            {
                throw new ArgumentException("The sum of 'offset' and 'count' is greater than 'buffer' length.");
            }
            HttpStreamAsyncResult httpStreamAsyncResult = new HttpStreamAsyncResult(callback, state);

            if (_noMoreData)
            {
                httpStreamAsyncResult.Complete();
                return(httpStreamAsyncResult);
            }
            int num2 = _decoder.Read(buffer, offset, count);

            offset += num2;
            count  -= num2;
            if (count == 0)
            {
                httpStreamAsyncResult.Count = num2;
                httpStreamAsyncResult.Complete();
                return(httpStreamAsyncResult);
            }
            if (!_decoder.WantMore)
            {
                _noMoreData = num2 == 0;
                httpStreamAsyncResult.Count = num2;
                httpStreamAsyncResult.Complete();
                return(httpStreamAsyncResult);
            }
            httpStreamAsyncResult.Buffer = new byte[8192];
            httpStreamAsyncResult.Offset = 0;
            httpStreamAsyncResult.Count  = 8192;
            ReadBufferState readBufferState = new ReadBufferState(buffer, offset, count, httpStreamAsyncResult);

            readBufferState.InitialCount += num2;
            base.BeginRead(httpStreamAsyncResult.Buffer, httpStreamAsyncResult.Offset, httpStreamAsyncResult.Count, (AsyncCallback)onRead, (object)readBufferState);
            return(httpStreamAsyncResult);
        }
        public override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback callback, object state)
        {
            if (this._disposed)
            {
                throw new ObjectDisposedException(base.GetType().ToString());
            }
            if (buffer == null)
            {
                throw new ArgumentNullException("buffer");
            }
            if (offset < 0)
            {
                throw new ArgumentOutOfRangeException("offset", "A negative value.");
            }
            if (count < 0)
            {
                throw new ArgumentOutOfRangeException("count", "A negative value.");
            }
            int length = buffer.Length;

            if ((offset + count) > length)
            {
                throw new ArgumentException("The sum of 'offset' and 'count' is greater than 'buffer' length.");
            }
            HttpStreamAsyncResult asyncResult = new HttpStreamAsyncResult(callback, state);

            if (this._noMoreData)
            {
                asyncResult.Complete();
                return(asyncResult);
            }
            int num2 = this._decoder.Read(buffer, offset, count);

            offset += num2;
            count  -= num2;
            if (count == 0)
            {
                asyncResult.Count = num2;
                asyncResult.Complete();
                return(asyncResult);
            }
            if (!this._decoder.WantMore)
            {
                this._noMoreData  = num2 == 0;
                asyncResult.Count = num2;
                asyncResult.Complete();
                return(asyncResult);
            }
            asyncResult.Buffer = new byte[0x2000];
            asyncResult.Offset = 0;
            asyncResult.Count  = 0x2000;
            ReadBufferState state2 = new ReadBufferState(buffer, offset, count, asyncResult);

            state2.InitialCount += num2;
            base.BeginRead(asyncResult.Buffer, asyncResult.Offset, asyncResult.Count, new AsyncCallback(this.onRead), state2);
            return(asyncResult);
        }
 public ReadBufferState (
   byte[] buffer, int offset, int count, HttpStreamAsyncResult asyncResult)
 {
   _buffer = buffer;
   _offset = offset;
   _count = count;
   _initialCount = count;
   _asyncResult = asyncResult;
 }
 public ReadBufferState(
     byte [] buffer, int offset, int count, HttpStreamAsyncResult asyncResult)
 {
     Buffer       = buffer;
     Offset       = offset;
     Count        = count;
     InitialCount = count;
     AsyncResult  = asyncResult;
 }
Example #18
0
 public ReadBufferState (
   byte [] buffer, int offset, int count, HttpStreamAsyncResult asyncResult)
 {
   Buffer = buffer;
   Offset = offset;
   Count = count;
   InitialCount = count;
   AsyncResult = asyncResult;
 }
        public override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback cback, object state)
        {
            if (this.disposed)
            {
                throw new ObjectDisposedException(base.GetType().ToString());
            }
            if (buffer == null)
            {
                throw new ArgumentNullException("buffer");
            }
            int num = buffer.Length;

            if (offset < 0 || offset > num)
            {
                throw new ArgumentOutOfRangeException("'offset' exceeds the size of buffer.");
            }
            if (count < 0 || offset > num - count)
            {
                throw new ArgumentOutOfRangeException("'offset' + 'count' exceeds the size of buffer.");
            }
            HttpStreamAsyncResult httpStreamAsyncResult = new HttpStreamAsyncResult();

            httpStreamAsyncResult.Callback = cback;
            httpStreamAsyncResult.State    = state;
            if (this.no_more_data)
            {
                httpStreamAsyncResult.Complete();
                return(httpStreamAsyncResult);
            }
            int num2 = this.decoder.Read(buffer, offset, count);

            offset += num2;
            count  -= num2;
            if (count == 0)
            {
                httpStreamAsyncResult.Count = num2;
                httpStreamAsyncResult.Complete();
                return(httpStreamAsyncResult);
            }
            if (!this.decoder.WantMore)
            {
                this.no_more_data           = (num2 == 0);
                httpStreamAsyncResult.Count = num2;
                httpStreamAsyncResult.Complete();
                return(httpStreamAsyncResult);
            }
            httpStreamAsyncResult.Buffer = new byte[8192];
            httpStreamAsyncResult.Offset = 0;
            httpStreamAsyncResult.Count  = 8192;
            ChunkedInputStream.ReadBufferState readBufferState = new ChunkedInputStream.ReadBufferState(buffer, offset, count, httpStreamAsyncResult);
            readBufferState.InitialCount += num2;
            base.BeginRead(httpStreamAsyncResult.Buffer, httpStreamAsyncResult.Offset, httpStreamAsyncResult.Count, new AsyncCallback(this.OnRead), readBufferState);
            return(httpStreamAsyncResult);
        }
Example #20
0
        public override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback callback, object state)
        {
            if (_disposed)
            {
                throw new ObjectDisposedException(GetType().ToString());
            }
            int num = fillFromBuffer(buffer, offset, count);

            if (num > 0 || num == -1)
            {
                HttpStreamAsyncResult httpStreamAsyncResult = new HttpStreamAsyncResult(callback, state);
                httpStreamAsyncResult.Buffer   = buffer;
                httpStreamAsyncResult.Offset   = offset;
                httpStreamAsyncResult.Count    = count;
                httpStreamAsyncResult.SyncRead = ((num > 0) ? num : 0);
                httpStreamAsyncResult.Complete();
                return(httpStreamAsyncResult);
            }
            if (_bodyLeft >= 0 && count > _bodyLeft)
            {
                count = (int)_bodyLeft;
            }
            return(_stream.BeginRead(buffer, offset, count, callback, state));
        }
Example #21
0
        public override IAsyncResult BeginRead(
            byte[] buffer, int offset, int count, AsyncCallback callback, object state
            )
        {
            if (_disposed)
            {
                var name = GetType().ToString();

                throw new ObjectDisposedException(name);
            }

            if (buffer == null)
            {
                throw new ArgumentNullException("buffer");
            }

            if (offset < 0)
            {
                var msg = "A negative value.";

                throw new ArgumentOutOfRangeException("offset", msg);
            }

            if (count < 0)
            {
                var msg = "A negative value.";

                throw new ArgumentOutOfRangeException("count", msg);
            }

            var len = buffer.Length;

            if (offset + count > len)
            {
                var msg = "The sum of 'offset' and 'count' is greater than the length of 'buffer'.";

                throw new ArgumentException(msg);
            }

            if (count == 0)
            {
                return(_stream.BeginRead(buffer, offset, 0, callback, state));
            }

            var nread = fillFromBuffer(buffer, offset, count);

            if (nread != 0)
            {
                var ares = new HttpStreamAsyncResult(callback, state);
                ares.Buffer   = buffer;
                ares.Offset   = offset;
                ares.Count    = count;
                ares.SyncRead = nread > 0 ? nread : 0;
                ares.Complete();

                return(ares);
            }

            if (_bodyLeft >= 0 && _bodyLeft < count)
            {
                count = (int)_bodyLeft;
            }

            return(_stream.BeginRead(buffer, offset, count, callback, state));
        }
Example #22
0
        public override IAsyncResult BeginRead(
            byte[] buffer, int offset, int count, AsyncCallback callback, object state)
        {
            if (_disposed)
            {
                throw new ObjectDisposedException(GetType().ToString());
            }

            if (buffer == null)
            {
                throw new ArgumentNullException(nameof(buffer));
            }

            if (offset < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(offset), "A negative value.");
            }

            if (count < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(count), "A negative value.");
            }

            var len = buffer.Length;

            if (offset + count > len)
            {
                throw new ArgumentException(
                          "The sum of 'offset' and 'count' is greater than 'buffer' length.");
            }

            var ares = new HttpStreamAsyncResult(callback, state);

            if (_noMoreData)
            {
                ares.Complete();
                return(ares);
            }

            var nread = _decoder.Read(buffer, offset, count);

            offset += nread;
            count  -= nread;
            if (count == 0)
            {
                // Got all we wanted, no need to bother the decoder yet.
                ares.Count = nread;
                ares.Complete();

                return(ares);
            }

            if (!_decoder.WantMore)
            {
                _noMoreData = nread == 0;
                ares.Count  = nread;
                ares.Complete();

                return(ares);
            }

            ares.Buffer = new byte[_bufferLength];
            ares.Offset = 0;
            ares.Count  = _bufferLength;

            var rstate = new ReadBufferState(buffer, offset, count, ares);

            rstate.InitialCount += nread;
            base.BeginRead(ares.Buffer, ares.Offset, ares.Count, onRead, rstate);

            return(ares);
        }
        public override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback callback, object state)
        {
            IAsyncResult asyncResult;

            if (this._disposed)
            {
                throw new ObjectDisposedException(base.GetType().ToString());
            }
            if (buffer == null)
            {
                throw new ArgumentNullException("buffer");
            }
            if (offset < 0)
            {
                throw new ArgumentOutOfRangeException("offset", "A negative value.");
            }
            if (count < 0)
            {
                throw new ArgumentOutOfRangeException("count", "A negative value.");
            }
            if (offset + count > (int)buffer.Length)
            {
                throw new ArgumentException("The sum of 'offset' and 'count' is greater than 'buffer' length.");
            }
            HttpStreamAsyncResult httpStreamAsyncResult = new HttpStreamAsyncResult(callback, state);

            if (!this._noMoreData)
            {
                int num = this._decoder.Read(buffer, offset, count);
                offset += num;
                count  -= num;
                if (count == 0)
                {
                    httpStreamAsyncResult.Count = num;
                    httpStreamAsyncResult.Complete();
                    asyncResult = httpStreamAsyncResult;
                }
                else if (this._decoder.WantMore)
                {
                    httpStreamAsyncResult.Buffer = new byte[8192];
                    httpStreamAsyncResult.Offset = 0;
                    httpStreamAsyncResult.Count  = 8192;
                    ReadBufferState readBufferState = new ReadBufferState(buffer, offset, count, httpStreamAsyncResult);
                    ReadBufferState initialCount    = readBufferState;
                    initialCount.InitialCount = initialCount.InitialCount + num;
                    base.BeginRead(httpStreamAsyncResult.Buffer, httpStreamAsyncResult.Offset, httpStreamAsyncResult.Count, new AsyncCallback(this.onRead), readBufferState);
                    asyncResult = httpStreamAsyncResult;
                }
                else
                {
                    this._noMoreData            = num == 0;
                    httpStreamAsyncResult.Count = num;
                    httpStreamAsyncResult.Complete();
                    asyncResult = httpStreamAsyncResult;
                }
            }
            else
            {
                httpStreamAsyncResult.Complete();
                asyncResult = httpStreamAsyncResult;
            }
            return(asyncResult);
        }
    public override IAsyncResult BeginRead (
      byte [] buffer, int offset, int count, AsyncCallback callback, object state)
    {
      if (_disposed)
        throw new ObjectDisposedException (GetType ().ToString ());

      if (buffer == null)
        throw new ArgumentNullException ("buffer");

      var len = buffer.Length;
      if (offset < 0 || offset > len)
        throw new ArgumentOutOfRangeException ("'offset' exceeds the size of buffer.");

      if (count < 0 || offset > len - count)
        throw new ArgumentOutOfRangeException ("'offset' + 'count' exceeds the size of buffer.");

      var ares = new HttpStreamAsyncResult (callback, state);
      if (_noMoreData) {
        ares.Complete ();
        return ares;
      }

      var nread = _decoder.Read (buffer, offset, count);
      offset += nread;
      count -= nread;
      if (count == 0) {
        // Got all we wanted, no need to bother the decoder yet.
        ares.Count = nread;
        ares.Complete ();

        return ares;
      }

      if (!_decoder.WantMore) {
        _noMoreData = nread == 0;
        ares.Count = nread;
        ares.Complete ();

        return ares;
      }

      ares.Buffer = new byte [_bufferSize];
      ares.Offset = 0;
      ares.Count = _bufferSize;

      var readState = new ReadBufferState (buffer, offset, count, ares);
      readState.InitialCount += nread;
      base.BeginRead (ares.Buffer, ares.Offset, ares.Count, onRead, readState);

      return ares;
    }
Example #25
0
        public override IAsyncResult BeginRead(
            byte [] buffer, int offset, int count, AsyncCallback cback, object state)
        {
            if (disposed)
            {
                throw new ObjectDisposedException(GetType().ToString());
            }

            if (buffer == null)
            {
                throw new ArgumentNullException("buffer");
            }

            int len = buffer.Length;

            if (offset < 0 || offset > len)
            {
                throw new ArgumentOutOfRangeException("offset exceeds the size of buffer");
            }

            if (count < 0 || offset > len - count)
            {
                throw new ArgumentOutOfRangeException("offset+size exceeds the size of buffer");
            }

            HttpStreamAsyncResult ares = new HttpStreamAsyncResult();

            ares.Callback = cback;
            ares.State    = state;
            if (no_more_data)
            {
                ares.Complete();
                return(ares);
            }
            int nread = decoder.Read(buffer, offset, count);

            offset += nread;
            count  -= nread;
            if (count == 0)
            {
                // got all we wanted, no need to bother the decoder yet
                ares.Count = nread;
                ares.Complete();
                return(ares);
            }
            if (!decoder.WantMore)
            {
                no_more_data = nread == 0;
                ares.Count   = nread;
                ares.Complete();
                return(ares);
            }
            ares.Buffer = new byte [8192];
            ares.Offset = 0;
            ares.Count  = 8192;
            ReadBufferState rb = new ReadBufferState(buffer, offset, count, ares);

            rb.InitialCount += nread;
            base.BeginRead(ares.Buffer, ares.Offset, ares.Count, OnRead, rb);
            return(ares);
        }
Example #26
0
    public override IAsyncResult BeginRead (
      byte [] buffer, int offset, int count, AsyncCallback callback, object state)
    {
      if (_disposed)
        throw new ObjectDisposedException (GetType ().ToString ());

      var nread = fillFromBuffer (buffer, offset, count);
      if (nread > 0 || nread == -1) {
        var ares = new HttpStreamAsyncResult (callback, state);
        ares.Buffer = buffer;
        ares.Offset = offset;
        ares.Count = count;
        ares.SyncRead = nread;
        ares.Complete ();

        return ares;
      }

      // Avoid reading past the end of the request to allow for HTTP pipelining.
      if (_remainingBody >= 0 && _remainingBody < count)
        count = (int) _remainingBody;

      return _stream.BeginRead (buffer, offset, count, callback, state);
    }
        public override IAsyncResult BeginRead(
			byte [] buffer, int offset, int count, AsyncCallback cback, object state)
        {
            if (disposed)
                throw new ObjectDisposedException (GetType ().ToString ());

            int nread = FillFromBuffer (buffer, offset, count);
            if (nread > 0 || nread == -1) {
                var ares = new HttpStreamAsyncResult ();
                ares.Buffer = buffer;
                ares.Offset = offset;
                ares.Count = count;
                ares.Callback = cback;
                ares.State = state;
                ares.SyncRead = nread;
                ares.Complete ();
                return ares;
            }

            // Avoid reading past the end of the request to allow
            // for HTTP pipelining
            if (remaining_body >= 0 && count > remaining_body)
                count = (int) Math.Min (Int32.MaxValue, remaining_body);

            return stream.BeginRead (buffer, offset, count, cback, state);
        }
        public override IAsyncResult BeginRead(
			byte [] buffer, int offset, int count, AsyncCallback cback, object state)
        {
            if (disposed)
                throw new ObjectDisposedException (GetType ().ToString ());

            if (buffer == null)
                throw new ArgumentNullException ("buffer");

            int len = buffer.Length;
            if (offset < 0 || offset > len)
                throw new ArgumentOutOfRangeException ("'offset' exceeds the size of buffer.");

            if (count < 0 || offset > len - count)
                throw new ArgumentOutOfRangeException ("'offset' + 'count' exceeds the size of buffer.");

            var ares = new HttpStreamAsyncResult ();
            ares.Callback = cback;
            ares.State = state;
            if (no_more_data) {
                ares.Complete ();
                return ares;
            }

            int nread = decoder.Read (buffer, offset, count);
            offset += nread;
            count -= nread;
            if (count == 0) {
                // got all we wanted, no need to bother the decoder yet
                ares.Count = nread;
                ares.Complete ();
                return ares;
            }

            if (!decoder.WantMore) {
                no_more_data = nread == 0;
                ares.Count = nread;
                ares.Complete ();
                return ares;
            }

            ares.Buffer = new byte [8192];
            ares.Offset = 0;
            ares.Count = 8192;
            var rb = new ReadBufferState (buffer, offset, count, ares);
            rb.InitialCount += nread;
            base.BeginRead (ares.Buffer, ares.Offset, ares.Count, OnRead, rb);
            return ares;
        }
        public override IAsyncResult BeginRead(
            byte [] buffer, int offset, int count, AsyncCallback callback, object state)
        {
            if (_disposed)
            {
                throw new ObjectDisposedException(GetType().ToString());
            }

            if (buffer == null)
            {
                throw new ArgumentNullException("buffer");
            }

            var len = buffer.Length;

            if (offset < 0 || offset > len)
            {
                throw new ArgumentOutOfRangeException("'offset' exceeds the size of buffer.");
            }

            if (count < 0 || offset > len - count)
            {
                throw new ArgumentOutOfRangeException("'offset' + 'count' exceeds the size of buffer.");
            }

            var ares = new HttpStreamAsyncResult(callback, state);

            if (_noMoreData)
            {
                ares.Complete();
                return(ares);
            }

            var nread = _decoder.Read(buffer, offset, count);

            offset += nread;
            count  -= nread;
            if (count == 0)
            {
                // Got all we wanted, no need to bother the decoder yet.
                ares.Count = nread;
                ares.Complete();

                return(ares);
            }

            if (!_decoder.WantMore)
            {
                _noMoreData = nread == 0;
                ares.Count  = nread;
                ares.Complete();

                return(ares);
            }

            ares.Buffer = new byte [_bufferSize];
            ares.Offset = 0;
            ares.Count  = _bufferSize;

            var readState = new ReadBufferState(buffer, offset, count, ares);

            readState.InitialCount += nread;
            base.BeginRead(ares.Buffer, ares.Offset, ares.Count, onRead, readState);

            return(ares);
        }