Skip to content

Commit 3fb1241

Browse files
committed
api: Turn on RFC 3986 parsing by default and update javadoc.
1 parent b38df6c commit 3fb1241

3 files changed

Lines changed: 35 additions & 27 deletions

File tree

api/src/main/java/io/grpc/FeatureFlags.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
import com.google.common.base.Strings;
2121

2222
class FeatureFlags {
23-
private static boolean enableRfc3986Uris = getFlag("GRPC_ENABLE_RFC3986_URIS", false);
23+
private static boolean enableRfc3986Uris = getFlag("GRPC_ENABLE_RFC3986_URIS", true);
2424

2525
/** Whether to parse targets as RFC 3986 URIs (true), or use {@link java.net.URI} (false). */
2626
@VisibleForTesting

api/src/main/java/io/grpc/Grpc.java

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -73,25 +73,25 @@ private Grpc() {
7373
public @interface TransportAttr {}
7474

7575
/**
76-
* Creates a channel builder with a target string and credentials. The target can be either a
77-
* valid {@link NameResolver}-compliant URI, or an authority string.
76+
* Creates a channel builder with a target string and credentials. The target can be either an RFC
77+
* 3986 URI, or an authority string.
78+
*
79+
* <p>Example URIs:
7880
*
79-
* <p>A {@code NameResolver}-compliant URI is an absolute hierarchical URI as defined by {@link
80-
* java.net.URI}. Example URIs:
8181
* <ul>
8282
* <li>{@code "dns:///foo.googleapis.com:8080"}</li>
8383
* <li>{@code "dns:///foo.googleapis.com"}</li>
8484
* <li>{@code "dns:///%5B2001:db8:85a3:8d3:1319:8a2e:370:7348%5D:443"}</li>
8585
* <li>{@code "dns://8.8.8.8/foo.googleapis.com:8080"}</li>
8686
* <li>{@code "dns://8.8.8.8/foo.googleapis.com"}</li>
8787
* <li>{@code "zookeeper://zk.example.com:9900/example_service"}</li>
88+
* <li>{@code "intent:#Intent;package=com.some.app;action=a;category=c;end;"}</li>
8889
* </ul>
8990
*
90-
* <p>An authority string will be converted to a {@code NameResolver}-compliant URI, which has
91-
* the scheme from the name resolver with the highest priority (e.g. {@code "dns"}),
92-
* no authority, and the original authority string as its path after properly escaped.
93-
* We recommend libraries to specify the schema explicitly if it is known, since libraries cannot
94-
* know which NameResolver will be default during runtime.
91+
* <p>An authority string will be converted to a URI having the scheme of the name resolver with
92+
* the highest priority (e.g. {@code "dns"}), the empty string as the authority, and 'target' as
93+
* its path. We recommend libraries specify the scheme explicitly if it is known, since libraries
94+
* cannot know which NameResolver will be default at runtime.
9595
* Example authority strings:
9696
* <ul>
9797
* <li>{@code "localhost"}</li>
@@ -102,6 +102,14 @@ private Grpc() {
102102
* <li>{@code "[2001:db8:85a3:8d3:1319:8a2e:370:7348]"}</li>
103103
* <li>{@code "[2001:db8:85a3:8d3:1319:8a2e:370:7348]:443"}</li>
104104
* </ul>
105+
*
106+
* <p>We also recommend the URI form of `target` because the alternative is ambiguous. For
107+
* example, the target string `foo:8080` is a valid authority string with host `foo` and port
108+
* `8080` but it is also a valid RFC 3986 URI with scheme `foo` and path `8080`. {@code
109+
* NameResolver}s are discovered dynamically from your classpath using SPI, so it's hard to be
110+
* sure in advance how such a target will be resolved. A {@code NameResolver} for scheme `foo`
111+
* might someday make a host named `foo` unreachable! On the other hand, the 'dns:///foo:8080'
112+
* target will always behave the same.
105113
*/
106114
public static ManagedChannelBuilder<?> newChannelBuilder(
107115
String target, ChannelCredentials creds) {
@@ -118,9 +126,7 @@ public static ManagedChannelBuilder<?> newChannelBuilderForAddress(
118126
return newChannelBuilder(authorityFromHostAndPort(host, port), creds);
119127
}
120128

121-
/**
122-
* Combine a host and port into an authority string.
123-
*/
129+
/** Combine a host and port into an authority string. */
124130
// A copy of GrpcUtil.authorityFromHostAndPort
125131
private static String authorityFromHostAndPort(String host, int port) {
126132
try {

api/src/main/java/io/grpc/ManagedChannelBuilder.java

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -45,25 +45,24 @@ public static ManagedChannelBuilder<?> forAddress(String name, int port) {
4545
}
4646

4747
/**
48-
* Creates a channel with a target string, which can be either a valid {@link
49-
* NameResolver}-compliant URI, or an authority string.
48+
* Creates a channel with a target string, which can be either an RFC 3986 URI, or an authority
49+
* string.
5050
*
51-
* <p>A {@code NameResolver}-compliant URI is an absolute hierarchical URI as defined by {@link
52-
* java.net.URI}. Example URIs:
51+
* <p>Example URIs:
5352
* <ul>
5453
* <li>{@code "dns:///foo.googleapis.com:8080"}</li>
5554
* <li>{@code "dns:///foo.googleapis.com"}</li>
5655
* <li>{@code "dns:///%5B2001:db8:85a3:8d3:1319:8a2e:370:7348%5D:443"}</li>
5756
* <li>{@code "dns://8.8.8.8/foo.googleapis.com:8080"}</li>
5857
* <li>{@code "dns://8.8.8.8/foo.googleapis.com"}</li>
5958
* <li>{@code "zookeeper://zk.example.com:9900/example_service"}</li>
59+
* <li>{@code "intent:#Intent;package=com.some.app;action=a;category=c;end;"}</li>
6060
* </ul>
6161
*
62-
* <p>An authority string will be converted to a {@code NameResolver}-compliant URI, which has
63-
* the scheme from the name resolver with the highest priority (e.g. {@code "dns"}),
64-
* no authority, and the original authority string as its path after properly escaped.
65-
* We recommend libraries to specify the schema explicitly if it is known, since libraries cannot
66-
* know which NameResolver will be default during runtime.
62+
* <p>An authority string will be converted to a URI having the scheme of the name resolver with
63+
* the highest priority (e.g. {@code "dns"}), the empty string as the authority, and 'target' as
64+
* its path. We recommend libraries specify the scheme explicitly if it is known, since libraries
65+
* cannot know which NameResolver will be default at runtime.
6766
* Example authority strings:
6867
* <ul>
6968
* <li>{@code "localhost"}</li>
@@ -75,11 +74,14 @@ public static ManagedChannelBuilder<?> forAddress(String name, int port) {
7574
* <li>{@code "[2001:db8:85a3:8d3:1319:8a2e:370:7348]:443"}</li>
7675
* </ul>
7776
*
78-
* <p>Note that there is an open JDK bug on {@link java.net.URI} class parsing an ipv6 scope ID:
79-
* bugs.openjdk.org/browse/JDK-8199396. This method is exposed to this bug. If you experience an
80-
* issue, a work-around is to convert the scope ID to its numeric form (e.g. by using
81-
* Inet6Address.getScopeId()) before calling this method.
82-
*
77+
* <p>We strongly recommend the URI form of `target` because the alternative is ambiguous. For
78+
* example, the target string `foo:8080` is a valid authority string with host `foo` and port
79+
* `8080` but it is also a valid RFC 3986 URI with scheme `foo` and path `8080`. {@code
80+
* NameResolver}s are discovered dynamically from your classpath using SPI, so it's hard to be
81+
* sure in advance how such a target will be resolved. A {@code NameResolver} for scheme `foo`
82+
* might someday make a host named `foo` unreachable! On the other hand, the 'dns:///foo:8080'
83+
* target will always behave the same.
84+
*
8385
* @since 1.0.0
8486
*/
8587
public static ManagedChannelBuilder<?> forTarget(String target) {

0 commit comments

Comments
 (0)