目录
Okhttp3使用及解析:https://mp.csdn.net/postedit/83339916
okhttp系统拦截器:https://mp.csdn.net/postedit/83536609
Okhttp的连接池ConnectionPool:https://mp.csdn.net/postedit/83650740
题外话:Okhttp中Dispatcher(分发器)与Okhttp拦截器属于比较核心的东西。
从上篇:https://blog.csdn.net/qq_37321098/article/details/83339916 能看到Dispatcher操作了请求的分发处理和请求完的移除等操作
拦截器作用:实现网络监听,请求及响应的重写,请求失败的重连等。
拦截器分为:application应用程序拦截器,network网络拦截器,okhttp系统内部拦截器。
1.系统拦截器作用及执行顺序
系统拦截器有5个,如下(先简单看看粗略的作用,后面做源码分析):
1.RetryAndFollowUpInterceptor:用来实现连接失败的重试和重定向
2.BridgeInterceptor:用来补充请求和响应header信息
3.CacheInterceptor:缓存响应信息
4.ConnectInterceptor:建立与服务端的连接
5.CallServerInterceptor:将http请求信息,写入到已打开的网络io流中,并将流中读取到数据封装成 Response 返回
他们的调用顺序是依次往下的 1->2->3->4->5 最终将5响应的Response再一层一层的向上返回,像递归一样。
2.源码验证执行顺序
拦截器的概念不区分同步,异步请求。在同步请求中有如下一段话:
Response result = getResponseWithInterceptorChain();
其内部:
Response getResponseWithInterceptorChain() throws IOException {
//第一步:将所有拦截器存入集合
List<Interceptor> interceptors = new ArrayList<>();
interceptors.addAll(client.interceptors()); //application拦截器
interceptors.add(retryAndFollowUpInterceptor);//4个系统拦截器
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拦截器链
Interceptor.Chain chain = new RealInterceptorChain(interceptors, null, null, null, 0,
originalRequest, this, eventListener, client.connectTimeoutMillis(),
client.readTimeoutMillis(), client.writeTimeoutMillis());
//第三步:拦截器链调用chain.proceed
return chain.proceed(originalRequest);
}
不看application拦截器和网络拦截器,可以发现5个系统拦截器确实是依次调用。
第二步将创建的拦截器链RealInterceptorChain(第5个参数为0,就是存拦截器集合的下标),调用proceed():
题外话:(RealInterceptorChain是Interceptor.Chain接口的实现类)
@Override public Response proceed(Request request) throws IOException {
return proceed(request, streamAllocation, httpCodec, connection);
}
//看其调用的4参方法
public Response proceed(Request request, StreamAllocation streamAllocation, HttpCodec httpCodec,
RealConnection connection) throws IOException {
......
//创建新的拦截器链,并调用链中的下一个拦截器。
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);//当前的拦截器的intercept
......
return response;
}
可以看出又创建了一个为index+1的新拦截器链,并且通过当前拦截器的intercept函数,传入新创建的拦截器链。(只看系统的拦截器,跨过应用及网络拦截器)此时的当前拦截器,也就是RetryAndFollowUpInterceptor,看其intercept()函数:
public final class RetryAndFollowUpInterceptor implements Interceptor {
......
@Override public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
RealInterceptorChain realChain = (RealInterceptorChain) chain;
......
while (true) {
......
Response response;
boolean releaseConnection = true;
try {
//调用下一个拦截器链的process,返回response
response = realChain.proceed(request, streamAllocation, null, null);
releaseConnection = false;
} catch (RouteException e) {
......
}
因为RetryAndFollowUpInterceptor的intercept()函数传入的Chain是新创建的下标为index+1的拦截器链,所以新拦截器又调用
proceed()函数,因为我们知道proceed()函数作用,就是将当前拦截器链中的拦截器通过intercept()函数传入新拦截器链。
可以得出proceed核心作用就是创建下一个拦截器链(构造方法传入index+1),导致依次调用集合中下一个拦截器的intercept方法,从而构建拦截器链条。同时Response也是由下一级拦截器链处理返回的,如同递归一般。
总结一下:
3.源码验证各个拦截器的作用
1)RetryAndFollowUpInterceptor
public final class RetryAndFollowUpInterceptor implements Interceptor {
private static final int MAX_FOLLOW_UPS = 20;
private volatile StreamAllocation streamAllocation;
@Override public Response intercept(Chain chain) throws IOException {
StreamAllocation streamAllocation = new StreamAllocation(
client.connectionPool(),
createAddress(request.url()),
call,
eventListener,
callStackTrace);
this.streamAllocation = streamAllocation;
...
//当前失败重连的次数
if (++followUpCount > MAX_FOLLOW_UPS) {
streamAllocation.release();
throw new ProtocolException("Too many follow-up requests: " + followUpCount);
}
...
priorResponse = response;
}
}
......
}
可以看出当前失败重连的次数大于20次,就释放请求。可以看出请求是被封装在StreamAllocation组件中。
StreamAllocation是用来建立执行HTTP请求所需网络设施的组件。看名字可理解作用包含分配Stream流。在ConnectInterceptor此拦截器中,StreamAllocation将会大显身手,会通过这里创建的StreamAllocation通过StreamAllocation.newStream() 完成所有的连接建立工作。
2)BridgeInterceptor
@Override
public Response intercept(Chain chain) throws IOException {
//获取用户构建的Request对象
Request userRequest = chain.request();
Request.Builder requestBuilder = userRequest.newBuilder();
RequestBody body = userRequest.body();
if (body != null) {
MediaType contentType = body.contentType();
//设置Content-Type
if (contentType != null) {
requestBuilder.header("Content-Type", contentType.toString());
}
//Content-Length和Transfer-Encoding互斥
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"); //保证链接在一定时间内活着
}
//如果我们添加一个“Accept-Encoding: gzip”头字段,我们也要负责解压缩
boolean transparentGzip = false;
if (userRequest.header("Accept-Encoding") == null && userRequest.header("Range") == null) {
transparentGzip = true;
requestBuilder.header("Accept-Encoding", "gzip");
}
//拿到创建Okhpptclitent时候配置的cookieJar
List<Cookie> cookies = cookieJar.loadForRequest(userRequest.url());
//解析成http协议的Cookie和User-Agent格式
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());
//响应header, 如果没有自定义配置cookie不会解析
//将网络返回的response解析成客户端规则的response
HttpHeaders.receiveHeaders(cookieJar, userRequest.url(), networkResponse.headers());
Response.Builder responseBuilder = networkResponse.newBuilder()
.request(userRequest);
//解析完header后,判断服务器是否支持gzip压缩格式,如果支持将交给Okio处理
//判断:1.支持gzip压缩 2.响应头是否支持gzip压缩 3.http头部是否有bodg体
if (transparentGzip
&& "gzip".equalsIgnoreCase(networkResponse.header("Content-Encoding"))
&& HttpHeaders.hasBody(networkResponse)) {
//将response的body体的输入流 转换成 GzipSource类型。方便后期解压方法获取body体
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();
}
Bridge桥-链接客户端代码和网络代码,将客户端构建的Request对象信息构建成真正的网络请求,然后发起网络请求,最终对response的网络返回做处理。可以看到源码中为Request设置User-Agent、Cookie、Accept-Encoding等相关请求头信息。
题外话,看看cookie的配置:
OkHttpClient okHttpClient = new OkHttpClient.Builder()
.cookieJar(new CookieJar() {
@Override
public void saveFromResponse(HttpUrl url, List<Cookie> cookies) {
//可用sp保存cookie
}
@Override
public List<Cookie> loadForRequest(HttpUrl url) {
// 从保存位置读取,为空导致空指针
return new ArrayList<>();
}
})
.build();
3)CacheInterceptor
说起缓存拦截器,先说说它的使用。
OkHttpClient okHttpClient = new OkHttpClient
.Builder()
.cache(new Cache(
new File("cache"),
24*1024*1024))
.build();
进入cache:
public Cache(File directory, long maxSize) {
this(directory, maxSize, FileSystem.SYSTEM);
}
Cache(File directory, long maxSize, FileSystem fileSystem) {
this.cache = DiskLruCache.create(fileSystem, directory, VERSION, ENTRY_COUNT, maxSize);
}
能看出最终是通过DiskLruCache算法做的缓存,看看put存入缓存的操作。
@Nullable
CacheRequest put(Response response) {
String requestMethod = response.request().method();
...
//不缓存非get请求的数据
if (!requestMethod.equals("GET")) {
return null;
}
...
//Entry就是存储的内容---包装了请求方法,请求头等等缓存信息
Entry entry = new Entry(response);
DiskLruCache.Editor editor = null;
try {
//将url做md5加密转化为key
editor = cache.edit(key(response.request().url()));
if (editor == null) {
return null;
}
//将editor写入缓存 缓存一些头部,请求方法.URL.时间等等
entry.writeTo(editor);
//给缓存拦截器使用
return new CacheRequestImpl(editor);
} catch (IOException e) {
abortQuietly(editor);
return null;
}
}
再看看get方法:
@Nullable
Response get(Request request) {
String key = key(request.url());//拿到key
DiskLruCache.Snapshot snapshot;
Entry entry;
try {
snapshot = cache.get(key);//从缓存拿数据,数据封装在Snapshot中
if (snapshot == null) {
return null;
}
} catch (IOException e) {
return null;
}
try {
entry = new Entry(snapshot.getSource(ENTRY_METADATA));
} catch (IOException e) {
Util.closeQuietly(snapshot);
return null;
}
//根据entry获取相应的response
Response response = entry.response(snapshot);
//request和response不匹配,就关闭流
if (!entry.matches(request, response)) {
Util.closeQuietly(response.body());
return null;
}
return response;
}
看完了简易使用和缓存cache,再看看intercept内部具体做了什么:
@Override public Response intercept(Chain chain) throws IOException {
//先看缓存是否有
Response cacheCandidate = cache != null
? cache.get(chain.request())
: null;
....
//缓存策略的获取
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()); // 关闭不符合要求的流
}
//不可使用网络且无缓存 抛出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 (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 (cacheResponse != null) {
//判断响应码是否是304 会从缓存拿数据
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)) {
//将网络响应写到缓存,方便下次直接从缓存取数据
CacheRequest cacheRequest = cache.put(response);
return cacheWritingResponse(cacheRequest, response);
}
//判断request是否是无效的缓存方法
if (HttpMethod.invalidatesCache(networkRequest.method())) {
try {
//从缓存删除request
cache.remove(networkRequest);
} catch (IOException ignored) {
// The cache cannot be written.
}
}
}
return response;
}
4)ConnectInterceptor
@Override
public Response intercept(Chain chain) throws IOException {
RealInterceptorChain realChain = (RealInterceptorChain) chain;
Request request = realChain.request();
//拿到重定向拦截器的StreamAllocation
StreamAllocation streamAllocation = realChain.streamAllocation();
boolean doExtensiveHealthChecks = !request.method().equals("GET");
//newStream创建HttpCodec,用来编码request和解码response
HttpCodec httpCodec = streamAllocation.newStream(client, chain, doExtensiveHealthChecks);
//RealConnection进行网络传输
RealConnection connection = streamAllocation.connection();
//调用下一个拦截器
return realChain.proceed(request, streamAllocation, httpCodec, connection);
}
进入newstream():
public HttpCodec newStream(OkHttpClient client, Interceptor.Chain chain,
boolean doExtensiveHealthChecks) {
int connectTimeout = chain.connectTimeoutMillis();
int readTimeout = chain.readTimeoutMillis();
int writeTimeout = chain.writeTimeoutMillis();
int pingIntervalMillis = client.pingIntervalMillis();
boolean connectionRetryEnabled = client.retryOnConnectionFailure();
try {
//findHealthyConnection方法创建RealConnection进行网络连接
RealConnection resultConnection = findHealthyConnection(connectTimeout, readTimeout,
writeTimeout, pingIntervalMillis, connectionRetryEnabled, doExtensiveHealthChecks);
//HttpCodec 封装好的处理request和response的类
HttpCodec resultCodec = resultConnection.newCodec(client, chain, this);
//返回结果
synchronized (connectionPool) {
codec = resultCodec;
return resultCodec;
}
} catch (IOException e) {
throw new RouteException(e);
}
}
看看findHealthyConnection做了什么:
private RealConnection findHealthyConnection(int connectTimeout, int readTimeout,
int writeTimeout, int pingIntervalMillis, boolean connectionRetryEnabled,
boolean doExtensiveHealthChecks) throws IOException {
while (true) {
//通过findConnection再进行一次封装
RealConnection candidate = findConnection(connectTimeout, readTimeout, writeTimeout,
pingIntervalMillis, connectionRetryEnabled);
synchronized (connectionPool) {
//网络连接结束
if (candidate.successCount == 0) {
return candidate;
}
}
//不健康的请求 比如流未关闭等等
if (!candidate.isHealthy(doExtensiveHealthChecks)) {
noNewStreams();
continue;
}
return candidate;
}
}
再看看findConnection()如何二次封装的:
private RealConnection findConnection(int connectTimeout, int readTimeout, int writeTimeout,
int pingIntervalMillis, boolean connectionRetryEnabled) throws IOException {
...
synchronized (connectionPool) {
....
// 尝试使用已分配的连接,要小心,因为已经分配的连接可能已经被限制创建新的流。
//尝试复用connect,赋值
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) {
// 尝试从连接池获取连接
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) {
//如果我们找到了一个已经分配或池化的连接,那么就完成了。
return result;
}
// 如果我们需要选择路线,就选一条。这是一个阻塞操作。
boolean newRouteSelection = false;
if (selectedRoute == null && (routeSelection == null || !routeSelection.hasNext())) {
newRouteSelection = true;
routeSelection = routeSelector.next();
}
...
// 进行实际网络连接
result.connect(connectTimeout, readTimeout, writeTimeout, pingIntervalMillis,
connectionRetryEnabled, call, eventListener);
routeDatabase().connected(result.route());
Socket socket = null;
synchronized (connectionPool) {
reportedAcquired = true;
// 放入连接池中
Internal.instance.put(connectionPool, result);
// 如果同时创建了另一个到同一地址的多路复用连接,则释放这个连接并获得那个连接。
if (result.isMultiplexed()) {
socket = Internal.instance.deduplicate(connectionPool, address, this);
result = connection;
}
}
closeQuietly(socket);
eventListener.connectionAcquired(call, result);
return result;
}
上面的result.connect()就是进行了实际网络连接。
5)CallServerInterceptor
@Override
public Response intercept(Chain chain) throws IOException {
//拿到链
RealInterceptorChain realChain = (RealInterceptorChain) chain;
//流对象的封装HttpCodec ->编码request和解码response
HttpCodec httpCodec = realChain.httpStream();
//StreamAllocation=建立http请求需要的其他网络设施的组件 ->分配stream
StreamAllocation streamAllocation = realChain.streamAllocation();
//Connection封装请求链接,RealConnection 为接口实现
RealConnection connection = (RealConnection) realChain.connection();
//网络请求
Request request = realChain.request();
...
//往socket写入请求的头部信息
httpCodec.writeRequestHeaders(request);
...
Response.Builder responseBuilder = null;
if (HttpMethod.permitsRequestBody(request.method()) && request.body() != null) {
// 如果请求上有一个“Expect: 100- Continue”报头,在发送请求主体之前等待一个“HTTP/1.1 100
//Continue”响应。如果我们没有得到那个,返回我们得到的(例如4xx响应),而不发送请求主体。
if ("100-continue".equalsIgnoreCase(request.header("Expect"))) {
httpCodec.flushRequest();
realChain.eventListener().responseHeadersStart(realChain.call());
responseBuilder = httpCodec.readResponseHeaders(true);
}
if (responseBuilder == null) {
// 如果满足了“Expect: 100-continue”期望,则编写请求主体。
realChain.eventListener().requestBodyStart(realChain.call());
long contentLength = request.body().contentLength();
CountingSink requestBodyOut =
new CountingSink(httpCodec.createRequestBody(request, contentLength));
BufferedSink bufferedRequestBody = Okio.buffer(requestBodyOut);
//向socket写入body信息
request.body().writeTo(bufferedRequestBody);
bufferedRequestBody.close();
realChain.eventListener()
.requestBodyEnd(realChain.call(), requestBodyOut.successfulCount);
} else if (!connection.isMultiplexed()) {
//如果没有满足“Expect: 100-continue”的期望,则阻止HTTP/1连接被重用。否则,我们仍然有义
//务发送请求主体,使连接保持一致状态。
streamAllocation.noNewStreams();
}
}
//完成请求的写入工作
httpCodec.finishRequest();
//读取响应start
if (responseBuilder == null) {
realChain.eventListener().responseHeadersStart(realChain.call());
//读响应头部信息
responseBuilder = httpCodec.readResponseHeaders(false);
}
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) {
// 连接正在升级,但是我们需要确保拦截器看到非空响应体。
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"))) {
//noNewStreams会在流创建好之后,禁止新的流的创建
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;
}