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());
}
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());
}
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;
}
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;
}
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();
}
Aggregations