use of io.netty.handler.codec.http2.DefaultHttp2Headers in project vert.x by eclipse.
the class Http2ClientTest method testClearText.
private void testClearText(boolean upgrade) throws Exception {
Assume.assumeTrue(testAddress.isInetSocket());
ServerBootstrap bootstrap = createH2CServer((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, true, ctx.newPromise());
ctx.flush();
}
}, upgrade);
ChannelFuture s = bootstrap.bind(DEFAULT_HTTPS_HOST, DEFAULT_HTTPS_PORT).sync();
try {
client.close();
client = vertx.createHttpClient(clientOptions.setUseAlpn(false).setSsl(false).setHttp2ClearTextUpgrade(upgrade));
client.request(requestOptions).onComplete(onSuccess(req1 -> {
req1.send(onSuccess(resp1 -> {
HttpConnection conn = resp1.request().connection();
assertEquals(HttpVersion.HTTP_2, resp1.version());
client.request(requestOptions).onComplete(onSuccess(req2 -> {
req2.send(onSuccess(resp2 -> {
assertSame(((HttpClientConnection) conn).channel(), ((HttpClientConnection) resp2.request().connection()).channel());
testComplete();
}));
}));
}));
}));
await();
} finally {
s.channel().close().sync();
}
}
use of io.netty.handler.codec.http2.DefaultHttp2Headers in project vert.x by eclipse.
the class Http2ClientTest method testInvalidServerResponse.
@Test
public void testInvalidServerResponse() throws Exception {
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("xyz"), 0, false, ctx.newPromise());
ctx.flush();
}
});
ChannelFuture s = bootstrap.bind(DEFAULT_HTTPS_HOST, DEFAULT_HTTPS_PORT).sync();
try {
Context ctx = vertx.getOrCreateContext();
client.close();
ctx.runOnContext(v -> {
client = vertx.createHttpClient(createBaseClientOptions());
client.connectionHandler(conn -> {
conn.exceptionHandler(err -> fail());
});
client.request(requestOptions).onComplete(onSuccess(req -> {
req.send(onFailure(err -> {
assertOnIOContext(ctx);
if (err instanceof NumberFormatException) {
testComplete();
}
}));
}));
});
await();
} finally {
s.channel().close().sync();
}
}
use of io.netty.handler.codec.http2.DefaultHttp2Headers in project vert.x by eclipse.
the class HeadersTestBase method testSetAllOnExistingMapUsingHashMapHttp2.
@Test
public void testSetAllOnExistingMapUsingHashMapHttp2() {
MultiMap mainMap = new Http2HeadersAdaptor(new DefaultHttp2Headers());
mainMap.add("originalKey", "originalValue");
Map<String, String> setAllMap = new HashMap<>();
setAllMap.put("originalKey", "newValue");
setAllMap.put("anotherKey", "anotherValue");
MultiMap result = mainMap.setAll(setAllMap);
assertNotNull(result);
assertFalse(result.isEmpty());
assertEquals(2, result.size());
assertEquals("newValue", result.get("originalKey"));
assertEquals("anotherValue", result.get("anotherKey"));
}
use of io.netty.handler.codec.http2.DefaultHttp2Headers in project rest.li by linkedin.
the class NettyRequestAdapter method toHttp2Headers.
/**
* Extracts fields from a {@link Request} and construct a {@link Http2Headers} instance.
*
* @param request StreamRequest to extract fields from
* @return a new instance of Http2Headers
* @throws {@link Exception}
*/
public static <R extends Request> Http2Headers toHttp2Headers(R request) throws Exception {
URI uri = request.getURI();
URL url = new URL(uri.toString());
String method = request.getMethod();
String authority = url.getAuthority();
String path = url.getFile();
String scheme = uri.getScheme();
// RFC 2616, section 5.1.2:
// Note that the absolute path cannot be empty; if none is present in the original URI,
// it MUST be given as "/" (the server root).
path = path.isEmpty() ? "/" : path;
final Http2Headers headers = new DefaultHttp2Headers().method(method).authority(authority).path(path).scheme(scheme);
for (Map.Entry<String, String> entry : request.getHeaders().entrySet()) {
// Ignores HTTP/2 blacklisted headers
if (HEADER_BLACKLIST.contains(entry.getKey().toLowerCase())) {
continue;
}
// RFC 7540, section 8.1.2:
// ... header field names MUST be converted to lowercase prior to their
// encoding in HTTP/2. A request or response containing uppercase
// header field names MUST be treated as malformed (Section 8.1.2.6).
String name = entry.getKey().toLowerCase();
String value = entry.getValue();
headers.set(name, value);
}
// Split up cookies to allow for better header compression
headers.set(HttpHeaderNames.COOKIE, request.getCookies());
return headers;
}
use of io.netty.handler.codec.http2.DefaultHttp2Headers in project ambry by linkedin.
the class AmbrySendToHttp2Adaptor method write.
/**
* Handles conversion of {@link Send} to HTTP/2 frames.
*/
@Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) {
if (!ctx.channel().isOpen()) {
logger.debug("Channel closed when write. Channel: {}", ctx.channel());
promise.setFailure(new ChannelException("Channel has been closed when write."));
}
if (!(msg instanceof Send)) {
ctx.write(msg, promise);
return;
}
Send send = (Send) msg;
Http2Headers http2Headers;
if (forServer) {
logger.trace("Write content to channel as server {}", ctx.channel());
http2Headers = new DefaultHttp2Headers().status(HttpResponseStatus.OK.codeAsText());
} else {
logger.trace("Write content to channel as client {}", ctx.channel());
http2Headers = new DefaultHttp2Headers().method(HttpMethod.POST.asciiName()).scheme("https").path("/");
}
DefaultHttp2HeadersFrame headersFrame = new DefaultHttp2HeadersFrame(http2Headers, false);
ctx.write(headersFrame);
// Referencing counting for derived {@link ByteBuf}: https://netty.io/wiki/reference-counted-objects.html#derived-buffers
try {
while (send.content().isReadable(maxFrameSize)) {
ByteBuf slice = send.content().readSlice(maxFrameSize);
slice.retain();
DefaultHttp2DataFrame dataFrame = new DefaultHttp2DataFrame(slice, false);
ctx.write(dataFrame);
}
// The last slice
ByteBuf slice = send.content().readSlice(send.content().readableBytes());
slice.retain();
DefaultHttp2DataFrame dataFrame = new DefaultHttp2DataFrame(slice, true);
ctx.write(dataFrame, promise);
} catch (Exception e) {
logger.error("Error while processing frames. Channel: {}", ctx.channel(), e);
} finally {
send.content().release();
}
}
Aggregations