package com.bytedance.apm.agent.instrumentation.okhttp3;

import X.C51159es;
import X.C51169et;
import X.C55359le;
import X.PEG;
import X.Q1G;
import android.text.TextUtils;
import com.bytedance.apm.agent.instrumentation.transaction.TransactionState;
import com.bytedance.covode.number.Covode;
import java.io.IOException;
import okhttp3.Interceptor;
import okhttp3.MediaType;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.ResponseBody;

public class InterceptorImpl implements Interceptor {
    static {
        Covode.recordClassIndex(23062);
    }

    public class ResponseBodyWrapper extends ResponseBody {
        public PEG bufferedSource;
        public final Response response;
        public final ResponseBody responseBody;
        public long totalBytesRead;
        public final TransactionState transactionState;

        static {
            Covode.recordClassIndex(23063);
        }

        public long contentLength() {
            return this.responseBody.contentLength();
        }

        public MediaType contentType() {
            return this.responseBody.contentType();
        }

        public void close() {
            this.responseBody.close();
            end();
        }

        public void end() {
            if (!this.transactionState.isComplete()) {
                this.transactionState.setBytesReceived(this.totalBytesRead);
                MonitorRecorder.reportMonitorData(this.transactionState, this.response);
            }
        }

        public PEG source() {
            if (this.bufferedSource == null) {
                this.bufferedSource = C55359le.LIZ(source(this.responseBody.source()));
            }
            return this.bufferedSource;
        }

        private C51169et source(C51169et r2) {
            return new C51159es(r2) {
                static {
                    Covode.recordClassIndex(23064);
                }

                public void close() {
                    super.close();
                    ResponseBodyWrapper.this.end();
                }

                public long read(Q1G q1g, long j) {
                    long read = super.read(q1g, j);
                    if (read >= 0) {
                        ResponseBodyWrapper.this.totalBytesRead += read;
                    }
                    return read;
                }
            };
        }

        public ResponseBodyWrapper(Response response2, TransactionState transactionState2) {
            this.response = response2;
            this.responseBody = response2.body();
            this.transactionState = transactionState2;
        }
    }

    public Response intercept(Interceptor.Chain chain) {
        Request request = chain.request();
        String header = request.header("User-Agent");
        if (header != null && header.contains("tt")) {
            return chain.proceed(request);
        }
        TransactionState transactionState = new TransactionState();
        MonitorRecorder.recordRequest(request, transactionState);
        try {
            Response proceed = chain.proceed(request);
            MonitorRecorder.recordResponse(proceed, transactionState);
            if (transactionState.getReceivedBytes() >= 0 || TextUtils.isEmpty(proceed.header("Transfer-Encoding"))) {
                MonitorRecorder.reportMonitorData(transactionState, proceed);
                return proceed;
            }
            transactionState.addAssistData("Transfer-Encoding", proceed.header("Transfer-Encoding"));
            Response.Builder newBuilder = proceed.newBuilder();
            newBuilder.body(new ResponseBodyWrapper(proceed, transactionState));
            return newBuilder.build();
        } catch (IOException e) {
            MonitorRecorder.reportExceptionMonitor(transactionState, e);
            throw e;
        }
    }
}
