Tomcat 在哪里将 / 附加到目录路径?
假设我的 Tomcat webapps 目录如下所示:
webapps/
webapps/fooapp/
webapps/fooapp/WEB-INF/
webapps/fooapp/WEB-INF/web.xml
webapps/fooapp/bardir/
当我对 /fooapp/bardir
发出 GET 请求时,Tomcat 发现 webapps/fooapp/bardir 是一个目录,并向 /fooapp 发送回 302 /bardir/
(末尾有斜杠)。
这是我的问题:在 Tomcat 源代码中的什么位置发生这种情况?(我正在查看 6.0.x,但任何版本的正确答案都将是一个很好的选择)起点。)
我能找到的关于这个主题的唯一参考资料是在 Catalina 功能规范 其中规定,关于默认 Servlet:
在此 servlet 处理的每个 HTTP GET 请求上,应执行以下处理:
[...]
- 如果请求的资源是目录:
- 如果请求路径不以“/”结尾,则重定向到附加“/”的相应路径,以便正确解析欢迎文件中的相对引用。
然而,这个功能似乎并不在 org.apache.catalina.servlets.DefaultServlet 中;或者至少,它不完全存在:如果我将 web.xml 中的默认 servlet 替换为 servlet-class 不存在 的 servlet,目录路径仍然会返回 302添加斜杠,而其他每个请求都会按预期返回错误。
Suppose my Tomcat webapps directory looks like this:
webapps/
webapps/fooapp/
webapps/fooapp/WEB-INF/
webapps/fooapp/WEB-INF/web.xml
webapps/fooapp/bardir/
When I make a GET request for /fooapp/bardir
, Tomcat sees that webapps/fooapp/bardir is a directory and sends back a 302 to /fooapp/bardir/
(with a slash at the end).
Here is my question: Where in the Tomcat source code does this take place? (I'm looking at 6.0.x but a correct answer for any version would be a great starting point.)
The only reference material I can find on this subject is in the Catalina Functional Specifications which states, regarding the Default Servlet:
On each HTTP GET request processed by this servlet, the following processing shall be performed:
[...]
- If the requested resource is a directory:
- If the request path does not end with "/", redirect to a corresponding path with "/" appended so that relative references in welcome files are resolved correctly.
However, this functionality does not appear to be in org.apache.catalina.servlets.DefaultServlet; or at least, it's not there exclusively: if I replace the default servlet in web.xml with a servlet whose servlet-class does not exist, directory paths still come back 302 to add the slash, while every other request comes back with an error as expected.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为它发生在 org.apache.tomcat.util.http.mapper.Mapper,即在
internalMapWrapper(Context, CharChunk, MappingData)中方法。
但不幸的是,我不太确定——也许这确实是一个更适合 的问题tomcat-users 邮件列表。抱歉没有更好的答案。
I think it happens in org.apache.tomcat.util.http.mapper.Mapper, namely in the
internalMapWrapper (Context, CharChunk, MappingData)
method.But unfortunately I'm not really sure -- maybe this really is a question better suited for the tomcat-users mailing list. Sorry for not having a better answer.
Eclipse 调试器了解到重定向发生在
CoyoteAdapter
类,几乎在postParseRequest()
方法。顺便说一句,Tomcat 6.0.20。
更新:实际上,
redirectPath
确实由 @Henning 的答案中提到的Mapper
填充,实际上是在internalMapWrapper()< /代码> 方法。在此处查看源代码。
The Eclipse debugger learnt me that the redirect happens in line 504 of
CoyoteAdapter
class, almost in the end of thepostParseRequest()
method.Tomcat 6.0.20 btw.
Update: actually, the
redirectPath
is indeed filled by theMapper
as mentioned in @Henning's answer, indeed in theinternalMapWrapper()
method. Checkout the source code here.