URL中的尾随&符合法吗? [英] Is a trailing ampersand legal in a URL?

查看:124
本文介绍了URL中的尾随&符合法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

很好 - 但是带有尾随&符号的同一网址的状态是什么?

is fine - but what is the status of the same URL with a trailing ampersand?

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

例如Java Servlet API允许它在哪里 Scala的Spray 没有(即它会抛出错误)。

For example the Java Servlet API allows it where Scala's Spray does not (ie it throws an error).

我试图找到 URI语法规范中的答案,但不确定如何解析语法。

I've tried to find the answer in the URI syntax spec, but not sure how to parse their grammar.

推荐答案

URI语法规范适用于通用URI。它允许查询中的任何内容。我不知道任何实际指定&符号分隔的键=值对的规范。我认为这仅仅是惯例。我知道PHP,例如,提供了使用不同分隔符的选项。但现在,每个人在需要键值对时都会使用与号分隔的东西。你偶尔会遇到一些使用它的东西只是一个简单的字符串,例如 http://example.com/?example 。这是完全有效的。

The URI syntax spec is for generic URIs. 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. 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, e.g. 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.

解密RFC语法,或为什么& 在查询字符串中的任何位置有效:

Demistifying the RFC syntax, or Why & is valid anywhere in the query string:

首先,您有

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

(因此查询字符串由任意数量的 pchar 和文字斜杠和问号组成。)

(So a query string is made of any number of pchar and literal slashes and question marks.)

回头,你有

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

早些时候仍然

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

所以文字& sub-delims 中,它位于 pchar 中,因此它在查询中有效

So a literal & is in sub-delims which is in pchar, so it's valid in query

这篇关于URL中的尾随&符合法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆