List of usage examples for io.netty.channel ChannelHandlerContext flush
@Override ChannelHandlerContext flush();
From source file:io.vertx.core.http.Http2ClientTest.java
License:Open Source License
@Test public void testGet() throws Exception { ServerBootstrap bootstrap = createH2Server((decoder, encoder) -> new Http2EventAdapter() { @Override// ww w. ja va 2s . c om public void onHeadersRead(ChannelHandlerContext ctx, int streamId, Http2Headers headers, int streamDependency, short weight, boolean exclusive, int padding, boolean endStream) throws Http2Exception { vertx.runOnContext(v -> { assertTrue(endStream); encoder.writeHeaders(ctx, streamId, new DefaultHttp2Headers().status("200"), 0, true, ctx.newPromise()); ctx.flush(); }); } @Override public void onGoAwayRead(ChannelHandlerContext ctx, int lastStreamId, long errorCode, ByteBuf debugData) throws Http2Exception { vertx.runOnContext(v -> { testComplete(); }); } }); ChannelFuture s = bootstrap.bind(DEFAULT_HTTPS_HOST, DEFAULT_HTTPS_PORT).sync(); try { client.getNow(DEFAULT_HTTPS_PORT, DEFAULT_HTTPS_HOST, "/somepath", onSuccess(resp -> { Context ctx = vertx.getOrCreateContext(); assertOnIOContext(ctx); resp.endHandler(v -> { assertOnIOContext(ctx); resp.request().connection().close(); }); })); await(); } finally { s.channel().close().sync(); } }
From source file:io.vertx.core.http.Http2ClientTest.java
License:Open Source License
@Test public void testStreamError() throws Exception { waitFor(3);/* w ww . j ava 2 s . co m*/ ServerBootstrap bootstrap = createH2Server((dec, enc) -> new Http2EventAdapter() { @Override public void onHeadersRead(ChannelHandlerContext ctx, int streamId, Http2Headers headers, int streamDependency, short weight, boolean exclusive, int padding, boolean endStream) throws Http2Exception { enc.writeHeaders(ctx, streamId, new DefaultHttp2Headers().status("200"), 0, false, ctx.newPromise()); // Send a corrupted frame on purpose to check we get the corresponding error in the request exception handler // the error is : greater padding value 0c -> 1F // ChannelFuture a = encoder.frameWriter().writeData(request.context, id, Buffer.buffer("hello").getByteBuf(), 12, false, request.context.newPromise()); // normal frame : 00 00 12 00 08 00 00 00 03 0c 68 65 6c 6c 6f 00 00 00 00 00 00 00 00 00 00 00 00 // corrupted frame : 00 00 12 00 08 00 00 00 03 1F 68 65 6c 6c 6f 00 00 00 00 00 00 00 00 00 00 00 00 ctx.channel() .write(Buffer.buffer(new byte[] { 0x00, 0x00, 0x12, 0x00, 0x08, 0x00, 0x00, 0x00, (byte) (streamId & 0xFF), 0x1F, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }).getByteBuf()); ctx.flush(); } }); ChannelFuture s = bootstrap.bind(DEFAULT_HTTPS_HOST, DEFAULT_HTTPS_PORT).sync(); try { Context ctx = vertx.getOrCreateContext(); ctx.runOnContext(v -> { client.get(DEFAULT_HTTPS_PORT, DEFAULT_HTTPS_HOST, "/somepath", onSuccess(resp -> { resp.exceptionHandler(err -> { assertOnIOContext(ctx); if (err instanceof Http2Exception) { complete(); } }); })).connectionHandler(conn -> { conn.exceptionHandler(err -> { assertOnIOContext(ctx); if (err instanceof Http2Exception) { complete(); } }); }).exceptionHandler(err -> { assertOnIOContext(ctx); if (err instanceof Http2Exception) { complete(); } }).sendHead(); }); await(); } finally { s.channel().close().sync(); } }
From source file:io.vertx.core.http.Http2ClientTest.java
License:Open Source License
@Test public void testConnectionDecodeError() throws Exception { waitFor(3);/* ww w . ja v a2s . co m*/ ServerBootstrap bootstrap = createH2Server((dec, enc) -> new Http2EventAdapter() { @Override public void onHeadersRead(ChannelHandlerContext ctx, int streamId, Http2Headers headers, int streamDependency, short weight, boolean exclusive, int padding, boolean endStream) throws Http2Exception { enc.writeHeaders(ctx, streamId, new DefaultHttp2Headers().status("200"), 0, false, ctx.newPromise()); enc.frameWriter().writeRstStream(ctx, 10, 0, ctx.newPromise()); ctx.flush(); } }); ChannelFuture s = bootstrap.bind(DEFAULT_HTTPS_HOST, DEFAULT_HTTPS_PORT).sync(); try { Context ctx = vertx.getOrCreateContext(); ctx.runOnContext(v -> { client.get(DEFAULT_HTTPS_PORT, DEFAULT_HTTPS_HOST, "/somepath", onSuccess(resp -> { resp.exceptionHandler(err -> { assertOnIOContext(ctx); if (err instanceof Http2Exception) { complete(); } }); })).connectionHandler(conn -> { conn.exceptionHandler(err -> { assertSame(ctx, Vertx.currentContext()); if (err instanceof Http2Exception) { complete(); } }); }).exceptionHandler(err -> { assertOnIOContext(ctx); if (err instanceof Http2Exception) { complete(); } }).sendHead(); }); await(); } finally { s.channel().close().sync(); } }
From source file:io.vertx.core.http.Http2ClientTest.java
License:Open Source License
@Test public void testInvalidServerResponse() throws Exception { ServerBootstrap bootstrap = createH2Server((dec, enc) -> new Http2EventAdapter() { @Override/* w ww . ja v a2 s. c o m*/ public void onHeadersRead(ChannelHandlerContext ctx, int streamId, Http2Headers headers, int streamDependency, short weight, boolean exclusive, int padding, boolean endStream) throws Http2Exception { enc.writeHeaders(ctx, streamId, new DefaultHttp2Headers().status("xyz"), 0, false, ctx.newPromise()); ctx.flush(); } }); ChannelFuture s = bootstrap.bind(DEFAULT_HTTPS_HOST, DEFAULT_HTTPS_PORT).sync(); try { Context ctx = vertx.getOrCreateContext(); ctx.runOnContext(v -> { client.get(DEFAULT_HTTPS_PORT, DEFAULT_HTTPS_HOST, "/somepath", onFailure(err -> { assertOnIOContext(ctx); if (err instanceof NumberFormatException) { testComplete(); } })).connectionHandler(conn -> conn.exceptionHandler(err -> fail())).end(); }); await(); } finally { s.channel().close().sync(); } }
From source file:io.vertx.core.http.Http2ClientTest.java
License:Open Source License
private void testClearText(boolean upgrade) throws Exception { ServerBootstrap bootstrap = createH2CServer((dec, enc) -> new Http2EventAdapter() { @Override/*ww w . ja va 2s. c om*/ public void onHeadersRead(ChannelHandlerContext ctx, int streamId, Http2Headers headers, int streamDependency, short weight, boolean exclusive, int padding, boolean endStream) throws Http2Exception { enc.writeHeaders(ctx, streamId, new DefaultHttp2Headers().status("200"), 0, true, ctx.newPromise()); ctx.flush(); } }, upgrade); ChannelFuture s = bootstrap.bind(DEFAULT_HTTP_HOST, DEFAULT_HTTP_PORT).sync(); try { client.close(); client = vertx.createHttpClient( clientOptions.setUseAlpn(false).setSsl(false).setHttp2ClearTextUpgrade(upgrade)); client.get(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/somepath", onSuccess(resp1 -> { HttpConnection conn = resp1.request().connection(); assertEquals(HttpVersion.HTTP_2, resp1.version()); client.get(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/somepath", onSuccess(resp2 -> { assertSame(((HttpClientConnection) conn).channel(), ((HttpClientConnection) resp2.request().connection()).channel()); testComplete(); })).exceptionHandler(this::fail).end(); })).exceptionHandler(this::fail).end(); await(); } finally { s.channel().close().sync(); } }
From source file:io.vertx.core.http.Http2ClientTest.java
License:Open Source License
@Test public void testStreamPriority() throws Exception { StreamPriority requestStreamPriority = new StreamPriority().setDependency(123).setWeight((short) 45) .setExclusive(true);//from ww w .ja v a 2 s . com StreamPriority responseStreamPriority = new StreamPriority().setDependency(153).setWeight((short) 75) .setExclusive(false); waitFor(2); ServerBootstrap bootstrap = createH2Server((decoder, encoder) -> new Http2EventAdapter() { @Override public void onHeadersRead(ChannelHandlerContext ctx, int streamId, Http2Headers headers, int streamDependency, short weight, boolean exclusive, int padding, boolean endStream) throws Http2Exception { vertx.runOnContext(v -> { assertEquals(requestStreamPriority.getDependency(), streamDependency); assertEquals(requestStreamPriority.getWeight(), weight); assertEquals(requestStreamPriority.isExclusive(), exclusive); encoder.writeHeaders(ctx, streamId, new DefaultHttp2Headers().status("200"), responseStreamPriority.getDependency(), responseStreamPriority.getWeight(), responseStreamPriority.isExclusive(), 0, true, ctx.newPromise()); ctx.flush(); if (endStream) complete(); }); } }); ChannelFuture s = bootstrap.bind(DEFAULT_HTTPS_HOST, DEFAULT_HTTPS_PORT).sync(); try { client.get(DEFAULT_HTTPS_PORT, DEFAULT_HTTPS_HOST, "/somepath", onSuccess(resp -> { assertEquals(responseStreamPriority, resp.request().getStreamPriority()); Context ctx = vertx.getOrCreateContext(); assertOnIOContext(ctx); resp.endHandler(v -> { complete(); }); })).setStreamPriority(requestStreamPriority).end(); await(); } finally { s.channel().close().sync(); } }
From source file:io.vertx.core.http.Http2ClientTest.java
License:Open Source License
@Test public void testStreamPriorityChange() throws Exception { StreamPriority requestStreamPriority = new StreamPriority().setDependency(123).setWeight((short) 45) .setExclusive(true);/*w w w .j ava 2 s . c o m*/ StreamPriority requestStreamPriority2 = new StreamPriority().setDependency(223).setWeight((short) 145) .setExclusive(false); StreamPriority responseStreamPriority = new StreamPriority().setDependency(153).setWeight((short) 75) .setExclusive(false); StreamPriority responseStreamPriority2 = new StreamPriority().setDependency(253).setWeight((short) 175) .setExclusive(true); waitFor(5); ServerBootstrap bootstrap = createH2Server((decoder, encoder) -> new Http2EventAdapter() { @Override public void onHeadersRead(ChannelHandlerContext ctx, int streamId, Http2Headers headers, int streamDependency, short weight, boolean exclusive, int padding, boolean endStream) throws Http2Exception { vertx.runOnContext(v -> { assertEquals(requestStreamPriority.getDependency(), streamDependency); assertEquals(requestStreamPriority.getWeight(), weight); assertEquals(requestStreamPriority.isExclusive(), exclusive); assertFalse(endStream); complete(); }); } @Override public void onPriorityRead(ChannelHandlerContext ctx, int streamId, int streamDependency, short weight, boolean exclusive) throws Http2Exception { vertx.runOnContext(v -> { assertEquals(requestStreamPriority2.getDependency(), streamDependency); assertEquals(requestStreamPriority2.getWeight(), weight); assertEquals(requestStreamPriority2.isExclusive(), exclusive); complete(); }); } @Override public int onDataRead(ChannelHandlerContext ctx, int streamId, ByteBuf data, int padding, boolean endOfStream) throws Http2Exception { if (endOfStream) { encoder.writeHeaders(ctx, streamId, new DefaultHttp2Headers().status("200"), responseStreamPriority.getDependency(), responseStreamPriority.getWeight(), responseStreamPriority.isExclusive(), 0, false, ctx.newPromise()); ctx.flush(); encoder.writePriority(ctx, streamId, responseStreamPriority2.getDependency(), responseStreamPriority2.getWeight(), responseStreamPriority2.isExclusive(), ctx.newPromise()); ctx.flush(); encoder.writeData(ctx, streamId, Buffer.buffer("hello").getByteBuf(), 0, true, ctx.newPromise()); ctx.flush(); vertx.runOnContext(v -> { complete(); }); } return super.onDataRead(ctx, streamId, data, padding, endOfStream); } }); ChannelFuture s = bootstrap.bind(DEFAULT_HTTPS_HOST, DEFAULT_HTTPS_PORT).sync(); try { HttpClientRequest req = client .get(DEFAULT_HTTPS_PORT, DEFAULT_HTTPS_HOST, "/somepath", onSuccess(resp -> { assertEquals(responseStreamPriority, resp.request().getStreamPriority()); Context ctx = vertx.getOrCreateContext(); assertOnIOContext(ctx); resp.streamPriorityHandler(streamPriority -> { assertOnIOContext(ctx); assertEquals(responseStreamPriority2, streamPriority); assertEquals(responseStreamPriority2, resp.request().getStreamPriority()); complete(); }); resp.endHandler(v -> { assertOnIOContext(ctx); assertEquals(responseStreamPriority2, resp.request().getStreamPriority()); complete(); }); })).setStreamPriority(requestStreamPriority); req.sendHead(h -> { req.setStreamPriority(requestStreamPriority2); req.end(); }); await(); } finally { s.channel().close().sync(); } }
From source file:io.vertx.core.http.Http2ClientTest.java
License:Open Source License
@Test public void testStreamPriorityNoChange() throws Exception { StreamPriority requestStreamPriority = new StreamPriority().setDependency(123).setWeight((short) 45) .setExclusive(true);/*from w w w . j a va2s .c o m*/ StreamPriority responseStreamPriority = new StreamPriority().setDependency(153).setWeight((short) 75) .setExclusive(false); waitFor(3); ServerBootstrap bootstrap = createH2Server((decoder, encoder) -> new Http2EventAdapter() { @Override public void onHeadersRead(ChannelHandlerContext ctx, int streamId, Http2Headers headers, int streamDependency, short weight, boolean exclusive, int padding, boolean endStream) throws Http2Exception { vertx.runOnContext(v -> { assertEquals(requestStreamPriority.getDependency(), streamDependency); assertEquals(requestStreamPriority.getWeight(), weight); assertEquals(requestStreamPriority.isExclusive(), exclusive); assertFalse(endStream); complete(); }); encoder.writeHeaders(ctx, streamId, new DefaultHttp2Headers().status("200"), responseStreamPriority.getDependency(), responseStreamPriority.getWeight(), responseStreamPriority.isExclusive(), 0, false, ctx.newPromise()); ctx.flush(); encoder.writePriority(ctx, streamId, responseStreamPriority.getDependency(), responseStreamPriority.getWeight(), responseStreamPriority.isExclusive(), ctx.newPromise()); ctx.flush(); encoder.writeData(ctx, streamId, Buffer.buffer("hello").getByteBuf(), 0, true, ctx.newPromise()); ctx.flush(); } @Override public void onPriorityRead(ChannelHandlerContext ctx, int streamId, int streamDependency, short weight, boolean exclusive) throws Http2Exception { fail("Priority frame shoudl not be sent"); } @Override public int onDataRead(ChannelHandlerContext ctx, int streamId, ByteBuf data, int padding, boolean endOfStream) throws Http2Exception { if (endOfStream) { vertx.runOnContext(v -> { complete(); }); } return super.onDataRead(ctx, streamId, data, padding, endOfStream); } }); ChannelFuture s = bootstrap.bind(DEFAULT_HTTPS_HOST, DEFAULT_HTTPS_PORT).sync(); try { HttpClientRequest req = client .get(DEFAULT_HTTPS_PORT, DEFAULT_HTTPS_HOST, "/somepath", onSuccess(resp -> { assertEquals(responseStreamPriority, resp.request().getStreamPriority()); Context ctx = vertx.getOrCreateContext(); assertOnIOContext(ctx); resp.streamPriorityHandler(streamPriority -> { fail("Stream priority handler shoudl not be called"); }); resp.endHandler(v -> { assertEquals(responseStreamPriority, resp.request().getStreamPriority()); complete(); }); })).setStreamPriority(requestStreamPriority); req.sendHead(h -> { req.setStreamPriority(requestStreamPriority); req.end(); }); await(); } finally { s.channel().close().sync(); } }
From source file:io.vertx.core.http.impl.Http2ServerConnection.java
License:Open Source License
@Override public synchronized void onHeadersRead(ChannelHandlerContext ctx, int streamId, Http2Headers headers, int padding, boolean endOfStream) { VertxHttp2Stream stream = streams.get(streamId); if (stream == null) { if (isMalformedRequest(headers)) { handler.writeReset(streamId, Http2Error.PROTOCOL_ERROR.code()); return; }// ww w . j a va 2s .c o m String contentEncoding = options.isCompressionSupported() ? HttpUtils.determineContentEncoding(headers) : null; Http2Stream s = handler.connection().stream(streamId); boolean writable = handler.encoder().flowController().isWritable(s); Http2ServerRequestImpl req = new Http2ServerRequestImpl(this, s, metrics, serverOrigin, headers, contentEncoding, writable); stream = req; CharSequence value = headers.get(HttpHeaderNames.EXPECT); if (options.isHandle100ContinueAutomatically() && ((value != null && HttpHeaderValues.CONTINUE.equals(value)) || headers.contains(HttpHeaderNames.EXPECT, HttpHeaderValues.CONTINUE))) { req.response().writeContinue(); } streams.put(streamId, req); context.executeFromIO(() -> { Http2ServerResponseImpl resp = req.response(); resp.beginRequest(); requestHandler.handle(req); boolean hasPush = resp.endRequest(); if (hasPush) { ctx.flush(); } }); } else { // Http server request trailer - not implemented yet (in api) } if (endOfStream) { context.executeFromIO(stream::onEnd); } }
From source file:io.vertx.test.core.Http2ClientTest.java
License:Open Source License
@Test public void testGet() throws Exception { ServerBootstrap bootstrap = createH2Server((decoder, encoder) -> new Http2EventAdapter() { @Override/* w ww . j av a2s.com*/ public void onHeadersRead(ChannelHandlerContext ctx, int streamId, Http2Headers headers, int streamDependency, short weight, boolean exclusive, int padding, boolean endStream) throws Http2Exception { vertx.runOnContext(v -> { assertTrue(endStream); encoder.writeHeaders(ctx, streamId, new DefaultHttp2Headers().status("200"), 0, true, ctx.newPromise()); ctx.flush(); }); } @Override public void onGoAwayRead(ChannelHandlerContext ctx, int lastStreamId, long errorCode, ByteBuf debugData) throws Http2Exception { vertx.runOnContext(v -> { testComplete(); }); } }); ChannelFuture s = bootstrap.bind(DEFAULT_HTTPS_HOST, DEFAULT_HTTPS_PORT).sync(); try { HttpClientRequest req = client.get(DEFAULT_HTTPS_PORT, DEFAULT_HTTPS_HOST, "/somepath"); req.handler(resp -> { Context ctx = vertx.getOrCreateContext(); assertOnIOContext(ctx); resp.endHandler(v -> { assertOnIOContext(ctx); req.connection().close(); }); }).end(); await(); } finally { s.channel().close().sync(); } }