357240 work in progress

This commit is contained in:
Greg Wilkins 2011-09-28 00:02:59 +10:00
commit 61dd1493b1

View File

@ -42,7 +42,7 @@ import org.eclipse.jetty.util.log.Logger;
public class SslSelectChannelEndPoint extends SelectChannelEndPoint public class SslSelectChannelEndPoint extends SelectChannelEndPoint
{ {
public static final Logger LOG=Log.getLogger("org.eclipse.jetty.io.nio").getLogger("ssl"); public static final Logger LOG=Log.getLogger("org.eclipse.jetty.io.nio").getLogger("ssl");
private static final Buffer __EMPTY_BUFFER=new DirectNIOBuffer(0); private static final Buffer __EMPTY_BUFFER=new DirectNIOBuffer(0);
private static final ByteBuffer __ZERO_BUFFER=ByteBuffer.allocate(0); private static final ByteBuffer __ZERO_BUFFER=ByteBuffer.allocate(0);
@ -151,7 +151,7 @@ public class SslSelectChannelEndPoint extends SelectChannelEndPoint
SSLEngineResult result = _result; SSLEngineResult result = _result;
return result!=null && (result.bytesConsumed()>0 || result.bytesProduced()>0); return result!=null && (result.bytesConsumed()>0 || result.bytesProduced()>0);
} }
/* ------------------------------------------------------------ */ /* ------------------------------------------------------------ */
/** /**
* @return True if SSL re-negotiation is allowed (default false) * @return True if SSL re-negotiation is allowed (default false)
@ -210,7 +210,7 @@ public class SslSelectChannelEndPoint extends SelectChannelEndPoint
private int process(ByteBuffer inBBuf, Buffer outBuf) throws IOException private int process(ByteBuffer inBBuf, Buffer outBuf) throws IOException
{ {
if (_debug) if (_debug)
LOG.debug("process {} {}",inBBuf!=null,outBuf!=null); LOG.debug("{} process closing={} in={} out={}",_session,_closing,inBBuf!=null,outBuf==null?null:outBuf.toDetailString());
// If there is no place to put incoming application data, // If there is no place to put incoming application data,
if (inBBuf==null) if (inBBuf==null)
@ -218,22 +218,22 @@ public class SslSelectChannelEndPoint extends SelectChannelEndPoint
// use ZERO buffer // use ZERO buffer
inBBuf=__ZERO_BUFFER; inBBuf=__ZERO_BUFFER;
} }
int received=0; int received=0;
int sent=0; int sent=0;
HandshakeStatus initialStatus = _engine.getHandshakeStatus(); HandshakeStatus initialStatus = _engine.getHandshakeStatus();
boolean progress=true; boolean progress=true;
while (progress) while (progress)
{ {
progress=false; progress=false;
// flush output data // flush output data
{ {
int len=_outNIOBuffer==null?0:_outNIOBuffer.length(); int len=_outNIOBuffer==null?0:_outNIOBuffer.length();
// we must flush it, as the other end might be // we must flush it, as the other end might be
// waiting for that outgoing data before sending // waiting for that outgoing data before sending
// more incoming data // more incoming data
@ -249,25 +249,25 @@ public class SslSelectChannelEndPoint extends SelectChannelEndPoint
{ {
case FINISHED: case FINISHED:
throw new IllegalStateException(); throw new IllegalStateException();
case NOT_HANDSHAKING: case NOT_HANDSHAKING:
_handshook=true; _handshook=true;
// If closing, don't process application data // If closing, don't process application data
if (_closing) if (_closing)
{
if (outBuf!=null && outBuf.hasContent())
throw new IOException("Write while closing");
break; break;
}
// Try wrapping some application data // Try wrapping some application data
if (outBuf!=null) if (outBuf!=null && outBuf.hasContent())
{ {
int c=0; int c=wrap(outBuf);
if (outBuf!=null && outBuf.hasContent()) progress=c>0||_result.bytesProduced()>0||_result.bytesConsumed()>0;
{
c=wrap(outBuf);
progress=_result.bytesProduced()>0||_result.bytesConsumed()>0;
}
if (c>0) if (c>0)
sent+=c; sent+=c;
else if (c<0 && sent==0) else if (c<0 && sent==0)
@ -283,7 +283,7 @@ public class SslSelectChannelEndPoint extends SelectChannelEndPoint
} }
break; break;
case NEED_TASK: case NEED_TASK:
{ {
// A task needs to be run, so run it! // A task needs to be run, so run it!
@ -324,31 +324,29 @@ public class SslSelectChannelEndPoint extends SelectChannelEndPoint
sent+=c; sent+=c;
else if (c<0 && sent==0) else if (c<0 && sent==0)
sent=-1; sent=-1;
break; break;
} }
case NEED_UNWRAP: case NEED_UNWRAP:
{ {
checkRenegotiate(); checkRenegotiate();
// Need more data to be unwrapped so try another call to unwrap // Need more data to be unwrapped so try another call to unwrap
progress|=unwrap(inBBuf); progress|=unwrap(inBBuf);
if (_closing) if (_closing)
inBBuf.clear(); inBBuf.clear();
break; break;
} }
} }
if (_debug) LOG.debug("{} progress {}",_session,progress); if (_debug) LOG.debug("{} progress {}",_session,progress);
} }
if (_debug) LOG.debug("{} received {} sent {}",_session,received,sent); if (_debug) LOG.debug("{} received {} sent {}",_session,received,sent);
return (received<0||sent<0)?-1:(received+sent); return (received<0||sent<0)?-1:(received+sent);
} }
/* ------------------------------------------------------------ */ /* ------------------------------------------------------------ */
@Override @Override
@ -358,8 +356,7 @@ public class SslSelectChannelEndPoint extends SelectChannelEndPoint
return; return;
LOG.debug("{} close",_session); LOG.debug("{} close",_session);
_closing=true; _closing=true;
_engine.closeOutbound();
// Processing will call flush(), which will handle the closing state with a closeOutbound then shutdownOutput
process(null,null); process(null,null);
} }
@ -370,18 +367,18 @@ public class SslSelectChannelEndPoint extends SelectChannelEndPoint
*/ */
@Override @Override
public int fill(Buffer buffer) throws IOException public int fill(Buffer buffer) throws IOException
{ {
LOG.debug("{} fill",_session); LOG.debug("{} fill",_session);
// This end point only works on NIO buffer type (director // This end point only works on NIO buffer type (director
// or indirect), so extract the NIO buffer that is wrapped // or indirect), so extract the NIO buffer that is wrapped
// by the passed jetty Buffer. // by the passed jetty Buffer.
ByteBuffer bbuf=((NIOBuffer)buffer).getByteBuffer(); ByteBuffer bbuf=((NIOBuffer)buffer).getByteBuffer();
// remember the original size of the unencrypted buffer // remember the original size of the unencrypted buffer
int size=buffer.length(); int size=buffer.length();
synchronized (bbuf) synchronized (bbuf)
{ {
bbuf.position(buffer.putIndex()); bbuf.position(buffer.putIndex());
@ -447,27 +444,19 @@ public class SslSelectChannelEndPoint extends SelectChannelEndPoint
/* ------------------------------------------------------------ */ /* ------------------------------------------------------------ */
@Override @Override
public void flush() throws IOException public void flush() throws IOException
{ {
LOG.debug(_session+" flush");
if (isBufferingOutput()) if (isBufferingOutput())
{ {
int flushed=super.flush(_outNIOBuffer); int flushed=super.flush(_outNIOBuffer);
if (_debug) if (_debug)
LOG.debug(_session+" flushed "+flushed+" left="+_outNIOBuffer.length()); LOG.debug(_session+" flushed "+flushed+" left="+_outNIOBuffer.length());
} }
else if (_closing) else if (_engine.isOutboundDone() && !super.isOutputShutdown())
{ {
if (_engine.isOutboundDone() && !super.isOutputShutdown()) if (_debug)
{ LOG.debug(_session+" flush shutdownOutput");
if (_debug) super.shutdownOutput();
LOG.debug(_session+" flush shutdownOutput");
super.shutdownOutput();
}
else
{
if (_debug)
LOG.debug(_session+" flush closeOutbound");
_engine.closeOutbound();
}
} }
} }