aboutsummaryrefslogtreecommitdiff
path: root/okhttp
diff options
context:
space:
mode:
Diffstat (limited to 'okhttp')
-rw-r--r--okhttp/Android.bp34
-rw-r--r--okhttp/src/main/java/io/grpc/okhttp/AsyncFrameWriter.java25
-rw-r--r--okhttp/src/main/java/io/grpc/okhttp/OkHttpClientTransport.java5
-rw-r--r--okhttp/src/main/java/io/grpc/okhttp/OkHttpProtocolNegotiator.java4
-rw-r--r--okhttp/src/main/java/io/grpc/okhttp/OkHttpTlsUpgrader.java2
-rw-r--r--okhttp/src/test/java/io/grpc/okhttp/AsyncFrameWriterTest.java27
-rw-r--r--okhttp/src/test/java/io/grpc/okhttp/OkHttpProtocolNegotiatorTest.java2
-rw-r--r--okhttp/third_party/okhttp/main/java/io/grpc/okhttp/internal/Platform.java6
8 files changed, 96 insertions, 9 deletions
diff --git a/okhttp/Android.bp b/okhttp/Android.bp
new file mode 100644
index 000000000..8193d9bc4
--- /dev/null
+++ b/okhttp/Android.bp
@@ -0,0 +1,34 @@
+// Copyright (C) 2018 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+
+java_library_host {
+ name: "grpc-java-okhttp",
+ srcs: [
+ "third_party/okhttp/main/java/**/*.java",
+ "src/main/java/**/*.java",
+ ],
+ java_resource_dirs: [
+ "src/main/resources",
+ ],
+ libs: [
+ "grpc-java-core",
+ "grpc-java-core-internal",
+ "jsr305",
+ "guava",
+ ],
+ static_libs: [
+ "okhttp",
+ ],
+}
diff --git a/okhttp/src/main/java/io/grpc/okhttp/AsyncFrameWriter.java b/okhttp/src/main/java/io/grpc/okhttp/AsyncFrameWriter.java
index 3049c5bd6..210aaa1f0 100644
--- a/okhttp/src/main/java/io/grpc/okhttp/AsyncFrameWriter.java
+++ b/okhttp/src/main/java/io/grpc/okhttp/AsyncFrameWriter.java
@@ -16,6 +16,7 @@
package io.grpc.okhttp;
+import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Preconditions;
import io.grpc.internal.SerializingExecutor;
import io.grpc.okhttp.internal.framed.ErrorCode;
@@ -24,7 +25,11 @@ import io.grpc.okhttp.internal.framed.Header;
import io.grpc.okhttp.internal.framed.Settings;
import java.io.IOException;
import java.net.Socket;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashSet;
import java.util.List;
+import java.util.Set;
import java.util.concurrent.atomic.AtomicLong;
import java.util.logging.Level;
import java.util.logging.Logger;
@@ -39,6 +44,9 @@ class AsyncFrameWriter implements FrameWriter {
private final SerializingExecutor executor;
private final TransportExceptionHandler transportExceptionHandler;
private final AtomicLong flushCounter = new AtomicLong();
+ // Some exceptions are not very useful and add too much noise to the log
+ private static final Set<String> QUIET_ERRORS =
+ Collections.unmodifiableSet(new HashSet<>(Arrays.asList("Socket closed")));
public AsyncFrameWriter(
TransportExceptionHandler transportExceptionHandler, SerializingExecutor executor) {
@@ -213,13 +221,28 @@ class AsyncFrameWriter implements FrameWriter {
frameWriter.close();
socket.close();
} catch (IOException e) {
- log.log(Level.WARNING, "Failed closing connection", e);
+ log.log(getLogLevel(e), "Failed closing connection", e);
}
}
}
});
}
+ /**
+ * Accepts a throwable and returns the appropriate logging level. Uninteresting exceptions
+ * should not clutter the log.
+ */
+ @VisibleForTesting
+ static Level getLogLevel(Throwable t) {
+ if (t instanceof IOException
+ && t.getMessage() != null
+ && QUIET_ERRORS.contains(t.getMessage())) {
+ return Level.FINE;
+
+ }
+ return Level.INFO;
+ }
+
private abstract class WriteRunnable implements Runnable {
@Override
public final void run() {
diff --git a/okhttp/src/main/java/io/grpc/okhttp/OkHttpClientTransport.java b/okhttp/src/main/java/io/grpc/okhttp/OkHttpClientTransport.java
index 9bca807eb..aa9e07499 100644
--- a/okhttp/src/main/java/io/grpc/okhttp/OkHttpClientTransport.java
+++ b/okhttp/src/main/java/io/grpc/okhttp/OkHttpClientTransport.java
@@ -31,7 +31,6 @@ import com.squareup.okhttp.HttpUrl;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.internal.http.StatusLine;
import io.grpc.Attributes;
-import io.grpc.CallCredentials;
import io.grpc.CallOptions;
import io.grpc.Grpc;
import io.grpc.InternalChannelz;
@@ -46,6 +45,7 @@ import io.grpc.Status.Code;
import io.grpc.StatusException;
import io.grpc.internal.ClientStreamListener.RpcProgress;
import io.grpc.internal.ConnectionClientTransport;
+import io.grpc.internal.GrpcAttributes;
import io.grpc.internal.GrpcUtil;
import io.grpc.internal.Http2Ping;
import io.grpc.internal.KeepAliveManager;
@@ -484,8 +484,9 @@ class OkHttpClientTransport implements ConnectionClientTransport, TransportExcep
attributes = Attributes
.newBuilder()
.set(Grpc.TRANSPORT_ATTR_REMOTE_ADDR, sock.getRemoteSocketAddress())
+ .set(Grpc.TRANSPORT_ATTR_LOCAL_ADDR, sock.getLocalSocketAddress())
.set(Grpc.TRANSPORT_ATTR_SSL_SESSION, sslSession)
- .set(CallCredentials.ATTR_SECURITY_LEVEL,
+ .set(GrpcAttributes.ATTR_SECURITY_LEVEL,
sslSession == null ? SecurityLevel.NONE : SecurityLevel.PRIVACY_AND_INTEGRITY)
.build();
} catch (StatusException e) {
diff --git a/okhttp/src/main/java/io/grpc/okhttp/OkHttpProtocolNegotiator.java b/okhttp/src/main/java/io/grpc/okhttp/OkHttpProtocolNegotiator.java
index 1858a9317..eabe385ff 100644
--- a/okhttp/src/main/java/io/grpc/okhttp/OkHttpProtocolNegotiator.java
+++ b/okhttp/src/main/java/io/grpc/okhttp/OkHttpProtocolNegotiator.java
@@ -93,7 +93,7 @@ class OkHttpProtocolNegotiator {
String negotiatedProtocol = getSelectedProtocol(sslSocket);
if (negotiatedProtocol == null) {
- throw new RuntimeException("protocol negotiation failed");
+ throw new RuntimeException("TLS ALPN negotiation failed with protocols: " + protocols);
}
return negotiatedProtocol;
} finally {
@@ -185,6 +185,7 @@ class OkHttpProtocolNegotiator {
return new String(alpnResult, Util.UTF_8);
}
} catch (Exception e) {
+ logger.log(Level.FINE, "Failed calling getAlpnSelectedProtocol()", e);
// In some implementations, querying selected protocol before the handshake will fail with
// exception.
}
@@ -198,6 +199,7 @@ class OkHttpProtocolNegotiator {
return new String(npnResult, Util.UTF_8);
}
} catch (Exception e) {
+ logger.log(Level.FINE, "Failed calling getNpnSelectedProtocol()", e);
// In some implementations, querying selected protocol before the handshake will fail with
// exception.
}
diff --git a/okhttp/src/main/java/io/grpc/okhttp/OkHttpTlsUpgrader.java b/okhttp/src/main/java/io/grpc/okhttp/OkHttpTlsUpgrader.java
index 0a8672c65..e43713d66 100644
--- a/okhttp/src/main/java/io/grpc/okhttp/OkHttpTlsUpgrader.java
+++ b/okhttp/src/main/java/io/grpc/okhttp/OkHttpTlsUpgrader.java
@@ -43,7 +43,7 @@ final class OkHttpTlsUpgrader {
*/
@VisibleForTesting
static final List<Protocol> TLS_PROTOCOLS =
- Collections.unmodifiableList(Arrays.<Protocol>asList(Protocol.GRPC_EXP, Protocol.HTTP_2));
+ Collections.unmodifiableList(Arrays.asList(Protocol.GRPC_EXP, Protocol.HTTP_2));
/**
* Upgrades given Socket to be a SSLSocket.
diff --git a/okhttp/src/test/java/io/grpc/okhttp/AsyncFrameWriterTest.java b/okhttp/src/test/java/io/grpc/okhttp/AsyncFrameWriterTest.java
index 479a35a78..45e80707c 100644
--- a/okhttp/src/test/java/io/grpc/okhttp/AsyncFrameWriterTest.java
+++ b/okhttp/src/test/java/io/grpc/okhttp/AsyncFrameWriterTest.java
@@ -16,11 +16,13 @@
package io.grpc.okhttp;
+import static com.google.common.truth.Truth.assertThat;
+import static io.grpc.okhttp.AsyncFrameWriter.getLogLevel;
import static org.mockito.Matchers.anyBoolean;
import static org.mockito.Matchers.anyInt;
import static org.mockito.Mockito.inOrder;
+import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
-import static org.mockito.internal.verification.VerificationModeFactory.times;
import io.grpc.internal.SerializingExecutor;
import io.grpc.okhttp.AsyncFrameWriter.TransportExceptionHandler;
@@ -30,6 +32,7 @@ import java.net.Socket;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.Executor;
+import java.util.logging.Level;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -92,6 +95,28 @@ public class AsyncFrameWriterTest {
inOrder.verify(frameWriter).flush();
}
+ @Test
+ public void unknownException() {
+ assertThat(getLogLevel(new Exception())).isEqualTo(Level.INFO);
+ }
+
+ @Test
+ public void quiet() {
+ assertThat(getLogLevel(new IOException("Socket closed"))).isEqualTo(Level.FINE);
+ }
+
+ @Test
+ public void nonquiet() {
+ assertThat(getLogLevel(new IOException("foo"))).isEqualTo(Level.INFO);
+ }
+
+ @Test
+ public void nullMessage() {
+ IOException e = new IOException();
+ assertThat(e.getMessage()).isNull();
+ assertThat(getLogLevel(e)).isEqualTo(Level.INFO);
+ }
+
/**
* Executor queues incoming runnables instead of running it. Runnables can be invoked via {@link
* QueueingExecutor#runAll} in serial order.
diff --git a/okhttp/src/test/java/io/grpc/okhttp/OkHttpProtocolNegotiatorTest.java b/okhttp/src/test/java/io/grpc/okhttp/OkHttpProtocolNegotiatorTest.java
index 5f183dcdf..67d678258 100644
--- a/okhttp/src/test/java/io/grpc/okhttp/OkHttpProtocolNegotiatorTest.java
+++ b/okhttp/src/test/java/io/grpc/okhttp/OkHttpProtocolNegotiatorTest.java
@@ -128,7 +128,7 @@ public class OkHttpProtocolNegotiatorTest {
OkHttpProtocolNegotiator negotiator = new OkHttpProtocolNegotiator(platform);
thrown.expect(RuntimeException.class);
- thrown.expectMessage("protocol negotiation failed");
+ thrown.expectMessage("TLS ALPN negotiation failed");
negotiator.negotiate(sock, "hostname", ImmutableList.of(Protocol.HTTP_2));
}
diff --git a/okhttp/third_party/okhttp/main/java/io/grpc/okhttp/internal/Platform.java b/okhttp/third_party/okhttp/main/java/io/grpc/okhttp/internal/Platform.java
index 51244c902..2610932a5 100644
--- a/okhttp/third_party/okhttp/main/java/io/grpc/okhttp/internal/Platform.java
+++ b/okhttp/third_party/okhttp/main/java/io/grpc/okhttp/internal/Platform.java
@@ -89,7 +89,8 @@ public class Platform {
"com.google.android.gms.org.conscrypt.OpenSSLProvider",
"org.conscrypt.OpenSSLProvider",
"com.android.org.conscrypt.OpenSSLProvider",
- "org.apache.harmony.xnet.provider.jsse.OpenSSLProvider"
+ "org.apache.harmony.xnet.provider.jsse.OpenSSLProvider",
+ "com.google.android.libraries.stitch.sslguard.SslGuardProvider"
};
private static final Platform PLATFORM = findPlatform();
@@ -185,7 +186,8 @@ public class Platform {
if (GrpcUtil.IS_RESTRICTED_APPENGINE) {
tlsExtensionType = TlsExtensionType.ALPN_AND_NPN;
} else if (androidOrAppEngineProvider.getName().equals("GmsCore_OpenSSL")
- || androidOrAppEngineProvider.getName().equals("Conscrypt")) {
+ || androidOrAppEngineProvider.getName().equals("Conscrypt")
+ || androidOrAppEngineProvider.getName().equals("Ssl_Guard")) {
tlsExtensionType = TlsExtensionType.ALPN_AND_NPN;
} else if (isAtLeastAndroid5()) {
tlsExtensionType = TlsExtensionType.ALPN_AND_NPN;