CallServerInterceptor.java 源代码
package com.mbridge.msdk.thrid.okhttp.internal.http;
import com.mbridge.msdk.thrid.okhttp.Interceptor;
import com.mbridge.msdk.thrid.okhttp.Request;
import com.mbridge.msdk.thrid.okhttp.Response;
import com.mbridge.msdk.thrid.okhttp.internal.Util;
import com.mbridge.msdk.thrid.okhttp.internal.connection.RealConnection;
import com.mbridge.msdk.thrid.okhttp.internal.connection.StreamAllocation;
import com.mbridge.msdk.thrid.okio.Buffer;
import com.mbridge.msdk.thrid.okio.BufferedSink;
import com.mbridge.msdk.thrid.okio.ForwardingSink;
import com.mbridge.msdk.thrid.okio.Okio;
import com.mbridge.msdk.thrid.okio.Sink;
import com.x8zs.plugin.apache.http.protocol.HTTP;
import java.io.IOException;
import java.net.ProtocolException;
public final class CallServerInterceptor implements Interceptor {
private final boolean forWebSocket;
static final class CountingSink extends ForwardingSink {
long successfulCount;
CountingSink(Sink sink) {
super(sink);
}
@Override
public void write(Buffer buffer, long j6) throws IOException {
super.write(buffer, j6);
this.successfulCount += j6;
}
}
public CallServerInterceptor(boolean z6) {
this.forWebSocket = z6;
}
@Override
public Response intercept(Interceptor.Chain chain) throws IOException {
Response build;
RealInterceptorChain realInterceptorChain = (RealInterceptorChain) chain;
HttpCodec httpStream = realInterceptorChain.httpStream();
StreamAllocation streamAllocation = realInterceptorChain.streamAllocation();
RealConnection realConnection = (RealConnection) realInterceptorChain.connection();
Request request = realInterceptorChain.request();
long currentTimeMillis = System.currentTimeMillis();
realInterceptorChain.eventListener().requestHeadersStart(realInterceptorChain.call());
httpStream.writeRequestHeaders(request);
realInterceptorChain.eventListener().requestHeadersEnd(realInterceptorChain.call(), request);
Response.Builder builder = null;
if (HttpMethod.permitsRequestBody(request.method()) && request.body() != null) {
if (HTTP.EXPECT_CONTINUE.equalsIgnoreCase(request.header(HTTP.EXPECT_DIRECTIVE))) {
httpStream.flushRequest();
realInterceptorChain.eventListener().responseHeadersStart(realInterceptorChain.call());
builder = httpStream.readResponseHeaders(true);
}
if (builder == null) {
realInterceptorChain.eventListener().requestBodyStart(realInterceptorChain.call());
CountingSink countingSink = new CountingSink(httpStream.createRequestBody(request, request.body().contentLength()));
BufferedSink buffer = Okio.buffer(countingSink);
request.body().writeTo(buffer);
buffer.close();
realInterceptorChain.eventListener().requestBodyEnd(realInterceptorChain.call(), countingSink.successfulCount);
} else if (!realConnection.isMultiplexed()) {
streamAllocation.noNewStreams();
}
}
httpStream.finishRequest();
if (builder == null) {
realInterceptorChain.eventListener().responseHeadersStart(realInterceptorChain.call());
builder = httpStream.readResponseHeaders(false);
}
Response build2 = builder.request(request).handshake(streamAllocation.connection().handshake()).sentRequestAtMillis(currentTimeMillis).receivedResponseAtMillis(System.currentTimeMillis()).build();
int code = build2.code();
if (code == 100) {
build2 = httpStream.readResponseHeaders(false).request(request).handshake(streamAllocation.connection().handshake()).sentRequestAtMillis(currentTimeMillis).receivedResponseAtMillis(System.currentTimeMillis()).build();
code = build2.code();
}
realInterceptorChain.eventListener().responseHeadersEnd(realInterceptorChain.call(), build2);
if (this.forWebSocket && code == 101) {
build = build2.newBuilder().body(Util.EMPTY_RESPONSE).build();
} else {
build = build2.newBuilder().body(httpStream.openResponseBody(build2)).build();
}
if ("close".equalsIgnoreCase(build.request().header(HTTP.CONN_DIRECTIVE)) || "close".equalsIgnoreCase(build.header(HTTP.CONN_DIRECTIVE))) {
streamAllocation.noNewStreams();
}
if ((code != 204 && code != 205) || build.body().contentLength() <= 0) {
return build;
}
throw new ProtocolException("HTTP " + code + " had non-zero Content-Length: " + build.body().contentLength());
}
}