Summary of OkHttp source code interpretation (VIII) -- > BridgeInterceptor

Keywords: network encoding OkHttp

Summary of OkHttp source code interpretation (VIII) - > bridgeinterceptor

Tags (space separated): learning notes of OkHttp source code

Preface

  • The following summary of relevant knowledge is based on the relevant learning and opinions of mooc.com. If you need to check the relevant teaching of mooc.com, you can still feel it.

Official website introduction

Bridges from application code to network code. First it builds a network request from a user
 * request. Then it proceeds to call the network. Finally it builds a user response from the network
 * response.

Mainly responsible for setting content length, encoding method, gzip compression, adding request headers, cookie s, etc

Main method (mainly to add header information)

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

    RequestBody body = userRequest.body();
    if (body != null) {
      //Content-Type
      MediaType contentType = body.contentType();
      if (contentType != null) {
        requestBuilder.header("Content-Type", contentType.toString());
      }

     //Response length
      long contentLength = body.contentLength();
      if (contentLength != -1) {
        requestBuilder.header("Content-Length", Long.toString(contentLength));
        requestBuilder.removeHeader("Transfer-Encoding");
      } else {
        //transform method 
        requestBuilder.header("Transfer-Encoding", "chunked");
        requestBuilder.removeHeader("Content-Length");
      }
    }
   //Host host
    if (userRequest.header("Host") == null) {
      requestBuilder.header("Host", hostHeader(userRequest.url(), false));
    }

    //Keep alive stay connected
    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;
    if (userRequest.header("Accept-Encoding") == null && userRequest.header("Range") == null) {
      transparentGzip = true;
      requestBuilder.header("Accept-Encoding", "gzip");
    }

    List<Cookie> 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());
    }
    //The most important thing is that the proceed() method sends the request server response to the server and returns the response
    Response networkResponse = chain.proceed(requestBuilder.build());
    //Convert the response of network request and server response to the response that our users can use
    HttpHeaders.receiveHeaders(cookieJar, userRequest.url(), networkResponse.headers());

    Response.Builder responseBuilder = networkResponse.newBuilder()
        .request(userRequest);
    //Convert the response returned by the network request
    //transparentGzip==true support gzip compression content encoding support gzip judge whether http header has body
    if (transparentGzip
        && "gzip".equalsIgnoreCase(networkResponse.header("Content-Encoding"))
        && HttpHeaders.hasBody(networkResponse)) {
      //Convert the response's body() input stream to the GzipSource type
      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");
      //Direct read
      responseBuilder.body(new RealResponseBody(contentType, -1L, Okio.buffer(responseBody)));
    }

    return responseBuilder.build();
  }

The above methods mainly set related request header information, gzip compression, cookie related

major function

  • 1. Be responsible for converting a Request built by the user into a Request capable of network access
  • 2. Make a network Request for the Request that meets the network Request
  • 3. Convert the Response from the network request to the Response that users can use (gzip compression / decompression)

Posted by bobthebuilder on Mon, 04 May 2020 08:25:20 -0700