Search in sources :

Example 11 with RealConnection

use of okhttp3.internal.connection.RealConnection in project okhttp by square.

the class HttpOverHttp2Test method connectionShutdownAfterHealthCheck.

/**
   * This simulates a race condition where we receive a healthy HTTP/2 connection and just prior to
   * writing our request, we get a GOAWAY frame from the server.
   */
@Test
public void connectionShutdownAfterHealthCheck() throws Exception {
    server.enqueue(new MockResponse().setSocketPolicy(SocketPolicy.DISCONNECT_AT_END).setBody("ABC"));
    server.enqueue(new MockResponse().setBody("DEF"));
    OkHttpClient client2 = client.newBuilder().addNetworkInterceptor(new Interceptor() {

        boolean executedCall;

        @Override
        public Response intercept(Chain chain) throws IOException {
            if (!executedCall) {
                // At this point, we have a healthy HTTP/2 connection. This call will trigger the
                // server to send a GOAWAY frame, leaving the connection in a shutdown state.
                executedCall = true;
                Call call = client.newCall(new Request.Builder().url(server.url("/")).build());
                Response response = call.execute();
                assertEquals("ABC", response.body().string());
                // Wait until the GOAWAY has been processed.
                RealConnection connection = (RealConnection) chain.connection();
                while (connection.isHealthy(false)) ;
            }
            return chain.proceed(chain.request());
        }
    }).build();
    Call call = client2.newCall(new Request.Builder().url(server.url("/")).build());
    Response response = call.execute();
    assertEquals("DEF", response.body().string());
    assertEquals(0, server.takeRequest().getSequenceNumber());
    assertEquals(0, server.takeRequest().getSequenceNumber());
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) Response(okhttp3.Response) MockResponse(okhttp3.mockwebserver.MockResponse) Call(okhttp3.Call) RealConnection(okhttp3.internal.connection.RealConnection) OkHttpClient(okhttp3.OkHttpClient) Request(okhttp3.Request) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) Interceptor(okhttp3.Interceptor) Test(org.junit.Test)

Example 12 with RealConnection

use of okhttp3.internal.connection.RealConnection in project okhttp by square.

the class StreamAllocation method cancel.

public void cancel() {
    HttpCodec codecToCancel;
    RealConnection connectionToCancel;
    synchronized (connectionPool) {
        canceled = true;
        codecToCancel = codec;
        connectionToCancel = connection;
    }
    if (codecToCancel != null) {
        codecToCancel.cancel();
    } else if (connectionToCancel != null) {
        connectionToCancel.cancel();
    }
}
Also used : HttpCodec(okhttp3.internal.http.HttpCodec)

Example 13 with RealConnection

use of okhttp3.internal.connection.RealConnection in project okhttp by square.

the class StreamAllocation method newStream.

public HttpCodec newStream(OkHttpClient client, boolean doExtensiveHealthChecks) {
    int connectTimeout = client.connectTimeoutMillis();
    int readTimeout = client.readTimeoutMillis();
    int writeTimeout = client.writeTimeoutMillis();
    boolean connectionRetryEnabled = client.retryOnConnectionFailure();
    try {
        RealConnection resultConnection = findHealthyConnection(connectTimeout, readTimeout, writeTimeout, connectionRetryEnabled, doExtensiveHealthChecks);
        HttpCodec resultCodec = resultConnection.newCodec(client, this);
        synchronized (connectionPool) {
            codec = resultCodec;
            return resultCodec;
        }
    } catch (IOException e) {
        throw new RouteException(e);
    }
}
Also used : HttpCodec(okhttp3.internal.http.HttpCodec) IOException(java.io.IOException)

Example 14 with RealConnection

use of okhttp3.internal.connection.RealConnection in project okhttp by square.

the class RealInterceptorChain method proceed.

public Response proceed(Request request, StreamAllocation streamAllocation, HttpCodec httpCodec, RealConnection 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 && !this.connection.supportsUrl(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);
    Interceptor interceptor = interceptors.get(index);
    Response response = interceptor.intercept(next);
    // 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;
}
Also used : Response(okhttp3.Response) Interceptor(okhttp3.Interceptor)

Aggregations

RealConnection (okhttp3.internal.connection.RealConnection)8 Test (org.junit.Test)6 HttpCodec (okhttp3.internal.http.HttpCodec)3 IOException (java.io.IOException)2 Interceptor (okhttp3.Interceptor)2 Request (okhttp3.Request)2 Response (okhttp3.Response)2 StreamAllocation (okhttp3.internal.connection.StreamAllocation)2 Socket (java.net.Socket)1 ArrayList (java.util.ArrayList)1 Call (okhttp3.Call)1 OkHttpClient (okhttp3.OkHttpClient)1 Route (okhttp3.Route)1 RealInterceptorChain (okhttp3.internal.http.RealInterceptorChain)1 MockResponse (okhttp3.mockwebserver.MockResponse)1 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)1