|
|
|
|
@ -3,13 +3,8 @@ package com.github.catvod.net.interceptor; |
|
|
|
|
import androidx.annotation.NonNull; |
|
|
|
|
|
|
|
|
|
import com.github.catvod.net.OkCookieJar; |
|
|
|
|
import com.github.catvod.utils.Json; |
|
|
|
|
import com.google.gson.JsonElement; |
|
|
|
|
import com.google.gson.JsonObject; |
|
|
|
|
|
|
|
|
|
import java.io.IOException; |
|
|
|
|
import java.util.List; |
|
|
|
|
import java.util.Map; |
|
|
|
|
import java.util.concurrent.ConcurrentHashMap; |
|
|
|
|
|
|
|
|
|
import okhttp3.HttpUrl; |
|
|
|
|
@ -20,23 +15,13 @@ import okhttp3.Response; |
|
|
|
|
public class RequestInterceptor implements Interceptor { |
|
|
|
|
|
|
|
|
|
private final ConcurrentHashMap<String, String> authMap; |
|
|
|
|
private final ConcurrentHashMap<String, JsonObject> headerMap; |
|
|
|
|
|
|
|
|
|
public RequestInterceptor() { |
|
|
|
|
authMap = new ConcurrentHashMap<>(); |
|
|
|
|
headerMap = new ConcurrentHashMap<>(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public synchronized void setHeaders(List<JsonElement> items) { |
|
|
|
|
for (JsonElement item : items) { |
|
|
|
|
JsonObject object = Json.safeObject(item); |
|
|
|
|
headerMap.put(object.get("host").getAsString(), object.get("header").getAsJsonObject()); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public void clear() { |
|
|
|
|
authMap.clear(); |
|
|
|
|
headerMap.clear(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@NonNull |
|
|
|
|
@ -46,7 +31,6 @@ public class RequestInterceptor implements Interceptor { |
|
|
|
|
Request.Builder builder = request.newBuilder(); |
|
|
|
|
HttpUrl url = request.url(); |
|
|
|
|
checkAuth(url, builder); |
|
|
|
|
checkHeader(url, builder); |
|
|
|
|
OkCookieJar.sync(url, request); |
|
|
|
|
return chain.proceed(builder.build()); |
|
|
|
|
} |
|
|
|
|
@ -56,11 +40,4 @@ public class RequestInterceptor implements Interceptor { |
|
|
|
|
if (auth != null) authMap.put(url.host(), auth); |
|
|
|
|
if (authMap.containsKey(url.host()) && auth == null) builder.url(url.newBuilder().addQueryParameter("auth", authMap.get(url.host())).build()); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private void checkHeader(HttpUrl url, Request.Builder builder) { |
|
|
|
|
if (!headerMap.containsKey(url.host())) return; |
|
|
|
|
for (Map.Entry<String, JsonElement> entry : headerMap.get(url.host()).entrySet()) { |
|
|
|
|
builder.header(entry.getKey(), entry.getValue().getAsString()); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|