OKHttp源码解析

本文将从类图和一次完整的同步网络请求角度分析OKHttp。如有不妥,欢迎指正。

类图

Okhttp.png

该类图不是很完整,但是包含了一次请求涉及的类。

GET请求过程

OkHttpClient client = new OkHttpClient();  //1
//新建一个Request对象
Request request = new Request.Builder() //2
    .url(url)
    .build();
Response response = client.newCall(request).execute(); //3
//获取响应结果
response.body().string(); //4

1.创建OKHttpClient:

final Dispatcher dispatcher;

final Proxy proxy;

final List<Protocol> protocols;

final List<ConnectionSpec> connectionSpecs;

final List<Interceptor> interceptors;

final List<Interceptor> networkInterceptors;

final ProxySelector proxySelector;

final CookieJar cookieJar;

final Cache cache;

final InternalCache internalCache;

final SocketFactory socketFactory;

final SSLSocketFactory sslSocketFactory;

final CertificateChainCleaner certificateChainCleaner;

final HostnameVerifier hostnameVerifier;

final CertificatePinner certificatePinner;

final Authenticator proxyAuthenticator;

final Authenticator authenticator;

final ConnectionPool connectionPool;

final Dns dns;

final boolean followSslRedirects;

final boolean followRedirects;

final boolean retryOnConnectionFailure;

final int connectTimeout;

final int readTimeout;

final int writeTimeout;

OKHttpClient包含了各种配置信息。

2.创建Request

private final HttpUrl url;  //url

private final String method;   //请求方法

private final Headers headers; //首部

private final RequestBody body;  //主题

private final Object tag; //唯一标识一个请求

Request类包含了一次http请求所有的信息。真正执行请求的是这段代码 Response response = client.newCall(request).execute();我们看下OKHttpClient类的newCall()方法:

@Override public Call newCall(Request request) {

return new RealCall(this, request, false /* for web socket */);

}

创建并返回了一个RealCall对象。

3.RealCall对象

RealCall实现了Call接口:
final class RealCall implements Call

成员变量:
final OkHttpClient client;
final RetryAndFollowUpInterceptor retryAndFollowUpInterceptor;
/** The application's original request unadulterated by redirects or auth headers. */
final Request originalRequest;  //包含请求信息
final boolean forWebSocket;
// Guarded by this.
private boolean executed;

核心方法:
    @Override protected void execute() {
      boolean signalledCallback = false;
      try {
        Response response = getResponseWithInterceptorChain();    //执行网络请求
        if (retryAndFollowUpInterceptor.isCanceled()) {
          signalledCallback = true;
          responseCallback.onFailure(RealCall.this, new IOException("Canceled"));
        } else {
          signalledCallback = true;
          responseCallback.onResponse(RealCall.this, response);
        }
      } catch (IOException e) {
        if (signalledCallback) {
          // Do not signal the callback twice!
          Platform.get().log(INFO, "Callback failure for " + toLoggableString(), e);
        } else {
          responseCallback.onFailure(RealCall.this, e);
        }
      } finally {
        client.dispatcher().finished(this);
      }
    }
  }

  Response getResponseWithInterceptorChain() throws IOException {
    // Build a full stack of interceptors.
    List<Interceptor> interceptors = new ArrayList<>();
    interceptors.addAll(client.interceptors());    //添加自定义拦截器
    interceptors.add(retryAndFollowUpInterceptor);    //这个拦截器的intercept创建StreamAllocation,回头再看
    interceptors.add(new BridgeInterceptor(client.cookieJar()));
    interceptors.add(new CacheInterceptor(client.internalCache()));
    interceptors.add(new ConnectInterceptor(client));    //这个拦截器的intercept创建HttpCodec,回头再看
    if (!forWebSocket) {
      interceptors.addAll(client.networkInterceptors());
    }
    interceptors.add(new CallServerInterceptor(forWebSocket));    //这个拦截器的intercept方法执行网络请求,回头再看

    Interceptor.Chain chain = new RealInterceptorChain(
        interceptors, null, null, null, 0, originalRequest);    //创建一个RealInterceptorChain对象,传入请求信息和拦截器信息
    return chain.proceed(originalRequest);    //开始处理
  }

RealCall的execute()中调用了getResponseWithInterceptorChain(),该方法中创建了很多Interceptor,这里使用了职责链模式,几个关键的Intercept已经在注释中标注,回头还会再看。然后创建RealInterceptorChain对象。

4.RealInterceptorChain类:

主要成员变量:
private final List<Interceptor> interceptors;  
private final StreamAllocation streamAllocation;
private final HttpCodec httpCodec;     //执行网络请求
private final Connection connection;
private final int index;              //表明当前对象使用的Inteceptor在interceptros列表中的下标
private final Request request;

主要方法:
  public Response proceed(Request request, StreamAllocation streamAllocation, HttpCodec httpCodec,
      Connection connection) throws IOException {
    if (index >= interceptors.size()) throw new AssertionError();

    calls++;

    // If we already have a stream, confirm that the incoming request will use it.
    if (this.httpCodec != null && !sameConnection(request.url())) {
      throw new IllegalStateException("network interceptor " + interceptors.get(index - 1)
          + " must retain the same host and port");
    }

    // If we already have a stream, confirm that this is the only call to chain.proceed().
    if (this.httpCodec != null && calls > 1) {
      throw new IllegalStateException("network interceptor " + interceptors.get(index - 1)
          + " must call proceed() exactly once");
    }

    // Call the next interceptor in the chain.
    RealInterceptorChain next = new RealInterceptorChain(
        interceptors, streamAllocation, httpCodec, connection, index + 1, request);          //创建一个新的RealInterceptorChain对象,该对象的index增加1,表明这个对象由下一个Interceptor处理
    Interceptor interceptor = interceptors.get(index);  //获取当前RealInterceptorChain对象的Interceptor
    Response response = interceptor.intercept(next); //处理新生成的RealInterceptorChain对象

    // Confirm that the next interceptor made its required call to chain.proceed().
    if (httpCodec != null && index + 1 < interceptors.size() && next.calls != 1) {
      throw new IllegalStateException("network interceptor " + interceptor
          + " must call proceed() exactly once");
    }

    // Confirm that the intercepted response isn't null.
    if (response == null) {
      throw new NullPointerException("interceptor " + interceptor + " returned null");
    }

    return response;
  }

proceed()关键的方法已经注释,该方法根据index下标获取一个interceptor,然后生成一个新的RealInterceptorChain对象,作为intercepter.intercept()的参数。我们根据interceptor加入的顺序看看第一个interceptor是谁?

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

除了我们自定义的第一个就是retryAndFollowUpInterceptor这个Interceptor。我们看看这个类。

5.RetryAndFollowUpInterceptor类

成员变量:
private final OkHttpClient client;

private final boolean forWebSocket;

private StreamAllocation streamAllocation;

private Object callStackTrace;

private volatile boolean canceled;

核心方法:
  @Override public Response intercept(Chain chain) throws IOException {
    Request request = chain.request();

    streamAllocation = new StreamAllocation(
        client.connectionPool(), createAddress(request.url()), callStackTrace);  //创建StreamAllocation,这个类和发送字节流有关,后面还会提到

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

      Response response = null;
      boolean releaseConnection = true;
      try {
        response = ((RealInterceptorChain) chain).proceed(request, streamAllocation, null, null);  //又调用chain的proceed方法,该方法又会生成一个RealInterceptorChain对象,交给下一个拦截器处理
        releaseConnection = false;
      } catch (RouteException e) {
        // The attempt to connect via a route failed. The request will not have been sent.
        if (!recover(e.getLastConnectException(), true, request)) throw e.getLastConnectException();
        releaseConnection = false;
        continue;
      } catch (IOException e) {
        // An attempt to communicate with a server failed. The request may have been sent.
        if (!recover(e, false, 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;
    }
  }

intercept()方法中重要的地方已经注释,该方法给RealInterceptorChain传入一个生成的StreamAllocation对象,这个对象和发送字节流相关。然后又调用chain的proceed方法,该方法又会生成一个RealInterceptorChain对象,交给下一个拦截器处理。我们看看最后一个拦截器是如何处理RealInterceptorChain的,最后一个拦截器是CallServerInterceptor。

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

    if (HttpMethod.permitsRequestBody(request.method()) && request.body() != null) {
      Sink requestBodyOut = httpCodec.createRequestBody(request, request.body().contentLength()); 
      BufferedSink bufferedRequestBody = Okio.buffer(requestBodyOut);  //这是okio中的类,就是用这个对象发送数据
      request.body().writeTo(bufferedRequestBody);   //发送请求实体
      bufferedRequestBody.close();
    }

    httpCodec.finishRequest();

    Response response = httpCodec.readResponseHeaders()
        .request(request)
        .handshake(streamAllocation.connection().handshake())
        .sentRequestAtMillis(sentRequestMillis)
        .receivedResponseAtMillis(System.currentTimeMillis())
        .build();        //接收响应

    int code = response.code();
    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;
  }
}

最后一个拦截器真正执行网络请求,关键代码已经注释,将BufferedSink作为RequestBody.writeTo()的参数。我们看看RequestBody是如何用BufferedSink发送数据的。

7.RequestBody

核心方法:
  public static RequestBody create(MediaType contentType, String content) {
    Charset charset = Util.UTF_8;
    if (contentType != null) {
      charset = contentType.charset();
      if (charset == null) {
        charset = Util.UTF_8;
        contentType = MediaType.parse(contentType + "; charset=utf-8");
      }
    }
    byte[] bytes = content.getBytes(charset);
    return create(contentType, bytes);
  }


  public static RequestBody create(final MediaType contentType, final byte[] content,
      final int offset, final int byteCount) {
    if (content == null) throw new NullPointerException("content == null");
    Util.checkOffsetAndCount(content.length, offset, byteCount);
    return new RequestBody() {
      @Override public MediaType contentType() {
        return contentType;
      }

      @Override public long contentLength() {
        return byteCount;
      }

      @Override public void writeTo(BufferedSink sink) throws IOException {
        sink.write(content, offset, byteCount);    //调用BufferedSink发送字节流
      }
    };
  }

我们看到RequestBody最终是调用传入的BufferedSink参数将内容的字节发送给服务器。

总结

我们仅仅分析了一次同步请求的过程,还没有分析Okio中的BufferedSink是如何将字节流发送给服务器的,先占个坑。

后面会将更多笔记整理成博客,欢迎关注。
支持原创,转载请注明出处。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 217,734评论 6 505
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,931评论 3 394
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 164,133评论 0 354
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,532评论 1 293
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,585评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,462评论 1 302
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,262评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,153评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,587评论 1 314
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,792评论 3 336
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,919评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,635评论 5 345
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,237评论 3 329
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,855评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,983评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,048评论 3 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,864评论 2 354

推荐阅读更多精彩内容