HTTP request header event arguments
Inheritance: HttpHeaderEventArgs
        public void Http11HasBody()
        {
            Mock<IHttpHeaders> mockHeaders = new Mock<IHttpHeaders>();

            HttpHeaderEventArgs args = new HttpRequestHeaderEventArgs("1.1", mockHeaders.Object, "get", "/");

            Assert.That(args.HasBody, Is.False);
        }
        public void IsSslTest()
        {
            List<KeyValuePair<string, string>> headerList = new List<KeyValuePair<string, string>>();
            headerList.Add(new KeyValuePair<string, string>("Host", "www.yahoo.com"));
            headerList.Add(new KeyValuePair<string, string>("Proxy-Connection", "keep-alive"));
            HttpHeaders headers = new HttpHeaders(headerList);

            var objectUnderTest = new HttpRequestHeaderEventArgs("1.1", headers, "cOnNeCt", "http://www.yahoo.com:443");

            Assert.That(objectUnderTest.IsSsl, Is.True);
        }
        public void CreateBufferTest()
        {
            List<KeyValuePair<string, string>> headerList = new List<KeyValuePair<string, string>>();
            headerList.Add(new KeyValuePair<string, string>("Host", "www.yahoo.com"));
            headerList.Add(new KeyValuePair<string, string>("Proxy-Connection", "keep-alive"));
            HttpHeaders headers = new HttpHeaders(headerList);

            var objectUnderTest = new HttpRequestHeaderEventArgs("1.1", headers, "PUT", "/");

            byte[] headerBytes = objectUnderTest.GetBuffer();

            string expectedHeader = "PUT / HTTP/1.1\r\nHost: www.yahoo.com\r\nConnection: keep-alive\r\n\r\n";
            var expectedBytes = Encoding.UTF8.GetBytes(expectedHeader);

            Assert.That(headerBytes, Is.EqualTo(expectedBytes));
        }
        public void ConvertProxyConnectionHeaderInHttp11()
        {
            List<KeyValuePair<string, string>> headerList = new List<KeyValuePair<string, string>>();
            headerList.Add(new KeyValuePair<string, string>("Host", "www.yahoo.com"));
            headerList.Add(new KeyValuePair<string, string>("Proxy-Connection", "keep-alive"));
            headerList.Add(new KeyValuePair<string, string>("Foo", "bar"));
            HttpHeaders headers = new HttpHeaders(headerList);

            var objectUnderTest = new HttpRequestHeaderEventArgs("1.1", headers, "GET", "http://www.yahoo.com/foo.html");

            Assert.That(objectUnderTest.Headers.Count, Is.EqualTo(3));
            Assert.That(objectUnderTest.Headers["Connection"], Is.EqualTo("keep-alive"));

            var buffer = objectUnderTest.GetBuffer();

            // This test uses three headers to verify that the Connection header is modified in-place
            // and not moved in the list. This is important because a proxy server cannot change
            // the header order according to HTTP spec.
            Assert.That(Encoding.UTF8.GetString(buffer), Is.EqualTo("GET /foo.html HTTP/1.1\r\nHost: www.yahoo.com\r\nConnection: keep-alive\r\nFoo: bar\r\n\r\n"));
        }
        public void RemoveProxyConnectionHeaderInHttp10()
        {
            List<KeyValuePair<string,string> > headerList = new List<KeyValuePair<string, string>>();
            headerList.Add(new KeyValuePair<string, string>("Proxy-Connection", "keep-alive"));
            HttpHeaders headers = new HttpHeaders(headerList);

            var objectUnderTest = new HttpRequestHeaderEventArgs( "1.0", headers, "GET", "http://www.yahoo.com/foo.html" );

            Assert.That(objectUnderTest.Headers.Count, Is.EqualTo(0));

            var buffer = objectUnderTest.GetBuffer();

            Assert.That(Encoding.UTF8.GetString(buffer), Is.EqualTo("GET /foo.html HTTP/1.0\r\n\r\n") );
        }
        private void HandleParserReadRequestHeaderComplete( object sender, HttpRequestHeaderEventArgs e )
        {
            ServiceLog.Logger.Verbose( "{0} ClientSession -- read HTTP request header from client\r\n{1}",
                                       Id,
                                       Encoding.UTF8.GetString( e.GetBuffer() ) );

            try
            {
                // TODO: apply filter here

                _lastRequest = HttpRequest.CreateRequest( e );

                _accessLog.Write( _clientConnection.ConnectionId, _lastRequest, "Connect OK" );

                if ( _lastRequest.IsSsl )
                {
                    ServiceLog.Logger.Info( "{0} HTTPS connection", Id );

                    ResetParser();

                    string host;
                    int port;

                    if ( ServerDispatcher.TryParseAddress( _lastRequest, out host, out port ) )
                    {
                        ServiceLog.Logger.Info( "{0} HTTPS host: {1}:{2}", Id, host, port );
                        _facadeFactory.BeginConnect( host, port, HttpsServerConnect );
                    }
                    else
                    {
                        ServiceLog.Logger.Warning( "{0} Unrecognized HTTPS address. Resetting session.", Id );
                        Reset();
                    }
                }
                else
                {
                    // Hold off sending data until the connection is established
                    _connectToServerEvent.Reset();

                    _dispatcher.BeginConnect( _lastRequest, HandleServerConnect );
                }
            }
            catch ( Exception ex )
            {
                ServiceLog.Logger.Exception( string.Format( "{0} Unhandled exception connecting to remote host.", Id ), ex );
                Reset();
            }
        }
        //private bool HasHostChanged( HttpRequestHeaderEventArgs newRequest )
        //{
        //    if ( RecentRequestHeader == null )
        //    {
        //        return false;
        //    }
        //    HttpRequest request = HttpRequest.CreateRequest( newRequest );
        //    int port;
        //    string host;
        //    if ( SessionStateUtils.TryParseAddress( request, out host, out port ) )
        //    {
        //        return ( RecentRequestHeader.IsSsl != request.IsSsl ||
        //                 Host != host || Port != port );
        //    }
        //    throw new InvalidDataException( "Malformed HTTP request" );
        //}
        //private void WaitForEmptyPipeline()
        //{
        //    while ( HttpPipelineDepth > 0 )
        //    {
        //        ServiceLog.Logger.Verbose( "{0} Waiting for pipeline to clear. Pipeline depth: {1}", Id, HttpPipelineDepth );
        //        _pipelineUpdateEvent.WaitOne();
        //    }
        //}
        private void HandleClientParserReadRequestHeaderComplete( object sender, HttpRequestHeaderEventArgs e )
        {
            Contract.Requires( e != null );

            ServiceLog.Logger.Verbose( "{0} SessionContext::HandleClientParserReadRequestHeaderComplete", Id );

            try
            {
                // Race condition: it's possible that the existing connection is being evaluated for persistence
                // when a new, concurrent, pipelined request is coming from the client. This new request could
                // have a different host/port but we cannot change the active server connection until the existing
                // pipeline is empty.

                //if ( HasHostChanged( e ) )
                //{
                //    ServiceLog.Logger.Info( "{0} Client has changed hosts. Waiting for pipeline to clear before reconnecting...", Id );
                //    WaitForEmptyPipeline();
                //    ServiceLog.Logger.Info( "{0} Pipeline cleared. Continuing with request.", Id );
                //}

                IHttpRequest request = HttpRequest.CreateRequest( e );

                lock ( _changeClientConnectionMutex )
                {
                    if (ClientConnection != null)
                    {
                        State.RequestHeaderAvailable(request, this);
                    }
                }

                HttpPipelineDepth++;

                // Update the recent request header AFTER sending the request header to the state so the state
                // can compare host changes
                RecentRequestHeader = request;
            }
            catch ( Exception ex )
            {
                ServiceLog.Logger.Exception(
                    string.Format( "{0} Unhandled exception when handling request header", Id ), ex );
                ChangeState( SessionStateType.Error );
            }
        }
Beispiel #8
0
        private void ParserReadRequestHeaderComplete( object sender, HttpRequestHeaderEventArgs e )
        {
            _logger.Verbose( () => string.Format( "Read HTTP request header\r\n{0}", Encoding.UTF8.GetString( e.GetBuffer() ) ) );

            try
            {
                _serverConnectingEvent.WaitOne();

                _lastRequest = HttpRequest.CreateRequest( e );

                string host;
                int port;

                if ( TryParseAddress( _lastRequest, out host, out port ) )
                {
                    var filterResults = _filter.ApplyConnectionFilters( _lastRequest, _connection.Id );

                    if (filterResults != null)
                    {
                        _logger.Info("Connection filter rejected connection");

                        _connection.SendData(filterResults);
                        Reset();
                    }
                    else
                    {
                        _logger.Info(string.Format("Connecting to {0}:{1}", host, port));

                        if (_lastRequest.IsSsl)
                        {
                            EstablishSslConnection(host, port, _lastRequest.Version);
                        }
                        else
                        {
                            var responseFilter = _filter.CreateResponseFilters( _lastRequest, _connection.Id );
                            _serverDispatcher.ConnectToServer(host, port, responseFilter, ServerConnected);
                        }
                    }
                }
                else
                {
                    _logger.Error( "Request header not a recognized format" );
                    Reset();
                }
            }
            catch ( Exception ex )
            {
                _logger.Exception( "Unhandled exception parsing request header", ex );
                Reset();
            }
        }
Beispiel #9
0
 internal static HttpRequest CreateRequest( HttpRequestHeaderEventArgs args )
 {
     Contract.Requires( args != null );
     return new HttpRequest( args );
 }
Beispiel #10
0
 private HttpRequest( HttpRequestHeaderEventArgs args )
 {
     Contract.Requires(args!=null);
     _args = args;
 }