use of org.eclipse.jetty.io.EndPoint in project jetty.project by eclipse.
the class NegotiatingServerConnectionFactory method newConnection.
@Override
public Connection newConnection(Connector connector, EndPoint endPoint) {
List<String> negotiated = this.negotiatedProtocols;
if (negotiated.isEmpty()) {
// Generate list of protocols that we can negotiate
negotiated = connector.getProtocols().stream().filter(p -> {
ConnectionFactory f = connector.getConnectionFactory(p);
return !(f instanceof SslConnectionFactory) && !(f instanceof NegotiatingServerConnectionFactory);
}).collect(Collectors.toList());
}
// if default protocol is not set, then it is either HTTP/1.1 or
// the first protocol given
String dft = defaultProtocol;
if (dft == null && !negotiated.isEmpty()) {
if (negotiated.contains(HttpVersion.HTTP_1_1.asString()))
dft = HttpVersion.HTTP_1_1.asString();
else
dft = negotiated.get(0);
}
SSLEngine engine = null;
EndPoint ep = endPoint;
while (engine == null && ep != null) {
// TODO make more generic
if (ep instanceof SslConnection.DecryptedEndPoint)
engine = ((SslConnection.DecryptedEndPoint) ep).getSslConnection().getSSLEngine();
else
ep = null;
}
return configure(newServerConnection(connector, endPoint, engine, negotiated, dft), connector, endPoint);
}
use of org.eclipse.jetty.io.EndPoint in project jetty.project by eclipse.
the class ClientCloseTest method testHalfClose.
@Test
public void testHalfClose() throws Exception {
// Set client timeout
final int timeout = 1000;
client.setMaxIdleTimeout(timeout);
// Client connects
CloseTrackingSocket clientSocket = new CloseTrackingSocket();
Future<Session> clientConnectFuture = client.connect(clientSocket, server.getWsUri());
// Server accepts connect
IBlockheadServerConnection serverConn = server.accept();
serverConn.upgrade();
// client confirms connection via echo
confirmConnection(clientSocket, clientConnectFuture, serverConn);
// client sends close frame (code 1000, normal)
final String origCloseReason = "Normal Close";
clientSocket.getSession().close(StatusCode.NORMAL, origCloseReason);
// server receives close frame
confirmServerReceivedCloseFrame(serverConn, StatusCode.NORMAL, is(origCloseReason));
// server sends 2 messages
serverConn.write(new TextFrame().setPayload("Hello"));
serverConn.write(new TextFrame().setPayload("World"));
// server sends close frame (code 1000, no reason)
CloseInfo sclose = new CloseInfo(StatusCode.NORMAL, "From Server");
serverConn.write(sclose.asFrame());
// client receives 2 messages
clientSocket.messageQueue.awaitEventCount(2, 1, TimeUnit.SECONDS);
// Verify received messages
String recvMsg = clientSocket.messageQueue.poll();
assertThat("Received message 1", recvMsg, is("Hello"));
recvMsg = clientSocket.messageQueue.poll();
assertThat("Received message 2", recvMsg, is("World"));
// Verify that there are no errors
assertThat("Error events", clientSocket.error.get(), nullValue());
// client close event on ws-endpoint
clientSocket.assertReceivedCloseEvent(timeout, is(StatusCode.NORMAL), containsString("From Server"));
}
use of org.eclipse.jetty.io.EndPoint in project jetty.project by eclipse.
the class AbstractWebSocketConnection method equals.
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
AbstractWebSocketConnection other = (AbstractWebSocketConnection) obj;
EndPoint endp = getEndPoint();
EndPoint otherEndp = other.getEndPoint();
if (endp == null) {
if (otherEndp != null)
return false;
} else if (!endp.equals(otherEndp))
return false;
return true;
}
use of org.eclipse.jetty.io.EndPoint in project jetty.project by eclipse.
the class AbstractWebSocketConnection method readParse.
private ReadMode readParse(ByteBuffer buffer) {
EndPoint endPoint = getEndPoint();
try {
// Process the content from the Endpoint next
while (// TODO: should this honor the LogicalConnection.suspend() ?
true) {
int filled = endPoint.fill(buffer);
if (filled < 0) {
LOG.debug("read - EOF Reached (remote: {})", getRemoteAddress());
ioState.onReadFailure(new EOFException("Remote Read EOF"));
return ReadMode.EOF;
} else if (filled == 0) {
// Done reading, wait for next onFillable
return ReadMode.PARSE;
}
if (LOG.isDebugEnabled()) {
LOG.debug("Filled {} bytes - {}", filled, BufferUtil.toDetailString(buffer));
}
parser.parse(buffer);
}
} catch (IOException e) {
LOG.warn(e);
close(StatusCode.PROTOCOL, e.getMessage());
return ReadMode.DISCARD;
} catch (CloseException e) {
LOG.debug(e);
close(e.getStatusCode(), e.getMessage());
return ReadMode.DISCARD;
} catch (Throwable t) {
LOG.warn(t);
close(StatusCode.ABNORMAL, t.getMessage());
// TODO: should probably only switch to discard if a non-ws-endpoint error
return ReadMode.DISCARD;
}
}
use of org.eclipse.jetty.io.EndPoint in project jetty.project by eclipse.
the class SslConnectionTest method testSslConnectionClosedBeforeFill.
@Test
public void testSslConnectionClosedBeforeFill() throws Exception {
File keyStore = MavenTestingUtils.getTestResourceFile("keystore.jks");
SslContextFactory sslContextFactory = new SslContextFactory();
sslContextFactory.setKeyStorePath(keyStore.getAbsolutePath());
sslContextFactory.setKeyStorePassword("storepwd");
sslContextFactory.start();
ByteBufferPool byteBufferPool = new MappedByteBufferPool();
QueuedThreadPool threadPool = new QueuedThreadPool();
threadPool.start();
ByteArrayEndPoint endPoint = new ByteArrayEndPoint();
SSLEngine sslEngine = sslContextFactory.newSSLEngine();
sslEngine.setUseClientMode(false);
SslConnection sslConnection = new SslConnection(byteBufferPool, threadPool, endPoint, sslEngine);
EndPoint sslEndPoint = sslConnection.getDecryptedEndPoint();
sslEndPoint.setConnection(new AbstractConnection(sslEndPoint, threadPool) {
@Override
public void onFillable() {
}
});
// There are no bytes in the endPoint, so we fill zero.
// However, this will trigger state changes in SSLEngine
// that will later cause it to throw ISE("Internal error").
sslEndPoint.fill(BufferUtil.EMPTY_BUFFER);
// Close the connection before filling.
sslEndPoint.shutdownOutput();
// Put some bytes in the endPoint to trigger
// the required state changes in SSLEngine.
byte[] bytes = new byte[] { 0x16, 0x03, 0x03, 0x00, 0x00 };
endPoint.addInput(ByteBuffer.wrap(bytes));
// reads from the EndPoint.
try {
sslEndPoint.fill(BufferUtil.EMPTY_BUFFER);
Assert.fail();
} catch (SSLHandshakeException x) {
// Expected.
}
}
Aggregations