目錄
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;
}