package f;

import java.io.IOException;
import java.util.regex.Pattern;
import javax.annotation.Nullable;
import okhttp3.FormBody;
import okhttp3.Headers;
import okhttp3.HttpUrl;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.Request;
import okhttp3.RequestBody;

/* JADX INFO: compiled from: RequestBuilder.java */
/* JADX INFO: loaded from: classes.dex */
final class r {
    private static final char[] l = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
    private static final Pattern m = Pattern.compile("(.*/)?(\\.|%2e|%2E){1,2}(/.*)?");

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    private final String f1725a;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    private final HttpUrl f1726b;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    @Nullable
    private String f1727c;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    @Nullable
    private HttpUrl.Builder f1728d;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    private final Request.Builder f1729e = new Request.Builder();

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    private final Headers.Builder f1730f;

    @Nullable
    private MediaType g;
    private final boolean h;

    @Nullable
    private MultipartBody.Builder i;

    @Nullable
    private FormBody.Builder j;

    @Nullable
    private RequestBody k;

    /* JADX INFO: compiled from: RequestBuilder.java */
    private static class a extends RequestBody {

        /* JADX INFO: renamed from: a, reason: collision with root package name */
        private final RequestBody f1731a;

        /* JADX INFO: renamed from: b, reason: collision with root package name */
        private final MediaType f1732b;

        a(RequestBody requestBody, MediaType mediaType) {
            this.f1731a = requestBody;
            this.f1732b = mediaType;
        }

        @Override // okhttp3.RequestBody
        public long contentLength() throws IOException {
            return this.f1731a.contentLength();
        }

        @Override // okhttp3.RequestBody
        public MediaType contentType() {
            return this.f1732b;
        }

        @Override // okhttp3.RequestBody
        public void writeTo(d.g gVar) throws IOException {
            this.f1731a.writeTo(gVar);
        }
    }

    r(String str, HttpUrl httpUrl, @Nullable String str2, @Nullable Headers headers, @Nullable MediaType mediaType, boolean z, boolean z2, boolean z3) {
        this.f1725a = str;
        this.f1726b = httpUrl;
        this.f1727c = str2;
        this.g = mediaType;
        this.h = z;
        if (headers != null) {
            this.f1730f = headers.newBuilder();
        } else {
            this.f1730f = new Headers.Builder();
        }
        if (z2) {
            this.j = new FormBody.Builder();
        } else if (z3) {
            MultipartBody.Builder builder = new MultipartBody.Builder();
            this.i = builder;
            builder.setType(MultipartBody.FORM);
        }
    }

    private static String g(String str, boolean z) {
        int length = str.length();
        int iCharCount = 0;
        while (iCharCount < length) {
            int iCodePointAt = str.codePointAt(iCharCount);
            if (iCodePointAt < 32 || iCodePointAt >= 127 || " \"<>^`{}|\\?#".indexOf(iCodePointAt) != -1 || (!z && (iCodePointAt == 47 || iCodePointAt == 37))) {
                d.f fVar = new d.f();
                fVar.s0(str, 0, iCharCount);
                h(fVar, str, iCharCount, length, z);
                return fVar.V();
            }
            iCharCount += Character.charCount(iCodePointAt);
        }
        return str;
    }

    private static void h(d.f fVar, String str, int i, int i2, boolean z) {
        d.f fVar2 = null;
        while (i < i2) {
            int iCodePointAt = str.codePointAt(i);
            if (!z || (iCodePointAt != 9 && iCodePointAt != 10 && iCodePointAt != 12 && iCodePointAt != 13)) {
                if (iCodePointAt < 32 || iCodePointAt >= 127 || " \"<>^`{}|\\?#".indexOf(iCodePointAt) != -1 || (!z && (iCodePointAt == 47 || iCodePointAt == 37))) {
                    if (fVar2 == null) {
                        fVar2 = new d.f();
                    }
                    fVar2.t0(iCodePointAt);
                    while (!fVar2.y()) {
                        int i3 = fVar2.readByte() & 255;
                        fVar.j0(37);
                        fVar.j0(l[(i3 >> 4) & 15]);
                        fVar.j0(l[i3 & 15]);
                    }
                } else {
                    fVar.t0(iCodePointAt);
                }
            }
            i += Character.charCount(iCodePointAt);
        }
    }

    void a(String str, String str2, boolean z) {
        if (z) {
            this.j.addEncoded(str, str2);
        } else {
            this.j.add(str, str2);
        }
    }

    void b(String str, String str2) {
        if (!"Content-Type".equalsIgnoreCase(str)) {
            this.f1730f.add(str, str2);
            return;
        }
        try {
            this.g = MediaType.get(str2);
        } catch (IllegalArgumentException e2) {
            throw new IllegalArgumentException("Malformed content type: " + str2, e2);
        }
    }

    void c(Headers headers, RequestBody requestBody) {
        this.i.addPart(headers, requestBody);
    }

    void d(MultipartBody.Part part) {
        this.i.addPart(part);
    }

    void e(String str, String str2, boolean z) {
        if (this.f1727c == null) {
            throw new AssertionError();
        }
        String strG = g(str2, z);
        String strReplace = this.f1727c.replace("{" + str + "}", strG);
        if (!m.matcher(strReplace).matches()) {
            this.f1727c = strReplace;
            return;
        }
        throw new IllegalArgumentException("@Path parameters shouldn't perform path traversal ('.' or '..'): " + str2);
    }

    void f(String str, @Nullable String str2, boolean z) {
        String str3 = this.f1727c;
        if (str3 != null) {
            HttpUrl.Builder builderNewBuilder = this.f1726b.newBuilder(str3);
            this.f1728d = builderNewBuilder;
            if (builderNewBuilder == null) {
                throw new IllegalArgumentException("Malformed URL. Base: " + this.f1726b + ", Relative: " + this.f1727c);
            }
            this.f1727c = null;
        }
        if (z) {
            this.f1728d.addEncodedQueryParameter(str, str2);
        } else {
            this.f1728d.addQueryParameter(str, str2);
        }
    }

    Request.Builder i() {
        HttpUrl httpUrlResolve;
        HttpUrl.Builder builder = this.f1728d;
        if (builder != null) {
            httpUrlResolve = builder.build();
        } else {
            httpUrlResolve = this.f1726b.resolve(this.f1727c);
            if (httpUrlResolve == null) {
                throw new IllegalArgumentException("Malformed URL. Base: " + this.f1726b + ", Relative: " + this.f1727c);
            }
        }
        RequestBody aVar = this.k;
        if (aVar == null) {
            FormBody.Builder builder2 = this.j;
            if (builder2 != null) {
                aVar = builder2.build();
            } else {
                MultipartBody.Builder builder3 = this.i;
                if (builder3 != null) {
                    aVar = builder3.build();
                } else if (this.h) {
                    aVar = RequestBody.create((MediaType) null, new byte[0]);
                }
            }
        }
        MediaType mediaType = this.g;
        if (mediaType != null) {
            if (aVar != null) {
                aVar = new a(aVar, mediaType);
            } else {
                this.f1730f.add("Content-Type", mediaType.toString());
            }
        }
        return this.f1729e.url(httpUrlResolve).headers(this.f1730f.build()).method(this.f1725a, aVar);
    }

    void j(RequestBody requestBody) {
        this.k = requestBody;
    }
}
