@Test public void createJavaUrlConnection_https_extraHttpsMethods() throws Exception { Request okRequest = createArbitraryOkRequest().newBuilder() .method("GET", null) .url("https://secure/request") .build(); Handshake handshake = Handshake.get("SecureCipher", Arrays.<Certificate>asList(SERVER_CERT), Arrays.<Certificate>asList(LOCAL_CERT)); Response okResponse = createArbitraryOkResponse(okRequest).newBuilder() .handshake(handshake) .build(); HttpsURLConnection httpsUrlConnection = (HttpsURLConnection) JavaApiConverter.createJavaUrlConnection(okResponse); assertEquals("SecureCipher", httpsUrlConnection.getCipherSuite()); assertEquals(SERVER_CERT.getSubjectX500Principal(), httpsUrlConnection.getPeerPrincipal()); assertArrayEquals(new Certificate[] { LOCAL_CERT }, httpsUrlConnection.getLocalCertificates()); assertArrayEquals(new Certificate[] { SERVER_CERT }, httpsUrlConnection.getServerCertificates()); assertEquals(LOCAL_CERT.getSubjectX500Principal(), httpsUrlConnection.getLocalPrincipal()); }
public Certificate[] getLocalCertificates() { Handshake handshake = handshake(); if (handshake == null) { return null; } List<Certificate> result = handshake.localCertificates(); if (result.isEmpty()) { return null; } return (Certificate[]) result.toArray(new Certificate[result.size()]); }
public Certificate[] getServerCertificates() throws SSLPeerUnverifiedException { Handshake handshake = handshake(); if (handshake == null) { return null; } List<Certificate> result = handshake.peerCertificates(); if (result.isEmpty()) { return null; } return (Certificate[]) result.toArray(new Certificate[result.size()]); }
protected Handshake handshake() { if (this.delegate.httpEngine != null) { return this.delegate.httpEngine.hasResponse() ? this.delegate.httpEngine.getResponse ().handshake() : this.delegate.handshake; } else { throw new IllegalStateException("Connection has not yet been established"); } }
@Override protected Handshake handshake() { if (delegate.httpEngine == null) { throw new IllegalStateException("Connection has not yet been established"); } // If there's a response, get the handshake from there so that caching // works. Otherwise get the handshake from the connection because we might // have not connected yet. return delegate.httpEngine.hasResponse() ? delegate.httpEngine.getResponse().handshake() : delegate.handshake; }
@Test public void createJavaCacheResponse_httpsPost() throws Exception { Request okRequest = createArbitraryOkRequest().newBuilder() .url("https://secure/request") .method("POST", createRequestBody("RequestBody") ) .build(); String statusLine = "HTTP/1.1 200 Fantastic"; Response.Body responseBody = createResponseBody("text/plain", "ResponseBody".getBytes(StandardCharsets.UTF_8)); Handshake handshake = Handshake.get("SecureCipher", Arrays.<Certificate>asList(SERVER_CERT), Arrays.<Certificate>asList(LOCAL_CERT)); Response okResponse = createArbitraryOkResponse(okRequest).newBuilder() .statusLine(statusLine) .addHeader("key1", "value1_1") .addHeader("key2", "value2") .addHeader("key1", "value1_2") .body(responseBody) .handshake(handshake) .build(); SecureCacheResponse javaCacheResponse = (SecureCacheResponse) JavaApiConverter.createJavaCacheResponse(okResponse); Map<String, List<String>> javaHeaders = javaCacheResponse.getHeaders(); assertEquals(Arrays.asList("value1_1", "value1_2"), javaHeaders.get("key1")); assertEquals(Arrays.asList(statusLine), javaHeaders.get(null)); assertArrayEquals(responseBody.bytes(), readAll(javaCacheResponse.getBody())); assertEquals(handshake.cipherSuite(), javaCacheResponse.getCipherSuite()); assertEquals(handshake.localCertificates(), javaCacheResponse.getLocalCertificateChain()); assertEquals(handshake.peerCertificates(), javaCacheResponse.getServerCertificateChain()); assertEquals(handshake.localPrincipal(), javaCacheResponse.getLocalPrincipal()); assertEquals(handshake.peerPrincipal(), javaCacheResponse.getPeerPrincipal()); }
public String getCipherSuite() { Handshake handshake = handshake(); return handshake != null ? handshake.cipherSuite() : null; }
public Principal getPeerPrincipal() throws SSLPeerUnverifiedException { Handshake handshake = handshake(); return handshake != null ? handshake.peerPrincipal() : null; }
public Principal getLocalPrincipal() { Handshake handshake = handshake(); return handshake != null ? handshake.localPrincipal() : null; }
public Handshake getHandshake() { return this.handshake; }
@Override public String getCipherSuite() { Handshake handshake = handshake(); return handshake != null ? handshake.cipherSuite() : null; }
@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; }
@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; }
@Override public Principal getPeerPrincipal() throws SSLPeerUnverifiedException { Handshake handshake = handshake(); return handshake != null ? handshake.peerPrincipal() : null; }
@Override public Principal getLocalPrincipal() { Handshake handshake = handshake(); return handshake != null ? handshake.localPrincipal() : null; }
/** * 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. */ public static Response createOkResponse(URI uri, URLConnection urlConnection) throws IOException { HttpURLConnection httpUrlConnection = (HttpURLConnection) urlConnection; Response.Builder okResponseBuilder = new Response.Builder(); // Request: Create one from the URL connection. // A connected HttpURLConnection does not permit access to request headers. Map<String, List<String>> requestHeaders = null; Request okRequest = createOkRequest(uri, httpUrlConnection.getRequestMethod(), requestHeaders); okResponseBuilder.request(okRequest); // Status line StatusLine statusLine = StatusLine.parse(extractStatusLine(httpUrlConnection)); okResponseBuilder.protocol(statusLine.protocol); okResponseBuilder.code(statusLine.code); okResponseBuilder.message(statusLine.message); // Response headers Headers okHeaders = extractOkResponseHeaders(httpUrlConnection); okResponseBuilder.headers(okHeaders); // Response body ResponseBody okBody = createOkBody(okHeaders, urlConnection.getInputStream()); 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(); Handshake handshake = Handshake.get( httpsUrlConnection.getCipherSuite(), nullSafeImmutableList(peerCertificates), nullSafeImmutableList(localCertificates)); okResponseBuilder.handshake(handshake); } return okResponseBuilder.build(); }
/** * Creates an OkHttp {@link Response} using the supplied {@link Request} and {@link CacheResponse} * to supply the data. */ static Response createOkResponse(Request request, CacheResponse javaResponse) throws IOException { Response.Builder okResponseBuilder = new Response.Builder(); // Request: Use the one provided. okResponseBuilder.request(request); // Status line: Java has this as one of the headers. StatusLine statusLine = StatusLine.parse(extractStatusLine(javaResponse)); okResponseBuilder.protocol(statusLine.protocol); okResponseBuilder.code(statusLine.code); okResponseBuilder.message(statusLine.message); // Response headers Headers okHeaders = extractOkHeaders(javaResponse); okResponseBuilder.headers(okHeaders); // Response body ResponseBody okBody = createOkBody(okHeaders, javaResponse.getBody()); okResponseBuilder.body(okBody); // Handle SSL handshake information as needed. if (javaResponse instanceof SecureCacheResponse) { SecureCacheResponse javaSecureCacheResponse = (SecureCacheResponse) javaResponse; // Handshake doesn't support null lists. List<Certificate> peerCertificates; try { peerCertificates = javaSecureCacheResponse.getServerCertificateChain(); } catch (SSLPeerUnverifiedException e) { peerCertificates = Collections.emptyList(); } List<Certificate> localCertificates = javaSecureCacheResponse.getLocalCertificateChain(); if (localCertificates == null) { localCertificates = Collections.emptyList(); } Handshake handshake = Handshake.get( javaSecureCacheResponse.getCipherSuite(), peerCertificates, localCertificates); okResponseBuilder.handshake(handshake); } return okResponseBuilder.build(); }
@Override protected Handshake handshake() { return delegate.response.handshake(); }
/** * 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. */ public static Response createOkResponse(URI uri, URLConnection urlConnection) throws IOException { HttpURLConnection httpUrlConnection = (HttpURLConnection) urlConnection; Response.Builder okResponseBuilder = new Response.Builder(); // Request: Create one from the URL connection. // A connected HttpURLConnection does not permit access to request headers. Map<String, List<String>> requestHeaders = null; Request okRequest = createOkRequest(uri, httpUrlConnection.getRequestMethod(), requestHeaders); okResponseBuilder.request(okRequest); // Status line String statusLine = extractStatusLine(httpUrlConnection); okResponseBuilder.statusLine(statusLine); // Response headers Headers okHeaders = extractOkResponseHeaders(httpUrlConnection); okResponseBuilder.headers(okHeaders); // Meta data: Defaulted okResponseBuilder.setResponseSource(ResponseSource.NETWORK); // Response body Response.Body okBody = createOkBody(okHeaders, urlConnection.getInputStream()); 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(); Handshake handshake = Handshake.get( httpsUrlConnection.getCipherSuite(), nullSafeImmutableList(peerCertificates), nullSafeImmutableList(localCertificates)); okResponseBuilder.handshake(handshake); } return okResponseBuilder.build(); }
/** * Creates an OkHttp {@link Response} using the supplied {@link Request} and {@link CacheResponse} * to supply the data. */ static Response createOkResponse(Request request, CacheResponse javaResponse) throws IOException { Response.Builder okResponseBuilder = new Response.Builder(); // Request: Use the one provided. okResponseBuilder.request(request); // Status line: Java has this as one of the headers. okResponseBuilder.statusLine(extractStatusLine(javaResponse)); // Response headers Headers okHeaders = extractOkHeaders(javaResponse); okResponseBuilder.headers(okHeaders); // Meta data: Defaulted okResponseBuilder.setResponseSource(ResponseSource.CACHE); // Response body Response.Body okBody = createOkBody(okHeaders, javaResponse.getBody()); okResponseBuilder.body(okBody); // Handle SSL handshake information as needed. if (javaResponse instanceof SecureCacheResponse) { SecureCacheResponse javaSecureCacheResponse = (SecureCacheResponse) javaResponse; // Handshake doesn't support null lists. List<Certificate> peerCertificates; try { peerCertificates = javaSecureCacheResponse.getServerCertificateChain(); } catch (SSLPeerUnverifiedException e) { peerCertificates = Collections.emptyList(); } List<Certificate> localCertificates = javaSecureCacheResponse.getLocalCertificateChain(); if (localCertificates == null) { localCertificates = Collections.emptyList(); } Handshake handshake = Handshake.get( javaSecureCacheResponse.getCipherSuite(), peerCertificates, localCertificates); okResponseBuilder.handshake(handshake); } return okResponseBuilder.build(); }
private void testCreateOkResponseInternal(HttpURLConnectionFactory httpUrlConnectionFactory, boolean isSecure) throws Exception { String statusLine = "HTTP/1.1 200 Fantastic"; String body = "Nothing happens"; final URL serverUrl; MockResponse mockResponse = new MockResponse() .setStatus(statusLine) .addHeader("xyzzy", "baz") .setBody(body); if (isSecure) { serverUrl = configureHttpsServer( mockResponse); assertEquals("https", serverUrl.getProtocol()); } else { serverUrl = configureServer( mockResponse); assertEquals("http", serverUrl.getProtocol()); } connection = httpUrlConnectionFactory.open(serverUrl); if (isSecure) { HttpsURLConnection httpsUrlConnection = (HttpsURLConnection) connection; httpsUrlConnection.setSSLSocketFactory(sslContext.getSocketFactory()); httpsUrlConnection.setHostnameVerifier(NULL_HOSTNAME_VERIFIER); } connection.setRequestProperty("snake", "bird"); connection.connect(); Response response = JavaApiConverter.createOkResponse(serverUrl.toURI(), connection); // Check the response.request() Request request = response.request(); assertEquals(isSecure, request.isHttps()); assertEquals(serverUrl.toURI(), request.uri()); assertNull(request.body()); Headers okRequestHeaders = request.headers(); // In Java the request headers are unavailable for a connected HttpURLConnection. assertEquals(0, okRequestHeaders.size()); assertEquals("GET", request.method()); // Check the response assertEquals(statusLine, response.statusLine()); Headers okResponseHeaders = response.headers(); assertEquals("baz", okResponseHeaders.get("xyzzy")); assertEquals(body, response.body().string()); if (isSecure) { Handshake handshake = response.handshake(); assertNotNull(handshake); HttpsURLConnection httpsURLConnection = (HttpsURLConnection) connection; assertNotNullAndEquals(httpsURLConnection.getCipherSuite(), handshake.cipherSuite()); assertEquals(httpsURLConnection.getLocalPrincipal(), handshake.localPrincipal()); assertNotNullAndEquals(httpsURLConnection.getPeerPrincipal(), handshake.peerPrincipal()); assertNotNull(httpsURLConnection.getServerCertificates()); assertEquals(Arrays.asList(httpsURLConnection.getServerCertificates()), handshake.peerCertificates()); assertNull(httpsURLConnection.getLocalCertificates()); } else { assertNull(response.handshake()); } }
@Test public void createOkResponse_fromSecureCacheResponse() throws Exception { final String statusLine = "HTTP/1.1 200 Fantastic"; final Principal localPrincipal = LOCAL_CERT.getSubjectX500Principal(); final List<Certificate> localCertificates = Arrays.<Certificate>asList(LOCAL_CERT); final Principal serverPrincipal = SERVER_CERT.getSubjectX500Principal(); final List<Certificate> serverCertificates = Arrays.<Certificate>asList(SERVER_CERT); URI uri = new URI("https://foo/bar"); Request request = new Request.Builder().url(uri.toURL()).method("GET", null).build(); SecureCacheResponse cacheResponse = new SecureCacheResponse() { @Override public Map<String, List<String>> getHeaders() throws IOException { Map<String, List<String>> headers = new HashMap<String, List<String>>(); headers.put(null, Collections.singletonList(statusLine)); headers.put("xyzzy", Arrays.asList("bar", "baz")); return headers; } @Override public InputStream getBody() throws IOException { return new ByteArrayInputStream("HelloWorld".getBytes(StandardCharsets.UTF_8)); } @Override public String getCipherSuite() { return "SuperSecure"; } @Override public List<Certificate> getLocalCertificateChain() { return localCertificates; } @Override public List<Certificate> getServerCertificateChain() throws SSLPeerUnverifiedException { return serverCertificates; } @Override public Principal getPeerPrincipal() throws SSLPeerUnverifiedException { return serverPrincipal; } @Override public Principal getLocalPrincipal() { return localPrincipal; } }; Response response = JavaApiConverter.createOkResponse(request, cacheResponse); assertSame(request, response.request()); assertNotNullAndEquals(statusLine, response.statusLine()); Headers okResponseHeaders = response.headers(); assertEquals("baz", okResponseHeaders.get("xyzzy")); assertEquals("HelloWorld", response.body().string()); Handshake handshake = response.handshake(); assertNotNull(handshake); assertNotNullAndEquals("SuperSecure", handshake.cipherSuite()); assertEquals(localPrincipal, handshake.localPrincipal()); assertEquals(serverPrincipal, handshake.peerPrincipal()); assertEquals(serverCertificates, handshake.peerCertificates()); assertEquals(localCertificates, handshake.localCertificates()); }
protected abstract Handshake handshake();