commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject cvs commit: jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/server SimpleConnPool.java HttpRequestHandlerChain.java HttpServiceHandler.java ProxyRequestHandler.java ResponseWriter.java SimpleHttpServer.java SimpleHttpServerConnection.java SimpleProxy.java SimpleResponse.java
Date Sun, 07 Nov 2004 12:31:42 GMT
olegk       2004/11/07 04:31:42

  Modified:    httpclient/src/test/org/apache/commons/httpclient
                        HttpClientTestBase.java TestBadContentLength.java
                        TestIdleConnectionTimeout.java TestNoHost.java
                        TestRedirects.java TestResponseHeaders.java
               httpclient/src/test/org/apache/commons/httpclient/auth
                        TestBasicAuth.java TestDigestAuth.java
                        TestNTLMAuth.java
               httpclient/src/test/org/apache/commons/httpclient/server
                        HttpRequestHandlerChain.java
                        HttpServiceHandler.java ProxyRequestHandler.java
                        ResponseWriter.java SimpleHttpServer.java
                        SimpleHttpServerConnection.java SimpleProxy.java
                        SimpleResponse.java
  Added:       httpclient/src/test/org/apache/commons/httpclient
                        TestConnectionPersistence.java
               httpclient/src/test/org/apache/commons/httpclient/server
                        SimpleConnPool.java
  Log:
  Connection keep-alive support in SimpleHttpServer
  
  Contributed by Oleg Kalnichevski
  
  Revision  Changes    Path
  1.7       +9 -4      jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/HttpClientTestBase.java
  
  Index: HttpClientTestBase.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/HttpClientTestBase.java,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- HttpClientTestBase.java	4 Nov 2004 12:08:53 -0000	1.6
  +++ HttpClientTestBase.java	7 Nov 2004 12:31:42 -0000	1.7
  @@ -37,6 +37,8 @@
   import org.apache.commons.httpclient.protocol.Protocol;
   import org.apache.commons.httpclient.server.SimpleHttpServer;
   import org.apache.commons.httpclient.server.SimpleProxy;
  +import org.apache.commons.logging.Log;
  +import org.apache.commons.logging.LogFactory;
   
   /**
    * Base class for test cases using 
  @@ -49,6 +51,8 @@
    */
   public class HttpClientTestBase extends TestCase {
   
  +    private static final Log LOG = LogFactory.getLog(HttpClientTestBase.class);
  +    
       protected HttpClient client = null;
       protected SimpleHttpServer server = null;
   
  @@ -80,6 +84,7 @@
   
       public void setUp() throws IOException {
           this.server = new SimpleHttpServer(); // use arbitrary port
  +        this.server.setTestname(getName());
   
           this.client = new HttpClient();
           this.client.getHostConfiguration().setHost(
  
  
  
  1.8       +5 -4      jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestBadContentLength.java
  
  Index: TestBadContentLength.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestBadContentLength.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- TestBadContentLength.java	13 Jun 2004 20:22:19 -0000	1.7
  +++ TestBadContentLength.java	7 Nov 2004 12:31:42 -0000	1.8
  @@ -80,6 +80,7 @@
       public void setUp() throws IOException {
           client = new HttpClient();
           server = new SimpleHttpServer(); // use arbitrary port
  +        server.setTestname(getName());
           server.setRequestHandler(new MyHttpRequestHandler());
       }
   
  
  
  
  1.5       +11 -3     jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestIdleConnectionTimeout.java
  
  Index: TestIdleConnectionTimeout.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestIdleConnectionTimeout.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- TestIdleConnectionTimeout.java	31 Oct 2004 13:46:54 -0000	1.4
  +++ TestIdleConnectionTimeout.java	7 Nov 2004 12:31:42 -0000	1.5
  @@ -28,7 +28,9 @@
    */
   package org.apache.commons.httpclient;
   
  +import junit.framework.Test;
   import junit.framework.TestCase;
  +import junit.framework.TestSuite;
   
   import org.apache.commons.httpclient.params.HttpConnectionManagerParams;
   import org.apache.commons.httpclient.util.IdleConnectionHandler;
  @@ -50,6 +52,12 @@
           super(arg0);
       }
       
  +    // ------------------------------------------------------- TestCase Methods
  +
  +    public static Test suite() {
  +        return new TestSuite(TestIdleConnectionTimeout.class);
  +    }
  +
       /**
        * Tests that the IdleConnectionHandler correctly closes connections.
        */
  
  
  
  1.45      +6 -5      jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestNoHost.java
  
  Index: TestNoHost.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestNoHost.java,v
  retrieving revision 1.44
  retrieving revision 1.45
  diff -u -r1.44 -r1.45
  --- TestNoHost.java	31 Oct 2004 13:46:54 -0000	1.44
  +++ TestNoHost.java	7 Nov 2004 12:31:42 -0000	1.45
  @@ -82,7 +82,8 @@
           // Redirects
           suite.addTest(TestRedirects.suite());
           // Connection management
  -        suite.addTestSuite(TestIdleConnectionTimeout.class);
  +        suite.addTest(TestConnectionPersistence.suite());
  +        suite.addTest(TestIdleConnectionTimeout.suite());
           suite.addTest(TestMethodAbort.suite());
           // Preferences
           suite.addTest(TestHttpParams.suite());
  
  
  
  1.7       +6 -6      jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestRedirects.java
  
  Index: TestRedirects.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestRedirects.java,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- TestRedirects.java	1 Nov 2004 02:21:15 -0000	1.6
  +++ TestRedirects.java	7 Nov 2004 12:31:42 -0000	1.7
  @@ -24,8 +24,6 @@
    * information on the Apache Software Foundation, please see
    * <http://www.apache.org/>.
    *
  - * [Additional notices, if required by prior licensing conditions]
  - *
    */
   
   package org.apache.commons.httpclient;
  @@ -103,6 +101,7 @@
                       response.setStatusLine(ver, this.statuscode);
                       response.addHeader(new Header("Location", 
                       		"http://" + this.host + ":" + this.port + "/newlocation/"));
  +                    response.addHeader(new Header("Connection", "close"));
                   } else if (reqline.getUri().equals("/newlocation/")) {
                       response.setStatusLine(ver, HttpStatus.SC_OK);
                       response.setBodyString("Successful redirect");
  @@ -436,6 +435,7 @@
           SimpleHttpServer thatserver = new SimpleHttpServer();
           this.server.setHttpService(new BasicRedirectService(host, port));
           thatserver.setHttpService(new BasicRedirectService(host, port));
  +        thatserver.setTestname(getName());
           
           HostConfiguration hostconfig = new HostConfiguration();
           hostconfig.setHost(
  
  
  
  1.18      +4 -41     jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestResponseHeaders.java
  
  Index: TestResponseHeaders.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestResponseHeaders.java,v
  retrieving revision 1.17
  retrieving revision 1.18
  diff -u -r1.17 -r1.18
  --- TestResponseHeaders.java	31 Oct 2004 14:42:59 -0000	1.17
  +++ TestResponseHeaders.java	7 Nov 2004 12:31:42 -0000	1.18
  @@ -124,43 +124,6 @@
           assertNotNull( "Response body is null.", method.getResponseBodyAsStream() );
       }
   
  -    public void testDuplicateProxyConnection() throws Exception {
  -        
  -        client.getHostConfiguration().setProxy(server.getLocalAddress(), server.getLocalPort());
  -        this.server.setHttpService(new HttpService() {
  -            public boolean process(SimpleRequest request,
  -                    SimpleResponse response) throws IOException {
  -                response.setStatusLine(request.getRequestLine().getHttpVersion(), 200);
  -                response.addHeader(new Header("proxy-connection", "close"));
  -                response.addHeader(new Header("proxy-connection", "close"));
  -                return true;
  -            }
  -        });
  -
  -        GetMethod method = new GetMethod("/");
  -        client.executeMethod(method);
  -        method.getResponseBodyAsString();
  -        
  -        assertFalse(connectionManager.getConection().isOpen());
  -        
  -        this.server.setHttpService(new HttpService() {
  -            public boolean process(SimpleRequest request,
  -                    SimpleResponse response) throws IOException {
  -                response.setStatusLine(HttpVersion.HTTP_1_0, 200);
  -                response.addHeader(new Header("proxy-connection", "keep-alive"));
  -                response.addHeader(new Header("proxy-connection", "keep-alive"));
  -                response.setBodyString("aa");
  -                return true;
  -            }
  -        });
  -
  -        method = new GetMethod("/");
  -        client.executeMethod(method);
  -        method.getResponseBodyAsString();
  -        
  -        assertTrue(connectionManager.getConection().isOpen());
  -    }
  -
       public void testDuplicateConnection() throws Exception {
           
           this.server.setHttpService(new HttpService() {
  
  
  
  1.1                  jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestConnectionPersistence.java
  
  Index: TestConnectionPersistence.java
  ===================================================================
  /*
   * $Header: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestConnectionPersistence.java,v
1.1 2004/11/07 12:31:42 olegk Exp $
   * $Revision: 1.1 $
   * $Date: 2004/11/07 12:31:42 $
   * ====================================================================
   *
   *  Copyright 2002-2004 The Apache Software Foundation
   *
   *  Licensed under the Apache License, Version 2.0 (the "License");
   *  you may not use this file except in compliance with the License.
   *  You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
   *
   *  Unless required by applicable law or agreed to in writing, software
   *  distributed under the License is distributed on an "AS IS" BASIS,
   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   *  See the License for the specific language governing permissions and
   *  limitations under the License.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  
  package org.apache.commons.httpclient;
  
  import java.io.IOException;
  
  import org.apache.commons.httpclient.methods.PostMethod;
  import org.apache.commons.httpclient.methods.StringRequestEntity;
  
  import junit.framework.Test;
  import junit.framework.TestSuite;
  
  /**
   * Connection persistence tests
   * 
   * @author Oleg Kalnichevski
   *
   * @version $Id: TestConnectionPersistence.java,v 1.1 2004/11/07 12:31:42 olegk Exp $
   */
  public class TestConnectionPersistence extends HttpClientTestBase {
      
      // ------------------------------------------------------------ Constructor
      public TestConnectionPersistence(final String testName) throws IOException {
          super(testName);
      }
  
      // ------------------------------------------------------------------- Main
      public static void main(String args[]) {
          String[] testCaseName = { TestConnectionPersistence.class.getName() };
          junit.textui.TestRunner.main(testCaseName);
      }
  
      // ------------------------------------------------------- TestCase Methods
  
      public static Test suite() {
          return new TestSuite(TestConnectionPersistence.class);
      }
  
      // ----------------------------------------------------------- Test Methods
  
      public void testConnPersisenceHTTP10() throws Exception {
          this.server.setHttpService(new EchoService());
  
          AccessibleHttpConnectionManager connman = new AccessibleHttpConnectionManager();
          
          this.client.getParams().setVersion(HttpVersion.HTTP_1_0);
          this.client.setHttpConnectionManager(connman);
          
          PostMethod httppost = new PostMethod("/test/");
          httppost.setRequestEntity(new StringRequestEntity("stuff"));
          try {
              this.client.executeMethod(httppost);
          } finally {
              httppost.releaseConnection();
          }
          assertFalse(connman.getConection().isOpen());
  
          httppost = new PostMethod("/test/");
          httppost.setRequestEntity(new StringRequestEntity("more stuff"));
          try {
              this.client.executeMethod(httppost);
          } finally {
              httppost.releaseConnection();
          }
          assertFalse(connman.getConection().isOpen());
      }
  
      public void testConnPersisenceHTTP11() throws Exception {
          this.server.setHttpService(new EchoService());
  
          AccessibleHttpConnectionManager connman = new AccessibleHttpConnectionManager();
          
          this.client.getParams().setVersion(HttpVersion.HTTP_1_1);
          this.client.setHttpConnectionManager(connman);
          
          PostMethod httppost = new PostMethod("/test/");
          httppost.setRequestEntity(new StringRequestEntity("stuff"));
          try {
              this.client.executeMethod(httppost);
          } finally {
              httppost.releaseConnection();
          }
          assertTrue(connman.getConection().isOpen());
  
          httppost = new PostMethod("/test/");
          httppost.setRequestEntity(new StringRequestEntity("more stuff"));
          try {
              this.client.executeMethod(httppost);
          } finally {
              httppost.releaseConnection();
          }
          assertTrue(connman.getConection().isOpen());
      }
  
      public void testConnClose() throws Exception {
          this.server.setHttpService(new EchoService());
  
          AccessibleHttpConnectionManager connman = new AccessibleHttpConnectionManager();
          
          this.client.getParams().setVersion(HttpVersion.HTTP_1_1);
          this.client.setHttpConnectionManager(connman);
          
          PostMethod httppost = new PostMethod("/test/");
          httppost.setRequestEntity(new StringRequestEntity("stuff"));
          try {
              this.client.executeMethod(httppost);
          } finally {
              httppost.releaseConnection();
          }
          assertTrue(connman.getConection().isOpen());
  
          httppost = new PostMethod("/test/");
          httppost.setRequestHeader("Connection", "close");
          httppost.setRequestEntity(new StringRequestEntity("more stuff"));
          try {
              this.client.executeMethod(httppost);
          } finally {
              httppost.releaseConnection();
          }
          assertFalse(connman.getConection().isOpen());
      }
  
      public void testConnKeepAlive() throws Exception {
          this.server.setHttpService(new EchoService());
  
          AccessibleHttpConnectionManager connman = new AccessibleHttpConnectionManager();
          
          this.client.getParams().setVersion(HttpVersion.HTTP_1_0);
          this.client.setHttpConnectionManager(connman);
          
          PostMethod httppost = new PostMethod("/test/");
          httppost.setRequestEntity(new StringRequestEntity("stuff"));
          try {
              this.client.executeMethod(httppost);
          } finally {
              httppost.releaseConnection();
          }
          assertFalse(connman.getConection().isOpen());
  
          httppost = new PostMethod("/test/");
          httppost.setRequestHeader("Connection", "keep-alive");
          httppost.setRequestEntity(new StringRequestEntity("more stuff"));
          try {
              this.client.executeMethod(httppost);
          } finally {
              httppost.releaseConnection();
          }
          assertTrue(connman.getConection().isOpen());
      }
  
  }
  
  
  
  
  1.7       +56 -43    jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/auth/TestBasicAuth.java
  
  Index: TestBasicAuth.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/auth/TestBasicAuth.java,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- TestBasicAuth.java	1 Nov 2004 02:21:15 -0000	1.6
  +++ TestBasicAuth.java	7 Nov 2004 12:31:42 -0000	1.7
  @@ -330,7 +330,7 @@
           assertEquals("test", authstate.getRealm());
       }
   
  -    public void testBasicAuthenticationWithMutlipleRealms() throws Exception {
  +    public void testBasicAuthenticationWithMutlipleRealms1() throws Exception {
           HttpState state = new HttpState();
           AuthScope realm1 = new AuthScope(
               this.server.getLocalAddress(), 
  @@ -343,46 +343,59 @@
           state.setCredentials(realm1, new UsernamePasswordCredentials("test","test"));
           state.setCredentials(realm2, new UsernamePasswordCredentials("test2","test2"));
           this.client.setState(state);
  -        {
  -            this.server.setHttpService(new BasicAuthService());
  -            GetMethod httpget = new GetMethod("/test/");
  -            try {
  -                this.client.executeMethod(httpget);
  -            } finally {
  -                httpget.releaseConnection();
  -            }
  -            assertNotNull(httpget.getStatusLine());
  -            assertEquals(HttpStatus.SC_OK, httpget.getStatusLine().getStatusCode());
  -            Header auth = httpget.getRequestHeader("Authorization");
  -            assertNotNull(auth);
  -            String expected = "Basic " + EncodingUtil.getAsciiString(
  -                Base64.encodeBase64(EncodingUtil.getAsciiBytes("test:test")));
  -            assertEquals(expected, auth.getValue());
  -            AuthState authstate = httpget.getHostAuthState();
  -            assertNotNull(authstate.getAuthScheme());
  -            assertTrue(authstate.getAuthScheme() instanceof BasicScheme);
  -            assertEquals("test", authstate.getRealm());
  +
  +        this.server.setHttpService(new BasicAuthService());
  +        GetMethod httpget = new GetMethod("/test/");
  +        try {
  +            this.client.executeMethod(httpget);
  +        } finally {
  +            httpget.releaseConnection();
           }
  -        {
  -            this.server.setHttpService(new BasicAuthService2());
  -            GetMethod httpget = new GetMethod("/test2/");
  -            try {
  -                this.client.executeMethod(httpget);
  -            } finally {
  -                httpget.releaseConnection();
  -            }
  -            assertNotNull(httpget.getStatusLine());
  -            assertEquals(HttpStatus.SC_OK, httpget.getStatusLine().getStatusCode());
  -            Header auth = httpget.getRequestHeader("Authorization");
  -            assertNotNull(auth);
  -            String expected = "Basic " + EncodingUtil.getAsciiString(
  -                Base64.encodeBase64(EncodingUtil.getAsciiBytes("test2:test2")));
  -            assertEquals(expected, auth.getValue());
  -            AuthState authstate = httpget.getHostAuthState();
  -            assertNotNull(authstate.getAuthScheme());
  -            assertTrue(authstate.getAuthScheme() instanceof BasicScheme);
  -            assertEquals("test2", authstate.getRealm());
  +        assertNotNull(httpget.getStatusLine());
  +        assertEquals(HttpStatus.SC_OK, httpget.getStatusLine().getStatusCode());
  +        Header auth = httpget.getRequestHeader("Authorization");
  +        assertNotNull(auth);
  +        String expected = "Basic " + EncodingUtil.getAsciiString(
  +            Base64.encodeBase64(EncodingUtil.getAsciiBytes("test:test")));
  +        assertEquals(expected, auth.getValue());
  +        AuthState authstate = httpget.getHostAuthState();
  +        assertNotNull(authstate.getAuthScheme());
  +        assertTrue(authstate.getAuthScheme() instanceof BasicScheme);
  +        assertEquals("test", authstate.getRealm());
  +    }
  +
  +    public void testBasicAuthenticationWithMutlipleRealms2() throws Exception {
  +        HttpState state = new HttpState();
  +        AuthScope realm1 = new AuthScope(
  +            this.server.getLocalAddress(), 
  +            this.server.getLocalPort(),
  +            "test");
  +        AuthScope realm2 = new AuthScope(
  +            this.server.getLocalAddress(), 
  +            this.server.getLocalPort(),
  +            "test2");
  +        state.setCredentials(realm1, new UsernamePasswordCredentials("test","test"));
  +        state.setCredentials(realm2, new UsernamePasswordCredentials("test2","test2"));
  +        this.client.setState(state);
  +
  +        this.server.setHttpService(new BasicAuthService2());
  +        GetMethod httpget = new GetMethod("/test2/");
  +        try {
  +            this.client.executeMethod(httpget);
  +        } finally {
  +            httpget.releaseConnection();
           }
  +        assertNotNull(httpget.getStatusLine());
  +        assertEquals(HttpStatus.SC_OK, httpget.getStatusLine().getStatusCode());
  +        Header auth = httpget.getRequestHeader("Authorization");
  +        assertNotNull(auth);
  +        String expected = "Basic " + EncodingUtil.getAsciiString(
  +            Base64.encodeBase64(EncodingUtil.getAsciiBytes("test2:test2")));
  +        assertEquals(expected, auth.getValue());
  +        AuthState authstate = httpget.getHostAuthState();
  +        assertNotNull(authstate.getAuthScheme());
  +        assertTrue(authstate.getAuthScheme() instanceof BasicScheme);
  +        assertEquals("test2", authstate.getRealm());
       }
   
       public void testPreemptiveAuthorizationTrueWithCreds() throws Exception {
  
  
  
  1.2       +5 -3      jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/auth/TestDigestAuth.java
  
  Index: TestDigestAuth.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/auth/TestDigestAuth.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- TestDigestAuth.java	31 Oct 2004 13:46:54 -0000	1.1
  +++ TestDigestAuth.java	7 Nov 2004 12:31:42 -0000	1.2
  @@ -300,6 +300,7 @@
       public void testDigestAuthenticationWithStaleNonce() throws Exception {
           // configure the server
           SimpleHttpServer server = new SimpleHttpServer(); // use arbitrary port
  +        server.setTestname(getName());
           server.setHttpService(new StaleNonceService());
   
           // configure the client
  @@ -326,6 +327,7 @@
           assertEquals("/", table.get("uri"));
           assertEquals("321CBA", table.get("nonce"));
           assertEquals("7f5948eefa115296e9279225041527b3", table.get("response"));
  +        server.destroy();
       }
   
   }
  
  
  
  1.2       +6 -4      jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/auth/TestNTLMAuth.java
  
  Index: TestNTLMAuth.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/auth/TestNTLMAuth.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- TestNTLMAuth.java	31 Oct 2004 13:46:54 -0000	1.1
  +++ TestNTLMAuth.java	7 Nov 2004 12:31:42 -0000	1.2
  @@ -152,6 +152,7 @@
       public void testNTLMAuthenticationRetry() throws Exception {
           // configure the server
           SimpleHttpServer server = new SimpleHttpServer(); // use arbitrary port
  +        server.setTestname(getName());
           server.setHttpService(new NTLMAuthService());
   
           // configure the client
  @@ -171,6 +172,7 @@
           }
           assertNull(httpget.getResponseHeader("WWW-Authenticate"));
           assertEquals(200, httpget.getStatusCode());
  +        server.destroy();
       }
       
   }
  
  
  
  1.5       +4 -3      jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/server/HttpRequestHandlerChain.java
  
  Index: HttpRequestHandlerChain.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/server/HttpRequestHandlerChain.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- HttpRequestHandlerChain.java	27 Feb 2004 19:01:34 -0000	1.4
  +++ HttpRequestHandlerChain.java	7 Nov 2004 12:31:42 -0000	1.5
  @@ -50,6 +50,7 @@
       private List subhandlers = new ArrayList();
       
       public HttpRequestHandlerChain() {
  +        super();
       }
       
       public synchronized void clear() {
  
  
  
  1.5       +26 -11    jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/server/HttpServiceHandler.java
  
  Index: HttpServiceHandler.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/server/HttpServiceHandler.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- HttpServiceHandler.java	6 Nov 2004 23:47:58 -0000	1.4
  +++ HttpServiceHandler.java	7 Nov 2004 12:31:42 -0000	1.5
  @@ -89,20 +89,35 @@
                       Header header = new Header("Content-Length", Long.toString(len)); 
                       response.setHeader(header);
                   }
  -                
               } else {
                   Header header = new Header("Content-Length", "0"); 
  -                response.setHeader(header);                
  +                response.setHeader(header);
               }
           }
   
  -        Header connheader = response.getFirstHeader("Connection");
  -        if (connheader == null) {
  -            Header header = new Header("Connection", "close"); 
  -            response.setHeader(header);
  -            conn.connectionClose();
  +        if (!response.containsHeader("Connection")) {
  +            // See if the the client explicitly handles connection persistence
  +            Header connheader = request.getFirstHeader("Connection");
  +            if (connheader != null) {
  +                if (connheader.getValue().equalsIgnoreCase("keep-alive")) {
  +                    Header header = new Header("Connection", "keep-alive"); 
  +                    response.addHeader(header);
  +                    conn.connectionKeepAlive();
  +                }
  +                if (connheader.getValue().equalsIgnoreCase("close")) {
  +                    Header header = new Header("Connection", "close"); 
  +                    response.addHeader(header);
  +                    conn.connectionClose();
  +                }
  +            } else {
  +                // Use protocol default connection policy
  +                if (response.getHttpVersion().greaterEquals(HttpVersion.HTTP_1_1)) {
  +                    conn.connectionKeepAlive();
  +                } else {
  +                    conn.connectionClose();
  +                }
  +            }
           }
  -        
           conn.writeResponse(response);
           return true;
       }
  
  
  
  1.7       +55 -40    jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/server/ProxyRequestHandler.java
  
  Index: ProxyRequestHandler.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/server/ProxyRequestHandler.java,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- ProxyRequestHandler.java	1 Nov 2004 02:21:15 -0000	1.6
  +++ ProxyRequestHandler.java	7 Nov 2004 12:31:42 -0000	1.7
  @@ -25,27 +25,27 @@
    * information on the Apache Software Foundation, please see
    * <http://www.apache.org/>.
    *
  - * [Additional notices, if required by prior licensing conditions]
  - *
    */
   
   package org.apache.commons.httpclient.server;
   
   import java.io.IOException;
   import java.io.InputStream;
  -import java.io.OutputStream;
   
  -import org.apache.commons.httpclient.ChunkedOutputStream;
   import org.apache.commons.httpclient.Header;
   import org.apache.commons.httpclient.HttpClient;
   import org.apache.commons.httpclient.HttpMethod;
  +import org.apache.commons.httpclient.HttpVersion;
  +import org.apache.commons.httpclient.StatusLine;
   import org.apache.commons.httpclient.URI;
   import org.apache.commons.httpclient.methods.ByteArrayRequestEntity;
   import org.apache.commons.httpclient.methods.EntityEnclosingMethod;
   import org.apache.commons.httpclient.methods.GetMethod;
   import org.apache.commons.httpclient.methods.HeadMethod;
  +import org.apache.commons.httpclient.methods.InputStreamRequestEntity;
   import org.apache.commons.httpclient.methods.PostMethod;
   import org.apache.commons.httpclient.methods.PutMethod;
  +import org.apache.commons.httpclient.methods.RequestEntity;
   
   /**
    * This request handler can handle GET, POST, PUT, and HEAD requests. It does
  @@ -54,6 +54,13 @@
    */
   public class ProxyRequestHandler implements HttpRequestHandler {
   
  +    private HttpClient client = null;
  +
  +    public ProxyRequestHandler() {
  +        super();
  +        this.client = new HttpClient();
  +    }
  +    
   	/**
   	 * @see org.apache.commons.httpclient.server.HttpRequestHandler#processRequest(org.apache.commons.httpclient.server.SimpleHttpServerConnection)
   	 */
  @@ -88,50 +95,58 @@
           final SimpleRequest request,
           final HttpMethod method) throws IOException
       {
  -        // TODO add support for Proxy-Connection headers
  -        
  -		HttpClient client = new HttpClient();
           method.setFollowRedirects(false);
   		Header[] headers = request.getHeaders();
  -		for (int i=0; i<headers.length; i++) {
  +		for (int i = 0; i < headers.length; i++) {
   			method.addRequestHeader(headers[i]);
   		}
   		if (method instanceof EntityEnclosingMethod) {
   			EntityEnclosingMethod emethod = (EntityEnclosingMethod) method;
   			emethod.setRequestEntity(new ByteArrayRequestEntity(request.getBody()));
   		}
  -		client.executeMethod(method);
  -		
  -		Header[] rheaders = method.getResponseHeaders();
  -		InputStream targetIn = method.getResponseBodyAsStream();
  -		ResponseWriter out = conn.getWriter();
  -		out.println(method.getStatusLine().toString());
  -		for (int i=0; i<rheaders.length; i++) {
  -			out.print(rheaders[i].toExternalForm());
  -		}
  -		if (rheaders.length > 0) out.println();
  -		out.flush();
  -		out = null;
  -
  -        // handle content, if present
  -        if (targetIn != null) {
  -            // if the server used chunking, so will we
  -            Header teHeader = method.getResponseHeader("Transfer-Encoding");
  -            ChunkedOutputStream cos = null;
  -            OutputStream os = conn.getOutputStream();
  -            if (teHeader != null && teHeader.getValue().toLowerCase().indexOf("chunked")
>= 0) {
  -                cos = new ChunkedOutputStream(os);
  -                os = cos;
  +        try {
  +            client.executeMethod(method);
  +            
  +            StatusLine statusline = method.getStatusLine(); 
  +            SimpleResponse response = new SimpleResponse();
  +            response.setStatusLine(
  +                    HttpVersion.parse(statusline.getHttpVersion()),
  +                    statusline.getStatusCode(),
  +                    statusline.getReasonPhrase());
  +            response.setHeaders(method.getResponseHeaders());
  +            
  +            long len = InputStreamRequestEntity.CONTENT_LENGTH_AUTO;
  +            Header header = response.getFirstHeader("Transfer-Encoding");
  +            if (header != null) {
  +                len = -1; // Use chunked encoding
  +            } else {
  +                header = response.getFirstHeader("Content-Length");
  +                try {
  +                    len = Long.parseLong(header.getValue());
  +                } catch (NumberFormatException e) {
  +                }
               }
  -            byte[] buffer = new byte[4096];
  -            int length = 0;
  -            while ((length = targetIn.read(buffer)) > 0) {
  -                os.write(buffer, 0, length);
  +            String contentype = null;
  +            header = method.getResponseHeader("Content-Type");
  +            if (header != null) {
  +                contentype = header.getValue();
               }
  -            if (cos != null) {
  -                cos.finish();
  -                cos.flush();
  +            
  +            InputStream instream = method.getResponseBodyAsStream();
  +            if (instream != null) {
  +                RequestEntity content = new InputStreamRequestEntity(
  +                        instream, len, contentype);
  +                response.setBodyEntity(content);
               }
  +            
  +            Header connheader = new Header("Proxy-Connection", "close"); 
  +            response.setHeader(connheader);
  +            conn.connectionClose();
  +            
  +            conn.writeResponse(response);
  +            
  +        } finally {
  +            method.releaseConnection();
           }
   	}
   
  
  
  
  1.5       +6 -6      jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/server/ResponseWriter.java
  
  Index: ResponseWriter.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/server/ResponseWriter.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- ResponseWriter.java	4 Nov 2004 19:40:51 -0000	1.4
  +++ ResponseWriter.java	7 Nov 2004 12:31:42 -0000	1.5
  @@ -71,9 +71,9 @@
       }
       
       public void close() throws IOException {
  -        if(out != null) {
  +        if(outStream != null) {
               super.close();
  -            out = null;
  +            outStream = null;
           }
       }
   
  @@ -81,7 +81,7 @@
        * @see java.io.Writer#flush()
        */
       public void flush() throws IOException {
  -        if(out != null) {
  +        if(outStream != null) {
               super.flush();
               outStream.flush();
           }
  
  
  
  1.12      +26 -24    jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/server/SimpleHttpServer.java
  
  Index: SimpleHttpServer.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/server/SimpleHttpServer.java,v
  retrieving revision 1.11
  retrieving revision 1.12
  diff -u -r1.11 -r1.12
  --- SimpleHttpServer.java	6 Nov 2004 15:50:33 -0000	1.11
  +++ SimpleHttpServer.java	7 Nov 2004 12:31:42 -0000	1.12
  @@ -33,9 +33,6 @@
   import java.net.InetAddress;
   import java.net.ServerSocket;
   import java.net.Socket;
  -import java.util.HashSet;
  -import java.util.Iterator;
  -import java.util.Set;
   
   import org.apache.commons.logging.Log;
   import org.apache.commons.logging.LogFactory;
  @@ -49,12 +46,14 @@
   public class SimpleHttpServer implements Runnable {
       private static final Log LOG = LogFactory.getLog(SimpleHttpServer.class);
       
  +    private String testname = "Simple test";
  +    private long count = 0;
       private ServerSocket listener = null;
       private Thread t;
       private ThreadGroup tg;
       private boolean stopped = false;
   
  -    private Set connections = new HashSet();
  +    private SimpleConnPool connections = new SimpleConnPool();
   
       private HttpRequestHandler requestHandler = null;
   
  @@ -78,12 +77,20 @@
           if(LOG.isDebugEnabled()) {
               LOG.debug("Starting test HTTP server on port " + getLocalPort());
           }
  -        tg = new ThreadGroup("SimpleHttpServer group");
  -        t = new Thread(tg, this, "SimpleHttpServer connection handler");
  +        tg = new ThreadGroup("SimpleHttpServer thread group");
  +        t = new Thread(tg, this, "SimpleHttpServer listener");
           t.setDaemon(true);
           t.start();
       }
   
  +    public String getTestname() {
  +        return this.testname;
  +    }
  +
  +    public void setTestname(final String testname) {
  +        this.testname = testname;
  +    }
  +    
       /**
        * Returns the TCP port that this HTTP server instance is bound to.
        *
  @@ -137,7 +144,7 @@
           }
   
           tg.interrupt();
  -
  +        
           if (listener != null) {
               try {
                   listener.close();
  @@ -145,12 +152,7 @@
                   
               }
           }
  -
  -        for (Iterator it = connections.iterator(); it.hasNext();) {
  -            SimpleHttpServerConnection conn =
  -                (SimpleHttpServerConnection) it.next();
  -            conn.destroy();
  -        }
  +        this.connections.shutdown();
       }
   
       /**
  @@ -175,10 +177,6 @@
           setRequestHandler(new HttpServiceHandler(service));
       }
   
  -    public void removeConnection(SimpleHttpServerConnection conn) {
  -        connections.remove(conn);
  -    }
  -
       public void run() {
           try {
               while (!Thread.interrupted()) {
  @@ -188,16 +186,20 @@
                           socket.close();
                           break;
                       }
  -                    SimpleHttpServerConnection conn = 
  -                        new SimpleHttpServerConnection(socket, this.requestHandler);
  -                    connections.add(conn);
  +                    SimpleHttpServerConnection conn = new SimpleHttpServerConnection(
  +                                this.testname + " connection " + this.count,
  +                                socket, 
  +                                this.connections, 
  +                                this.requestHandler);
  +                    connections.addConnection(conn);
   
  -                    Thread t = new Thread(tg, conn, "SimpleHttpServer connection");
  +                    Thread t = new Thread(tg, conn, this.testname + " thread " + this.count);
                       t.setDaemon(true);
                       t.start();
                   } catch (IOException e) {
                       LOG.error("I/O error: " + e.getMessage());
                   }
  +                this.count++;
                   Thread.sleep(100);
               }
           } catch (InterruptedException accept) {
  
  
  
  1.17      +33 -11    jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/server/SimpleHttpServerConnection.java
  
  Index: SimpleHttpServerConnection.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/server/SimpleHttpServerConnection.java,v
  retrieving revision 1.16
  retrieving revision 1.17
  diff -u -r1.16 -r1.17
  --- SimpleHttpServerConnection.java	6 Nov 2004 23:47:58 -0000	1.16
  +++ SimpleHttpServerConnection.java	7 Nov 2004 12:31:42 -0000	1.17
  @@ -59,29 +59,50 @@
   public class SimpleHttpServerConnection implements Runnable {
   
       private static final Log LOG = LogFactory.getLog(SimpleHttpServerConnection.class);
  +    
       private static final String HTTP_ELEMENT_CHARSET = "US-ASCII";
       public static final String DEFAULT_CONTENT_CHARSET = "ISO-8859-1";
   
  -    private Socket socket;
  -    private InputStream in;
  -    private OutputStream out;
  +    private String name = null;
  +    private Socket socket = null;
  +    private SimpleConnPool connpool = null;    
  +    private InputStream in = null;
  +    private OutputStream out = null;
       private boolean keepAlive = false;
       private SimpleRequest request = null;
       private HttpRequestHandler handler = null;
       transient boolean destroyed; 
   
  -    public SimpleHttpServerConnection(final Socket socket, final HttpRequestHandler handler)

  +    public SimpleHttpServerConnection(
  +            final String name,
  +            final Socket socket,
  +            final SimpleConnPool connpool,
  +            final HttpRequestHandler handler) 
       throws IOException {
           super();
  +        if (socket == null) {
  +            throw new IllegalArgumentException("Socket may not be null");
  +        }
  +        if (connpool == null) {
  +            throw new IllegalArgumentException("Connection pool not be null");
  +        }
  +        if (handler == null) {
  +            throw new IllegalArgumentException("Request handler may not be null");
  +        }
  +        this.name = name;
           this.socket = socket;
  -        this.socket.setSoTimeout(2000);
  +        this.socket.setSoTimeout(500);
           this.in = socket.getInputStream();
           this.out = socket.getOutputStream();
  +        this.connpool = connpool;
           this.handler = handler;
           this.destroyed = false; 
       }
   
       public synchronized void destroy() {
  +        if (this.destroyed) {
  +            return;
  +        }
           this.destroyed = true; 
           try {
               if (socket != null) {
  @@ -102,15 +123,16 @@
                   readRequest();
                   if (this.request != null) {
                       this.handler.processRequest(this, this.request);
  +                    this.out.flush();
                   }
  -                this.out.flush();
               } while (this.keepAlive);
           } catch (IOException e) {
  -            if (!this.destroyed) {
  -                LOG.error("I/O error: " + e.getMessage());
  +            if (!this.destroyed && LOG.isWarnEnabled()) {
  +                LOG.warn("[" + this.name + "] I/O error: " + e.getMessage());
               }
           } finally {
               destroy();
  +            this.connpool.removeConnection(this);
           }
       }
   
  
  
  
  1.5       +15 -12    jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/server/SimpleProxy.java
  
  Index: SimpleProxy.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/server/SimpleProxy.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- SimpleProxy.java	22 Feb 2004 18:08:52 -0000	1.4
  +++ SimpleProxy.java	7 Nov 2004 12:31:42 -0000	1.5
  @@ -39,15 +39,21 @@
    * Simple server that registers default request handlers to act as a proxy.
    * 
    * @author Ortwin Glueck
  + * @author Oleg Kalnichevski
    */
   public class SimpleProxy extends SimpleHttpServer {
  -    private HttpRequestHandlerChain chain = new SimpleChain();
  +    
  +    private HttpRequestHandlerChain chain = null;
   
       /**
        * @throws IOException
        */
       public SimpleProxy() throws IOException {
  -        setRequestHandler(chain);
  +        super();
  +        this.chain = new HttpRequestHandlerChain();
  +        this.chain.appendHandler(new TransparentProxyRequestHandler());
  +        this.chain.appendHandler(new ProxyRequestHandler());
  +        setRequestHandler(this.chain);
       }
   
       /**
  @@ -60,14 +66,11 @@
       }
   
       public void requireCredentials(Credentials creds) {
  -        chain.prependHandler(new ProxyAuthRequestHandler(creds));
  +        this.chain.prependHandler(new ProxyAuthRequestHandler(creds));
       }
   
  -    private class SimpleChain extends HttpRequestHandlerChain {
  -        public SimpleChain() {
  -            appendHandler(new TransparentProxyRequestHandler());
  -            appendHandler(new ProxyRequestHandler());
  -        }
  +    public void destroy() {
  +        super.destroy();
       }
  -
  +    
   }
  
  
  
  1.7       +15 -11    jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/server/SimpleResponse.java
  
  Index: SimpleResponse.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/server/SimpleResponse.java,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- SimpleResponse.java	6 Nov 2004 23:47:58 -0000	1.6
  +++ SimpleResponse.java	7 Nov 2004 12:31:42 -0000	1.7
  @@ -91,14 +91,6 @@
           return this.ver;
       }
   
  -    public void setBodyString(final String string) {
  -        try {
  -            this.entity = new StringRequestEntity(string, "text/plain", "ISO-8859-1");
  -        } catch (UnsupportedEncodingException e) {
  -            this.entity = new StringRequestEntity(string);
  -        }
  -    }
  -    
       public String getStatusLine() {
           StringBuffer buffer = new StringBuffer();
           buffer.append(this.ver);
  @@ -172,6 +164,18 @@
   
       public RequestEntity getEntity() {
           return this.entity;
  +    }
  +
  +    public void setBodyString(final String string) {
  +        try {
  +            this.entity = new StringRequestEntity(string, "text/plain", "ISO-8859-1");
  +        } catch (UnsupportedEncodingException e) {
  +            this.entity = new StringRequestEntity(string);
  +        }
  +    }
  +    
  +    public void setBodyEntity(final RequestEntity entity) {
  +        this.entity = entity;
       }
       
   }
  
  
  
  1.1                  jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/server/SimpleConnPool.java
  
  Index: SimpleConnPool.java
  ===================================================================
  /*
   * $Header: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/server/SimpleConnPool.java,v
1.1 2004/11/07 12:31:42 olegk Exp $
   * $Revision: 1.1 $
   * $Date: 2004/11/07 12:31:42 $
   *
   * ====================================================================
   *
   *  Copyright 1999-2004 The Apache Software Foundation
   *
   *  Licensed under the Apache License, Version 2.0 (the "License");
   *  you may not use this file except in compliance with the License.
   *  You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
   *
   *  Unless required by applicable law or agreed to in writing, software
   *  distributed under the License is distributed on an "AS IS" BASIS,
   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   *  See the License for the specific language governing permissions and
   *  limitations under the License.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   */
  
  package org.apache.commons.httpclient.server;
  
  import java.util.HashSet;
  import java.util.Iterator;
  import java.util.Set;
  
  import org.apache.commons.logging.Log;
  import org.apache.commons.logging.LogFactory;
  
  /**
   * A simple pool of connections.
   * 
   * @author Oleg Kalnichevski
   */
  public class SimpleConnPool {
      
      private static final Log LOG = LogFactory.getLog(SimpleConnPool.class);
      
      private Set connections = new HashSet();
      
      public SimpleConnPool() {
          super();
      }
  
      public synchronized void addConnection(final SimpleHttpServerConnection conn) {
          this.connections.add(conn);
      }
      
      public synchronized void removeConnection(final SimpleHttpServerConnection conn) {
          this.connections.remove(conn);
      }
  
      public synchronized void shutdown() {
          for (Iterator it = connections.iterator(); it.hasNext();) {
              SimpleHttpServerConnection conn =
                  (SimpleHttpServerConnection) it.next();
              conn.destroy();
          }
      }
  
  }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message