Search in sources :

Example 11 with Handshake

use of okhttp3.Handshake in project okhttp by square.

the class URLConnectionTest method inspectHandshakeThroughoutRequestLifecycle.

@Test
public void inspectHandshakeThroughoutRequestLifecycle() throws Exception {
    server.useHttps(sslClient.socketFactory, false);
    server.enqueue(new MockResponse());
    urlFactory.setClient(urlFactory.client().newBuilder().sslSocketFactory(sslClient.socketFactory, sslClient.trustManager).hostnameVerifier(new RecordingHostnameVerifier()).build());
    HttpsURLConnection httpsConnection = (HttpsURLConnection) urlFactory.open(server.url("/foo").url());
    // Prior to calling connect(), getting the cipher suite is forbidden.
    try {
        httpsConnection.getCipherSuite();
        fail();
    } catch (IllegalStateException expected) {
    }
    // Calling connect establishes a handshake...
    httpsConnection.connect();
    assertNotNull(httpsConnection.getCipherSuite());
    // ...which remains after we read the response body...
    assertContent("", httpsConnection);
    assertNotNull(httpsConnection.getCipherSuite());
    // ...and after we disconnect.
    httpsConnection.disconnect();
    assertNotNull(httpsConnection.getCipherSuite());
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) HttpsURLConnection(javax.net.ssl.HttpsURLConnection) Test(org.junit.Test)

Example 12 with Handshake

use of okhttp3.Handshake in project okhttp by square.

the class CacheTest method secureResponseCaching.

@Test
public void secureResponseCaching() throws IOException {
    server.useHttps(sslClient.socketFactory, false);
    server.enqueue(new MockResponse().addHeader("Last-Modified: " + formatDate(-1, TimeUnit.HOURS)).addHeader("Expires: " + formatDate(1, TimeUnit.HOURS)).setBody("ABC"));
    client = client.newBuilder().sslSocketFactory(sslClient.socketFactory, sslClient.trustManager).hostnameVerifier(NULL_HOSTNAME_VERIFIER).build();
    Request request = new Request.Builder().url(server.url("/")).build();
    Response response1 = client.newCall(request).execute();
    BufferedSource in = response1.body().source();
    assertEquals("ABC", in.readUtf8());
    // OpenJDK 6 fails on this line, complaining that the connection isn't open yet
    CipherSuite cipherSuite = response1.handshake().cipherSuite();
    List<Certificate> localCerts = response1.handshake().localCertificates();
    List<Certificate> serverCerts = response1.handshake().peerCertificates();
    Principal peerPrincipal = response1.handshake().peerPrincipal();
    Principal localPrincipal = response1.handshake().localPrincipal();
    // Cached!
    Response response2 = client.newCall(request).execute();
    assertEquals("ABC", response2.body().string());
    assertEquals(2, cache.requestCount());
    assertEquals(1, cache.networkCount());
    assertEquals(1, cache.hitCount());
    assertEquals(cipherSuite, response2.handshake().cipherSuite());
    assertEquals(localCerts, response2.handshake().localCertificates());
    assertEquals(serverCerts, response2.handshake().peerCertificates());
    assertEquals(peerPrincipal, response2.handshake().peerPrincipal());
    assertEquals(localPrincipal, response2.handshake().localPrincipal());
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) MockResponse(okhttp3.mockwebserver.MockResponse) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) Principal(java.security.Principal) BufferedSource(okio.BufferedSource) Certificate(java.security.cert.Certificate) Test(org.junit.Test)

Example 13 with Handshake

use of okhttp3.Handshake in project okhttp by square.

the class DelegatingHttpsURLConnection method getLocalCertificates.

@Override
public Certificate[] getLocalCertificates() {
    Handshake handshake = handshake();
    if (handshake == null)
        return null;
    List<Certificate> result = handshake.localCertificates();
    return !result.isEmpty() ? result.toArray(new Certificate[result.size()]) : null;
}
Also used : Handshake(okhttp3.Handshake) Certificate(java.security.cert.Certificate)

Example 14 with Handshake

use of okhttp3.Handshake in project okhttp by square.

the class DelegatingHttpsURLConnection method getServerCertificates.

@Override
public Certificate[] getServerCertificates() throws SSLPeerUnverifiedException {
    Handshake handshake = handshake();
    if (handshake == null)
        return null;
    List<Certificate> result = handshake.peerCertificates();
    return !result.isEmpty() ? result.toArray(new Certificate[result.size()]) : null;
}
Also used : Handshake(okhttp3.Handshake) Certificate(java.security.cert.Certificate)

Example 15 with Handshake

use of okhttp3.Handshake in project okhttp by square.

the class JavaApiConverter method createOkResponseForCachePut.

/**
   * Creates an OkHttp {@link Response} using the supplied {@link URI} and {@link URLConnection} to
   * supply the data. The URLConnection is assumed to already be connected. If this method returns
   * {@code null} the response is uncacheable.
   */
public static Response createOkResponseForCachePut(URI uri, URLConnection urlConnection) throws IOException {
    HttpURLConnection httpUrlConnection = (HttpURLConnection) urlConnection;
    Response.Builder okResponseBuilder = new Response.Builder();
    // Request: Create one from the URL connection.
    Headers responseHeaders = createHeaders(urlConnection.getHeaderFields());
    // Some request headers are needed for Vary caching.
    Headers varyHeaders = varyHeaders(urlConnection, responseHeaders);
    if (varyHeaders == null) {
        return null;
    }
    // OkHttp's Call API requires a placeholder body; the real body will be streamed separately.
    String requestMethod = httpUrlConnection.getRequestMethod();
    RequestBody placeholderBody = HttpMethod.requiresRequestBody(requestMethod) ? Util.EMPTY_REQUEST : null;
    Request okRequest = new Request.Builder().url(uri.toString()).method(requestMethod, placeholderBody).headers(varyHeaders).build();
    okResponseBuilder.request(okRequest);
    // Status line
    StatusLine statusLine = StatusLine.parse(extractStatusLine(httpUrlConnection));
    okResponseBuilder.protocol(statusLine.protocol);
    okResponseBuilder.code(statusLine.code);
    okResponseBuilder.message(statusLine.message);
    // A network response is required for the Cache to find any Vary headers it needs.
    Response networkResponse = okResponseBuilder.build();
    okResponseBuilder.networkResponse(networkResponse);
    // Response headers
    Headers okHeaders = extractOkResponseHeaders(httpUrlConnection, okResponseBuilder);
    okResponseBuilder.headers(okHeaders);
    // Response body
    ResponseBody okBody = createOkBody(urlConnection);
    okResponseBuilder.body(okBody);
    // Handle SSL handshake information as needed.
    if (httpUrlConnection instanceof HttpsURLConnection) {
        HttpsURLConnection httpsUrlConnection = (HttpsURLConnection) httpUrlConnection;
        Certificate[] peerCertificates;
        try {
            peerCertificates = httpsUrlConnection.getServerCertificates();
        } catch (SSLPeerUnverifiedException e) {
            peerCertificates = null;
        }
        Certificate[] localCertificates = httpsUrlConnection.getLocalCertificates();
        String cipherSuiteString = httpsUrlConnection.getCipherSuite();
        CipherSuite cipherSuite = CipherSuite.forJavaName(cipherSuiteString);
        Handshake handshake = Handshake.get(null, cipherSuite, nullSafeImmutableList(peerCertificates), nullSafeImmutableList(localCertificates));
        okResponseBuilder.handshake(handshake);
    }
    return okResponseBuilder.build();
}
Also used : HttpHeaders(okhttp3.internal.http.HttpHeaders) Headers(okhttp3.Headers) JavaNetHeaders(okhttp3.internal.JavaNetHeaders) CipherSuite(okhttp3.CipherSuite) SSLPeerUnverifiedException(javax.net.ssl.SSLPeerUnverifiedException) CacheRequest(okhttp3.internal.cache.CacheRequest) Request(okhttp3.Request) ResponseBody(okhttp3.ResponseBody) CacheResponse(java.net.CacheResponse) Response(okhttp3.Response) SecureCacheResponse(java.net.SecureCacheResponse) StatusLine(okhttp3.internal.http.StatusLine) HttpURLConnection(java.net.HttpURLConnection) HttpsURLConnection(javax.net.ssl.HttpsURLConnection) RequestBody(okhttp3.RequestBody) Certificate(java.security.cert.Certificate) Handshake(okhttp3.Handshake)

Aggregations

Handshake (okhttp3.Handshake)9 Certificate (java.security.cert.Certificate)8 Test (org.junit.Test)8 Request (okhttp3.Request)7 CacheResponse (java.net.CacheResponse)6 SecureCacheResponse (java.net.SecureCacheResponse)6 Response (okhttp3.Response)6 MockResponse (okhttp3.mockwebserver.MockResponse)5 SSLPeerUnverifiedException (javax.net.ssl.SSLPeerUnverifiedException)4 Headers (okhttp3.Headers)4 ResponseBody (okhttp3.ResponseBody)4 IOException (java.io.IOException)3 List (java.util.List)3 HttpsURLConnection (javax.net.ssl.HttpsURLConnection)3 JavaNetHeaders (okhttp3.internal.JavaNetHeaders)3 HttpHeaders (okhttp3.internal.http.HttpHeaders)3 Principal (java.security.Principal)2 X509Certificate (java.security.cert.X509Certificate)2 CipherSuite (okhttp3.CipherSuite)2 ConnectionSpec (okhttp3.ConnectionSpec)2