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();
            }
        }
Exemple #5
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();
            }
        }