哪些字符使URL无效?
这些url是否有效?
example.com/file [/] . html http://example.com/file [/] . html
哪些字符使URL无效?
这些url是否有效?
example.com/file [/] . html http://example.com/file [/] . html
当前回答
我需要选择字符来分割字符串中的URL,所以我决定创建一个字符列表,这些字符无法在URL中自己找到:
>>> allowed = "-_.~!*'();:@&=+$,/?%#[]?@ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
>>> from string import printable
>>> ''.join(set(printable).difference(set(allowed)))
'`" <\x0b\n\r\x0c\\\t{^}|>'
因此,可能的选择是换行符、制表符、空格、反斜杠和“<>{}^|”。我想我还是用空格或者换行吧。:)
其他回答
来源(需要时加强调):
Unsafe: Characters can be unsafe for a number of reasons. The space character is unsafe because significant spaces may disappear and insignificant spaces may be introduced when URLs are transcribed or typeset or subjected to the treatment of word-processing programs. The characters "<" and ">" are unsafe because they are used as the delimiters around URLs in free text; the quote mark (""") is used to delimit URLs in some systems. The character "#" is unsafe and should always be encoded because it is used in World Wide Web and in other systems to delimit a URL from a fragment/anchor identifier that might follow it. The character "%" is unsafe because it is used for encodings of other characters. Other characters are unsafe because gateways and other transport agents are known to sometimes modify such characters. These characters are "{", "}", "|", "", "^", "~", "[", "]", and "`". All unsafe characters must always be encoded within a URL. For example, the character "#" must be encoded within URLs even in systems that do not normally deal with fragment or anchor identifiers, so that if the URL is copied into another system that does use them, it will not be necessary to change the URL encoding. Source
我不能评论以上的答案,但我想强调的是,并非所有地方都允许使用允许的字符。例如,域名不能有下划线,因此http://test_url.com无效。
一些Unicode字符范围是有效的HTML5,尽管使用它们可能仍然不是一个好主意。
例如,href文档说http://www.w3.org/TR/html5/links.html#attr-hyperlink-href:
a和area元素上的href属性必须具有一个有效的URL值,该值可能被空格包围。
那么“有效URL”的定义指向http://url.spec.whatwg.org/,它说它的目标是:
将RFC 3986和RFC 3987与当代实现对齐,并在此过程中淘汰它们。
该文档将URL代码点定义为:
ASCII alphanumeric, "!", "$", "&", "'", "(", ")", "*", "+", ",", "-", ".", "/", ":", ";", "=", "?", "@", "_", "~", and code points in the ranges U+00A0 to U+D7FF, U+E000 to U+FDCF, U+FDF0 to U+FFFD, U+10000 to U+1FFFD, U+20000 to U+2FFFD, U+30000 to U+3FFFD, U+40000 to U+4FFFD, U+50000 to U+5FFFD, U+60000 to U+6FFFD, U+70000 to U+7FFFD, U+80000 to U+8FFFD, U+90000 to U+9FFFD, U+A0000 to U+AFFFD, U+B0000 to U+BFFFD, U+C0000 to U+CFFFD, U+D0000 to U+DFFFD, U+E1000 to U+EFFFD, U+F0000 to U+FFFFD, U+100000 to U+10FFFD.
然后在语句中使用术语“URL代码点”:
如果c不是URL代码点,也不是“%”,解析错误。
在解析算法的几个部分,包括模式,权限,相对路径,查询和片段状态:所以基本上是整个URL。
同样,验证器http://validator.w3.org/会通过“你好”这样的url,而不会通过像空格“a b”这样的字符的url
当然,正如Stephen C所提到的,这不仅与字符有关,还与上下文有关:你必须理解整个算法。但由于类“URL代码点”用于算法的关键点,它提供了一个很好的想法,你可以使用或不使用。
请参见:url中的Unicode字符
通常,RFC 3986定义的uri(参见章节2:字符)可以包含以下84个字符中的任意一个:
ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._~:/?#[]@!$&'()*+,;=
注意,这个列表没有说明这些字符可能出现在URI中的哪个位置。
任何其他字符都需要使用百分比编码(%hh)进行编码。URI的每个部分对于百分比编码的单词需要表示哪些字符有进一步的限制。
我需要选择字符来分割字符串中的URL,所以我决定创建一个字符列表,这些字符无法在URL中自己找到:
>>> allowed = "-_.~!*'();:@&=+$,/?%#[]?@ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
>>> from string import printable
>>> ''.join(set(printable).difference(set(allowed)))
'`" <\x0b\n\r\x0c\\\t{^}|>'
因此,可能的选择是换行符、制表符、空格、反斜杠和“<>{}^|”。我想我还是用空格或者换行吧。:)