@@ -33,6 +33,8 @@ final class SSLSessionHandlerAdapter extends AbstractPostgresSSLHandlerAdapter {
33
33
34
34
private final SSLConfig sslConfig ;
35
35
36
+ private boolean negotiating = true ;
37
+
36
38
SSLSessionHandlerAdapter (ByteBufAllocator alloc , SSLConfig sslConfig ) {
37
39
super (alloc , sslConfig );
38
40
this .alloc = alloc ;
@@ -41,36 +43,44 @@ final class SSLSessionHandlerAdapter extends AbstractPostgresSSLHandlerAdapter {
41
43
42
44
@ Override
43
45
public void channelActive (ChannelHandlerContext ctx ) throws Exception {
44
- Mono .from (SSLRequest .INSTANCE .encode (this .alloc )).subscribe (ctx ::writeAndFlush );
46
+ if (negotiating ) {
47
+ Mono .from (SSLRequest .INSTANCE .encode (this .alloc )).subscribe (ctx ::writeAndFlush );
48
+ }
45
49
super .channelActive (ctx );
46
50
}
47
51
48
52
@ Override
49
53
public void channelInactive (ChannelHandlerContext ctx ) throws Exception {
50
- // If we receive channel inactive before removing this handler, then the inbound has closed early.
51
- PostgresqlSslException e = new PostgresqlSslException ("Connection closed during SSL negotiation" );
52
- completeHandshakeExceptionally (e );
54
+ if (negotiating ) {
55
+ // If we receive channel inactive before negotiated, then the inbound has closed early.
56
+ PostgresqlSslException e = new PostgresqlSslException ("Connection closed during SSL negotiation" );
57
+ completeHandshakeExceptionally (e );
58
+ }
53
59
super .channelInactive (ctx );
54
60
}
55
61
56
62
@ Override
57
- public void channelRead (ChannelHandlerContext ctx , Object msg ) {
58
- ByteBuf buf = (ByteBuf ) msg ;
59
- char response = (char ) buf .readByte ();
60
- try {
61
- switch (response ) {
62
- case 'S' :
63
- processSslEnabled (ctx , buf );
64
- break ;
65
- case 'N' :
66
- processSslDisabled (ctx );
67
- break ;
68
- default :
69
- buf .release ();
70
- throw new IllegalStateException ("Unknown SSLResponse from server: '" + response + "'" );
63
+ public void channelRead (ChannelHandlerContext ctx , Object msg ) throws Exception {
64
+ if (negotiating ) {
65
+ ByteBuf buf = (ByteBuf ) msg ;
66
+ char response = (char ) buf .readByte ();
67
+ try {
68
+ switch (response ) {
69
+ case 'S' :
70
+ processSslEnabled (ctx , buf );
71
+ break ;
72
+ case 'N' :
73
+ processSslDisabled (ctx );
74
+ break ;
75
+ default :
76
+ throw new IllegalStateException ("Unknown SSLResponse from server: '" + response + "'" );
77
+ }
78
+ } finally {
79
+ buf .release ();
80
+ negotiating = false ;
71
81
}
72
- } finally {
73
- buf . release ( );
82
+ } else {
83
+ super . channelRead ( ctx , msg );
74
84
}
75
85
}
76
86
@@ -81,7 +91,6 @@ private void processSslDisabled(ChannelHandlerContext ctx) {
81
91
completeHandshakeExceptionally (e );
82
92
} else {
83
93
completeHandshake ();
84
- ctx .channel ().pipeline ().remove (this );
85
94
}
86
95
}
87
96
@@ -92,9 +101,7 @@ private void processSslEnabled(ChannelHandlerContext ctx, ByteBuf msg) {
92
101
completeHandshakeExceptionally (e );
93
102
return ;
94
103
}
95
- ctx .channel ().pipeline ()
96
- .addFirst (this .getSslHandler ())
97
- .remove (this );
104
+ ctx .channel ().pipeline ().addFirst (this .getSslHandler ());
98
105
ctx .fireChannelRead (msg .retain ());
99
106
}
100
107
0 commit comments