package retrofit2; import com.airbnb.deeplinkdispatch.UrlTreeKt; import com.google.common.net.HttpHeaders; import com.google.common.primitives.UnsignedBytes; import java.io.IOException; import java.util.regex.Pattern; import o.C15111ghZ; import o.InterfaceC15148gic; import o.gdP; import okhttp3.FormBody; import okhttp3.Headers; import okhttp3.HttpUrl; import okhttp3.MediaType; import okhttp3.MultipartBody; import okhttp3.Request; import okhttp3.RequestBody; /* JADX INFO: Access modifiers changed from: package-private */ /* loaded from: classes.dex */ public final class RequestBuilder { private static final String PATH_SEGMENT_ALWAYS_ENCODE_SET = " \"<>^`{}|\\?#"; private final HttpUrl baseUrl; private RequestBody body; private MediaType contentType; private FormBody.Builder formBuilder; private final boolean hasBody; private final Headers.Builder headersBuilder; private final String method; private MultipartBody.Builder multipartBuilder; private String relativeUrl; private final Request.Builder requestBuilder = new Request.Builder(); private HttpUrl.Builder urlBuilder; private static final char[] HEX_DIGITS = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'}; private static final Pattern PATH_TRAVERSAL = Pattern.compile("(.*/)?(\\.|%2e|%2E){1,2}(/.*)?"); /* JADX INFO: Access modifiers changed from: package-private */ public RequestBuilder(String str, HttpUrl httpUrl, String str2, Headers headers, MediaType mediaType, boolean z, boolean z2, boolean z3) { this.method = str; this.baseUrl = httpUrl; this.relativeUrl = str2; this.contentType = mediaType; this.hasBody = z; if (headers != null) { this.headersBuilder = headers.newBuilder(); } else { this.headersBuilder = new Headers.Builder(); } if (z2) { this.formBuilder = new FormBody.Builder(); } else if (z3) { MultipartBody.Builder builder = new MultipartBody.Builder(); this.multipartBuilder = builder; builder.setType(MultipartBody.FORM); } } /* JADX INFO: Access modifiers changed from: package-private */ public final void setRelativeUrl(Object obj) { this.relativeUrl = obj.toString(); } /* JADX INFO: Access modifiers changed from: package-private */ public final void addHeader(String str, String str2) { if (HttpHeaders.CONTENT_TYPE.equalsIgnoreCase(str)) { try { this.contentType = MediaType.get(str2); return; } catch (IllegalArgumentException e) { throw new IllegalArgumentException("Malformed content type: ".concat(String.valueOf(str2)), e); } } this.headersBuilder.add(str, str2); } /* JADX INFO: Access modifiers changed from: package-private */ public final void addHeaders(Headers headers) { this.headersBuilder.addAll(headers); } /* JADX INFO: Access modifiers changed from: package-private */ public final void addPathParam(String str, String str2, boolean z) { if (this.relativeUrl == null) { throw new AssertionError(); } String canonicalizeForPath = canonicalizeForPath(str2, z); String str3 = this.relativeUrl; StringBuilder sb = new StringBuilder(UrlTreeKt.componentParamPrefix); sb.append(str); sb.append(UrlTreeKt.componentParamSuffix); String replace = str3.replace(sb.toString(), canonicalizeForPath); if (PATH_TRAVERSAL.matcher(replace).matches()) { throw new IllegalArgumentException("@Path parameters shouldn't perform path traversal ('.' or '..'): ".concat(String.valueOf(str2))); } this.relativeUrl = replace; } private static String canonicalizeForPath(String str, boolean z) { int length = str.length(); int i = 0; while (i < length) { int codePointAt = str.codePointAt(i); if (codePointAt >= 32 && codePointAt < 127 && PATH_SEGMENT_ALWAYS_ENCODE_SET.indexOf(codePointAt) == -1 && (z || (codePointAt != 47 && codePointAt != 37))) { i += Character.charCount(codePointAt); } else { C15111ghZ c15111ghZ = new C15111ghZ(); c15111ghZ.c(str, 0, i); canonicalizeForPath(c15111ghZ, str, i, length, z); return c15111ghZ.b(c15111ghZ.c, gdP.a); } } return str; } private static void canonicalizeForPath(C15111ghZ c15111ghZ, String str, int i, int i2, boolean z) { C15111ghZ c15111ghZ2 = null; while (i < i2) { int codePointAt = str.codePointAt(i); if (!z || (codePointAt != 9 && codePointAt != 10 && codePointAt != 12 && codePointAt != 13)) { if (codePointAt < 32 || codePointAt >= 127 || PATH_SEGMENT_ALWAYS_ENCODE_SET.indexOf(codePointAt) != -1 || (!z && (codePointAt == 47 || codePointAt == 37))) { if (c15111ghZ2 == null) { c15111ghZ2 = new C15111ghZ(); } c15111ghZ2.i(codePointAt); while (c15111ghZ2.c != 0) { byte f = c15111ghZ2.f(); c15111ghZ.b(37); char[] cArr = HEX_DIGITS; c15111ghZ.b((int) cArr[((f & UnsignedBytes.MAX_VALUE) >> 4) & 15]); c15111ghZ.b((int) cArr[f & 15]); } } else { c15111ghZ.i(codePointAt); } } i += Character.charCount(codePointAt); } } /* JADX INFO: Access modifiers changed from: package-private */ public final void addQueryParam(String str, String str2, boolean z) { String str3 = this.relativeUrl; if (str3 != null) { HttpUrl.Builder newBuilder = this.baseUrl.newBuilder(str3); this.urlBuilder = newBuilder; if (newBuilder == null) { StringBuilder sb = new StringBuilder("Malformed URL. Base: "); sb.append(this.baseUrl); sb.append(", Relative: "); sb.append(this.relativeUrl); throw new IllegalArgumentException(sb.toString()); } this.relativeUrl = null; } if (z) { this.urlBuilder.addEncodedQueryParameter(str, str2); } else { this.urlBuilder.addQueryParameter(str, str2); } } /* JADX INFO: Access modifiers changed from: package-private */ public final void addFormField(String str, String str2, boolean z) { if (z) { this.formBuilder.addEncoded(str, str2); } else { this.formBuilder.add(str, str2); } } /* JADX INFO: Access modifiers changed from: package-private */ public final void addPart(Headers headers, RequestBody requestBody) { this.multipartBuilder.addPart(headers, requestBody); } /* JADX INFO: Access modifiers changed from: package-private */ public final void addPart(MultipartBody.Part part) { this.multipartBuilder.addPart(part); } /* JADX INFO: Access modifiers changed from: package-private */ public final void addTag(Class cls, T t) { this.requestBuilder.tag(cls, t); } /* JADX INFO: Access modifiers changed from: package-private */ public final Request.Builder get() { HttpUrl resolve; HttpUrl.Builder builder = this.urlBuilder; if (builder != null) { resolve = builder.build(); } else { resolve = this.baseUrl.resolve(this.relativeUrl); if (resolve == null) { StringBuilder sb = new StringBuilder("Malformed URL. Base: "); sb.append(this.baseUrl); sb.append(", Relative: "); sb.append(this.relativeUrl); throw new IllegalArgumentException(sb.toString()); } } RequestBody requestBody = this.body; if (requestBody == null) { FormBody.Builder builder2 = this.formBuilder; if (builder2 != null) { requestBody = builder2.build(); } else { MultipartBody.Builder builder3 = this.multipartBuilder; if (builder3 != null) { requestBody = builder3.build(); } else if (this.hasBody) { requestBody = RequestBody.create((MediaType) null, new byte[0]); } } } MediaType mediaType = this.contentType; if (mediaType != null) { if (requestBody != null) { requestBody = new ContentTypeOverridingRequestBody(requestBody, mediaType); } else { this.headersBuilder.add(HttpHeaders.CONTENT_TYPE, mediaType.toString()); } } return this.requestBuilder.url(resolve).headers(this.headersBuilder.build()).method(this.method, requestBody); } /* loaded from: classes.dex */ static class ContentTypeOverridingRequestBody extends RequestBody { private final MediaType contentType; private final RequestBody delegate; ContentTypeOverridingRequestBody(RequestBody requestBody, MediaType mediaType) { this.delegate = requestBody; this.contentType = mediaType; } @Override // okhttp3.RequestBody public long contentLength() throws IOException { return this.delegate.contentLength(); } @Override // okhttp3.RequestBody public void writeTo(InterfaceC15148gic interfaceC15148gic) throws IOException { this.delegate.writeTo(interfaceC15148gic); } @Override // okhttp3.RequestBody public MediaType contentType() { return this.contentType; } } /* JADX INFO: Access modifiers changed from: package-private */ public final void setBody(RequestBody requestBody) { this.body = requestBody; } }