代码之家  ›  专栏  ›  技术社区  ›  Anonymoose

Tomcat在哪里追加/到目录路径?

  •  6
  • Anonymoose  · 技术社区  · 15 年前

    假设我的tomcat webapps目录如下:

    webapps/
    webapps/fooapp/
    webapps/fooapp/WEB-INF/
    webapps/fooapp/WEB-INF/web.xml
    webapps/fooapp/bardir/
    

    当我提出GET请求时 /fooapp/bardir ,Tomcat看到webapps/fooapp/bardir是一个目录,并将302发送回 /fooapp/bardir/ (结尾有一个斜线)。

    我的问题是: 在哪里? 在Tomcat源代码中 这发生了吗? (我看的是6.0.x,但任何版本的正确答案都是一个很好的起点。)

    我能找到的关于这个主题的唯一参考资料是 Catalina Functional Specifications 关于默认servlet,说明:

    对于此servlet处理的每个HTTP GET请求,应执行以下处理:

    […]

    • 如果请求的资源是目录:
      • 如果请求路径不是以“/”结尾,请重定向到附加了“/”的相应路径,以便正确解析欢迎文件中的相对引用。

    但是,这个功能似乎不在org.apache.catalina.servlet s.default servlet中;或者至少,它不是专门存在的:如果我将web.xml中的默认servlet替换为 servlet类不存在 ,目录路径 还是回来302 添加斜线,而其他每个请求都会按预期返回一个错误。

    2 回复  |  直到 15 年前
        1
  •  3
  •   Henning    15 年前

    认为 它发生在 org.apache.tomcat.util.http.mapper.Mapper ,即在 internalMapWrapper (Context, CharChunk, MappingData) 方法。

    但不幸的是,我并不确定——也许这真的是一个更适合 tomcat-users 邮件列表。很抱歉没有更好的答案。

        2
  •  0
  •   BalusC    15 年前

    Eclipse调试器了解到重定向发生在 CoyoteAdapter 上课,差不多快结束了 postParseRequest() 方法。

        // Possible redirect
        MessageBytes redirectPathMB = request.getMappingData().redirectPath;
        if (!redirectPathMB.isNull()) {
            // ...
            response.sendRedirect(redirectPath); // <--- Here.
            return false;
        }
    

    Tomcat 6.0.20英国热量

    更新 事实上, redirectPath 确实是由 Mapper 正如@henning's answer中提到的,实际上 internalMapWrapper() 方法。签出源代码 here .

        if(mappingData.wrapper == null && noServletPath) {
            // The path is empty, redirect to "/"
            mappingData.redirectPath.setChars
                (path.getBuffer(), pathOffset, pathEnd);
            path.setEnd(pathEnd - 1);
            return;
        }