BridgeInterceptor

BridgeInterceptor

桥接拦截器


  @Override public Response intercept(Chain chain) throws IOException {
    Request userRequest = chain.request();
    Request.Builder requestBuilder = userRequest.newBuilder();

    RequestBody body = userRequest.body();
    //如果有RequestBody比如post请求
    if (body != null) {
      MediaType contentType = body.contentType();
      if (contentType != null) {
          //设置Content-Type头
        requestBuilder.header("Content-Type", contentType.toString());
      }

      long contentLength = body.contentLength();
      //如果有contentLength则设置Content-Length头,删除分块传输头,这2个头不能同时存在
      //如果没有Content-Length则设置分块传输头
      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头则自动根据请求url设置
    if (userRequest.header("Host") == null) {
      requestBuilder.header("Host", hostHeader(userRequest.url(), false));
    }

//如果没有设置Connection 则默认设置为Keep-Alive
    if (userRequest.header("Connection") == null) {
      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;
    //如果我们没有指定压缩格式 也没有包含Range请求某一部分,则默认设置为支持gzip压缩
    if (userRequest.header("Accept-Encoding") == null && userRequest.header("Range") == null) {
      transparentGzip = true;
      requestBuilder.header("Accept-Encoding", "gzip");
    }

//将cookie 拼接设置到Cookie头中
    List cookies = cookieJar.loadForRequest(userRequest.url());
    if (!cookies.isEmpty()) {
      requestBuilder.header("Cookie", cookieHeader(cookies));
    }
//如果没有设置标识,则默认设置为okhttp自己的
    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);
//如果是okhttp设置的gzip 并且 响应头Content-Encoding 是gzip 并且有响应体
//则删除响应头中的压缩格式和Content-Length重新构建响应
    if (transparentGzip
        && "gzip".equalsIgnoreCase(networkResponse.header("Content-Encoding"))
        && HttpHeaders.hasBody(networkResponse)) {
      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();
  }

你可能感兴趣的:(BridgeInterceptor)