Search in sources :

Example 6 with EndOfStreamException

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

the class XOPReconstitutor method split.

@SuppressWarnings("deprecation")
private HashMap<String, Part> split(Message message, String boundary) throws IOException, EndOfStreamException, MalformedStreamException {
    HashMap<String, Part> parts = new HashMap<String, Part>();
    MultipartStream multipartStream = new MultipartStream(MessageUtil.getContentAsStream(message), boundary.getBytes(Constants.UTF_8_CHARSET));
    boolean nextPart = multipartStream.skipPreamble();
    while (nextPart) {
        Header header = new Header(multipartStream.readHeaders());
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        multipartStream.readBodyData(baos);
        // see http://www.iana.org/assignments/transfer-encodings/transfer-encodings.xml
        String cte = header.getFirstValue("Content-Transfer-Encoding");
        if (cte != null && !cte.equals("binary") && !cte.equals("8bit") && !cte.equals("7bit"))
            throw new RuntimeException("Content-Transfer-Encoding '" + cte + "' not implemented.");
        Part part = new Part(header, baos.toByteArray());
        String id = part.getContentID();
        if (id != null) {
            parts.put(id, part);
        }
        nextPart = multipartStream.readBoundary();
    }
    return parts;
}
Also used : Header(com.predic8.membrane.core.http.Header) HashMap(java.util.HashMap) ByteArrayOutputStream(java.io.ByteArrayOutputStream) MultipartStream(org.apache.commons.fileupload.MultipartStream)

Example 7 with EndOfStreamException

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

the class BoundConnectionTest method doExchange.

private void doExchange(Connection c, boolean includeAuthorizationHeader) throws IOException, EndOfStreamException {
    createRequest(includeAuthorizationHeader).write(c.out);
    Response r = new Response();
    r.read(c.in, true);
}
Also used : Response(com.predic8.membrane.core.http.Response)

Example 8 with EndOfStreamException

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

the class HttpUtil method readLine.

public static String readLine(InputStream in) throws IOException, EndOfStreamException {
    StringBuilder line = new StringBuilder(128);
    int b;
    int l = 0;
    while ((b = in.read()) != -1) {
        if (b == 13) {
            in.read();
            return line.toString();
        }
        if (b == 10) {
            in.mark(2);
            if (in.read() != 13)
                in.reset();
            return line.toString();
        }
        line.append((char) b);
        if (++l == MAX_LINE_LENGTH)
            throw new LineTooLongException(line.toString());
    }
    throw new EOFWhileReadingLineException(line.toString());
}
Also used : LineTooLongException(com.predic8.membrane.core.transport.http.LineTooLongException) EOFWhileReadingLineException(com.predic8.membrane.core.transport.http.EOFWhileReadingLineException)

Example 9 with EndOfStreamException

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

use of com.predic8.membrane.core.util.EndOfStreamException 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)4 EOFWhileReadingFirstLineException (com.predic8.membrane.core.transport.http.EOFWhileReadingFirstLineException)3 EOFWhileReadingLineException (com.predic8.membrane.core.transport.http.EOFWhileReadingLineException)3 EndOfStreamException (com.predic8.membrane.core.util.EndOfStreamException)3 Exchange (com.predic8.membrane.core.exchange.Exchange)2 Message (com.predic8.membrane.core.http.Message)2 Request (com.predic8.membrane.core.http.Request)2 Matcher (java.util.regex.Matcher)2 Body (com.predic8.membrane.core.http.Body)1 Header (com.predic8.membrane.core.http.Header)1 Response (com.predic8.membrane.core.http.Response)1 AbortException (com.predic8.membrane.core.transport.http.AbortException)1 LineTooLongException (com.predic8.membrane.core.transport.http.LineTooLongException)1 NoMoreRequestsException (com.predic8.membrane.core.transport.http.NoMoreRequestsException)1 NoResponseException (com.predic8.membrane.core.transport.http.NoResponseException)1 DNSCache (com.predic8.membrane.core.util.DNSCache)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 InputStream (java.io.InputStream)1 SocketException (java.net.SocketException)1