An HTTP line is larger than 4096 bytes

697 阅读1分钟

异常背景

  1. httpserver 使用的是netty实现
  2. httpclient 采用get请求,其中一个参数为content,content在一定情况下会有超长内容的情况
  3. debug 跟进代码确认完整异常如下:
io.netty.handler.codec.TooLongFrameException: An HTTP line is larger than 4096 bytes.

debug源码

   入口 --   ch.pipeline().addLast(new HttpRequestDecoder());
  主要代码执行过程:
    >> io.netty.handler.codec.http.HttpRequestDecoder
	 >> io.netty.handler.codec.http.HttpObjectDecoder#HttpObjectDecoder()
            >>io.netty.handler.codec.http.HttpObjectDecoder.LineParser#parse()
               >>io.netty.handler.codec.http.HttpObjectDecoder.HeaderParser#parse()
                  >>io.netty.handler.codec.http.HttpObjectDecoder.HeaderParser#process()
  • HttpObjectDecoder() 跟踪代码,发现maxInitialLineLength用于初始化LineParser; maxInitialLineLength

  • LineParser

LineParser

  • 异常抛出点 LineParser

  • 查看 io.netty.handler.codec.http.HttpObjectDecoder.HeaderParser#parse (process调用入库) HeaderParser#parse

  • 查看io.netty.handler.codec.http.HttpObjectDecoder.HeaderParser#process 执行逻辑

结论

  1. 完整请求uri字节流-bytebuffer
  2. HeaderParser 完成bytebuffer中二进制到字符的解码,在解码过程中,会将char 扔到AppendableCharSequence里面。 3.在放char append过程中校验最大字符值,因此当大于4096抛出异常。

解决方案

httpclient 将get请求调整为post 请求.