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"));
}
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);
}
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);
}
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();
}
}
}
Aggregations