@@ -90,6 +90,7 @@ public void checkServerTrusted(X509Certificate[] certs, String authType) { }
9090 private byte [] mNpnProtocols = null ;
9191
9292 private final int mHandshakeTimeoutMillis ;
93+ private final int mWriteTimeoutMillis ;
9394 private final SSLClientSessionCache mSessionCache ;
9495 private final boolean mSecure ;
9596
@@ -100,12 +101,21 @@ public SSLCertificateSocketFactory(int handshakeTimeoutMillis) {
100101 }
101102
102103 private SSLCertificateSocketFactory (
103- int handshakeTimeoutMillis , SSLSessionCache cache , boolean secure ) {
104+ int handshakeTimeoutMillis ,
105+ int writeTimeoutMillis ,
106+ SSLSessionCache cache ,
107+ boolean secure ) {
104108 mHandshakeTimeoutMillis = handshakeTimeoutMillis ;
109+ mWriteTimeoutMillis = writeTimeoutMillis ;
105110 mSessionCache = cache == null ? null : cache .mSessionCache ;
106111 mSecure = secure ;
107112 }
108113
114+ private SSLCertificateSocketFactory (
115+ int handshakeTimeoutMillis , SSLSessionCache cache , boolean secure ) {
116+ this (handshakeTimeoutMillis , 0 , cache , secure );
117+ }
118+
109119 /**
110120 * Returns a new socket factory instance with an optional handshake timeout.
111121 *
@@ -161,6 +171,24 @@ public static org.apache.http.conn.ssl.SSLSocketFactory getHttpSocketFactory(
161171 new SSLCertificateSocketFactory (handshakeTimeoutMillis , cache , true ));
162172 }
163173
174+ /**
175+ * Returns a socket factory (also named SSLSocketFactory, but in a different
176+ * namespace) for use with the Apache HTTP stack.
177+ *
178+ * @param handshakeTimeoutMillis to use for SSL connection handshake, or 0
179+ * for none. The socket timeout is reset to 0 after the handshake.
180+ * @param writeTimeoutMillis the desired write timeout in milliseconds or 0 for none.
181+ * @param cache The {@link SSLSessionCache} to use, or null for no cache.
182+ * @return a new SocketFactory with the specified parameters
183+ */
184+ public static org .apache .http .conn .ssl .SSLSocketFactory getHttpSocketFactory (
185+ int handshakeTimeoutMillis ,
186+ int writeTimeoutMillis ,
187+ SSLSessionCache cache ) {
188+ return new org .apache .http .conn .ssl .SSLSocketFactory (new SSLCertificateSocketFactory (
189+ handshakeTimeoutMillis , writeTimeoutMillis , cache , true ));
190+ }
191+
164192 /**
165193 * Verify the hostname of the certificate used by the other end of a
166194 * connected socket. You MUST call this if you did not supply a hostname
@@ -376,6 +404,7 @@ public Socket createSocket(Socket k, String host, int port, boolean close) throw
376404 OpenSSLSocketImpl s = (OpenSSLSocketImpl ) getDelegate ().createSocket (k , host , port , close );
377405 s .setNpnProtocols (mNpnProtocols );
378406 s .setHandshakeTimeout (mHandshakeTimeoutMillis );
407+ s .setSoWriteTimeout (mWriteTimeoutMillis );
379408 if (mSecure ) {
380409 verifyHostname (s , host );
381410 }
@@ -395,6 +424,7 @@ public Socket createSocket() throws IOException {
395424 OpenSSLSocketImpl s = (OpenSSLSocketImpl ) getDelegate ().createSocket ();
396425 s .setNpnProtocols (mNpnProtocols );
397426 s .setHandshakeTimeout (mHandshakeTimeoutMillis );
427+ s .setSoWriteTimeout (mWriteTimeoutMillis );
398428 return s ;
399429 }
400430
@@ -412,6 +442,7 @@ public Socket createSocket(InetAddress addr, int port, InetAddress localAddr, in
412442 addr , port , localAddr , localPort );
413443 s .setNpnProtocols (mNpnProtocols );
414444 s .setHandshakeTimeout (mHandshakeTimeoutMillis );
445+ s .setSoWriteTimeout (mWriteTimeoutMillis );
415446 return s ;
416447 }
417448
@@ -427,6 +458,7 @@ public Socket createSocket(InetAddress addr, int port) throws IOException {
427458 OpenSSLSocketImpl s = (OpenSSLSocketImpl ) getDelegate ().createSocket (addr , port );
428459 s .setNpnProtocols (mNpnProtocols );
429460 s .setHandshakeTimeout (mHandshakeTimeoutMillis );
461+ s .setSoWriteTimeout (mWriteTimeoutMillis );
430462 return s ;
431463 }
432464
@@ -443,6 +475,7 @@ public Socket createSocket(String host, int port, InetAddress localAddr, int loc
443475 host , port , localAddr , localPort );
444476 s .setNpnProtocols (mNpnProtocols );
445477 s .setHandshakeTimeout (mHandshakeTimeoutMillis );
478+ s .setSoWriteTimeout (mWriteTimeoutMillis );
446479 if (mSecure ) {
447480 verifyHostname (s , host );
448481 }
@@ -460,6 +493,7 @@ public Socket createSocket(String host, int port) throws IOException {
460493 OpenSSLSocketImpl s = (OpenSSLSocketImpl ) getDelegate ().createSocket (host , port );
461494 s .setNpnProtocols (mNpnProtocols );
462495 s .setHandshakeTimeout (mHandshakeTimeoutMillis );
496+ s .setSoWriteTimeout (mWriteTimeoutMillis );
463497 if (mSecure ) {
464498 verifyHostname (s , host );
465499 }
0 commit comments