开发者

Is NSURL supposed to parse invalid URLs?

I could not really find this in the documentation.

I'm writing some unit tests and one of the tests that is supposed to fail is [NSURL URLWithString: @"cow"]. Because cow is not a valid URL.

However, it is happily parsed by NSURL with no errors at all. It does not return nil and it does not throw an exception. Calling [url absoluteString] on it turns it back into @"cow"开发者_JS百科.

What is going on here? Is NSURL really supposed to allow this?


NSURL allows relative URLs, which would include "cow".

The general form of a URI given in RFC 2396 allows relative references:

URI-reference = [ absoluteURI | relativeURI ] [ "#" fragment ]
relativeURI   = ( net_path | abs_path | rel_path ) [ "?" query ]
rel_path      = rel_segment [ abs_path ]
rel_segment   = 1*( unreserved | escaped |
                      ";" | "@" | "&" | "=" | "+" | "$" | "," )

Unless I'm misreading horribly, this means any sequence of valid characters can form a valid relative URL.


From the documentation for +URLWithString:

The string with which to initialize the NSURL object. Must conform to RFC 2396. This method parses URLString according to RFCs 1738 and 1808. ... If the string was malformed, returns nil.

Not being intimately familiar with RFC 2396, I can't say for certain, but I think you must have a minimum of a protocol and a host (even if the domain itself forwards to a host). I'd certainly expect nil.

If you're not getting "nil" back by passing @"cow", you should file a bug at bugreporter.apple.com. You may want to title it, "Don't Have a Cow."

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜