Search in sources :

Example 21 with HttpProcessor

use of org.apache.hc.core5.http.protocol.HttpProcessor in project httpcomponents-core by apache.

the class Http1IntegrationTest method testPostIdentityTransfer.

@Test
public void testPostIdentityTransfer() throws Exception {
    server.register("/hello", () -> new SingleLineResponseHandler("Hi there"));
    final HttpProcessor httpProcessor = new DefaultHttpProcessor(new RequestValidateHost());
    final InetSocketAddress serverEndpoint = server.start(httpProcessor, Http1Config.DEFAULT);
    client.start();
    final int reqNo = 5;
    for (int i = 0; i < reqNo; i++) {
        final Future<ClientSessionEndpoint> connectFuture = client.connect("localhost", serverEndpoint.getPort(), TIMEOUT);
        final ClientSessionEndpoint streamEndpoint = connectFuture.get();
        final Future<Message<HttpResponse, String>> future = streamEndpoint.execute(new BasicRequestProducer(Method.POST, createRequestURI(serverEndpoint, "/hello"), new MultiLineEntityProducer("Hello", 16 * i)), new BasicResponseConsumer<>(new StringAsyncEntityConsumer()), null);
        final Message<HttpResponse, String> result = future.get(TIMEOUT.getDuration(), TIMEOUT.getTimeUnit());
        streamEndpoint.close();
        Assertions.assertNotNull(result);
        final HttpResponse response = result.getHead();
        final String entity = result.getBody();
        Assertions.assertNotNull(response);
        Assertions.assertEquals(200, response.getCode());
        Assertions.assertEquals("Hi there", entity);
    }
}
Also used : StringAsyncEntityConsumer(org.apache.hc.core5.http.nio.entity.StringAsyncEntityConsumer) Message(org.apache.hc.core5.http.Message) HttpProcessor(org.apache.hc.core5.http.protocol.HttpProcessor) DefaultHttpProcessor(org.apache.hc.core5.http.protocol.DefaultHttpProcessor) InetSocketAddress(java.net.InetSocketAddress) BasicRequestProducer(org.apache.hc.core5.http.nio.support.BasicRequestProducer) HttpResponse(org.apache.hc.core5.http.HttpResponse) BasicHttpResponse(org.apache.hc.core5.http.message.BasicHttpResponse) DefaultHttpProcessor(org.apache.hc.core5.http.protocol.DefaultHttpProcessor) RequestValidateHost(org.apache.hc.core5.http.protocol.RequestValidateHost) Test(org.junit.Test)

Example 22 with HttpProcessor

use of org.apache.hc.core5.http.protocol.HttpProcessor in project httpcomponents-core by apache.

the class H2TestServer method start.

public InetSocketAddress start(final HttpProcessor httpProcessor, final Decorator<AsyncServerExchangeHandler> exchangeHandlerDecorator, final Http1Config http1Config) throws Exception {
    start(new InternalServerProtocolNegotiationStarter(httpProcessor != null ? httpProcessor : HttpProcessors.server(), new DefaultAsyncResponseExchangeHandlerFactory(registry, exchangeHandlerDecorator != null ? exchangeHandlerDecorator : BasicAsyncServerExpectationDecorator::new), HttpVersionPolicy.FORCE_HTTP_1, H2Config.DEFAULT, http1Config, CharCodingConfig.DEFAULT, sslContext, sslSessionInitializer, sslSessionVerifier));
    final Future<ListenerEndpoint> future = listen(new InetSocketAddress(0));
    final ListenerEndpoint listener = future.get();
    return (InetSocketAddress) listener.getAddress();
}
Also used : ListenerEndpoint(org.apache.hc.core5.reactor.ListenerEndpoint) InetSocketAddress(java.net.InetSocketAddress) DefaultAsyncResponseExchangeHandlerFactory(org.apache.hc.core5.http.nio.support.DefaultAsyncResponseExchangeHandlerFactory)

Example 23 with HttpProcessor

use of org.apache.hc.core5.http.protocol.HttpProcessor in project httpcomponents-core by apache.

the class InternalClientProtocolNegotiationStarter method createHandler.

@Override
public IOEventHandler createHandler(final ProtocolIOSession ioSession, final Object attachment) {
    if (sslContext != null) {
        ioSession.startTls(sslContext, null, null, sslSessionInitializer, sslSessionVerifier, null);
    }
    final ClientHttp1StreamDuplexerFactory http1StreamHandlerFactory = new ClientHttp1StreamDuplexerFactory(httpProcessor != null ? httpProcessor : HttpProcessors.client(), http1Config, charCodingConfig, LoggingHttp1StreamListener.INSTANCE_CLIENT);
    final ClientH2StreamMultiplexerFactory http2StreamHandlerFactory = new ClientH2StreamMultiplexerFactory(httpProcessor != null ? httpProcessor : H2Processors.client(), exchangeHandlerFactory, h2Config, charCodingConfig, LoggingH2StreamListener.INSTANCE);
    ioSession.registerProtocol(ApplicationProtocol.HTTP_1_1.id, new ClientHttp1UpgradeHandler(http1StreamHandlerFactory));
    ioSession.registerProtocol(ApplicationProtocol.HTTP_2.id, new ClientH2UpgradeHandler(http2StreamHandlerFactory));
    switch(versionPolicy) {
        case FORCE_HTTP_2:
            return new ClientH2PrefaceHandler(ioSession, http2StreamHandlerFactory, false);
        case FORCE_HTTP_1:
            return new ClientHttp1IOEventHandler(http1StreamHandlerFactory.create(ioSession));
        default:
            return new HttpProtocolNegotiator(ioSession, null);
    }
}
Also used : ClientH2PrefaceHandler(org.apache.hc.core5.http2.impl.nio.ClientH2PrefaceHandler) ClientHttp1StreamDuplexerFactory(org.apache.hc.core5.http.impl.nio.ClientHttp1StreamDuplexerFactory) HttpProtocolNegotiator(org.apache.hc.core5.http2.impl.nio.HttpProtocolNegotiator) ClientH2StreamMultiplexerFactory(org.apache.hc.core5.http2.impl.nio.ClientH2StreamMultiplexerFactory) ClientH2UpgradeHandler(org.apache.hc.core5.http2.impl.nio.ClientH2UpgradeHandler) ClientHttp1UpgradeHandler(org.apache.hc.core5.http2.impl.nio.ClientHttp1UpgradeHandler) ClientHttp1IOEventHandler(org.apache.hc.core5.http.impl.nio.ClientHttp1IOEventHandler)

Example 24 with HttpProcessor

use of org.apache.hc.core5.http.protocol.HttpProcessor in project httpcomponents-core by apache.

the class InternalServerProtocolNegotiationStarter method createHandler.

@Override
public IOEventHandler createHandler(final ProtocolIOSession ioSession, final Object attachment) {
    if (sslContext != null) {
        ioSession.startTls(sslContext, null, null, sslSessionInitializer, sslSessionVerifier, null);
    }
    final ServerHttp1StreamDuplexerFactory http1StreamHandlerFactory = new ServerHttp1StreamDuplexerFactory(httpProcessor != null ? httpProcessor : HttpProcessors.server(), exchangeHandlerFactory, http1Config, charCodingConfig, LoggingHttp1StreamListener.INSTANCE_SERVER);
    final ServerH2StreamMultiplexerFactory http2StreamHandlerFactory = new ServerH2StreamMultiplexerFactory(httpProcessor != null ? httpProcessor : H2Processors.server(), exchangeHandlerFactory, h2Config, charCodingConfig, LoggingH2StreamListener.INSTANCE);
    ioSession.registerProtocol(ApplicationProtocol.HTTP_1_1.id, new ServerHttp1UpgradeHandler(http1StreamHandlerFactory));
    ioSession.registerProtocol(ApplicationProtocol.HTTP_2.id, new ServerH2UpgradeHandler(http2StreamHandlerFactory));
    switch(versionPolicy) {
        case FORCE_HTTP_2:
            return new ServerH2PrefaceHandler(ioSession, http2StreamHandlerFactory);
        case FORCE_HTTP_1:
            return new ServerHttp1IOEventHandler(http1StreamHandlerFactory.create(sslContext != null ? URIScheme.HTTPS.id : URIScheme.HTTP.id, ioSession));
        default:
            return new HttpProtocolNegotiator(ioSession, null);
    }
}
Also used : ServerHttp1StreamDuplexerFactory(org.apache.hc.core5.http.impl.nio.ServerHttp1StreamDuplexerFactory) ServerH2PrefaceHandler(org.apache.hc.core5.http2.impl.nio.ServerH2PrefaceHandler) HttpProtocolNegotiator(org.apache.hc.core5.http2.impl.nio.HttpProtocolNegotiator) ServerH2StreamMultiplexerFactory(org.apache.hc.core5.http2.impl.nio.ServerH2StreamMultiplexerFactory) ServerHttp1IOEventHandler(org.apache.hc.core5.http.impl.nio.ServerHttp1IOEventHandler) ServerHttp1UpgradeHandler(org.apache.hc.core5.http2.impl.nio.ServerHttp1UpgradeHandler) ServerH2UpgradeHandler(org.apache.hc.core5.http2.impl.nio.ServerH2UpgradeHandler)

Example 25 with HttpProcessor

use of org.apache.hc.core5.http.protocol.HttpProcessor in project httpcomponents-core by apache.

the class TestHttpRequestExecutor method testInvalidInput.

@Test
public void testInvalidInput() throws Exception {
    final HttpCoreContext context = HttpCoreContext.create();
    final ClassicHttpRequest request = new BasicClassicHttpRequest(Method.GET, "/");
    final ClassicHttpResponse response = new BasicClassicHttpResponse(200, "OK");
    final HttpClientConnection conn = Mockito.mock(HttpClientConnection.class);
    final HttpProcessor httprocessor = Mockito.mock(HttpProcessor.class);
    Assertions.assertThrows(NullPointerException.class, () -> {
        final HttpRequestExecutor executor = new HttpRequestExecutor();
        executor.execute(null, conn, context);
    });
    Assertions.assertThrows(NullPointerException.class, () -> {
        final HttpRequestExecutor executor = new HttpRequestExecutor();
        executor.execute(request, null, context);
    });
    Assertions.assertThrows(NullPointerException.class, () -> {
        final HttpRequestExecutor executor = new HttpRequestExecutor();
        executor.execute(request, conn, null);
    });
    Assertions.assertThrows(NullPointerException.class, () -> {
        final HttpRequestExecutor executor = new HttpRequestExecutor();
        executor.preProcess(null, httprocessor, context);
    });
    Assertions.assertThrows(NullPointerException.class, () -> {
        final HttpRequestExecutor executor = new HttpRequestExecutor();
        executor.preProcess(request, null, context);
    });
    Assertions.assertThrows(NullPointerException.class, () -> {
        final HttpRequestExecutor executor = new HttpRequestExecutor();
        executor.preProcess(request, httprocessor, null);
    });
    Assertions.assertThrows(NullPointerException.class, () -> {
        final HttpRequestExecutor executor = new HttpRequestExecutor();
        executor.postProcess(null, httprocessor, context);
    });
    Assertions.assertThrows(NullPointerException.class, () -> {
        final HttpRequestExecutor executor = new HttpRequestExecutor();
        executor.postProcess(response, null, context);
    });
    Assertions.assertThrows(NullPointerException.class, () -> {
        final HttpRequestExecutor executor = new HttpRequestExecutor();
        executor.postProcess(response, httprocessor, null);
    });
}
Also used : BasicClassicHttpResponse(org.apache.hc.core5.http.message.BasicClassicHttpResponse) ClassicHttpResponse(org.apache.hc.core5.http.ClassicHttpResponse) BasicClassicHttpRequest(org.apache.hc.core5.http.message.BasicClassicHttpRequest) ClassicHttpRequest(org.apache.hc.core5.http.ClassicHttpRequest) BasicClassicHttpRequest(org.apache.hc.core5.http.message.BasicClassicHttpRequest) HttpClientConnection(org.apache.hc.core5.http.io.HttpClientConnection) HttpProcessor(org.apache.hc.core5.http.protocol.HttpProcessor) HttpCoreContext(org.apache.hc.core5.http.protocol.HttpCoreContext) BasicClassicHttpResponse(org.apache.hc.core5.http.message.BasicClassicHttpResponse) Test(org.junit.jupiter.api.Test)

Aggregations

HttpProcessor (org.apache.hc.core5.http.protocol.HttpProcessor)17 HttpCoreContext (org.apache.hc.core5.http.protocol.HttpCoreContext)16 HttpClientConnection (org.apache.hc.core5.http.io.HttpClientConnection)12 ClassicHttpResponse (org.apache.hc.core5.http.ClassicHttpResponse)11 ClassicHttpRequest (org.apache.hc.core5.http.ClassicHttpRequest)10 Test (org.junit.jupiter.api.Test)10 BasicClassicHttpRequest (org.apache.hc.core5.http.message.BasicClassicHttpRequest)8 BasicClassicHttpResponse (org.apache.hc.core5.http.message.BasicClassicHttpResponse)8 InetSocketAddress (java.net.InetSocketAddress)7 HttpResponse (org.apache.hc.core5.http.HttpResponse)7 Timeout (org.apache.hc.core5.util.Timeout)7 HttpEntity (org.apache.hc.core5.http.HttpEntity)5 HttpHost (org.apache.hc.core5.http.HttpHost)5 Message (org.apache.hc.core5.http.Message)5 IOSession (org.apache.hc.core5.reactor.IOSession)5 Supplier (org.apache.hc.core5.function.Supplier)4 DefaultAsyncResponseExchangeHandlerFactory (org.apache.hc.core5.http.nio.support.DefaultAsyncResponseExchangeHandlerFactory)4 RequestHandlerRegistry (org.apache.hc.core5.http.protocol.RequestHandlerRegistry)4 IOEventHandlerFactory (org.apache.hc.core5.reactor.IOEventHandlerFactory)4 List (java.util.List)3