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 HttpServiceHandler.java SimpleHttpServer.java SimpleHttpServerConnection.java SimpleResponse.java
Date Sat, 06 Nov 2004 15:50:33 GMT
olegk       2004/11/06 07:50:33

  Modified:    httpclient/src/test/org/apache/commons/httpclient
                        TestHttpMethodFundamentals.java
               httpclient/src/test/org/apache/commons/httpclient/server
                        HttpServiceHandler.java SimpleHttpServer.java
                        SimpleHttpServerConnection.java SimpleResponse.java
  Log:
  * Chunk encoding support for SimpleHttpServer
  * Various minor code cleanups
  
  Contributed by Oleg Kalnichevski
  
  Revision  Changes    Path
  1.5       +4 -5      jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestHttpMethodFundamentals.java
  
  Index: TestHttpMethodFundamentals.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestHttpMethodFundamentals.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- TestHttpMethodFundamentals.java	1 Nov 2004 02:21:15 -0000	1.4
  +++ TestHttpMethodFundamentals.java	6 Nov 2004 15:50:32 -0000	1.5
  @@ -122,7 +122,6 @@
               response.addHeader(new Header("Content-Type", "text/plain"));            
               response.addHeader(new Header("Transfer-Encoding", "chunked")); 
               response.addHeader(new Header("Connection", "close"));            
  -            response.setBodyString(null);
               return true;
           }
       }
  
  
  
  1.3       +29 -18    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.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- HttpServiceHandler.java	4 Nov 2004 19:40:51 -0000	1.2
  +++ HttpServiceHandler.java	6 Nov 2004 15:50:33 -0000	1.3
  @@ -32,6 +32,8 @@
   import java.io.IOException;
   
   import org.apache.commons.httpclient.Header;
  +import org.apache.commons.httpclient.HttpVersion;
  +import org.apache.commons.httpclient.methods.RequestEntity;
   
   /**
    * This request handler provides service interface similar to that of Servlet API.
  @@ -65,24 +67,33 @@
           
           // Ensure there's a content type header
           if (!response.containsHeader("Content-Type")) {
  -            Header header = new Header("Content-Type", "text/plain; charset=ISO-8859-1");

  -            response.addHeader(header);
  +            if (response.getEntity() != null) {
  +                Header header = new Header("Content-Type", response.getEntity().getContentType());

  +                response.addHeader(header);
  +            }
           }
           
  -        // Ensure there's a content length header
  -        if (!response.containsHeader("Content-Length")) {
  -
  -            String charset = request.getCharSet();
  -            if (charset == null) {
  -                charset = "ISO-8859-1";
  -            }
  -            int len = 0;
  -            if (response.getBodyString() != null) {
  -                byte[] rawbody = response.getBodyString().getBytes(charset);
  -                len = rawbody.length;
  +        // Ensure there's a content length or transfer encoding header
  +        if (!response.containsHeader("Content-Length") && !response.containsHeader("Transfer-Encoding"))
{
  +            RequestEntity entity = response.getEntity();
  +            if (entity != null) {
  +                long len = entity.getContentLength();
  +                if (len < 0) {
  +                    if (response.getHttpVersion().lessEquals(HttpVersion.HTTP_1_0)) {
  +                        throw new IOException("Chunked encoding not supported for HTTP
version " 
  +                                + response.getHttpVersion());
  +                    }
  +                    Header header = new Header("Transfer-Encoding", "chunked"); 
  +                    response.addHeader(header);                
  +                } else {
  +                    Header header = new Header("Content-Length", Long.toString(len)); 
  +                    response.addHeader(header);
  +                }
  +                
  +            } else {
  +                Header header = new Header("Content-Length", "0"); 
  +                response.addHeader(header);                
               }
  -            Header header = new Header("Content-Length", Integer.toString(len)); 
  -            response.addHeader(header);
           }
   
           Header connheader = response.getFirstHeader("Connection");
  
  
  
  1.11      +6 -11     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.10
  retrieving revision 1.11
  diff -u -r1.10 -r1.11
  --- SimpleHttpServer.java	4 Nov 2004 19:40:51 -0000	1.10
  +++ SimpleHttpServer.java	6 Nov 2004 15:50:33 -0000	1.11
  @@ -57,7 +57,6 @@
       private Set connections = new HashSet();
   
       private HttpRequestHandler requestHandler = null;
  -    private HttpService service = null;
   
       /**
        * Creates a new HTTP server instance, using an arbitrary free TCP port
  @@ -173,7 +172,7 @@
       }
   
       public void setHttpService(HttpService service) {
  -        this.service = service;
  +        setRequestHandler(new HttpServiceHandler(service));
       }
   
       public void removeConnection(SimpleHttpServerConnection conn) {
  @@ -185,16 +184,12 @@
               while (!Thread.interrupted()) {
                   Socket socket = listener.accept();
                   try {
  -                    HttpRequestHandler handler = this.requestHandler;
  -                    if (handler == null && this.service != null) {
  -                        handler = new HttpServiceHandler(this.service);
  -                    }
  -                    if (handler == null) {
  +                    if (this.requestHandler == null) {
                           socket.close();
                           break;
                       }
                       SimpleHttpServerConnection conn = 
  -                        new SimpleHttpServerConnection(socket, handler);
  +                        new SimpleHttpServerConnection(socket, this.requestHandler);
                       connections.add(conn);
   
                       Thread t = new Thread(tg, conn, "SimpleHttpServer connection");
  
  
  
  1.15      +28 -8     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.14
  retrieving revision 1.15
  diff -u -r1.14 -r1.15
  --- SimpleHttpServerConnection.java	4 Nov 2004 19:40:51 -0000	1.14
  +++ SimpleHttpServerConnection.java	6 Nov 2004 15:50:33 -0000	1.15
  @@ -32,18 +32,21 @@
   import java.io.ByteArrayOutputStream;
   import java.io.IOException;
   import java.io.InputStream;
  +import java.io.InterruptedIOException;
   import java.io.OutputStream;
   import java.io.UnsupportedEncodingException;
   import java.net.Socket;
   import java.util.Iterator;
   
   import org.apache.commons.httpclient.ChunkedInputStream;
  +import org.apache.commons.httpclient.ChunkedOutputStream;
   import org.apache.commons.httpclient.ContentLengthInputStream;
   import org.apache.commons.httpclient.Header;
   import org.apache.commons.httpclient.HeaderGroup;
   import org.apache.commons.httpclient.HttpException;
   import org.apache.commons.httpclient.HttpParser;
   import org.apache.commons.httpclient.HttpStatus;
  +import org.apache.commons.httpclient.methods.RequestEntity;
   import org.apache.commons.logging.Log;
   import org.apache.commons.logging.LogFactory;
   
  @@ -161,9 +164,15 @@
                   getResponse(HttpStatus.SC_BAD_REQUEST);
               writeResponse(response);
               return;
  +        } catch (InterruptedIOException e) {
  +            connectionClose();
  +            LOG.debug("Connection timed out", e);
  +            return;
           } catch (IOException e) {
               connectionClose();
  -            LOG.error("I/O error processing request", e);
  +            if (!this.destroyed) {
  +                LOG.error("I/O error processing request", e);
  +            }
               return;
           }
       }
  @@ -172,7 +181,7 @@
           if (response == null) {
               return;
           }
  -        ResponseWriter writer = new ResponseWriter(this.out, response.getCharset());
  +        ResponseWriter writer = new ResponseWriter(this.out, "US-ASCII");
           writer.println(response.getStatusLine());
           Iterator item = response.getHeaderIterator();
           while (item.hasNext()) {
  @@ -180,10 +189,21 @@
               writer.print(header.toExternalForm());
           }
           writer.println();
  -        if (response.getBodyString() != null) {
  -            writer.print(response.getBodyString());   
  -        }
           writer.flush();
  +        
  +        OutputStream outsream = this.out;
  +        Header transferenc = response.getFirstHeader("Transfer-Encoding");
  +        if (transferenc != null) {
  +            if (transferenc.getValue().indexOf("chunked") != -1) {
  +                outsream = new ChunkedOutputStream(outsream);
  +            }
  +        }
  +        
  +        RequestEntity entity = response.getEntity(); 
  +        if (entity != null) {
  +            entity.writeRequest(this.out); 
  +        }
  +        this.out.flush();
       }
       
       /**
  
  
  
  1.5       +16 -12    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.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- SimpleResponse.java	4 Nov 2004 19:40:51 -0000	1.4
  +++ SimpleResponse.java	6 Nov 2004 15:50:33 -0000	1.5
  @@ -25,8 +25,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.server;
  @@ -39,6 +37,8 @@
   import org.apache.commons.httpclient.HttpStatus;
   import org.apache.commons.httpclient.HttpVersion;
   import org.apache.commons.httpclient.NameValuePair;
  +import org.apache.commons.httpclient.methods.RequestEntity;
  +import org.apache.commons.httpclient.methods.StringRequestEntity;
   
   /**
    * A generic HTTP response.
  @@ -52,7 +52,7 @@
       private int statuscode = HttpStatus.SC_OK;
       private String phrase = HttpStatus.getStatusText(HttpStatus.SC_OK);
       private HeaderGroup headers = new HeaderGroup();
  -    private String bodyString = null;
  +    private RequestEntity entity = null;
   
       public SimpleResponse() {
           super();
  @@ -90,14 +90,10 @@
           return this.ver;
       }
   
  -    public void setBodyString(String string) {
  -        this.bodyString = string;
  +    public void setBodyString(final String string) {
  +        this.entity = new StringRequestEntity(string);
       }
       
  -    public String getBodyString() {
  -        return this.bodyString;
  -    }
  -
       public String getStatusLine() {
           StringBuffer buffer = new StringBuffer();
           buffer.append(this.ver);
  @@ -154,6 +150,9 @@
       public String getCharset() {
           String charset = "ISO-8859-1";
           Header content = this.headers.getFirstHeader("Content-Type");
  +        if (content == null && this.entity != null) {
  +            content = new Header("Content-Type", this.entity.getContentType());
  +        }
           if (content != null) {
               HeaderElement values[] = content.getElements();
               if (values.length == 1) {
  @@ -165,4 +164,9 @@
           }
           return charset;
       }
  +
  +    public RequestEntity getEntity() {
  +        return this.entity;
  +    }
  +    
   }
  
  
  

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