Exemple #1
0
 protected abstract void ParseAndAttachToBody_Callback(Tcp.TcpConnection sender, Tcp.TcpConnectionAsyncEventArgs e);
        protected override void ParseAndAttachToBody_Callback(Tcp.TcpConnection sender, Tcp.TcpConnectionAsyncEventArgs e)
        {
            HttpNetworkStream ns;

            byte[] newBuffer = null;

            AsyncCallback callback = (AsyncCallback)e.UserToken;

            AppendAndParse(e.Buffer, 0, e.BytesTransferred);
            if (AllHeadersReceived)
            {
                // Sets up the buffer to prepend
                if (_remainingBufferAppendPosition > 0)
                {
                    // We need to take the left over buffer from _responseBuilder and prepend that
                    // to an HttpNetworkStream wrapping the _tcpConnection and then give the user
                    // that HttpNetworkStream... cake

                    newBuffer      = new byte[_remainingBufferAppendPosition];
                    BytesReceived += e.BytesTransferred - newBuffer.Length;
                    Buffer.BlockCopy(_remainingBuffer, 0, newBuffer, 0, newBuffer.Length);
                }
                else
                {
                    newBuffer = null;
                }



                if (!Response.ContentLength.HasValue)
                { // If content length is not set
                    if (!Response.Headers.ContainsKey(new Message.ChunkedTransferEncodingHeader()))
                    {
                        throw new HttpNetworkStreamException("A Content-Length header was not found.");
                    }

                    MessageSize = 0;

                    // NetworkStream needs modified to handle undetermined length
                    ns = new HttpNetworkStream(HttpNetworkStream.DirectionType.Download, newBuffer,
                                               sender.Socket, System.IO.FileAccess.Read, false);

                    Response.Body.IsChunked     = true;
                    Response.Body.ReceiveStream = new Interceptors.InterceptorStream(new Interceptors.ChunkedEncodingInterceptor(ns));
                }
                else
                { // Content length is set
                    if (Response.Headers.ContainsKey(new Message.ChunkedTransferEncodingHeader()))
                    {
                        throw new HttpNetworkStreamException("A Content-Length header was found in a chunked transfer.");
                    }

                    ulong temp = (ulong)Response.ContentLength.Value;
                    MessageSize = BytesReceived + Response.ContentLength.Value;

                    ns = new HttpNetworkStream(HttpNetworkStream.DirectionType.Download,
                                               temp, newBuffer, sender.Socket, System.IO.FileAccess.Read, false);
                    Response.Body.ReceiveStream = ns;
                }


                callback(this, Response);
            }
            else
            {
                BytesReceived += e.BytesTransferred;
                sender.ReceiveAsync(ParseAndAttachToBody_Callback, callback);
            }
        }
        protected override void ParseAndAttachToBody_Callback(Tcp.TcpConnection sender, Tcp.TcpConnectionAsyncEventArgs e)
        {
            HttpNetworkStream ns;

            AsyncCallback callback = (AsyncCallback)e.UserToken;

            AppendAndParse(e.Buffer, 0, e.Length);
            if (AllHeadersReceived)
            {
                if (!Request.ContentLength.HasValue)
                {
                    throw new HttpNetworkStreamException("A Content-Length header was not found.");
                }

                ulong temp = (ulong)Request.ContentLength.Value;

                if (_remainingBufferAppendPosition > 0)
                {
                    // We need to take the left over buffer from _responseBuilder and prepend that
                    // to an HttpNetworkStream wrapping the _tcpConnection and then give the user
                    // that HttpNetworkStream... cake

                    byte[] newBuffer = new byte[_remainingBufferAppendPosition];

                    BytesReceived += e.BytesTransferred - newBuffer.Length;
                    MessageSize    = BytesReceived + Request.ContentLength.Value;
                    Buffer.BlockCopy(_remainingBuffer, 0, newBuffer, 0, newBuffer.Length);

                    ns = new HttpNetworkStream(HttpNetworkStream.DirectionType.Upload,
                                               temp, newBuffer, sender.Socket, System.IO.FileAccess.Write, false);
                }
                else
                {
                    BytesReceived += e.BytesTransferred;
                    MessageSize    = BytesReceived + Response.ContentLength.Value;

                    ns = new HttpNetworkStream(HttpNetworkStream.DirectionType.Upload,
                                               temp, sender.Socket, System.IO.FileAccess.Write, false);
                }

                if (Request.Headers.ContainsKey(new Message.ChunkedTransferEncodingHeader()))
                {
                    Request.Body.IsChunked = true;
                }

                Response.Body.ReceiveStream = ns;

                callback(this, Response);
            }
            else
            {
                BytesReceived += e.Length;
                sender.ReceiveAsync(ParseAndAttachToBody_Callback, callback);
            }
        }