源码分析:Android Okhttp源码浅析(二)

承接的Okhttp的框架分析。

“源码分析:Android Okhttp源码浅析(一)”

我们知道了,拦截器都是链式调用的。
当有请求发送时,每个拦截器都会处理请求;然后,扔给下一个拦截器来处理,直到返回结果;
然后,把结果,再一次的扔到上一个的拦截器来处理结果。最后把Response处理完成的Response返回。

看下官方图
源码分析:Android Okhttp源码浅析(二)_第1张图片

下面,我们就看下每个拦截器的作用。

  • 我们自己的应用拦截器
  • RetryAndFollowUpInterceptor
  • BridgeInterceptor
  • CacheInterceptor
  • ConnectInterceptor
  • NetworkInterceptors
  • 我们自己的networkInterceptors
  • CallServerInterceptor

自己的应用拦截器

我们自己的拦截器放到后面。先看下系统的拦截器

RetryAndFollowUpInterceptor

@Override public Response intercept(Chain chain) throws IOException {
	//拿到请求配置的Request对象。
    Request request = chain.request();
	//创建StreamAllocation。这个在后面的拦截器会用到
    streamAllocation = new StreamAllocation(
        client.connectionPool(), createAddress(request.url()), callStackTrace);

    int followUpCount = 0;
    Response priorResponse = null;
	//循环
    while (true) {
      if (canceled) {
        streamAllocation.release();
        throw new IOException("Canceled");
      }

      Response response = null;
      boolean releaseConnection = true;
      try {
		//调用下一个RealInterceptorChain的proceed()方法
		//获取下一个拦截器的响应
        response = ((RealInterceptorChain) chain).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(), 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, requestSendStarted, request)) throw e;
        releaseConnection = false;
        continue;
      } finally {
        // We're throwing an unchecked exception. Release any resources.
        if (releaseConnection) {
          streamAllocation.streamFailed(null);
          streamAllocation.release();
        }
      }

      // Attach the prior response if it exists. Such responses never have a body.
      if (priorResponse != null) {
        response = response.newBuilder()
            .priorResponse(priorResponse.newBuilder()
                    .body(null)
                    .build())
            .build();
      }

      Request followUp = followUpRequest(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());
      }

      if (!sameConnection(response, followUp.url())) {
        streamAllocation.release();
        streamAllocation = new StreamAllocation(
            client.connectionPool(), createAddress(followUp.url()), callStackTrace);
      } 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;
    }
  }

就是,当路由错误,或者连接异常后进行重试。

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");
      }
    }
	//host信息
    if (userRequest.header("Host") == null) {
      requestBuilder.header("Host", hostHeader(userRequest.url(), false));
    }

    if (userRequest.header("Connection") == null) {
		//设置Keep-Alive。保持连接
      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;
		//使用gzip压缩
      requestBuilder.header("Accept-Encoding", "gzip");
    }
	//添加cookie
    List 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());
	//接收cookie
    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)) {
		//当返回的结果是gzip的话,就去解压
      GzipSource responseBody = new GzipSource(networkResponse.body().source());
      Headers strippedHeaders = networkResponse.headers().newBuilder()
          .removeAll("Content-Encoding")
          .removeAll("Content-Length")
          .build();
      responseBuilder.headers(strippedHeaders);
      responseBuilder.body(new RealResponseBody(strippedHeaders, Okio.buffer(responseBody)));
    }

    return responseBuilder.build();
  }

到这里可以看出,BridgeInterceptor拦截器的作用就是处理header头信息,cookie。在请求之前和响应之后,处理一些头信息(使用gzip进行压缩,解压等)。

CacheInterceptor

从名字判断,这个拦截器是处理Http请求的缓存策略的。下面具体看下

 @Override public Response intercept(Chain chain) throws IOException {
	//根据Request获取缓存
    Response cacheCandidate = cache != null
        ? cache.get(chain.request())
        : null;

    long now = System.currentTimeMillis();
	//【1】缓存策略,通过请求跟获取的缓存来判断。下面分析
    CacheStrategy strategy = new CacheStrategy.Factory(now, chain.request(), cacheCandidate).get();
	//通过缓存策略,得到的获取网络请求
    Request networkRequest = strategy.networkRequest;
	//通过缓存策略,得到的获取缓存响应
    Response cacheResponse = strategy.cacheResponse;
	//如果缓存不为null
    if (cache != null) {
      cache.trackResponse(strategy);
    }
	//缓存不可用
    if (cacheCandidate != null && cacheResponse == null) {
      closeQuietly(cacheCandidate.body()); // The cache candidate wasn't applicable. Close it.
    }

    // If we're forbidden from using the network and the cache is insufficient, fail.
	//如果没有网络的请求,又没有缓存。直接返回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 we don't need the network, we're done.
	//如果没有网络的请求,直接返回缓存
    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 we have a cache response too, then we're doing a conditional get.
	//如果得到了网络请求,缓存也不为null。就合并网络跟缓存
    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 (HttpHeaders.hasBody(response)) {
		//通过maybeCache来判断是否需要缓存
      CacheRequest cacheRequest = maybeCache(response, networkResponse.request(), cache);
      response = cacheWritingResponse(cacheRequest, response);
    }

    return response;
  }

这个主要是使用缓存策略判断,是否使用缓存还是使用网络的请求。
这里,我们发现如果请求不可用/缓存可用的话,直接返回缓存,不会调用下一个拦截器。

我们发现里面的判断都是跟CacheStrategy类有很大的关系,我们看下new CacheStrategy.Factory(now, chain.request(), cacheCandidate).get()这个方法。

public CacheStrategy get() {
		//拿到CacheStrategy
      CacheStrategy candidate = getCandidate();
		
      if (candidate.networkRequest != null && request.cacheControl().onlyIfCached()) {
        // We're forbidden from using the network and the cache is insufficient.
        return new CacheStrategy(null, null);
      }

      return candidate;
    }

...

/** Returns a strategy to use assuming the request can use the network. */
    private CacheStrategy getCandidate() {
      // No cached response.
		//没有缓存的话。只使用新的请求
      if (cacheResponse == null) {
        return new CacheStrategy(request, null);
      }
		//请求是https并且,缓存没有握手。只使用新的请求
      // Drop the cached response if it's missing a required handshake.
      if (request.isHttps() && cacheResponse.handshake() == null) {
        return new CacheStrategy(request, null);
      }

      // If this response shouldn't have been stored, it should never be used
      // as a response source. This check should be redundant as long as the
      // persistence store is well-behaved and the rules are constant.
		//缓存不可用。只使用新的请求
      if (!isCacheable(cacheResponse, request)) {
        return new CacheStrategy(request, null);
      }

      CacheControl requestCaching = request.cacheControl();
		//请求头noCache或者包含"If-Modified-Since"等等情况
      if (requestCaching.noCache() || hasConditions(request)) {
        return new CacheStrategy(request, null);
      }

      long ageMillis = cacheResponseAge();
      long freshMillis = computeFreshnessLifetime();

      if (requestCaching.maxAgeSeconds() != -1) {
        freshMillis = Math.min(freshMillis, SECONDS.toMillis(requestCaching.maxAgeSeconds()));
      }

      long minFreshMillis = 0;
      if (requestCaching.minFreshSeconds() != -1) {
        minFreshMillis = SECONDS.toMillis(requestCaching.minFreshSeconds());
      }

      long maxStaleMillis = 0;
      CacheControl responseCaching = cacheResponse.cacheControl();
      if (!responseCaching.mustRevalidate() && requestCaching.maxStaleSeconds() != -1) {
        maxStaleMillis = SECONDS.toMillis(requestCaching.maxStaleSeconds());
      }
		
      if (!responseCaching.noCache() && ageMillis + minFreshMillis < freshMillis + maxStaleMillis) {
        Response.Builder builder = cacheResponse.newBuilder();
        if (ageMillis + minFreshMillis >= freshMillis) {
          builder.addHeader("Warning", "110 HttpURLConnection \"Response is stale\"");
        }
        long oneDayMillis = 24 * 60 * 60 * 1000L;
        if (ageMillis > oneDayMillis && isFreshnessLifetimeHeuristic()) {
		//缓存过期,但可用
          builder.addHeader("Warning", "113 HttpURLConnection \"Heuristic expiration\"");
        }
        return new CacheStrategy(null, builder.build());
      }
		//缓存过期
      // Find a condition to add to the request. If the condition is satisfied, the response body
      // will not be transmitted.
      String conditionName;
      String conditionValue;
      if (etag != null) {
        conditionName = "If-None-Match";
        conditionValue = etag;
      } else if (lastModified != null) {
        conditionName = "If-Modified-Since";
        conditionValue = lastModifiedString;
      } else if (servedDate != null) {
        conditionName = "If-Modified-Since";
        conditionValue = servedDateString;
      } else {
        return new CacheStrategy(request, null); // No condition! Make a regular request.
      }

      Headers.Builder conditionalRequestHeaders = request.headers().newBuilder();
      Internal.instance.addLenient(conditionalRequestHeaders, conditionName, conditionValue);

      Request conditionalRequest = request.newBuilder()
          .headers(conditionalRequestHeaders.build())
          .build();
      return new CacheStrategy(conditionalRequest, cacheResponse);
    }

这个里面其实就是根据缓存的各种情况,来判断缓存是否可用。返回缓存策略。

  • cacheResponse缓存为null的话,直接使用网络请求。
  • https并且cacheResponse.handshake() == null的直接使用网络请求
  • isCacheable(),缓存数据的相应码来判断,是否直接使用网络请求
  • 通过请求头里是否包含(“If-None-Match”/“If-None-Match”)来判断是否使用网络请求

通过上面一些列判断来返回缓存策略CacheStrategy类

CacheInterceptor来根据这个缓存策略,来决定使用缓存还是执行网络的请求。这个拦截器主要就是通过缓存的数据跟新的网络请求来判断,是否执行新的网络请求。

ConnectInterceptor

可以看到这个拦截器里面的代码非常少。

public final class ConnectInterceptor implements Interceptor {
  public final OkHttpClient client;

  public ConnectInterceptor(OkHttpClient client) {
    this.client = client;
  }

  @Override public Response intercept(Chain chain) throws IOException {
    RealInterceptorChain realChain = (RealInterceptorChain) chain;
    Request request = realChain.request();
	//获取StreamAllocation对象。(它是在第一个拦截器里面创建的)
    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 httpCodec = streamAllocation.newStream(client, doExtensiveHealthChecks);
	//获取一个链接
    RealConnection connection = streamAllocation.connection();
	//调用下一个拦截器
    return realChain.proceed(request, streamAllocation, httpCodec, connection);
  }
}

这个主要是:

  • 1,获取StreamAllocation对象
  • 2,通过StreamAllocation获取HttpCodec
  • 3,通过StreamAllocation获取一个链接
  • 调用下一个拦截器

我们发现所有的操作都集中到StreamAllocation里了,先看下StreamAllocation#newStream()方法

StreamAllocation#newStream()方法

public HttpCodec newStream(OkHttpClient client, boolean doExtensiveHealthChecks) {	
	//设置我们配置的超时时间
    int connectTimeout = client.connectTimeoutMillis();
    int readTimeout = client.readTimeoutMillis();
    int writeTimeout = client.writeTimeoutMillis();
	//是否链接重试
    boolean connectionRetryEnabled = client.retryOnConnectionFailure();

    try {
		//在连接池中获取一个连接或者创建一个新的连接
      RealConnection resultConnection = findHealthyConnection(connectTimeout, readTimeout,
          writeTimeout, connectionRetryEnabled, doExtensiveHealthChecks);
      HttpCodec resultCodec = resultConnection.newCodec(client, this);

      synchronized (connectionPool) {
        codec = resultCodec;
        return resultCodec;
      }
    } catch (IOException e) {
      throw new RouteException(e);
    }
  }

通过findHealthyConnection()方法找到一个新的连接。

private RealConnection findHealthyConnection(int connectTimeout, int readTimeout,
      int writeTimeout, boolean connectionRetryEnabled, boolean doExtensiveHealthChecks)
      throws IOException {
    while (true) {
		//调用findConnection方法获取
      RealConnection candidate = findConnection(connectTimeout, readTimeout, writeTimeout,
          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()**方法一个连接。然后,检查连接的可用性,不可用就关闭。

private RealConnection findConnection(int connectTimeout, int readTimeout, int writeTimeout,
      boolean connectionRetryEnabled) throws IOException {
    Route selectedRoute;
    synchronized (connectionPool) {
    

      // Attempt to use an already-allocated connection.
      RealConnection allocatedConnection = this.connection;
      if (allocatedConnection != null && !allocatedConnection.noNewStreams) {
        return allocatedConnection;
      }

      // Attempt to get a connection from the pool.
      Internal.instance.get(connectionPool, address, this, null);
      if (connection != null) {
        return connection;
      }

      selectedRoute = route;
    }

    // If we need a route, make one. This is a blocking operation.
    if (selectedRoute == null) {
      selectedRoute = routeSelector.next();
    }

    RealConnection result;
    synchronized (connectionPool) {

      Internal.instance.get(connectionPool, address, this, selectedRoute);
      if (connection != null) return connection;

      route = selectedRoute;
      refusedStreamCount = 0;
		//创建一个新的连接
      result = new RealConnection(connectionPool, selectedRoute);
		//放到连接池?
      acquire(result);
    }

    // Do TCP + TLS handshakes. This is a blocking operation.
	//核心操作,建立连接
    result.connect(connectTimeout, readTimeout, writeTimeout, connectionRetryEnabled);
    routeDatabase().connected(result.route());

    Socket socket = null;
    synchronized (connectionPool) {
      // 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);

    return result;
  }

我们发现,这里是联网的核心。获取一个连接,并建立连接。
到这里streamAllocation.newStream()方法就分析完了,有点长

这里是在连接池中找一个可用的连接或者重新建一个连接。调用下一个拦截器

CallServerInterceptor

@Override public Response intercept(Chain chain) throws IOException {
	//这个就是上个拦截器创建出来的
    HttpCodec httpCodec = ((RealInterceptorChain) chain).httpStream();
    StreamAllocation streamAllocation = ((RealInterceptorChain) chain).streamAllocation();
    Request request = chain.request();

    long sentRequestMillis = System.currentTimeMillis();
	//写入请求头信息
    httpCodec.writeRequestHeaders(request);

    Response.Builder responseBuilder = null;
	//如果有请求体,写入请求体
    if (HttpMethod.permitsRequestBody(request.method()) && request.body() != null) {
     
      if ("100-continue".equalsIgnoreCase(request.header("Expect"))) {
        httpCodec.flushRequest();
        responseBuilder = httpCodec.readResponseHeaders(true);
      }

      // Write the request body, unless an "Expect: 100-continue" expectation failed.
      if (responseBuilder == null) {
        Sink requestBodyOut = httpCodec.createRequestBody(request, request.body().contentLength());
        BufferedSink bufferedRequestBody = Okio.buffer(requestBodyOut);
        request.body().writeTo(bufferedRequestBody);
        bufferedRequestBody.close();
      }
    }
	
	//结束请求
    httpCodec.finishRequest();
	//读取请求信息
    if (responseBuilder == null) {
      responseBuilder = httpCodec.readResponseHeaders(false);
    }

    Response response = responseBuilder
        .request(request)
        .handshake(streamAllocation.connection().handshake())
		//发送的请求时间
        .sentRequestAtMillis(sentRequestMillis)
			//响应的时间
        .receivedResponseAtMillis(System.currentTimeMillis())
        .build();
	
    int code = response.code();
	//websocket 或者获取的code是101。没有响应体
    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();
    }

    if ("close".equalsIgnoreCase(response.request().header("Connection"))
        || "close".equalsIgnoreCase(response.header("Connection"))) {
      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;
  }

这里的操作就是:

  • 写入请求头,请求体
  • 结束请求
  • 获取请求头,请求体,发送时间,接收时间等

参考

https://square.github.io/okhttp/interceptors/
https://www.jianshu.com/p/4510ae14dbe9

你可能感兴趣的:(Android-源码分析)