From bccaf074973278ed62573577cad9c3d5fdb8e320 Mon Sep 17 00:00:00 2001 From: Eric Anderson Date: Thu, 10 Sep 2015 15:25:37 -0700 Subject: [PATCH] Use real authority parsing in ClientAuthInterceptor --- .../io/grpc/auth/ClientAuthInterceptor.java | 25 ++++++++++++++----- 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/auth/src/main/java/io/grpc/auth/ClientAuthInterceptor.java b/auth/src/main/java/io/grpc/auth/ClientAuthInterceptor.java index 329685a917..add337f8f2 100644 --- a/auth/src/main/java/io/grpc/auth/ClientAuthInterceptor.java +++ b/auth/src/main/java/io/grpc/auth/ClientAuthInterceptor.java @@ -116,18 +116,31 @@ public final class ClientAuthInterceptor implements ClientInterceptor { } // Always use HTTPS, by definition. final String scheme = "https"; - // The default port must not be present. Alternative ports should be present. - final String suffixToStrip = ":443"; - if (authority.endsWith(suffixToStrip)) { - authority = authority.substring(0, authority.length() - suffixToStrip.length()); - } + final int defaultPort = 443; String path = "/" + MethodDescriptor.extractFullServiceName(method.getFullMethodName()); + URI uri; try { - return new URI(scheme, authority, path, null, null); + uri = new URI(scheme, authority, path, null, null); } catch (URISyntaxException e) { throw Status.UNAUTHENTICATED.withDescription("Unable to construct service URI for auth") .withCause(e).asException(); } + // The default port must not be present. Alternative ports should be present. + if (uri.getPort() == defaultPort) { + uri = removePort(uri); + } + return uri; + } + + private URI removePort(URI uri) throws StatusException { + try { + return new URI(uri.getScheme(), uri.getUserInfo(), uri.getHost(), -1 /* port */, + uri.getPath(), uri.getQuery(), uri.getFragment()); + } catch (URISyntaxException e) { + throw Status.UNAUTHENTICATED.withDescription( + "Unable to construct service URI after removing port") + .withCause(e).asException(); + } } private Map> getRequestMetadata(URI uri) throws StatusException {