zoukankan      html  css  js  c++  java
  • tomcat源码分析 Http11Processor process

    这个类中Request , Response属性都是可重复使用的,每次请求结束,都会回收,以避免请求量大时创建大量对象。

    一个Processor同一时刻只处理一个请求。

    1、请求开始时设置一些初始信息,如获取socket的inputStream, outputStream,设置当前处理状态为STATE_PARSE

    RequestInfo rp = request.getRequestProcessor();
            rp.setStage(org.apache.coyote.Constants.STAGE_PARSE);
    
            // Setting up the I/O
            setSocketWrapper(socketWrapper);
            getInputBuffer().init(socketWrapper, endpoint);
            getOutputBuffer().init(socketWrapper, endpoint);
    
            // Flags
            error = false;
            keepAlive = true;
            comet = false;
            openSocket = false;
            sendfileInProgress = false;
            readComplete = true;
            if (endpoint.getUsePolling()) {
                keptAlive = false;
            } else {
                keptAlive = socketWrapper.isKeptAlive();
            }
    
            if (disableKeepAlive()) {
                socketWrapper.setKeepAliveLeft(0);
            }

    2、解析部分头信息

    if (!getInputBuffer().parseRequestLine(keptAlive)) {
                        if (handleIncompleteRequestLineRead()) {
                            break;
                        }
                    }

    在这个过程,先最多读取8192个字节,可在server.xml或context.xml中配置protocol 的maxTrailerSize属性,未设置则默认为8192。

    然后根据读取到头信息,进行解析,

    a、请求方法,get或是post等,

    b、请求URL,包括URL中的请求参数

    c、请求协议类型,一般为http/1.1

    3、解析完整的头信息

    // Currently only NIO will ever return false here
                        if (!getInputBuffer().parseHeaders()) {
                            // We've read part of the request, don't recycle it
                            // instead associate it with the socket
                            openSocket = true;
                            readComplete = false;
                            break;
                        }

    4、CoyoteAdapter对象处理请求

    rp.setStage(org.apache.coyote.Constants.STAGE_SERVICE);
                        adapter.service(request, response);
                        // Handle when the response was committed before a serious
                        // error occurred.  Throwing a ServletException should both
                        // set the status to 500 and set the errorException.
                        // If we fail here, then the response is likely already
                        // committed, so we can't try and set headers.
                        if(keepAlive && !error) { // Avoid checking twice.
                            error = response.getErrorException() != null ||
                                    (!isAsync() &&
                                    statusDropsConnection(response.getStatus()));
                        }
                        setCometTimeouts(socketWrapper);
    adapter.service(request, response); 这一步的处理,较为复杂,进入了tomcat的pipeline的处理流程中,最终将调用web应用的filter、servlet。

    5、处理完请求,并做一些善后事宜,如重置processor中request, response对象,以便下一次使用。

  • 相关阅读:
    监听器模式
    接口幂等性实现
    如何设计一个良好的API接口
    接口重试实现
    Spring不常用但有用的注解
    angular项目语言切换功能
    解决IOS上传竖向照片会旋转90度的问题
    微信点击链接:debugx5.qq.com提示您使用的不是x5内核
    swagger注释@API详细说明
    创建swap虚拟内存分区
  • 原文地址:https://www.cnblogs.com/knockon/p/3344734.html
Copyright © 2011-2022 走看看