Search in sources :

Example 11 with Connection

use of com.predic8.membrane.core.transport.http.Connection in project service-proxy by membrane.

the class HttpKeepAliveTest method testConnectionClose.

@Test
public void testConnectionClose() throws Exception {
    HttpClient client = createHttpClient(500);
    sp1.getInterceptors().add(0, new AbstractInterceptor() {

        @Override
        public Outcome handleResponse(Exchange exc) throws Exception {
            exc.getResponse().getHeader().add(Header.KEEP_ALIVE, "max=2");
            return Outcome.CONTINUE;
        }
    });
    // opens connection 1
    assertEquals(200, issueRequest(client));
    assertEquals(1, set.size());
    assertEquals(1, client.getConnectionManager().getNumberInPool());
    // connection closer did not yet run
    Thread.sleep(600);
    // connection 1 is now dead, but still in pool
    assertEquals(1, client.getConnectionManager().getNumberInPool());
    // opens connection 2
    assertEquals(200, issueRequest(client));
    assertEquals(2, set.size());
    // connection closer runs and closes both
    Thread.sleep(600);
    assertEquals(0, client.getConnectionManager().getNumberInPool());
}
Also used : Exchange(com.predic8.membrane.core.exchange.Exchange) Outcome(com.predic8.membrane.core.interceptor.Outcome) AbstractInterceptor(com.predic8.membrane.core.interceptor.AbstractInterceptor) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) Test(org.junit.Test)

Example 12 with Connection

use of com.predic8.membrane.core.transport.http.Connection in project service-proxy by membrane.

the class HttpServletHandler method run.

public void run() {
    try {
        srcReq = createRequest();
        exchange.received();
        try {
            DNSCache dnsCache = getTransport().getRouter().getDnsCache();
            String ip = dnsCache.getHostAddress(remoteAddr);
            exchange.setRemoteAddrIp(ip);
            exchange.setRemoteAddr(getTransport().isReverseDNS() ? dnsCache.getHostName(remoteAddr) : ip);
            exchange.setRequest(srcReq);
            exchange.setOriginalRequestUri(srcReq.getUri());
            invokeHandlers();
        } catch (AbortException e) {
            exchange.finishExchange(true, exchange.getErrorMessage());
            writeResponse(exchange.getResponse());
            return;
        }
        // read if not alread read
        exchange.getRequest().readBody();
        writeResponse(exchange.getResponse());
        exchange.setCompleted();
    } catch (EndOfStreamException e) {
        log.debug("stream closed");
    } catch (EOFWhileReadingFirstLineException e) {
        log.debug("Client connection terminated before line was read. Line so far: (" + e.getLineSoFar() + ")");
    } catch (Exception e) {
        log.error(e.getMessage(), e);
    } finally {
        exchange.detach();
    }
}
Also used : EndOfStreamException(com.predic8.membrane.core.util.EndOfStreamException) EOFWhileReadingFirstLineException(com.predic8.membrane.core.transport.http.EOFWhileReadingFirstLineException) DNSCache(com.predic8.membrane.core.util.DNSCache) IOException(java.io.IOException) AbortException(com.predic8.membrane.core.transport.http.AbortException) EOFWhileReadingFirstLineException(com.predic8.membrane.core.transport.http.EOFWhileReadingFirstLineException) EndOfStreamException(com.predic8.membrane.core.util.EndOfStreamException) AbortException(com.predic8.membrane.core.transport.http.AbortException)

Example 13 with Connection

use of com.predic8.membrane.core.transport.http.Connection in project service-proxy by membrane.

the class HttpEndpointListener method run.

@Override
public void run() {
    while (!closed) {
        try {
            Socket socket = serverSocket.accept();
            openSockets.put(socket, Boolean.TRUE);
            try {
                if (log.isDebugEnabled())
                    log.debug("Accepted connection from " + socket.getRemoteSocketAddress());
                transport.getExecutorService().execute(new HttpServerHandler(socket, this));
            } catch (RejectedExecutionException e) {
                openSockets.remove(socket);
                log.error("HttpServerHandler execution rejected. Might be due to a proxies.xml hot deployment in progress or a low" + " value for <transport maxThreadPoolSize=\"...\">.");
                socket.close();
            }
        } catch (SocketException e) {
            String message = e.getMessage();
            if (message != null && (message.endsWith("socket closed") || message.endsWith("Socket closed"))) {
                log.debug("socket closed.");
                break;
            } else
                log.error("", e);
        } catch (NullPointerException e) {
        // Ignore this. serverSocket variable is set null during a loop in the process of closing server socket.
        } catch (Exception e) {
            log.error("", e);
        }
    }
}
Also used : SocketException(java.net.SocketException) Socket(java.net.Socket) ServerSocket(java.net.ServerSocket) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) IOException(java.io.IOException) BindException(java.net.BindException) PortOccupiedException(com.predic8.membrane.core.transport.PortOccupiedException) SocketException(java.net.SocketException) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) InvalidParameterException(java.security.InvalidParameterException)

Example 14 with Connection

use of com.predic8.membrane.core.transport.http.Connection in project service-proxy by membrane.

the class HttpServerHandler method run.

public void run() {
    // see Request.isBindTargetConnectionToIncoming()
    Connection boundConnection = null;
    try {
        updateThreadName(true);
        setup();
        while (true) {
            srcReq = new Request();
            endpointListener.setIdleStatus(sourceSocket, true);
            try {
                srcIn.mark(2);
                if (srcIn.read() == -1)
                    break;
                srcIn.reset();
            } finally {
                endpointListener.setIdleStatus(sourceSocket, false);
            }
            if (boundConnection != null) {
                exchange.setTargetConnection(boundConnection);
                boundConnection = null;
            }
            srcReq.read(srcIn, true);
            exchange.received();
            if (srcReq.getHeader().getProxyConnection() != null) {
                srcReq.getHeader().add(Header.CONNECTION, srcReq.getHeader().getProxyConnection());
                srcReq.getHeader().removeFields(Header.PROXY_CONNECTION);
            }
            process();
            if (srcReq.isCONNECTRequest()) {
                log.debug("stopping HTTP Server Thread after establishing an HTTP connect");
                return;
            }
            boundConnection = exchange.getTargetConnection();
            exchange.setTargetConnection(null);
            if (!exchange.canKeepConnectionAlive())
                break;
            if (exchange.getResponse().isRedirect()) {
                break;
            }
            exchange.detach();
            exchange = new Exchange(this);
        }
    } catch (SocketTimeoutException e) {
        log.debug("Socket of thread " + counter + " timed out");
    } catch (SocketException se) {
        log.debug("client socket closed");
    } catch (SSLException s) {
        if (showSSLExceptions) {
            if (s.getCause() instanceof SSLException)
                s = (SSLException) s.getCause();
            if (s.getCause() instanceof SocketException)
                log.debug("ssl socket closed");
            else
                log.error("", s);
        }
    } catch (IOException e) {
        log.error("", e);
    } catch (EndOfStreamException e) {
        log.debug("stream closed");
    } catch (AbortException e) {
        log.debug("exchange aborted.");
    } catch (NoMoreRequestsException e) {
    // happens at the end of a keep-alive connection
    } catch (NoResponseException e) {
        log.debug("No response received. Maybe increase the keep-alive timeout on the server.");
    } catch (EOFWhileReadingFirstLineException e) {
        log.debug("Client connection terminated before line was read. Line so far: (" + e.getLineSoFar() + ")");
    } catch (Exception e) {
        log.error("", e);
    } finally {
        endpointListener.setOpenStatus(sourceSocket, false);
        if (boundConnection != null)
            try {
                boundConnection.close();
            } catch (IOException e) {
                log.debug("Closing bound connection.", e);
            }
        closeConnections();
        exchange.detach();
        updateThreadName(false);
    }
}
Also used : SocketException(java.net.SocketException) EndOfStreamException(com.predic8.membrane.core.util.EndOfStreamException) Request(com.predic8.membrane.core.http.Request) IOException(java.io.IOException) SSLException(javax.net.ssl.SSLException) IOException(java.io.IOException) EndOfStreamException(com.predic8.membrane.core.util.EndOfStreamException) SocketException(java.net.SocketException) SSLException(javax.net.ssl.SSLException) SocketTimeoutException(java.net.SocketTimeoutException) Exchange(com.predic8.membrane.core.exchange.Exchange) SocketTimeoutException(java.net.SocketTimeoutException)

Aggregations

IOException (java.io.IOException)7 Exchange (com.predic8.membrane.core.exchange.Exchange)3 EndOfStreamException (com.predic8.membrane.core.util.EndOfStreamException)3 Request (com.predic8.membrane.core.http.Request)2 AbstractInterceptor (com.predic8.membrane.core.interceptor.AbstractInterceptor)2 Outcome (com.predic8.membrane.core.interceptor.Outcome)2 EOFWhileReadingFirstLineException (com.predic8.membrane.core.transport.http.EOFWhileReadingFirstLineException)2 SSLProvider (com.predic8.membrane.core.transport.ssl.SSLProvider)2 Socket (java.net.Socket)2 SocketException (java.net.SocketException)2 URISyntaxException (java.net.URISyntaxException)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 HttpRouter (com.predic8.membrane.core.HttpRouter)1 Header (com.predic8.membrane.core.http.Header)1 Response (com.predic8.membrane.core.http.Response)1 ByThreadStrategy (com.predic8.membrane.core.interceptor.balancer.ByThreadStrategy)1 LoadBalancingInterceptor (com.predic8.membrane.core.interceptor.balancer.LoadBalancingInterceptor)1 RoundRobinStrategy (com.predic8.membrane.core.interceptor.balancer.RoundRobinStrategy)1 User (com.predic8.membrane.core.interceptor.registration.entity.User)1 AbstractExchangeViewerListener (com.predic8.membrane.core.model.AbstractExchangeViewerListener)1