Author: remm Date: Tue Mar 11 15:41:02 2014 New Revision: 1576385 URL: http://svn.apache.org/r1576385 Log: - Fix SSL output with blocking writes when a handshake occurs during the flow. - Fix relevant test as pointed out by Konstantin.
Modified: tomcat/trunk/java/org/apache/tomcat/util/net/SecureNio2Channel.java tomcat/trunk/test/org/apache/tomcat/util/net/TestSsl.java Modified: tomcat/trunk/java/org/apache/tomcat/util/net/SecureNio2Channel.java URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/tomcat/util/net/SecureNio2Channel.java?rev=1576385&r1=1576384&r2=1576385&view=diff ============================================================================== --- tomcat/trunk/java/org/apache/tomcat/util/net/SecureNio2Channel.java (original) +++ tomcat/trunk/java/org/apache/tomcat/util/net/SecureNio2Channel.java Tue Mar 11 15:41:02 2014 @@ -581,22 +581,6 @@ public class SecureNio2Channel extends N t = new IOException("Channel is in closing state."); return; } - //The data buffer should be empty, we can reuse the entire buffer. - netOutBuffer.clear(); - try { - SSLEngineResult result = sslEngine.wrap(bufHandler.getWriteBuffer(), netOutBuffer); - written = result.bytesConsumed(); - netOutBuffer.flip(); - if (result.getStatus() == Status.OK) { - if (result.getHandshakeStatus() == HandshakeStatus.NEED_TASK) - tasks(); - } else { - t = new IOException("Unable to wrap data, invalid engine state: " +result.getStatus()); - } - integer = sc.write(netOutBuffer); - } catch (SSLException e) { - t = e; - } } @Override public boolean cancel(boolean mayInterruptIfRunning) { @@ -612,21 +596,49 @@ public class SecureNio2Channel extends N } @Override public Integer get() throws InterruptedException, ExecutionException { + wrap(); if (t != null) { throw new ExecutionException(t); } integer.get(); - return Integer.valueOf(written); + if (written == 0) { + return get(); + } else { + return Integer.valueOf(written); + } } @Override public Integer get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException { + wrap(); if (t != null) { throw new ExecutionException(t); } integer.get(timeout, unit); - return Integer.valueOf(written); + if (written == 0) { + return get(timeout, unit); + } else { + return Integer.valueOf(written); + } + } + protected void wrap() { + //The data buffer should be empty, we can reuse the entire buffer. + netOutBuffer.clear(); + try { + SSLEngineResult result = sslEngine.wrap(bufHandler.getWriteBuffer(), netOutBuffer); + written = result.bytesConsumed(); + netOutBuffer.flip(); + if (result.getStatus() == Status.OK) { + if (result.getHandshakeStatus() == HandshakeStatus.NEED_TASK) + tasks(); + } else { + t = new IOException("Unable to wrap data, invalid engine state: " +result.getStatus()); + } + integer = sc.write(netOutBuffer); + } catch (SSLException e) { + t = e; + } } } Modified: tomcat/trunk/test/org/apache/tomcat/util/net/TestSsl.java URL: http://svn.apache.org/viewvc/tomcat/trunk/test/org/apache/tomcat/util/net/TestSsl.java?rev=1576385&r1=1576384&r2=1576385&view=diff ============================================================================== --- tomcat/trunk/test/org/apache/tomcat/util/net/TestSsl.java (original) +++ tomcat/trunk/test/org/apache/tomcat/util/net/TestSsl.java Tue Mar 11 15:41:02 2014 @@ -33,6 +33,7 @@ import javax.net.ssl.SSLSocketFactory; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; +import org.junit.Assert; import org.junit.Test; import org.apache.catalina.Context; @@ -90,7 +91,6 @@ public class TestSsl extends TomcatBaseT assertTrue(res.toString().indexOf("<h1>Hello World!</h1>") > 0); } - boolean handshakeDone = false; @Test @@ -204,6 +204,7 @@ public class TestSsl extends TomcatBaseT Reader r = new InputStreamReader(is); BufferedReader br = new BufferedReader(r); String line = br.readLine(); + Assert.assertEquals("HTTP/1.1 200 OK", line); while (line != null) { // For debugging System.out.println(line); // Linux clients see a Connection Reset in some circumstances and a --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org For additional commands, e-mail: dev-h...@tomcat.apache.org