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 — Avoid unpooled allocator in `CloseWebSocketFrame` (#16486) (#16512) · netty/netty@c84115a

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • codec-http/src
    • main/java/io/netty/handler/codec/http/websocketx
      • CloseWebSocketFrame.java
      • WebSocketProtocolHandler.java
    • test/java/io/netty/handler/codec/http/websocketx
      • WebSocketServerProtocolHandlerTest.java
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎codec-http/src/main/java/io/netty/handler/codec/http/websocketx/CloseWebSocketFrame.java‎

Copy file name to clipboardExpand all lines: codec-http/src/main/java/io/netty/handler/codec/http/websocketx/CloseWebSocketFrame.java
+36-3Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616
package io.netty.handler.codec.http.websocketx;
1717

1818
import io.netty.buffer.ByteBuf;
19+
import io.netty.buffer.ByteBufAllocator;
1920
import io.netty.buffer.Unpooled;
21+
import io.netty.buffer.UnpooledByteBufAllocator;
2022
import io.netty.util.CharsetUtil;
2123
import io.netty.util.internal.StringUtil;
2224

@@ -43,6 +45,17 @@ public CloseWebSocketFrame(WebSocketCloseStatus status) {
4345
this(requireValidStatusCode(status.code()), status.reasonText());
4446
}
4547

48+
/**
49+
* Creates a new empty close frame with closing status code and reason text, using the provided ByteBuf allocator.
50+
*
51+
* @param status
52+
* Status code as per <a href="https://tools.ietf.org/html/rfc6455#section-7.4">RFC 6455</a>. For
53+
* example, <tt>1000</tt> indicates normal closure.
54+
*/
55+
public CloseWebSocketFrame(WebSocketCloseStatus status, ByteBufAllocator allocator) {
56+
this(true, 0, allocator, requireValidStatusCode(status.code()), status.reasonText());
57+
}
58+
4659
/**
4760
* Creates a new empty close frame with closing status code and reason text
4861
*
@@ -95,15 +108,35 @@ public CloseWebSocketFrame(boolean finalFragment, int rsv) {
95108
* Reason text. Set to null if no text.
96109
*/
97110
public CloseWebSocketFrame(boolean finalFragment, int rsv, int statusCode, String reasonText) {
98-
super(finalFragment, rsv, newBinaryData(requireValidStatusCode(statusCode), reasonText));
111+
this(finalFragment, rsv, UnpooledByteBufAllocator.DEFAULT, statusCode, reasonText);
112+
}
113+
114+
/**
115+
* Creates a new close frame with closing status code and reason text
116+
*
117+
* @param finalFragment
118+
* flag indicating if this frame is the final fragment
119+
* @param rsv
120+
* reserved bits used for protocol extensions
121+
* @param allocator
122+
* the ByteBuf allocator to use to build the binary data.
123+
* @param statusCode
124+
* Integer status code as per <a href="https://tools.ietf.org/html/rfc6455#section-7.4">RFC 6455</a>. For
125+
* example, <tt>1000</tt> indicates normal closure.
126+
* @param reasonText
127+
* Reason text. Set to null if no text.
128+
*/
129+
public CloseWebSocketFrame(boolean finalFragment, int rsv, ByteBufAllocator allocator,
130+
int statusCode, String reasonText) {
131+
super(finalFragment, rsv, newBinaryData(allocator, requireValidStatusCode(statusCode), reasonText));
99132
}
100133

101-
private static ByteBuf newBinaryData(int statusCode, String reasonText) {
134+
private static ByteBuf newBinaryData(ByteBufAllocator allocator, int statusCode, String reasonText) {
102135
if (reasonText == null) {
103136
reasonText = StringUtil.EMPTY_STRING;
104137
}
105138

106-
ByteBuf binaryData = Unpooled.buffer(2 + reasonText.length());
139+
ByteBuf binaryData = allocator.buffer(2 + reasonText.length());
107140
binaryData.writeShort(statusCode);
108141
if (!reasonText.isEmpty()) {
109142
binaryData.writeCharSequence(reasonText, CharsetUtil.UTF_8);
Collapse file

‎codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketProtocolHandler.java‎

Copy file name to clipboardExpand all lines: codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketProtocolHandler.java
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ public void close(final ChannelHandlerContext ctx, final CompletionHandler<Void>
9494
ctx.close(handler);
9595
} else {
9696
if (closeSent == null) {
97-
write(ctx, new CloseWebSocketFrame(closeStatus), CompletionHandler.ignore());
97+
write(ctx, new CloseWebSocketFrame(closeStatus, ctx.alloc()), CompletionHandler.ignore());
9898
}
9999
flush(ctx);
100100
applyCloseSentTimeout(ctx);
Collapse file

‎codec-http/src/test/java/io/netty/handler/codec/http/websocketx/WebSocketServerProtocolHandlerTest.java‎

Copy file name to clipboardExpand all lines: codec-http/src/test/java/io/netty/handler/codec/http/websocketx/WebSocketServerProtocolHandlerTest.java
+3-1Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -393,8 +393,10 @@ public void testCloseFrameSentWhenClientChannelClosedSilently() throws Exception
393393
assertFalse(server.isOpen());
394394

395395
CloseWebSocketFrame closeMessage = decode(server.<ByteBuf>readOutbound(), CloseWebSocketFrame.class);
396-
assertEquals(closeMessage, new CloseWebSocketFrame(WebSocketCloseStatus.NORMAL_CLOSURE));
396+
CloseWebSocketFrame expectedFrame = new CloseWebSocketFrame(WebSocketCloseStatus.NORMAL_CLOSURE);
397+
assertEquals(expectedFrame, closeMessage);
397398
closeMessage.release();
399+
expectedFrame.release();
398400

399401
assertFalse(client.finishAndReleaseAll());
400402
assertFalse(server.finishAndReleaseAll());

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.