Java Code Examples for io.grpc.internal.GrpcUtil#authorityToUri()

The following examples show how to use io.grpc.internal.GrpcUtil#authorityToUri() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ProtocolNegotiators.java    From grpc-nebula-java with Apache License 2.0 6 votes vote down vote up
@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);
}
 
Example 2
Source File: ProtocolNegotiators.java    From grpc-java with Apache License 2.0 6 votes vote down vote up
@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);
}
 
Example 3
Source File: OkHttpClientTransport.java    From grpc-nebula-java with Apache License 2.0 5 votes vote down vote up
@VisibleForTesting
int getOverridenPort() {
  URI uri = GrpcUtil.authorityToUri(defaultAuthority);
  if (uri.getPort() != -1) {
    return uri.getPort();
  }

  return address.getPort();
}
 
Example 4
Source File: OkHttpClientTransport.java    From grpc-java with Apache License 2.0 5 votes vote down vote up
@VisibleForTesting
int getOverridenPort() {
  URI uri = GrpcUtil.authorityToUri(defaultAuthority);
  if (uri.getPort() != -1) {
    return uri.getPort();
  }

  return address.getPort();
}
 
Example 5
Source File: OkHttpClientTransport.java    From grpc-nebula-java with Apache License 2.0 3 votes vote down vote up
/**
 * 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;
}
 
Example 6
Source File: OkHttpClientTransport.java    From grpc-java with Apache License 2.0 3 votes vote down vote up
/**
 * 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;
}