From 41add5d29c43687329923e28ecf0f52b1a30b10c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81lvaro=20Brey?= Date: Fri, 13 May 2022 10:00:09 +0200 Subject: Bump okhttp to 5.0 for Happy Eyeballs support MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This should solve our IPv4/6 problems once and for all Signed-off-by: Álvaro Brey --- library/build.gradle | 2 +- .../java/com/nextcloud/common/IPV6PreferringDNS.kt | 38 ----------- .../nextcloud/common/IPv4FallbackInterceptor.kt | 76 ---------------------- .../java/com/nextcloud/common/NextcloudClient.kt | 6 +- .../main/java/com/nextcloud/common/PlainClient.kt | 6 +- 5 files changed, 3 insertions(+), 125 deletions(-) delete mode 100644 library/src/main/java/com/nextcloud/common/IPV6PreferringDNS.kt delete mode 100644 library/src/main/java/com/nextcloud/common/IPv4FallbackInterceptor.kt diff --git a/library/build.gradle b/library/build.gradle index ad8d3cac..bda505c6 100644 --- a/library/build.gradle +++ b/library/build.gradle @@ -49,7 +49,7 @@ configurations { dependencies { implementation 'org.apache.jackrabbit:jackrabbit-webdav:2.13.5' - api 'com.squareup.okhttp3:okhttp:4.9.3' + api 'com.squareup.okhttp3:okhttp:5.0.0-alpha.7' implementation 'com.gitlab.bitfireAT:dav4jvm:2.1.3' // in transition phase, we use old and new libs implementation group: 'com.google.code.gson', name: 'gson', version: '2.9.0' implementation 'androidx.annotation:annotation:1.3.0' diff --git a/library/src/main/java/com/nextcloud/common/IPV6PreferringDNS.kt b/library/src/main/java/com/nextcloud/common/IPV6PreferringDNS.kt deleted file mode 100644 index b7622c9d..00000000 --- a/library/src/main/java/com/nextcloud/common/IPV6PreferringDNS.kt +++ /dev/null @@ -1,38 +0,0 @@ -/* - * Nextcloud Android Library is available under MIT license - * - * @author Álvaro Brey Vilas - * Copyright (C) 2022 Álvaro Brey Vilas - * Copyright (C) 2022 Nextcloud GmbH - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, - * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND - * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS - * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN - * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN - * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ - -package com.nextcloud.common - -import okhttp3.Dns -import java.net.InetAddress - -object IPV6PreferringDNS : Dns { - - override fun lookup(hostname: String): List { - return DNSCache.lookup(hostname) - } -} diff --git a/library/src/main/java/com/nextcloud/common/IPv4FallbackInterceptor.kt b/library/src/main/java/com/nextcloud/common/IPv4FallbackInterceptor.kt deleted file mode 100644 index a41ea05a..00000000 --- a/library/src/main/java/com/nextcloud/common/IPv4FallbackInterceptor.kt +++ /dev/null @@ -1,76 +0,0 @@ -/* - * Nextcloud Android Library is available under MIT license - * - * @author Álvaro Brey Vilas - * Copyright (C) 2022 Álvaro Brey Vilas - * Copyright (C) 2022 Nextcloud GmbH - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, - * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND - * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS - * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN - * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN - * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ - -package com.nextcloud.common - -import com.owncloud.android.lib.common.utils.Log_OC -import okhttp3.ConnectionPool -import okhttp3.Interceptor -import okhttp3.Request -import okhttp3.Response -import java.net.ConnectException -import java.net.SocketTimeoutException - -class IPv4FallbackInterceptor(private val connectionPool: ConnectionPool) : Interceptor { - companion object { - private const val TAG = "IPv4FallbackInterceptor" - private val SERVER_ERROR_RANGE = 500..599 - } - - @Suppress("TooGenericExceptionCaught") - override fun intercept(chain: Interceptor.Chain): Response { - val request = chain.request() - val response = chain.proceed(request) - - val hostname = request.url.host - - return try { - if (response.code in SERVER_ERROR_RANGE && DNSCache.isIPV6First(hostname)) { - Log_OC.d(TAG, "Response error with IPv6, trying IPv4") - retryWithIPv4(hostname, chain, request) - } else { - response - } - } catch (e: Exception) { - if (DNSCache.isIPV6First(hostname) && (e is SocketTimeoutException || e is ConnectException)) { - return retryWithIPv4(hostname, chain, request) - } - throw e - } - } - - private fun retryWithIPv4( - hostname: String, - chain: Interceptor.Chain, - request: Request - ): Response { - Log_OC.d(TAG, "Error with IPv6, trying IPv4") - DNSCache.setIPVersionPreference(hostname, true) - connectionPool.evictAll() - return chain.proceed(request) - } -} diff --git a/library/src/main/java/com/nextcloud/common/NextcloudClient.kt b/library/src/main/java/com/nextcloud/common/NextcloudClient.kt index 30dca153..722e0c9d 100644 --- a/library/src/main/java/com/nextcloud/common/NextcloudClient.kt +++ b/library/src/main/java/com/nextcloud/common/NextcloudClient.kt @@ -40,7 +40,6 @@ import com.owncloud.android.lib.common.network.RedirectionPath import com.owncloud.android.lib.common.operations.RemoteOperation import com.owncloud.android.lib.common.operations.RemoteOperationResult import com.owncloud.android.lib.common.utils.Log_OC -import okhttp3.ConnectionPool import okhttp3.CookieJar import okhttp3.OkHttpClient import okhttp3.Request @@ -82,17 +81,14 @@ class NextcloudClient private constructor( sslContext.init(null, arrayOf(trustManager), null) val sslSocketFactory = sslContext.socketFactory - val connectionPool = ConnectionPool() return OkHttpClient.Builder() - .connectionPool(connectionPool) .cookieJar(CookieJar.NO_COOKIES) .connectTimeout(DEFAULT_CONNECTION_TIMEOUT_LONG, TimeUnit.MILLISECONDS) .readTimeout(DEFAULT_DATA_TIMEOUT_LONG, TimeUnit.MILLISECONDS) .callTimeout(DEFAULT_CONNECTION_TIMEOUT_LONG + DEFAULT_DATA_TIMEOUT_LONG, TimeUnit.MILLISECONDS) .sslSocketFactory(sslSocketFactory, trustManager) .hostnameVerifier { _: String?, _: SSLSession? -> true } - .addNetworkInterceptor(IPv4FallbackInterceptor(connectionPool)) - .dns(IPV6PreferringDNS) + .fastFallback(true) .build() } } diff --git a/library/src/main/java/com/nextcloud/common/PlainClient.kt b/library/src/main/java/com/nextcloud/common/PlainClient.kt index 2600dcc1..5437ef14 100644 --- a/library/src/main/java/com/nextcloud/common/PlainClient.kt +++ b/library/src/main/java/com/nextcloud/common/PlainClient.kt @@ -32,7 +32,6 @@ import android.content.Context import com.owncloud.android.lib.common.OwnCloudClientFactory.DEFAULT_DATA_TIMEOUT_LONG import com.owncloud.android.lib.common.network.AdvancedX509TrustManager import com.owncloud.android.lib.common.network.NetworkUtils -import okhttp3.ConnectionPool import okhttp3.CookieJar import okhttp3.OkHttpClient import okhttp3.Request @@ -57,15 +56,12 @@ class PlainClient(context: Context) { sslContext.init(null, arrayOf(trustManager), null) val sslSocketFactory = sslContext.socketFactory - val connectionPool = ConnectionPool() return OkHttpClient.Builder() - .connectionPool(connectionPool) .cookieJar(CookieJar.NO_COOKIES) .callTimeout(DEFAULT_DATA_TIMEOUT_LONG, TimeUnit.MILLISECONDS) .sslSocketFactory(sslSocketFactory, trustManager) .hostnameVerifier { _: String?, _: SSLSession? -> true } - .addNetworkInterceptor(IPv4FallbackInterceptor(connectionPool)) - .dns(IPV6PreferringDNS) + .fastFallback(true) .build() } } -- cgit v1.2.3