|
18 | 18 | import io.netty.internal.tcnative.SSL; |
19 | 19 | import io.netty.util.AsciiString; |
20 | 20 |
|
| 21 | +import java.util.ArrayList; |
21 | 22 | import java.util.HashMap; |
| 23 | +import java.util.HashSet; |
| 24 | +import java.util.List; |
22 | 25 | import java.util.Map; |
| 26 | +import java.util.Set; |
23 | 27 |
|
24 | 28 | /** |
25 | 29 | * {@link OpenSslSessionCache} that is used by the client-side. |
26 | 30 | */ |
27 | 31 | final class OpenSslClientSessionCache extends OpenSslSessionCache { |
28 | | - // TODO: Should we support to have a List of OpenSslSessions for a Host/Port key and so be able to |
29 | | - // support sessions for different protocols / ciphers to the same remote peer ? |
30 | | - private final Map<HostPort, NativeSslSession> sessions = new HashMap<HostPort, NativeSslSession>(); |
| 32 | + private final Map<HostPort, Set<NativeSslSession>> sessions = new HashMap<HostPort, Set<NativeSslSession>>(); |
31 | 33 |
|
32 | 34 | OpenSslClientSessionCache(OpenSslEngineMap engineMap) { |
33 | 35 | super(engineMap); |
@@ -37,10 +39,17 @@ final class OpenSslClientSessionCache extends OpenSslSessionCache { |
37 | 39 | protected boolean sessionCreated(NativeSslSession session) { |
38 | 40 | assert Thread.holdsLock(this); |
39 | 41 | HostPort hostPort = keyFor(session.getPeerHost(), session.getPeerPort()); |
40 | | - if (hostPort == null || sessions.containsKey(hostPort)) { |
| 42 | + if (hostPort == null) { |
41 | 43 | return false; |
42 | 44 | } |
43 | | - sessions.put(hostPort, session); |
| 45 | + Set<NativeSslSession> sessionsForHost = sessions.get(hostPort); |
| 46 | + if (sessionsForHost == null) { |
| 47 | + // Let's start with something small as usually the server does not provide too many of these per hostPort |
| 48 | + // mapping. |
| 49 | + sessionsForHost = new HashSet<NativeSslSession>(4); |
| 50 | + sessions.put(hostPort, sessionsForHost); |
| 51 | + } |
| 52 | + sessionsForHost.add(session); |
44 | 53 | return true; |
45 | 54 | } |
46 | 55 |
|
@@ -51,7 +60,13 @@ protected void sessionRemoved(NativeSslSession session) { |
51 | 60 | if (hostPort == null) { |
52 | 61 | return; |
53 | 62 | } |
54 | | - sessions.remove(hostPort); |
| 63 | + Set<NativeSslSession> sessionsForHost = sessions.get(hostPort); |
| 64 | + if (sessionsForHost != null) { |
| 65 | + sessionsForHost.remove(session); |
| 66 | + if (sessionsForHost.isEmpty()) { |
| 67 | + sessions.remove(hostPort); |
| 68 | + } |
| 69 | + } |
55 | 70 | } |
56 | 71 |
|
57 | 72 | @Override |
@@ -60,18 +75,45 @@ boolean setSession(long ssl, OpenSslSession session, String host, int port) { |
60 | 75 | if (hostPort == null) { |
61 | 76 | return false; |
62 | 77 | } |
63 | | - final NativeSslSession nativeSslSession; |
| 78 | + NativeSslSession nativeSslSession = null; |
64 | 79 | final boolean reused; |
65 | 80 | boolean singleUsed = false; |
66 | 81 | synchronized (this) { |
67 | | - nativeSslSession = sessions.get(hostPort); |
68 | | - if (nativeSslSession == null) { |
| 82 | + Set<NativeSslSession> sessionsForHost = sessions.get(hostPort); |
| 83 | + if (sessionsForHost == null) { |
69 | 84 | return false; |
70 | 85 | } |
71 | | - if (!nativeSslSession.isValid()) { |
72 | | - removeSessionWithId(nativeSslSession.sessionId()); |
| 86 | + if (sessionsForHost.isEmpty()) { |
| 87 | + sessions.remove(hostPort); |
| 88 | + // There is no session that we can use. |
73 | 89 | return false; |
74 | 90 | } |
| 91 | + |
| 92 | + List<NativeSslSession> toBeRemoved = null; |
| 93 | + // Loop through all the sessions that might be usable and check if we can use one of these. |
| 94 | + for (NativeSslSession sslSession : sessionsForHost) { |
| 95 | + if (sslSession.isValid()) { |
| 96 | + nativeSslSession = sslSession; |
| 97 | + break; |
| 98 | + } else { |
| 99 | + if (toBeRemoved == null) { |
| 100 | + toBeRemoved = new ArrayList<NativeSslSession>(2); |
| 101 | + } |
| 102 | + toBeRemoved.add(nativeSslSession); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + // Remove everything that is not valid anymore |
| 107 | + if (toBeRemoved != null) { |
| 108 | + for (NativeSslSession sslSession : toBeRemoved) { |
| 109 | + removeSessionWithId(sslSession.sessionId()); |
| 110 | + } |
| 111 | + } |
| 112 | + if (nativeSslSession == null) { |
| 113 | + // Couldn't find a valid session that could be used. |
| 114 | + return false; |
| 115 | + } |
| 116 | + |
75 | 117 | // Try to set the session, if true is returned OpenSSL incremented the reference count |
76 | 118 | // of the underlying SSL_SESSION*. |
77 | 119 | reused = SSL.setSession(ssl, nativeSslSession.session()); |
|