jimu-decompiled/sources/retrofit2/RequestBuilder.java
2025-05-13 19:24:51 +02:00

208 lines
7.4 KiB
Java

package retrofit2;
import java.io.IOException;
import okhttp3.FormBody;
import okhttp3.Headers;
import okhttp3.HttpUrl;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.Request;
import okhttp3.RequestBody;
import okio.Buffer;
import okio.BufferedSink;
/* loaded from: classes2.dex */
final class RequestBuilder {
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 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 String method;
private MultipartBody.Builder multipartBuilder;
private String relativeUrl;
private final Request.Builder requestBuilder = new Request.Builder();
private HttpUrl.Builder urlBuilder;
private 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 MediaType contentType() {
return this.contentType;
}
@Override // okhttp3.RequestBody
public void writeTo(BufferedSink bufferedSink) throws IOException {
this.delegate.writeTo(bufferedSink);
}
}
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.requestBuilder.headers(headers);
}
if (z2) {
this.formBuilder = new FormBody.Builder();
} else if (z3) {
this.multipartBuilder = new MultipartBody.Builder();
this.multipartBuilder.setType(MultipartBody.FORM);
}
}
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))) {
Buffer buffer = new Buffer();
buffer.writeUtf8(str, 0, i);
canonicalizeForPath(buffer, str, i, length, z);
return buffer.readUtf8();
}
i += Character.charCount(codePointAt);
}
return str;
}
void addFormField(String str, String str2, boolean z) {
if (z) {
this.formBuilder.addEncoded(str, str2);
} else {
this.formBuilder.add(str, str2);
}
}
void addHeader(String str, String str2) {
if (!"Content-Type".equalsIgnoreCase(str)) {
this.requestBuilder.addHeader(str, str2);
return;
}
MediaType parse = MediaType.parse(str2);
if (parse != null) {
this.contentType = parse;
return;
}
throw new IllegalArgumentException("Malformed content type: " + str2);
}
void addPart(Headers headers, RequestBody requestBody) {
this.multipartBuilder.addPart(headers, requestBody);
}
void addPathParam(String str, String str2, boolean z) {
String str3 = this.relativeUrl;
if (str3 == null) {
throw new AssertionError();
}
this.relativeUrl = str3.replace("{" + str + "}", canonicalizeForPath(str2, z));
}
void addQueryParam(String str, String str2, boolean z) {
String str3 = this.relativeUrl;
if (str3 != null) {
this.urlBuilder = this.baseUrl.newBuilder(str3);
if (this.urlBuilder == null) {
throw new IllegalArgumentException("Malformed URL. Base: " + this.baseUrl + ", Relative: " + this.relativeUrl);
}
this.relativeUrl = null;
}
if (z) {
this.urlBuilder.addEncodedQueryParameter(str, str2);
} else {
this.urlBuilder.addQueryParameter(str, str2);
}
}
Request build() {
HttpUrl resolve;
HttpUrl.Builder builder = this.urlBuilder;
if (builder != null) {
resolve = builder.build();
} else {
resolve = this.baseUrl.resolve(this.relativeUrl);
if (resolve == null) {
throw new IllegalArgumentException("Malformed URL. Base: " + this.baseUrl + ", Relative: " + this.relativeUrl);
}
}
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.requestBuilder.addHeader("Content-Type", mediaType.toString());
}
}
return this.requestBuilder.url(resolve).method(this.method, requestBody).build();
}
void setBody(RequestBody requestBody) {
this.body = requestBody;
}
void setRelativeUrl(Object obj) {
this.relativeUrl = obj.toString();
}
void addPart(MultipartBody.Part part) {
this.multipartBuilder.addPart(part);
}
private static void canonicalizeForPath(Buffer buffer, String str, int i, int i2, boolean z) {
Buffer buffer2 = 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))) {
buffer.writeUtf8CodePoint(codePointAt);
} else {
if (buffer2 == null) {
buffer2 = new Buffer();
}
buffer2.writeUtf8CodePoint(codePointAt);
while (!buffer2.exhausted()) {
int readByte = buffer2.readByte() & 255;
buffer.writeByte(37);
buffer.writeByte((int) HEX_DIGITS[(readByte >> 4) & 15]);
buffer.writeByte((int) HEX_DIGITS[readByte & 15]);
}
}
}
i += Character.charCount(codePointAt);
}
}
}