下面列出了io.grpc.internal.GrpcUtil#authorityToUri ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@VisibleForTesting
HostPort parseAuthority(String authority) {
URI uri = GrpcUtil.authorityToUri(Preconditions.checkNotNull(authority, "authority"));
String host;
int port;
if (uri.getHost() != null) {
host = uri.getHost();
port = uri.getPort();
} else {
/*
* Implementation note: We pick -1 as the port here rather than deriving it from the
* original socket address. The SSL engine doens't use this port number when contacting the
* remote server, but rather it is used for other things like SSL Session caching. When an
* invalid authority is provided (like "bad_cert"), picking the original port and passing it
* in would mean that the port might used under the assumption that it was correct. By
* using -1 here, it forces the SSL implementation to treat it as invalid.
*/
host = authority;
port = -1;
}
return new HostPort(host, port);
}
@VisibleForTesting
static HostPort parseAuthority(String authority) {
URI uri = GrpcUtil.authorityToUri(Preconditions.checkNotNull(authority, "authority"));
String host;
int port;
if (uri.getHost() != null) {
host = uri.getHost();
port = uri.getPort();
} else {
/*
* Implementation note: We pick -1 as the port here rather than deriving it from the
* original socket address. The SSL engine doesn't use this port number when contacting the
* remote server, but rather it is used for other things like SSL Session caching. When an
* invalid authority is provided (like "bad_cert"), picking the original port and passing it
* in would mean that the port might used under the assumption that it was correct. By
* using -1 here, it forces the SSL implementation to treat it as invalid.
*/
host = authority;
port = -1;
}
return new HostPort(host, port);
}
@VisibleForTesting
int getOverridenPort() {
URI uri = GrpcUtil.authorityToUri(defaultAuthority);
if (uri.getPort() != -1) {
return uri.getPort();
}
return address.getPort();
}
@VisibleForTesting
int getOverridenPort() {
URI uri = GrpcUtil.authorityToUri(defaultAuthority);
if (uri.getPort() != -1) {
return uri.getPort();
}
return address.getPort();
}
/**
* Gets the overridden authority hostname. If the authority is overridden to be an invalid
* authority, uri.getHost() will (rightly) return null, since the authority is no longer
* an actual service. This method overrides the behavior for practical reasons. For example,
* if an authority is in the form "invalid_authority" (note the "_"), rather than return null,
* we return the input. This is because the return value, in conjunction with getOverridenPort,
* are used by the SSL library to reconstruct the actual authority. It /already/ has a
* connection to the port, independent of this function.
*
* <p>Note: if the defaultAuthority has a port number in it and is also bad, this code will do
* the wrong thing. An example wrong behavior would be "invalid_host:443". Registry based
* authorities do not have ports, so this is even more wrong than before. Sorry.
*/
@VisibleForTesting
String getOverridenHost() {
URI uri = GrpcUtil.authorityToUri(defaultAuthority);
if (uri.getHost() != null) {
return uri.getHost();
}
return defaultAuthority;
}
/**
* Gets the overridden authority hostname. If the authority is overridden to be an invalid
* authority, uri.getHost() will (rightly) return null, since the authority is no longer
* an actual service. This method overrides the behavior for practical reasons. For example,
* if an authority is in the form "invalid_authority" (note the "_"), rather than return null,
* we return the input. This is because the return value, in conjunction with getOverridenPort,
* are used by the SSL library to reconstruct the actual authority. It /already/ has a
* connection to the port, independent of this function.
*
* <p>Note: if the defaultAuthority has a port number in it and is also bad, this code will do
* the wrong thing. An example wrong behavior would be "invalid_host:443". Registry based
* authorities do not have ports, so this is even more wrong than before. Sorry.
*/
@VisibleForTesting
String getOverridenHost() {
URI uri = GrpcUtil.authorityToUri(defaultAuthority);
if (uri.getHost() != null) {
return uri.getHost();
}
return defaultAuthority;
}