larksuite-oapi/src/main/java/com/larksuite/oapi/okhttp3_14/internal/http/RetryAndFollowUpInterceptor.java
private Request followUpRequest(Response userResponse, @Nullable Route route) throws IOException {
    if (userResponse == null) throw new IllegalStateException();
    int responseCode = userResponse.code();

    final String method = userResponse.request().method();
    switch (responseCode) {
      case HTTP_PROXY_AUTH:
        Proxy selectedProxy = route != null
            ? route.proxy()
            : client.proxy();
        if (selectedProxy.type() != Proxy.Type.HTTP) {
          throw new ProtocolException("Received HTTP_PROXY_AUTH (407) code while not using proxy");
        }
        return client.proxyAuthenticator().authenticate(route, userResponse);

      case HTTP_UNAUTHORIZED:
        return client.authenticator().authenticate(route, userResponse);

      case HTTP_PERM_REDIRECT:
      case HTTP_TEMP_REDIRECT:
        // "If the 307 or 308 status code is received in response to a request other than GET
        // or HEAD, the user agent MUST NOT automatically redirect the request"
        if (!method.equals("GET") && !method.equals("HEAD")) {
          return null;
        }
        // fall-through
      case HTTP_MULT_CHOICE:
      case HTTP_MOVED_PERM:
      case HTTP_MOVED_TEMP:
      case HTTP_SEE_OTHER:
        // Does the client allow redirects?
        if (!client.followRedirects()) return null;

        String location = userResponse.header("Location");
        if (location == null) return null;
        HttpUrl url = userResponse.request().url().resolve(location);

        // Don't follow redirects to unsupported protocols.
        if (url == null) return null;

        // If configured, don't follow redirects between SSL and non-SSL.
        boolean sameScheme = url.scheme().equals(userResponse.request().url().scheme());
        if (!sameScheme && !client.followSslRedirects()) return null;

        // Most redirects don't include a request body.
        Request.Builder requestBuilder = userResponse.request().newBuilder();
        if (HttpMethod.permitsRequestBody(method)) {
          final boolean maintainBody = HttpMethod.redirectsWithBody(method);
          if (HttpMethod.redirectsToGet(method)) {
            requestBuilder.method("GET", null);
          } else {
            RequestBody requestBody = maintainBody ? userResponse.request().body() : null;
            requestBuilder.method(method, requestBody);
          }
          if (!maintainBody) {
            requestBuilder.removeHeader("Transfer-Encoding");
            requestBuilder.removeHeader("Content-Length");
            requestBuilder.removeHeader("Content-Type");
          }
        }

        // When redirecting across hosts, drop all authentication headers. This
        // is potentially annoying to the application layer since they have no
        // way to retain them.
        if (!sameConnection(userResponse.request().url(), url)) {
          requestBuilder.removeHeader("Authorization");
        }

        return requestBuilder.url(url).build();

      case HTTP_CLIENT_TIMEOUT:
        // 408's are rare in practice, but some servers like HAProxy use this response code. The
        // spec says that we may repeat the request without modifications. Modern browsers also
        // repeat the request (even non-idempotent ones.)
        if (!client.retryOnConnectionFailure()) {
          // The application layer has directed us not to retry the request.
          return null;
        }

        RequestBody requestBody = userResponse.request().body();
        if (requestBody != null && requestBody.isOneShot()) {
          return null;
        }

        if (userResponse.priorResponse() != null
            && userResponse.priorResponse().code() == HTTP_CLIENT_TIMEOUT) {
          // We attempted to retry and got another timeout. Give up.
          return null;
        }

        if (retryAfter(userResponse, 0) > 0) {
          return null;
        }

        return userResponse.request();

      case HTTP_UNAVAILABLE:
        if (userResponse.priorResponse() != null
            && userResponse.priorResponse().code() == HTTP_UNAVAILABLE) {
          // We attempted to retry and got another timeout. Give up.
          return null;
        }

        if (retryAfter(userResponse, Integer.MAX_VALUE) == 0) {
          // specifically received an instruction to retry without delay
          return userResponse.request();
        }

        return null;

      default:
        return null;
    }
  }
/**
   * Figures out the HTTP request to make in response to receiving {@code userResponse}. This will
   * either add authentication headers, follow redirects or handle a client request timeout. If a
   * follow-up is either unnecessary or not applicable, this returns null.
   */
Figures out the HTTP request to make in response to receiving  userResponse. This will
either add authentication headers, follow redirects or handle a client request timeout. If a
follow-up is either unnecessary or not applicable, this returns null.
Figures out the HTTP request to make in response to receiving userResponse . This will either add authentication headers follow redirects or handle a client request timeout . If a follow - up is either unnecessary or not applicable this returns null .
private Request followUpRequest(Response userResponse, @Nullable Route route) throws IOException {
    if (userResponse == null) throw new IllegalStateException();
    int responseCode = userResponse.code();
    final String method = userResponse.request().method();
    switch (responseCode) {
      case HTTP_PROXY_AUTH:
        Proxy selectedProxy = route != null
            ? route.proxy()
            : client.proxy();
        if (selectedProxy.type() != Proxy.Type.HTTP) {
          throw new ProtocolException("Received HTTP_PROXY_AUTH (407) code while not using proxy");
        }
        return client.proxyAuthenticator().authenticate(route, userResponse);
      case HTTP_UNAUTHORIZED:
        return client.authenticator().authenticate(route, userResponse);
      case HTTP_PERM_REDIRECT:
      case HTTP_TEMP_REDIRECT:
        if (!method.equals("GET") && !method.equals("HEAD")) {
          return null;
        }
      case HTTP_MULT_CHOICE:
      case HTTP_MOVED_PERM:
      case HTTP_MOVED_TEMP:
      case HTTP_SEE_OTHER:
        if (!client.followRedirects()) return null;
        String location = userResponse.header("Location");
        if (location == null) return null;
        HttpUrl url = userResponse.request().url().resolve(location);
        if (url == null) return null;
        boolean sameScheme = url.scheme().equals(userResponse.request().url().scheme());
        if (!sameScheme && !client.followSslRedirects()) return null;
        Request.Builder requestBuilder = userResponse.request().newBuilder();
        if (HttpMethod.permitsRequestBody(method)) {
          final boolean maintainBody = HttpMethod.redirectsWithBody(method);
          if (HttpMethod.redirectsToGet(method)) {
            requestBuilder.method("GET", null);
          } else {
            RequestBody requestBody = maintainBody ? userResponse.request().body() : null;
            requestBuilder.method(method, requestBody);
          }
          if (!maintainBody) {
            requestBuilder.removeHeader("Transfer-Encoding");
            requestBuilder.removeHeader("Content-Length");
            requestBuilder.removeHeader("Content-Type");
          }
        }
        if (!sameConnection(userResponse.request().url(), url)) {
          requestBuilder.removeHeader("Authorization");
        }
        return requestBuilder.url(url).build();
      case HTTP_CLIENT_TIMEOUT:
        if (!client.retryOnConnectionFailure()) {
          return null;
        }
        RequestBody requestBody = userResponse.request().body();
        if (requestBody != null && requestBody.isOneShot()) {
          return null;
        }
        if (userResponse.priorResponse() != null
            && userResponse.priorResponse().code() == HTTP_CLIENT_TIMEOUT) {
          return null;
        }
        if (retryAfter(userResponse, 0) > 0) {
          return null;
        }
        return userResponse.request();
      case HTTP_UNAVAILABLE:
        if (userResponse.priorResponse() != null
            && userResponse.priorResponse().code() == HTTP_UNAVAILABLE) {
          return null;
        }
        if (retryAfter(userResponse, Integer.MAX_VALUE) == 0) {
          return userResponse.request();
        }
        return null;
      default:
        return null;
    }
  }
private Request followUpRequest ( Response userResponse , @ Nullable Route route ) throws IOException { if ( userResponse == null ) throw new IllegalStateException ( ) ; int responseCode = userResponse . code ( ) ; final String method = userResponse . request ( ) . method ( ) ; switch ( responseCode ) { case HTTP_PROXY_AUTH : Proxy selectedProxy = route != null ? route . proxy ( ) : client . proxy ( ) ; if ( selectedProxy . type ( ) != Proxy . Type . HTTP ) { throw new ProtocolException ( "Received HTTP_PROXY_AUTH (407) code while not using proxy" ) ; } return client . proxyAuthenticator ( ) . authenticate ( route , userResponse ) ; case HTTP_UNAUTHORIZED : return client . authenticator ( ) . authenticate ( route , userResponse ) ; case HTTP_PERM_REDIRECT : case HTTP_TEMP_REDIRECT : if ( ! method . equals ( "GET" ) && ! method . equals ( "HEAD" ) ) { return null ; } case HTTP_MULT_CHOICE : case HTTP_MOVED_PERM : case HTTP_MOVED_TEMP : case HTTP_SEE_OTHER : if ( ! client . followRedirects ( ) ) return null ; String location = userResponse . header ( "Location" ) ; if ( location == null ) return null ; HttpUrl url = userResponse . request ( ) . url ( ) . resolve ( location ) ; if ( url == null ) return null ; boolean sameScheme = url . scheme ( ) . equals ( userResponse . request ( ) . url ( ) . scheme ( ) ) ; if ( ! sameScheme && ! client . followSslRedirects ( ) ) return null ; Request . Builder requestBuilder = userResponse . request ( ) . newBuilder ( ) ; if ( HttpMethod . permitsRequestBody ( method ) ) { final boolean maintainBody = HttpMethod . redirectsWithBody ( method ) ; if ( HttpMethod . redirectsToGet ( method ) ) { requestBuilder . method ( "GET" , null ) ; } else { RequestBody requestBody = maintainBody ? userResponse . request ( ) . body ( ) : null ; requestBuilder . method ( method , requestBody ) ; } if ( ! maintainBody ) { requestBuilder . removeHeader ( "Transfer-Encoding" ) ; requestBuilder . removeHeader ( "Content-Length" ) ; requestBuilder . removeHeader ( "Content-Type" ) ; } } if ( ! sameConnection ( userResponse . request ( ) . url ( ) , url ) ) { requestBuilder . removeHeader ( "Authorization" ) ; } return requestBuilder . url ( url ) . build ( ) ; case HTTP_CLIENT_TIMEOUT : if ( ! client . retryOnConnectionFailure ( ) ) { return null ; } RequestBody requestBody = userResponse . request ( ) . body ( ) ; if ( requestBody != null && requestBody . isOneShot ( ) ) { return null ; } if ( userResponse . priorResponse ( ) != null && userResponse . priorResponse ( ) . code ( ) == HTTP_CLIENT_TIMEOUT ) { return null ; } if ( retryAfter ( userResponse , 0 ) > 0 ) { return null ; } return userResponse . request ( ) ; case HTTP_UNAVAILABLE : if ( userResponse . priorResponse ( ) != null && userResponse . priorResponse ( ) . code ( ) == HTTP_UNAVAILABLE ) { return null ; } if ( retryAfter ( userResponse , Integer . MAX_VALUE ) == 0 ) { return userResponse . request ( ) ; } return null ; default : return null ; } }
Figures out the HTTP request to make in response to receiving {@code userResponse}.
Figures out the HTTP request to make in response to receiving { @code userResponse } .
// "If the 307 or 308 status code is received in response to a request other than GET // or HEAD, the user agent MUST NOT automatically redirect the request" // fall-through // Does the client allow redirects? // Don't follow redirects to unsupported protocols. // If configured, don't follow redirects between SSL and non-SSL. // Most redirects don't include a request body. // When redirecting across hosts, drop all authentication headers. This // is potentially annoying to the application layer since they have no // way to retain them. // 408's are rare in practice, but some servers like HAProxy use this response code. The // spec says that we may repeat the request without modifications. Modern browsers also // repeat the request (even non-idempotent ones.) // The application layer has directed us not to retry the request. // We attempted to retry and got another timeout. Give up. // We attempted to retry and got another timeout. Give up. // specifically received an instruction to retry without delay