use of io.netty.handler.codec.http2.Http2Settings in project vert.x by eclipse.
the class HttpUtils method decodeSettings.
static Http2Settings decodeSettings(String base64Settings) {
try {
Http2Settings settings = new Http2Settings();
Buffer buffer = Buffer.buffer(Base64.getUrlDecoder().decode(base64Settings));
int pos = 0;
int len = buffer.length();
while (pos < len) {
int i = buffer.getUnsignedShort(pos);
pos += 2;
long j = buffer.getUnsignedInt(pos);
pos += 4;
settings.put((char) i, (Long) j);
}
return settings;
} catch (Exception ignore) {
}
return null;
}
use of io.netty.handler.codec.http2.Http2Settings in project vert.x by eclipse.
the class Http2ConnectionBase method updateSettings.
protected synchronized void updateSettings(Http2Settings settingsUpdate, Handler<AsyncResult<Void>> completionHandler) {
Context completionContext = completionHandler != null ? context.owner().getOrCreateContext() : null;
Http2Settings current = handler.decoder().localSettings();
for (Map.Entry<Character, Long> entry : current.entrySet()) {
Character key = entry.getKey();
if (Objects.equals(settingsUpdate.get(key), entry.getValue())) {
settingsUpdate.remove(key);
}
}
handler.writeSettings(settingsUpdate).addListener(fut -> {
if (fut.isSuccess()) {
synchronized (Http2ConnectionBase.this) {
updateSettingsHandlers.add(() -> {
serverSettings.putAll(settingsUpdate);
if (completionHandler != null) {
completionContext.runOnContext(v -> {
completionHandler.handle(Future.succeededFuture());
});
}
});
}
} else {
if (completionHandler != null) {
completionContext.runOnContext(v -> {
completionHandler.handle(Future.failedFuture(fut.cause()));
});
}
}
});
}
use of io.netty.handler.codec.http2.Http2Settings in project vert.x by eclipse.
the class Http2ServerTest method testServerSettings.
@Test
public void testServerSettings() throws Exception {
waitFor(2);
io.vertx.core.http.Http2Settings expectedSettings = TestUtils.randomHttp2Settings();
expectedSettings.setHeaderTableSize((int) io.vertx.core.http.Http2Settings.DEFAULT_HEADER_TABLE_SIZE);
server.close();
server = vertx.createHttpServer(serverOptions);
Context otherContext = vertx.getOrCreateContext();
server.connectionHandler(conn -> {
otherContext.runOnContext(v -> {
conn.updateSettings(expectedSettings, ar -> {
assertSame(otherContext, Vertx.currentContext());
io.vertx.core.http.Http2Settings ackedSettings = conn.settings();
assertEquals(expectedSettings.getMaxHeaderListSize(), ackedSettings.getMaxHeaderListSize());
assertEquals(expectedSettings.getMaxFrameSize(), ackedSettings.getMaxFrameSize());
assertEquals(expectedSettings.getInitialWindowSize(), ackedSettings.getInitialWindowSize());
assertEquals(expectedSettings.getMaxConcurrentStreams(), ackedSettings.getMaxConcurrentStreams());
assertEquals(expectedSettings.getHeaderTableSize(), ackedSettings.getHeaderTableSize());
assertEquals(expectedSettings.get(''), ackedSettings.get(7));
complete();
});
});
});
server.requestHandler(req -> {
fail();
});
startServer();
TestClient client = new TestClient();
ChannelFuture fut = client.connect(DEFAULT_HTTPS_PORT, DEFAULT_HTTPS_HOST, request -> {
request.decoder.frameListener(new Http2FrameAdapter() {
AtomicInteger count = new AtomicInteger();
@Override
public void onSettingsRead(ChannelHandlerContext ctx, Http2Settings newSettings) throws Http2Exception {
vertx.runOnContext(v -> {
switch(count.getAndIncrement()) {
case 0:
break;
case 1:
assertEquals((Long) expectedSettings.getMaxHeaderListSize(), newSettings.maxHeaderListSize());
assertEquals((Integer) expectedSettings.getMaxFrameSize(), newSettings.maxFrameSize());
assertEquals((Integer) expectedSettings.getInitialWindowSize(), newSettings.initialWindowSize());
assertEquals((Long) expectedSettings.getMaxConcurrentStreams(), newSettings.maxConcurrentStreams());
assertEquals(null, newSettings.headerTableSize());
complete();
break;
default:
fail();
}
});
}
});
});
fut.sync();
await();
}
use of io.netty.handler.codec.http2.Http2Settings in project netty by netty.
the class Http2ConnectionRoundtripTest method encodeViolatesMaxHeaderListSizeCanStillUseConnection.
@Test
public void encodeViolatesMaxHeaderListSizeCanStillUseConnection() throws Exception {
bootstrapEnv(1, 2, 1, 0, 0);
final CountDownLatch serverSettingsAckLatch1 = new CountDownLatch(2);
final CountDownLatch serverSettingsAckLatch2 = new CountDownLatch(3);
final CountDownLatch clientSettingsLatch1 = new CountDownLatch(3);
final CountDownLatch serverRevHeadersLatch = new CountDownLatch(1);
final CountDownLatch clientHeadersLatch = new CountDownLatch(1);
final CountDownLatch clientDataWrite = new CountDownLatch(1);
final AtomicReference<Throwable> clientHeadersWriteException = new AtomicReference<Throwable>();
final AtomicReference<Throwable> clientHeadersWriteException2 = new AtomicReference<Throwable>();
final AtomicReference<Throwable> clientDataWriteException = new AtomicReference<Throwable>();
final Http2Headers headers = dummyHeaders();
doAnswer(new Answer<Void>() {
@Override
public Void answer(InvocationOnMock invocationOnMock) throws Throwable {
serverSettingsAckLatch1.countDown();
serverSettingsAckLatch2.countDown();
return null;
}
}).when(serverListener).onSettingsAckRead(any(ChannelHandlerContext.class));
doAnswer(new Answer<Void>() {
@Override
public Void answer(InvocationOnMock invocationOnMock) throws Throwable {
clientSettingsLatch1.countDown();
return null;
}
}).when(clientListener).onSettingsRead(any(ChannelHandlerContext.class), any(Http2Settings.class));
// Manually add a listener for when we receive the expected headers on the server.
doAnswer(new Answer<Void>() {
@Override
public Void answer(InvocationOnMock invocationOnMock) throws Throwable {
serverRevHeadersLatch.countDown();
return null;
}
}).when(serverListener).onHeadersRead(any(ChannelHandlerContext.class), eq(5), eq(headers), anyInt(), anyShort(), anyBoolean(), eq(0), eq(true));
// Set the maxHeaderListSize to 100 so we may be able to write some headers, but not all. We want to verify
// that we don't corrupt state if some can be written but not all.
runInChannel(serverConnectedChannel, new Http2Runnable() {
@Override
public void run() throws Http2Exception {
http2Server.encoder().writeSettings(serverCtx(), new Http2Settings().copyFrom(http2Server.decoder().localSettings()).maxHeaderListSize(100), serverNewPromise());
http2Server.flush(serverCtx());
}
});
assertTrue(serverSettingsAckLatch1.await(DEFAULT_AWAIT_TIMEOUT_SECONDS, SECONDS));
runInChannel(clientChannel, new Http2Runnable() {
@Override
public void run() throws Http2Exception {
http2Client.encoder().writeHeaders(ctx(), 3, headers, 0, false, newPromise()).addListener(new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture future) throws Exception {
clientHeadersWriteException.set(future.cause());
}
});
// It is expected that this write should fail locally and the remote peer will never see this.
http2Client.encoder().writeData(ctx(), 3, Unpooled.buffer(), 0, true, newPromise()).addListener(new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture future) throws Exception {
clientDataWriteException.set(future.cause());
clientDataWrite.countDown();
}
});
http2Client.flush(ctx());
}
});
assertTrue(clientDataWrite.await(DEFAULT_AWAIT_TIMEOUT_SECONDS, SECONDS));
assertNotNull("Header encode should have exceeded maxHeaderListSize!", clientHeadersWriteException.get());
assertNotNull("Data on closed stream should fail!", clientDataWriteException.get());
// Set the maxHeaderListSize to the max value so we can send the headers.
runInChannel(serverConnectedChannel, new Http2Runnable() {
@Override
public void run() throws Http2Exception {
http2Server.encoder().writeSettings(serverCtx(), new Http2Settings().copyFrom(http2Server.decoder().localSettings()).maxHeaderListSize(Http2CodecUtil.MAX_HEADER_LIST_SIZE), serverNewPromise());
http2Server.flush(serverCtx());
}
});
assertTrue(clientSettingsLatch1.await(DEFAULT_AWAIT_TIMEOUT_SECONDS, SECONDS));
assertTrue(serverSettingsAckLatch2.await(DEFAULT_AWAIT_TIMEOUT_SECONDS, SECONDS));
runInChannel(clientChannel, new Http2Runnable() {
@Override
public void run() throws Http2Exception {
http2Client.encoder().writeHeaders(ctx(), 5, headers, 0, true, newPromise()).addListener(new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture future) throws Exception {
clientHeadersWriteException2.set(future.cause());
clientHeadersLatch.countDown();
}
});
http2Client.flush(ctx());
}
});
assertTrue(clientHeadersLatch.await(DEFAULT_AWAIT_TIMEOUT_SECONDS, SECONDS));
assertNull("Client write of headers should succeed with increased header list size!", clientHeadersWriteException2.get());
assertTrue(serverRevHeadersLatch.await(DEFAULT_AWAIT_TIMEOUT_SECONDS, SECONDS));
verify(serverListener, never()).onDataRead(any(ChannelHandlerContext.class), anyInt(), any(ByteBuf.class), anyInt(), anyBoolean());
// Verify that no errors have been received.
verify(serverListener, never()).onGoAwayRead(any(ChannelHandlerContext.class), anyInt(), anyLong(), any(ByteBuf.class));
verify(serverListener, never()).onRstStreamRead(any(ChannelHandlerContext.class), anyInt(), anyLong());
verify(clientListener, never()).onGoAwayRead(any(ChannelHandlerContext.class), anyInt(), anyLong(), any(ByteBuf.class));
verify(clientListener, never()).onRstStreamRead(any(ChannelHandlerContext.class), anyInt(), anyLong());
}
use of io.netty.handler.codec.http2.Http2Settings in project netty by netty.
the class InboundHttp2ToHttpAdapterTest method propagateSettings.
@Test
public void propagateSettings() throws Exception {
boostrapEnv(1, 1, 2);
final Http2Settings settings = new Http2Settings().pushEnabled(true);
runInChannel(clientChannel, new Http2Runnable() {
@Override
public void run() {
clientHandler.encoder().writeSettings(ctxClient(), settings, newPromiseClient());
clientChannel.flush();
}
});
assertTrue(settingsLatch.await(5, SECONDS));
ArgumentCaptor<Http2Settings> settingsCaptor = ArgumentCaptor.forClass(Http2Settings.class);
verify(settingsListener, times(2)).messageReceived(settingsCaptor.capture());
assertEquals(settings, settingsCaptor.getValue());
}
Aggregations