Search in sources :

Example 1 with DNSCache

use of com.predic8.membrane.core.util.DNSCache 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 2 with DNSCache

use of com.predic8.membrane.core.util.DNSCache in project service-proxy by membrane.

the class HttpServerHandler method process.

private void process() throws Exception {
    try {
        DNSCache dnsCache = getTransport().getRouter().getDnsCache();
        InetAddress remoteAddr = sourceSocket.getInetAddress();
        String ip = dnsCache.getHostAddress(remoteAddr);
        exchange.setRemoteAddrIp(ip);
        exchange.setRemoteAddr(getTransport().isReverseDNS() ? dnsCache.getHostName(remoteAddr) : ip);
        exchange.setRequest(srcReq);
        exchange.setOriginalRequestUri(srcReq.getUri());
        if (exchange.getRequest().getHeader().is100ContinueExpected()) {
            final Request request = exchange.getRequest();
            request.addObserver(new MessageObserver() {

                public void bodyRequested(AbstractBody body) {
                    try {
                        if (request.getHeader().is100ContinueExpected()) {
                            // request body from client so that interceptors can handle it
                            Response.continue100().build().write(srcOut);
                            // remove "Expect: 100-continue" since we already sent "100 Continue"
                            request.getHeader().removeFields(Header.EXPECT);
                        }
                    } catch (Exception e) {
                        throw new RuntimeException(e);
                    }
                }

                public void bodyComplete(AbstractBody body) {
                }
            });
        }
        invokeHandlers();
        exchange.blockResponseIfNeeded();
    } catch (AbortException e) {
        log.debug("Aborted");
        exchange.finishExchange(true, e.getMessage());
        removeBodyFromBuffer();
        writeResponse(exchange.getResponse());
        log.debug("exchange set aborted");
        return;
    }
    try {
        removeBodyFromBuffer();
        writeResponse(exchange.getResponse());
        exchange.setCompleted();
        log.debug("exchange set completed");
    } catch (Exception e) {
        exchange.finishExchange(true, e.getMessage());
        throw e;
    }
}
Also used : MessageObserver(com.predic8.membrane.core.http.MessageObserver) Request(com.predic8.membrane.core.http.Request) AbstractBody(com.predic8.membrane.core.http.AbstractBody) DNSCache(com.predic8.membrane.core.util.DNSCache) InetAddress(java.net.InetAddress) IOException(java.io.IOException) EndOfStreamException(com.predic8.membrane.core.util.EndOfStreamException) SocketException(java.net.SocketException) SSLException(javax.net.ssl.SSLException) SocketTimeoutException(java.net.SocketTimeoutException)

Aggregations

DNSCache (com.predic8.membrane.core.util.DNSCache)2 EndOfStreamException (com.predic8.membrane.core.util.EndOfStreamException)2 IOException (java.io.IOException)2 AbstractBody (com.predic8.membrane.core.http.AbstractBody)1 MessageObserver (com.predic8.membrane.core.http.MessageObserver)1 Request (com.predic8.membrane.core.http.Request)1 AbortException (com.predic8.membrane.core.transport.http.AbortException)1 EOFWhileReadingFirstLineException (com.predic8.membrane.core.transport.http.EOFWhileReadingFirstLineException)1 InetAddress (java.net.InetAddress)1 SocketException (java.net.SocketException)1 SocketTimeoutException (java.net.SocketTimeoutException)1 SSLException (javax.net.ssl.SSLException)1