Search in sources :

Example 21 with EndPoint

use of org.eclipse.jetty.io.EndPoint in project jetty.project by eclipse.

the class ConnectHandlerTest method testCONNECTAndPOSTWithContext.

@Test
public void testCONNECTAndPOSTWithContext() throws Exception {
    final String contextKey = "contextKey";
    final String contextValue = "contextValue";
    // Replace the default ProxyHandler with a subclass to test context information passing
    disposeProxy();
    proxy.setHandler(new ConnectHandler() {

        @Override
        protected boolean handleAuthentication(HttpServletRequest request, HttpServletResponse response, String address) {
            request.setAttribute(contextKey, contextValue);
            return super.handleAuthentication(request, response, address);
        }

        @Override
        protected void connectToServer(HttpServletRequest request, String host, int port, Promise<SocketChannel> promise) {
            Assert.assertEquals(contextValue, request.getAttribute(contextKey));
            super.connectToServer(request, host, port, promise);
        }

        @Override
        protected void prepareContext(HttpServletRequest request, ConcurrentMap<String, Object> context) {
            // Transfer data from the HTTP request to the connection context
            Assert.assertEquals(contextValue, request.getAttribute(contextKey));
            context.put(contextKey, request.getAttribute(contextKey));
        }

        @Override
        protected int read(EndPoint endPoint, ByteBuffer buffer, ConcurrentMap<String, Object> context) throws IOException {
            Assert.assertEquals(contextValue, context.get(contextKey));
            return super.read(endPoint, buffer, context);
        }

        @Override
        protected void write(EndPoint endPoint, ByteBuffer buffer, Callback callback, ConcurrentMap<String, Object> context) {
            Assert.assertEquals(contextValue, context.get(contextKey));
            super.write(endPoint, buffer, callback, context);
        }
    });
    proxy.start();
    String hostPort = "localhost:" + serverConnector.getLocalPort();
    String request = "" + "CONNECT " + hostPort + " HTTP/1.1\r\n" + "Host: " + hostPort + "\r\n" + "\r\n";
    try (Socket socket = newSocket()) {
        OutputStream output = socket.getOutputStream();
        InputStream input = socket.getInputStream();
        output.write(request.getBytes(StandardCharsets.UTF_8));
        output.flush();
        // Expect 200 OK from the CONNECT request
        HttpTester.Response response = readResponse(input);
        Assert.assertEquals(HttpStatus.OK_200, response.getStatus());
        String body = "0123456789ABCDEF";
        request = "" + "POST /echo HTTP/1.1\r\n" + "Host: " + hostPort + "\r\n" + "Content-Length: " + body.length() + "\r\n" + "\r\n" + body;
        output.write(request.getBytes(StandardCharsets.UTF_8));
        output.flush();
        response = readResponse(input);
        Assert.assertEquals(HttpStatus.OK_200, response.getStatus());
        Assert.assertEquals("POST /echo\r\n" + body, response.getContent());
    }
}
Also used : SocketChannel(java.nio.channels.SocketChannel) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ServletOutputStream(javax.servlet.ServletOutputStream) OutputStream(java.io.OutputStream) HttpServletResponse(javax.servlet.http.HttpServletResponse) EndPoint(org.eclipse.jetty.io.EndPoint) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer) EndPoint(org.eclipse.jetty.io.EndPoint) HttpTester(org.eclipse.jetty.http.HttpTester) HttpServletRequest(javax.servlet.http.HttpServletRequest) Callback(org.eclipse.jetty.util.Callback) Socket(java.net.Socket) Test(org.junit.Test)

Example 22 with EndPoint

use of org.eclipse.jetty.io.EndPoint in project jetty.project by eclipse.

the class ForwardProxyServerTest method testRequestTarget.

@Test
public void testRequestTarget() throws Exception {
    startServer(new AbstractConnectionFactory("http/1.1") {

        @Override
        public Connection newConnection(Connector connector, EndPoint endPoint) {
            return new AbstractConnection(endPoint, connector.getExecutor()) {

                @Override
                public void onOpen() {
                    super.onOpen();
                    fillInterested();
                }

                @Override
                public void onFillable() {
                    try {
                        // When using TLS, multiple reads are required.
                        ByteBuffer buffer = BufferUtil.allocate(1024);
                        int filled = 0;
                        while (filled == 0) filled = getEndPoint().fill(buffer);
                        Utf8StringBuilder builder = new Utf8StringBuilder();
                        builder.append(buffer);
                        String request = builder.toString();
                        // ProxyServlet will receive an absolute URI from
                        // the client, and convert it to a relative URI.
                        // The ConnectHandler won't modify what the client
                        // sent, which must be a relative URI.
                        Assert.assertThat(request.length(), Matchers.greaterThan(0));
                        if (serverSslContextFactory == null)
                            Assert.assertFalse(request.contains("http://"));
                        else
                            Assert.assertFalse(request.contains("https://"));
                        String response = "" + "HTTP/1.1 200 OK\r\n" + "Content-Length: 0\r\n" + "\r\n";
                        getEndPoint().write(Callback.NOOP, ByteBuffer.wrap(response.getBytes(StandardCharsets.UTF_8)));
                    } catch (Throwable x) {
                        x.printStackTrace();
                        close();
                    }
                }
            };
        }
    });
    startProxy();
    HttpClient httpClient = new HttpClient(newSslContextFactory());
    httpClient.getProxyConfiguration().getProxies().add(newHttpProxy());
    httpClient.start();
    try {
        ContentResponse response = httpClient.newRequest("localhost", serverConnector.getLocalPort()).scheme(serverSslContextFactory == null ? "http" : "https").method(HttpMethod.GET).path("/test").send();
        Assert.assertEquals(HttpStatus.OK_200, response.getStatus());
    } finally {
        httpClient.stop();
    }
}
Also used : ServerConnector(org.eclipse.jetty.server.ServerConnector) Connector(org.eclipse.jetty.server.Connector) AbstractConnectionFactory(org.eclipse.jetty.server.AbstractConnectionFactory) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) AbstractConnection(org.eclipse.jetty.io.AbstractConnection) Utf8StringBuilder(org.eclipse.jetty.util.Utf8StringBuilder) HttpClient(org.eclipse.jetty.client.HttpClient) AbstractConnection(org.eclipse.jetty.io.AbstractConnection) Connection(org.eclipse.jetty.io.Connection) EndPoint(org.eclipse.jetty.io.EndPoint) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 23 with EndPoint

use of org.eclipse.jetty.io.EndPoint in project jetty.project by eclipse.

the class AbstractWebSocketConnection method hashCode.

@Override
public int hashCode() {
    final int prime = 31;
    int result = 1;
    EndPoint endp = getEndPoint();
    if (endp != null) {
        result = prime * result + endp.getLocalAddress().hashCode();
        result = prime * result + endp.getRemoteAddress().hashCode();
    }
    return result;
}
Also used : EndPoint(org.eclipse.jetty.io.EndPoint) EndPoint(org.eclipse.jetty.io.EndPoint)

Example 24 with EndPoint

use of org.eclipse.jetty.io.EndPoint in project jetty.project by eclipse.

the class AbstractWebSocketConnection method disconnect.

private void disconnect(boolean onlyOutput) {
    if (LOG_CLOSE.isDebugEnabled())
        LOG_CLOSE.debug("{} disconnect({})", policy.getBehavior(), onlyOutput ? "outputOnly" : "both");
    // close FrameFlusher, we cannot write anymore at this point.
    flusher.close();
    EndPoint endPoint = getEndPoint();
    // SSL close alerts to be sent by Jetty
    if (LOG_CLOSE.isDebugEnabled())
        LOG_CLOSE.debug("Shutting down output {}", endPoint);
    endPoint.shutdownOutput();
    if (!onlyOutput) {
        if (LOG_CLOSE.isDebugEnabled())
            LOG_CLOSE.debug("Closing {}", endPoint);
        endPoint.close();
    }
}
Also used : EndPoint(org.eclipse.jetty.io.EndPoint)

Example 25 with EndPoint

use of org.eclipse.jetty.io.EndPoint in project blade by biezhi.

the class SslClientConnectionFactory method newConnection.

@Override
public Connection newConnection(EndPoint endPoint, Map<String, Object> context) throws IOException {
    String host = (String) context.get(SSL_PEER_HOST_CONTEXT_KEY);
    int port = (Integer) context.get(SSL_PEER_PORT_CONTEXT_KEY);
    SSLEngine engine = sslContextFactory.newSSLEngine(host, port);
    engine.setUseClientMode(true);
    context.put(SSL_ENGINE_CONTEXT_KEY, engine);
    SslConnection sslConnection = newSslConnection(byteBufferPool, executor, endPoint, engine);
    endPoint.setConnection(sslConnection);
    customize(sslConnection, context);
    EndPoint appEndPoint = sslConnection.getDecryptedEndPoint();
    appEndPoint.setConnection(connectionFactory.newConnection(appEndPoint, context));
    return sslConnection;
}
Also used : SSLEngine(javax.net.ssl.SSLEngine) EndPoint(org.eclipse.jetty.io.EndPoint) EndPoint(org.eclipse.jetty.io.EndPoint)

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