Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NUTCH-2447 Work-around SSLProtocolException: handshake alert: unrecognized_name #305

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -124,28 +124,29 @@ public HttpResponse(HttpBase http, URL url, CrawlDatum datum)
socket.connect(sockAddr, http.getTimeout());

if (scheme == Scheme.HTTPS) {
SSLSocketFactory factory = (SSLSocketFactory) SSLSocketFactory
.getDefault();
SSLSocket sslsocket = (SSLSocket) factory
.createSocket(socket, sockHost, sockPort, true);
sslsocket.setUseClientMode(true);

// Get the protocols and ciphers supported by this JVM
Set<String> protocols = new HashSet<String>(
Arrays.asList(sslsocket.getSupportedProtocols()));
Set<String> ciphers = new HashSet<String>(
Arrays.asList(sslsocket.getSupportedCipherSuites()));

// Intersect with preferred protocols and ciphers
protocols.retainAll(http.getTlsPreferredProtocols());
ciphers.retainAll(http.getTlsPreferredCipherSuites());

sslsocket.setEnabledProtocols(
protocols.toArray(new String[protocols.size()]));
sslsocket.setEnabledCipherSuites(
ciphers.toArray(new String[ciphers.size()]));

sslsocket.startHandshake();
SSLSocket sslsocket = null;

try {
sslsocket = getSSLSocket(socket, sockHost, sockPort);
sslsocket.startHandshake();
} catch (IOException e) {
Http.LOG.debug("SSL connection to {} failed with: {}", url,
e.getMessage());
if ("handshake alert: unrecognized_name".equals(e.getMessage())) {
try {
// Reconnect, see NUTCH-2447
socket = new Socket();
socket.setSoTimeout(http.getTimeout());
socket.connect(sockAddr, http.getTimeout());
sslsocket = getSSLSocket(socket, "", sockPort);
sslsocket.startHandshake();
} catch (IOException ex) {
String msg = "SSL reconnect to " + url + " failed with: "
+ e.getMessage();
throw new HttpException(msg);
}
}
}
socket = sslsocket;
}

Expand Down Expand Up @@ -318,6 +319,31 @@ public byte[] getContent() {
* -------------------------
*/

private SSLSocket getSSLSocket(Socket socket, String sockHost, int sockPort) throws IOException {
SSLSocketFactory factory = (SSLSocketFactory) SSLSocketFactory
.getDefault();
SSLSocket sslsocket = (SSLSocket) factory
.createSocket(socket, sockHost, sockPort, true);
sslsocket.setUseClientMode(true);

// Get the protocols and ciphers supported by this JVM
Set<String> protocols = new HashSet<String>(
Arrays.asList(sslsocket.getSupportedProtocols()));
Set<String> ciphers = new HashSet<String>(
Arrays.asList(sslsocket.getSupportedCipherSuites()));

// Intersect with preferred protocols and ciphers
protocols.retainAll(http.getTlsPreferredProtocols());
ciphers.retainAll(http.getTlsPreferredCipherSuites());

sslsocket.setEnabledProtocols(
protocols.toArray(new String[protocols.size()]));
sslsocket.setEnabledCipherSuites(
ciphers.toArray(new String[ciphers.size()]));

return sslsocket;
}

private void readPlainContent(InputStream in)
throws HttpException, IOException {

Expand Down