okhttp3源码初探
简介
okhttp3 是目前android平台最流行的网络访问框架,而目前大火的retrofit底层封装的也是OKHttp。所以可以这么说,okhttp是我们做android 开发的过程中绕不过的一座大山。
但是由于okhttp实在太易用了,几句代码就可以完成网络请求的发起,导致我们很少去了解okhttp底层去做了什么。但是俗话说的好“要知其然还要知其所以然”,然而我们现在“知其然”都做不到。所以接下来我们便去看看okhttp底层到底做了什么。
GET请求
使用
public class OkhttpTestActivity extends AppCompatActivity {
private static final String TAG = "OkhttpTestActivity";
private Button button;
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_okhttp_test);
button= (Button) findViewById(R.id.btn_send_get);
textView= (TextView) findViewById(R.id.tv_display);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
sendGetRequest();
}
});
}
private void sendGetRequest() {
String url = "http://wwww.baidu.com";
final OkHttpClient okHttpClient = new OkHttpClient();
final Request request = new Request.Builder()
.url(url)
.get()//默认就是GET请求,可以不写
.build();
Call call = okHttpClient.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
Log.d(TAG, "onFailure: ");
}
@Override
public void onResponse(Call call, Response response) throws IOException {
// Log.d(TAG, "onResponse: " + response.body().string());
final String content=response.body().string();
OkhttpTestActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
textView.setText(content);
}
});
}
});
}
}
这里就是最基本的一个基于okhttp的网络请求。
源码阅读
发起请求
这里我们选择call.enqueue,请求执行的时候作为整个源码阅读的起点,因为前面的构造方法都很简单,Request使用了Builder模式,然后再利用request构造了call对象。最后执行。
//okhttp3.RealCall
@Override public void enqueue(Callback responseCallback) {
synchronized (this) {
//判断该请求是否已经发起,如果已经发起,则抛出错误
if (executed) throw new IllegalStateException("Already Executed");
executed = true;
}
//这句话好像是用来监测内存泄露的,其实我也不太确定 O(∩_∩)O哈哈~
//反正不影响主流程,我们暂且跳过
captureCallStackTrace();
eventListener.callStart(this);
//真实开始执行请求
client.dispatcher().enqueue(new AsyncCall(responseCallback));
}
eventListener的由来
上面的代码很简单,不过这个eventListener的来源可是很值得探究的。这首先要从okhttp的对象的构建开始看起。
public OkHttpClient() {
this(new Builder());
}
//利用了Builder的模式,简化了okhttp的构建过程
OkHttpClient(Builder builder) {
//....
this.eventListenerFactory = builder.eventListenerFactory;
//....
}
public static final class Builder {
//....
EventListener.Factory eventListenerFactory;
//....
public Builder() {
//....
eventListenerFactory = EventListener.factory(EventListener.NONE);
//....
}
}
//okhttp3.EventListener
public static final EventListener NONE = new EventListener() {
};
static EventListener.Factory factory(final EventListener listener) {
return new EventListener.Factory() {
public EventListener create(Call call) {
return listener;
}
};
}
嗯么么,一顿操作猛如虎,一看战记0-5。上面的代码虽然多,但是很好懂,如果一切都采用默认值,那么最后okHttpClient对象最后会被赋予一个包含一个空的EventListener的EventListener.factory。当然我们可以在构建okHttpClient对象的时候传入我们自己的EventListener。那么接下来这里的okHttpClient中的EventListener又是怎么和call勾搭到一起的呢?
//okhttp3.OkHttpClient
@Override public Call newCall(Request request) {
return RealCall.newRealCall(this, request, false /* for web socket */);
}
//okhttp3.RealCall
static RealCall newRealCall(OkHttpClient client, Request originalRequest, boolean forWebSocket) {
// Safely publish the Call instance to the EventListener.
RealCall call = new RealCall(client, originalRequest, forWebSocket);
call.eventListener = client.eventListenerFactory().create(call);
return call;
}
好了,真相大白,也是在构建的时候勾搭到一起的。
那么我接下来继续看真正的网络请求
真正的网络请求
/** Ready async calls in the order they'll be run. */
private final Deque<AsyncCall> readyAsyncCalls = new ArrayDeque<>();
synchronized void enqueue(AsyncCall call) {
//如果队列中的请求小于maxRequests,并且正在运行的请求小于maxRequestsPerHost,则将其加入请求队列,并且立即执行。
if (runningAsyncCalls.size() < maxRequests && runningCallsForHost(call) < maxRequestsPerHost) {
runningAsyncCalls.add(call);
executorService().execute(call);
} else {
readyAsyncCalls.add(call);
}
}
这里通过代码可以看到okhttp 构建了一个CacheThreadPool的线程池,在请求较少时,直接丢入队列并执行。多的时候只丢入队列。那么我们看下这里线程池究竟都执行了什么任务。这里的call是AsyncCall的实例,而AsyncCall 实现了runnable接口,所以我们直接看run方法。但是AsyncCall自己本身并没有run方法,所以这里我们查看AsyncCall的父类NamedRunnable的run方法。
public abstract class NamedRunnable implements Runnable {
protected final String name;
public NamedRunnable(String format, Object... args) {
this.name = Util.format(format, args);
}
@Override public final void run() {
String oldName = Thread.currentThread().getName();
Thread.currentThread().setName(name);
try {
execute();
} finally {
Thread.currentThread().setName(oldName);
}
}
protected abstract void execute();
}
这里调用了抽象方法execute,所以我们去看AsyncCall中的实现
@Override protected void execute() {
boolean signalledCallback = false;
try {
//获取接口获取的内容,这里为同步方法
Response response = getResponseWithInterceptorChain();
if (retryAndFollowUpInterceptor.isCanceled()) {
signalledCallback = true;
responseCallback.onFailure(RealCall.this, new IOException("Canceled"));
} else {
signalledCallback = true;
responseCallback.onResponse(RealCall.this, response);
}
} catch (IOException e) {
if (signalledCallback) {
// Do not signal the callback twice!
Platform.get().log(INFO, "Callback failure for " + toLoggableString(), e);
} else {
eventListener.callFailed(RealCall.this, e);
responseCallback.onFailure(RealCall.this, e);
}
} finally {
client.dispatcher().finished(this);
}
}
这里其实只有一句重点,就是获取接口内容的那句,其他都是异常处理。唯一值得我们注意的可能就是最后再finally中执行了finished的方法,这里其实也就是调用了我们的回调中的finished方法,这样就确保了我们的finished的方法,肯定可以得到调用。
拦截器
Response getResponseWithInterceptorChain() throws IOException {
// Build a full stack of interceptors.
//此处的interceptors 便是如雷贯耳的拦截器
List<Interceptor> interceptors = new ArrayList<>();
//拦截器列表中添加用户自定义的拦截器,这里用户添加的拦截器可以拦截所有的请求,包括从缓存走的,不经过网络的请求
interceptors.addAll(client.interceptors());
//此拦截器,用于访问失败的时候进行重定向
interceptors.add(retryAndFollowUpInterceptor);
//这个拦截器,用户将用户的参数转换为网络参数。例如用户设置了body.contentType,
//则会在此拦截器中执行,requestBuilder.header("Content-Type", contentType.toString());
interceptors.add(new BridgeInterceptor(client.cookieJar()));
//缓存相关拦截器
interceptors.add(new CacheInterceptor(client.internalCache()));
//提前打开一个网络连接
interceptors.add(new ConnectInterceptor(client));
if (!forWebSocket) {
//添加用户自定义的网络拦截器,这里只能拦截真正的网络请求
interceptors.addAll(client.networkInterceptors());
}
//最后一个拦截器,用于从网络获取数据
interceptors.add(new CallServerInterceptor(forWebSocket));
//构建一个Chain 实例
Interceptor.Chain chain = new RealInterceptorChain(interceptors, null, null, null, 0,
originalRequest, this, eventListener, client.connectTimeoutMillis(),
client.readTimeoutMillis(), client.writeTimeoutMillis());
//调用chain的proceed方法,并传入,我们最初构建的request。这里的originalRequest,就是我们
//Call call = okHttpClient.newCall(request); 的时候,传递的request。
return chain.proceed(originalRequest);
}
好了,接下来,我们就看下最初的Chain 的相关代码
@Override public Response proceed(Request request) throws IOException {
return proceed(request, streamAllocation, httpCodec, connection);
}
public Response proceed(Request request, StreamAllocation streamAllocation, HttpCodec httpCodec,
RealConnection connection) throws IOException {
//......
// Call the next interceptor in the chain.
// 构建一个新的chain ,并将其index+1
RealInterceptorChain next = new RealInterceptorChain(interceptors, streamAllocation, httpCodec,
connection, index + 1, request, call, eventListener, connectTimeout, readTimeout,
writeTimeout);
//取出第一个拦截器
Interceptor interceptor = interceptors.get(index);
//执行拦截器的intercept方法并将新的chain方法传入。
Response response = interceptor.intercept(next);
//....
return response;
}
RetryAndFollowUpInterceptor拦截器
这里,我们假设用户没有传入自定义的拦截器,那么接下来执行的拦截器,就是RetryAndFollowUpInterceptor,接下来我们就查看一下RetryAndFollowUpInterceptor的相关源码
@Override public Response intercept(Chain chain) throws IOException {
RealInterceptorChain realChain = (RealInterceptorChain) chain;
//.......
while (true) {
//这里是一个死循环,okhttp3会不断的重试访问,除非访问成功,或者取消访问。或者抛出其他异常
if (canceled) {
streamAllocation.release();
throw new IOException("Canceled");
}
//.....
//重点看这里。这里又去调用了,一开始的realChain的proceed方法。从这里大家可以看出来,这里就是典型的责任链模式,只不过
//和模板责任链不太相同的是,就是这里专门把传递工作到下一级的方法抽离出来由一个统一的方法去处理
response = realChain.proceed(request, streamAllocation, null, null);
releaseConnection = false;
//.....
// Attach the prior response if it exists. Such responses never have a body.
if (priorResponse != null) {
response = response.newBuilder()
.priorResponse(priorResponse.newBuilder()
.body(null)
.build())
.build();
}
Request followUp = followUpRequest(response, streamAllocation.route());
if (followUp == null) {
if (!forWebSocket) {
streamAllocation.release();
}
return response;
}
//....
request = followUp;
priorResponse = response;
}
}
其实,几乎每个拦截器都是如此的处理,所以接下来,我们只需要看我们关心的具体的拦截器中的具体业务代码即可。
BridgeInterceptor拦截器
@Override public Response intercept(Chain chain) throws IOException {
Request userRequest = chain.request();
Request.Builder requestBuilder = userRequest.newBuilder();
RequestBody body = userRequest.body();
//如果body 不为null的话,则将参数转换为网络参数
if (body != null) {
MediaType contentType = body.contentType();
if (contentType != null) {
requestBuilder.header("Content-Type", contentType.toString());
}
long contentLength = body.contentLength();
if (contentLength != -1) {
requestBuilder.header("Content-Length", Long.toString(contentLength));
requestBuilder.removeHeader("Transfer-Encoding");
} else {
requestBuilder.header("Transfer-Encoding", "chunked");
requestBuilder.removeHeader("Content-Length");
}
}
//将host转换为网络参数
if (userRequest.header("Host") == null) {
requestBuilder.header("Host", hostHeader(userRequest.url(), false));
}
//将connection转换为网络参数
if (userRequest.header("Connection") == null) {
requestBuilder.header("Connection", "Keep-Alive");
}
// If we add an "Accept-Encoding: gzip" header field we're responsible for also decompressing
// the transfer stream.
boolean transparentGzip = false;
//添加Accept-Encoding默认参数
if (userRequest.header("Accept-Encoding") == null && userRequest.header("Range") == null) {
transparentGzip = true;
requestBuilder.header("Accept-Encoding", "gzip");
}
List<Cookie> cookies = cookieJar.loadForRequest(userRequest.url());
//转换cookies为网络参数
if (!cookies.isEmpty()) {
requestBuilder.header("Cookie", cookieHeader(cookies));
}
//添加User-Agent默认值
if (userRequest.header("User-Agent") == null) {
requestBuilder.header("User-Agent", Version.userAgent());
}
//将请求向下传递
Response networkResponse = chain.proceed(requestBuilder.build());
//解析返回结果的cookie
HttpHeaders.receiveHeaders(cookieJar, userRequest.url(), networkResponse.headers());
Response.Builder responseBuilder = networkResponse.newBuilder()
.request(userRequest);
//将网络返回的headers转换为application Headers.
if (transparentGzip
&& "gzip".equalsIgnoreCase(networkResponse.header("Content-Encoding"))
&& HttpHeaders.hasBody(networkResponse)) {
GzipSource responseBody = new GzipSource(networkResponse.body().source());
Headers strippedHeaders = networkResponse.headers().newBuilder()
.removeAll("Content-Encoding")
.removeAll("Content-Length")
.build();
responseBuilder.headers(strippedHeaders);
String contentType = networkResponse.header("Content-Type");
responseBuilder.body(new RealResponseBody(contentType, -1L, Okio.buffer(responseBody)));
}
return responseBuilder.build();
}
其实这个函数的主要功能就如同它的自我描述一样,就是把application header 转换为网络 header.
CacheInterceptor拦截器
@Override public Response intercept(Chain chain) throws IOException {
//取出缓存的response
Response cacheCandidate = cache != null
? cache.get(chain.request())
: null;
long now = System.currentTimeMillis();
//构建缓存策略
CacheStrategy strategy = new CacheStrategy.Factory(now, chain.request(), cacheCandidate).get();
Request networkRequest = strategy.networkRequest;
Response cacheResponse = strategy.cacheResponse;
if (cache != null) {
cache.trackResponse(strategy);
}
if (cacheCandidate != null && cacheResponse == null) {
closeQuietly(cacheCandidate.body()); // The cache candidate wasn't applicable. Close it.
}
// If we're forbidden from using the network and the cache is insufficient, fail.
//如果网络请求为空,并且cache也为空的话,则直接返回504
if (networkRequest == null && cacheResponse == null) {
return new Response.Builder()
.request(chain.request())
.protocol(Protocol.HTTP_1_1)
.code(504)
.message("Unsatisfiable Request (only-if-cached)")
.body(Util.EMPTY_RESPONSE)
.sentRequestAtMillis(-1L)
.receivedResponseAtMillis(System.currentTimeMillis())
.build();
}
// If we don't need the network, we're done.
//网络请求为空,cache不会空,则返回cache
if (networkRequest == null) {
return cacheResponse.newBuilder()
.cacheResponse(stripBody(cacheResponse))
.build();
}
Response networkResponse = null;
try {
networkResponse = chain.proceed(networkRequest);
} finally {
// If we're crashing on I/O or otherwise, don't leak the cache body.
if (networkResponse == null && cacheCandidate != null) {
closeQuietly(cacheCandidate.body());
}
}
// If we have a cache response too, then we're doing a conditional get.
if (cacheResponse != null) {
//如果网络数据和上次相比没有修改,则直接返回缓存
if (networkResponse.code() == HTTP_NOT_MODIFIED) {
Response response = cacheResponse.newBuilder()
.headers(combine(cacheResponse.headers(), networkResponse.headers()))
.sentRequestAtMillis(networkResponse.sentRequestAtMillis())
.receivedResponseAtMillis(networkResponse.receivedResponseAtMillis())
.cacheResponse(stripBody(cacheResponse))
.networkResponse(stripBody(networkResponse))
.build();
networkResponse.body().close();
// Update the cache after combining headers but before stripping the
// Content-Encoding header (as performed by initContentStream()).
cache.trackConditionalCacheHit();
cache.update(cacheResponse, response);
return response;
} else {
closeQuietly(cacheResponse.body());
}
}
Response response = networkResponse.newBuilder()
.cacheResponse(stripBody(cacheResponse))
.networkResponse(stripBody(networkResponse))
.build();
if (cache != null) {
if (HttpHeaders.hasBody(response) && CacheStrategy.isCacheable(response, networkRequest)) {
// Offer this request to the cache.
//缓存这次网络请求
CacheRequest cacheRequest = cache.put(response);
return cacheWritingResponse(cacheRequest, response);
}
if (HttpMethod.invalidatesCache(networkRequest.method())) {
try {
cache.remove(networkRequest);
} catch (IOException ignored) {
// The cache cannot be written.
}
}
}
return response;
}
ConnectInterceptor
@Override public Response intercept(Chain chain) throws IOException {
RealInterceptorChain realChain = (RealInterceptorChain) chain;
Request request = realChain.request();
StreamAllocation streamAllocation = realChain.streamAllocation();
// We need the network to satisfy this request. Possibly for validating a conditional GET.
boolean doExtensiveHealthChecks = !request.method().equals("GET");
//httpCodec是一个流的管理接口,Http2Codec实现了这个接口。在这里的时候已经打开了连接
HttpCodec httpCodec = streamAllocation.newStream(client, chain, doExtensiveHealthChecks);
//这里只是单纯的将连接返回。
RealConnection connection = streamAllocation.connection();
return realChain.proceed(request, streamAllocation, httpCodec, connection);
}
CallServerInterceptor
@Override public Response intercept(Chain chain) throws IOException {
RealInterceptorChain realChain = (RealInterceptorChain) chain;
HttpCodec httpCodec = realChain.httpStream();
StreamAllocation streamAllocation = realChain.streamAllocation();
//获得在上一个拦截器中打开的connection
RealConnection connection = (RealConnection) realChain.connection();
Request request = realChain.request();
long sentRequestMillis = System.currentTimeMillis();
//调用监听事件
realChain.eventListener().requestHeadersStart(realChain.call());
//将headers写入网络流
httpCodec.writeRequestHeaders(request);
//打开监听事件
realChain.eventListener().requestHeadersEnd(realChain.call(), request);
Response.Builder responseBuilder = null;
//如果请求不为get (get请求不包含request body),并且request.body不为空
if (HttpMethod.permitsRequestBody(request.method()) && request.body() != null) {
// If there's a "Expect: 100-continue" header on the request, wait for a "HTTP/1.1 100
// Continue" response before transmitting the request body. If we don't get that, return
// what we did get (such as a 4xx response) without ever transmitting the request body.
//如果请求包含“Expect: 100-continue”,则先判断服务器是否愿意接收消息,如果不愿意,则返回错误码
if ("100-continue".equalsIgnoreCase(request.header("Expect"))) {
//发出之前构建的request
httpCodec.flushRequest();
realChain.eventListener().responseHeadersStart(realChain.call());
//读取服务器传递来的消息
responseBuilder = httpCodec.readResponseHeaders(true);
}
if (responseBuilder == null) {
// Write the request body if the "Expect: 100-continue" expectation was met.
realChain.eventListener().requestBodyStart(realChain.call());
long contentLength = request.body().contentLength();
CountingSink requestBodyOut =
new CountingSink(httpCodec.createRequestBody(request, contentLength));
BufferedSink bufferedRequestBody = Okio.buffer(requestBodyOut);
request.body().writeTo(bufferedRequestBody);
bufferedRequestBody.close();
realChain.eventListener()
.requestBodyEnd(realChain.call(), requestBodyOut.successfulCount);
} else if (!connection.isMultiplexed()) {
// If the "Expect: 100-continue" expectation wasn't met, prevent the HTTP/1 connection
// from being reused. Otherwise we're still obligated to transmit the request body to
// leave the connection in a consistent state.
streamAllocation.noNewStreams();
}
}
将消息流推出
httpCodec.finishRequest();
if (responseBuilder == null) {
realChain.eventListener().responseHeadersStart(realChain.call());
//读取服务器返回的消息
responseBuilder = httpCodec.readResponseHeaders(false);
}
//构建response消息体
Response response = responseBuilder
.request(request)
.handshake(streamAllocation.connection().handshake())
.sentRequestAtMillis(sentRequestMillis)
.receivedResponseAtMillis(System.currentTimeMillis())
.build();
int code = response.code();
if (code == 100) {
// server sent a 100-continue even though we did not request one.
// try again to read the actual response
responseBuilder = httpCodec.readResponseHeaders(false);
response = responseBuilder
.request(request)
.handshake(streamAllocation.connection().handshake())
.sentRequestAtMillis(sentRequestMillis)
.receivedResponseAtMillis(System.currentTimeMillis())
.build();
code = response.code();
}
realChain.eventListener()
.responseHeadersEnd(realChain.call(), response);
if (forWebSocket && code == 101) {
// Connection is upgrading, but we need to ensure interceptors see a non-null response body.
response = response.newBuilder()
.body(Util.EMPTY_RESPONSE)
.build();
} else {
response = response.newBuilder()
.body(httpCodec.openResponseBody(response))
.build();
}
if ("close".equalsIgnoreCase(response.request().header("Connection"))
|| "close".equalsIgnoreCase(response.header("Connection"))) {
streamAllocation.noNewStreams();
}
if ((code == 204 || code == 205) && response.body().contentLength() > 0) {
throw new ProtocolException(
"HTTP " + code + " had non-zero Content-Length: " + response.body().contentLength());
}
return response;
}