Search in sources :

Example 1 with HttpServerConnection

use of org.apache.hc.core5.http.io.HttpServerConnection in project httpcomponents-core by apache.

the class RequestListener method run.

@Override
public void run() {
    try {
        while (!isTerminated() && !Thread.interrupted()) {
            final Socket socket = this.serverSocket.accept();
            socket.setSoTimeout(this.socketConfig.getSoTimeout().toMillisecondsIntBound());
            socket.setKeepAlive(this.socketConfig.isSoKeepAlive());
            socket.setTcpNoDelay(this.socketConfig.isTcpNoDelay());
            if (this.socketConfig.getRcvBufSize() > 0) {
                socket.setReceiveBufferSize(this.socketConfig.getRcvBufSize());
            }
            if (this.socketConfig.getSndBufSize() > 0) {
                socket.setSendBufferSize(this.socketConfig.getSndBufSize());
            }
            if (this.socketConfig.getSoLinger().toSeconds() >= 0) {
                socket.setSoLinger(true, this.socketConfig.getSoLinger().toSecondsIntBound());
            }
            final HttpServerConnection conn = this.connectionFactory.createConnection(socket);
            final Worker worker = new Worker(this.httpService, conn, this.exceptionListener);
            this.executorService.execute(worker);
        }
    } catch (final Exception ex) {
        this.exceptionListener.onError(ex);
    }
}
Also used : HttpServerConnection(org.apache.hc.core5.http.io.HttpServerConnection) ServerSocket(java.net.ServerSocket) Socket(java.net.Socket) IOException(java.io.IOException)

Example 2 with HttpServerConnection

use of org.apache.hc.core5.http.io.HttpServerConnection in project httpcomponents-core by apache.

the class HttpService method handleRequest.

/**
 * Handles receives one HTTP request over the given connection within the
 * given execution context and sends a response back to the client.
 *
 * @param conn the active connection to the client
 * @param context the actual execution context.
 * @throws IOException in case of an I/O error.
 * @throws HttpException in case of HTTP protocol violation or a processing
 *   problem.
 */
public void handleRequest(final HttpServerConnection conn, final HttpContext context) throws IOException, HttpException {
    final AtomicBoolean responseSubmitted = new AtomicBoolean(false);
    try {
        final ClassicHttpRequest request = conn.receiveRequestHeader();
        if (request == null) {
            conn.close();
            return;
        }
        if (streamListener != null) {
            streamListener.onRequestHead(conn, request);
        }
        conn.receiveRequestEntity(request);
        final ProtocolVersion transportVersion = request.getVersion();
        context.setProtocolVersion(transportVersion != null ? transportVersion : HttpVersion.HTTP_1_1);
        context.setAttribute(HttpCoreContext.SSL_SESSION, conn.getSSLSession());
        context.setAttribute(HttpCoreContext.CONNECTION_ENDPOINT, conn.getEndpointDetails());
        context.setAttribute(HttpCoreContext.HTTP_REQUEST, request);
        this.processor.process(request, request.getEntity(), context);
        this.requestHandler.handle(request, new HttpServerRequestHandler.ResponseTrigger() {

            @Override
            public void sendInformation(final ClassicHttpResponse response) throws HttpException, IOException {
                if (responseSubmitted.get()) {
                    throw new HttpException("Response already submitted");
                }
                if (response.getCode() >= HttpStatus.SC_SUCCESS) {
                    throw new HttpException("Invalid intermediate response");
                }
                if (streamListener != null) {
                    streamListener.onResponseHead(conn, response);
                }
                conn.sendResponseHeader(response);
                conn.flush();
            }

            @Override
            public void submitResponse(final ClassicHttpResponse response) throws HttpException, IOException {
                try {
                    final ProtocolVersion transportVersion = response.getVersion();
                    if (transportVersion != null && transportVersion.greaterEquals(HttpVersion.HTTP_2)) {
                        throw new UnsupportedHttpVersionException(transportVersion);
                    }
                    ServerSupport.validateResponse(response, response.getEntity());
                    context.setProtocolVersion(transportVersion != null ? transportVersion : HttpVersion.HTTP_1_1);
                    context.setAttribute(HttpCoreContext.HTTP_RESPONSE, response);
                    processor.process(response, response.getEntity(), context);
                    responseSubmitted.set(true);
                    conn.sendResponseHeader(response);
                    if (streamListener != null) {
                        streamListener.onResponseHead(conn, response);
                    }
                    if (MessageSupport.canResponseHaveBody(request.getMethod(), response)) {
                        conn.sendResponseEntity(response);
                    }
                    // Make sure the request content is fully consumed
                    EntityUtils.consume(request.getEntity());
                    final boolean keepAlive = connReuseStrategy.keepAlive(request, response, context);
                    if (streamListener != null) {
                        streamListener.onExchangeComplete(conn, keepAlive);
                    }
                    if (!keepAlive) {
                        conn.close();
                    }
                    conn.flush();
                } finally {
                    response.close();
                }
            }
        }, context);
    } catch (final HttpException ex) {
        if (responseSubmitted.get()) {
            throw ex;
        }
        try (final ClassicHttpResponse errorResponse = new BasicClassicHttpResponse(HttpStatus.SC_INTERNAL_SERVER_ERROR)) {
            handleException(ex, errorResponse);
            errorResponse.setHeader(HttpHeaders.CONNECTION, HeaderElements.CLOSE);
            context.setAttribute(HttpCoreContext.HTTP_RESPONSE, errorResponse);
            this.processor.process(errorResponse, errorResponse.getEntity(), context);
            conn.sendResponseHeader(errorResponse);
            if (streamListener != null) {
                streamListener.onResponseHead(conn, errorResponse);
            }
            conn.sendResponseEntity(errorResponse);
            conn.close();
        }
    }
}
Also used : ClassicHttpResponse(org.apache.hc.core5.http.ClassicHttpResponse) BasicClassicHttpResponse(org.apache.hc.core5.http.message.BasicClassicHttpResponse) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ClassicHttpRequest(org.apache.hc.core5.http.ClassicHttpRequest) HttpServerRequestHandler(org.apache.hc.core5.http.io.HttpServerRequestHandler) BasicHttpServerRequestHandler(org.apache.hc.core5.http.io.support.BasicHttpServerRequestHandler) HttpException(org.apache.hc.core5.http.HttpException) IOException(java.io.IOException) UnsupportedHttpVersionException(org.apache.hc.core5.http.UnsupportedHttpVersionException) ProtocolVersion(org.apache.hc.core5.http.ProtocolVersion) BasicClassicHttpResponse(org.apache.hc.core5.http.message.BasicClassicHttpResponse)

Aggregations

IOException (java.io.IOException)2 ServerSocket (java.net.ServerSocket)1 Socket (java.net.Socket)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 ClassicHttpRequest (org.apache.hc.core5.http.ClassicHttpRequest)1 ClassicHttpResponse (org.apache.hc.core5.http.ClassicHttpResponse)1 HttpException (org.apache.hc.core5.http.HttpException)1 ProtocolVersion (org.apache.hc.core5.http.ProtocolVersion)1 UnsupportedHttpVersionException (org.apache.hc.core5.http.UnsupportedHttpVersionException)1 HttpServerConnection (org.apache.hc.core5.http.io.HttpServerConnection)1 HttpServerRequestHandler (org.apache.hc.core5.http.io.HttpServerRequestHandler)1 BasicHttpServerRequestHandler (org.apache.hc.core5.http.io.support.BasicHttpServerRequestHandler)1 BasicClassicHttpResponse (org.apache.hc.core5.http.message.BasicClassicHttpResponse)1