Search in sources :

Example 26 with EndPoint

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

the class InetAccessHandler method handle.

/**
     * Checks the incoming request against the whitelist and blacklist
     */
@Override
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
    // Get the real remote IP (not the one set by the forwarded headers (which may be forged))
    HttpChannel channel = baseRequest.getHttpChannel();
    if (channel != null) {
        EndPoint endp = channel.getEndPoint();
        if (endp != null) {
            InetSocketAddress address = endp.getRemoteAddress();
            if (address != null && !isAllowed(address.getAddress(), request)) {
                response.sendError(HttpStatus.FORBIDDEN_403);
                baseRequest.setHandled(true);
                return;
            }
        }
    }
    getHandler().handle(target, baseRequest, request, response);
}
Also used : InetSocketAddress(java.net.InetSocketAddress) HttpChannel(org.eclipse.jetty.server.HttpChannel) EndPoint(org.eclipse.jetty.io.EndPoint)

Example 27 with EndPoint

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

the class HttpClientTest method testRequestSentOnlyAfterConnectionOpen.

@Test
public void testRequestSentOnlyAfterConnectionOpen() throws Exception {
    startServer(new AbstractHandler() {

        @Override
        public void handle(String target, org.eclipse.jetty.server.Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            baseRequest.setHandled(true);
        }
    });
    final AtomicBoolean open = new AtomicBoolean();
    client = new HttpClient(new HttpClientTransportOverHTTP() {

        @Override
        protected HttpConnectionOverHTTP newHttpConnection(EndPoint endPoint, HttpDestination destination, Promise<Connection> promise) {
            return new HttpConnectionOverHTTP(endPoint, destination, promise) {

                @Override
                public void onOpen() {
                    open.set(true);
                    super.onOpen();
                }
            };
        }
    }, sslContextFactory);
    client.start();
    final CountDownLatch latch = new CountDownLatch(2);
    client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).onRequestBegin(request -> {
        Assert.assertTrue(open.get());
        latch.countDown();
    }).send(result -> {
        if (result.isSucceeded())
            latch.countDown();
    });
    Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
}
Also used : Arrays(java.util.Arrays) EndPoint(org.eclipse.jetty.io.EndPoint) TestingDir(org.eclipse.jetty.toolchain.test.TestingDir) ServletException(javax.servlet.ServletException) TimeoutException(java.util.concurrent.TimeoutException) Random(java.util.Random) Request(org.eclipse.jetty.client.api.Request) ByteBuffer(java.nio.ByteBuffer) Assert.assertThat(org.junit.Assert.assertThat) ServerSocket(java.net.ServerSocket) HttpCookie(java.net.HttpCookie) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) FutureResponseListener(org.eclipse.jetty.client.util.FutureResponseListener) DeferredContentProvider(org.eclipse.jetty.client.util.DeferredContentProvider) URI(java.net.URI) Path(java.nio.file.Path) Response(org.eclipse.jetty.client.api.Response) Callback(org.eclipse.jetty.util.Callback) SocketAddressResolver(org.eclipse.jetty.util.SocketAddressResolver) BufferingResponseListener(org.eclipse.jetty.client.util.BufferingResponseListener) Slow(org.eclipse.jetty.toolchain.test.annotation.Slow) StandardOpenOption(java.nio.file.StandardOpenOption) IO(org.eclipse.jetty.util.IO) AbstractConnection(org.eclipse.jetty.io.AbstractConnection) InetSocketAddress(java.net.InetSocketAddress) StandardCharsets(java.nio.charset.StandardCharsets) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) FuturePromise(org.eclipse.jetty.util.FuturePromise) StacklessLogging(org.eclipse.jetty.util.log.StacklessLogging) HttpConnectionOverHTTP(org.eclipse.jetty.client.http.HttpConnectionOverHTTP) BadMessageException(org.eclipse.jetty.http.BadMessageException) Connection(org.eclipse.jetty.client.api.Connection) Result(org.eclipse.jetty.client.api.Result) Socket(java.net.Socket) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) SslContextFactory(org.eclipse.jetty.util.ssl.SslContextFactory) HttpVersion(org.eclipse.jetty.http.HttpVersion) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) StringContentProvider(org.eclipse.jetty.client.util.StringContentProvider) AtomicReference(java.util.concurrent.atomic.AtomicReference) BytesContentProvider(org.eclipse.jetty.client.util.BytesContentProvider) ArrayList(java.util.ArrayList) HttpClientTransportOverHTTP(org.eclipse.jetty.client.http.HttpClientTransportOverHTTP) HttpHeader(org.eclipse.jetty.http.HttpHeader) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletOutputStream(javax.servlet.ServletOutputStream) Assume(org.junit.Assume) NoSuchElementException(java.util.NoSuchElementException) OutputStream(java.io.OutputStream) HttpHeaderValue(org.eclipse.jetty.http.HttpHeaderValue) Iterator(java.util.Iterator) Files(java.nio.file.Files) HttpServletResponse(javax.servlet.http.HttpServletResponse) Promise(org.eclipse.jetty.util.Promise) Matchers(org.hamcrest.Matchers) IOException(java.io.IOException) Test(org.junit.Test) UnknownHostException(java.net.UnknownHostException) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) HttpDestinationOverHTTP(org.eclipse.jetty.client.http.HttpDestinationOverHTTP) AtomicLong(java.util.concurrent.atomic.AtomicLong) URLEncoder(java.net.URLEncoder) HttpMethod(org.eclipse.jetty.http.HttpMethod) Rule(org.junit.Rule) Paths(java.nio.file.Paths) HttpField(org.eclipse.jetty.http.HttpField) ContentProvider(org.eclipse.jetty.client.api.ContentProvider) Destination(org.eclipse.jetty.client.api.Destination) Assert(org.junit.Assert) Collections(java.util.Collections) InputStream(java.io.InputStream) Exchanger(java.util.concurrent.Exchanger) HttpServletResponse(javax.servlet.http.HttpServletResponse) HttpClientTransportOverHTTP(org.eclipse.jetty.client.http.HttpClientTransportOverHTTP) IOException(java.io.IOException) EndPoint(org.eclipse.jetty.io.EndPoint) CountDownLatch(java.util.concurrent.CountDownLatch) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) HttpConnectionOverHTTP(org.eclipse.jetty.client.http.HttpConnectionOverHTTP) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) FuturePromise(org.eclipse.jetty.util.FuturePromise) Promise(org.eclipse.jetty.util.Promise) Test(org.junit.Test)

Example 28 with EndPoint

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

the class HttpClientFailureTest method testFailureAfterRequestCommit.

@Test
public void testFailureAfterRequestCommit() throws Exception {
    startServer(new EmptyServerHandler());
    final AtomicReference<HttpConnectionOverHTTP> connectionRef = new AtomicReference<>();
    client = new HttpClient(new HttpClientTransportOverHTTP() {

        @Override
        protected HttpConnectionOverHTTP newHttpConnection(EndPoint endPoint, HttpDestination destination, Promise<Connection> promise) {
            HttpConnectionOverHTTP connection = super.newHttpConnection(endPoint, destination, promise);
            connectionRef.set(connection);
            return connection;
        }
    }, null);
    client.start();
    final CountDownLatch commitLatch = new CountDownLatch(1);
    final CountDownLatch completeLatch = new CountDownLatch(1);
    DeferredContentProvider content = new DeferredContentProvider();
    client.newRequest("localhost", connector.getLocalPort()).onRequestCommit(request -> {
        connectionRef.get().getEndPoint().close();
        commitLatch.countDown();
    }).content(content).idleTimeout(2, TimeUnit.SECONDS).send(result -> {
        if (result.isFailed())
            completeLatch.countDown();
    });
    Assert.assertTrue(commitLatch.await(5, TimeUnit.SECONDS));
    final CountDownLatch contentLatch = new CountDownLatch(1);
    content.offer(ByteBuffer.allocate(1024), new Callback() {

        @Override
        public void failed(Throwable x) {
            contentLatch.countDown();
        }
    });
    Assert.assertTrue(commitLatch.await(5, TimeUnit.SECONDS));
    Assert.assertTrue(contentLatch.await(5, TimeUnit.SECONDS));
    Assert.assertTrue(completeLatch.await(5, TimeUnit.SECONDS));
    DuplexConnectionPool connectionPool = (DuplexConnectionPool) connectionRef.get().getHttpDestination().getConnectionPool();
    Assert.assertEquals(0, connectionPool.getConnectionCount());
    Assert.assertEquals(0, connectionPool.getActiveConnections().size());
    Assert.assertEquals(0, connectionPool.getIdleConnections().size());
}
Also used : Connection(org.eclipse.jetty.client.api.Connection) Handler(org.eclipse.jetty.server.Handler) EndPoint(org.eclipse.jetty.io.EndPoint) Promise(org.eclipse.jetty.util.Promise) Test(org.junit.Test) AtomicReference(java.util.concurrent.atomic.AtomicReference) ByteBuffer(java.nio.ByteBuffer) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) HttpClientTransportOverHTTP(org.eclipse.jetty.client.http.HttpClientTransportOverHTTP) ServerConnector(org.eclipse.jetty.server.ServerConnector) QueuedThreadPool(org.eclipse.jetty.util.thread.QueuedThreadPool) DeferredContentProvider(org.eclipse.jetty.client.util.DeferredContentProvider) After(org.junit.After) Assert(org.junit.Assert) Server(org.eclipse.jetty.server.Server) Callback(org.eclipse.jetty.util.Callback) HttpConnectionOverHTTP(org.eclipse.jetty.client.http.HttpConnectionOverHTTP) AtomicReference(java.util.concurrent.atomic.AtomicReference) HttpClientTransportOverHTTP(org.eclipse.jetty.client.http.HttpClientTransportOverHTTP) EndPoint(org.eclipse.jetty.io.EndPoint) CountDownLatch(java.util.concurrent.CountDownLatch) HttpConnectionOverHTTP(org.eclipse.jetty.client.http.HttpConnectionOverHTTP) Promise(org.eclipse.jetty.util.Promise) Callback(org.eclipse.jetty.util.Callback) DeferredContentProvider(org.eclipse.jetty.client.util.DeferredContentProvider) Test(org.junit.Test)

Example 29 with EndPoint

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

the class HttpClientUploadDuringServerShutdown method testUploadDuringServerShutdown.

@Test
public void testUploadDuringServerShutdown() throws Exception {
    final AtomicReference<EndPoint> endPointRef = new AtomicReference<>();
    final CountDownLatch serverLatch = new CountDownLatch(1);
    QueuedThreadPool serverThreads = new QueuedThreadPool();
    serverThreads.setName("server");
    Server server = new Server(serverThreads);
    ServerConnector connector = new ServerConnector(server);
    server.addConnector(connector);
    server.setHandler(new AbstractHandler() {

        @Override
        public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            baseRequest.setHandled(true);
            endPointRef.set(baseRequest.getHttpChannel().getEndPoint());
            serverLatch.countDown();
        }
    });
    server.start();
    final AtomicBoolean afterSetup = new AtomicBoolean();
    final CountDownLatch sendLatch = new CountDownLatch(1);
    final CountDownLatch beginLatch = new CountDownLatch(1);
    final CountDownLatch associateLatch = new CountDownLatch(1);
    QueuedThreadPool clientThreads = new QueuedThreadPool();
    clientThreads.setName("client");
    HttpClient client = new HttpClient(new HttpClientTransportOverHTTP(1) {

        @Override
        protected HttpConnectionOverHTTP newHttpConnection(EndPoint endPoint, HttpDestination destination, Promise<Connection> promise) {
            return new HttpConnectionOverHTTP(endPoint, destination, promise) {

                @Override
                protected HttpChannelOverHTTP newHttpChannel() {
                    return new HttpChannelOverHTTP(this) {

                        @Override
                        public void send() {
                            if (afterSetup.get()) {
                                associateLatch.countDown();
                            }
                            super.send();
                        }
                    };
                }

                @Override
                protected void close(Throwable failure) {
                    try {
                        sendLatch.countDown();
                        beginLatch.await(5, TimeUnit.SECONDS);
                        super.close(failure);
                    } catch (InterruptedException x) {
                        x.printStackTrace();
                    }
                }

                @Override
                protected boolean abort(Throwable failure) {
                    try {
                        associateLatch.await(5, TimeUnit.SECONDS);
                        return super.abort(failure);
                    } catch (InterruptedException x) {
                        x.printStackTrace();
                        return false;
                    }
                }
            };
        }
    }, null);
    client.setIdleTimeout(10000);
    client.setExecutor(clientThreads);
    client.start();
    // Create one connection.
    client.newRequest("localhost", connector.getLocalPort()).send();
    Assert.assertTrue(serverLatch.await(5, TimeUnit.SECONDS));
    afterSetup.set(true);
    Thread.sleep(1000);
    // Close the connection, so that the receiver is woken
    // up and will call HttpConnectionOverHTTP.close().
    EndPoint endPoint = endPointRef.get();
    endPoint.close();
    // Wait for close() so that the connection that
    // is being closed is used to send the request.
    Assert.assertTrue(sendLatch.await(5, TimeUnit.SECONDS));
    final CountDownLatch completeLatch = new CountDownLatch(1);
    client.newRequest("localhost", connector.getLocalPort()).timeout(10, TimeUnit.SECONDS).onRequestBegin(request -> {
        try {
            beginLatch.countDown();
            completeLatch.await(5, TimeUnit.SECONDS);
        } catch (InterruptedException x) {
            x.printStackTrace();
        }
    }).send(result -> completeLatch.countDown());
    Assert.assertTrue(completeLatch.await(5, TimeUnit.SECONDS));
    HttpDestinationOverHTTP destination = (HttpDestinationOverHTTP) client.getDestination("http", "localhost", connector.getLocalPort());
    DuplexConnectionPool pool = (DuplexConnectionPool) destination.getConnectionPool();
    Assert.assertEquals(0, pool.getConnectionCount());
    Assert.assertEquals(0, pool.getIdleConnections().size());
    Assert.assertEquals(0, pool.getActiveConnections().size());
}
Also used : Request(org.eclipse.jetty.server.Request) Connection(org.eclipse.jetty.client.api.Connection) EndPoint(org.eclipse.jetty.io.EndPoint) ServletException(javax.servlet.ServletException) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Random(java.util.Random) AtomicReference(java.util.concurrent.atomic.AtomicReference) BytesContentProvider(org.eclipse.jetty.client.util.BytesContentProvider) HttpChannelOverHTTP(org.eclipse.jetty.client.http.HttpChannelOverHTTP) HttpClientTransportOverHTTP(org.eclipse.jetty.client.http.HttpClientTransportOverHTTP) HttpServletRequest(javax.servlet.http.HttpServletRequest) QueuedThreadPool(org.eclipse.jetty.util.thread.QueuedThreadPool) Server(org.eclipse.jetty.server.Server) HttpServletResponse(javax.servlet.http.HttpServletResponse) Promise(org.eclipse.jetty.util.Promise) IOException(java.io.IOException) Test(org.junit.Test) TimeUnit(java.util.concurrent.TimeUnit) HttpDestinationOverHTTP(org.eclipse.jetty.client.http.HttpDestinationOverHTTP) CountDownLatch(java.util.concurrent.CountDownLatch) ServerConnector(org.eclipse.jetty.server.ServerConnector) Assert(org.junit.Assert) HttpConnectionOverHTTP(org.eclipse.jetty.client.http.HttpConnectionOverHTTP) InputStream(java.io.InputStream) Server(org.eclipse.jetty.server.Server) HttpClientTransportOverHTTP(org.eclipse.jetty.client.http.HttpClientTransportOverHTTP) EndPoint(org.eclipse.jetty.io.EndPoint) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) HttpConnectionOverHTTP(org.eclipse.jetty.client.http.HttpConnectionOverHTTP) ServerConnector(org.eclipse.jetty.server.ServerConnector) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) QueuedThreadPool(org.eclipse.jetty.util.thread.QueuedThreadPool) Request(org.eclipse.jetty.server.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) Connection(org.eclipse.jetty.client.api.Connection) HttpServletResponse(javax.servlet.http.HttpServletResponse) AtomicReference(java.util.concurrent.atomic.AtomicReference) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HttpDestinationOverHTTP(org.eclipse.jetty.client.http.HttpDestinationOverHTTP) HttpChannelOverHTTP(org.eclipse.jetty.client.http.HttpChannelOverHTTP) Test(org.junit.Test)

Example 30 with EndPoint

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

the class ServerFCGIConnection method onFillable.

@Override
public void onFillable() {
    EndPoint endPoint = getEndPoint();
    ByteBufferPool bufferPool = connector.getByteBufferPool();
    ByteBuffer buffer = bufferPool.acquire(configuration.getResponseHeaderSize(), true);
    try {
        while (true) {
            int read = endPoint.fill(buffer);
            if (// Avoid boxing of variable 'read'
            LOG.isDebugEnabled())
                LOG.debug("Read {} bytes from {}", read, endPoint);
            if (read > 0) {
                parse(buffer);
            } else if (read == 0) {
                bufferPool.release(buffer);
                fillInterested();
                break;
            } else {
                bufferPool.release(buffer);
                shutdown();
                break;
            }
        }
    } catch (Exception x) {
        if (LOG.isDebugEnabled())
            LOG.debug(x);
        bufferPool.release(buffer);
    // TODO: fail and close ?
    }
}
Also used : ByteBufferPool(org.eclipse.jetty.io.ByteBufferPool) EndPoint(org.eclipse.jetty.io.EndPoint) ByteBuffer(java.nio.ByteBuffer) 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