简体   繁体   English

URL中的方括号导致Tomcat中的异常

[英]Square brackets in URL causes exception in Tomcat

I noticed the following exception is thrown in my Play! 我发现Play中引发了以下异常! application running on Tomcat: 在Tomcat上运行的应用程序:

javax.servlet.ServletException: java.net.URISyntaxException: Illegal character in path at index 35: /K-NAAN/lyrics/Is-Anybody-Out-There[]-lyrics/3807088
    play.server.ServletWrapper.service(ServletWrapper.java:157)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
root cause

java.net.URISyntaxException: Illegal character in path at index 35: /K-NAAN/lyrics/Is-Anybody-Out-There[]-lyrics/3807088
    java.net.URI$Parser.fail(Unknown Source)
    java.net.URI$Parser.checkChars(Unknown Source)
    java.net.URI$Parser.parseHierarchical(Unknown Source)
    java.net.URI$Parser.parse(Unknown Source)
    java.net.URI.<init>(Unknown Source)
    play.server.ServletWrapper.parseRequest(ServletWrapper.java:241)
    play.server.ServletWrapper.service(ServletWrapper.java:132)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:717)

According to this thread square brackets aren't valid characters in an URI, and it's the client's responsibility to encode them? 根据此线程,方括号不是URI中的有效字符,对它们进行编码是客户的责任吗? I get that exception in Chrome on MacOS, but not on Firefox, which seems to imply Chrome doesn't encode them. 我在MacOS的Chrome浏览器中遇到了例外,但在Firefox上却没有,这似乎暗示Chrome浏览器未对它们进行编码。 What's worse, I'm pretty sure googlebot also encounters that exception. 更糟糕的是,我敢肯定googlebot也会遇到该异常。

What's the right course of action here? 正确的做法是什么?

Can I tweak Tomcat to somehow accept or encode square brackets? 我可以调整Tomcat以某种方式接受或编码方括号吗? Or is it a bug or behavior in Play? 还是Play中的错误或行为?

Or should I just make sure that every single link to my pages has square brackets URL-encoded? 还是应该只确保指向我网页的每个链接都用方括号括住网址编码? Possible on my own website, of course, but not so much for external links. 当然可以在我自己的网站上,但对于外部链接则没有那么多。

Any link that wants to use characters that may not be used directly in a URL or have a special meaning needs to encode the characters in the URL. 任何想要使用可能不会在URL中直接使用或具有特殊含义的字符的链接都需要对URL中的字符进行编码。

To make life just a little more complicated, I have noticed that FireFox often displays the unencoded characters but sends the encoded characters in the address bar. 为了使生活稍微复杂一点,我注意到FireFox经常显示未编码的字符,但会在地址栏中发送已编码的字符。 It is worth using something like Firebug (or the equivalent for your browser) to make sure you know what is actually being sent when trying to debug this sort of issue. 值得使用Firebug之类的东西(或与您的浏览器相当的东西)来确保您知道在尝试调试此类问题时实际发送的内容。

The specific exception we were encountering seems to be a bug in Play 1.2.4, and should be fixed in 1.2.5 by returning code 400 instead of 500. 我们遇到的特定异常似乎是Play 1.2.4中的错误,应该在1.2.5中通过返回代码400而不是500进行修复。

https://groups.google.com/d/msg/play-framework/165UJ8LZ0e8/sD0na-NSxLUJ https://groups.google.com/d/msg/play-framework/165UJ8LZ0e8/sD0na-NSxLUJ

We'll also go through and double-check that all our links encode square brackets (which they already should). 我们还将仔细检查并仔细检查所有链接是否都对方括号进行了编码(他们已经应该这样做)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM