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 SimpleConnectionThread.java HttpServiceHandler.java ProxyAuthRequestHandler.java ProxyRequestHandler.java SimpleConnPool.java SimpleHttpServer.java SimpleHttpServerConnection.java
Date Sun, 07 Nov 2004 19:57:09 GMT
olegk       2004/11/07 11:57:09

  Modified:    httpclient/src/test/org/apache/commons/httpclient
                        TestBadContentLength.java
               httpclient/src/test/org/apache/commons/httpclient/server
                        HttpServiceHandler.java
                        ProxyAuthRequestHandler.java
                        ProxyRequestHandler.java SimpleConnPool.java
                        SimpleHttpServer.java
                        SimpleHttpServerConnection.java
  Added:       httpclient/src/test/org/apache/commons/httpclient/server
                        SimpleConnectionThread.java
  Log:
  Threading logic factored out of SimpleHttpServerConnection into a separate class
  
  Contributed by Oleg Kalnichevski
  
  Revision  Changes    Path
  1.9       +5 -5      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.8
  retrieving revision 1.9
  diff -u -r1.8 -r1.9
  --- TestBadContentLength.java	7 Nov 2004 12:31:42 -0000	1.8
  +++ TestBadContentLength.java	7 Nov 2004 19:57:08 -0000	1.9
  @@ -196,7 +196,7 @@
                   out.flush();
                   // process max. 2 subsequents requests per connection
                   if (requestNo < 2) {
  -                    conn.connectionKeepAlive();
  +                    conn.connectionKeepAlive(true);
                   }
               }
               return true;
  
  
  
  1.6       +7 -7      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.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- HttpServiceHandler.java	7 Nov 2004 12:31:42 -0000	1.5
  +++ HttpServiceHandler.java	7 Nov 2004 19:57:08 -0000	1.6
  @@ -102,19 +102,19 @@
                   if (connheader.getValue().equalsIgnoreCase("keep-alive")) {
                       Header header = new Header("Connection", "keep-alive"); 
                       response.addHeader(header);
  -                    conn.connectionKeepAlive();
  +                    conn.connectionKeepAlive(true);
                   }
                   if (connheader.getValue().equalsIgnoreCase("close")) {
                       Header header = new Header("Connection", "close"); 
                       response.addHeader(header);
  -                    conn.connectionClose();
  +                    conn.connectionKeepAlive(false);
                   }
               } else {
                   // Use protocol default connection policy
                   if (response.getHttpVersion().greaterEquals(HttpVersion.HTTP_1_1)) {
  -                    conn.connectionKeepAlive();
  +                    conn.connectionKeepAlive(true);
                   } else {
  -                    conn.connectionClose();
  +                    conn.connectionKeepAlive(false);
                   }
               }
           }
  
  
  
  1.10      +5 -5      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.9
  retrieving revision 1.10
  diff -u -r1.9 -r1.10
  --- ProxyAuthRequestHandler.java	27 Feb 2004 19:01:34 -0000	1.9
  +++ ProxyAuthRequestHandler.java	7 Nov 2004 19:57:08 -0000	1.10
  @@ -79,7 +79,7 @@
           if (clientAuth != null) {
               boolean ok = checkAuthorization(clientAuth);
               if (ok)
  -                conn.connectionKeepAlive();
  +                conn.connectionKeepAlive(true);
               return !ok;
           } else {
               performHandshake(conn);
  @@ -99,7 +99,7 @@
           out.print(new Header("Content-Length", "0").toExternalForm());
           out.println();
           out.flush();
  -        conn.connectionKeepAlive();
  +        conn.connectionKeepAlive(true);
       }
   
       /**
  
  
  
  1.8       +4 -4      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.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- ProxyRequestHandler.java	7 Nov 2004 12:31:42 -0000	1.7
  +++ ProxyRequestHandler.java	7 Nov 2004 19:57:08 -0000	1.8
  @@ -141,7 +141,7 @@
               
               Header connheader = new Header("Proxy-Connection", "close"); 
               response.setHeader(connheader);
  -            conn.connectionClose();
  +            conn.connectionKeepAlive(false);
               
               conn.writeResponse(response);
               
  
  
  
  1.2       +4 -4      jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/server/SimpleConnPool.java
  
  Index: SimpleConnPool.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/server/SimpleConnPool.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- SimpleConnPool.java	7 Nov 2004 12:31:42 -0000	1.1
  +++ SimpleConnPool.java	7 Nov 2004 19:57:08 -0000	1.2
  @@ -63,7 +63,7 @@
           for (Iterator it = connections.iterator(); it.hasNext();) {
               SimpleHttpServerConnection conn =
                   (SimpleHttpServerConnection) it.next();
  -            conn.destroy();
  +            conn.close();
           }
       }
   
  
  
  
  1.13      +11 -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.12
  retrieving revision 1.13
  diff -u -r1.12 -r1.13
  --- SimpleHttpServer.java	7 Nov 2004 12:31:42 -0000	1.12
  +++ SimpleHttpServer.java	7 Nov 2004 19:57:08 -0000	1.13
  @@ -186,14 +186,15 @@
                           socket.close();
                           break;
                       }
  -                    SimpleHttpServerConnection conn = new SimpleHttpServerConnection(
  -                                this.testname + " connection " + this.count,
  -                                socket, 
  -                                this.connections, 
  -                                this.requestHandler);
  -                    connections.addConnection(conn);
  +                    SimpleHttpServerConnection conn = new SimpleHttpServerConnection(socket);

  +                    this.connections.addConnection(conn);
   
  -                    Thread t = new Thread(tg, conn, this.testname + " thread " + this.count);
  +                    Thread t = new SimpleConnectionThread(
  +                            tg,
  +                            this.testname + " thread " + this.count,
  +                            conn, 
  +                            this.connections,
  +                            this.requestHandler);
                       t.setDaemon(true);
                       t.start();
                   } catch (IOException e) {
  
  
  
  1.18      +15 -88    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.17
  retrieving revision 1.18
  diff -u -r1.17 -r1.18
  --- SimpleHttpServerConnection.java	7 Nov 2004 12:31:42 -0000	1.17
  +++ SimpleHttpServerConnection.java	7 Nov 2004 19:57:08 -0000	1.18
  @@ -32,7 +32,6 @@
   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;
  @@ -43,9 +42,7 @@
   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;
  @@ -56,54 +53,31 @@
    * @author Christian Kohlschuetter
    * @author Oleg Kalnichevski
    */
  -public class SimpleHttpServerConnection implements Runnable {
  +public class SimpleHttpServerConnection {
   
       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 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 String name,
  -            final Socket socket,
  -            final SimpleConnPool connpool,
  -            final HttpRequestHandler handler) 
  +
  +    public SimpleHttpServerConnection(final Socket socket) 
       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(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; 
  +    public synchronized void close() {
           try {
               if (socket != null) {
                   in.close();
  @@ -112,45 +86,11 @@
                   socket = null;
               }
           } catch (IOException e) {
  -            // fail("Unexpected exception: " + e);
  -        }
  -    }
  -
  -    public void run() {
  -        try {
  -            do {
  -                this.keepAlive = false;
  -                readRequest();
  -                if (this.request != null) {
  -                    this.handler.processRequest(this, this.request);
  -                    this.out.flush();
  -                }
  -            } while (this.keepAlive);
  -        } catch (IOException e) {
  -            if (!this.destroyed && LOG.isWarnEnabled()) {
  -                LOG.warn("[" + this.name + "] I/O error: " + e.getMessage());
  -            }
  -        } finally {
  -            destroy();
  -            this.connpool.removeConnection(this);
           }
       }
   
  -    /**
  -     * Requests to close connection after processing this request.
  -     */
  -    public void connectionClose() {
  -        keepAlive = false;
  -    }
  -
  -    /**
  -     * Requests to keep the connection alive after processing this request
  -     * (must be re-issued for every request if permanent keep-alive is
  -     * desired).
  -     *  
  -     */
  -    public void connectionKeepAlive() {
  -        keepAlive = true;
  +    public void connectionKeepAlive(boolean b) {
  +        this.keepAlive = b;
       }
   
       public boolean isKeepAlive() {
  @@ -166,7 +106,7 @@
           return new ResponseWriter(out);
       }
   
  -    private void readRequest() throws IOException {
  +    public SimpleRequest readRequest() throws IOException {
           try {
               String line = null;
               do {
  @@ -174,28 +114,15 @@
               } while (line != null && line.length() == 0);
   
               if (line == null) {
  -                connectionClose();
  -                return;
  +                connectionKeepAlive(false);
  +                return null;
               }
               RequestLine statusLine = RequestLine.parseLine(line);
               Header[] headers = HttpParser.parseHeaders(in, HTTP_ELEMENT_CHARSET);
  -            this.request = new SimpleRequest(statusLine, headers, getBody(statusLine, headers,
in));
  -        } catch (HttpException e) {
  -            connectionClose();
  -            SimpleResponse response = ErrorResponse.getInstance().
  -                getResponse(HttpStatus.SC_BAD_REQUEST);
  -            writeResponse(response);
  -            return;
  -        } catch (InterruptedIOException e) {
  -            connectionClose();
  -            LOG.debug("Connection timed out", e);
  -            return;
  +            return new SimpleRequest(statusLine, headers, getBody(statusLine, headers,
in));
           } catch (IOException e) {
  -            connectionClose();
  -            if (!this.destroyed) {
  -                LOG.error("I/O error processing request", e);
  -            }
  -            return;
  +            close();
  +            throw e;
           }
       }
   
  
  
  
  1.1                  jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/server/SimpleConnectionThread.java
  
  Index: SimpleConnectionThread.java
  ===================================================================
  /*
   * $Header: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/server/SimpleConnectionThread.java,v
1.1 2004/11/07 19:57:08 olegk Exp $
   * $Revision: 1.1 $
   * $Date: 2004/11/07 19:57:08 $
   *
   * ====================================================================
   *
   *  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.io.IOException;
  
  import org.apache.commons.logging.Log;
  import org.apache.commons.logging.LogFactory;
  
  /**
   * Simple HTTP connection thread.
   * 
   * @author Christian Kohlschuetter
   * @author Oleg Kalnichevski
   */
  public class SimpleConnectionThread extends Thread {
  
      private static final Log LOG = LogFactory.getLog(SimpleConnectionThread.class);
      
      private static final String HTTP_ELEMENT_CHARSET = "US-ASCII";
      public static final String DEFAULT_CONTENT_CHARSET = "ISO-8859-1";
  
      private SimpleHttpServerConnection conn = null;
      private SimpleConnPool connpool = null;    
      private HttpRequestHandler handler = null;
      transient boolean stopped; 
  
      public SimpleConnectionThread(
              final ThreadGroup tg,
              final String name,
              final SimpleHttpServerConnection conn,
              final SimpleConnPool connpool,
              final HttpRequestHandler handler) 
      throws IOException {
          super(tg, name);
          if (conn == null) {
              throw new IllegalArgumentException("Connection 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.conn = conn;
          this.connpool = connpool;
          this.handler = handler;
          this.stopped = false; 
      }
  
      public synchronized void destroy() {
          if (this.stopped) {
              return;
          }
          this.stopped = true; 
          if (conn != null) {
              conn.close();
              conn = null;
          }
          interrupt();
      }
  
      public void run() {
          try {
              do {
                  this.conn.connectionKeepAlive(false);
                  SimpleRequest request = this.conn.readRequest();
                  if (request != null) {
                      this.handler.processRequest(this.conn, request);
                  }
              } while (this.conn.isKeepAlive());
          } catch (IOException e) {
              if (!this.stopped && !isInterrupted() && LOG.isWarnEnabled())
{
                  LOG.warn("[" + getName() + "] I/O error: " + e.getMessage());
              }
          } finally {
              destroy();
              this.connpool.removeConnection(this.conn);
          }
      }
  
  }
      
  
  

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