OkHttp源码(二)

2018-06-03  本文已影响24人  Utte

在RealCall的execute()和AsyncCall的execute()中都调用了getResponseWithInterceptorChain(),这个方法返回了Response,
从request变成了response,这其中发生了什么?拦截器和拦截器链是什么?

Interceptor是什么

Interceptors are a powerful mechanism that can monitor, rewrite, and retry calls.

OkHttp官网说拦截器是一种强大的机制,可以监视、重写和重试调用,它记录传出的请求和传入的响应。

image

看上面那张图,有三种拦截器,应用拦截器、网络拦截器和OkHttp core核心代码中的拦截器。可以这样理解,application生成request,通过应用拦截器对其做一系列处理后给OkHttp core中的拦截器,之后通过网络拦截器再做处理,最终访问到服务端返回response,经过网络拦截器处理response,再经过OkHttp core的拦截器处理,最终交给应用拦截器处理,返回给应用。

我们回过头来看getResponseWithInterceptorChain()中初始拦截器的队列的代码:

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);
}

client.interceptors()、client.networkInterceptors()这两个队列就是在client中初始化的应用拦截器队列和网络拦截器队列。拦截器队列加入的顺序是,应用拦截器、retryAndFollowUpInterceptor、BridgeInterceptor、CacheInterceptor、ConnectIntercepto、网络拦截器(非webSocket)、CallServerInterceptor发送请求。这样就和上面分析的过程一样了。

画张图更好的理解一下过程:

request和response都经过了每一个拦截器,每个拦截器有每个拦截器的作用,这里用到了责任链模式。

那这样的链式调用是如何实现的呢?

InterceptorChain拦截器链

在getResponseWithInterceptorChain()中,添加完拦截器后创建了RealInterceptorChain对象。

Interceptor.Chain chain = new RealInterceptorChain(
        interceptors, null, null, null, 0,
        originalRequest, this, eventListener, 
        client.connectTimeoutMillis(), 
        client.readTimeoutMillis(), 
        client.writeTimeoutMillis()
);
public RealInterceptorChain(
    List<Interceptor> interceptors, 
    StreamAllocation streamAllocation,
    HttpCodec httpCodec, 
    RealConnection connection, 
    int index, 
    Request request, 
    Call call,
    EventListener eventListener, 
    int connectTimeout, 
    int readTimeout, 
    int writeTimeout
)

我们先注意这几个参数,interceptors为先前创建好的拦截器集合,index为当前拦截器在interceptors的下标,request传入当前realReques。构造器没有做其他的工作,只是将参数赋值给成员变量。

在创建好RealInterceptorChain后调用了proceed()。

@Override public Response proceed(Request request) throws IOException {
    return proceed(request, streamAllocation, httpCodec, connection);
}

调用了四个参数的proceed()

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);

    //对response等的判断...

    return response;
}

proceed()中除了一些对参数的判断和对response的判断外剩下部分就是实现链式调用的重点了。首先看到又创建了一个RealInterceptorChain对象,除了index不一样,其他参数和自己都是一样的,index加了1,就说明这一个拦截器链的起始是比自身起始后一位的,移到了拦截器队列的下一个拦截器。

Interceptor interceptor = interceptors.get(index);
Response response = interceptor.intercept(next);

先获取了拦截器链当前的拦截器,之后调用其intercept()方法,传入了下一个拦截器链。来看intercept()的实现,interceptor是一个接口,有五个实现类分别是上面说到的五个OkHttp默认拦截器。我们先来看RetryAndFollowUpInterceptor的intercept(),因为它是OkHttp core的第一个拦截器。把处理相关的代码都省略了,我们先看链式调用的实现。

先是将chain强制转换为RealInterceptorChain,之后调用了它的proceed(),回到了最初的RealInterceptorChain的proceed(),而proceed()中又有对下一个拦截器链intercept()的调用。

@Override public Response intercept(Chain chain) throws IOException {
    //...
    RealInterceptorChain realChain = (RealInterceptorChain) chain;
    //...
    response = realChain.proceed(request, streamAllocation, null, null);
    //...
}

总结下来就是RealInterceptorChain的proceed()和index控制的不同拦截器实现的intercpt()的递归调用实现了链式处理。

现在我们大概知道了拦截器的操作流程,接下来该分析每一个拦截器的功能了。

上一篇下一篇

猜你喜欢

热点阅读