1、首先开始看HttpServerCodec
可以看到他继承了ChannelHandlerAppender,并且创建了一个HttpRequestDecode和一个HttpResponseEncoder。
Appender内部有一个Entry的list按顺序存放这两个编码解码器
ps:在initchannelhandler时,添加的是HttpServerCode,查看addLast方法代码,也没有发现将这两个编码解码器add在pipeline里面了,是不是在处理是有判断handler是否是一个appender,是的话依次调用list<entry>里的handler。
针对上面的问题,先留着,先看看httprequestencode的代码
2、HttpRequestDecode的处理
对于http的request请求,协议如下
对于基于tcp的协议,底层拿到的字节流需要按照上层的协议格式去解析字节流。
2.1 HttpRequestDecoder类
比较简单,就是调用父类的构造方法,然后增加了以下方法
2.2 HttpObjectDecoder类
比较复杂,看一下官方api的解释
public abstract class HttpObjectDecoder extends ByteToMessageDecoder
Decodes ByteBufs into HttpMessages and HttpContents.
将bytebuf编码成为HttpMessages和HttpContents对象
Parameters that prevents excessive memory consumption
Name |
Meaning |
maxInitialLineLength |
The maximum length of the initial line (e.g. "GET / HTTP/1.0" or "HTTP/1.0 200 OK") If the length of the initial line exceeds this value, a TooLongFrameException will be raised. |
maxHeaderSize |
The maximum length of all headers. If the sum of the length of each header exceeds this value, a TooLongFrameException will be raised. |
maxChunkSize |
The maximum length of the content or each chunk. If the content length (or the length of each chunk) exceeds this value, the content or chunk will be split into multiple HttpContents whose length is maxChunkSize at maximum. |
maxInittialLineLength 初始行例如:"GET / HTTP/1.0" or "HTTP/1.0 200 OK"的最大长度
maxChunkSize content或者一个chunk的最大长度
If the content of an HTTP message is greater than maxChunkSize or the transfer encoding of the HTTP message is
如果一个http消息的content(内容)大于maxChunkSize或者chunked(http消息传输的编码)
'chunked', this decoder generates one HttpMessage instance and its following HttpContents per single HTTP
这个解码器生成一个HttpMessage 实例,在一个http消息里它可能伴随多个HttpContent,为了避免过多的内存消耗
message to avoid excessive memory consumption. For example, the following HTTP message:
GET / HTTP/1.1 Transfer-Encoding: chunked 1a abcdefghijklmnopqrstuvwxyz 10 1234567890abcdef 0 Content-MD5: ... [blank line]
triggers HttpRequestDecoder to generate 3 objects:
- An HttpRequest,
- The first HttpContent whose content is 'abcdefghijklmnopqrstuvwxyz',
- The second LastHttpContent whose conte