Skip to content

Commit ad8c84e

Browse files
committed
Merge branch 'release/1.0.0'
2 parents e207c5c + 9f5ae78 commit ad8c84e

File tree

7 files changed

+120
-32
lines changed

7 files changed

+120
-32
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ repositories {
173173
}
174174
175175
dependencies {
176-
implementation "com.jauntsdn.netty:netty-websocket-http1:0.9.1"
176+
implementation "com.jauntsdn.netty:netty-websocket-http1:0.9.2"
177177
}
178178
```
179179

gradle.properties

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
group=com.jauntsdn.netty
2-
version=0.9.2
2+
version=1.0.0
33

44
googleJavaFormatPluginVersion=0.9
55
dependencyManagementPluginVersion=1.1.0
66
gitPluginVersion=0.13.0
7-
osDetectorPluginVersion=1.7.1
8-
versionsPluginVersion=0.44.0
7+
osDetectorPluginVersion=1.7.3
8+
versionsPluginVersion=0.45.0
99

10-
nettyVersion=4.1.87.Final
11-
nettyTcnativeVersion=2.0.56.Final
10+
nettyVersion=4.1.90.Final
11+
nettyTcnativeVersion=2.0.59.Final
1212
hdrHistogramVersion=2.1.12
1313
slf4jVersion=1.7.36
1414
logbackVersion=1.2.11

netty-websocket-http1-test/gradle.lockfile

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,16 @@ com.google.errorprone:javac-shaded:9+181-r4173-1=googleJavaFormat1.6
99
com.google.googlejavaformat:google-java-format:1.6=googleJavaFormat1.6
1010
com.google.guava:guava:22.0=googleJavaFormat1.6
1111
com.google.j2objc:j2objc-annotations:1.1=googleJavaFormat1.6
12-
io.netty:netty-buffer:4.1.87.Final=compileClasspath,testCompileClasspath,testRuntimeClasspath
13-
io.netty:netty-codec-http:4.1.87.Final=compileClasspath,testCompileClasspath,testRuntimeClasspath
14-
io.netty:netty-codec:4.1.87.Final=compileClasspath,testCompileClasspath,testRuntimeClasspath
15-
io.netty:netty-common:4.1.87.Final=compileClasspath,testCompileClasspath,testRuntimeClasspath
16-
io.netty:netty-handler:4.1.87.Final=compileClasspath,testCompileClasspath,testRuntimeClasspath
17-
io.netty:netty-resolver:4.1.87.Final=compileClasspath,testCompileClasspath,testRuntimeClasspath
18-
io.netty:netty-transport-classes-epoll:4.1.87.Final=compileClasspath,testCompileClasspath,testRuntimeClasspath
19-
io.netty:netty-transport-classes-kqueue:4.1.87.Final=compileClasspath,testCompileClasspath,testRuntimeClasspath
20-
io.netty:netty-transport-native-unix-common:4.1.87.Final=compileClasspath,testCompileClasspath,testRuntimeClasspath
21-
io.netty:netty-transport:4.1.87.Final=compileClasspath,testCompileClasspath,testRuntimeClasspath
12+
io.netty:netty-buffer:4.1.90.Final=compileClasspath,testCompileClasspath,testRuntimeClasspath
13+
io.netty:netty-codec-http:4.1.90.Final=compileClasspath,testCompileClasspath,testRuntimeClasspath
14+
io.netty:netty-codec:4.1.90.Final=compileClasspath,testCompileClasspath,testRuntimeClasspath
15+
io.netty:netty-common:4.1.90.Final=compileClasspath,testCompileClasspath,testRuntimeClasspath
16+
io.netty:netty-handler:4.1.90.Final=compileClasspath,testCompileClasspath,testRuntimeClasspath
17+
io.netty:netty-resolver:4.1.90.Final=compileClasspath,testCompileClasspath,testRuntimeClasspath
18+
io.netty:netty-transport-classes-epoll:4.1.90.Final=compileClasspath,testCompileClasspath,testRuntimeClasspath
19+
io.netty:netty-transport-classes-kqueue:4.1.90.Final=compileClasspath,testCompileClasspath,testRuntimeClasspath
20+
io.netty:netty-transport-native-unix-common:4.1.90.Final=compileClasspath,testCompileClasspath,testRuntimeClasspath
21+
io.netty:netty-transport:4.1.90.Final=compileClasspath,testCompileClasspath,testRuntimeClasspath
2222
net.bytebuddy:byte-buddy:1.12.21=testCompileClasspath,testRuntimeClasspath
2323
org.apiguardian:apiguardian-api:1.1.2=testCompileClasspath
2424
org.assertj:assertj-core:3.24.2=testCompileClasspath,testRuntimeClasspath

netty-websocket-http1-test/src/test/java/com/jauntsdn/netty/handler/codec/http/websocketx/WebSocketHandshakeTest.java

Lines changed: 76 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,18 @@
4242
import io.netty.handler.codec.http.HttpVersion;
4343
import io.netty.handler.codec.http.websocketx.WebSocketClientHandshakeException;
4444
import io.netty.handler.codec.http.websocketx.WebSocketDecoderConfig;
45+
import io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler.HandshakeComplete;
46+
import io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler.ServerHandshakeStateEvent;
4547
import io.netty.util.AttributeKey;
4648
import io.netty.util.ReferenceCountUtil;
4749
import io.netty.util.concurrent.DefaultPromise;
4850
import io.netty.util.concurrent.Future;
4951
import io.netty.util.concurrent.Promise;
5052
import java.net.SocketAddress;
5153
import java.nio.channels.ClosedChannelException;
54+
import java.util.List;
5255
import java.util.concurrent.CompletableFuture;
56+
import java.util.concurrent.CopyOnWriteArrayList;
5357
import java.util.function.Consumer;
5458
import org.assertj.core.api.Assertions;
5559
import org.junit.jupiter.api.AfterEach;
@@ -305,9 +309,36 @@ protected void initChannel(SocketChannel ch) {
305309
Assertions.assertThat(client.isOpen()).isFalse();
306310
}
307311

312+
@SuppressWarnings("deprecation")
313+
@Timeout(15)
314+
@Test
315+
void serverHandshakeEvents() throws InterruptedException {
316+
WebSocketDecoderConfig decoderConfig = webSocketDecoderConfig(false, true, 125);
317+
TestWebSocketHandler serverHandler = new TestWebSocketHandler();
318+
TestWebSocketHandler clientHandler = new TestWebSocketHandler();
319+
String subprotocol = "subprotocol";
320+
String path = "/";
321+
Channel s = server = testServer(path, subprotocol, decoderConfig, serverHandler, null);
322+
Channel client =
323+
testClient(s.localAddress(), path, subprotocol, true, true, 65_535, clientHandler);
324+
serverHandler.onOpen.join();
325+
client.close();
326+
serverHandler.onClose.join();
327+
List<Object> events = serverHandler.events;
328+
Assertions.assertThat(events).hasSize(2);
329+
Assertions.assertThat(events.get(0)).isEqualTo(ServerHandshakeStateEvent.HANDSHAKE_COMPLETE);
330+
Object event = serverHandler.events.get(1);
331+
Assertions.assertThat(event).isExactlyInstanceOf(HandshakeComplete.class);
332+
HandshakeComplete completeEvent = (HandshakeComplete) event;
333+
Assertions.assertThat(completeEvent.requestUri()).isEqualTo(path);
334+
Assertions.assertThat(completeEvent.requestHeaders()).isNotNull().isNotEmpty();
335+
Assertions.assertThat(completeEvent.selectedSubprotocol()).isEqualTo(subprotocol);
336+
}
337+
308338
static Channel testClient(
309339
SocketAddress address,
310340
String path,
341+
String subprotocol,
311342
boolean mask,
312343
boolean allowMaskMismatch,
313344
int maxFramePayloadLength,
@@ -331,6 +362,7 @@ protected void initChannel(SocketChannel ch) {
331362
.allowMaskMismatch(allowMaskMismatch)
332363
.maxFramePayloadLength(maxFramePayloadLength)
333364
.webSocketHandler(webSocketCallbacksHandler)
365+
.subprotocol(subprotocol)
334366
.build();
335367

336368
ChannelPipeline pipeline = ch.pipeline();
@@ -342,6 +374,24 @@ protected void initChannel(SocketChannel ch) {
342374
.channel();
343375
}
344376

377+
static Channel testClient(
378+
SocketAddress address,
379+
String path,
380+
boolean mask,
381+
boolean allowMaskMismatch,
382+
int maxFramePayloadLength,
383+
WebSocketCallbacksHandler webSocketCallbacksHandler)
384+
throws InterruptedException {
385+
return testClient(
386+
address,
387+
path,
388+
null,
389+
mask,
390+
allowMaskMismatch,
391+
maxFramePayloadLength,
392+
webSocketCallbacksHandler);
393+
}
394+
345395
static Channel testServer(
346396
String path,
347397
WebSocketDecoderConfig decoderConfig,
@@ -356,12 +406,27 @@ static Channel testServer(
356406
WebSocketCallbacksHandler webSocketCallbacksHandler,
357407
Consumer<Object> nonHandledMessageConsumer)
358408
throws InterruptedException {
409+
return testServer(
410+
path, null, decoderConfig, webSocketCallbacksHandler, nonHandledMessageConsumer);
411+
}
412+
413+
static Channel testServer(
414+
String path,
415+
String subprotocol,
416+
WebSocketDecoderConfig decoderConfig,
417+
WebSocketCallbacksHandler webSocketCallbacksHandler,
418+
Consumer<Object> nonHandledMessageConsumer)
419+
throws InterruptedException {
359420
return new ServerBootstrap()
360421
.group(new NioEventLoopGroup(1))
361422
.channel(NioServerSocketChannel.class)
362423
.childHandler(
363424
new TestAcceptor(
364-
path, decoderConfig, webSocketCallbacksHandler, nonHandledMessageConsumer))
425+
path,
426+
subprotocol,
427+
decoderConfig,
428+
webSocketCallbacksHandler,
429+
nonHandledMessageConsumer))
365430
.bind("localhost", 0)
366431
.sync()
367432
.channel();
@@ -414,16 +479,19 @@ Future<FullHttpResponse> response() {
414479

415480
static class TestAcceptor extends ChannelInitializer<SocketChannel> {
416481
private final String path;
482+
private final String subprotocol;
417483
private final WebSocketDecoderConfig webSocketDecoderConfig;
418484
private final WebSocketCallbacksHandler webSocketCallbacksHandler;
419485
private final Consumer<Object> nonHandledMessageConsumer;
420486

421487
TestAcceptor(
422488
String path,
489+
String subprotocol,
423490
WebSocketDecoderConfig decoderConfig,
424491
WebSocketCallbacksHandler webSocketCallbacksHandler,
425492
Consumer<Object> nonHandledMessageConsumer) {
426493
this.path = path;
494+
this.subprotocol = subprotocol;
427495
this.webSocketDecoderConfig = decoderConfig;
428496
this.webSocketCallbacksHandler = webSocketCallbacksHandler;
429497
this.nonHandledMessageConsumer = nonHandledMessageConsumer;
@@ -436,6 +504,7 @@ protected void initChannel(SocketChannel ch) {
436504
WebSocketServerProtocolHandler webSocketProtocolHandler =
437505
WebSocketServerProtocolHandler.create()
438506
.path(path)
507+
.subprotocols(subprotocol)
439508
.decoderConfig(webSocketDecoderConfig)
440509
.webSocketCallbacksHandler(webSocketCallbacksHandler)
441510
.build();
@@ -458,6 +527,7 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) {
458527
static class TestWebSocketHandler implements WebSocketCallbacksHandler {
459528
final CompletableFuture<Void> onOpen = new CompletableFuture<>();
460529
final CompletableFuture<Void> onClose = new CompletableFuture<>();
530+
final List<Object> events = new CopyOnWriteArrayList<>();
461531

462532
volatile WebSocketFrameFactory webSocketFrameFactory;
463533
volatile Channel channel;
@@ -477,6 +547,11 @@ public void onChannelRead(
477547
int opcode,
478548
ByteBuf payload) {}
479549

550+
@Override
551+
public void onUserEventTriggered(ChannelHandlerContext ctx, Object evt) {
552+
events.add(evt);
553+
}
554+
480555
@Override
481556
public void onOpen(ChannelHandlerContext ctx) {
482557
onOpen.complete(null);

netty-websocket-http1/gradle.lockfile

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ com.google.errorprone:javac-shaded:9+181-r4173-1=googleJavaFormat1.6
77
com.google.googlejavaformat:google-java-format:1.6=googleJavaFormat1.6
88
com.google.guava:guava:22.0=googleJavaFormat1.6
99
com.google.j2objc:j2objc-annotations:1.1=googleJavaFormat1.6
10-
io.netty:netty-buffer:4.1.87.Final=compileClasspath
11-
io.netty:netty-codec-http:4.1.87.Final=compileClasspath
12-
io.netty:netty-codec:4.1.87.Final=compileClasspath
13-
io.netty:netty-common:4.1.87.Final=compileClasspath
14-
io.netty:netty-handler:4.1.87.Final=compileClasspath
15-
io.netty:netty-resolver:4.1.87.Final=compileClasspath
16-
io.netty:netty-transport-native-unix-common:4.1.87.Final=compileClasspath
17-
io.netty:netty-transport:4.1.87.Final=compileClasspath
10+
io.netty:netty-buffer:4.1.90.Final=compileClasspath
11+
io.netty:netty-codec-http:4.1.90.Final=compileClasspath
12+
io.netty:netty-codec:4.1.90.Final=compileClasspath
13+
io.netty:netty-common:4.1.90.Final=compileClasspath
14+
io.netty:netty-handler:4.1.90.Final=compileClasspath
15+
io.netty:netty-resolver:4.1.90.Final=compileClasspath
16+
io.netty:netty-transport-native-unix-common:4.1.90.Final=compileClasspath
17+
io.netty:netty-transport:4.1.90.Final=compileClasspath
1818
org.codehaus.mojo:animal-sniffer-annotations:1.14=googleJavaFormat1.6
1919
empty=annotationProcessor

netty-websocket-http1/src/main/java/com/jauntsdn/netty/handler/codec/http/websocketx/WebSocketDecoder.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,8 @@
1818

1919
import io.netty.buffer.ByteBuf;
2020
import io.netty.channel.ChannelHandlerContext;
21-
import io.netty.channel.ChannelInboundHandler;
2221

23-
abstract class WebSocketDecoder implements ChannelInboundHandler, WebSocketCallbacksFrameDecoder {
22+
abstract class WebSocketDecoder implements WebSocketCallbacksFrameDecoder {
2423

2524
static final int STATE_NON_PARTIAL = 0;
2625
static final int STATE_PARTIAL_PREFIX = 1;
@@ -97,7 +96,6 @@ public void channelWritabilityChanged(ChannelHandlerContext ctx) {
9796
}
9897
}
9998

100-
@SuppressWarnings("deprecation")
10199
@Override
102100
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
103101
WebSocketFrameListener listener = webSocketFrameListener;

netty-websocket-http1/src/main/java/com/jauntsdn/netty/handler/codec/http/websocketx/WebSocketServerProtocolHandler.java

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,12 @@
2525
import io.netty.channel.ChannelFutureListener;
2626
import io.netty.channel.ChannelHandlerContext;
2727
import io.netty.channel.ChannelInboundHandlerAdapter;
28+
import io.netty.channel.ChannelPipeline;
2829
import io.netty.channel.ChannelPromise;
2930
import io.netty.handler.codec.http.DefaultFullHttpResponse;
3031
import io.netty.handler.codec.http.FullHttpRequest;
3132
import io.netty.handler.codec.http.FullHttpResponse;
33+
import io.netty.handler.codec.http.HttpHeaders;
3234
import io.netty.handler.codec.http.HttpRequest;
3335
import io.netty.handler.codec.http.HttpResponseStatus;
3436
import io.netty.handler.codec.http.websocketx.WebSocketDecoderConfig;
@@ -141,13 +143,15 @@ private void completeHandshake(ChannelHandlerContext ctx, HttpRequest request) {
141143
WebSocketServerHandshakerFactory.sendUnsupportedVersionResponse(ctx.channel());
142144
} else {
143145
ChannelPromise handshake = handshakeCompleted;
146+
String uri = request.uri();
147+
HttpHeaders headers = request.headers();
144148

145149
ChannelFuture handshakeFuture;
146150
/*netty's websocket handshaker throws exceptions instead of notifying handshake future*/
147151
try {
148152
handshakeFuture = handshaker.handshake(ctx.channel(), request);
149153
} catch (Exception e) {
150-
handleHandshakeResult(ctx, handshake, e);
154+
handleHandshakeResult(ctx, handshake, uri, headers, handshaker.selectedSubprotocol(), e);
151155
return;
152156
}
153157
ScheduledFuture<?> timeout = startHandshakeTimeout(ctx, handshakeTimeoutMillis, handshake);
@@ -156,13 +160,20 @@ private void completeHandshake(ChannelHandlerContext ctx, HttpRequest request) {
156160
if (timeout != null) {
157161
timeout.cancel(true);
158162
}
159-
handleHandshakeResult(ctx, handshake, future.cause());
163+
handleHandshakeResult(
164+
ctx, handshake, uri, headers, handshaker.selectedSubprotocol(), future.cause());
160165
});
161166
}
162167
}
163168

169+
@SuppressWarnings("deprecation")
164170
private void handleHandshakeResult(
165-
ChannelHandlerContext ctx, ChannelPromise handshake, Throwable cause) {
171+
ChannelHandlerContext ctx,
172+
ChannelPromise handshake,
173+
String uri,
174+
HttpHeaders headers,
175+
String subprotocol,
176+
Throwable cause) {
166177
if (cause != null) {
167178
handshake.tryFailure(cause);
168179
if (cause instanceof WebSocketHandshakeException) {
@@ -179,9 +190,13 @@ private void handleHandshakeResult(
179190
} else {
180191
WebSocketCallbacksHandler.exchange(ctx, webSocketHandler);
181192
handshake.trySuccess();
182-
ctx.fireUserEventTriggered(
193+
ChannelPipeline p = ctx.channel().pipeline();
194+
p.fireUserEventTriggered(
183195
io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler
184196
.ServerHandshakeStateEvent.HANDSHAKE_COMPLETE);
197+
p.fireUserEventTriggered(
198+
new io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler
199+
.HandshakeComplete(uri, headers, subprotocol));
185200
}
186201
ctx.pipeline().remove(this);
187202
}

0 commit comments

Comments
 (0)