Search in sources :

Example 1 with ProtocolNegotiationException

use of org.apache.hc.core5.http2.impl.nio.ProtocolNegotiationException in project httpcomponents-core by apache.

the class H2AlpnTest method testALPN.

@Test
public void testALPN() throws Exception {
    server.start();
    final Future<ListenerEndpoint> future = server.listen(new InetSocketAddress(0), URIScheme.HTTPS);
    final ListenerEndpoint listener = future.get();
    final InetSocketAddress address = (InetSocketAddress) listener.getAddress();
    requester.start();
    final HttpHost target = new HttpHost(URIScheme.HTTPS.id, "localhost", address.getPort());
    final Future<Message<HttpResponse, String>> resultFuture1 = requester.execute(new BasicRequestProducer(Method.POST, target, "/stuff", new StringAsyncEntityProducer("some stuff", ContentType.TEXT_PLAIN)), new BasicResponseConsumer<>(new StringAsyncEntityConsumer()), TIMEOUT, null);
    final Message<HttpResponse, String> message1;
    try {
        message1 = resultFuture1.get(TIMEOUT.getDuration(), TIMEOUT.getTimeUnit());
    } catch (final ExecutionException e) {
        final Throwable cause = e.getCause();
        assertFalse(h2Allowed, "h2 negotiation was enabled, but h2 was not negotiated");
        assertTrue(cause instanceof ProtocolNegotiationException);
        assertEquals("ALPN: missing application protocol", cause.getMessage());
        assertTrue(strictALPN, "strict ALPN mode was not enabled, but the client negotiator still threw");
        return;
    }
    assertTrue(h2Allowed, "h2 negotiation was disabled, but h2 was negotiated");
    assertThat(message1, CoreMatchers.notNullValue());
    final HttpResponse response1 = message1.getHead();
    assertThat(response1.getCode(), CoreMatchers.equalTo(HttpStatus.SC_OK));
    final String body1 = message1.getBody();
    assertThat(body1, CoreMatchers.equalTo("some stuff"));
}
Also used : StringAsyncEntityConsumer(org.apache.hc.core5.http.nio.entity.StringAsyncEntityConsumer) Message(org.apache.hc.core5.http.Message) InetSocketAddress(java.net.InetSocketAddress) BasicRequestProducer(org.apache.hc.core5.http.nio.support.BasicRequestProducer) StringAsyncEntityProducer(org.apache.hc.core5.http.nio.entity.StringAsyncEntityProducer) HttpResponse(org.apache.hc.core5.http.HttpResponse) ListenerEndpoint(org.apache.hc.core5.reactor.ListenerEndpoint) HttpHost(org.apache.hc.core5.http.HttpHost) ProtocolNegotiationException(org.apache.hc.core5.http2.impl.nio.ProtocolNegotiationException) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test)

Example 2 with ProtocolNegotiationException

use of org.apache.hc.core5.http2.impl.nio.ProtocolNegotiationException in project httpcomponents-core by apache.

the class ClientH2PrefaceHandler method initialize.

private void initialize() throws IOException {
    final TlsDetails tlsDetails = ioSession.getTlsDetails();
    if (tlsDetails != null) {
        final String applicationProtocol = tlsDetails.getApplicationProtocol();
        if (TextUtils.isEmpty(applicationProtocol)) {
            if (strictALPNHandshake) {
                throw new ProtocolNegotiationException("ALPN: missing application protocol");
            }
        } else {
            if (!ApplicationProtocol.HTTP_2.id.equals(applicationProtocol)) {
                throw new ProtocolNegotiationException("ALPN: unexpected application protocol '" + applicationProtocol + "'");
            }
        }
    }
    this.preface = ByteBuffer.wrap(PREFACE);
    ioSession.setEvent(SelectionKey.OP_WRITE);
}
Also used : TlsDetails(org.apache.hc.core5.reactor.ssl.TlsDetails)

Example 3 with ProtocolNegotiationException

use of org.apache.hc.core5.http2.impl.nio.ProtocolNegotiationException in project httpcomponents-core by apache.

the class HttpProtocolNegotiator method connected.

@Override
public void connected(final IOSession session) throws IOException {
    final HttpVersion httpVersion;
    final TlsDetails tlsDetails = ioSession.getTlsDetails();
    if (tlsDetails != null) {
        final String appProtocol = tlsDetails.getApplicationProtocol();
        if (TextUtils.isEmpty(appProtocol)) {
            httpVersion = HttpVersion.HTTP_1_1;
        } else if (appProtocol.equals(ApplicationProtocol.HTTP_1_1.id)) {
            httpVersion = HttpVersion.HTTP_1_1;
        } else if (appProtocol.equals(ApplicationProtocol.HTTP_2.id)) {
            httpVersion = HttpVersion.HTTP_2;
        } else {
            throw new ProtocolNegotiationException("Unsupported application protocol: " + appProtocol);
        }
    } else {
        httpVersion = HttpVersion.HTTP_1_1;
    }
    startProtocol(httpVersion);
}
Also used : TlsDetails(org.apache.hc.core5.reactor.ssl.TlsDetails) HttpVersion(org.apache.hc.core5.http.HttpVersion)

Example 4 with ProtocolNegotiationException

use of org.apache.hc.core5.http2.impl.nio.ProtocolNegotiationException in project httpcomponents-core by apache.

the class ServerH2PrefaceHandler method inputReady.

@Override
public void inputReady(final IOSession session, final ByteBuffer src) throws IOException {
    if (src != null) {
        inBuf.put(src);
    }
    boolean endOfStream = false;
    if (inBuf.length() < PREFACE.length) {
        final int bytesRead = inBuf.readFrom(session);
        if (bytesRead == -1) {
            endOfStream = true;
        }
    }
    final ByteBuffer data = inBuf.data();
    if (data.remaining() >= PREFACE.length) {
        for (int i = 0; i < PREFACE.length; i++) {
            if (data.get() != PREFACE[i]) {
                throw new ProtocolNegotiationException("Unexpected HTTP/2 preface");
            }
        }
        startProtocol(new ServerH2IOEventHandler(http2StreamHandlerFactory.create(ioSession)), data.hasRemaining() ? data : null);
    } else {
        if (endOfStream) {
            throw new ConnectionClosedException();
        }
    }
}
Also used : ConnectionClosedException(org.apache.hc.core5.http.ConnectionClosedException) ByteBuffer(java.nio.ByteBuffer)

Aggregations

TlsDetails (org.apache.hc.core5.reactor.ssl.TlsDetails)2 InetSocketAddress (java.net.InetSocketAddress)1 ByteBuffer (java.nio.ByteBuffer)1 ExecutionException (java.util.concurrent.ExecutionException)1 ConnectionClosedException (org.apache.hc.core5.http.ConnectionClosedException)1 HttpHost (org.apache.hc.core5.http.HttpHost)1 HttpResponse (org.apache.hc.core5.http.HttpResponse)1 HttpVersion (org.apache.hc.core5.http.HttpVersion)1 Message (org.apache.hc.core5.http.Message)1 StringAsyncEntityConsumer (org.apache.hc.core5.http.nio.entity.StringAsyncEntityConsumer)1 StringAsyncEntityProducer (org.apache.hc.core5.http.nio.entity.StringAsyncEntityProducer)1 BasicRequestProducer (org.apache.hc.core5.http.nio.support.BasicRequestProducer)1 ProtocolNegotiationException (org.apache.hc.core5.http2.impl.nio.ProtocolNegotiationException)1 ListenerEndpoint (org.apache.hc.core5.reactor.ListenerEndpoint)1 Test (org.junit.Test)1