Http2RequestHandler Class — netty Architecture
Architecture documentation for the Http2RequestHandler class in Http2RequestHandler.java from the netty codebase.
Entity Profile
Dependency Diagram
graph TD 0c1aeb28_a194_4c7d_9730_36d1126b321b["Http2RequestHandler"] 756e8d67_1cbd_928d_5670_41512d2c49b1["Http2RequestHandler.java"] 0c1aeb28_a194_4c7d_9730_36d1126b321b -->|defined in| 756e8d67_1cbd_928d_5670_41512d2c49b1 738c7b3e_7255_a9ac_d890_2af909b9ba80["channelRead0()"] 0c1aeb28_a194_4c7d_9730_36d1126b321b -->|method| 738c7b3e_7255_a9ac_d890_2af909b9ba80 06ec59f8_ee2e_b482_16d4_74e81cd0c2d8["sendBadRequest()"] 0c1aeb28_a194_4c7d_9730_36d1126b321b -->|method| 06ec59f8_ee2e_b482_16d4_74e81cd0c2d8 6539abb6_e956_a28c_66e5_64aa3895f8df["handleImage()"] 0c1aeb28_a194_4c7d_9730_36d1126b321b -->|method| 6539abb6_e956_a28c_66e5_64aa3895f8df 58398d89_f8bb_fe40_4ff8_975ce680b5c4["handlePage()"] 0c1aeb28_a194_4c7d_9730_36d1126b321b -->|method| 58398d89_f8bb_fe40_4ff8_975ce680b5c4 9e0fc724_23e1_aeee_41a6_2092a463b22e["sendResponse()"] 0c1aeb28_a194_4c7d_9730_36d1126b321b -->|method| 9e0fc724_23e1_aeee_41a6_2092a463b22e 4c48b4a2_9f88_de60_a11e_17f51888d0cd["String()"] 0c1aeb28_a194_4c7d_9730_36d1126b321b -->|method| 4c48b4a2_9f88_de60_a11e_17f51888d0cd 06b9da8f_bd5a_7a3f_b3df_1085982e709b["streamId()"] 0c1aeb28_a194_4c7d_9730_36d1126b321b -->|method| 06b9da8f_bd5a_7a3f_b3df_1085982e709b be1b7177_1022_bc07_81c0_890decf6e29a["exceptionCaught()"] 0c1aeb28_a194_4c7d_9730_36d1126b321b -->|method| be1b7177_1022_bc07_81c0_890decf6e29a
Relationship Graph
Source Code
example/src/main/java/io/netty/example/http2/tiles/Http2RequestHandler.java lines 46–122
public class Http2RequestHandler extends SimpleChannelInboundHandler<FullHttpRequest> {
private static final String LATENCY_FIELD_NAME = "latency";
private static final int MIN_LATENCY = 0;
private static final int MAX_LATENCY = 1000;
private static final String IMAGE_COORDINATE_Y = "y";
private static final String IMAGE_COORDINATE_X = "x";
@Override
protected void channelRead0(ChannelHandlerContext ctx, FullHttpRequest request) throws Exception {
QueryStringDecoder queryString = new QueryStringDecoder(request.uri());
String streamId = streamId(request);
int latency = toInt(firstValue(queryString, LATENCY_FIELD_NAME), 0);
if (latency < MIN_LATENCY || latency > MAX_LATENCY) {
sendBadRequest(ctx, streamId);
return;
}
String x = firstValue(queryString, IMAGE_COORDINATE_X);
String y = firstValue(queryString, IMAGE_COORDINATE_Y);
if (x == null || y == null) {
handlePage(ctx, streamId, latency, request);
} else {
handleImage(x, y, ctx, streamId, latency, request);
}
}
private static void sendBadRequest(ChannelHandlerContext ctx, String streamId) {
FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, BAD_REQUEST, EMPTY_BUFFER);
streamId(response, streamId);
ctx.writeAndFlush(response);
}
private void handleImage(String x, String y, ChannelHandlerContext ctx, String streamId, int latency,
FullHttpRequest request) {
ByteBuf image = ImageCache.INSTANCE.image(parseInt(x), parseInt(y));
FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, OK, image.duplicate());
response.headers().set(CONTENT_TYPE, "image/jpeg");
sendResponse(ctx, streamId, latency, response, request);
}
private void handlePage(ChannelHandlerContext ctx, String streamId, int latency, FullHttpRequest request) {
byte[] body = Html.body(latency);
ByteBuf content = ctx.alloc().buffer(Html.HEADER.length + body.length + Html.FOOTER.length);
content.writeBytes(Html.HEADER);
content.writeBytes(body);
content.writeBytes(Html.FOOTER);
FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, OK, content);
response.headers().set(CONTENT_TYPE, "text/html; charset=UTF-8");
sendResponse(ctx, streamId, latency, response, request);
}
protected void sendResponse(final ChannelHandlerContext ctx, String streamId, int latency,
final FullHttpResponse response, final FullHttpRequest request) {
setContentLength(response, response.content().readableBytes());
streamId(response, streamId);
ctx.executor().schedule(new Runnable() {
@Override
public void run() {
ctx.writeAndFlush(response);
}
}, latency, TimeUnit.MILLISECONDS);
}
private static String streamId(FullHttpRequest request) {
return request.headers().get(HttpConversionUtil.ExtensionHeaderNames.STREAM_ID.text());
}
private static void streamId(FullHttpResponse response, String streamId) {
response.headers().set(HttpConversionUtil.ExtensionHeaderNames.STREAM_ID.text(), streamId);
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
cause.printStackTrace();
ctx.close();
}
}
Source
Frequently Asked Questions
What is the Http2RequestHandler class?
Http2RequestHandler is a class in the netty codebase, defined in example/src/main/java/io/netty/example/http2/tiles/Http2RequestHandler.java.
Where is Http2RequestHandler defined?
Http2RequestHandler is defined in example/src/main/java/io/netty/example/http2/tiles/Http2RequestHandler.java at line 46.
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free