1 故事背景

  • 最近项目上有个业务需求,翻译成技术需求,即:将request.headers中的几个header入参转换成request.body(pageRequest)中的内置参数。
  • 技术实现,主要就 springmvcorg.springframework.web.bind.WebDataBinder ,并结合 javax.servlet.http.HttpServletRequest,实现将header中的指定参数转发至request.body(pageRequest).params
@RestController("cn.johnnyzen.bd.dataservice.biz.dataservice.controller.v2.CommonSearchController")
@Validated
@Api(tags = "DATA2API Controller # V2")
public class CommonSearchController implements DataServiceOpenApi {
    
    // ...

    @Autowired
    private ServiceConfig serviceConfig;

    // ...

    @InitBinder
    public void forwardedHeadersToParamsDataBinder(WebDataBinder binder, HttpServletRequest request) {
        //判断是否启用本特性
        ForwardedHeaders forwardedHeaders = serviceConfig.getForwardedHeaders();
        if(ObjectUtils.isEmpty(forwardedHeaders) || forwardedHeaders.getEnable().equals(Boolean.FALSE)){
            logger.warn("Fail to forward headers to body's params because that `service-config.forwardedHeaders` be empty or not enabled!");
            return ;
        }
        logger.info("Start to forward request's headers to request body params with `service-config.forwardedHeaders`,config data as follows : \n{}", forwardedHeaders);

        //获取被绑定对象----PageRequest
        PageRequest<Map<String,Object>> pageRequest = (PageRequest<Map<String, Object>>) binder.getTarget();
        if(ObjectUtils.isEmpty(pageRequest) || ObjectUtils.isEmpty(pageRequest.getParams())){
            logger.error("`request.body(pageRequest)` or `request.body(pageRequest).params` is empty!");
            return;
        }
        Map<String,Object> dynamicParams = pageRequest.getParams();

        List<ForwardHeaderToParamConfig> forwardHeaderToParamConfigList = forwardedHeaders.getHeaders();
        forwardHeaderToParamConfigList.stream().forEach(forwardedHeaderConfig -> {
            //获取目标header参数值,并转发至params中
            String headerName = forwardedHeaderConfig.getHeader();
            String headerValue = request.getHeader( forwardedHeaderConfig.getHeader() );
            String paramName = forwardedHeaderConfig.getParam();
            logger.debug("headerName:{}, headerValue:{}, paramName:{}", headerName, headerValue, paramName);
            dynamicParams.put(paramName, headerValue);
        });
    }

    // ...
}

[Servlet/Tomcat] HttpServletRequest#getHeader(headerNameWithIgnoreCase)(获取header时不区分大小写)-LMLPHP

[Servlet/Tomcat] HttpServletRequest#getHeader(headerNameWithIgnoreCase)(获取header时不区分大小写)-LMLPHP

  • 那么,我写这篇博客的目的是什么呢?
  • 你有没有这么一个疑惑:request.getHeader(headerName),这个基于Tomcat.catalina实现的方法,是否区分headerName的大小写?

2 源码分析

  • springmvc: 5.2.15.RELEASE
  • springboot: 2.3.12.RELEASE
  • tomcat-embed: 9.0.46 (springboot内嵌的tomcat)
  • 调试工具: IDEA

Step1 javax.servlet.http.HttpServletRequest : request.getHeader("Accept-Language")

import javax.servlet.http.HttpServletRequest;

//...
request.getHeader("Accept-Language")
//...

[Servlet/Tomcat] HttpServletRequest#getHeader(headerNameWithIgnoreCase)(获取header时不区分大小写)-LMLPHP

Step2 javax.servlet.http.HttpServletRequest#getHeader

javax.servlet.http.HttpServletRequest#getHeader

[Servlet/Tomcat] HttpServletRequest#getHeader(headerNameWithIgnoreCase)(获取header时不区分大小写)-LMLPHP

Step3 org.apache.catalina.connector.Request#getHeader

org.apache.catalina.connector.Request#getHeader

[Servlet/Tomcat] HttpServletRequest#getHeader(headerNameWithIgnoreCase)(获取header时不区分大小写)-LMLPHP

Step4 org.apache.coyote.Request#getHeader

org.apache.coyote.Request#getHeader

[Servlet/Tomcat] HttpServletRequest#getHeader(headerNameWithIgnoreCase)(获取header时不区分大小写)-LMLPHP

Step5 org.apache.tomcat.util.http.MimeHeaders#getHeader

org.apache.tomcat.util.http.MimeHeaders#getHeader

[Servlet/Tomcat] HttpServletRequest#getHeader(headerNameWithIgnoreCase)(获取header时不区分大小写)-LMLPHP

Step6 org.apache.tomcat.util.http.MimeHeaders#getValue(java.lang.String)

org.apache.tomcat.util.http.MimeHeaders#getValue(java.lang.String)

[Servlet/Tomcat] HttpServletRequest#getHeader(headerNameWithIgnoreCase)(获取header时不区分大小写)-LMLPHP

X 参考文献

09-17 02:49