简体   繁体   English

URL 中的尾随 & 符号是否合法?

[英]Is a trailing ampersand legal in a URL?

A URL like URL 像

http://localhost/path?a=b&c=d

is fine - but what is the status of the same URL with a trailing ampersand?很好 - 但是带有尾随&符号的相同 URL 的状态是什么?

http://localhost/path?a=b&c=d&

For example the Java Servlet API allows it where Scala's Spray does not (ie it throws an error).例如,Java Servlet API 允许在Scala 的 Spray不允许的地方使用它(即它会引发错误)。

I've tried to find the answer in the URI syntax spec , but not sure how to parse their grammar.我试图在URI syntax spec中找到答案,但不确定如何解析它们的语法。

The URI syntax spec is for generic URIs. URI 语法规范适用于通用 URI。 It allows anything in the query .它允许查询中的任何内容。 I am not aware of any specification which actually specifies ampersand-separated key=value pairs.我不知道有任何规范实际上指定了与号分隔的键=值对。 I believe it is merely convention.我相信这只是惯例。 I know that PHP, for example, offers an option for using a different separator.例如,我知道 PHP 提供了使用不同分隔符的选项。 But now, everyone uses ampersand-separated things when they want key-value pairs.但是现在,每个人在需要键值对时都使用 & 分隔的东西。 You still occasionally come across things which use it for just a simple string, eg http://example.com/?example .您仍然偶尔会遇到仅将它用于简单字符串的东西,例如http://example.com/?example This is perfectly valid.这是完全有效的。

The basic answer, though, is that & is valid anywhere in the query string, including at the end.不过,基本答案是&在查询字符串中的任何位置都有效,包括末尾。


Demistifying the RFC syntax , or Why & is valid anywhere in the query string:消除 RFC 语法的神秘性,或者为什么&在查询字符串中的任何地方都有效:

First, you have首先,你有

query       = *( pchar / "/" / "?" )

(So a query string is made of any number of pchar and literal slashes and question marks.) (因此,查询字符串由任意数量的pchar和文字斜杠和问号组成。)

Going back, you have回过头来,你有

pchar         = unreserved / pct-encoded / sub-delims / ":" / "@"

And earlier still更早的时候

sub-delims  = "!" / "$" / "&" / "'" / "(" / ")"
            / "*" / "+" / "," / ";" / "="

So a literal & is in sub-delims which is in pchar , so it's valid in query因此,文字&位于pchar中的sub-delims中,因此在query中有效

I think there is an unwritten rule that all RFCs must be near impossible to understand.我认为有一条不成文的规则,即所有 RFC 必须几乎无法理解。 You're not the first person unable to parse the grammar and - in my humble opinion - Spray also failed too.你不是第一个无法解析语法的人,而且——以我的拙见——Spray 也失败了。

There is nothing wrong with a trailing ampersand.尾随 & 号没有任何问题。 It is a legal character in the URI used to separate parameters.它是 URI 中用于分隔参数的合法字符。 A trailing ampersand may be pointless, but it isn't invalid.尾随的 & 号可能毫无意义,但并非无效。 Spray should (again, only in my opinion) be simply ignoring it.喷雾应该(再次,仅在我看来)只是忽略它。

If you have a url like the following in a plain-text email:如果您在纯文本 email 中有如下 url:

Please follow this link https://server/doc?param=test.请点击此链接https://server/doc?param=test. to reset the password of your account.重置您的帐户密码。

It becomes a useful feature since if the last parameter ends with ". " (dot + space) and is placed in an e-mail, all kinds of mail clients assume that the dot is ending a sentence and exclude it from the clickable link they add if they detect a URL.它成为一个有用的功能,因为如果最后一个参数以“.”(点 + 空格)结尾并放置在电子邮件中,所有类型的邮件客户端都会假设点是句子的结尾,并将其从可点击的链接中排除。如果他们检测到 URL,请添加。

Putting the link between double-quotes or angle-brackets isn't supported in all mail clients and default URL encoding leaves the dot as it is not a reserved URI character...并非所有邮件客户端都支持在双引号或尖括号之间放置链接,并且默认的 URL 编码会留下点,因为它不是保留的 URI 字符...

I ended up appending a trailing ampersand "&" to the URL to fix this, call me crazy but it works.我最终在 URL 上附加了一个尾随的 & 符号来解决这个问题,叫我疯了,但它有效。

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

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