java.lang.Object
org.newsclub.net.unix.ssl.SNIHostnameCapture
Helper class to simplify retrieving the requested SNI hostname sent from an SSL client to an SSL
server.
- Author:
- Christian Kohlschütter
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final SNIMatcherSNIMatcherthat accepts and matches "any hostname". -
Method Summary
Modifier and TypeMethodDescriptionstatic SNIHostnameCaptureconfigure(SSLSocket sslSocket, SNIMatcher hostnameMatcher) Configures the given serverSSLSocketto match the given hostname pattern.static SNIHostnameCaptureconfigure(SSLSocket sslSocket, SNIMatcher hostnameMatcher, Supplier<String> defaultHostnameSupplier) Configures the given serverSSLSocketto match the given hostname pattern.Returns the hostname (either the retrieved one, or a fallback), which could also benull.booleanChecks if a hostname can be returned by callinggetHostname()(which most likely means the handshake is complete).booleanisComplete(long timeout, TimeUnit unit) Checks if a hostname can be returned by callinggetHostname()(which most likely means the handshake is complete), optionally waiting up to the given time interval if not yet complete.
-
Field Details
-
ACCEPT_ANY_HOSTNAME
SNIMatcherthat accepts and matches "any hostname".
-
-
Method Details
-
configure
Configures the given serverSSLSocketto match the given hostname pattern.The matched hostname is then accessible after the handshake is completed, via
getHostname(). If no hostname was retrieved,nullis assumed.- Parameters:
sslSocket- The socket to configure.hostnameMatcher- The matcher to use.- Returns:
- The
SNIHostnameCaptureinstance. - See Also:
-
configure
public static SNIHostnameCapture configure(SSLSocket sslSocket, SNIMatcher hostnameMatcher, Supplier<String> defaultHostnameSupplier) Configures the given serverSSLSocketto match the given hostname pattern.The matched hostname is then accessible after the handshake is completed, via
getHostname(). If no hostname was retrieved, a fallback is retrieved via the given supplier.- Parameters:
sslSocket- The socket to configure.hostnameMatcher- The matcher to use.defaultHostnameSupplier- The supplier for a default hostname, ornull.- Returns:
- The
SNIHostnameCaptureinstance. - See Also:
-
isComplete
Checks if a hostname can be returned by callinggetHostname()(which most likely means the handshake is complete), optionally waiting up to the given time interval if not yet complete.- Parameters:
timeout- The maximum timeout value to wait for.unit- Timeout unit.- Returns:
trueifgetHostname()will not throw anIllegalStateException.- Throws:
InterruptedException- if the current thread was interrupted while waiting.
-
isComplete
public boolean isComplete()Checks if a hostname can be returned by callinggetHostname()(which most likely means the handshake is complete).- Returns:
trueifgetHostname()will not throw anIllegalStateException.
-
getHostname
Returns the hostname (either the retrieved one, or a fallback), which could also benull.If the method is called before the handshake is complete (check with
isComplete()or simply call afterSSLSocket.startHandshake()), anIllegalStateExceptionis thrown.- Returns:
- The hostname, or
null. - Throws:
IllegalStateException- if the method was called before a hostname could be retrieved.
-