From 8e4b7c66b5f336dda466410efe42c02a74433cdb Mon Sep 17 00:00:00 2001 From: Vitor Pamplona Date: Wed, 6 Dec 2023 15:40:01 -0500 Subject: [PATCH] Adds a UserAgent to all HTTP requests. --- .../amethyst/service/HttpClient.kt | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/app/src/main/java/com/vitorpamplona/amethyst/service/HttpClient.kt b/app/src/main/java/com/vitorpamplona/amethyst/service/HttpClient.kt index a0a53815b..d8eca104c 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/service/HttpClient.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/service/HttpClient.kt @@ -1,7 +1,12 @@ package com.vitorpamplona.amethyst.service import android.util.Log +import com.vitorpamplona.amethyst.BuildConfig +import okhttp3.Interceptor import okhttp3.OkHttpClient +import okhttp3.Request +import okhttp3.Response +import java.io.IOException import java.net.InetSocketAddress import java.net.Proxy import java.time.Duration @@ -48,11 +53,24 @@ object HttpClient { .readTimeout(duration) .connectTimeout(duration) .writeTimeout(duration) + .addInterceptor(DefaultContentTypeInterceptor()) .followRedirects(true) .followSslRedirects(true) .build() } + class DefaultContentTypeInterceptor : Interceptor { + @Throws(IOException::class) + override fun intercept(chain: Interceptor.Chain): Response { + val originalRequest: Request = chain.request() + val requestWithUserAgent: Request = originalRequest + .newBuilder() + .header("User-Agent", "Amethyst/${BuildConfig.VERSION_NAME}") + .build() + return chain.proceed(requestWithUserAgent) + } + } + fun getHttpClientForRelays(): OkHttpClient { if (this.defaultHttpClient == null) { this.defaultHttpClient = getHttpClient(defaultTimeout)