Search in sources :

Example 11 with ProtocolIOSession

use of org.apache.hc.core5.reactor.ProtocolIOSession in project httpcomponents-core by apache.

the class TestAbstractH2StreamMultiplexer method testInputMultipleFrames.

@Test
public void testInputMultipleFrames() throws Exception {
    final WritableByteChannelMock writableChannel = new WritableByteChannelMock(1024);
    final FrameOutputBuffer outbuffer = new FrameOutputBuffer(16 * 1024);
    final byte[] data = new byte[FrameConsts.MIN_FRAME_SIZE];
    for (int i = 0; i < FrameConsts.MIN_FRAME_SIZE; i++) {
        data[i] = (byte) (i % 16);
    }
    final RawFrame frame1 = new RawFrame(FrameType.DATA.getValue(), 0, 1, ByteBuffer.wrap(data));
    outbuffer.write(frame1, writableChannel);
    final RawFrame frame2 = new RawFrame(FrameType.DATA.getValue(), 0, 1, ByteBuffer.wrap(data));
    outbuffer.write(frame2, writableChannel);
    final byte[] bytes = writableChannel.toByteArray();
    final AbstractH2StreamMultiplexer streamMultiplexer = new H2StreamMultiplexerImpl(protocolIOSession, DefaultFrameFactory.INSTANCE, StreamIdGenerator.ODD, httpProcessor, CharCodingConfig.DEFAULT, H2Config.custom().setMaxFrameSize(FrameConsts.MIN_FRAME_SIZE).build(), h2StreamListener);
    Assertions.assertThrows(H2ConnectionException.class, () -> streamMultiplexer.onInput(ByteBuffer.wrap(bytes)));
    Mockito.verify(h2StreamListener).onFrameInput(Mockito.same(streamMultiplexer), Mockito.eq(1), Mockito.any());
    Assertions.assertThrows(H2ConnectionException.class, () -> {
        int pos = 0;
        int remaining = bytes.length;
        while (remaining > 0) {
            final int chunk = Math.min(4096, remaining);
            streamMultiplexer.onInput(ByteBuffer.wrap(bytes, pos, chunk));
            pos += chunk;
            remaining -= chunk;
        }
        Mockito.verify(h2StreamListener).onFrameInput(Mockito.same(streamMultiplexer), Mockito.eq(1), Mockito.any());
    });
}
Also used : WritableByteChannelMock(org.apache.hc.core5.http2.WritableByteChannelMock) RawFrame(org.apache.hc.core5.http2.frame.RawFrame) Test(org.junit.jupiter.api.Test)

Example 12 with ProtocolIOSession

use of org.apache.hc.core5.reactor.ProtocolIOSession in project httpcomponents-core by apache.

the class ServerHttpProtocolNegotiationStarter method createHandler.

@Override
public HttpConnectionEventHandler createHandler(final ProtocolIOSession ioSession, final Object attachment) {
    HttpVersionPolicy endpointPolicy = versionPolicy;
    URIScheme uriScheme = URIScheme.HTTP;
    if (attachment instanceof EndpointParameters) {
        final EndpointParameters params = (EndpointParameters) attachment;
        if (tlsStrategy != null && URIScheme.HTTPS.same(params.getScheme())) {
            uriScheme = URIScheme.HTTPS;
            tlsStrategy.upgrade(ioSession, params, params.getAttachment(), handshakeTimeout, null);
        }
        if (params.getAttachment() instanceof HttpVersionPolicy) {
            endpointPolicy = (HttpVersionPolicy) params.getAttachment();
        }
    }
    ioSession.registerProtocol(ApplicationProtocol.HTTP_1_1.id, new ServerHttp1UpgradeHandler(http1StreamHandlerFactory));
    ioSession.registerProtocol(ApplicationProtocol.HTTP_2.id, new ServerH2UpgradeHandler(http2StreamHandlerFactory));
    switch(endpointPolicy) {
        case FORCE_HTTP_2:
            return new ServerH2PrefaceHandler(ioSession, http2StreamHandlerFactory);
        case FORCE_HTTP_1:
            return new ServerHttp1IOEventHandler(http1StreamHandlerFactory.create(uriScheme.id, ioSession));
        default:
            return new HttpProtocolNegotiator(ioSession, null);
    }
}
Also used : HttpVersionPolicy(org.apache.hc.core5.http2.HttpVersionPolicy) URIScheme(org.apache.hc.core5.http.URIScheme) ServerHttp1IOEventHandler(org.apache.hc.core5.http.impl.nio.ServerHttp1IOEventHandler) EndpointParameters(org.apache.hc.core5.reactor.EndpointParameters)

Example 13 with ProtocolIOSession

use of org.apache.hc.core5.reactor.ProtocolIOSession in project httpcomponents-core by apache.

the class ClientH2UpgradeHandler method upgrade.

@Override
public void upgrade(final ProtocolIOSession ioSession, final FutureCallback<ProtocolIOSession> callback) {
    final HttpConnectionEventHandler protocolNegotiator = new ClientH2PrefaceHandler(ioSession, http2StreamHandlerFactory, true, callback);
    ioSession.upgrade(protocolNegotiator);
    try {
        protocolNegotiator.connected(ioSession);
    } catch (final IOException ex) {
        protocolNegotiator.exception(ioSession, ex);
    }
}
Also used : HttpConnectionEventHandler(org.apache.hc.core5.http.impl.nio.HttpConnectionEventHandler) IOException(java.io.IOException)

Aggregations

IOException (java.io.IOException)5 ClientHttp1IOEventHandler (org.apache.hc.core5.http.impl.nio.ClientHttp1IOEventHandler)3 EndpointParameters (org.apache.hc.core5.reactor.EndpointParameters)3 List (java.util.List)2 Header (org.apache.hc.core5.http.Header)2 HttpException (org.apache.hc.core5.http.HttpException)2 HttpHost (org.apache.hc.core5.http.HttpHost)2 HttpRequest (org.apache.hc.core5.http.HttpRequest)2 HttpResponse (org.apache.hc.core5.http.HttpResponse)2 Message (org.apache.hc.core5.http.Message)2 Http1StreamListener (org.apache.hc.core5.http.impl.Http1StreamListener)2 ServerHttp1IOEventHandler (org.apache.hc.core5.http.impl.nio.ServerHttp1IOEventHandler)2 BasicHttpRequest (org.apache.hc.core5.http.message.BasicHttpRequest)2 StringAsyncEntityConsumer (org.apache.hc.core5.http.nio.entity.StringAsyncEntityConsumer)2 BasicRequestProducer (org.apache.hc.core5.http.nio.support.BasicRequestProducer)2 WritableByteChannelMock (org.apache.hc.core5.http2.WritableByteChannelMock)2 RawFrame (org.apache.hc.core5.http2.frame.RawFrame)2 HttpProtocolNegotiator (org.apache.hc.core5.http2.impl.nio.HttpProtocolNegotiator)2 ProtocolIOSession (org.apache.hc.core5.reactor.ProtocolIOSession)2 Test (org.junit.jupiter.api.Test)2