hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From asan...@apache.org
Subject svn commit: r891654 - /httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/impl/nio/reactor/SSLIOSession.java
Date Thu, 17 Dec 2009 11:35:41 GMT
Author: asankha
Date: Thu Dec 17 11:35:40 2009
New Revision: 891654

URL: http://svn.apache.org/viewvc?rev=891654&view=rev
Log:
fix HTTPCORE-211

Modified:
    httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/impl/nio/reactor/SSLIOSession.java

Modified: httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/impl/nio/reactor/SSLIOSession.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/impl/nio/reactor/SSLIOSession.java?rev=891654&r1=891653&r2=891654&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/impl/nio/reactor/SSLIOSession.java
(original)
+++ httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/impl/nio/reactor/SSLIOSession.java
Thu Dec 17 11:35:40 2009
@@ -27,28 +27,27 @@
 
 package org.apache.http.impl.nio.reactor;
 
-import java.io.IOException;
-import java.net.InetSocketAddress;
-import java.net.SocketAddress;
-import java.nio.ByteBuffer;
-import java.nio.channels.ByteChannel;
-import java.nio.channels.SelectionKey;
+import org.apache.http.nio.reactor.EventMask;
+import org.apache.http.nio.reactor.IOSession;
+import org.apache.http.nio.reactor.SessionBufferStatus;
+import org.apache.http.params.HttpParams;
 
 import javax.net.ssl.SSLContext;
 import javax.net.ssl.SSLEngine;
 import javax.net.ssl.SSLEngineResult;
-import javax.net.ssl.SSLException;
 import javax.net.ssl.SSLEngineResult.HandshakeStatus;
 import javax.net.ssl.SSLEngineResult.Status;
-
-import org.apache.http.nio.reactor.EventMask;
-import org.apache.http.nio.reactor.IOSession;
-import org.apache.http.nio.reactor.SessionBufferStatus;
-import org.apache.http.params.HttpParams;
+import javax.net.ssl.SSLException;
+import java.io.IOException;
+import java.net.InetSocketAddress;
+import java.net.SocketAddress;
+import java.nio.ByteBuffer;
+import java.nio.channels.ByteChannel;
+import java.nio.channels.SelectionKey;
 
 /**
- * A decorator class intended to transparently extend an {@link IOSession} 
- * with transport layer security capabilities based on the SSL/TLS protocol. 
+ * A decorator class intended to transparently extend an {@link IOSession}
+ * with transport layer security capabilities based on the SSL/TLS protocol.
  *
  * @since 4.0
  */
@@ -62,16 +61,16 @@
     private final ByteBuffer outPlain;
     private final InternalByteChannel channel;
     private final SSLIOSessionHandler handler;
-    
+
     private int appEventMask;
     private SessionBufferStatus appBufferStatus;
-    
+
     private boolean endOfStream;
     private volatile int status;
-    
+
     public SSLIOSession(
-            final IOSession session, 
-            final SSLContext sslContext, 
+            final IOSession session,
+            final SSLContext sslContext,
             final SSLIOSessionHandler handler) {
         super();
         if (session == null) {
@@ -84,10 +83,10 @@
         this.appEventMask = session.getEventMask();
         this.channel = new InternalByteChannel();
         this.handler = handler;
-        
+
         // Override the status buffer interface
         this.session.setBufferStatus(this);
-        
+
         SocketAddress address = session.getRemoteAddress();
         if (address instanceof InetSocketAddress) {
             String hostname = ((InetSocketAddress) address).getHostName();
@@ -96,20 +95,20 @@
         } else {
             this.sslEngine = sslContext.createSSLEngine();
         }
-        
+
         // Allocate buffers for network (encrypted) data
         int netBuffersize = this.sslEngine.getSession().getPacketBufferSize();
-        this.inEncrypted = ByteBuffer.allocateDirect(netBuffersize);
-        this.outEncrypted = ByteBuffer.allocateDirect(netBuffersize);
+        this.inEncrypted = ByteBuffer.allocate(netBuffersize);
+        this.outEncrypted = ByteBuffer.allocate(netBuffersize);
 
         // Allocate buffers for application (unencrypted) data
         int appBuffersize = this.sslEngine.getSession().getApplicationBufferSize();
-        this.inPlain = ByteBuffer.allocateDirect(appBuffersize);
-        this.outPlain = ByteBuffer.allocateDirect(appBuffersize);
+        this.inPlain = ByteBuffer.allocate(appBuffersize);
+        this.outPlain = ByteBuffer.allocate(appBuffersize);
     }
-    
+
     public synchronized void bind(
-            final SSLMode mode, 
+            final SSLMode mode,
             final HttpParams params) throws SSLException {
         if (params == null) {
             throw new IllegalArgumentException("HTTP parameters may not be null");
@@ -128,10 +127,10 @@
         this.sslEngine.beginHandshake();
         doHandshake();
     }
-    
+
     private void doHandshake() throws SSLException {
         boolean handshaking = true;
-        
+
         SSLEngineResult result = null;
         while (handshaking) {
             switch (this.sslEngine.getHandshakeStatus()) {
@@ -176,7 +175,7 @@
 
         // The SSLEngine has just finished handshaking. This value is only generated by a
call
         // to SSLEngine.wrap()/unwrap() when that call finishes a handshake.
-        // It is never generated by SSLEngine.getHandshakeStatus(). 
+        // It is never generated by SSLEngine.getHandshakeStatus().
         if (result != null && result.getHandshakeStatus() == HandshakeStatus.FINISHED)
{
             if (this.handler != null) {
                 this.handler.verify(
@@ -212,7 +211,7 @@
         case FINISHED:
             break;
         }
-        
+
         // Do we have encrypted data ready to be sent?
         if (this.outEncrypted.position() > 0) {
             newMask = newMask | EventMask.WRITE;
@@ -223,7 +222,7 @@
             this.session.setEventMask(newMask);
         }
     }
-    
+
     private int sendEncryptedData() throws IOException {
         this.outEncrypted.flip();
         int bytesWritten = this.session.channel().write(this.outEncrypted);
@@ -234,7 +233,7 @@
     private int receiveEncryptedData() throws IOException {
         return this.session.channel().read(this.inEncrypted);
     }
-    
+
     private boolean decryptData() throws SSLException {
         boolean decrypted = false;
         SSLEngineResult.Status opStatus = Status.OK;
@@ -242,7 +241,7 @@
             this.inEncrypted.flip();
             SSLEngineResult result = this.sslEngine.unwrap(this.inEncrypted, this.inPlain);
             this.inEncrypted.compact();
-            
+
             opStatus = result.getStatus();
             if (opStatus == Status.CLOSED) {
                 this.status = CLOSED;
@@ -265,10 +264,10 @@
         doHandshake();
         decryptData();
         // Some decrypted data is available or at the end of stream
-        return (this.appEventMask & SelectionKey.OP_READ) > 0 
+        return (this.appEventMask & SelectionKey.OP_READ) > 0
             && (this.inPlain.position() > 0 || (this.endOfStream && this.status
== ACTIVE));
     }
-    
+
     /**
      * @throws IOException - not thrown currently
      */
@@ -279,18 +278,18 @@
     }
 
     /**
-     * @throws IOException - not thrown currently 
+     * @throws IOException - not thrown currently
      */
     public synchronized void inboundTransport() throws IOException {
         updateEventMask();
     }
-    
+
     public synchronized void outboundTransport() throws IOException {
         sendEncryptedData();
         doHandshake();
         updateEventMask();
     }
-    
+
     private synchronized int writePlain(final ByteBuffer src) throws SSLException {
         if (src == null) {
             throw new IllegalArgumentException("Byte buffer may not be null");
@@ -313,9 +312,9 @@
             return 0;
         }
     }
-    
+
     /**
-     * @throws SSLException - not thrown currently 
+     * @throws SSLException - not thrown currently
      */
     private synchronized int readPlain(final ByteBuffer dst) throws SSLException {
         if (dst == null) {
@@ -328,7 +327,7 @@
                 dst.put(this.inPlain.get());
             }
             this.inPlain.compact();
-            return n; 
+            return n;
         } else {
             if (this.endOfStream) {
                 return -1;
@@ -337,7 +336,7 @@
             }
         }
     }
-    
+
     public void close() {
         if (this.status != ACTIVE) {
             return;
@@ -348,7 +347,7 @@
             updateEventMask();
         }
     }
-    
+
     public void shutdown() {
         this.status = CLOSED;
         this.session.shutdown();
@@ -365,11 +364,11 @@
     public synchronized boolean isInboundDone() {
         return this.sslEngine.isInboundDone();
     }
-    
+
     public synchronized boolean isOutboundDone() {
         return this.sslEngine.isOutboundDone();
     }
-    
+
     public ByteChannel channel() {
         return this.channel;
     }
@@ -436,7 +435,7 @@
     public void setAttribute(final String name, final Object obj) {
         this.session.setAttribute(name, obj);
     }
-    
+
     @Override
     public String toString() {
         StringBuffer buffer = new StringBuffer();
@@ -472,7 +471,7 @@
         public boolean isOpen() {
             return !SSLIOSession.this.isClosed();
         }
-        
+
     }
 
 }



Mime
View raw message