Search in sources :

Example 1 with DefaultBHttpServerConnection

use of org.apache.http.impl.DefaultBHttpServerConnection in project oap by oaplatform.

the class Server method accepted.

@SneakyThrows
public void accepted(final Socket socket) {
    try {
        final DefaultBHttpServerConnection connection = connectionFactory.createConnection(socket);
        final String connectionId = connection.toString();
        executor.submit(() -> {
            try {
                connections.put(connectionId, connection);
                log.debug("connection accepted: {}", connection);
                final HttpContext httpContext = createHttpContext(socket);
                Thread.currentThread().setName(connection.toString());
                log.debug("start handling {}", connection);
                while (!Thread.interrupted() && connection.isOpen()) httpService.handleRequest(connection, httpContext);
            } catch (SocketException e) {
                if (socketClosed(e))
                    log.debug("Socket closed: {}", connection);
                else if (connectionReset(e))
                    log.warn("Connection reset: {}", connection);
                else
                    log.error(e.getMessage(), e);
            } catch (ConnectionClosedException e) {
                log.debug("connection closed: {}", connection);
            } catch (Throwable e) {
                log.error(e.getMessage(), e);
            } finally {
                connections.remove(connectionId);
                Closeables.close(connection);
            }
        });
    } catch (final IOException e) {
        log.warn(e.getMessage());
        connections.values().forEach(Closeables::close);
        connections.clear();
        throw e;
    }
}
Also used : SocketException(java.net.SocketException) DefaultBHttpServerConnection(org.apache.http.impl.DefaultBHttpServerConnection) HttpContext(org.apache.http.protocol.HttpContext) ConnectionClosedException(org.apache.http.ConnectionClosedException) IOException(java.io.IOException) SneakyThrows(lombok.SneakyThrows)

Example 2 with DefaultBHttpServerConnection

use of org.apache.http.impl.DefaultBHttpServerConnection in project JSCover by tntim96.

the class PersistentStaticHttpServer method run.

public void run() {
    DefaultBHttpServerConnection conn = new DefaultBHttpServerConnection(8 * 1024);
    try {
        conn.bind(socket);
        try {
            boolean keepAlive = true;
            while (keepAlive && !socket.isClosed()) {
                // fully read the request, whatever it is
                HttpRequest request = conn.receiveRequestHeader();
                logger.log(FINE, "Received request: {0}", request);
                keepAlive = isKeepAlive(request);
                if (request instanceof HttpEntityEnclosingRequest) {
                    conn.receiveRequestEntity((HttpEntityEnclosingRequest) request);
                    HttpEntity entity = ((HttpEntityEnclosingRequest) request).getEntity();
                    if (entity != null) {
                        // consume all content to allow reuse
                        EntityUtils.consume(entity);
                    }
                }
                // send static content or reject the method
                String method = request.getRequestLine().getMethod();
                if (method.matches("(?i)get|post|put"))
                    sendOkContent(conn);
                else
                    rejectMethod(conn);
            }
        } finally {
            ioUtils.closeQuietly(conn);
            ioUtils.closeQuietly(socket);
        }
    } catch (HttpException | IOException e) {
        e.printStackTrace();
        ioUtils.closeQuietly(socket);
    }
}
Also used : HttpRequest(org.apache.http.HttpRequest) HttpEntity(org.apache.http.HttpEntity) BasicHttpEntity(org.apache.http.entity.BasicHttpEntity) DefaultBHttpServerConnection(org.apache.http.impl.DefaultBHttpServerConnection) HttpEntityEnclosingRequest(org.apache.http.HttpEntityEnclosingRequest) HttpException(org.apache.http.HttpException) IOException(java.io.IOException)

Aggregations

IOException (java.io.IOException)2 DefaultBHttpServerConnection (org.apache.http.impl.DefaultBHttpServerConnection)2 SocketException (java.net.SocketException)1 SneakyThrows (lombok.SneakyThrows)1 ConnectionClosedException (org.apache.http.ConnectionClosedException)1 HttpEntity (org.apache.http.HttpEntity)1 HttpEntityEnclosingRequest (org.apache.http.HttpEntityEnclosingRequest)1 HttpException (org.apache.http.HttpException)1 HttpRequest (org.apache.http.HttpRequest)1 BasicHttpEntity (org.apache.http.entity.BasicHttpEntity)1 HttpContext (org.apache.http.protocol.HttpContext)1