use of io.netty.handler.codec.http2.DefaultHttp2Headers in project grpc-java by grpc.
the class GrpcHttp2HeadersDecoderTest method decode_responseHeaders.
@Test
public void decode_responseHeaders() throws Http2Exception {
Http2HeadersDecoder decoder = new GrpcHttp2ClientHeadersDecoder(DEFAULT_MAX_HEADER_LIST_SIZE);
Http2HeadersEncoder encoder = new DefaultHttp2HeadersEncoder(NEVER_SENSITIVE);
Http2Headers headers = new DefaultHttp2Headers(false);
headers.add(of(":status"), of("200")).add(of("custom"), of("header"));
encodedHeaders = Unpooled.buffer();
encoder.encodeHeaders(1, /* randomly chosen */
headers, encodedHeaders);
Http2Headers decodedHeaders = decoder.decodeHeaders(3, /* randomly chosen */
encodedHeaders);
assertEquals(headers.get(of(":status")), decodedHeaders.get(of(":status")));
assertEquals(headers.get(of("custom")), decodedHeaders.get(of("custom")));
assertEquals(headers.size(), decodedHeaders.size());
String toString = decodedHeaders.toString();
assertContainsKeyAndValue(toString, ":status", decodedHeaders.get(of(":status")));
assertContainsKeyAndValue(toString, "custom", decodedHeaders.get(of("custom")));
}
use of io.netty.handler.codec.http2.DefaultHttp2Headers in project netty by netty.
the class ReadOnlyHttp2HeadersBenchmark method defaultClientHeaders.
@Benchmark
@BenchmarkMode(Mode.AverageTime)
public int defaultClientHeaders() {
Http2Headers headers = new DefaultHttp2Headers(false);
for (int i = 0; i < headerCount; ++i) {
headers.add(headerNames[i], headerValues[i]);
}
headers.method(HttpMethod.POST.asciiName());
headers.scheme(HttpScheme.HTTPS.name());
headers.path(path);
headers.authority(authority);
return iterate(headers);
}
use of io.netty.handler.codec.http2.DefaultHttp2Headers in project netty by netty.
the class InboundHttp2ToHttpAdapterTest method clientRequestMultipleHeaders.
@Test
public void clientRequestMultipleHeaders() throws Exception {
boostrapEnv(1, 1, 1);
// writeHeaders will implicitly add an END_HEADERS tag each time and so this test does not follow the HTTP
// message flow. We currently accept this message flow and just add the second headers to the trailing headers.
final String text = "";
final ByteBuf content = Unpooled.copiedBuffer(text.getBytes());
final FullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/some/path/resource2", content, true);
try {
HttpHeaders httpHeaders = request.headers();
httpHeaders.setInt(HttpConversionUtil.ExtensionHeaderNames.STREAM_ID.text(), 3);
httpHeaders.setInt(HttpHeaderNames.CONTENT_LENGTH, text.length());
httpHeaders.setShort(HttpConversionUtil.ExtensionHeaderNames.STREAM_WEIGHT.text(), (short) 16);
HttpHeaders trailingHeaders = request.trailingHeaders();
trailingHeaders.set(of("FoO"), of("goo"));
trailingHeaders.set(of("foO2"), of("goo2"));
trailingHeaders.add(of("fOo2"), of("goo3"));
final Http2Headers http2Headers = new DefaultHttp2Headers().method(new AsciiString("GET")).path(new AsciiString("/some/path/resource2"));
final Http2Headers http2Headers2 = new DefaultHttp2Headers().set(new AsciiString("foo"), new AsciiString("goo")).set(new AsciiString("foo2"), new AsciiString("goo2")).add(new AsciiString("foo2"), new AsciiString("goo3"));
runInChannel(clientChannel, new Http2Runnable() {
@Override
public void run() throws Http2Exception {
clientHandler.encoder().writeHeaders(ctxClient(), 3, http2Headers, 0, false, newPromiseClient());
clientHandler.encoder().writeHeaders(ctxClient(), 3, http2Headers2, 0, false, newPromiseClient());
clientHandler.encoder().writeData(ctxClient(), 3, content.retain(), 0, true, newPromiseClient());
clientChannel.flush();
}
});
awaitRequests();
ArgumentCaptor<FullHttpMessage> requestCaptor = ArgumentCaptor.forClass(FullHttpMessage.class);
verify(serverListener).messageReceived(requestCaptor.capture());
capturedRequests = requestCaptor.getAllValues();
assertEquals(request, capturedRequests.get(0));
} finally {
request.release();
}
}
use of io.netty.handler.codec.http2.DefaultHttp2Headers in project vert.x by eclipse.
the class Http2ServerTest method testNetSocketConnect.
@Test
public void testNetSocketConnect() throws Exception {
waitFor(2);
List<Integer> callbacks = Collections.synchronizedList(new ArrayList<>());
server.requestHandler(req -> {
req.toNetSocket().onComplete(onSuccess(socket -> {
AtomicInteger status = new AtomicInteger();
socket.handler(buff -> {
switch(status.getAndIncrement()) {
case 0:
assertEquals(Buffer.buffer("some-data"), buff);
socket.write(buff, onSuccess(v2 -> callbacks.add(0)));
break;
case 1:
assertEquals(Buffer.buffer("last-data"), buff);
break;
default:
fail();
break;
}
});
socket.endHandler(v1 -> {
assertEquals(2, status.getAndIncrement());
socket.write(Buffer.buffer("last-data"), onSuccess(v2 -> callbacks.add(1)));
});
socket.closeHandler(v -> {
assertEquals(2, callbacks.size());
assertEquals(Arrays.asList(0, 1), callbacks);
complete();
});
}));
});
startServer();
TestClient client = new TestClient();
ChannelFuture fut = client.connect(DEFAULT_HTTPS_PORT, DEFAULT_HTTPS_HOST, request -> {
int id = request.nextStreamId();
request.decoder.frameListener(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("200", headers.status().toString());
assertFalse(endStream);
});
request.encoder.writeData(request.context, id, Buffer.buffer("some-data").getByteBuf(), 0, false, request.context.newPromise());
request.context.flush();
}
StringBuilder received = new StringBuilder();
@Override
public int onDataRead(ChannelHandlerContext ctx, int streamId, ByteBuf data, int padding, boolean endOfStream) throws Http2Exception {
String s = data.toString(StandardCharsets.UTF_8);
received.append(s);
if (received.toString().equals("some-data")) {
received.setLength(0);
vertx.runOnContext(v -> {
assertFalse(endOfStream);
});
request.encoder.writeData(request.context, id, Buffer.buffer("last-data").getByteBuf(), 0, true, request.context.newPromise());
} else if (endOfStream) {
vertx.runOnContext(v -> {
assertEquals("last-data", received.toString());
complete();
});
}
return data.readableBytes() + padding;
}
});
request.encoder.writeHeaders(request.context, id, new DefaultHttp2Headers().method("CONNECT").authority("example.com:80"), 0, false, request.context.newPromise());
request.context.flush();
});
fut.sync();
await();
}
use of io.netty.handler.codec.http2.DefaultHttp2Headers in project vert.x by eclipse.
the class Http2ClientTest method testStreamPriorityChange.
@Test
public void testStreamPriorityChange() throws Exception {
StreamPriority requestStreamPriority = new StreamPriority().setDependency(123).setWeight((short) 45).setExclusive(true);
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 {
client.request(new RequestOptions().setPort(DEFAULT_HTTPS_PORT).setHost(DEFAULT_HTTPS_HOST).setURI("/somepath")).onComplete(onSuccess(req -> {
req.response(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();
}
}
Aggregations