Search in sources :

Example 1 with HttpProcessor

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

the class ClassicTestServer method start.

public void start(final Http1Config http1Config, final HttpProcessor httpProcessor, final Decorator<HttpServerRequestHandler> handlerDecorator) throws IOException {
    if (serverRef.get() == null) {
        final HttpServerRequestHandler handler = new BasicHttpServerRequestHandler(registry);
        final HttpService httpService = new HttpService(httpProcessor != null ? httpProcessor : HttpProcessors.server(), handlerDecorator != null ? handlerDecorator.decorate(handler) : new BasicHttpServerExpectationDecorator(handler), DefaultConnectionReuseStrategy.INSTANCE, LoggingHttp1StreamListener.INSTANCE);
        final HttpServer server = new HttpServer(0, httpService, null, socketConfig, sslContext != null ? sslContext.getServerSocketFactory() : ServerSocketFactory.getDefault(), new LoggingBHttpServerConnectionFactory(sslContext != null ? URIScheme.HTTPS.id : URIScheme.HTTP.id, http1Config != null ? http1Config : Http1Config.DEFAULT, CharCodingConfig.DEFAULT), null, LoggingExceptionListener.INSTANCE);
        if (serverRef.compareAndSet(null, server)) {
            server.start();
        }
    } else {
        throw new IllegalStateException("Server already running");
    }
}
Also used : BasicHttpServerExpectationDecorator(org.apache.hc.core5.http.io.support.BasicHttpServerExpectationDecorator) HttpServerRequestHandler(org.apache.hc.core5.http.io.HttpServerRequestHandler) BasicHttpServerRequestHandler(org.apache.hc.core5.http.io.support.BasicHttpServerRequestHandler) HttpService(org.apache.hc.core5.http.impl.io.HttpService) HttpServer(org.apache.hc.core5.http.impl.bootstrap.HttpServer) BasicHttpServerRequestHandler(org.apache.hc.core5.http.io.support.BasicHttpServerRequestHandler)

Example 2 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 H2Config h2Config) throws Exception {
    start(new InternalServerProtocolNegotiationStarter(httpProcessor != null ? httpProcessor : H2Processors.server(), new DefaultAsyncResponseExchangeHandlerFactory(registry, exchangeHandlerDecorator != null ? exchangeHandlerDecorator : BasicAsyncServerExpectationDecorator::new), HttpVersionPolicy.FORCE_HTTP_2, h2Config, Http1Config.DEFAULT, 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 3 with HttpProcessor

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

the class H2RequesterBootstrap method create.

public H2AsyncRequester create() {
    final ManagedConnPool<HttpHost, IOSession> connPool;
    switch(poolConcurrencyPolicy != null ? poolConcurrencyPolicy : PoolConcurrencyPolicy.STRICT) {
        case LAX:
            connPool = new LaxConnPool<>(defaultMaxPerRoute > 0 ? defaultMaxPerRoute : 20, timeToLive, poolReusePolicy, new DefaultDisposalCallback<>(), connPoolListener);
            break;
        case STRICT:
        default:
            connPool = new StrictConnPool<>(defaultMaxPerRoute > 0 ? defaultMaxPerRoute : 20, maxTotal > 0 ? maxTotal : 50, timeToLive, poolReusePolicy, new DefaultDisposalCallback<>(), connPoolListener);
            break;
    }
    final RequestHandlerRegistry<Supplier<AsyncPushConsumer>> registry = new RequestHandlerRegistry<>(uriPatternType);
    for (final HandlerEntry<Supplier<AsyncPushConsumer>> entry : pushConsumerList) {
        registry.register(entry.hostname, entry.uriPattern, entry.handler);
    }
    final ClientH2StreamMultiplexerFactory http2StreamHandlerFactory = new ClientH2StreamMultiplexerFactory(httpProcessor != null ? httpProcessor : H2Processors.client(), new DefaultAsyncPushConsumerFactory(registry), h2Config != null ? h2Config : H2Config.DEFAULT, charCodingConfig != null ? charCodingConfig : CharCodingConfig.DEFAULT, streamListener);
    final TlsStrategy actualTlsStrategy = tlsStrategy != null ? tlsStrategy : new H2ClientTlsStrategy();
    final ClientHttp1StreamDuplexerFactory http1StreamHandlerFactory = new ClientHttp1StreamDuplexerFactory(httpProcessor != null ? httpProcessor : HttpProcessors.client(), http1Config != null ? http1Config : Http1Config.DEFAULT, charCodingConfig != null ? charCodingConfig : CharCodingConfig.DEFAULT, DefaultConnectionReuseStrategy.INSTANCE, new DefaultHttpResponseParserFactory(http1Config), DefaultHttpRequestWriterFactory.INSTANCE, DefaultContentLengthStrategy.INSTANCE, DefaultContentLengthStrategy.INSTANCE, http1StreamListener);
    final IOEventHandlerFactory ioEventHandlerFactory = new ClientHttpProtocolNegotiationStarter(http1StreamHandlerFactory, http2StreamHandlerFactory, versionPolicy != null ? versionPolicy : HttpVersionPolicy.NEGOTIATE, actualTlsStrategy, handshakeTimeout);
    return new H2AsyncRequester(versionPolicy != null ? versionPolicy : HttpVersionPolicy.NEGOTIATE, ioReactorConfig, ioEventHandlerFactory, ioSessionDecorator, exceptionCallback, sessionListener, connPool, actualTlsStrategy, handshakeTimeout);
}
Also used : H2ClientTlsStrategy(org.apache.hc.core5.http2.ssl.H2ClientTlsStrategy) TlsStrategy(org.apache.hc.core5.http.nio.ssl.TlsStrategy) DefaultDisposalCallback(org.apache.hc.core5.pool.DefaultDisposalCallback) ClientH2StreamMultiplexerFactory(org.apache.hc.core5.http2.impl.nio.ClientH2StreamMultiplexerFactory) ClientHttpProtocolNegotiationStarter(org.apache.hc.core5.http2.impl.nio.ClientHttpProtocolNegotiationStarter) RequestHandlerRegistry(org.apache.hc.core5.http.protocol.RequestHandlerRegistry) DefaultAsyncPushConsumerFactory(org.apache.hc.core5.http2.nio.support.DefaultAsyncPushConsumerFactory) DefaultHttpResponseParserFactory(org.apache.hc.core5.http.impl.nio.DefaultHttpResponseParserFactory) ClientHttp1StreamDuplexerFactory(org.apache.hc.core5.http.impl.nio.ClientHttp1StreamDuplexerFactory) HttpHost(org.apache.hc.core5.http.HttpHost) H2ClientTlsStrategy(org.apache.hc.core5.http2.ssl.H2ClientTlsStrategy) IOSession(org.apache.hc.core5.reactor.IOSession) Supplier(org.apache.hc.core5.function.Supplier) IOEventHandlerFactory(org.apache.hc.core5.reactor.IOEventHandlerFactory)

Example 4 with HttpProcessor

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

the class H2ServerBootstrap method create.

public HttpAsyncServer create() {
    final String actualCanonicalHostName = canonicalHostName != null ? canonicalHostName : InetAddressUtils.getCanonicalLocalHostName();
    final RequestHandlerRegistry<Supplier<AsyncServerExchangeHandler>> registry = new RequestHandlerRegistry<>(actualCanonicalHostName, () -> lookupRegistry != null ? lookupRegistry : UriPatternType.newMatcher(UriPatternType.URI_PATTERN));
    for (final HandlerEntry<Supplier<AsyncServerExchangeHandler>> entry : handlerList) {
        registry.register(entry.hostname, entry.uriPattern, entry.handler);
    }
    final HandlerFactory<AsyncServerExchangeHandler> handlerFactory;
    if (!filters.isEmpty()) {
        final NamedElementChain<AsyncFilterHandler> filterChainDefinition = new NamedElementChain<>();
        filterChainDefinition.addLast(new TerminalAsyncServerFilter(new DefaultAsyncResponseExchangeHandlerFactory(registry)), StandardFilter.MAIN_HANDLER.name());
        filterChainDefinition.addFirst(new AsyncServerExpectationFilter(), StandardFilter.EXPECT_CONTINUE.name());
        for (final FilterEntry<AsyncFilterHandler> entry : filters) {
            switch(entry.position) {
                case AFTER:
                    filterChainDefinition.addAfter(entry.existing, entry.filterHandler, entry.name);
                    break;
                case BEFORE:
                    filterChainDefinition.addBefore(entry.existing, entry.filterHandler, entry.name);
                    break;
                case REPLACE:
                    filterChainDefinition.replace(entry.existing, entry.filterHandler);
                    break;
                case FIRST:
                    filterChainDefinition.addFirst(entry.filterHandler, entry.name);
                    break;
                case LAST:
                    // Don't add last, after TerminalAsyncServerFilter, as that does not delegate to the chain
                    // Instead, add the filter just before it, making it effectively the last filter
                    filterChainDefinition.addBefore(StandardFilter.MAIN_HANDLER.name(), entry.filterHandler, entry.name);
                    break;
            }
        }
        NamedElementChain<AsyncFilterHandler>.Node current = filterChainDefinition.getLast();
        AsyncServerFilterChainElement execChain = null;
        while (current != null) {
            execChain = new AsyncServerFilterChainElement(current.getValue(), execChain);
            current = current.getPrevious();
        }
        handlerFactory = new AsyncServerFilterChainExchangeHandlerFactory(execChain, exceptionCallback);
    } else {
        handlerFactory = new DefaultAsyncResponseExchangeHandlerFactory(registry, handler -> new BasicAsyncServerExpectationDecorator(handler, exceptionCallback));
    }
    final ServerH2StreamMultiplexerFactory http2StreamHandlerFactory = new ServerH2StreamMultiplexerFactory(httpProcessor != null ? httpProcessor : H2Processors.server(), handlerFactory, h2Config != null ? h2Config : H2Config.DEFAULT, charCodingConfig != null ? charCodingConfig : CharCodingConfig.DEFAULT, h2StreamListener);
    final TlsStrategy actualTlsStrategy = tlsStrategy != null ? tlsStrategy : new H2ServerTlsStrategy();
    final ServerHttp1StreamDuplexerFactory http1StreamHandlerFactory = new ServerHttp1StreamDuplexerFactory(httpProcessor != null ? httpProcessor : HttpProcessors.server(), handlerFactory, http1Config != null ? http1Config : Http1Config.DEFAULT, charCodingConfig != null ? charCodingConfig : CharCodingConfig.DEFAULT, DefaultConnectionReuseStrategy.INSTANCE, DefaultHttpRequestParserFactory.INSTANCE, DefaultHttpResponseWriterFactory.INSTANCE, DefaultContentLengthStrategy.INSTANCE, DefaultContentLengthStrategy.INSTANCE, http1StreamListener);
    final IOEventHandlerFactory ioEventHandlerFactory = new ServerHttpProtocolNegotiationStarter(http1StreamHandlerFactory, http2StreamHandlerFactory, versionPolicy != null ? versionPolicy : HttpVersionPolicy.NEGOTIATE, actualTlsStrategy, handshakeTimeout);
    return new HttpAsyncServer(ioEventHandlerFactory, ioReactorConfig, ioSessionDecorator, exceptionCallback, sessionListener, actualCanonicalHostName);
}
Also used : AsyncServerFilterChainElement(org.apache.hc.core5.http.nio.support.AsyncServerFilterChainElement) HttpAsyncServer(org.apache.hc.core5.http.impl.bootstrap.HttpAsyncServer) AsyncServerFilterChainExchangeHandlerFactory(org.apache.hc.core5.http.nio.support.AsyncServerFilterChainExchangeHandlerFactory) RequestHandlerRegistry(org.apache.hc.core5.http.protocol.RequestHandlerRegistry) Args(org.apache.hc.core5.util.Args) H2Config(org.apache.hc.core5.http2.config.H2Config) IOReactorConfig(org.apache.hc.core5.reactor.IOReactorConfig) CharCodingConfig(org.apache.hc.core5.http.config.CharCodingConfig) AsyncServerRequestHandler(org.apache.hc.core5.http.nio.AsyncServerRequestHandler) AsyncServerFilterChainElement(org.apache.hc.core5.http.nio.support.AsyncServerFilterChainElement) BasicAsyncServerExpectationDecorator(org.apache.hc.core5.http.nio.support.BasicAsyncServerExpectationDecorator) IOSessionListener(org.apache.hc.core5.reactor.IOSessionListener) Supplier(org.apache.hc.core5.function.Supplier) ArrayList(java.util.ArrayList) HttpVersionPolicy(org.apache.hc.core5.http2.HttpVersionPolicy) HttpProcessor(org.apache.hc.core5.http.protocol.HttpProcessor) HttpProcessors(org.apache.hc.core5.http.impl.HttpProcessors) DefaultHttpResponseWriterFactory(org.apache.hc.core5.http.impl.nio.DefaultHttpResponseWriterFactory) LookupRegistry(org.apache.hc.core5.http.protocol.LookupRegistry) Http1Config(org.apache.hc.core5.http.config.Http1Config) NamedElementChain(org.apache.hc.core5.http.config.NamedElementChain) Http1StreamListener(org.apache.hc.core5.http.impl.Http1StreamListener) AsyncServerExchangeHandler(org.apache.hc.core5.http.nio.AsyncServerExchangeHandler) AsyncFilterHandler(org.apache.hc.core5.http.nio.AsyncFilterHandler) ServerHttpProtocolNegotiationStarter(org.apache.hc.core5.http2.impl.nio.ServerHttpProtocolNegotiationStarter) BasicServerExchangeHandler(org.apache.hc.core5.http.nio.support.BasicServerExchangeHandler) Decorator(org.apache.hc.core5.function.Decorator) HandlerFactory(org.apache.hc.core5.http.nio.HandlerFactory) Callback(org.apache.hc.core5.function.Callback) Timeout(org.apache.hc.core5.util.Timeout) H2Processors(org.apache.hc.core5.http2.impl.H2Processors) DefaultContentLengthStrategy(org.apache.hc.core5.http.impl.DefaultContentLengthStrategy) UriPatternType(org.apache.hc.core5.http.protocol.UriPatternType) H2ServerTlsStrategy(org.apache.hc.core5.http2.ssl.H2ServerTlsStrategy) IOEventHandlerFactory(org.apache.hc.core5.reactor.IOEventHandlerFactory) List(java.util.List) ServerH2StreamMultiplexerFactory(org.apache.hc.core5.http2.impl.nio.ServerH2StreamMultiplexerFactory) H2StreamListener(org.apache.hc.core5.http2.impl.nio.H2StreamListener) DefaultHttpRequestParserFactory(org.apache.hc.core5.http.impl.nio.DefaultHttpRequestParserFactory) IOSession(org.apache.hc.core5.reactor.IOSession) TerminalAsyncServerFilter(org.apache.hc.core5.http.nio.support.TerminalAsyncServerFilter) InetAddressUtils(org.apache.hc.core5.net.InetAddressUtils) ServerHttp1StreamDuplexerFactory(org.apache.hc.core5.http.impl.nio.ServerHttp1StreamDuplexerFactory) TlsStrategy(org.apache.hc.core5.http.nio.ssl.TlsStrategy) AsyncServerExpectationFilter(org.apache.hc.core5.http.nio.support.AsyncServerExpectationFilter) DefaultAsyncResponseExchangeHandlerFactory(org.apache.hc.core5.http.nio.support.DefaultAsyncResponseExchangeHandlerFactory) DefaultConnectionReuseStrategy(org.apache.hc.core5.http.impl.DefaultConnectionReuseStrategy) StandardFilter(org.apache.hc.core5.http.impl.bootstrap.StandardFilter) H2ServerTlsStrategy(org.apache.hc.core5.http2.ssl.H2ServerTlsStrategy) TlsStrategy(org.apache.hc.core5.http.nio.ssl.TlsStrategy) ServerHttp1StreamDuplexerFactory(org.apache.hc.core5.http.impl.nio.ServerHttp1StreamDuplexerFactory) BasicAsyncServerExpectationDecorator(org.apache.hc.core5.http.nio.support.BasicAsyncServerExpectationDecorator) AsyncServerExpectationFilter(org.apache.hc.core5.http.nio.support.AsyncServerExpectationFilter) DefaultAsyncResponseExchangeHandlerFactory(org.apache.hc.core5.http.nio.support.DefaultAsyncResponseExchangeHandlerFactory) Supplier(org.apache.hc.core5.function.Supplier) ServerH2StreamMultiplexerFactory(org.apache.hc.core5.http2.impl.nio.ServerH2StreamMultiplexerFactory) AsyncServerExchangeHandler(org.apache.hc.core5.http.nio.AsyncServerExchangeHandler) AsyncFilterHandler(org.apache.hc.core5.http.nio.AsyncFilterHandler) TerminalAsyncServerFilter(org.apache.hc.core5.http.nio.support.TerminalAsyncServerFilter) NamedElementChain(org.apache.hc.core5.http.config.NamedElementChain) RequestHandlerRegistry(org.apache.hc.core5.http.protocol.RequestHandlerRegistry) HttpAsyncServer(org.apache.hc.core5.http.impl.bootstrap.HttpAsyncServer) H2ServerTlsStrategy(org.apache.hc.core5.http2.ssl.H2ServerTlsStrategy) ServerHttpProtocolNegotiationStarter(org.apache.hc.core5.http2.impl.nio.ServerHttpProtocolNegotiationStarter) AsyncServerFilterChainExchangeHandlerFactory(org.apache.hc.core5.http.nio.support.AsyncServerFilterChainExchangeHandlerFactory) IOEventHandlerFactory(org.apache.hc.core5.reactor.IOEventHandlerFactory)

Example 5 with HttpProcessor

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

the class AbstractH2StreamMultiplexer method processPendingCommands.

private void processPendingCommands() throws IOException, HttpException {
    while (streamMap.size() < remoteConfig.getMaxConcurrentStreams()) {
        final Command command = ioSession.poll();
        if (command == null) {
            break;
        }
        if (command instanceof ShutdownCommand) {
            final ShutdownCommand shutdownCommand = (ShutdownCommand) command;
            if (shutdownCommand.getType() == CloseMode.IMMEDIATE) {
                for (final Iterator<Map.Entry<Integer, H2Stream>> it = streamMap.entrySet().iterator(); it.hasNext(); ) {
                    final Map.Entry<Integer, H2Stream> entry = it.next();
                    final H2Stream stream = entry.getValue();
                    stream.cancel();
                }
                streamMap.clear();
                connState = ConnectionHandshake.SHUTDOWN;
            } else {
                if (connState.compareTo(ConnectionHandshake.ACTIVE) <= 0) {
                    final RawFrame goAway = frameFactory.createGoAway(processedRemoteStreamId, H2Error.NO_ERROR, "Graceful shutdown");
                    commitFrame(goAway);
                    connState = streamMap.isEmpty() ? ConnectionHandshake.SHUTDOWN : ConnectionHandshake.GRACEFUL_SHUTDOWN;
                }
            }
            break;
        } else if (command instanceof PingCommand) {
            final PingCommand pingCommand = (PingCommand) command;
            final AsyncPingHandler handler = pingCommand.getHandler();
            pingHandlers.add(handler);
            final RawFrame ping = frameFactory.createPing(handler.getData());
            commitFrame(ping);
        } else if (command instanceof ExecutableCommand) {
            final int streamId = generateStreamId();
            final H2StreamChannelImpl channel = new H2StreamChannelImpl(streamId, true, initInputWinSize, initOutputWinSize);
            final ExecutableCommand executableCommand = (ExecutableCommand) command;
            final H2StreamHandler streamHandler = createLocallyInitiatedStream(executableCommand, channel, httpProcessor, connMetrics);
            final H2Stream stream = new H2Stream(channel, streamHandler, false);
            streamMap.put(streamId, stream);
            if (streamListener != null) {
                final int initInputWindow = stream.getInputWindow().get();
                streamListener.onInputFlowControl(this, streamId, initInputWindow, initInputWindow);
                final int initOutputWindow = stream.getOutputWindow().get();
                streamListener.onOutputFlowControl(this, streamId, initOutputWindow, initOutputWindow);
            }
            if (stream.isOutputReady()) {
                stream.produceOutput();
            }
            final CancellableDependency cancellableDependency = executableCommand.getCancellableDependency();
            if (cancellableDependency != null) {
                cancellableDependency.setDependency(stream::abort);
            }
            if (!outputQueue.isEmpty()) {
                return;
            }
        }
    }
}
Also used : ShutdownCommand(org.apache.hc.core5.http.nio.command.ShutdownCommand) PingCommand(org.apache.hc.core5.http2.nio.command.PingCommand) CancellableDependency(org.apache.hc.core5.concurrent.CancellableDependency) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) PingCommand(org.apache.hc.core5.http2.nio.command.PingCommand) ExecutableCommand(org.apache.hc.core5.http.nio.command.ExecutableCommand) Command(org.apache.hc.core5.reactor.Command) ShutdownCommand(org.apache.hc.core5.http.nio.command.ShutdownCommand) AsyncPingHandler(org.apache.hc.core5.http2.nio.AsyncPingHandler) RawFrame(org.apache.hc.core5.http2.frame.RawFrame) ExecutableCommand(org.apache.hc.core5.http.nio.command.ExecutableCommand) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

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