OKHttp源码解析 4 - 1:拦截器源码分析、拦截器链

前言

看了我们这个系列文章的应该知道,前面我们多次提到拦截器链这个概念,然后说它是 OKHttp 当中非常重要的一个内容。那拦截器到底是个啥呢?官网给我们的解释是这样的:拦截器是OKHttp中提供的一种强大机制,它可以实现网络监听、请求以及响应重写、请求失败重试等功能。官网把拦截器分为两种:Application 拦截器、network拦截器,而 OKHttp 内部提供了一系列的拦截器给我们,这也是 OKHttp 拦截器的核心。

而拦截器链就是由一系列拦截器组成的,顺序调用的一个机制,简称拦截器链。另外很重要的一点是:拦截器链是不区分同步异步的,也就是说同步异步都会用到拦截器链。

拦截器链源码分析

在第二篇介绍异步请求时,我们提到过:在异步请求中,我们是通过拦截器链来获取 Response 的,也就是通过 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()));
    //连接拦截器:建立可用的连接,是CallServerInterceptor的基本
    interceptors.add(new ConnectInterceptor(client));
    if (!forWebSocket) {
      interceptors.addAll(client.networkInterceptors());
    }
    //将http请求写进io流当中,并且从io流中读取response
    interceptors.add(new CallServerInterceptor(forWebSocket));

    //初始化 RealInterceptorChain
    Interceptor.Chain chain = new RealInterceptorChain(interceptors, null, null, null, 0,
        originalRequest, this, eventListener, client.connectTimeoutMillis(),
        client.readTimeoutMillis(), client.writeTimeoutMillis());

    return chain.proceed(originalRequest);
  }

源码中前面部分主要提到了五个拦截器,分别是:
- RetryAndFollowUpInterceptor:重试和失败重定向拦截器
- BridgeInterceptor:桥接拦截器,处理一些必须的请求头信息的拦截器
- CacheInterceptor:缓存拦截器,用于处理缓存
- ConnectInterceptor:连接拦截器,建立可用的连接,是CallServerInterceptor的基本
- CallServerInterceptor:请求服务器拦截器将 http 请求写进 IO 流当中,并且从 IO 流中读取响应 Response

后半部分很重要了,先是初始化 RealInterceptorChain,然后调用 proceed() 方法,我们跟踪这个方法,进入到 RealInterceptorChain 的源码中,最终定位到 RealInterceptorChain 中的 proceed() 方法

public Response proceed(Request request, StreamAllocation streamAllocation, HttpCodec httpCodec,
      RealConnection connection) throws IOException {

    //......省略部分代码,只显示核心代码

    // Call the next interceptor in the chain.
    //调用拦截器链中的下一个拦截器,注意 index + 1 参数
    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;
  }

从 RealInterceptorChain 的 proceed() 方法的核心代码中,我们可以看到。在这个方法中,主要就是新建了一个新的 RealInterceptorChain,只是在第五个 index 参数中,传入了 index + 1,确保调用到的是下一个拦截器,这也是 OKhttp 中拦截器链的关键所在。然后获取当前的拦截器,调用 Interceptor 的 intercept() 方法。我们可以查看上面五个拦截器的 intercept() 方法,可以发现,这五个拦截器的 intercept() 方法中,无一例外都调用了 chain.proceed() 方法,然后

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值