码迷,mamicode.com
首页 > Web开发 > 详细

源码系列--OkHttp(CallServerInterceptor)

时间:2020-04-30 18:57:40      阅读:82      评论:0      收藏:0      [点我收藏+]

标签:github   eal   发送   end   throw   http   inf   头信息   左右   

Github下载源码地址https://github.com/square/okhttp

我们今天来看看拦截器里的CallServerInterceptor

来看CallServerInterceptor类的intercept方法

1)源码:

Exchange exchange = realChain.exchange();
Request request = realChain.request();

RealInterceptorChain类分别返回Exchange和Request对象

2)源码

exchange.writeRequestHeaders(request)

进到Exchange类

public void writeRequestHeaders(Request request) throws IOException {
  try {
    eventListener.requestHeadersStart(call);
    codec.writeRequestHeaders(request);
    eventListener.requestHeadersEnd(call, request);
  } catch (IOException e) {
    eventListener.requestFailed(call, e);
    trackFailure(e);
    throw e;
  }
}

进到ExchangeCodec的实现类Http1ExchangeCodec(Http1.1版本)的writeRequestHeaders方法

@Override public void writeRequestHeaders(Request request) throws IOException {
  String requestLine = RequestLine.get(
      request, realConnection.route().proxy().type());
  writeRequest(request.headers(), requestLine);
}

进到RequestLine的get方法

public static String get(Request request, Proxy.Type proxyType) {
  StringBuilder result = new StringBuilder();
  result.append(request.method());
  result.append(‘ ‘);

  if (includeAuthorityInRequestLine(request, proxyType)) {
    result.append(request.url());
  } else {
    result.append(requestPath(request.url()));
  }

  result.append(" HTTP/1.1");
  return result.toString();
}

这里是一些字符串的拼接和请求的判断

紧接着是Http1ExchangeCodec的writeRequest方法

public void writeRequest(Headers headers, String requestLine) throws IOException {
  if (state != STATE_IDLE) throw new IllegalStateException("state: " + state);
  sink.writeUtf8(requestLine).writeUtf8("\r\n");
  for (int i = 0, size = headers.size(); i < size; i++) {
    sink.writeUtf8(headers.name(i))
        .writeUtf8(": ")
        .writeUtf8(headers.value(i))
        .writeUtf8("\r\n");
  }
  sink.writeUtf8("\r\n");
  state = STATE_OPEN_REQUEST_BODY;
}

可以看到是通过Okio的BufferedSink实现的

Exchange类的writeRequestHeaders方法的catch中的trackFailure会走失败流程,输出ErrorCode

3)源码

responseBuilder = exchange.readResponseHeaders(true);

进到Exchange类的readResponseHeaders方法,然后再进到Http1ExchangeCodec类的readResponseHeaders方法

看名字都知道是读响应头信息,进到StatusLine的parse方法里

4)源码

BufferedSink bufferedRequestBody = Okio.buffer(
    exchange.createRequestBody(request, true));

最终进到Http1ExchangeCodec类的createRequestBody方法中

5)CallServerInterceptor类的intercept方法最后是生成response

源码:

Response response = responseBuilder
    .request(request)
    .handshake(exchange.connection().handshake())
    .sentRequestAtMillis(sentRequestMillis)
    .receivedResponseAtMillis(System.currentTimeMillis())
    .build();

int code = response.code();
if (code == 100) {
  // server sent a 100-continue even though we did not request one.
  // try again to read the actual response
  response = exchange.readResponseHeaders(false)
      .request(request)
      .handshake(exchange.connection().handshake())
      .sentRequestAtMillis(sentRequestMillis)
      .receivedResponseAtMillis(System.currentTimeMillis())
      .build();

  code = response.code();
}

exchange.responseHeadersEnd(response);

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(exchange.openResponseBody(response))
      .build();
}

总的来说,还是比较简单的,150行左右的代码。先写请求头,然后发送请求,最后得到响应

而且有两个类Http1ExchangeCodec和Http2ExchangeCodec,很好的适配了不同的http版本

欢迎关注我的微信公众号:安卓圈

技术图片

源码系列--OkHttp(CallServerInterceptor)

标签:github   eal   发送   end   throw   http   inf   头信息   左右   

原文地址:https://www.cnblogs.com/anni-qianqian/p/12810703.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!