public void HandleRemainingBytes( SocketClient client, ArraySegment<byte> bytes, bool requestCanHaveBody ) 
 {
     if( requestCanHaveBody )
         HandleNextRead( client.Id, bytes );
     else
         client.ReadBytes = bytes;
 }
 public void RequestComplete( SocketClient client ) 
 {
     if( client.Request.KeepAlive )
     {
         if(client.ReadBytes.Offset >= client.ReadBytes.Count - 1 )
         {
             client.Socket.Read();
             //client.Socket.Close();
         }
         else
             HandleNextRead( client.Id, client.ReadBytes );
     }
     else
         client.Socket.Close();
 }
 public void ReadOrComplete( SocketClient client ) 
 {
     var expected = client.Request.GetExpectedBodyLength();
     var readComplete = expected > 0 && client.BytesRead >= expected;
     if( readComplete )
         client.ReadComplete();
     else
         client.Socket.Read();
 }
        public void ParseRequest( SocketClient client, ArraySegment<byte> bytes ) 
        {
            client.Next = ( client.Request == null || !client.Request.HeadersComplete ) 
                ? client.Next 
                : CreateApplication;

            client.Request = client.Request ?? new Request( 
                ( remaining, canHaveBody ) => HandleRemainingBytes( client, remaining, canHaveBody ) );

            RequestParser.PopulateRequest( client.Request, bytes );

            if( !client.Request.CanHaveBody && client.Request.HeadersComplete )
            {
                CreateApplication( client, new ArraySegment<byte>() );
                client.ReadComplete();
            }
        }
        public void CreateApplication( SocketClient client, ArraySegment<byte> bytes ) 
        {
            client.Application = Router.GetApplicationFor( client.Request );
            var responseHelper = new ResponseHelper( Configuration );
            var writer = new ResponseWriter( client.Socket, responseHelper.PendingWrite, () => RequestComplete( client ) );
            
            responseHelper.Initialize( 
                    ( status, headers, setup ) =>
                    {
                        writer.Begin( status, headers );
                        setup( writer.OnNext, writer.OnError, writer.OnComplete );
                    }
                );
            
            client.Request.Body = 
                ( next, error, complete ) =>
                {
                    client.Next = ( c, b ) =>
                    {
                        if( b.Count == 0 )
                            client.ReadComplete();
                        else if( !next( b, () => ReadOrComplete( c ) ) )
                            ReadOrComplete( c );
                    };
                    client.ReadError = error;
                    client.ReadComplete = () => 
                    {
                        client.ReadComplete = () => { };
                        client.Next = ParseRequest;
                        client.Request = new Request( 
                            ( remaining, canHaveBody ) => HandleRemainingBytes( client, remaining, canHaveBody ) );
                        complete();
                    };
                    return () => client.Socket.Close();
                };

            client.Request.Response = responseHelper;

            client.Application.Run( 
                client.Request.ToEnvironmentDictionary(),
                responseHelper.Respond,
                Console.WriteLine );

            client.Next( client, bytes );
        }