Body HTTP debug info
This commit is contained in:
parent
732dcb83fb
commit
836ae442f3
2 changed files with 61 additions and 55 deletions
|
@ -1,17 +1,22 @@
|
||||||
/* Copyright 2018 charlag
|
/*
|
||||||
|
* Husky -- A Pleroma client for Android
|
||||||
*
|
*
|
||||||
* This file is a part of Tusky.
|
* Copyright (C) 2021 The Husky Developers
|
||||||
|
* Copyright (C) 2018 charlag
|
||||||
*
|
*
|
||||||
* This program is free software; you can redistribute it and/or modify it under the terms of the
|
* This program is free software: you can redistribute it and/or modify
|
||||||
* GNU General Public License as published by the Free Software Foundation; either version 3 of the
|
* it under the terms of the GNU General Public License as published by
|
||||||
* License, or (at your option) any later version.
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
*
|
*
|
||||||
* Tusky is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
|
* This program is distributed in the hope that it will be useful,
|
||||||
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
* Public License for more details.
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
*
|
*
|
||||||
* You should have received a copy of the GNU General Public License along with Tusky; if not,
|
* You should have received a copy of the GNU General Public License
|
||||||
* see <http://www.gnu.org/licenses>. */
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
package com.keylesspalace.tusky.di
|
package com.keylesspalace.tusky.di
|
||||||
|
|
||||||
|
@ -19,7 +24,6 @@ import android.content.Context
|
||||||
import android.text.Spanned
|
import android.text.Spanned
|
||||||
import com.google.gson.Gson
|
import com.google.gson.Gson
|
||||||
import com.google.gson.GsonBuilder
|
import com.google.gson.GsonBuilder
|
||||||
import com.keylesspalace.tusky.BuildConfig
|
|
||||||
import com.keylesspalace.tusky.db.AccountManager
|
import com.keylesspalace.tusky.db.AccountManager
|
||||||
import com.keylesspalace.tusky.json.SpannedTypeAdapter
|
import com.keylesspalace.tusky.json.SpannedTypeAdapter
|
||||||
import com.keylesspalace.tusky.network.InstanceSwitchAuthInterceptor
|
import com.keylesspalace.tusky.network.InstanceSwitchAuthInterceptor
|
||||||
|
@ -27,12 +31,11 @@ import com.keylesspalace.tusky.network.MastodonApi
|
||||||
import com.keylesspalace.tusky.util.OkHttpUtils
|
import com.keylesspalace.tusky.util.OkHttpUtils
|
||||||
import dagger.Module
|
import dagger.Module
|
||||||
import dagger.Provides
|
import dagger.Provides
|
||||||
|
import javax.inject.Singleton
|
||||||
import okhttp3.OkHttpClient
|
import okhttp3.OkHttpClient
|
||||||
import okhttp3.logging.HttpLoggingInterceptor
|
|
||||||
import retrofit2.Retrofit
|
import retrofit2.Retrofit
|
||||||
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory
|
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory
|
||||||
import retrofit2.converter.gson.GsonConverterFactory
|
import retrofit2.converter.gson.GsonConverterFactory
|
||||||
import javax.inject.Singleton
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Created by charlag on 3/24/18.
|
* Created by charlag on 3/24/18.
|
||||||
|
@ -45,41 +48,34 @@ class NetworkModule {
|
||||||
@Singleton
|
@Singleton
|
||||||
fun providesGson(): Gson {
|
fun providesGson(): Gson {
|
||||||
return GsonBuilder()
|
return GsonBuilder()
|
||||||
.registerTypeAdapter(Spanned::class.java, SpannedTypeAdapter())
|
.registerTypeAdapter(Spanned::class.java, SpannedTypeAdapter())
|
||||||
.create()
|
.create()
|
||||||
}
|
}
|
||||||
|
|
||||||
@Provides
|
@Provides
|
||||||
@Singleton
|
@Singleton
|
||||||
fun providesHttpClient(
|
fun providesHttpClient(
|
||||||
accountManager: AccountManager,
|
accountManager: AccountManager,
|
||||||
context: Context
|
context: Context
|
||||||
): OkHttpClient {
|
): OkHttpClient {
|
||||||
return OkHttpUtils.getCompatibleClientBuilder(context)
|
return OkHttpUtils.getCompatibleClientBuilder(context)
|
||||||
.apply {
|
.apply {
|
||||||
addInterceptor(InstanceSwitchAuthInterceptor(accountManager))
|
addInterceptor(InstanceSwitchAuthInterceptor(accountManager))
|
||||||
if (BuildConfig.DEBUG) {
|
}
|
||||||
addInterceptor(HttpLoggingInterceptor().apply {
|
.build()
|
||||||
level = HttpLoggingInterceptor.Level.BASIC
|
|
||||||
//level = HttpLoggingInterceptor.Level.HEADERS
|
|
||||||
//level = HttpLoggingInterceptor.Level.BODY
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
.build()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Provides
|
@Provides
|
||||||
@Singleton
|
@Singleton
|
||||||
fun providesRetrofit(
|
fun providesRetrofit(
|
||||||
httpClient: OkHttpClient,
|
httpClient: OkHttpClient,
|
||||||
gson: Gson
|
gson: Gson
|
||||||
): Retrofit {
|
): Retrofit {
|
||||||
return Retrofit.Builder().baseUrl("https://" + MastodonApi.PLACEHOLDER_DOMAIN)
|
return Retrofit.Builder().baseUrl("https://" + MastodonApi.PLACEHOLDER_DOMAIN)
|
||||||
.client(httpClient)
|
.client(httpClient)
|
||||||
.addConverterFactory(GsonConverterFactory.create(gson))
|
.addConverterFactory(GsonConverterFactory.create(gson))
|
||||||
.addCallAdapterFactory(RxJava2CallAdapterFactory.createAsync())
|
.addCallAdapterFactory(RxJava2CallAdapterFactory.createAsync())
|
||||||
.build()
|
.build()
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,44 +1,46 @@
|
||||||
/* Copyright 2017 Andrew Dawson
|
/*
|
||||||
|
* Husky -- A Pleroma client for Android
|
||||||
*
|
*
|
||||||
* This file is part of Tusky.
|
* Copyright (C) 2021 The Husky Developers
|
||||||
|
* Copyright (C) 2017 Andrew Dawson
|
||||||
*
|
*
|
||||||
* Tusky is free software: you can redistribute it and/or modify it under the terms of the GNU
|
* This program is free software: you can redistribute it and/or modify
|
||||||
* Lesser General Public License as published by the Free Software Foundation, either version 3 of
|
* it under the terms of the GNU General Public License as published by
|
||||||
* the License, or (at your option) any later version.
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
*
|
*
|
||||||
* Tusky is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
|
* This program is distributed in the hope that it will be useful,
|
||||||
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
* General Public License for more details.
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
*
|
*
|
||||||
* You should have received a copy of the GNU Lesser General Public License along with Tusky. If
|
* You should have received a copy of the GNU General Public License
|
||||||
* not, see <http://www.gnu.org/licenses/>. */
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
package com.keylesspalace.tusky.util;
|
package com.keylesspalace.tusky.util;
|
||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.SharedPreferences;
|
import android.content.SharedPreferences;
|
||||||
import android.os.Build;
|
import android.os.Build;
|
||||||
|
|
||||||
import androidx.annotation.NonNull;
|
import androidx.annotation.NonNull;
|
||||||
import androidx.preference.PreferenceManager;
|
import androidx.preference.PreferenceManager;
|
||||||
|
|
||||||
import com.keylesspalace.tusky.BuildConfig;
|
import com.keylesspalace.tusky.BuildConfig;
|
||||||
|
import com.keylesspalace.tusky.core.utils.ApplicationUtils;
|
||||||
import java.net.InetSocketAddress;
|
import java.net.InetSocketAddress;
|
||||||
import java.net.Proxy;
|
import java.net.Proxy;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
import okhttp3.Cache;
|
import okhttp3.Cache;
|
||||||
import okhttp3.Interceptor;
|
import okhttp3.Interceptor;
|
||||||
import okhttp3.OkHttpClient;
|
import okhttp3.OkHttpClient;
|
||||||
import okhttp3.Request;
|
import okhttp3.Request;
|
||||||
import okhttp3.brotli.BrotliInterceptor;
|
import okhttp3.brotli.BrotliInterceptor;
|
||||||
|
import okhttp3.logging.HttpLoggingInterceptor;
|
||||||
|
|
||||||
public class OkHttpUtils {
|
public class OkHttpUtils {
|
||||||
|
|
||||||
@NonNull
|
@NonNull
|
||||||
public static OkHttpClient.Builder getCompatibleClientBuilder(@NonNull Context context) {
|
public static OkHttpClient.Builder getCompatibleClientBuilder(@NonNull Context context) {
|
||||||
|
|
||||||
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
|
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
|
||||||
|
|
||||||
boolean httpProxyEnabled = preferences.getBoolean("httpProxyEnabled", false);
|
boolean httpProxyEnabled = preferences.getBoolean("httpProxyEnabled", false);
|
||||||
|
@ -46,12 +48,12 @@ public class OkHttpUtils {
|
||||||
int httpPort;
|
int httpPort;
|
||||||
try {
|
try {
|
||||||
httpPort = Integer.parseInt(preferences.getString("httpProxyPort", "-1"));
|
httpPort = Integer.parseInt(preferences.getString("httpProxyPort", "-1"));
|
||||||
} catch (NumberFormatException e) {
|
} catch(NumberFormatException e) {
|
||||||
// user has entered wrong port, fall back to no proxy
|
// user has entered wrong port, fall back to no proxy
|
||||||
httpPort = -1;
|
httpPort = -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
int cacheSize = 25*1024*1024; // 25 MiB
|
int cacheSize = 25 * 1024 * 1024; // 25 MiB
|
||||||
|
|
||||||
OkHttpClient.Builder builder = new OkHttpClient.Builder()
|
OkHttpClient.Builder builder = new OkHttpClient.Builder()
|
||||||
.addInterceptor(getUserAgentInterceptor())
|
.addInterceptor(getUserAgentInterceptor())
|
||||||
|
@ -60,7 +62,11 @@ public class OkHttpUtils {
|
||||||
.writeTimeout(30, TimeUnit.SECONDS)
|
.writeTimeout(30, TimeUnit.SECONDS)
|
||||||
.cache(new Cache(context.getCacheDir(), cacheSize));
|
.cache(new Cache(context.getCacheDir(), cacheSize));
|
||||||
|
|
||||||
if (httpProxyEnabled && !httpServer.isEmpty() && (httpPort > 0) && (httpPort < 65535)) {
|
if(ApplicationUtils.INSTANCE.isDebug()) {
|
||||||
|
builder.addInterceptor(getDebugInformation());
|
||||||
|
}
|
||||||
|
|
||||||
|
if(httpProxyEnabled && !httpServer.isEmpty() && (httpPort > 0) && (httpPort < 65535)) {
|
||||||
InetSocketAddress address = InetSocketAddress.createUnresolved(httpServer, httpPort);
|
InetSocketAddress address = InetSocketAddress.createUnresolved(httpServer, httpPort);
|
||||||
builder.proxy(new Proxy(Proxy.Type.HTTP, address));
|
builder.proxy(new Proxy(Proxy.Type.HTTP, address));
|
||||||
}
|
}
|
||||||
|
@ -78,12 +84,16 @@ public class OkHttpUtils {
|
||||||
return chain -> {
|
return chain -> {
|
||||||
Request originalRequest = chain.request();
|
Request originalRequest = chain.request();
|
||||||
Request requestWithUserAgent = originalRequest.newBuilder()
|
Request requestWithUserAgent = originalRequest.newBuilder()
|
||||||
.header("User-Agent", BuildConfig.APPLICATION_NAME + "/"+ BuildConfig.VERSION_NAME+" Android/"+Build.VERSION.RELEASE)
|
.header("User-Agent", BuildConfig.APPLICATION_NAME + "/" + BuildConfig.VERSION_NAME + " Android/" + Build.VERSION.RELEASE)
|
||||||
.build();
|
.build();
|
||||||
return chain.proceed(requestWithUserAgent);
|
return chain.proceed(requestWithUserAgent);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@NonNull
|
||||||
|
private static Interceptor getDebugInformation() {
|
||||||
|
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
|
||||||
|
interceptor.level(HttpLoggingInterceptor.Level.BODY);
|
||||||
|
return interceptor;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue