GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Ensure ApplicationProtocolNegotiationHandler does handle handshake fa… · ihanyong/netty@d7cc7ca

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • handler/src
    • main/java/io/netty/handler/ssl
      • ApplicationProtocolNegotiationHandler.java
    • test/java/io/netty/handler/ssl
      • ApplicationProtocolNegotiationHandlerTest.java
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎handler/src/main/java/io/netty/handler/ssl/ApplicationProtocolNegotiationHandler.java‎

Copy file name to clipboardExpand all lines: handler/src/main/java/io/netty/handler/ssl/ApplicationProtocolNegotiationHandler.java
+27-7Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,12 @@
2222
import io.netty.channel.ChannelHandlerContext;
2323
import io.netty.channel.ChannelInitializer;
2424
import io.netty.channel.ChannelPipeline;
25+
import io.netty.handler.codec.DecoderException;
2526
import io.netty.util.internal.logging.InternalLogger;
2627
import io.netty.util.internal.logging.InternalLoggerFactory;
2728

29+
import javax.net.ssl.SSLException;
30+
2831
/**
2932
* Configures a {@link ChannelPipeline} depending on the application-level protocol negotiation result of
3033
* {@link SslHandler}. For example, you could configure your HTTP pipeline depending on the result of ALPN:
@@ -80,9 +83,8 @@ protected ApplicationProtocolNegotiationHandler(String fallbackProtocol) {
8083
@Override
8184
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
8285
if (evt instanceof SslHandshakeCompletionEvent) {
83-
86+
SslHandshakeCompletionEvent handshakeEvent = (SslHandshakeCompletionEvent) evt;
8487
try {
85-
SslHandshakeCompletionEvent handshakeEvent = (SslHandshakeCompletionEvent) evt;
8688
if (handshakeEvent.isSuccess()) {
8789
SslHandler sslHandler = ctx.pipeline().get(SslHandler.class);
8890
if (sslHandler == null) {
@@ -92,23 +94,32 @@ public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exc
9294
String protocol = sslHandler.applicationProtocol();
9395
configurePipeline(ctx, protocol != null ? protocol : fallbackProtocol);
9496
} else {
95-
handshakeFailure(ctx, handshakeEvent.cause());
97+
// if the event is not produced because of an successful handshake we will receive the same
98+
// exception in exceptionCaught(...) and handle it there. This will allow us more fine-grained
99+
// control over which exception we propagate down the ChannelPipeline.
100+
//
101+
// See https://github.com/netty/netty/issues/10342
96102
}
97103
} catch (Throwable cause) {
98104
exceptionCaught(ctx, cause);
99105
} finally {
100106
ctx.fireUserEventTriggered(evt);
101-
102-
ChannelPipeline pipeline = ctx.pipeline();
103-
if (pipeline.context(this) != null) {
104-
pipeline.remove(this);
107+
// Handshake failures are handled in exceptionCaught(...).
108+
if (handshakeEvent.isSuccess()) {
109+
removeSelfIfPresent(ctx);
105110
}
106111
}
107112
} else {
108113
ctx.fireUserEventTriggered(evt);
109114
}
110115
}
111116

117+
private void removeSelfIfPresent(ChannelHandlerContext ctx) {
118+
ChannelPipeline pipeline = ctx.pipeline();
119+
if (pipeline.context(this) != null) {
120+
pipeline.remove(this);
121+
}
122+
}
112123
/**
113124
* Invoked on successful initial SSL/TLS handshake. Implement this method to configure your pipeline
114125
* for the negotiated application-level protocol.
@@ -129,6 +140,15 @@ protected void handshakeFailure(ChannelHandlerContext ctx, Throwable cause) thro
129140

130141
@Override
131142
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
143+
Throwable wrapped;
144+
if (cause instanceof DecoderException && ((wrapped = cause.getCause()) instanceof SSLException)) {
145+
try {
146+
handshakeFailure(ctx, wrapped);
147+
return;
148+
} finally {
149+
removeSelfIfPresent(ctx);
150+
}
151+
}
132152
logger.warn("{} Failed to select the application-level protocol:", ctx.channel(), cause);
133153
ctx.fireExceptionCaught(cause);
134154
ctx.close();
Collapse file

‎handler/src/test/java/io/netty/handler/ssl/ApplicationProtocolNegotiationHandlerTest.java‎

Copy file name to clipboard
+95Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/*
2+
* Copyright 2020 The Netty Project
3+
*
4+
* The Netty Project licenses this file to you under the Apache License,
5+
* version 2.0 (the "License"); you may not use this file except in compliance
6+
* with the License. You may obtain a copy of the License at:
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations
14+
* under the License.
15+
*/
16+
package io.netty.handler.ssl;
17+
18+
import io.netty.channel.ChannelHandler;
19+
import io.netty.channel.ChannelHandlerContext;
20+
import io.netty.channel.embedded.EmbeddedChannel;
21+
import io.netty.handler.codec.DecoderException;
22+
import org.junit.Test;
23+
24+
import javax.net.ssl.SSLContext;
25+
import javax.net.ssl.SSLEngine;
26+
import javax.net.ssl.SSLHandshakeException;
27+
import java.security.NoSuchAlgorithmException;
28+
import java.util.concurrent.atomic.AtomicBoolean;
29+
30+
import static org.junit.Assert.assertEquals;
31+
import static org.junit.Assert.assertFalse;
32+
import static org.junit.Assert.assertNull;
33+
import static org.junit.Assert.assertTrue;
34+
import static org.junit.Assert.fail;
35+
36+
37+
public class ApplicationProtocolNegotiationHandlerTest {
38+
39+
@Test
40+
public void testHandshakeFailure() {
41+
ChannelHandler alpnHandler = new ApplicationProtocolNegotiationHandler(ApplicationProtocolNames.HTTP_1_1) {
42+
@Override
43+
protected void configurePipeline(ChannelHandlerContext ctx, String protocol) {
44+
fail();
45+
}
46+
};
47+
48+
EmbeddedChannel channel = new EmbeddedChannel(alpnHandler);
49+
SSLHandshakeException exception = new SSLHandshakeException("error");
50+
SslHandshakeCompletionEvent completionEvent = new SslHandshakeCompletionEvent(exception);
51+
channel.pipeline().fireUserEventTriggered(completionEvent);
52+
channel.pipeline().fireExceptionCaught(new DecoderException(exception));
53+
assertNull(channel.pipeline().context(alpnHandler));
54+
assertFalse(channel.finishAndReleaseAll());
55+
}
56+
57+
@Test
58+
public void testHandshakeSuccess() throws NoSuchAlgorithmException {
59+
final AtomicBoolean configureCalled = new AtomicBoolean(false);
60+
ChannelHandler alpnHandler = new ApplicationProtocolNegotiationHandler(ApplicationProtocolNames.HTTP_1_1) {
61+
@Override
62+
protected void configurePipeline(ChannelHandlerContext ctx, String protocol) {
63+
configureCalled.set(true);
64+
assertEquals(ApplicationProtocolNames.HTTP_1_1, protocol);
65+
}
66+
};
67+
68+
SSLEngine engine = SSLContext.getDefault().createSSLEngine();
69+
engine.setUseClientMode(false);
70+
71+
EmbeddedChannel channel = new EmbeddedChannel(new SslHandler(engine), alpnHandler);
72+
channel.pipeline().fireUserEventTriggered(SslHandshakeCompletionEvent.SUCCESS);
73+
assertNull(channel.pipeline().context(alpnHandler));
74+
// Should produce the close_notify messages
75+
assertTrue(channel.finishAndReleaseAll());
76+
assertTrue(configureCalled.get());
77+
}
78+
79+
@Test(expected = IllegalStateException.class)
80+
public void testHandshakeSuccessButNoSslHandler() {
81+
ChannelHandler alpnHandler = new ApplicationProtocolNegotiationHandler(ApplicationProtocolNames.HTTP_1_1) {
82+
@Override
83+
protected void configurePipeline(ChannelHandlerContext ctx, String protocol) {
84+
fail();
85+
}
86+
};
87+
EmbeddedChannel channel = new EmbeddedChannel(alpnHandler);
88+
try {
89+
channel.pipeline().fireUserEventTriggered(SslHandshakeCompletionEvent.SUCCESS);
90+
} finally {
91+
assertNull(channel.pipeline().context(alpnHandler));
92+
assertFalse(channel.finishAndReleaseAll());
93+
}
94+
}
95+
}

0 commit comments

Comments
0 (0)
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.