diff --git a/app/src/main/java/per/goweii/wanandroid/http/GoweiiHostInterceptor.kt b/app/src/main/java/per/goweii/wanandroid/http/GoweiiHostInterceptor.kt new file mode 100644 index 00000000..9b461a0a --- /dev/null +++ b/app/src/main/java/per/goweii/wanandroid/http/GoweiiHostInterceptor.kt @@ -0,0 +1,50 @@ +package per.goweii.wanandroid.http + +import android.net.Uri +import okhttp3.HttpUrl +import okhttp3.Interceptor +import okhttp3.Response +import java.io.IOException + +class GoweiiHostInterceptor : Interceptor { + companion object { + private const val GOWEII_HOST = "goweii" + + private const val GITEE_PAGE_SERVER_BASE_URL = "https://goweii.gitee.io/wanandroidserver" + private const val GITEE_RAW_SERVER_BASE_URL = "https://gitee.com/goweii/WanAndroidServer/raw/master" + } + + override fun intercept(chain: Interceptor.Chain): Response { + val request = chain.request() + + val httpUrl = request.url() + if (httpUrl.host() != GOWEII_HOST) { + return chain.proceed(request) + } + + HttpUrl.parse(GITEE_PAGE_SERVER_BASE_URL) + ?.let { chain.tryWithBaseUrl(it) } + ?.takeIf { it.isSuccessful } + ?.let { return it } + + HttpUrl.parse(GITEE_RAW_SERVER_BASE_URL) + ?.let { chain.tryWithBaseUrl(it) } + ?.takeIf { it.isSuccessful } + ?.let { return it } + + throw IOException("Unknown goweii host.") + } + + private fun Interceptor.Chain.tryWithBaseUrl(baseUrl: HttpUrl): Response { + val httpUrl = request().url().replaceWithBaseUrl(baseUrl) + return proceed(request().newBuilder().url(httpUrl).build()) + } + + private fun HttpUrl.replaceWithBaseUrl(baseUrl: HttpUrl): HttpUrl { + val uriBuilder = Uri.parse(baseUrl.toString()).buildUpon() + encodedPathSegments().forEach { uriBuilder.appendEncodedPath(it) } + uriBuilder.encodedQuery(encodedQuery()) + val uri = uriBuilder.build() + return HttpUrl.parse(uri.toString())!! + } +} \ No newline at end of file diff --git a/app/src/main/java/per/goweii/wanandroid/http/RxHttpRequestSetting.java b/app/src/main/java/per/goweii/wanandroid/http/RxHttpRequestSetting.java index 2d600e8a..b97495b0 100644 --- a/app/src/main/java/per/goweii/wanandroid/http/RxHttpRequestSetting.java +++ b/app/src/main/java/per/goweii/wanandroid/http/RxHttpRequestSetting.java @@ -9,6 +9,7 @@ import java.util.Map; import okhttp3.CacheControl; +import okhttp3.Interceptor; import okhttp3.OkHttpClient; import per.goweii.basic.core.common.Config; import per.goweii.basic.utils.DebugUtils; @@ -65,4 +66,12 @@ public void setOkHttpClient(OkHttpClient.Builder builder) { HttpsCompat.enableTls12ForHttpsURLConnection(); builder.cookieJar(mCookieJar); } + + @Nullable + @Override + public Interceptor[] getInterceptors() { + return new Interceptor[]{ + new GoweiiHostInterceptor() + }; + } } diff --git a/app/src/main/java/per/goweii/wanandroid/http/WanApi.java b/app/src/main/java/per/goweii/wanandroid/http/WanApi.java index 31b71899..b1a20e8a 100644 --- a/app/src/main/java/per/goweii/wanandroid/http/WanApi.java +++ b/app/src/main/java/per/goweii/wanandroid/http/WanApi.java @@ -72,35 +72,35 @@ public interface ApiService { @GET("https://v2.jinrishici.com/sentence") Observable> getJinrishici(@retrofit2.http.Header("X-User-Token") String token); - @GET("https://goweii.gitee.io/wanandroidserver/web/article.json") + @GET("https://goweii/web/article.json") @retrofit2.http.Headers({HEADER_USER_AGENT}) Observable>> getWebArticleUrlRegex(); - @GET("https://goweii.gitee.io/wanandroidserver/update/update.json") + @GET("https://goweii/update/update.json") @retrofit2.http.Headers({HEADER_USER_AGENT}) Observable> update(); - @GET("https://goweii.gitee.io/wanandroidserver/about/about_me.json") + @GET("https://goweii/about/about_me.json") @retrofit2.http.Headers({HEADER_USER_AGENT}) Observable> getAboutMe(); - @GET("https://goweii.gitee.io/wanandroidserver/config/config.json") + @GET("https://goweii/config/config.json") @retrofit2.http.Headers({HEADER_USER_AGENT}) Observable> getConfig(); - @GET("https://goweii.gitee.io/wanandroidserver/advert/advert.json") + @GET("https://goweii/advert/advert.json") @retrofit2.http.Headers({HEADER_USER_AGENT}) Observable> getAdvert(); - @GET("https://goweii.gitee.io/wanandroidserver/advert/recommend.json") + @GET("https://goweii/advert/recommend.json") @retrofit2.http.Headers({HEADER_USER_AGENT}) Observable> getRecommend(); - @GET("https://goweii.gitee.io/wanandroidserver/update/beta/update.json") + @GET("https://goweii/update/beta/update.json") @retrofit2.http.Headers({HEADER_USER_AGENT}) Observable> betaUpdate(); - @GET("https://goweii.gitee.io/wanandroidserver/update/beta/users.json") + @GET("https://goweii/update/beta/users.json") @retrofit2.http.Headers({HEADER_USER_AGENT}) Observable>> betaUsers();