Tomcat7 Header 解析 源码分析

我们在上篇文章分析了http request line tomcat的解析。本文主要讲header 的解析

tomcat 默认对header的个数做了限制,通过maxHeaderCount 参数设置,默认
maxHeaderCount = 100

//解析header 部分前,设置了header 个数的限制
request.getMimeHeaders().setLimit(endpoint.getMaxHeaderCount());
 if (!inputBuffer.parseHeaders()) {
         // head 部分数据缺少,半包的情况,需要等待后面的数据报到来继续读。
         // We've read part of the request, don't recycle it
        // instead associate it with the socket
        openSocket = true;
        readComplete = false;
        break;
 }

parseHeaders 是循环解析所有的header name 和value 对,并添加到
MimeHeaders 的headers 数组中,headers 数组的大小是maxHeaderCount,也就是100,

do {
    //循环读所有的header头
    status = parseHeader();
    // Checking that
    // (1) Headers plus request line size does not exceed its limit
    // (2) There are enough bytes to avoid expanding the buffer when
    // reading body
    // Technically, (2) is technical limitation, (1) is logical
    // limitation to enforce the meaning of headerBufferSize
    // From the way how buf is allocated and how blank lines are being
    // read, it should be enough to check (1) only.
    if (byteBuffer.position() > headerBufferSize || byteBuffer.capacity() - byteBuffer.position() < socketReadBufferSize) {
        throw new IllegalArgumentException(sm.getString("iib.requestheadertoolarge.error"));
    }
} while (status == HeaderParseStatus.HAVE_MORE_HEADERS);
if (status == HeaderParseStatus.DONE) {
    parsingHeader = false;
    end = byteBuffer.position();
    return true;
} else {
    return false;
}

读到':' 时,就添加一个header 并返回value的引用,后面value 解析完后,直接设置headerData.headerValue 即可,headers.addValue() 会判断大小是否超过maxHeaderCount,如超过,则抛headers.maxCountFail 错误。

if (chr == Constants.COLON) {
        headerParsePos = HeaderParsePosition.HEADER_VALUE_START;
 //header name 读完了,创建一个MimeHeaderField 标记该name 的在buffer 里的 offset 和 length headerData.headerValue = headers.addValue(byteBuffer.array(), headerData.start,
                        pos - headerData.start);

headers 里存放的是MimeHeaderField,代表一个header,MimeHeaderField 有两个MessageBytes 类型的nameB和valueB,分别表示header name 和 header value

MessageBytes 只是对上篇讲到的httpinputbuffer的引用,并记录偏移量和长度。这样所有的header name 和value 都引用了tomcat input buffer,只是每一个的offset 和length 不一样而已。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,982评论 19 139
  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 31,767评论 18 399
  • Spring Boot 参考指南 介绍 转载自:https://www.gitbook.com/book/qbgb...
    毛宇鹏阅读 46,970评论 6 342
  • 从三月份找实习到现在,面了一些公司,挂了不少,但最终还是拿到小米、百度、阿里、京东、新浪、CVTE、乐视家的研发岗...
    时芥蓝阅读 42,372评论 11 349
  • "咪蒙"公众号 2017年3月28号 原标题"如何管理你的老板" 我来改标题 1.在职场混不懂这个技巧你会哭成狗。...
    永利哥阅读 208评论 0 3