コード例 #1
0
        internal async Task <int> InnerReadAsync(byte[] buffer, int offset, int size, CancellationToken cancellationToken)
        {
            WebConnection.Debug($"{ME} INNER READ ASYNC");

            Operation.ThrowIfDisposed(cancellationToken);

            int  nbytes = 0;
            bool done   = false;

            if (!ChunkedRead || (!ChunkStream.DataAvailable && ChunkStream.WantMore))
            {
                nbytes = await InnerStream.ReadAsync(buffer, offset, size, cancellationToken).ConfigureAwait(false);

                WebConnection.Debug($"{ME} INNER READ ASYNC #1: {nbytes} {ChunkedRead}");
                if (!ChunkedRead)
                {
                    return(nbytes);
                }
                done = nbytes == 0;
            }

            try {
                ChunkStream.WriteAndReadBack(buffer, offset, size, ref nbytes);
                WebConnection.Debug($"{ME} INNER READ ASYNC #1: {done} {nbytes} {ChunkStream.WantMore}");
                if (!done && nbytes == 0 && ChunkStream.WantMore)
                {
                    nbytes = await EnsureReadAsync(buffer, offset, size, cancellationToken).ConfigureAwait(false);
                }
            } catch (Exception e) {
                if (e is WebException || e is OperationCanceledException)
                {
                    throw;
                }
                throw new WebException("Invalid chunked data.", e, WebExceptionStatus.ServerProtocolViolation, null);
            }

            if ((done || nbytes == 0) && ChunkStream.ChunkLeft != 0)
            {
                // HandleError (WebExceptionStatus.ReceiveFailure, null, "chunked EndRead");
                throw new WebException("Read error", null, WebExceptionStatus.ReceiveFailure, null);
            }

            return(nbytes);
        }
コード例 #2
0
        internal int EndRead(HttpWebRequest request, IAsyncResult result)
        {
            Stream s = null;

            lock (this) {
                if (request.Aborted)
                {
                    throw new WebException("Request aborted", WebExceptionStatus.RequestCanceled);
                }
                if (Data.request != request)
                {
                    throw new ObjectDisposedException(typeof(NetworkStream).FullName);
                }
                if (nstream == null)
                {
                    throw new ObjectDisposedException(typeof(NetworkStream).FullName);
                }
                s = nstream;
            }

            int            nbytes  = 0;
            bool           done    = false;
            WebAsyncResult wr      = null;
            IAsyncResult   nsAsync = ((WebAsyncResult)result).InnerAsyncResult;

            if (chunkedRead && (nsAsync is WebAsyncResult))
            {
                wr = (WebAsyncResult)nsAsync;
                IAsyncResult inner = wr.InnerAsyncResult;
                if (inner != null && !(inner is WebAsyncResult))
                {
                    nbytes = s.EndRead(inner);
                    done   = nbytes == 0;
                }
            }
            else if (!(nsAsync is WebAsyncResult))
            {
                nbytes = s.EndRead(nsAsync);
                wr     = (WebAsyncResult)result;
                done   = nbytes == 0;
            }

            if (chunkedRead)
            {
                try {
                    chunkStream.WriteAndReadBack(wr.Buffer, wr.Offset, wr.Size, ref nbytes);
                    if (!done && nbytes == 0 && chunkStream.WantMore)
                    {
                        nbytes = EnsureRead(wr.Buffer, wr.Offset, wr.Size);
                    }
                } catch (Exception e) {
                    if (e is WebException)
                    {
                        throw e;
                    }

                    throw new WebException("Invalid chunked data.", e,
                                           WebExceptionStatus.ServerProtocolViolation, null);
                }

                if ((done || nbytes == 0) && chunkStream.ChunkLeft != 0)
                {
                    HandleError(WebExceptionStatus.ReceiveFailure, null, "chunked EndRead");
                    throw new WebException("Read error", null, WebExceptionStatus.ReceiveFailure, null);
                }
            }

            return((nbytes != 0) ? nbytes : -1);
        }