use of io.netty.handler.codec.http2.Http2Headers in project rest.li by linkedin.
the class Http2StreamCodec method write.
@Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
if (!(msg instanceof RequestWithCallback)) {
ctx.write(msg, promise);
return;
}
Request request = ((RequestWithCallback) msg).request();
Http2ConnectionEncoder encoder = encoder();
int streamId = connection().local().incrementAndGetNextStreamId();
final ChannelFuture headersFuture;
if (request instanceof StreamRequest) {
final StreamRequest streamRequest = (StreamRequest) request;
final Http2Headers http2Headers = NettyRequestAdapter.toHttp2Headers(streamRequest);
final BufferedReader bufferedReader = new BufferedReader(ctx, encoder, streamId, ((RequestWithCallback) msg).handle());
final OrderedEntityStreamReader reader = new OrderedEntityStreamReader(ctx, bufferedReader);
streamRequest.getEntityStream().setReader(reader);
LOG.debug("Sent HTTP/2 HEADERS frame, stream={}, end={}, headers={}, padding={}bytes", new Object[] { streamId, NOT_END_STREAM, http2Headers.size(), NO_PADDING });
headersFuture = encoder.writeHeaders(ctx, streamId, http2Headers, NO_PADDING, NOT_END_STREAM, promise);
headersFuture.addListener(future -> {
if (future.isSuccess()) {
reader.request(BufferedReader.MAX_BUFFERED_CHUNKS);
}
});
} else if (request instanceof RestRequest) {
final RestRequest restRequest = (RestRequest) request;
final Http2Headers headers = NettyRequestAdapter.toHttp2Headers(restRequest);
LOG.debug("Sent HTTP/2 HEADERS frame, stream={}, end={}, headers={}, padding={}bytes", new Object[] { streamId, NOT_END_STREAM, headers.size(), NO_PADDING });
headersFuture = encoder.writeHeaders(ctx, streamId, headers, NO_PADDING, NOT_END_STREAM, promise);
headersFuture.addListener(future -> {
if (future.isSuccess()) {
final ByteBuf data = Unpooled.wrappedBuffer(restRequest.getEntity().asByteBuffer());
LOG.debug("Sent HTTP/2 DATA frame, stream={}, end={}, data={}bytes, padding={}bytes", new Object[] { streamId, END_STREAM, data.readableBytes(), NO_PADDING });
encoder.writeData(ctx, streamId, data, NO_PADDING, END_STREAM, ctx.newPromise());
ctx.channel().flush();
}
});
} else {
// Release the handle to put the channel back to the pool
((RequestWithCallback) msg).handle().release();
throw new IllegalArgumentException("Request is neither StreamRequest or RestRequest");
}
final TransportCallback<?> callback = ((RequestWithCallback) msg).callback();
@SuppressWarnings("unchecked") final TimeoutAsyncPoolHandle<Channel> handle = (TimeoutAsyncPoolHandle<Channel>) ((RequestWithCallback) msg).handle();
headersFuture.addListener(future -> {
if (future.isSuccess()) {
// Sets TransportCallback as a stream property to be retrieved later
Http2PipelinePropertyUtil.set(ctx, connection(), streamId, Http2ClientPipelineInitializer.CALLBACK_ATTR_KEY, callback);
// Sets AsyncPoolHandle as a stream property to be retrieved later
Http2PipelinePropertyUtil.set(ctx, connection(), streamId, Http2ClientPipelineInitializer.CHANNEL_POOL_HANDLE_ATTR_KEY, handle);
// Sets a timeout task to reset stream
// Channel pool handle is also released at timeout
handle.addTimeoutTask(() -> {
LOG.debug("Reset stream upon timeout, stream={}", streamId);
resetStream(ctx, streamId, Http2Error.CANCEL.code(), ctx.newPromise());
ctx.flush();
});
} else {
// Invokes callback onResponse with the error thrown during write header or data
callback.onResponse(TransportResponseImpl.error(future.cause()));
// Releases the handle to put the channel back to the pool
handle.release();
// Resets the stream if a stream is created after we sent header
if (connection().stream(streamId) != null) {
LOG.debug("Reset stream upon timeout, stream={}", streamId);
resetStream(ctx, streamId, Http2Error.CANCEL.code(), ctx.newPromise());
ctx.flush();
}
}
});
}
use of io.netty.handler.codec.http2.Http2Headers 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();
}
}
use of io.netty.handler.codec.http2.Http2Headers in project vert.x by eclipse.
the class Http2ServerTest method testURI.
@Test
public void testURI() throws Exception {
server.requestHandler(req -> {
assertEquals("/some/path", req.path());
assertEquals("foo=foo_value&bar=bar_value_1&bar=bar_value_2", req.query());
assertEquals("/some/path?foo=foo_value&bar=bar_value_1&bar=bar_value_2", req.uri());
assertEquals("http://whatever.com/some/path?foo=foo_value&bar=bar_value_1&bar=bar_value_2", req.absoluteURI());
assertEquals("/some/path?foo=foo_value&bar=bar_value_1&bar=bar_value_2", req.getHeader(":path"));
assertEquals("whatever.com", req.host());
MultiMap params = req.params();
Set<String> names = params.names();
assertEquals(2, names.size());
assertTrue(names.contains("foo"));
assertTrue(names.contains("bar"));
assertEquals("foo_value", params.get("foo"));
assertEquals(Collections.singletonList("foo_value"), params.getAll("foo"));
assertEquals("bar_value_2", params.get("bar"));
assertEquals(Arrays.asList("bar_value_1", "bar_value_2"), params.getAll("bar"));
testComplete();
});
startServer();
TestClient client = new TestClient();
ChannelFuture fut = client.connect(DEFAULT_HTTPS_PORT, DEFAULT_HTTPS_HOST, request -> {
int id = request.nextStreamId();
Http2Headers headers = new DefaultHttp2Headers().method("GET").scheme("http").authority("whatever.com").path("/some/path?foo=foo_value&bar=bar_value_1&bar=bar_value_2");
request.encoder.writeHeaders(request.context, id, headers, 0, true, request.context.newPromise());
request.context.flush();
});
fut.sync();
await();
}
use of io.netty.handler.codec.http2.Http2Headers in project vert.x by eclipse.
the class Http2ServerTest method testSendFile.
private void testSendFile(Buffer expected, String path, long offset, long length) throws Exception {
waitFor(2);
server.requestHandler(req -> {
HttpServerResponse resp = req.response();
resp.bodyEndHandler(v -> {
assertEquals(resp.bytesWritten(), length);
complete();
});
resp.sendFile(path, offset, length);
});
startServer();
TestClient client = new TestClient();
ChannelFuture fut = client.connect(DEFAULT_HTTPS_PORT, DEFAULT_HTTPS_HOST, request -> {
request.decoder.frameListener(new Http2EventAdapter() {
Buffer buffer = Buffer.buffer();
Http2Headers responseHeaders;
@Override
public void onHeadersRead(ChannelHandlerContext ctx, int streamId, Http2Headers headers, int streamDependency, short weight, boolean exclusive, int padding, boolean endStream) throws Http2Exception {
responseHeaders = headers;
}
@Override
public int onDataRead(ChannelHandlerContext ctx, int streamId, ByteBuf data, int padding, boolean endOfStream) throws Http2Exception {
buffer.appendBuffer(Buffer.buffer(data.duplicate()));
if (endOfStream) {
vertx.runOnContext(v -> {
assertEquals("" + length, responseHeaders.get("content-length").toString());
assertEquals(expected, buffer);
complete();
});
}
return data.readableBytes() + padding;
}
});
int id = request.nextStreamId();
request.encoder.writeHeaders(request.context, id, GET("/"), 0, true, request.context.newPromise());
request.context.flush();
});
fut.sync();
await();
}
use of io.netty.handler.codec.http2.Http2Headers in project vert.x by eclipse.
the class Http2ServerTest method test100Continue.
private void test100Continue() throws Exception {
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() {
int count = 0;
@Override
public void onHeadersRead(ChannelHandlerContext ctx, int streamId, Http2Headers headers, int streamDependency, short weight, boolean exclusive, int padding, boolean endStream) throws Http2Exception {
switch(count++) {
case 0:
vertx.runOnContext(v -> {
assertEquals("100", headers.status().toString());
});
request.encoder.writeData(request.context, id, Buffer.buffer("the-body").getByteBuf(), 0, true, request.context.newPromise());
request.context.flush();
break;
case 1:
vertx.runOnContext(v -> {
assertEquals("200", headers.status().toString());
assertEquals("wibble-value", headers.get("wibble").toString());
testComplete();
});
break;
default:
vertx.runOnContext(v -> {
fail();
});
}
}
});
request.encoder.writeHeaders(request.context, id, GET("/").add("expect", "100-continue"), 0, false, request.context.newPromise());
request.context.flush();
});
fut.sync();
await();
}
Aggregations