OkHttp是一个高效的Http客户端,有如下的特点:
1,支持HTTP2/SPDY黑科技(主要是多路复用)
socket自动选择最好路线,并支持自动重连
拥有自动维护的socket连接池,减少握手次数
拥有队列线程池,轻松写并发
拥有Interceptors轻松处理请求与响应(比如透明GZIP压缩,LOGGING)
基于Headers的缓存策略
一,控制流程学习
同步用法
client = new OkHttpClient();
Request request = new Request.Builder()
.url("https://www.baidu.com")
.build();
Response response = null;
try {
response = client.newCall(request).execute();
} catch (IOException e) {
e.printStackTrace();
}
异步请求
OkHttpClient okHttpClient = new OkHttpClient();//1.定义一个client
Request request = new Request.Builder().url("http://www.baidu.com").build();//2.定义一个request
Call call = okHttpClient.newCall(request);//3.使用client去请求
call.enqueue(new Callback() {//4.回调方法
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
String result = response.body().string();//5.获得网络数据
System.out.println(result);
}
});
这是整个使用的入口,new OkHttpClient()从这里点进去源码来看,基本okhttp所有的配置,属性等等一系列全都放到了这个类中,并且封装到了内部类Builder中,创建client的时候可以直接new 出来,他默认会初始化一个build,也可以自定义配置创建.
public OkHttpClient() {
this(new Builder());
}
仔细的来看一下这些配置都是写什么东西
-------------------------------------------------------------------------------------------
OkHttpClient(Builder builder) {
//这个是okhttp的一个重要模块,分发器
this.dispatcher = builder.dispatcher;
//代理模块
this.proxy = builder.proxy;
//协议集合
this.protocols = builder.protocols;
//链接规格
this.connectionSpecs = builder.connectionSpecs;
//应用层的拦截器
this.interceptors = Util.immutableList(builder.interceptors);
//网络层的拦截器
this.networkInterceptors = Util.immutableList(builder.networkInterceptors);
//此乃链接的回调(包括dns开始,结束,链接开始等...一系列回调)
this.eventListenerFactory = builder.eventListenerFactory;
//代理选择
this.proxySelector = builder.proxySelector;
//cookie,默认是不添加cookies的
this.cookieJar = builder.cookieJar;
//缓存
this.cache = builder.cache;
//内部缓存
this.internalCache = builder.internalCache;
//使用一个单例工场来生产socket
this.socketFactory = builder.socketFactory;
//tls证书,以及tls一系列的初始化
boolean isTLS = false;
for (ConnectionSpec spec : connectionSpecs) {
isTLS = isTLS || spec.isTls();
}
if (builder.sslSocketFactory != null || !isTLS) {
this.sslSocketFactory = builder.sslSocketFactory;
this.certificateChainCleaner = builder.certificateChainCleaner;
} else {
X509TrustManager trustManager = systemDefaultTrustManager();
this.sslSocketFactory = systemDefaultSslSocketFactory(trustManager);
this.certificateChainCleaner = CertificateChainCleaner.get(trustManager);
}
this.hostnameVerifier = builder.hostnameVerifier;
this.certificatePinner = builder.certificatePinner.withCertificateChainCleaner(
certificateChainCleaner);
this.proxyAuthenticator = builder.proxyAuthenticator;
this.authenticator = builder.authenticator;
this.connectionPool = builder.connectionPool;
this.dns = builder.dns;
this.followSslRedirects = builder.followSslRedirects;
this.followRedirects = builder.followRedirects;
this.retryOnConnectionFailure = builder.retryOnConnectionFailure;
this.connectTimeout = builder.connectTimeout;
this.readTimeout = builder.readTimeout;
this.writeTimeout = builder.writeTimeout;
this.pingInterval = builder.pingInterval;
if (interceptors.contains(null)) {
throw new IllegalStateException("Null interceptor: " + interceptors);
}
if (networkInterceptors.contains(null)) {
throw new IllegalStateException("Null network interceptor: " + networkInterceptors);
}
}
再看Request,同样也是用build封装了一系列参数来创建一个请求实例
public static class Builder {
HttpUrl url;
String method;
Headers.Builder headers;
RequestBody body;
Object tag;
..........
}
然后是重要的执行
client.newCall(request).execute();
首先创建Call,newcall()方法如下
@Override public Call newCall(Request request) {
return RealCall.newRealCall(this, request, false);
}
----------------
forWebSocket
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;
}
------------
private RealCall(OkHttpClient client, Request originalRequest, boolean forWebSocket) {
this.client = client;
this.originalRequest = originalRequest;
this.forWebSocket = forWebSocket;
this.retryAndFollowUpInterceptor = new RetryAndFollowUpInterceptor(client, forWebSocket);
}
RealCall是一个核心的机制类,该类将构造方法私有化,通过newCall的静态方法来i初始化一个对象,初始化的时候保存了client 对象,originalRequest对象,forWebSocket 默认未false,这是长链接的标示,okhttp本身是可以来实现长链接的,使用WebSocketCall来操作,retryAndFollowUpInterceptor 是默认配置的一个重试和重定向的策略
然后就是最重要的,来看它是如何执行一个请求
这是同步请求执行的方法
首先它是线程安全的,同一时刻只能有一个线程来操作这个call对象,并且该对象只能被执行一次(executed变量未true的时候会抛出一个异常).
@Override public Response execute() throws IOException {
synchronized (this) {
if (executed) throw new IllegalStateException("Already Executed");
executed = true;
}
captureCallStackTrace();
eventListener.callStart(this);
try {
client.dispatcher().executed(this);
Response result = getResponseWithInterceptorChain();
if (result == null) throw new IOException("Canceled");
return result;
} catch (IOException e) {
eventListener.callFailed(this, e);
throw e;
} finally {
client.dispatcher().finished(this);
}
}
当将executed变量设置未true之后会执行一个captureCallStackTrace()方法,然后执行开始的回调 eventListener.callStart(this),然后走到最关键的 client.dispatcher().executed(this);有client中初始化的分发器开始执行这个方法
然后来看这个分发器Dispatcher
Dispatcher中有以下几个重要的东西
//最大的执行数量
private int maxRequests = 64;
//单个host最大执行的数量
private int maxRequestsPerHost = 5;
private @Nullable Runnable idleCallback;
private @Nullable ExecutorService executorService;
//这三个是同步请求与异步请求的双端队列
private final Deque<AsyncCall> readyAsyncCalls = new ArrayDeque<>();
private final Deque<AsyncCall> runningAsyncCalls = new ArrayDeque<>();
private final Deque<RealCall> runningSyncCalls = new ArrayDeque<>();
我们看Dispatcher的executed(this)方法发生了神们事情
很简单,直接将该同步请求加入到同步请求的队列
/** Used by {@code Call#execute} to signal it is in-flight. */
synchronized void executed(RealCall call) {
runningSyncCalls.add(call);
}
然后执行了getResponseWithInterceptorChain()方法,该方法也简单,首先搞一个拦截器的集合,先把一系列的系统拦截器放进去,然后创建一个chain对象,包括一些超时啊,读写啊一系列的配置...最关键的是这个chain对象
Response getResponseWithInterceptorChain() throws IOException {
// Build a full stack of interceptors.
List<Interceptor> interceptors = new ArrayList<>();
interceptors.addAll(client.interceptors());
interceptors.add(retryAndFollowUpInterceptor);
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));
Interceptor.Chain chain = new RealInterceptorChain(interceptors, null, null, null, 0,
originalRequest, this, eventListener, client.connectTimeoutMillis(),
client.readTimeoutMillis(), client.writeTimeoutMillis());
return chain.proceed(originalRequest);
}
来看一下这个chain对象是干什们的 chain.proceed(originalRequest)该方法是关键,RealInterceptorChain是实现了chain接口的一个实现类,
首先做了一堆判断然后就是把拦截器执行一遍,最后返回相应体
public Response proceed(Request request, StreamAllocation streamAllocation, HttpCodec httpCodec,
RealConnection connection) throws IOException {
......................
// Call the next interceptor in the chain.
RealInterceptorChain next = new RealInterceptorChain(interceptors, streamAllocation, httpCodec,
connection, index + 1, request, call, eventListener, connectTimeout, readTimeout,
writeTimeout);
Interceptor interceptor = interceptors.get(index);
Response response = interceptor.intercept(next);
........................
return response;
}
然后继续执行 execute()方法,同步请求直接返回结果,并执行finally中的方法 client.dispatcher().finished(this);
来看一下这个方法中发生了什么事情
首先dispatcher会更具call的类型去调用该方法(把不同的队列传递到该方法)
这个方法做了以下一些事情
1,将call从队列中移除
2如果是异步的话会走一个promoteCalls()方法去搞一些事情
3调用runningCallsCount方法来刷新正在执行的call的数量runningCallsCount,实质是正在执行的异步与同步请求的和
4做一个判断回调一下idleCallback.run()方法.
private <T> void finished(Deque<T> calls, T call, boolean promoteCalls) {
int runningCallsCount;
Runnable idleCallback;
synchronized (this) {
if (!calls.remove(call)) throw new AssertionError("Call wasn't in-flight!");
if (promoteCalls) promoteCalls();
runningCallsCount = runningCallsCount();
idleCallback = this.idleCallback;
}
if (runningCallsCount == 0 && idleCallback != null) {
idleCallback.run();
}
}
基本一个同步请求就是这样还是很简单的,异步请求小伟复杂一点,回去执行一个promoteCalls()方法
来看该方法高了什么事情
private void promoteCalls() {
if (runningAsyncCalls.size() >= maxRequests) return; // Already running max capacity.
if (readyAsyncCalls.isEmpty()) return; // No ready calls to promote.
for (Iterator<AsyncCall> i = readyAsyncCalls.iterator(); i.hasNext(); ) {
AsyncCall call = i.next();
if (runningCallsForHost(call) < maxRequestsPerHost) {
i.remove();
runningAsyncCalls.add(call);
executorService().execute(call);
}
if (runningAsyncCalls.size() >= maxRequests) return; // Reached max capacity.
}
}
runningAsyncCalls 正在跑的异步call队列
readyAsyncCalls 准备跑的异步call队列
首先做了一波队列的判断,当正在跑的队列满了,或者准备跑的队列是空的,返回return,然后开始遍历并执行异步请求待执行集合readyAsyncCalls 的请求.
然后来看那异步请求是如何的一个流程
@Override public void enqueue(Callback responseCallback) {
synchronized (this) {
if (executed) throw new IllegalStateException("Already Executed");
executed = true;
}
captureCallStackTrace();
eventListener.callStart(this);
client.dispatcher().enqueue(new AsyncCall(responseCallback));
}
与同步相同,显示执行一堆判断,回调.重点是最后一句 client.dispatcher().enqueue(new AsyncCall(responseCallback));
dispatcher.enqueue代码如下
因为okhttp默认对请求做了64/5的限制,所以进行了一波判断,如果条件符合,就将其加入到正在跑得异步队列runningAsyncCalls,然后交给 executorService()去执行该call,当然如果runningAsyncCalls队列已经满了就交给readyAsyncCalls去等待执行
synchronized void enqueue(AsyncCall call) {
if (runningAsyncCalls.size() < maxRequests && runningCallsForHost(call) < maxRequestsPerHost) {
runningAsyncCalls.add(call);
executorService().execute(call);
} else {
readyAsyncCalls.add(call);
}
}
executorService()方法返回一个线程池,
new ThreadPoolExecutor(0, Integer.MAX_VALUE, 60, TimeUnit.SECONDS,
new SynchronousQueue<Runnable>(), Util.threadFactory("OkHttp Dispatcher", false));
AsyncCall 本身就是Runnable的子类,这是其父类的run方法,最终会调用 execute();方法去搞事情
@Override public final void run() {
String oldName = Thread.currentThread().getName();
Thread.currentThread().setName(name);
try {
execute();
} finally {
Thread.currentThread().setName(oldName);
}
}
再看AsyncCall 的execute方法干了什么事情
同样调用getResponseWithInterceptorChain()把拦截器执行一遍拿到response ,执行重试重定向等等各种回调,最后任然需要finished这个call,走的方法和同步的差不多,不过传递的是异步的calls集合,并且参数串true.
@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);
}
}
到此整体一个执行流程大概是这个样子,首先创建okhttpClient对象,在构造中初始化一系列相关的配置参数,然后创建一个Request,request封装一些列该请求需要的配置等等,然后在创建一个call,实则未realcall,当调用call的execute,或者enqueue参数的时候,会将该call添加到相应的队列中,因为异步链接有64-5的限制,所以异步操作会有俩个队列,正在执行队列,等待执行队列,之后会调用chain的方法去执行一个个拦截器,okhttp的一系列机制都是拦截器去实现的,包括缓存机制,重连机制,连接池等等,dispater其实就是整个大流程的控制器,如果是异步请求的话dispater会通过一个线程池来执行该请求,而异步请求的拦截器也是在具体执行该线程的时候去调用,最后根据请求的最终结果来来finshed该请求,实则就是将其从相应的队列中移除掉.
二,拦截器学习
然而上面知识okhttp的一个大体控制执行流程,真真的细节,核心是chain的执行,当dispter执行execute方法时并未发生真真的网络请求,只是将请求call放入请求队列,我认为一下代码才是okhttp的真真核心代码,即dispater将call放入相应的队列之后执行方法,而该端代码最核心的又应该是proceed()方法,okhttp的核心功能(缓存,连接池等)就是中间的虚线部分.
Response getResponseWithInterceptorChain() throws IOException {
// Build a full stack of interceptors.
List<Interceptor> interceptors = new ArrayList<>();
//此处是用户自定义的拦截器(应用层面的拦截器)
interceptors.addAll(client.interceptors());
-------------------------
interceptors.add(retryAndFollowUpInterceptor);
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));
------------------------------
Interceptor.Chain chain = new RealInterceptorChain(interceptors, null, null, null, 0,
originalRequest, this, eventListener, client.connectTimeoutMillis(),
client.readTimeoutMillis(), client.writeTimeoutMillis());
return chain.proceed(originalRequest);
}
okhttp的拦截器有俩种,一下是俩种拦截器的区别,一般应用层面的拦截器用的偏多,比如log,cokies,统一参数,加密等,网络层面的我只能理解可能重定向会使用,其他的不理解,望大佬指点
Application interceptors
不需要去关心中发生的重定向和重试操作。
只会被调用一次,即使这个响应是从缓存中获取的。
只关注最原始的请求,不去关系请求的资源是否发生了改变,我只关注最后的 response 结果而已。
因为是第一个被执行的拦截器,因此它有权决定了是否要调用其他拦截,也就是 Chain.proceed() 方法是否要被执行。
.因为是第一个被执行的拦截器,因此它有可以多次调用 Chain.proceed() 方法,其实也就是相当与重新请求的作用了。
Network Interceptors
因为 NetworkInterceptor 是排在第 6 个拦截器中,因此会经过 RetryAndFollowup 进行失败重试或者重定向,因此可以操作中间的 resposne。
这个我不知道是什么意思,望大神指点。TODO
观察数据在网络中的传输,具体我也不太清除是干嘛的。TODO
因为它排在 ConnectInterceptor 后执行,因此返回执行这个 request 请求的 Connection 连接。
RealInterceptorChain这个类是拦截器的核心执行,该类的主要作用就是把拦截器一个个的执行,通过proceed方法开始执行,并且判断index 的数量来循环的执行拦截器,直到全部执行完毕.
Interceptor.Chain chain = new RealInterceptorChain(interceptors, null, null, null, 0,
originalRequest, this, eventListener, client.connectTimeoutMillis(),
client.readTimeoutMillis(), client.writeTimeoutMillis());
return chain.proceed(originalRequest);
---------------------------------------------------------------------------
public Response proceed(Request request, StreamAllocation streamAllocation, HttpCodec httpCodec,
RealConnection connection) throws IOException {
...............
// Call the next interceptor in the chain.
RealInterceptorChain next = new RealInterceptorChain(interceptors, streamAllocation, httpCodec,
connection, index + 1, request, call, eventListener, connectTimeout, readTimeout,
writeTimeout);
Interceptor interceptor = interceptors.get(index);
Response response = interceptor.intercept(next);
..............
return response;
}
(1)retryAndFollowUpInterceptor 重试重定向机制
该对象在创建call的时候被初始化,并且持有client的对象,关于其相关配置都是在client的build对象中配置
intercept(Chain chain) 该方法是每个拦截器的核心方法,来看retryAndFollowUpInterceptor 的intercept方法做了神们
@Override public Response intercept(Chain chain) throws IOException {
//该方法获取到了我们最初的那个请求体
Request request = chain.request();
//转成RealInterceptorChain,获取到该call对象
RealInterceptorChain realChain = (RealInterceptorChain) chain;
Call call = realChain.call();
//监听器
EventListener eventListener = realChain.eventListener();
//创建StreamAllocation对象,把请求地址封装到一个Address对象中
StreamAllocation streamAllocation = new StreamAllocation(client.connectionPool(),
createAddress(request.url()), call, eventListener, callStackTrace);
this.streamAllocation = streamAllocation;
//重试次数
int followUpCount = 0;
Response priorResponse = null;
//StreamAllocation是用来建立执行HTTP请求所需网络设施的组件
while (true) {
//一个安全检查,如果该请求已经被取消,就streamAllocation释放链接
if (canceled) {
streamAllocation.release();
throw new IOException("Canceled");
}
Response response;
boolean releaseConnection = true;
//接下来会通过RealInterceptorChain的proceed方法处理请求,在请求的过程中将releaseConnection设置为false,请求的时候一旦发生异常,releaseConnection 就设置未true,StreamAllocation就会释放掉链接
try {
response = realChain.proceed(request, streamAllocation, null, null);
releaseConnection = false;
} catch (RouteException e) {
// The attempt to connect via a route failed. The request will not have been sent.
if (!recover(e.getLastConnectException(), streamAllocation, false, request)) {
throw e.getLastConnectException();
}
releaseConnection = false;
continue;
} catch (IOException e) {
// An attempt to communicate with a server failed. The request may have been sent.
boolean requestSendStarted = !(e instanceof ConnectionShutdownException);
if (!recover(e, streamAllocation, requestSendStarted, request)) throw e;
releaseConnection = false;
continue;
} finally {
// We're throwing an unchecked exception. Release any resources.
if (releaseConnection) {
streamAllocation.streamFailed(null);
streamAllocation.release();
}
}
//做了一个空判断
if (priorResponse != null) {
response = response.newBuilder()
.priorResponse(priorResponse.newBuilder()
.body(null)
.build())
.build();
}
//如果一切顺利,前面的执行没有发生异常,就会走followUpRequest方法去处理重定向
Request followUp = followUpRequest(response, streamAllocation.route());
//followUp 为处理重定向之后的结果,如果为空说明该请求不需要进行重定向,直接返回response即可,如果不为空说明是需要进行重定向的,继续往下走
if (followUp == null) {
if (!forWebSocket) {
streamAllocation.release();
}
return response;
}
//回收资源
closeQuietly(response.body());
//如果重试次数达到最大
if (++followUpCount > MAX_FOLLOW_UPS) {
streamAllocation.release();
throw new ProtocolException("Too many follow-up requests: " + followUpCount);
}
//安全检查
if (followUp.body() instanceof UnrepeatableRequestBody) {
streamAllocation.release();
throw new HttpRetryException("Cannot retry streamed HTTP body", response.code());
}
//判断是否为同一个请求,一般重定向的时候url会发生变化,不视为同一个请求,所以释放streamAllocation,并重新创建一个streamAllocation进行请求
if (!sameConnection(response, followUp.url())) {
streamAllocation.release();
streamAllocation = new StreamAllocation(client.connectionPool(),
createAddress(followUp.url()), call, eventListener, callStackTrace);
this.streamAllocation = streamAllocation;
} else if (streamAllocation.codec() != null) {
throw new IllegalStateException("Closing the body of " + response
+ " didn't close its backing stream. Bad interceptor?");
}
//更新请求提与返回体
request = followUp;
priorResponse = response;
}
}
总体来说就是在该拦截器中会创建请求需要的连接池,开启循环去执行请求,调用chain去把其他的拦截器 执行一遍获取到返回体,真真的请求不再此地发生,然后更具该返回提去判断是否需要去执行重定向,如果不需要,直接返回该返回体,如果需要就需要重新请求,重新执行该循环中的流程,知道最大次数用完或者不再需要重定向的时候再返回相应体.该拦截器不发生网络请求,只进行相关的重试和重定向的策略执行.
(2)BridgeInterceptor
然后我们再来看BridgeInterceptor这个拦截器,这个拦截器其实简单,完善了一系列的请求头
@Override public Response intercept(Chain chain) throws IOException {
Request userRequest = chain.request();
Request.Builder requestBuilder = userRequest.newBuilder();
RequestBody body = userRequest.body();
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");
}
}
if (userRequest.header("Host") == null) {
requestBuilder.header("Host", hostHeader(userRequest.url(), false));
}
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;
if (userRequest.header("Accept-Encoding") == null && userRequest.header("Range") == null) {
transparentGzip = true;
requestBuilder.header("Accept-Encoding", "gzip");
}
List<Cookie> cookies = cookieJar.loadForRequest(userRequest.url());
if (!cookies.isEmpty()) {
requestBuilder.header("Cookie", cookieHeader(cookies));
}
if (userRequest.header("User-Agent") == null) {
requestBuilder.header("User-Agent", Version.userAgent());
}
Response networkResponse = chain.proceed(requestBuilder.build());
HttpHeaders.receiveHeaders(cookieJar, userRequest.url(), networkResponse.headers());
Response.Builder responseBuilder = networkResponse.newBuilder()
.request(userRequest);
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();
}
(3)CacheInterceptor
该拦截器也是okhttp的一个核心机制,做了很精妙的缓存策略,主要代码如下:
@Override public Response intercept(Chain chain) throws IOException {
//首先获取缓存的相应体
<1>
Response cacheCandidate = cache != null
? cache.get(chain.request())
: null;
long now = System.currentTimeMillis();
<2>
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.
}
<3>
// If we're forbidden from using the network and the cache is insufficient, fail.
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();
}
<4>
// If we don't need the network, we're done.
if (networkRequest == null) {
return cacheResponse.newBuilder()
.cacheResponse(stripBody(cacheResponse))
.build();
}
<5>
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());
}
}
<6>
// 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());
}
}
<7>
Response response = networkResponse.newBuilder()
.cacheResponse(stripBody(cacheResponse))
.networkResponse(stripBody(networkResponse))
.build();
<8>
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.
}
}
}
<9>
return response;
}
缓存策略本非okhttp的功能,知识对http的缓存机制用拦截器的模式做了实现
1,首先会像本地取一波缓存
2,CacheStrategy类是一个缓存策略的类,该类更具本次请求的requset以及本地的缓存做一系列的处理,最后生成一个缓存的策略出来(包括各种headers的判断,tags,lastTieme等等)
3,如果禁用网络请求,并且缓存失效,那么会直接返回一个空的resposne;
4,如果禁用网络请求,且有缓存,直接返回缓存,
5,剩下的情况就是需要去网络请求,所以执行下一个拦截器去网络请求;
6,如果本地有缓存,并且返回码为304的时候,我们可以直接复用缓存了,并且更新本地的缓存;
7,如果响应吗不为304,那么说明缓存失效了或者没有缓存或者不使用缓存;
8,根据用户的缓存配置实现http缓存;
9,返回相应体;
整体就是这么一个缓存的流程,
(4)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 httpCodec = streamAllocation.newStream(client, chain, doExtensiveHealthChecks);
RealConnection connection = streamAllocation.connection();
return realChain.proceed(request, streamAllocation, httpCodec, connection);
}
这一步开始正真的开始链接网络了,首先获取到在重定向拦截器中创建的stream,然后大部分的链接工作都交给了stream去完成了;
下面是stream的newStream()方法,
<1> 复用或新建一个Connection对象
<2>新建流,即是创建HttpCodec对象并返回给调用处
public HttpCodec newStream(OkHttpClient client, Interceptor.Chain chain, boolean doExtensiveHealthChecks) {
int connectTimeout = chain.connectTimeoutMillis();
int readTimeout = chain.readTimeoutMillis();
int writeTimeout = chain.writeTimeoutMillis();
boolean connectionRetryEnabled = client.retryOnConnectionFailure();
try {
<1>
RealConnection resultConnection = findHealthyConnection(connectTimeout, readTimeout,
writeTimeout, connectionRetryEnabled, doExtensiveHealthChecks);
HttpCodec resultCodec = resultConnection.newCodec(client, chain, this);
synchronized (connectionPool) {
codec = resultCodec;
return resultCodec;
}
} catch (IOException e) {
throw new RouteException(e);
}
}
stream是如何复用链接的:
下面的代码中开启一个循环不停的调用findConnection方法去获取一个Connection对象,
private RealConnection findHealthyConnection(int connectTimeout, int readTimeout,
int writeTimeout, int pingIntervalMillis, boolean connectionRetryEnabled,
boolean doExtensiveHealthChecks) throws IOException {
while (true) {
RealConnection candidate = findConnection(connectTimeout, readTimeout, writeTimeout,
pingIntervalMillis, connectionRetryEnabled);
// If this is a brand new connection, we can skip the extensive health checks.
synchronized (connectionPool) {
if (candidate.successCount == 0) {
return candidate;
}
}
// Do a (potentially slow) check to confirm that the pooled connection is still good. If it
// isn't, take it out of the pool and start again.
if (!candidate.isHealthy(doExtensiveHealthChecks)) {
noNewStreams();
continue;
}
return candidate;
}
}
那么findConnection方法做了些神们东西
这个方法很长很长,它具体做了以下几点事情
<1>. 首先判断当前StreamAllocation对象是否已经有一个Connection对象了(这种情况会在请求重定向,且重定向的Request的host、port、scheme与之前一致时出现)
<2>. 如果1不满足,则尝试从ConnectionPool中获取一个
<3>. 如果2中没有获取到,则遍历所有路由路径,尝试从再次从ConnectionPool中寻找可复用的连接,找到则返回
<4>. 如果3中没有找到可复用的连接,则尝试新建一个,进行三次握手/TLS握手(如果需要)
<5>.把新建的连接放入ConnectionPool中
<6>.返回结果
所以总体就是这么一个链接的复用流程;
private RealConnection findConnection(int connectTimeout, int readTimeout, int writeTimeout,
int pingIntervalMillis, boolean connectionRetryEnabled) throws IOException {
boolean foundPooledConnection = false;
RealConnection result = null;
Route selectedRoute = null;
Connection releasedConnection;
Socket toClose;
synchronized (connectionPool) {
if (released) throw new IllegalStateException("released");
if (codec != null) throw new IllegalStateException("codec != null");
if (canceled) throw new IOException("Canceled");
// Attempt to use an already-allocated connection. We need to be careful here because our
// already-allocated connection may have been restricted from creating new streams.
releasedConnection = this.connection;
toClose = releaseIfNoNewStreams();
if (this.connection != null) {
// We had an already-allocated connection and it's good.
result = this.connection;
releasedConnection = null;
}
if (!reportedAcquired) {
// If the connection was never reported acquired, don't report it as released!
releasedConnection = null;
}
if (result == null) {
// Attempt to get a connection from the pool.
Internal.instance.get(connectionPool, address, this, null);
if (connection != null) {
foundPooledConnection = true;
result = connection;
} else {
selectedRoute = route;
}
}
}
closeQuietly(toClose);
if (releasedConnection != null) {
eventListener.connectionReleased(call, releasedConnection);
}
if (foundPooledConnection) {
eventListener.connectionAcquired(call, result);
}
if (result != null) {
// If we found an already-allocated or pooled connection, we're done.
return result;
}
// If we need a route selection, make one. This is a blocking operation.
boolean newRouteSelection = false;
if (selectedRoute == null && (routeSelection == null || !routeSelection.hasNext())) {
newRouteSelection = true;
routeSelection = routeSelector.next();
}
synchronized (connectionPool) {
if (canceled) throw new IOException("Canceled");
if (newRouteSelection) {
// Now that we have a set of IP addresses, make another attempt at getting a connection from
// the pool. This could match due to connection coalescing.
List<Route> routes = routeSelection.getAll();
for (int i = 0, size = routes.size(); i < size; i++) {
Route route = routes.get(i);
Internal.instance.get(connectionPool, address, this, route);
if (connection != null) {
foundPooledConnection = true;
result = connection;
this.route = route;
break;
}
}
}
if (!foundPooledConnection) {
if (selectedRoute == null) {
selectedRoute = routeSelection.next();
}
// Create a connection and assign it to this allocation immediately. This makes it possible
// for an asynchronous cancel() to interrupt the handshake we're about to do.
route = selectedRoute;
refusedStreamCount = 0;
result = new RealConnection(connectionPool, selectedRoute);
acquire(result, false);
}
}
// If we found a pooled connection on the 2nd time around, we're done.
if (foundPooledConnection) {
eventListener.connectionAcquired(call, result);
return result;
}
// Do TCP + TLS handshakes. This is a blocking operation.
result.connect(connectTimeout, readTimeout, writeTimeout, pingIntervalMillis,
connectionRetryEnabled, call, eventListener);
routeDatabase().connected(result.route());
Socket socket = null;
synchronized (connectionPool) {
reportedAcquired = true;
// Pool the connection.
Internal.instance.put(connectionPool, result);
// If another multiplexed connection to the same address was created concurrently, then
// release this connection and acquire that one.
if (result.isMultiplexed()) {
socket = Internal.instance.deduplicate(connectionPool, address, this);
result = connection;
}
}
closeQuietly(socket);
eventListener.connectionAcquired(call, result);
return result;
}
以上是创建复用或者创建一个链接并拿到一个链接对象,下一步就搞新建一个Io对象HttpCodec,HttpCodec是依赖okio的一个io组件库,这里只是创建了,具体还没有调用
<5>CallServerInterceptor
CallServerInterceptor是正真的网络请求拦截器
这个拦截方法主要步骤:
<1>,获取几个前面已经创建的重要类;
<2>,先向sink(OutputStream)中写头信息(sink, 在创建连接时候已经创建好);
<3>判断是否有请求体,如有,走4,5的操作,没有直接到6;
<4>如果头部添加了"100-continue", 相对于一次见到的握手操作,只有拿到服务的结果再继续;
<5>当“100-continue”成功或者不需要这个简单握手的,写入请求实体;
<6>finishRequest( 实际是调用了 sink.flush(), 来刷数据 )
<7>读取头部信息(通过source(InputStream), 读取头部信息,状态码等)
<8>构建Response, 写入原请求,握手情况,请求时间,得到的结果时间
<9>通过Response 状态码判断以及是否webSocket判断,是否返回一个空的body, 或者读取Body信息(通过 source(InputStream) 读取);
<10>读取到请求时的连接close ,或者服务器返回的 close, 进行断开操作;
<11>对于204,205的特殊状态码进行处理。
@Override public Response intercept(Chain chain) throws IOException {
<1>
RealInterceptorChain realChain = (RealInterceptorChain) chain;
HttpCodec httpCodec = realChain.httpStream();
StreamAllocation streamAllocation = realChain.streamAllocation();
RealConnection connection = (RealConnection) realChain.connection();
Request request = realChain.request();
long sentRequestMillis = System.currentTimeMillis();
realChain.eventListener().requestHeadersStart(realChain.call());
<2>
httpCodec.writeRequestHeaders(request);
realChain.eventListener().requestHeadersEnd(realChain.call(), request);
<3>
Response.Builder responseBuilder = null;
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.
<4>
if ("100-continue".equalsIgnoreCase(request.header("Expect"))) {
httpCodec.flushRequest();
realChain.eventListener().responseHeadersStart(realChain.call());
responseBuilder = httpCodec.readResponseHeaders(true);
}
<5>
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();
}
}
<6>
httpCodec.finishRequest();
<7>
if (responseBuilder == null) {
realChain.eventListener().responseHeadersStart(realChain.call());
responseBuilder = httpCodec.readResponseHeaders(false);
}
<8>
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);
<9>
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();
}
<10>
if ("close".equalsIgnoreCase(response.request().header("Connection"))
|| "close".equalsIgnoreCase(response.header("Connection"))) {
streamAllocation.noNewStreams();
}
<11>
if ((code == 204 || code == 205) && response.body().contentLength() > 0) {
throw new ProtocolException(
"HTTP " + code + " had non-zero Content-Length: " + response.body().contentLength());
}
return response;
}
经过这最后一个拦截器的处理,一个完整的网络请求就完成了,
最后上一张经典的流程图