return inputStream;
}
+ private Socket getSocket() {
+ return sslSocket != null ? sslSocket : socket;
+ }
+
public Address getAddress() {
return address;
}
return true;
}
- if (inputStream.available() > 0) {
+ InputStream in = getInputStream();
+ if (in.available() > 0) {
return false;
}
+ Socket socket = getSocket();
int soTimeout = socket.getSoTimeout();
try {
socket.setSoTimeout(1);
- inputStream.mark(1);
- int byteRead = inputStream.read();
+ in.mark(1);
+ int byteRead = in.read();
if (byteRead != -1) {
- inputStream.reset();
+ in.reset();
return false;
}
return true; // the socket is reporting all data read; it's stale
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLPeerUnverifiedException;
import javax.net.ssl.SSLSocket;
+import org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection;
import org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl;
/**
super.connect();
// make SSL Tunnel
- if (usingProxy()) {
+ if (requiresTunnel()) {
String originalMethod = method;
method = CONNECT;
intermediateResponse = true;
setUpTransportIO(connection);
}
+ @Override protected void setUpTransportIO(HttpConnection connection) throws IOException {
+ if (!requiresTunnel() && sslSocket == null) {
+ return; // don't initialize streams that won't be used
+ }
+ super.setUpTransportIO(connection);
+ }
+
@Override protected boolean requiresTunnel() {
return usingProxy();
}
server.enqueue(new MockResponse().setBody("another response via HTTPS"));
server.play();
+ HttpsURLConnection connection = (HttpsURLConnection) server.getUrl("/").openConnection();
+ connection.setSSLSocketFactory(testSSLContext.clientContext.getSocketFactory());
+ assertContent("this response comes via HTTPS", connection);
+
+ connection = (HttpsURLConnection) server.getUrl("/").openConnection();
+ connection.setSSLSocketFactory(testSSLContext.clientContext.getSocketFactory());
+ assertContent("another response via HTTPS", connection);
+
+ assertEquals(0, server.takeRequest().getSequenceNumber());
+ assertEquals(1, server.takeRequest().getSequenceNumber());
+ }
+
+ public void testConnectViaHttpsReusingConnectionsDiffeerentFactories()
+ throws IOException, InterruptedException {
+ TestSSLContext testSSLContext = TestSSLContext.create();
+
+ server.useHttps(testSSLContext.serverContext.getSocketFactory(), false);
+ server.enqueue(new MockResponse().setBody("this response comes via HTTPS"));
+ server.enqueue(new MockResponse().setBody("another response via HTTPS"));
+ server.play();
+
// install a custom SSL socket factory so the server can be authorized
HttpsURLConnection connection = (HttpsURLConnection) server.getUrl("/").openConnection();
connection.setSSLSocketFactory(testSSLContext.clientContext.getSocketFactory());
assertContent("this response comes via HTTPS", connection);
- // without an SSL socket factory, the connection should fail
connection = (HttpsURLConnection) server.getUrl("/").openConnection();
try {
readAscii(connection.getInputStream(), Integer.MAX_VALUE);
- fail();
+ fail("without an SSL socket factory, the connection should fail");
} catch (SSLException expected) {
}
}