package okhttp3; import java.io.Closeable; import okhttp3.Headers; import okhttp3.internal.connection.Exchange; /* loaded from: classes5.dex */ public final class Response implements Closeable { public final ResponseBody body; public volatile CacheControl cacheControl; public final Response cacheResponse; public final int code; public final Exchange exchange; public final Handshake handshake; public final Headers headers; public final String message; public final Response networkResponse; public final Response priorResponse; public final Protocol protocol; public final long receivedResponseAtMillis; public final Request request; public final long sentRequestAtMillis; public ResponseBody body() { return this.body; } public Response cacheResponse() { return this.cacheResponse; } public int code() { return this.code; } public Handshake handshake() { return this.handshake; } public Headers headers() { return this.headers; } public boolean isSuccessful() { int i = this.code; return i >= 200 && i < 300; } public String message() { return this.message; } public Response networkResponse() { return this.networkResponse; } public Response priorResponse() { return this.priorResponse; } public Protocol protocol() { return this.protocol; } public long receivedResponseAtMillis() { return this.receivedResponseAtMillis; } public Request request() { return this.request; } public long sentRequestAtMillis() { return this.sentRequestAtMillis; } public Response(Builder builder) { this.request = builder.request; this.protocol = builder.protocol; this.code = builder.code; this.message = builder.message; this.handshake = builder.handshake; this.headers = builder.headers.build(); this.body = builder.body; this.networkResponse = builder.networkResponse; this.cacheResponse = builder.cacheResponse; this.priorResponse = builder.priorResponse; this.sentRequestAtMillis = builder.sentRequestAtMillis; this.receivedResponseAtMillis = builder.receivedResponseAtMillis; this.exchange = builder.exchange; } public String header(String str) { return header(str, null); } public String header(String str, String str2) { String str3 = this.headers.get(str); return str3 != null ? str3 : str2; } public Builder newBuilder() { return new Builder(this); } public CacheControl cacheControl() { CacheControl cacheControl = this.cacheControl; if (cacheControl != null) { return cacheControl; } CacheControl parse = CacheControl.parse(this.headers); this.cacheControl = parse; return parse; } @Override // java.io.Closeable, java.lang.AutoCloseable public void close() { ResponseBody responseBody = this.body; if (responseBody == null) { throw new IllegalStateException("response is not eligible for a body and must not be closed"); } responseBody.close(); } public String toString() { return "Response{protocol=" + this.protocol + ", code=" + this.code + ", message=" + this.message + ", url=" + this.request.url() + '}'; } public static class Builder { public ResponseBody body; public Response cacheResponse; public int code; public Exchange exchange; public Handshake handshake; public Headers.Builder headers; public String message; public Response networkResponse; public Response priorResponse; public Protocol protocol; public long receivedResponseAtMillis; public Request request; public long sentRequestAtMillis; public Builder body(ResponseBody responseBody) { this.body = responseBody; return this; } public Builder code(int i) { this.code = i; return this; } public Builder handshake(Handshake handshake) { this.handshake = handshake; return this; } public void initExchange(Exchange exchange) { this.exchange = exchange; } public Builder message(String str) { this.message = str; return this; } public Builder protocol(Protocol protocol) { this.protocol = protocol; return this; } public Builder receivedResponseAtMillis(long j) { this.receivedResponseAtMillis = j; return this; } public Builder request(Request request) { this.request = request; return this; } public Builder sentRequestAtMillis(long j) { this.sentRequestAtMillis = j; return this; } public Builder() { this.code = -1; this.headers = new Headers.Builder(); } public Builder(Response response) { this.code = -1; this.request = response.request; this.protocol = response.protocol; this.code = response.code; this.message = response.message; this.handshake = response.handshake; this.headers = response.headers.newBuilder(); this.body = response.body; this.networkResponse = response.networkResponse; this.cacheResponse = response.cacheResponse; this.priorResponse = response.priorResponse; this.sentRequestAtMillis = response.sentRequestAtMillis; this.receivedResponseAtMillis = response.receivedResponseAtMillis; this.exchange = response.exchange; } public Builder header(String str, String str2) { this.headers.set(str, str2); return this; } public Builder addHeader(String str, String str2) { this.headers.add(str, str2); return this; } public Builder headers(Headers headers) { this.headers = headers.newBuilder(); return this; } public Builder networkResponse(Response response) { if (response != null) { checkSupportResponse("networkResponse", response); } this.networkResponse = response; return this; } public Builder cacheResponse(Response response) { if (response != null) { checkSupportResponse("cacheResponse", response); } this.cacheResponse = response; return this; } public final void checkSupportResponse(String str, Response response) { if (response.body != null) { throw new IllegalArgumentException(str + ".body != null"); } if (response.networkResponse != null) { throw new IllegalArgumentException(str + ".networkResponse != null"); } if (response.cacheResponse != null) { throw new IllegalArgumentException(str + ".cacheResponse != null"); } if (response.priorResponse == null) { return; } throw new IllegalArgumentException(str + ".priorResponse != null"); } public Builder priorResponse(Response response) { if (response != null) { checkPriorResponse(response); } this.priorResponse = response; return this; } public final void checkPriorResponse(Response response) { if (response.body != null) { throw new IllegalArgumentException("priorResponse.body != null"); } } public Response build() { if (this.request == null) { throw new IllegalStateException("request == null"); } if (this.protocol == null) { throw new IllegalStateException("protocol == null"); } if (this.code >= 0) { if (this.message == null) { throw new IllegalStateException("message == null"); } return new Response(this); } throw new IllegalStateException("code < 0: " + this.code); } } }