Search in sources :

Example 36 with EndPoint

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);
}
Also used : SslConnection(org.eclipse.jetty.io.ssl.SslConnection) SSLEngine(javax.net.ssl.SSLEngine) EndPoint(org.eclipse.jetty.io.EndPoint)

Example 37 with 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"));
}
Also used : IBlockheadServerConnection(org.eclipse.jetty.websocket.common.test.IBlockheadServerConnection) TextFrame(org.eclipse.jetty.websocket.common.frames.TextFrame) Matchers.containsString(org.hamcrest.Matchers.containsString) CloseInfo(org.eclipse.jetty.websocket.common.CloseInfo) EndPoint(org.eclipse.jetty.io.EndPoint) SocketChannelEndPoint(org.eclipse.jetty.io.SocketChannelEndPoint) Session(org.eclipse.jetty.websocket.api.Session) WebSocketSession(org.eclipse.jetty.websocket.common.WebSocketSession) Test(org.junit.Test)

Example 38 with EndPoint

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;
}
Also used : EndPoint(org.eclipse.jetty.io.EndPoint)

Example 39 with EndPoint

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;
    }
}
Also used : CloseException(org.eclipse.jetty.websocket.api.CloseException) EOFException(java.io.EOFException) EndPoint(org.eclipse.jetty.io.EndPoint) IOException(java.io.IOException) EndPoint(org.eclipse.jetty.io.EndPoint)

Example 40 with EndPoint

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.
    }
}
Also used : ByteBufferPool(org.eclipse.jetty.io.ByteBufferPool) MappedByteBufferPool(org.eclipse.jetty.io.MappedByteBufferPool) SSLEngine(javax.net.ssl.SSLEngine) ByteArrayEndPoint(org.eclipse.jetty.io.ByteArrayEndPoint) EndPoint(org.eclipse.jetty.io.EndPoint) ByteArrayEndPoint(org.eclipse.jetty.io.ByteArrayEndPoint) SSLHandshakeException(javax.net.ssl.SSLHandshakeException) MappedByteBufferPool(org.eclipse.jetty.io.MappedByteBufferPool) SslConnection(org.eclipse.jetty.io.ssl.SslConnection) SslContextFactory(org.eclipse.jetty.util.ssl.SslContextFactory) QueuedThreadPool(org.eclipse.jetty.util.thread.QueuedThreadPool) AbstractConnection(org.eclipse.jetty.io.AbstractConnection) File(java.io.File) Test(org.junit.Test)

Aggregations

EndPoint (org.eclipse.jetty.io.EndPoint)42 Test (org.junit.Test)19 IOException (java.io.IOException)11 ByteBuffer (java.nio.ByteBuffer)10 HttpServletRequest (javax.servlet.http.HttpServletRequest)9 HttpServletResponse (javax.servlet.http.HttpServletResponse)9 SSLEngine (javax.net.ssl.SSLEngine)8 ServletException (javax.servlet.ServletException)8 WebSocketSession (org.eclipse.jetty.websocket.common.WebSocketSession)8 InputStream (java.io.InputStream)7 Socket (java.net.Socket)7 ByteBufferPool (org.eclipse.jetty.io.ByteBufferPool)7 SocketChannelEndPoint (org.eclipse.jetty.io.SocketChannelEndPoint)7 OutputStream (java.io.OutputStream)6 SslConnection (org.eclipse.jetty.io.ssl.SslConnection)6 CountDownLatch (java.util.concurrent.CountDownLatch)5 AtomicReference (java.util.concurrent.atomic.AtomicReference)5 HttpClientTransportOverHTTP (org.eclipse.jetty.client.http.HttpClientTransportOverHTTP)5 Connector (org.eclipse.jetty.server.Connector)5 ServerConnector (org.eclipse.jetty.server.ServerConnector)5