Add MatrixConfiguration network interceptors after all the other, to ensure to view all the network request headers in Flipper.

This commit is contained in:
Benoit Marty 2022-06-15 10:30:38 +02:00 committed by Benoit Marty
parent aea94d79eb
commit 514c4234f2
4 changed files with 34 additions and 12 deletions

View File

@ -29,6 +29,7 @@ import org.matrix.android.sdk.api.MatrixConfiguration
import org.matrix.android.sdk.internal.network.ApiInterceptor
import org.matrix.android.sdk.internal.network.TimeOutInterceptor
import org.matrix.android.sdk.internal.network.UserAgentInterceptor
import org.matrix.android.sdk.internal.network.httpclient.applyMatrixConfiguration
import org.matrix.android.sdk.internal.network.interceptors.CurlLoggingInterceptor
import org.matrix.android.sdk.internal.network.interceptors.FormattedJsonHttpLogger
import java.util.Collections
@ -92,14 +93,9 @@ internal object NetworkModule {
if (BuildConfig.LOG_PRIVATE_DATA) {
addInterceptor(curlLoggingInterceptor)
}
matrixConfiguration.proxy?.let {
proxy(it)
}
matrixConfiguration.networkInterceptors.forEach {
addInterceptor(it)
}
}
.connectionSpecs(Collections.singletonList(spec))
.applyMatrixConfiguration(matrixConfiguration)
.build()
}

View File

@ -17,6 +17,7 @@
package org.matrix.android.sdk.internal.network.httpclient
import okhttp3.OkHttpClient
import org.matrix.android.sdk.api.MatrixConfiguration
import org.matrix.android.sdk.api.auth.data.HomeServerConnectionConfig
import org.matrix.android.sdk.internal.network.AccessTokenInterceptor
import org.matrix.android.sdk.internal.network.interceptors.CurlLoggingInterceptor
@ -51,3 +52,17 @@ internal fun OkHttpClient.Builder.addSocketFactory(homeServerConnectionConfig: H
return this
}
internal fun OkHttpClient.Builder.applyMatrixConfiguration(matrixConfiguration: MatrixConfiguration): OkHttpClient.Builder {
matrixConfiguration.proxy?.let {
proxy(it)
}
// Move networkInterceptors provided in the configuration after all the others
interceptors().removeAll(matrixConfiguration.networkInterceptors)
matrixConfiguration.networkInterceptors.forEach {
addInterceptor(it)
}
return this
}

View File

@ -73,6 +73,7 @@ import org.matrix.android.sdk.internal.network.PreferredNetworkCallbackStrategy
import org.matrix.android.sdk.internal.network.RetrofitFactory
import org.matrix.android.sdk.internal.network.httpclient.addAccessTokenInterceptor
import org.matrix.android.sdk.internal.network.httpclient.addSocketFactory
import org.matrix.android.sdk.internal.network.httpclient.applyMatrixConfiguration
import org.matrix.android.sdk.internal.network.interceptors.CurlLoggingInterceptor
import org.matrix.android.sdk.internal.network.token.AccessTokenProvider
import org.matrix.android.sdk.internal.network.token.HomeserverAccessTokenProvider
@ -212,7 +213,7 @@ internal abstract class SessionModule {
@UnauthenticatedWithCertificate
fun providesOkHttpClientWithCertificate(
@Unauthenticated okHttpClient: OkHttpClient,
homeServerConnectionConfig: HomeServerConnectionConfig
homeServerConnectionConfig: HomeServerConnectionConfig,
): OkHttpClient {
return okHttpClient
.newBuilder()
@ -228,7 +229,8 @@ internal abstract class SessionModule {
@UnauthenticatedWithCertificate okHttpClient: OkHttpClient,
@Authenticated accessTokenProvider: AccessTokenProvider,
@SessionId sessionId: String,
@MockHttpInterceptor testInterceptor: TestInterceptor?
@MockHttpInterceptor testInterceptor: TestInterceptor?,
matrixConfiguration: MatrixConfiguration,
): OkHttpClient {
return okHttpClient
.newBuilder()
@ -239,6 +241,7 @@ internal abstract class SessionModule {
addInterceptor(testInterceptor)
}
}
.applyMatrixConfiguration(matrixConfiguration)
.build()
}
@ -248,9 +251,11 @@ internal abstract class SessionModule {
@UnauthenticatedWithCertificateWithProgress
fun providesProgressOkHttpClient(
@UnauthenticatedWithCertificate okHttpClient: OkHttpClient,
downloadProgressInterceptor: DownloadProgressInterceptor
downloadProgressInterceptor: DownloadProgressInterceptor,
matrixConfiguration: MatrixConfiguration,
): OkHttpClient {
return okHttpClient.newBuilder()
return okHttpClient
.newBuilder()
.apply {
// Remove the previous CurlLoggingInterceptor, to add it after the accessTokenInterceptor
val existingCurlInterceptors = interceptors().filterIsInstance<CurlLoggingInterceptor>()
@ -262,7 +267,9 @@ internal abstract class SessionModule {
existingCurlInterceptors.forEach {
addInterceptor(it)
}
}.build()
}
.applyMatrixConfiguration(matrixConfiguration)
.build()
}
@JvmStatic

View File

@ -21,6 +21,7 @@ import dagger.Module
import dagger.Provides
import io.realm.RealmConfiguration
import okhttp3.OkHttpClient
import org.matrix.android.sdk.api.MatrixConfiguration
import org.matrix.android.sdk.api.session.identity.IdentityService
import org.matrix.android.sdk.internal.database.RealmKeysUtils
import org.matrix.android.sdk.internal.di.AuthenticatedIdentity
@ -29,6 +30,7 @@ import org.matrix.android.sdk.internal.di.SessionFilesDirectory
import org.matrix.android.sdk.internal.di.UnauthenticatedWithCertificate
import org.matrix.android.sdk.internal.di.UserMd5
import org.matrix.android.sdk.internal.network.httpclient.addAccessTokenInterceptor
import org.matrix.android.sdk.internal.network.httpclient.applyMatrixConfiguration
import org.matrix.android.sdk.internal.network.token.AccessTokenProvider
import org.matrix.android.sdk.internal.session.SessionModule
import org.matrix.android.sdk.internal.session.SessionScope
@ -49,11 +51,13 @@ internal abstract class IdentityModule {
@AuthenticatedIdentity
fun providesOkHttpClient(
@UnauthenticatedWithCertificate okHttpClient: OkHttpClient,
@AuthenticatedIdentity accessTokenProvider: AccessTokenProvider
@AuthenticatedIdentity accessTokenProvider: AccessTokenProvider,
matrixConfiguration: MatrixConfiguration,
): OkHttpClient {
return okHttpClient
.newBuilder()
.addAccessTokenInterceptor(accessTokenProvider)
.applyMatrixConfiguration(matrixConfiguration)
.build()
}