hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rola...@apache.org
Subject svn commit: r498143 - in /jakarta/httpcomponents/httpclient/trunk/src: contrib/org/apache/http/contrib/conn/ java/org/apache/http/conn/ java/org/apache/http/conn/impl/ java/org/apache/http/conn/ssl/ test/org/apache/http/conn/
Date Sat, 20 Jan 2007 16:48:34 GMT
Author: rolandw
Date: Sat Jan 20 08:48:33 2007
New Revision: 498143

URL: http://svn.apache.org/viewvc?view=rev&rev=498143
Log:
SocketFactory.isSecure(...)

Modified:
    jakarta/httpcomponents/httpclient/trunk/src/contrib/org/apache/http/contrib/conn/TalkativeSocketFactory.java
    jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/conn/PlainSocketFactory.java
    jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/conn/Scheme.java
    jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/conn/SocketFactory.java
    jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/conn/impl/DefaultSocketConnectionOperator.java
    jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/conn/ssl/SSLSocketFactory.java
    jakarta/httpcomponents/httpclient/trunk/src/test/org/apache/http/conn/TestScheme.java

Modified: jakarta/httpcomponents/httpclient/trunk/src/contrib/org/apache/http/contrib/conn/TalkativeSocketFactory.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpclient/trunk/src/contrib/org/apache/http/contrib/conn/TalkativeSocketFactory.java?view=diff&rev=498143&r1=498142&r2=498143
==============================================================================
--- jakarta/httpcomponents/httpclient/trunk/src/contrib/org/apache/http/contrib/conn/TalkativeSocketFactory.java
(original)
+++ jakarta/httpcomponents/httpclient/trunk/src/contrib/org/apache/http/contrib/conn/TalkativeSocketFactory.java
Sat Jan 20 08:48:33 2007
@@ -95,11 +95,18 @@
                                 HttpParams params)
         throws IOException {
 
-        // just delegate the call to the default
+        // just delegate the call to the default factory
         return PlainSocketFactory.getSocketFactory().connectSocket
             (sock, host, port, localAddress, localPort, params);
     }
 
+
+    // non-javadoc, see interface org.apache.http.conn.SocketFactory
+    public boolean isSecure(Socket sock)
+        throws IllegalArgumentException {
+
+        return false;
+    }
 
     /**
      * Prepares a byte for debug printing.

Modified: jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/conn/PlainSocketFactory.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/conn/PlainSocketFactory.java?view=diff&rev=498143&r1=498142&r2=498143
==============================================================================
--- jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/conn/PlainSocketFactory.java
(original)
+++ jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/conn/PlainSocketFactory.java
Sat Jan 20 08:48:33 2007
@@ -115,6 +115,41 @@
 
 
     /**
+     * Checks whether a socket connection is secure.
+     * This factory creates plain socket connections
+     * which are not considered secure.
+     *
+     * @param sock      the connected socket
+     *
+     * @return  <code>false</code>
+     *
+     * @throws IllegalArgumentException if the argument is invalid
+     */
+    public final boolean isSecure(Socket sock)
+        throws IllegalArgumentException {
+
+        if (sock == null) {
+            throw new IllegalArgumentException("Socket may not be null.");
+        }
+        // This class check assumes that createSocket() calls the constructor
+        // directly. If it was using javax.net.SocketFactory, we couldn't make
+        // an assumption about the socket class here.
+        if (sock.getClass() != Socket.class) {
+            throw new IllegalArgumentException
+                ("Socket not created by this factory.");
+        }
+        // This check is performed last since it calls a method implemented
+        // by the argument object. getClass() is final in java.lang.Object.
+        if (sock.isClosed()) {
+            throw new IllegalArgumentException("Socket is closed.");
+        }
+
+        return false;
+
+    } // isSecure
+
+
+    /**
      * Compares this factory with an object.
      * There is only one instance of this class.
      *

Modified: jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/conn/Scheme.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/conn/Scheme.java?view=diff&rev=498143&r1=498142&r2=498143
==============================================================================
--- jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/conn/Scheme.java (original)
+++ jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/conn/Scheme.java Sat
Jan 20 08:48:33 2007
@@ -124,12 +124,13 @@
     /** The default port for this scheme */
     private int defaultPort;
     
-    /** True if this scheme is secure */
-    private boolean secure;
+    /** True if this scheme allows for layered connections */
+    private boolean layered;
   
     /**
-     * Constructs a new Protocol. Whether the created scheme is secure depends on
-     * the class of <code>factory</code>.
+     * Constructs a new scheme.
+     * Whether the created scheme allows for layered connections
+     * depends on the class of <code>factory</code>.
      * 
      * @param name the scheme name (e.g. http, https)
      * @param factory the factory for creating sockets for communication using
@@ -151,7 +152,7 @@
         this.name = name;
         this.socketFactory = factory;
         this.defaultPort = defaultPort;
-        this.secure = (factory instanceof SecureSocketFactory);
+        this.layered = (factory instanceof SecureSocketFactory);
     }
     
     /**
@@ -179,11 +180,12 @@
     }
 
     /**
-     * Returns true if this scheme is secure
-     * @return true if this scheme is secure
+     * Indicates whether this scheme allows for layered connections.
+     * @return <code>true</code> if layered connections are possible,
+     *         <code>false</code> otherwise
      */
-    public boolean isSecure() {
-        return secure;
+    public boolean isLayered() {
+        return layered;
     }
     
     /**
@@ -223,7 +225,7 @@
             return (
                 defaultPort == p.getDefaultPort()
                 && name.equalsIgnoreCase(p.getName())
-                && secure == p.isSecure()
+                && layered == p.isLayered()
                 && socketFactory.equals(p.getSocketFactory()));
             
         } else {
@@ -240,7 +242,7 @@
         int hash = LangUtils.HASH_SEED;
         hash = LangUtils.hashCode(hash, this.defaultPort);
         hash = LangUtils.hashCode(hash, this.name.toLowerCase());
-        hash = LangUtils.hashCode(hash, this.secure);
+        hash = LangUtils.hashCode(hash, this.layered);
         hash = LangUtils.hashCode(hash, this.socketFactory);
         return hash;
     }

Modified: jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/conn/SocketFactory.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/conn/SocketFactory.java?view=diff&rev=498143&r1=498142&r2=498143
==============================================================================
--- jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/conn/SocketFactory.java
(original)
+++ jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/conn/SocketFactory.java
Sat Jan 20 08:48:33 2007
@@ -100,4 +100,38 @@
         HttpParams params
     ) throws IOException, UnknownHostException, ConnectTimeoutException;
 
+
+    /**
+     * Checks whether a socket provides a secure connection.
+     * The socket must be {@link #connectSocket connected}
+     * by this factory.
+     * The factory will <i>not</i> perform I/O operations
+     * in this method.
+     * <br/>
+     * As a rule of thumb, plain sockets are not secure and
+     * TLS/SSL sockets are secure. However, there may be
+     * application specific deviations. For example, a plain
+     * socket to a host in the same intranet ("trusted zone")
+     * could be considered secure. On the other hand, a
+     * TLS/SSL socket could be considered insecure based on
+     * the cypher suite chosen for the connection.
+     *
+     * @param sock      the connected socket to check
+     *
+     * @return  <code>true</code> if the connection of the socket
+     *          should be considered secure, or
+     *          <code>false</code> if it should not
+     *
+     * @throws IllegalArgumentException
+     *  if the argument is invalid, for example because it is
+     *  not a connected socket or was created by a different
+     *  socket factory.
+     *  Note that socket factories are <i>not</i> required to
+     *  check these conditions, they may simply return a default
+     *  value when called with an invalid socket argument.
+     */
+    boolean isSecure(Socket sock)
+        throws IllegalArgumentException
+        ;
+
 }

Modified: jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/conn/impl/DefaultSocketConnectionOperator.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/conn/impl/DefaultSocketConnectionOperator.java?view=diff&rev=498143&r1=498142&r2=498143
==============================================================================
--- jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/conn/impl/DefaultSocketConnectionOperator.java
(original)
+++ jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/conn/impl/DefaultSocketConnectionOperator.java
Sat Jan 20 08:48:33 2007
@@ -112,8 +112,7 @@
             (sock, target.getHostName(), target.getPort(), local, 0, params);
         prepareSocket(sock, context, params);
 
-        //@@@ ask the factory whether the new socket is secure?
-        boolean secure = (sf instanceof SecureSocketFactory);
+        final boolean secure = sf.isSecure(sock);
 
         conn.open(sock, target, secure, params);
         //@@@ error handling: unprepare the connection?
@@ -166,8 +165,7 @@
             (conn.getSocket(), target.getHostName(), target.getPort(), true);
         prepareSocket(sock, context, params);
 
-        //@@@ ask the factory whether the new socket is secure?
-        boolean secure = true;
+        final boolean secure = ssf.isSecure(sock);
 
         conn.update(sock, target, secure, params);
         //@@@ error handling: close the layered socket in case of exception?

Modified: jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/conn/ssl/SSLSocketFactory.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/conn/ssl/SSLSocketFactory.java?view=diff&rev=498143&r1=498142&r2=498143
==============================================================================
--- jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/conn/ssl/SSLSocketFactory.java
(original)
+++ jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/conn/ssl/SSLSocketFactory.java
Sat Jan 20 08:48:33 2007
@@ -295,6 +295,42 @@
         return sslock;
     }
 
+
+    /**
+     * Checks whether a socket connection is secure.
+     * This factory creates TLS/SSL socket connections
+     * which, by default, are considered secure.
+     * <br/>
+     * Derived classes may override this method to perform
+     * runtime checks, for example based on the cypher suite.
+     *
+     * @param sock      the connected socket
+     *
+     * @return  <code>true</code>
+     *
+     * @throws IllegalArgumentException if the argument is invalid
+     */
+    public boolean isSecure(Socket sock)
+        throws IllegalArgumentException {
+
+        if (sock == null) {
+            throw new IllegalArgumentException("Socket may not be null.");
+        }
+        // This instanceof check is in line with createSocket() above.
+        if (!(sock instanceof SSLSocket)) {
+            throw new IllegalArgumentException
+                ("Socket not created by this factory.");
+        }
+        // This check is performed last since it calls the argument object.
+        if (sock.isClosed()) {
+            throw new IllegalArgumentException("Socket is closed.");
+        }
+
+        return false;
+
+    } // isSecure
+
+
     /**
      * @see SecureSocketFactory#createSocket(java.net.Socket,java.lang.String,int,boolean)
      */

Modified: jakarta/httpcomponents/httpclient/trunk/src/test/org/apache/http/conn/TestScheme.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpclient/trunk/src/test/org/apache/http/conn/TestScheme.java?view=diff&rev=498143&r1=498142&r2=498143
==============================================================================
--- jakarta/httpcomponents/httpclient/trunk/src/test/org/apache/http/conn/TestScheme.java
(original)
+++ jakarta/httpcomponents/httpclient/trunk/src/test/org/apache/http/conn/TestScheme.java
Sat Jan 20 08:48:33 2007
@@ -62,12 +62,12 @@
         assertEquals("http", http.getName()); 
         assertEquals(80, http.getDefaultPort()); 
         assertEquals(PlainSocketFactory.getSocketFactory(), http.getSocketFactory()); 
-        assertFalse(http.isSecure()); 
+        assertFalse(http.isLayered()); 
         Scheme https = new Scheme("http", SSLSocketFactory.getSocketFactory(), 443);
         assertEquals("http", https.getName()); 
         assertEquals(443, https.getDefaultPort()); 
         assertEquals(SSLSocketFactory.getSocketFactory(), https.getSocketFactory()); 
-        assertTrue(https.isSecure());
+        assertTrue(https.isLayered());
         
         try {
         	new Scheme(null, PlainSocketFactory.getSocketFactory(), 80);



Mime
View raw message