Are query parameters allowed in opaque URIs?

a 夏天 提交于 2020-01-05 08:31:31

问题


Java's URI class defines opaque URIs as follows (emphasis mine).

A URI is opaque if, and only if, it is absolute and its scheme-specific part does not begin with a slash character ('/'). An opaque URI has a scheme, a scheme-specific part, and possibly a fragment; all other components are undefined.

True to the documentation, an opaque URI returns null for a query param.

URI uri = URI.create("stackoverflow:foo?key=value#frag");
uri.isOpaque() == true
uri.getScheme() == stackoverflow
uri.getSchemeSpecificPart() == foo?key=value
uri.getQuery() == null
uri.getFragment() == frag

Is this behavior specific to Java's URI implementation, or does the URI spec disallow query params in opaque URIs?


回答1:


Java's URI class is documented as conforming to RFC 2396 and RFC 2732:

Aside from some minor deviations noted below, an instance of this class represents a URI reference as defined by RFC 2396: Uniform Resource Identifiers (URI): Generic Syntax, amended by RFC 2732: Format for Literal IPv6 Addresses in URLs.

Section 3. URI Syntactic Components doesn't specifically disallow query components in opaque URIs, rather it simply doesn't define a syntax for them. An opaque URI, by definition, requires scheme-specific knowledge to know if there is something analogous to a query component and exactly how to parse it. It is completely legal to have one, but it cannot be supported in the general sense (under this RFC) without that special knowledge. The relevant RFC text is:

The URI syntax is dependent upon the scheme. In general, absolute URI are written as follows:

<scheme>:<scheme-specific-part>

An absolute URI contains the name of the scheme being used (<scheme>) followed by a colon (":") and then a string (the <scheme-specific-part>) whose interpretation depends on the scheme.

Only the generic syntax provides an RFC-defined query component, and that syntax requires at least one slash (/) immediately following the first colon(:) after the scheme.

The URI syntax does not require that the scheme-specific-part have any general structure or set of semantics which is common among all URI. However, a subset of URI do share a common syntax for representing hierarchical relationships within the namespace. This "generic URI" syntax consists of a sequence of four main components:

<scheme>://<authority><path>?<query>

each of which, except <scheme>, may be absent from a particular URI. For example, some URI schemes do not allow an <authority> component, and others do not use a <query> component.

absoluteURI   = scheme ":" ( hier_part | opaque_part )

...

hier_part     = ( net_path | abs_path ) [ "?" query ]
net_path      = "//" authority [ abs_path ]>
abs_path      = "/"  path_segments

Both of the aforementioned RFCs have been obsoleted by RFC 3986, but for backwards-compatibility reasons Java's existing API and behavior is unlikely to be changed.

The URI implementation varies from the newer RFC not only in how it defines valid components of the URI, but also behaviorally. For example, see:

  • Is Java's URI.resolve incompatible with RFC 3986 when the relative URI contains an empty path?

A quick search turns up at least one open-source library attempting to provide an RFC 3986-compatible implementation, but the previous link is neither an endorsement nor recommendation. Compatibility with existing java.net.URI-based APIs may be limited.



来源:https://stackoverflow.com/questions/46833320/are-query-parameters-allowed-in-opaque-uris

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!