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 ErrorResponse.java HttpRequestHandler.java HttpRequestHandlerChain.java ProxyAuthRequestHandler.java ProxyRequestHandler.java SimpleHttpServer.java SimpleHttpServerConnection.java TransparentProxyRequestHandler.java GenericResponse.java
Date Fri, 27 Feb 2004 19:01:34 GMT
olegk       2004/02/27 11:01:34

  Modified:    httpclient/src/test/org/apache/commons/httpclient
                        HttpClientTestBase.java TestAuthenticator.java
                        TestBadContentLength.java TestNoHost.java
               httpclient/src/test/org/apache/commons/httpclient/server
                        ErrorResponse.java HttpRequestHandler.java
                        HttpRequestHandlerChain.java
                        ProxyAuthRequestHandler.java
                        ProxyRequestHandler.java SimpleHttpServer.java
                        SimpleHttpServerConnection.java
                        TransparentProxyRequestHandler.java
  Removed:     httpclient/src/test/org/apache/commons/httpclient/server
                        GenericResponse.java
  Log:
  Changelog
  
  * SimpleRequest and SimpleResponse classes factored out
  * Interface HttpService can be used instead of HttpRequestHandler to implement test cases
in a way very similar to writing servlets.
  * Basic authentication test cases ported to the new testing framework
  
  Contributed by Oleg Kalnichevski
  
  Revision  Changes    Path
  1.2       +4 -37     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.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- HttpClientTestBase.java	25 Feb 2004 22:33:58 -0000	1.1
  +++ HttpClientTestBase.java	27 Feb 2004 19:01:33 -0000	1.2
  @@ -37,10 +37,7 @@
   import junit.framework.TestSuite;
   
   import org.apache.commons.httpclient.protocol.Protocol;
  -import org.apache.commons.httpclient.server.RequestLine;
  -import org.apache.commons.httpclient.server.ResponseWriter;
   import org.apache.commons.httpclient.server.SimpleHttpServer;
  -import org.apache.commons.httpclient.server.SimpleHttpServerConnection;
   
   /**
    * Base class for test cases using 
  @@ -92,35 +89,5 @@
           this.client = null;
           this.server.destroy();
           this.server = null;
  -    }
  -    
  -    protected static void respondNotImplemented(SimpleHttpServerConnection conn) 
  -        throws IOException
  -    {
  -        RequestLine requestLine = conn.getRequestLine();
  -        ResponseWriter out = conn.getWriter();
  -        String content = requestLine.getMethod() + " method not supported";
  -        out.println("HTTP/1.1 501 Not implemented");
  -        out.println("Content-Type: text/plain");
  -        out.println("Content-Length: " + content.length());
  -        out.println("Connection: close");
  -        out.println();
  -        out.println(content);
  -        out.flush();
  -    }
  -
  -    protected static void respondNotFound(SimpleHttpServerConnection conn) 
  -        throws IOException
  -    {
  -        RequestLine requestLine = conn.getRequestLine();
  -        ResponseWriter out = conn.getWriter();
  -        String content = requestLine.getUri() + " not found";
  -        out.println("HTTP/1.1 404 Not found");
  -        out.println("Content-Type: text/plain");
  -        out.println("Content-Length: " + content.length());
  -        out.println("Connection: close");
  -        out.println();
  -        out.println(content);
  -        out.flush();
       }
   }
  
  
  
  1.41      +7 -154    jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestAuthenticator.java
  
  Index: TestAuthenticator.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestAuthenticator.java,v
  retrieving revision 1.40
  retrieving revision 1.41
  diff -u -r1.40 -r1.41
  --- TestAuthenticator.java	22 Feb 2004 18:08:49 -0000	1.40
  +++ TestAuthenticator.java	27 Feb 2004 19:01:33 -0000	1.41
  @@ -35,16 +35,13 @@
   import junit.framework.Test;
   import junit.framework.TestSuite;
   
  -import org.apache.commons.codec.binary.Base64;
   import org.apache.commons.httpclient.auth.AuthChallengeParser;
   import org.apache.commons.httpclient.auth.AuthScheme;
   import org.apache.commons.httpclient.auth.AuthenticationException;
  -import org.apache.commons.httpclient.auth.BasicScheme;
   import org.apache.commons.httpclient.auth.CredentialsNotAvailableException;
   import org.apache.commons.httpclient.auth.DigestScheme;
   import org.apache.commons.httpclient.auth.MalformedChallengeException;
   import org.apache.commons.httpclient.auth.NTLMScheme;
  -import org.apache.commons.httpclient.util.EncodingUtil;
   
   /**
    * Unit tests for {@link Authenticator}.
  @@ -138,7 +135,7 @@
       ) throws AuthenticationException {
          return doAuthenticate(authscheme, method, conn, state, true);
       }
  -    // ---------------------------------- Test Methods for BasicScheme Authentication
  +
   
       public void testCredentialConstructors() {
           try {
  @@ -166,153 +163,9 @@
           assertNotNull(creds.getHost());
       }
   
  +    // ---------------------------------- Test Methods for BasicScheme Authentication
   
  -    public void testBasicAuthenticationWithNoCreds() {
  -        String challenge = "Basic realm=\"realm1\"";
  -        HttpState state = new HttpState();
  -        HttpMethod method = new SimpleHttpMethod(new Header("WWW-Authenticate", challenge));
  -        try {
  -            AuthScheme authscheme = new BasicScheme();
  -            authscheme.processChallenge(challenge);
  -            authenticate(authscheme, method, null, state);
  -            fail("Should have thrown HttpException");
  -        } catch(HttpException e) {
  -            // expected
  -        }
  -    }
  -
  -    public void testBasicAuthenticationWithNoRealm() {
  -        String challenge = "Basic";
  -        HttpState state = new HttpState();
  -        HttpMethod method = new SimpleHttpMethod(new Header("WWW-Authenticate", challenge));
  -        try {
  -            AuthScheme authscheme = new BasicScheme();
  -            authscheme.processChallenge(challenge);
  -            authenticate(authscheme, method, null, state);
  -            fail("Should have thrown HttpException");
  -        } catch(HttpException e) {
  -            // expected
  -        }
  -    }
  -
  -    public void testBasicAuthenticationWithNoRealm2() {
  -        String challenge = "Basic ";
  -        HttpState state = new HttpState();
  -        HttpMethod method = new SimpleHttpMethod(new Header("WWW-Authenticate", challenge));
  -        try {
  -            AuthScheme authscheme = new BasicScheme();
  -            authscheme.processChallenge(challenge);
  -            authenticate(authscheme, method, null, state);
  -            fail("Should have thrown HttpException");
  -        } catch(HttpException e) {
  -            // expected
  -        }
  -    }
  -
  -    public void testBasicAuthenticationWithNullHttpState() throws Exception {
  -        String challenge = "Basic realm=\"realm1\"";
  -        HttpMethod method = new SimpleHttpMethod(new Header("WWW-Authenticate", challenge));
  -        try {
  -            AuthScheme authscheme = new BasicScheme();
  -            authscheme.processChallenge(challenge);
  -            authenticate(authscheme, method, null, null);
  -            fail("Should have thrown IllegalArgumentException");
  -        } catch(IllegalArgumentException e) {
  -            // expected
  -        }
  -    }
  -
  -    public void testBasicAuthenticationCaseInsensitivity() throws Exception {
  -        String challenge = "bAsIc ReAlM=\"realm1\"";
  -        HttpState state = new HttpState();
  -        state.setCredentials(null, null, new UsernamePasswordCredentials("username","password"));
  -        HttpMethod method = new SimpleHttpMethod(new Header("WwW-AuThEnTiCaTe", challenge));
  -        AuthScheme authscheme = new BasicScheme();
  -        authscheme.processChallenge(challenge);
  -        assertTrue(authenticate(authscheme, method, null, state));
  -        assertTrue(null != method.getRequestHeader("Authorization"));
  -        String expected = "Basic " + EncodingUtil.getAsciiString(
  -            Base64.encodeBase64(EncodingUtil.getAsciiBytes("username:password")));
  -        assertEquals(expected,method.getRequestHeader("Authorization").getValue());
  -    }
  -
  -
  -    public void testBasicAuthenticationWithDefaultCreds() throws Exception {
  -        HttpState state = new HttpState();
  -        state.setCredentials(null, null, new UsernamePasswordCredentials("username","password"));
  -        HttpMethod method = new SimpleHttpMethod(new Header("WWW-Authenticate","Basic realm=\"realm1\""));
  -        assertTrue(authenticate(new BasicScheme(), method, null, state));
  -        assertTrue(null != method.getRequestHeader("Authorization"));
  -        String expected = "Basic " + EncodingUtil.getAsciiString(
  -            Base64.encodeBase64(EncodingUtil.getAsciiBytes("username:password")));
  -        assertEquals(expected,method.getRequestHeader("Authorization").getValue());
  -    }
  -
  -    public void testBasicAuthentication() throws Exception {
  -        String challenge = "Basic realm=\"realm\"";
  -        HttpState state = new HttpState();
  -        state.setCredentials("realm", null, new UsernamePasswordCredentials("username","password"));
  -        HttpMethod method = new SimpleHttpMethod(new Header("WWW-Authenticate", challenge));
  -        AuthScheme authscheme = new BasicScheme();
  -        authscheme.processChallenge(challenge);
  -        assertTrue(authenticate(authscheme, method, null, state));
  -        assertTrue(null != method.getRequestHeader("Authorization"));
  -        String expected = "Basic " + EncodingUtil.getAsciiString(
  -            Base64.encodeBase64(EncodingUtil.getAsciiBytes("username:password")));
  -        assertEquals(expected,method.getRequestHeader("Authorization").getValue());
  -    }
  -    
  -    public void testBasicAuthenticationWith88591Chars() throws Exception {
  -        int[] germanChars = { 0xE4, 0x2D, 0xF6, 0x2D, 0xFc };
  -        StringBuffer buffer = new StringBuffer();
  -        for (int i = 0; i < germanChars.length; i++) {
  -            buffer.append((char)germanChars[i]); 
  -        }
  -        
  -        UsernamePasswordCredentials credentials = new UsernamePasswordCredentials("dh",
buffer.toString());
  -        assertEquals("Basic ZGg65C32Lfw=", 
  -            BasicScheme.authenticate(credentials, "ISO-8859-1"));
  -    }
  -    
  -    public void testBasicAuthenticationWithMutlipleRealms() throws Exception {
  -        String challenge1 = "Basic realm=\"realm1\"";
  -        String challenge2 = "Basic realm=\"realm2\"";
  -        HttpState state = new HttpState();
  -        state.setCredentials("realm1", null, new UsernamePasswordCredentials("username","password"));
  -        state.setCredentials("realm2", null, new UsernamePasswordCredentials("uname2","password2"));
  -        AuthScheme authscheme1 = new BasicScheme();
  -        authscheme1.processChallenge(challenge1);
  -        AuthScheme authscheme2 = new BasicScheme();
  -        authscheme2.processChallenge(challenge2);
  -        {
  -            HttpMethod method = new SimpleHttpMethod(new Header("WWW-Authenticate",challenge1));
  -            assertTrue(authenticate(authscheme1, method, null, state));
  -            assertTrue(null != method.getRequestHeader("Authorization"));
  -            String expected = "Basic " + EncodingUtil.getAsciiString(
  -                Base64.encodeBase64(EncodingUtil.getAsciiBytes("username:password")));
  -            assertEquals(expected,method.getRequestHeader("Authorization").getValue());
  -        }
  -        {
  -            HttpMethod method = new SimpleHttpMethod(new Header("WWW-Authenticate", challenge2));
  -            assertTrue(authenticate(authscheme2, method, null, state));
  -            assertTrue(null != method.getRequestHeader("Authorization"));
  -            String expected = "Basic " + EncodingUtil.getAsciiString(
  -                Base64.encodeBase64(EncodingUtil.getAsciiBytes("uname2:password2")));
  -            assertEquals(expected,method.getRequestHeader("Authorization").getValue());
  -        }
  -    }
  -
  -    public void testPreemptiveAuthorizationTrueWithCreds() throws Exception {
  -        HttpState state = new HttpState();
  -        HttpMethod method = new SimpleHttpMethod();
  -        state.setCredentials(null, null, new UsernamePasswordCredentials("username","password"));
  -
  -        assertTrue(authenticate(new BasicScheme(), method, null, state));
  -        assertTrue(null != method.getRequestHeader("Authorization"));
  -        String expected = "Basic " + EncodingUtil.getAsciiString(
  -            Base64.encodeBase64(EncodingUtil.getAsciiBytes("username:password")));
  -        assertEquals(expected, method.getRequestHeader("Authorization").getValue());
  -    }
  +    // Moved to a separate test case based on a new testing framework
   
       // --------------------------------- Test Methods for DigestScheme Authentication
   
  
  
  
  1.6       +10 -6     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.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- TestBadContentLength.java	22 Feb 2004 18:08:49 -0000	1.5
  +++ TestBadContentLength.java	27 Feb 2004 19:01:33 -0000	1.6
  @@ -41,6 +41,7 @@
   import org.apache.commons.httpclient.server.ResponseWriter;
   import org.apache.commons.httpclient.server.SimpleHttpServer;
   import org.apache.commons.httpclient.server.SimpleHttpServerConnection;
  +import org.apache.commons.httpclient.server.SimpleRequest;
   
   /**
    * Tests HttpClient's behaviour when receiving more response data than expected.
  @@ -166,8 +167,11 @@
       private class MyHttpRequestHandler implements HttpRequestHandler {
           private int requestNo = 0;
   
  -        public boolean processRequest(SimpleHttpServerConnection conn)  throws IOException
{
  -        	RequestLine requestLine = conn.getRequestLine();
  +        public boolean processRequest(
  +            final SimpleHttpServerConnection conn,
  +            final SimpleRequest request) throws IOException
  +        {
  +        	RequestLine requestLine = request.getRequestLine();
           	ResponseWriter out = conn.getWriter();
               if ("GET".equals(requestLine.getMethod())
                   && "/".equals(requestLine.getUri())) {
  
  
  
  1.32      +10 -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.31
  retrieving revision 1.32
  diff -u -r1.31 -r1.32
  --- TestNoHost.java	22 Feb 2004 18:08:49 -0000	1.31
  +++ TestNoHost.java	27 Feb 2004 19:01:33 -0000	1.32
  @@ -30,7 +30,11 @@
   
   package org.apache.commons.httpclient;
   
  -import junit.framework.*;
  +import junit.framework.Test;
  +import junit.framework.TestCase;
  +import junit.framework.TestSuite;
  +
  +import org.apache.commons.httpclient.auth.TestBasicAuth;
   
   /**
    * Tests that don't require any external host.
  @@ -58,6 +62,7 @@
           suite.addTest(TestHeaderElement.suite());
           suite.addTest(TestChallengeParser.suite());
           suite.addTest(TestAuthenticator.suite());
  +        suite.addTest(TestBasicAuth.suite());
           suite.addTest(TestHttpUrlMethod.suite());
           suite.addTest(TestURI.suite());
           suite.addTest(TestURIUtil.suite());
  
  
  
  1.4       +24 -19    jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/server/ErrorResponse.java
  
  Index: ErrorResponse.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/server/ErrorResponse.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- ErrorResponse.java	22 Feb 2004 18:08:52 -0000	1.3
  +++ ErrorResponse.java	27 Feb 2004 19:01:33 -0000	1.4
  @@ -52,28 +52,33 @@
       private final HashMap responses = new HashMap();
       
       private ErrorResponse() {
  +        super();
       }
       
  -    public void setResponse(int statusCode, GenericResponse r) {
  +    public void setResponse(int statusCode, SimpleResponse response) {
           Integer code = new Integer(statusCode);
  -        responses.put(code, r);
  +        responses.put(code, response);
       }
       
  -    public GenericResponse getResponse(int statusCode) {
  +    public SimpleResponse getResponse(int statusCode) {
           Integer code = new Integer(statusCode);
  -        GenericResponse r = (GenericResponse)responses.get(code);
  -        if(r == null) {
  -            String text = statusCode+" "+HttpStatus.getStatusText(statusCode);
  -            r = new GenericResponse("HTTP/1.0 "+text,
  -            "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\n"
  -                + "<html>\n<head>"
  -                + "<title>"+text+"</title>"
  -                + "</head>\n<body>"
  -                + "<h1>"+text+"</h1></body>\n</html>\n",
  -            "text/html");
  -            
  -            responses.put(code, r);
  +        SimpleResponse response = (SimpleResponse)responses.get(code);
  +        if (response == null) {
  +            StringBuffer buffer = new StringBuffer();
  +            buffer.append(statusCode);
  +            String s = HttpStatus.getStatusText(statusCode);
  +            if (s != null) {
  +                buffer.append(' ');
  +                buffer.append(s);
  +            }
  +            response = new SimpleResponse(buffer.toString());
  +            response.setContentType("text/plain");
  +            if (s == null) {
  +                s = "Error " + code;     
  +            }
  +            response.setBodyString(s);
  +            responses.put(code, response);
           }
  -        return r;
  +        return response;
       }
   }
  
  
  
  1.4       +8 -4      jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/server/HttpRequestHandler.java
  
  Index: HttpRequestHandler.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/server/HttpRequestHandler.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- HttpRequestHandler.java	22 Feb 2004 18:08:52 -0000	1.3
  +++ HttpRequestHandler.java	27 Feb 2004 19:01:33 -0000	1.4
  @@ -37,6 +37,7 @@
    * Defines an HTTP request handler for the SimpleHttpServer
    * 
    * @author Christian Kohlschuetter
  + * @author Oleg Kalnichevski
    */
   public interface HttpRequestHandler {
       /**
  @@ -53,9 +54,12 @@
        * {@link HttpRequestHandlerChain} structures).
        * 
        * @param conn          The Connection object to which this request belongs to.
  +     * @param request       The request object.
        * @return true if this handler handled the request and no other handlers in the 
        * chain should be called, false otherwise.
        * @throws IOException
        */
  -    public boolean processRequest(SimpleHttpServerConnection conn) throws IOException;
  +    public boolean processRequest(
  +        final SimpleHttpServerConnection conn,
  +        final SimpleRequest request) throws IOException;
   }
  
  
  
  1.4       +8 -6      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.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- HttpRequestHandlerChain.java	22 Feb 2004 18:08:52 -0000	1.3
  +++ HttpRequestHandlerChain.java	27 Feb 2004 19:01:34 -0000	1.4
  @@ -62,11 +62,13 @@
           subhandlers.add(handler);
       }
   
  -    public synchronized boolean processRequest(SimpleHttpServerConnection conn)  throws
IOException {
  -        
  +    public synchronized boolean processRequest(
  +        final SimpleHttpServerConnection conn,
  +        final SimpleRequest request) throws IOException 
  +    {
           for(Iterator it=subhandlers.iterator();it.hasNext();) {
               HttpRequestHandler h = (HttpRequestHandler)it.next();
  -            boolean stop = h.processRequest(conn);
  +            boolean stop = h.processRequest(conn, request);
               if (stop) {
                   return true;
               }
  
  
  
  1.9       +8 -15     jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/server/ProxyAuthRequestHandler.java
  
  Index: ProxyAuthRequestHandler.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/server/ProxyAuthRequestHandler.java,v
  retrieving revision 1.8
  retrieving revision 1.9
  diff -u -r1.8 -r1.9
  --- ProxyAuthRequestHandler.java	22 Feb 2004 18:08:52 -0000	1.8
  +++ ProxyAuthRequestHandler.java	27 Feb 2004 19:01:34 -0000	1.9
  @@ -71,9 +71,11 @@
           this.credentials = creds;
       }
   
  -    public boolean processRequest(SimpleHttpServerConnection conn) throws IOException {
  -        Header[] headers = conn.getHeaders();
  -        Header clientAuth = findHeader(headers, PROXY_AUTH_RESP);
  +    public boolean processRequest(
  +        final SimpleHttpServerConnection conn,
  +        final SimpleRequest request) throws IOException
  +    {
  +        Header clientAuth = request.getFirstHeader(PROXY_AUTH_RESP);
           if (clientAuth != null) {
               boolean ok = checkAuthorization(clientAuth);
               if (ok)
  @@ -124,15 +126,6 @@
               (UsernamePasswordCredentials)credentials,
               "ISO-8859-1");
           return expectedAuthString.equals(clientAuth.getValue());
  -    }
  -
  -    private Header findHeader(Header[] headers, String name) {
  -        for (int i = 0; i < headers.length; i++) {
  -            Header header = headers[i];
  -            if (header.getName().equalsIgnoreCase(name))
  -                return header;
  -        }
  -        return null;
       }
   
   }
  
  
  
  1.4       +16 -9     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.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- ProxyRequestHandler.java	22 Feb 2004 18:08:52 -0000	1.3
  +++ ProxyRequestHandler.java	27 Feb 2004 19:01:34 -0000	1.4
  @@ -55,22 +55,29 @@
   	/**
   	 * @see org.apache.commons.httpclient.server.HttpRequestHandler#processRequest(org.apache.commons.httpclient.server.SimpleHttpServerConnection)
   	 */
  -	public boolean processRequest(SimpleHttpServerConnection conn) throws IOException {
  -		RequestLine line = conn.getRequestLine();
  +	public boolean processRequest(
  +        final SimpleHttpServerConnection conn,
  +        final SimpleRequest request) throws IOException
  +    {
  +		RequestLine line = request.getRequestLine();
   		String method = line.getMethod();
   		//TODO add POST method handling
   		if (!"GET".equalsIgnoreCase(method)) return false;
  -		URI url = new HttpURL(line.getUri());
  -		httpProxy(conn, url);
  +		httpProxy(conn, request);
   		return true;
   	}
   
   	/**
   	 * @param conn
   	 */
  -	private void httpProxy(SimpleHttpServerConnection conn, URI url) throws IOException {
  +	private void httpProxy(
  +        final SimpleHttpServerConnection conn,
  +        final SimpleRequest request) throws IOException
  +    {
   		Log wireLog = LogFactory.getLog("httpclient.wire");
   		
  +        URI url = new HttpURL(request.getRequestLine().getUri());
  +
   		HttpClient client = new HttpClient();
   		HostConfiguration hc = new HostConfiguration();
   		hc.setHost(url);
  @@ -78,7 +85,7 @@
   		
   		//TODO support other methods
   		HttpMethod method = new GetMethod(url.getPathQuery());
  -		Header[] headers = conn.getHeaders();
  +		Header[] headers = request.getHeaders();
   		for (int i=0; i<headers.length; i++) {
   			method.addRequestHeader(headers[i]);
   		}
  
  
  
  1.5       +80 -10    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.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- SimpleHttpServer.java	22 Feb 2004 18:08:52 -0000	1.4
  +++ SimpleHttpServer.java	27 Feb 2004 19:01:34 -0000	1.5
  @@ -40,6 +40,7 @@
   import java.util.Iterator;
   import java.util.Set;
   
  +import org.apache.commons.httpclient.Header;
   import org.apache.commons.httpclient.HttpStatus;
   import org.apache.commons.logging.Log;
   import org.apache.commons.logging.LogFactory;
  @@ -48,6 +49,7 @@
    * A simple, but extensible HTTP server, mostly for testing purposes.
    * 
    * @author Christian Kohlschuetter
  + * @author Oleg Kalnichevski
    */
   public class SimpleHttpServer implements Runnable {
       private static final Log LOG = LogFactory.getLog(SimpleHttpServer.class);
  @@ -60,6 +62,7 @@
       private Set connections = new HashSet();
   
       private HttpRequestHandler requestHandler = null;
  +    private HttpService serivce = null;
   
       /**
        * Creates a new HTTP server instance, using an arbitrary free TCP port
  @@ -171,24 +174,91 @@
        * @param rh    Request handler to be used, or null to disable.
        */
       public void setRequestHandler(HttpRequestHandler rh) {
  -        requestHandler = rh;
  +        this.requestHandler = rh;
  +    }
  +
  +    public void setHttpService(HttpService service) {
  +        this.serivce = service;
       }
   
       public void removeConnection(SimpleHttpServerConnection conn) {
           connections.remove(conn);
       }
   
  -    public void processRequest(SimpleHttpServerConnection conn)
  -        throws IOException {
  -
  +    public void processRequest(
  +        final SimpleHttpServerConnection conn,
  +        final SimpleRequest request) throws IOException
  +    {
  +        if (conn == null) {
  +            throw new IllegalArgumentException("Connection may not be null");
  +        }
  +        if (request == null) {
  +            throw new IllegalArgumentException("Request may not be null");
  +        }
       	boolean complete = false;
  -        if (requestHandler != null) {
  -            complete = requestHandler.processRequest(conn);
  +        if (this.requestHandler != null) {
  +            complete = requestHandler.processRequest(conn, request);
  +            if (complete) {
  +                return;
  +            }
  +        }
  +        SimpleResponse response = null;
  +        if (this.serivce != null) {
  +            response = new SimpleResponse();
  +            complete = this.serivce.process(request, response);
           }
           if (!complete) {
  +            response = ErrorResponse.getInstance().
  +                getResponse(HttpStatus.SC_SERVICE_UNAVAILABLE);
               conn.connectionClose();
  -            ErrorResponse.getInstance().getResponse(HttpStatus.SC_SERVICE_UNAVAILABLE).processRequest(conn);
           }
  +        writeResponse(conn, response);
  +    }
  +
  +    public void writeResponse(
  +        final SimpleHttpServerConnection conn, 
  +        final SimpleResponse response) throws IOException 
  +   {
  +        if (response == null) {
  +            return;
  +        }
  +        ResponseWriter out = conn.getWriter();
  +        if (!response.containsHeader("Content-Length")) {
  +            int len = 0;
  +            if (response.getBodyString() != null) {
  +                len = response.getBodyString().length();
  +            }
  +            response.addHeader(
  +                new Header("Content-Length", Integer.toString(len), true)); 
  +        }
  +        if (!response.containsHeader("Content-Type")) {
  +            StringBuffer buffer = new StringBuffer();
  +            if (response.getContentType() != null) {
  +                buffer.append(response.getContentType());
  +                if (out.getEncoding() != null) {
  +                    buffer.append("; charset=");
  +                    buffer.append(out.getEncoding());
  +                }
  +            }
  +            response.addHeader(
  +                new Header("Content-Type", buffer.toString(), true)); 
  +        }
  +        // @TODO implement HTTP/1.1 persistent connections
  +        if (!conn.isKeepAlive()) {
  +            response.setHeader(
  +                new Header("Connection", "close", true)); 
  +        }
  +        out.println(response.getStatusLine());
  +        Iterator item = response.getHeaderIterator();
  +        while (item.hasNext()) {
  +            Header header = (Header) item.next();
  +            out.print(header.toExternalForm());
  +        }
  +        out.println();
  +        if (response.getBodyString() != null) {
  +            out.print(response.getBodyString());   
  +        }
  +        out.flush();
       }
   
       public void run() {
  
  
  
  1.7       +18 -35    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.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- SimpleHttpServerConnection.java	25 Feb 2004 22:33:59 -0000	1.6
  +++ SimpleHttpServerConnection.java	27 Feb 2004 19:01:34 -0000	1.7
  @@ -38,7 +38,6 @@
   import java.net.Socket;
   import java.net.SocketException;
   
  -import org.apache.commons.httpclient.Header;
   import org.apache.commons.httpclient.HttpParser;
   import org.apache.commons.httpclient.HttpStatus;
   import org.apache.commons.logging.Log;
  @@ -63,10 +62,6 @@
   
       private boolean keepAlive = false;
   
  -    private RequestLine requestLine;
  -
  -    private Header[] headers;
  -
       public SimpleHttpServerConnection(SimpleHttpServer server, Socket socket) throws IOException
{
           this.server = server;
           this.socket = socket;
  @@ -157,41 +152,23 @@
               return;
           }
   
  +        SimpleRequest request = null;
           try {
  -            requestLine = RequestLine.parseLine(line);
  -            headers = HttpParser.parseHeaders(in, HTTP_ELEMENT_CHARSET);
  +            request = new SimpleRequest( 
  +                RequestLine.parseLine(line),
  +                HttpParser.parseHeaders(in, HTTP_ELEMENT_CHARSET),
  +                null);
           } catch (IOException e) {
               connectionClose();
  -            ErrorResponse.getInstance().getResponse(HttpStatus.SC_BAD_REQUEST).processRequest(this);
  +            SimpleResponse response = ErrorResponse.getInstance().
  +                getResponse(HttpStatus.SC_BAD_REQUEST);
  +            server.writeResponse(this, response);
               return;
           }
  -        server.processRequest(this);
  +        server.processRequest(this, request);
           out.flush();
       }
   
  -    public Header[] getHeaders() {
  -        Header[] copy = new Header[headers.length];
  -        System.arraycopy(headers, 0, copy, 0, headers.length);
  -        return copy;
  -    }
  -
  -    public Header getHeader(final String name) {
  -        if (name == null) {
  -            throw new IllegalArgumentException("Header name may not be null");
  -        }
  -        for (int i = 0; i < this.headers.length; i++) {
  -            Header header = this.headers[i];
  -            if (name.equalsIgnoreCase(header.getName())) {
  -                return header;
  -            }
  -        }
  -        return null;
  -    }
  -
  -    public RequestLine getRequestLine() {
  -        return requestLine;
  -    }
  -
       public InputStream getInputStream() {
           return in;
       }
  @@ -199,4 +176,10 @@
       public OutputStream getOutputStream() {
           return out;
       }
  +
  +    public boolean isKeepAlive() {
  +        return this.keepAlive;
  +    }
  +
   }
  +    
  \ No newline at end of file
  
  
  
  1.6       +8 -5      jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/server/TransparentProxyRequestHandler.java
  
  Index: TransparentProxyRequestHandler.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/server/TransparentProxyRequestHandler.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- TransparentProxyRequestHandler.java	22 Feb 2004 18:08:52 -0000	1.5
  +++ TransparentProxyRequestHandler.java	27 Feb 2004 19:01:34 -0000	1.6
  @@ -56,8 +56,11 @@
        * 
        * @see org.apache.commons.httpclient.server.HttpRequestHandler#processRequest(org.apache.commons.httpclient.server.SimpleHttpServerConnection)
        */
  -    public boolean processRequest(SimpleHttpServerConnection conn) throws IOException {
  -        RequestLine line = conn.getRequestLine();
  +    public boolean processRequest(
  +        final SimpleHttpServerConnection conn,
  +        final SimpleRequest request) throws IOException
  +    {
  +        RequestLine line = request.getRequestLine();
           String method = line.getMethod();
           if (!"CONNECT".equalsIgnoreCase(method))
               return false;
  
  
  

---------------------------------------------------------------------
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