Introduction
Regardless of the protocol, if it is to be actually used, the protocol needs to be converted into the corresponding language to be truly applied. This article will start from the structure of the http2 message, discuss the encapsulation of the http2 message by netty, and take you to have a taste. How far should the real framework be?
Structure of http2 message
The difference between http2 and http1.1 is that it uses a new binary framing, through the client and server to establish a data stream steam to carry out the message interaction between the client and the server. The data stream is a bidirectional byte stream used to send one or more messages.
The message is a logically complete data sent by the client and the server. According to different data sizes, the message can be divided into different frames. In other words, the message is composed of different frames.
Frame is the smallest unit of communication in http2. According to the introduction in the previous section, we know that there are several types of frames:
- DATA frame
- HEADERS frame
- PRIORITY frame
- RST_STREAM frame
- SETTINGS acknowledgment frame
- SETTINGS frame
- PING frame
- PING acknowledgment
- PUSH_PROMISE frame
- GO_AWAY frame
- WINDOW_UPDATE frame
- Unknown Frame
Let's look at a general structure of stream and frame in http2:
In http2, a TCP connection can carry multiple data streams, and different frames in multiple streams can be interleaved.
Each frame uses the stream id to mark the stream to which it belongs.
With the basic concept of http2 above, let's look at the encapsulation of http2 by netty next.
Netty's encapsulation of http2
Http2Stream
As the largest unit stream under a TCP connection, netty provides the interface Http2Stream. Note that Http2Stream is an interface, and it has two implementation classes, DefaultStream and ConnectionStream.
There are two very important attributes in Http2Stream, namely id and state.
The id has been introduced before, and it is the only mark of the stream. Note here that the Stream ID established by the client must be an odd number, while the Stream ID established by the server must be an even number. In addition, the stream with Stream ID 0 has a special function, it is CONNECTION_STREAM_ID, and 1 means HTTP_UPGRADE_STREAM_ID.
state represents the state of the stream. Specifically, the stream has the following states:
IDLE(false, false),
RESERVED_LOCAL(false, false),
RESERVED_REMOTE(false, false),
OPEN(true, true),
HALF_CLOSED_LOCAL(false, true),
HALF_CLOSED_REMOTE(true, false),
CLOSED(false, false);
Why does the state need to distinguish between local and remote? This is because the two ends of the stream connection have the state of both ends.
Corresponding to the stream state is the life cycle of http2. Netty provides Http2LifecycleManager to represent the management of http2 life cycle:
void closeStreamLocal(Http2Stream stream, ChannelFuture future);
void closeStreamRemote(Http2Stream stream, ChannelFuture future);
void closeStream(Http2Stream stream, ChannelFuture future);
ChannelFuture resetStream(ChannelHandlerContext ctx, int streamId, long errorCode,
ChannelPromise promise);
ChannelFuture goAway(ChannelHandlerContext ctx, int lastStreamId, long errorCode,
ByteBuf debugData, ChannelPromise promise);
void onError(ChannelHandlerContext ctx, boolean outbound, Throwable cause);
They are closing the stream, resetting the stream, refusing to create a new stream: goAway, and handling error status.
Http2Frame
After the stream, it is the Http2Frame that actually carries the http2 message. In netty, Http2Frame is an interface, which has many specific implementations.
Direct subclasses of Http2Frame include HTTP2GoAwayFrame, HTTPPingFrame, Http2SettingsFrame, and HTTP2SettingsAckFrame.
Among them, goAway means that new streams are not accepted, and ping is used for heartbeat detection. SETTINGS is used to modify the configuration of the connection or Stream stream.
There is a Http2Settings class and its corresponding in netty.
Some special setting names are defined in this class:
SETTINGS name | meaning |
---|---|
SETTINGS_HEADER_TABLE_SIZE | The maximum size of the peer index table |
SETTINGS_ENABLE_PUSH | Whether to enable server push function |
SETTINGS_MAX_CONCURRENT_STREAMS | The maximum number of concurrent streams allowed on the receiving end |
SETTINGS_INITIAL_WINDOW_SIZE | The window size of the sender, used for Stream level flow control |
SETTINGS_MAX_FRAME_SIZE | Set the maximum size of the frame |
SETTINGS_MAX_HEADER_LIST_SIZE | The maximum size of the peer head index table |
In addition to the four frames mentioned above, other frame implementations are inherited from Http2StreamFrame, specifically PriorityFrame, ResetFrame, HeadersFrame, DataFrame, WindowUpdateFrame, PushPromiseFrame and UnknownFrame.
Each frame represents a different function. The most important ones here are Http2HeadersFrame and Http2DataFrame.
Http2HeadersFrame is mainly the http2 request sent by the client to the server.
Specifically, in addition to the standard http1.1 header, http2 also supports the following headers:
METHOD(":method", true),
SCHEME(":scheme", true),
AUTHORITY(":authority", true),
PATH(":path", true),
STATUS(":status", false),
PROTOCOL(":protocol", true);
For Http2DataFrame, it itself is a ByteBufHolder, used to transmit specific data information. The payload of the data frame is directly stored in ByteBuf.
Summarize
The above is netty's encapsulation of http2 messages.
For the examples in this article, please refer to: learn-netty4
This article has been included in http://www.flydean.com/28-netty-wrap-http2/
The most popular interpretation, the most profound dry goods, the most concise tutorial, and many tips you don't know are waiting for you to discover!
Welcome to pay attention to my official account: "Program those things", know technology, know you better!
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。