public ChunkedInputStream(HttpListenerContext context, Stream stream, byte[] buffer, int offset, int length) : base(stream, buffer, offset, length)
        {
            this.context = context;
            WebHeaderCollection headers = (WebHeaderCollection)context.Request.Headers;

            this.decoder = new ChunkStream(headers);
        }
Example #2
0
 public ChunkedInputStream(HttpListenerContext context, Stream stream,
                           byte[] buffer, int offset, int length)
     : base(stream, buffer, offset, length)
 {
     _context = context;
     Decoder  = new ChunkStream(context.Request.Headers);
 }
		public ChunkedInputStream (HttpListenerContext context, Stream stream,
						byte [] buffer, int offset, int length)
					: base (stream, buffer, offset, length)
		{
			this.context = context;
			WebHeaderCollection coll = (WebHeaderCollection) context.Request.Headers;
			decoder = new ChunkStream (coll);
		}
Example #4
0
        private ChunkStream.State ReadTrailer(byte[] buffer, ref int offset, int size)
        {
            if (this.trailerState == 2 && (ushort)buffer[offset] == 13 && this.saved.Length == 0)
            {
                offset++;
                if (offset < size && (ushort)buffer[offset] == 10)
                {
                    offset++;
                    return(ChunkStream.State.None);
                }
                offset--;
            }
            int    num  = this.trailerState;
            string text = "\r\n\r";

            while (offset < size && num < 4)
            {
                char c = (char)buffer[offset++];
                if ((num == 0 || num == 2) && c == '\r')
                {
                    num++;
                }
                else if ((num == 1 || num == 3) && c == '\n')
                {
                    num++;
                }
                else if (num > 0)
                {
                    this.saved.Append(text.Substring(0, (this.saved.Length != 0) ? num : (num - 2)));
                    num = 0;
                    if (this.saved.Length > 4196)
                    {
                        ChunkStream.ThrowProtocolViolation("Error reading trailer (too long).");
                    }
                }
            }
            if (num < 4)
            {
                this.trailerState = num;
                if (offset < size)
                {
                    ChunkStream.ThrowProtocolViolation("Error reading trailer.");
                }
                return(ChunkStream.State.Trailer);
            }
            StringReader stringReader = new StringReader(this.saved.ToString());
            string       text2;

            while ((text2 = stringReader.ReadLine()) != null && text2 != string.Empty)
            {
                this.headers.Add(text2);
            }
            return(ChunkStream.State.None);
        }
Example #5
0
 private ChunkStream.State ReadCRLF(byte[] buffer, ref int offset, int size)
 {
     if (!this.sawCR)
     {
         if ((ushort)buffer[offset++] != 13)
         {
             ChunkStream.ThrowProtocolViolation("Expecting \\r");
         }
         this.sawCR = true;
         if (offset == size)
         {
             return(ChunkStream.State.BodyFinished);
         }
     }
     if (this.sawCR && (ushort)buffer[offset++] != 10)
     {
         ChunkStream.ThrowProtocolViolation("Expecting \\n");
     }
     return(ChunkStream.State.None);
 }
Example #6
0
        private void Connect(HttpWebRequest request)
        {
            lock (socketLock)
            {
                if (this.socket != null && this.socket.Connected && status == WebExceptionStatus.Success && CanReuse() && CompleteChunkedRead())
                {
                    reused = true;
                }
                else
                {
                    reused = false;
                    if (this.socket != null)
                    {
                        this.socket.Close();
                        this.socket = null;
                    }
                    chunkStream = null;
                    IPHostEntry hostEntry = sPoint.HostEntry;
                    if (hostEntry == null)
                    {
                        status = ((!sPoint.UsesProxy) ? WebExceptionStatus.NameResolutionFailure : WebExceptionStatus.ProxyNameResolutionFailure);
                    }
                    else
                    {
                        WebConnectionData data        = Data;
                        IPAddress[]       addressList = hostEntry.AddressList;
                        foreach (IPAddress iPAddress in addressList)
                        {
                            this.socket = new Socket(iPAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
                            IPEndPoint iPEndPoint = new IPEndPoint(iPAddress, sPoint.Address.Port);
                            this.socket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.Debug, (!sPoint.UseNagleAlgorithm) ? 1 : 0);
                            this.socket.NoDelay = !sPoint.UseNagleAlgorithm;
                            if (!sPoint.CallEndPointDelegate(this.socket, iPEndPoint))
                            {
                                this.socket.Close();
                                this.socket = null;
                                status      = WebExceptionStatus.ConnectFailure;
                            }
                            else
                            {
                                try
                                {
                                    if (!request.Aborted)
                                    {
                                        CheckUnityWebSecurity(request);
                                        this.socket.Connect(iPEndPoint, requireSocketPolicy: false);
                                        status = WebExceptionStatus.Success;
                                    }
                                    return;

                                    IL_01a1 :;
                                }
                                catch (ThreadAbortException)
                                {
                                    Socket socket = this.socket;
                                    this.socket = null;
                                    socket?.Close();
                                    return;

                                    IL_01c9 :;
                                }
                                catch (ObjectDisposedException)
                                {
                                    return;

                                    IL_01d5 :;
                                }
                                catch (Exception ex3)
                                {
                                    Socket socket2 = this.socket;
                                    this.socket = null;
                                    socket2?.Close();
                                    if (!request.Aborted)
                                    {
                                        status = WebExceptionStatus.ConnectFailure;
                                    }
                                    connect_exception = ex3;
                                }
                            }
                        }
                    }
                }
            }
        }
Example #7
0
		void Connect (HttpWebRequest request)
		{
			lock (socketLock) {
				if (socket != null && socket.Connected && status == WebExceptionStatus.Success) {
					// Take the chunked stream to the expected state (State.None)
					if (CanReuse () && CompleteChunkedRead ()) {
						reused = true;
						return;
					}
				}

				reused = false;
				if (socket != null) {
					socket.Close();
					socket = null;
				}

				chunkStream = null;
				IPHostEntry hostEntry = sPoint.HostEntry;

				if (hostEntry == null) {
#if MONOTOUCH
					if (start_wwan != null) {
						start_wwan.Invoke (null, new object [1] { sPoint.Address });
						hostEntry = sPoint.HostEntry;
					}
					if (hostEntry == null) {
#endif
						status = sPoint.UsesProxy ? WebExceptionStatus.ProxyNameResolutionFailure :
									    WebExceptionStatus.NameResolutionFailure;
						return;
#if MONOTOUCH
					}
#endif
				}

				//WebConnectionData data = Data;
				foreach (IPAddress address in hostEntry.AddressList) {
					try {
						socket = new Socket (address.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
					} catch (Exception se) {
						// The Socket ctor can throw if we run out of FD's
						if (!request.Aborted)
								status = WebExceptionStatus.ConnectFailure;
						connect_exception = se;
						return;
					}
					IPEndPoint remote = new IPEndPoint (address, sPoint.Address.Port);
					socket.NoDelay = !sPoint.UseNagleAlgorithm;
					try {
						sPoint.KeepAliveSetup (socket);
					} catch {
						// Ignore. Not supported in all platforms.
					}

					if (!sPoint.CallEndPointDelegate (socket, remote)) {
						socket.Close ();
						socket = null;
						status = WebExceptionStatus.ConnectFailure;
					} else {
						try {
							if (request.Aborted)
								return;
							socket.Connect (remote);
							status = WebExceptionStatus.Success;
							break;
						} catch (ThreadAbortException) {
							// program exiting...
							Socket s = socket;
							socket = null;
							if (s != null)
								s.Close ();
							return;
						} catch (ObjectDisposedException) {
							// socket closed from another thread
							return;
						} catch (Exception exc) {
							Socket s = socket;
							socket = null;
							if (s != null)
								s.Close ();
							if (!request.Aborted)
								status = WebExceptionStatus.ConnectFailure;
							connect_exception = exc;
						}
					}
				}
			}
		}
Example #8
0
        void Connect(HttpWebRequest request)
        {
            lock (socketLock) {
                if (socket != null && socket.Connected && status == WebExceptionStatus.Success)
                {
                    // Take the chunked stream to the expected state (State.None)
                    if (CanReuse() && CompleteChunkedRead())
                    {
                        reused = true;
                        return;
                    }
                }

                reused = false;
                if (socket != null)
                {
                    socket.Close();
                    socket = null;
                }

                chunkStream = null;
                IPHostEntry hostEntry = sPoint.HostEntry;

                if (hostEntry == null)
                {
#if MONOTOUCH
                    if (start_wwan != null)
                    {
                        start_wwan.Invoke(null, new object [1] {
                            sPoint.Address
                        });
                        hostEntry = sPoint.HostEntry;
                    }
                    if (hostEntry == null)
                    {
#endif
                    status = sPoint.UsesProxy ? WebExceptionStatus.ProxyNameResolutionFailure :
                             WebExceptionStatus.NameResolutionFailure;
                    return;

#if MONOTOUCH
                }
#endif
                }

                WebConnectionData data = Data;
                foreach (IPAddress address in hostEntry.AddressList)
                {
                    socket = new Socket(address.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
                    IPEndPoint remote = new IPEndPoint(address, sPoint.Address.Port);
#if NET_1_1
                    socket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.NoDelay, sPoint.UseNagleAlgorithm ? 0 : 1);
#endif
#if NET_2_0
                    socket.NoDelay = !sPoint.UseNagleAlgorithm;
                    if (!sPoint.CallEndPointDelegate(socket, remote))
                    {
                        socket.Close();
                        socket = null;
                        status = WebExceptionStatus.ConnectFailure;
                    }
                    else
                    {
#endif
                    try {
                        if (request.Aborted)
                        {
                            return;
                        }
                        socket.Connect(remote);
                        status = WebExceptionStatus.Success;
                        break;
                    } catch (ThreadAbortException) {
                        // program exiting...
                        Socket s = socket;
                        socket = null;
                        if (s != null)
                        {
                            s.Close();
                        }
                        return;
                    } catch (ObjectDisposedException exc) {
                        // socket closed from another thread
                        return;
                    } catch (Exception exc) {
                        Socket s = socket;
                        socket = null;
                        if (s != null)
                        {
                            s.Close();
                        }
                        if (!request.Aborted)
                        {
                            status = WebExceptionStatus.ConnectFailure;
                        }
                        connect_exception = exc;
                    }
#if NET_2_0
                }
#endif
                }
            }
        }
Example #9
0
        void Connect()
        {
            lock (socketLock) {
                if (socket != null && socket.Connected && status == WebExceptionStatus.Success)
                {
                    // Take the chunked stream to the expected state (State.None)
                    if (CanReuse() && CompleteChunkedRead())
                    {
                        reused = true;
                        return;
                    }
                }

                reused = false;
                if (socket != null)
                {
                    socket.Close();
                    socket = null;
                }

                chunkStream = null;
                IPHostEntry hostEntry = sPoint.HostEntry;

                if (hostEntry == null)
                {
                    status = sPoint.UsesProxy ? WebExceptionStatus.ProxyNameResolutionFailure :
                             WebExceptionStatus.NameResolutionFailure;
                    return;
                }

                foreach (IPAddress address in hostEntry.AddressList)
                {
                    socket = new Socket(address.AddressFamily, SocketType.Stream, ProtocolType.Tcp);

                    IPEndPoint remote = new IPEndPoint(address, sPoint.Address.Port);

#if NET_2_0
                    if (!sPoint.CallEndPointDelegate(socket, remote))
                    {
                        socket.Close();
                        socket = null;
                        status = WebExceptionStatus.ConnectFailure;
                    }
                    else
                    {
#endif
                    try {
                        socket.Connect(remote);
                        status = WebExceptionStatus.Success;
                        break;
                    } catch (SocketException) {
                        // This might be null if the request is aborted
                        if (socket != null)
                        {
                            socket.Close();
                            socket = null;
                            status = WebExceptionStatus.ConnectFailure;
                        }
                    }
#if NET_2_0
                }
#endif
                }
            }
        }
Example #10
0
        void Connect(HttpWebRequest request)
        {
            lock (socketLock) {
                if (socket != null && socket.Connected && status == WebExceptionStatus.Success)
                {
                    // Take the chunked stream to the expected state (State.None)
                    if (CanReuse() && CompleteChunkedRead())
                    {
                        reused = true;
                        return;
                    }
                }

                reused = false;
                if (socket != null)
                {
                    socket.Close();
                    socket = null;
                }

                chunkStream = null;
                IPHostEntry hostEntry = sPoint.HostEntry;

                if (hostEntry == null)
                {
#if MONOTOUCH
                    xamarin_start_wwan(sPoint.Address.ToString());
                    hostEntry = sPoint.HostEntry;
                    if (hostEntry == null)
                    {
#endif
                    status = sPoint.UsesProxy ? WebExceptionStatus.ProxyNameResolutionFailure :
                             WebExceptionStatus.NameResolutionFailure;
                    return;

#if MONOTOUCH
                }
#endif
                }

                //WebConnectionData data = Data;
                foreach (IPAddress address in hostEntry.AddressList)
                {
                    try {
                        socket = new Socket(address.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
                    }
                    catch (Exception se) {
                        // The Socket ctor can throw if we run out of FD's
                        if (!request.Aborted)
                        {
                            status = WebExceptionStatus.ConnectFailure;
                        }
                        connect_exception = se;
                        return;
                    }
                    IPEndPoint remote = new IPEndPoint(address, sPoint.Address.Port);
                    socket.NoDelay = !sPoint.UseNagleAlgorithm;
                    try {
                        sPoint.KeepAliveSetup(socket);
                    } catch {
                        // Ignore. Not supported in all platforms.
                    }

                    if (!sPoint.CallEndPointDelegate(socket, remote))
                    {
                        socket.Close();
                        socket = null;
                        status = WebExceptionStatus.ConnectFailure;
                    }
                    else
                    {
                        try {
                            if (request.Aborted)
                            {
                                return;
                            }
                            socket.Connect(remote);
                            status = WebExceptionStatus.Success;
                            break;
                        } catch (ThreadAbortException) {
                            // program exiting...
                            Socket s = socket;
                            socket = null;
                            if (s != null)
                            {
                                s.Close();
                            }
                            return;
                        } catch (ObjectDisposedException) {
                            // socket closed from another thread
                            return;
                        } catch (Exception exc) {
                            Socket s = socket;
                            socket = null;
                            if (s != null)
                            {
                                s.Close();
                            }
                            if (!request.Aborted)
                            {
                                status = WebExceptionStatus.ConnectFailure;
                            }
                            connect_exception = exc;
                        }
                    }
                }
            }
        }
        async Task Initialize(BufferOffsetSize buffer, CancellationToken cancellationToken)
        {
            WebConnection.Debug($"{ME} INIT: status={(int)StatusCode} bos={buffer.Offset}/{buffer.Size}");

            string contentType = Headers["Transfer-Encoding"];
            bool   chunkedRead = (contentType != null && contentType.IndexOf("chunked", StringComparison.OrdinalIgnoreCase) != -1);
            string clength     = Headers["Content-Length"];

            if (!chunkedRead && !string.IsNullOrEmpty(clength))
            {
                if (!long.TryParse(clength, out contentLength))
                {
                    contentLength = Int64.MaxValue;
                }
            }
            else
            {
                contentLength = Int64.MaxValue;
            }

            if (Version == HttpVersion.Version11 && RequestStream.KeepAlive)
            {
                KeepAlive = true;
                var cncHeader = Headers[ServicePoint.UsesProxy ? "Proxy-Connection" : "Connection"];
                if (cncHeader != null)
                {
                    cncHeader = cncHeader.ToLower();
                    KeepAlive = cncHeader.IndexOf("keep-alive", StringComparison.Ordinal) != -1;
                    if (cncHeader.IndexOf("close", StringComparison.Ordinal) != -1)
                    {
                        KeepAlive = false;
                    }
                }
            }

            // Negative numbers?
            if (!Int32.TryParse(clength, out stream_length))
            {
                stream_length = -1;
            }

            string me        = "WebResponseStream.Initialize()";
            string tencoding = null;

            if (ExpectContent)
            {
                tencoding = Headers["Transfer-Encoding"];
            }

            ChunkedRead = (tencoding != null && tencoding.IndexOf("chunked", StringComparison.OrdinalIgnoreCase) != -1);
            if (!ChunkedRead)
            {
                readBuffer = buffer;
                try {
                    if (contentLength > 0 && readBuffer.Size >= contentLength)
                    {
                        if (!IsNtlmAuth())
                        {
                            await ReadAllAsync(false, cancellationToken).ConfigureAwait(false);
                        }
                    }
                } catch (Exception e) {
                    throw GetReadException(WebExceptionStatus.ReceiveFailure, e, me);
                }
            }
            else if (ChunkStream == null)
            {
                try {
                    ChunkStream = new MonoChunkStream(buffer.Buffer, buffer.Offset, buffer.Offset + buffer.Size, Headers);
                } catch (Exception e) {
                    throw GetReadException(WebExceptionStatus.ServerProtocolViolation, e, me);
                }
            }
            else
            {
                ChunkStream.ResetBuffer();
                try {
                    ChunkStream.Write(buffer.Buffer, buffer.Offset, buffer.Size);
                } catch (Exception e) {
                    throw GetReadException(WebExceptionStatus.ServerProtocolViolation, e, me);
                }
            }

            WebConnection.Debug($"{ME} INIT #1: - {ExpectContent} {closed} {nextReadCalled}");

            if (!ExpectContent)
            {
                if (!closed && !nextReadCalled)
                {
                    if (contentLength == Int64.MaxValue)
                    {
                        contentLength = 0;
                    }
                    nextReadCalled = true;
                }
                Operation.Finish(true);
            }
        }
        private void Connect(HttpWebRequest request)
        {
            object obj = this.socketLock;

            lock (obj)
            {
                if (this.socket != null && this.socket.Connected && this.status == WebExceptionStatus.Success && this.CanReuse() && this.CompleteChunkedRead())
                {
                    this.reused = true;
                }
                else
                {
                    this.reused = false;
                    if (this.socket != null)
                    {
                        this.socket.Close();
                        this.socket = null;
                    }
                    this.chunkStream = null;
                    IPHostEntry hostEntry = this.sPoint.HostEntry;
                    if (hostEntry == null)
                    {
                        this.status = ((!this.sPoint.UsesProxy) ? WebExceptionStatus.NameResolutionFailure : WebExceptionStatus.ProxyNameResolutionFailure);
                    }
                    else
                    {
                        WebConnectionData data = this.Data;
                        foreach (IPAddress ipaddress in hostEntry.AddressList)
                        {
                            this.socket = new System.Net.Sockets.Socket(ipaddress.AddressFamily, System.Net.Sockets.SocketType.Stream, System.Net.Sockets.ProtocolType.Tcp);
                            IPEndPoint ipendPoint = new IPEndPoint(ipaddress, this.sPoint.Address.Port);
                            this.socket.SetSocketOption(System.Net.Sockets.SocketOptionLevel.Tcp, System.Net.Sockets.SocketOptionName.Debug, (!this.sPoint.UseNagleAlgorithm) ? 1 : 0);
                            this.socket.NoDelay = !this.sPoint.UseNagleAlgorithm;
                            if (!this.sPoint.CallEndPointDelegate(this.socket, ipendPoint))
                            {
                                this.socket.Close();
                                this.socket = null;
                                this.status = WebExceptionStatus.ConnectFailure;
                            }
                            else
                            {
                                try
                                {
                                    if (request.Aborted)
                                    {
                                        break;
                                    }
                                    this.CheckUnityWebSecurity(request);
                                    this.socket.Connect(ipendPoint, false);
                                    this.status = WebExceptionStatus.Success;
                                    break;
                                }
                                catch (ThreadAbortException)
                                {
                                    System.Net.Sockets.Socket socket = this.socket;
                                    this.socket = null;
                                    if (socket != null)
                                    {
                                        socket.Close();
                                    }
                                    break;
                                }
                                catch (ObjectDisposedException ex)
                                {
                                    break;
                                }
                                catch (Exception ex2)
                                {
                                    System.Net.Sockets.Socket socket2 = this.socket;
                                    this.socket = null;
                                    if (socket2 != null)
                                    {
                                        socket2.Close();
                                    }
                                    if (!request.Aborted)
                                    {
                                        this.status = WebExceptionStatus.ConnectFailure;
                                    }
                                    this.connect_exception = ex2;
                                }
                            }
                        }
                    }
                }
            }
        }
Example #13
0
        private ChunkStream.State GetChunkSize(byte[] buffer, ref int offset, int size)
        {
            char c = '\0';

            while (offset < size)
            {
                c = (char)buffer[offset++];
                if (c == '\r')
                {
                    if (this.sawCR)
                    {
                        ChunkStream.ThrowProtocolViolation("2 CR found");
                    }
                    this.sawCR = true;
                }
                else
                {
                    if (this.sawCR && c == '\n')
                    {
                        break;
                    }
                    if (c == ' ')
                    {
                        this.gotit = true;
                    }
                    if (!this.gotit)
                    {
                        this.saved.Append(c);
                    }
                    if (this.saved.Length > 20)
                    {
                        ChunkStream.ThrowProtocolViolation("chunk size too long.");
                    }
                }
            }
            if (!this.sawCR || c != '\n')
            {
                if (offset < size)
                {
                    ChunkStream.ThrowProtocolViolation("Missing \\n");
                }
                try
                {
                    if (this.saved.Length > 0)
                    {
                        this.chunkSize = int.Parse(ChunkStream.RemoveChunkExtension(this.saved.ToString()), NumberStyles.HexNumber);
                    }
                }
                catch (Exception)
                {
                    ChunkStream.ThrowProtocolViolation("Cannot parse chunk size.");
                }
                return(ChunkStream.State.None);
            }
            this.chunkRead = 0;
            try
            {
                this.chunkSize = int.Parse(ChunkStream.RemoveChunkExtension(this.saved.ToString()), NumberStyles.HexNumber);
            }
            catch (Exception)
            {
                ChunkStream.ThrowProtocolViolation("Cannot parse chunk size.");
            }
            if (this.chunkSize == 0)
            {
                this.trailerState = 2;
                return(ChunkStream.State.Trailer);
            }
            return(ChunkStream.State.Body);
        }
Example #14
0
        void ReadDone(IAsyncResult result)
        {
            WebConnectionData data = Data;
            Stream            ns   = nstream;

            if (ns == null)
            {
                Close(true);
                return;
            }

            int nread = -1;

            try {
                nread = ns.EndRead(result);
            } catch (ObjectDisposedException) {
                return;
            } catch (Exception e) {
                if (e.InnerException is ObjectDisposedException)
                {
                    return;
                }

                HandleError(WebExceptionStatus.ReceiveFailure, e, "ReadDone1");
                return;
            }

            if (nread == 0)
            {
                HandleError(WebExceptionStatus.ReceiveFailure, null, "ReadDone2");
                return;
            }

            if (nread < 0)
            {
                HandleError(WebExceptionStatus.ServerProtocolViolation, null, "ReadDone3");
                return;
            }

            int pos = -1;

            nread += position;
            if (data.ReadState == ReadState.None)
            {
                Exception exc = null;
                try {
                    pos = GetResponse(data, sPoint, buffer, nread);
                } catch (Exception e) {
                    exc = e;
                }

                if (exc != null || pos == -1)
                {
                    HandleError(WebExceptionStatus.ServerProtocolViolation, exc, "ReadDone4");
                    return;
                }
            }

            if (data.ReadState == ReadState.Aborted)
            {
                HandleError(WebExceptionStatus.RequestCanceled, null, "ReadDone");
                return;
            }

            if (data.ReadState != ReadState.Content)
            {
                int     est       = nread * 2;
                int     max       = (est < buffer.Length) ? buffer.Length : est;
                byte [] newBuffer = new byte [max];
                Buffer.BlockCopy(buffer, 0, newBuffer, 0, nread);
                buffer         = newBuffer;
                position       = nread;
                data.ReadState = ReadState.None;
                InitRead();
                return;
            }

            position = 0;

            WebConnectionStream stream = new WebConnectionStream(this, data);
            bool   expect_content      = ExpectContent(data.StatusCode, data.request.Method);
            string tencoding           = null;

            if (expect_content)
            {
                tencoding = data.Headers ["Transfer-Encoding"];
            }

            chunkedRead = (tencoding != null && tencoding.IndexOf("chunked", StringComparison.OrdinalIgnoreCase) != -1);
            if (!chunkedRead)
            {
                stream.ReadBuffer       = buffer;
                stream.ReadBufferOffset = pos;
                stream.ReadBufferSize   = nread;
                try {
                    stream.CheckResponseInBuffer();
                } catch (Exception e) {
                    HandleError(WebExceptionStatus.ReceiveFailure, e, "ReadDone7");
                }
            }
            else if (chunkStream == null)
            {
                try {
                    chunkStream = new ChunkStream(buffer, pos, nread, data.Headers);
                } catch (Exception e) {
                    HandleError(WebExceptionStatus.ServerProtocolViolation, e, "ReadDone5");
                    return;
                }
            }
            else
            {
                chunkStream.ResetBuffer();
                try {
                    chunkStream.Write(buffer, pos, nread);
                } catch (Exception e) {
                    HandleError(WebExceptionStatus.ServerProtocolViolation, e, "ReadDone6");
                    return;
                }
            }

            data.stream = stream;

            if (!expect_content)
            {
                stream.ForceCompletion();
            }

            data.request.SetResponseData(data);
        }