Search in sources :

Example 1 with UnrepeatableRequestBody

use of okhttp3.internal.http.UnrepeatableRequestBody in project okhttp by square.

the class RetryAndFollowUpInterceptor method followUpRequest.

/**
   * 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.
   */
private Request followUpRequest(Response userResponse) throws IOException {
    if (userResponse == null)
        throw new IllegalStateException();
    Connection connection = streamAllocation.connection();
    Route route = connection != null ? connection.route() : null;
    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:
            // 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");
                }
            }
            // way to retain them.
            if (!sameConnection(userResponse, url)) {
                requestBuilder.removeHeader("Authorization");
            }
            return requestBuilder.url(url).build();
        case HTTP_CLIENT_TIMEOUT:
            // repeat the request (even non-idempotent ones.)
            if (userResponse.request().body() instanceof UnrepeatableRequestBody) {
                return null;
            }
            return userResponse.request();
        default:
            return null;
    }
}
Also used : ProtocolException(java.net.ProtocolException) Connection(okhttp3.Connection) Request(okhttp3.Request) HttpUrl(okhttp3.HttpUrl) Proxy(java.net.Proxy) Route(okhttp3.Route) RequestBody(okhttp3.RequestBody)

Example 2 with UnrepeatableRequestBody

use of okhttp3.internal.http.UnrepeatableRequestBody in project okhttp by square.

the class RetryAndFollowUpInterceptor method intercept.

@Override
public Response intercept(Chain chain) throws IOException {
    Request request = chain.request();
    streamAllocation = new StreamAllocation(client.connectionPool(), createAddress(request.url()), callStackTrace);
    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);
            releaseConnection = false;
        } catch (RouteException e) {
            // The attempt to connect via a route failed. The request will not have been sent.
            if (!recover(e.getLastConnectException(), false, request)) {
                throw e.getLastConnectException();
            }
            releaseConnection = false;
            continue;
        } catch (IOException e) {
            // An attempt to communicate with a server failed. The request may have been sent.
            boolean requestSendStarted = !(e instanceof ConnectionShutdownException);
            if (!recover(e, requestSendStarted, 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;
    }
}
Also used : StreamAllocation(okhttp3.internal.connection.StreamAllocation) Response(okhttp3.Response) RouteException(okhttp3.internal.connection.RouteException) ProtocolException(java.net.ProtocolException) ConnectionShutdownException(okhttp3.internal.http2.ConnectionShutdownException) Request(okhttp3.Request) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) HttpRetryException(java.net.HttpRetryException)

Example 3 with UnrepeatableRequestBody

use of okhttp3.internal.http.UnrepeatableRequestBody in project Douya by DreaminginCodeZH.

the class AuthenticationInterceptor method intercept.

@Override
public Response intercept(@NonNull Chain chain) throws IOException {
    Authenticator authenticator = getAuthenticator();
    Request request = authenticator.authenticate(chain.request());
    Response response = null;
    for (int retryCount = 0; retryCount < mMaxNumRetries; ++retryCount) {
        response = chain.proceed(request);
        if (response.isSuccessful()) {
            return response;
        }
        Request retryRequest = authenticator.retryAuthentication(response);
        if (retryRequest == null) {
            return response;
        }
        ResponseBody responseBody = response.body();
        if (responseBody != null) {
            responseBody.close();
        }
        if (retryRequest.body() instanceof UnrepeatableRequestBody) {
            throw new HttpRetryException("Cannot retry streamed HTTP body", response.code());
        }
        request = retryRequest;
    }
    return response;
}
Also used : Response(okhttp3.Response) Request(okhttp3.Request) UnrepeatableRequestBody(okhttp3.internal.http.UnrepeatableRequestBody) HttpRetryException(java.net.HttpRetryException) ResponseBody(okhttp3.ResponseBody)

Aggregations

Request (okhttp3.Request)3 HttpRetryException (java.net.HttpRetryException)2 ProtocolException (java.net.ProtocolException)2 Response (okhttp3.Response)2 IOException (java.io.IOException)1 InterruptedIOException (java.io.InterruptedIOException)1 Proxy (java.net.Proxy)1 Connection (okhttp3.Connection)1 HttpUrl (okhttp3.HttpUrl)1 RequestBody (okhttp3.RequestBody)1 ResponseBody (okhttp3.ResponseBody)1 Route (okhttp3.Route)1 RouteException (okhttp3.internal.connection.RouteException)1 StreamAllocation (okhttp3.internal.connection.StreamAllocation)1 UnrepeatableRequestBody (okhttp3.internal.http.UnrepeatableRequestBody)1 ConnectionShutdownException (okhttp3.internal.http2.ConnectionShutdownException)1