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 — Port 5.0: Avoid unnecessary String allocation in WebSocketServerProto… · netty/netty@196bb3a

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • codec-http/src/main/java/io/netty/handler/codec/http/websocketx
    • WebSocketServerHandshaker.java
    • WebSocketServerHandshaker00.java
    • WebSocketServerHandshaker07.java
    • WebSocketServerHandshaker08.java
    • WebSocketServerHandshaker13.java
    • WebSocketServerHandshakerFactory.java
    • WebSocketServerProtocolHandshakeHandler.java
Expand file treeCollapse file tree
Open diff view settings
Collapse file

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

Copy file name to clipboardExpand all lines: codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketServerHandshaker.java
+33-5Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,12 @@ public abstract class WebSocketServerHandshaker {
5757

5858
private final String uri;
5959

60+
private final boolean isSecure;
61+
62+
private final String host;
63+
64+
private final String path;
65+
6066
private final String[] subprotocols;
6167

6268
private final WebSocketVersion version;
@@ -108,24 +114,46 @@ protected WebSocketServerHandshaker(
108114
WebSocketVersion version, String uri, String subprotocols, WebSocketDecoderConfig decoderConfig) {
109115
this.version = version;
110116
this.uri = uri;
117+
this.isSecure = false;
118+
this.host = null;
119+
this.path = null;
120+
this.subprotocols = parseSubprotocols(subprotocols);
121+
this.decoderConfig = ObjectUtil.checkNotNull(decoderConfig, "decoderConfig");
122+
}
123+
124+
// Package-private: defers URI construction to avoid String allocation.
125+
WebSocketServerHandshaker(
126+
WebSocketVersion version, boolean isSecure, String host, String path,
127+
String subprotocols, WebSocketDecoderConfig decoderConfig) {
128+
this.version = version;
129+
this.uri = null;
130+
this.isSecure = isSecure;
131+
this.host = host;
132+
this.path = path;
133+
this.subprotocols = parseSubprotocols(subprotocols);
134+
this.decoderConfig = ObjectUtil.checkNotNull(decoderConfig, "decoderConfig");
135+
}
136+
137+
private static String[] parseSubprotocols(String subprotocols) {
111138
if (subprotocols != null) {
112139
String[] subprotocolArray = subprotocols.split(",");
113140
for (int i = 0; i < subprotocolArray.length; i++) {
114141
subprotocolArray[i] = subprotocolArray[i].trim();
115142
}
116-
this.subprotocols = subprotocolArray;
117-
} else {
118-
this.subprotocols = EmptyArrays.EMPTY_STRINGS;
143+
return subprotocolArray;
119144
}
120-
this.decoderConfig = ObjectUtil.checkNotNull(decoderConfig, "decoderConfig");
145+
return EmptyArrays.EMPTY_STRINGS;
121146
}
122147

123148
/**
124149
* Returns the URL of the web socket
125150
*/
126151
@Deprecated
127152
public String uri() {
128-
return uri;
153+
if (uri != null) {
154+
return uri;
155+
}
156+
return (isSecure ? "wss" : "ws") + "://" + host + path;
129157
}
130158

131159
/**
Collapse file

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

Copy file name to clipboardExpand all lines: codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketServerHandshaker00.java
+5Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,11 @@ public WebSocketServerHandshaker00(String webSocketURL, String subprotocols, Web
8383
super(WebSocketVersion.V00, webSocketURL, subprotocols, decoderConfig);
8484
}
8585

86+
WebSocketServerHandshaker00(boolean isSecure, String host, String path,
87+
String subprotocols, WebSocketDecoderConfig decoderConfig) {
88+
super(WebSocketVersion.V00, isSecure, host, path, subprotocols, decoderConfig);
89+
}
90+
8691
/**
8792
* <p>
8893
* Handle the web socket handshake for the web socket specification <a href=
Collapse file

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

Copy file name to clipboardExpand all lines: codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketServerHandshaker07.java
+5Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,11 @@ public WebSocketServerHandshaker07(String webSocketURL, String subprotocols, Web
9595
super(WebSocketVersion.V07, webSocketURL, subprotocols, decoderConfig);
9696
}
9797

98+
WebSocketServerHandshaker07(boolean isSecure, String host, String path,
99+
String subprotocols, WebSocketDecoderConfig decoderConfig) {
100+
super(WebSocketVersion.V07, isSecure, host, path, subprotocols, decoderConfig);
101+
}
102+
98103
/**
99104
* <p>
100105
* Handle the web socket handshake for the web socket specification <a href=
Collapse file

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

Copy file name to clipboardExpand all lines: codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketServerHandshaker08.java
+5Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,11 @@ public WebSocketServerHandshaker08(
101101
super(WebSocketVersion.V08, webSocketURL, subprotocols, decoderConfig);
102102
}
103103

104+
WebSocketServerHandshaker08(boolean isSecure, String host, String path,
105+
String subprotocols, WebSocketDecoderConfig decoderConfig) {
106+
super(WebSocketVersion.V08, isSecure, host, path, subprotocols, decoderConfig);
107+
}
108+
104109
/**
105110
* <p>
106111
* Handle the web socket handshake for the web socket specification <a href=
Collapse file

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

Copy file name to clipboardExpand all lines: codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketServerHandshaker13.java
+5Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,11 @@ public WebSocketServerHandshaker13(
103103
super(WebSocketVersion.V13, webSocketURL, subprotocols, decoderConfig);
104104
}
105105

106+
WebSocketServerHandshaker13(boolean isSecure, String host, String path,
107+
String subprotocols, WebSocketDecoderConfig decoderConfig) {
108+
super(WebSocketVersion.V13, isSecure, host, path, subprotocols, decoderConfig);
109+
}
110+
106111
/**
107112
* <p>
108113
* Handle the web socket handshake for the web socket specification <a href=
Collapse file

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

Copy file name to clipboardExpand all lines: codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketServerHandshakerFactory.java
+36Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,13 @@ public static WebSocketServerHandshaker resolveHandshaker(HttpRequest req, Strin
155155
return resolveHandshaker0(req, webSocketURL, subprotocols, decoderConfig);
156156
}
157157

158+
// Like resolveHandshaker(HttpRequest, String, String, WebSocketDecoderConfig) but defers URI construction.
159+
static WebSocketServerHandshaker resolveHandshaker(HttpRequest req, boolean isSecure, String host, String path,
160+
String subprotocols, WebSocketDecoderConfig decoderConfig) {
161+
ObjectUtil.checkNotNull(decoderConfig, "decoderConfig");
162+
return resolveHandshaker0(req, isSecure, host, path, subprotocols, decoderConfig);
163+
}
164+
158165
private static WebSocketServerHandshaker resolveHandshaker0(HttpRequest req,
159166
String webSocketURL,
160167
String subprotocols,
@@ -182,6 +189,35 @@ private static WebSocketServerHandshaker resolveHandshaker0(HttpRequest req,
182189
}
183190
}
184191

192+
private static WebSocketServerHandshaker resolveHandshaker0(HttpRequest req,
193+
boolean isSecure,
194+
String host,
195+
String path,
196+
String subprotocols,
197+
WebSocketDecoderConfig decoderConfig) {
198+
CharSequence version = req.headers().get(HttpHeaderNames.SEC_WEBSOCKET_VERSION);
199+
if (version != null) {
200+
if (version.equals(WebSocketVersion.V13.toHttpHeaderValue())) {
201+
// Version 13 of the wire protocol - RFC 6455 (version 17 of the draft hybi specification).
202+
return new WebSocketServerHandshaker13(
203+
isSecure, host, path, subprotocols, decoderConfig);
204+
} else if (version.equals(WebSocketVersion.V08.toHttpHeaderValue())) {
205+
// Version 8 of the wire protocol - version 10 of the draft hybi specification.
206+
return new WebSocketServerHandshaker08(
207+
isSecure, host, path, subprotocols, decoderConfig);
208+
} else if (version.equals(WebSocketVersion.V07.toHttpHeaderValue())) {
209+
// Version 8 of the wire protocol - version 07 of the draft hybi specification.
210+
return new WebSocketServerHandshaker07(
211+
isSecure, host, path, subprotocols, decoderConfig);
212+
} else {
213+
return null;
214+
}
215+
} else {
216+
// Assume version 00 where version header was not specified
217+
return new WebSocketServerHandshaker00(isSecure, host, path, subprotocols, decoderConfig);
218+
}
219+
}
220+
185221
/**
186222
* @deprecated use {@link #sendUnsupportedVersionResponse(Channel)}
187223
*/
Collapse file

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

Copy file name to clipboardExpand all lines: codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketServerProtocolHandshakeHandler.java
+3-13Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
import io.netty.channel.ChannelHandlerContext;
1919
import io.netty.channel.ChannelInboundHandler;
20-
import io.netty.channel.ChannelPipeline;
2120
import io.netty.handler.codec.http.HttpHeaderNames;
2221
import io.netty.handler.codec.http.HttpObject;
2322
import io.netty.handler.codec.http.HttpRequest;
@@ -64,9 +63,10 @@ public void channelRead(final ChannelHandlerContext ctx, Object msg) throws Exce
6463
}
6564

6665
try {
66+
final boolean isSecure = ctx.pipeline().get(SslHandler.class) != null;
67+
final String host = req.headers().get(HttpHeaderNames.HOST);
6768
final WebSocketServerHandshaker handshaker = WebSocketServerHandshakerFactory.resolveHandshaker(
68-
req,
69-
getWebSocketLocation(ctx.pipeline(), req, serverConfig.websocketPath()),
69+
req, isSecure, host, serverConfig.websocketPath(),
7070
serverConfig.subprotocols(), serverConfig.decoderConfig());
7171
final Promise<Void> localHandshakePromise = handshakePromise;
7272
if (handshaker == null) {
@@ -125,16 +125,6 @@ private boolean checkNextUri(String uri, String websocketPath) {
125125
return true;
126126
}
127127

128-
private static String getWebSocketLocation(ChannelPipeline cp, HttpRequest req, String path) {
129-
String protocol = "ws";
130-
if (cp.get(SslHandler.class) != null) {
131-
// SSL in use so use Secure WebSockets
132-
protocol = "wss";
133-
}
134-
String host = req.headers().get(HttpHeaderNames.HOST);
135-
return protocol + "://" + host + path;
136-
}
137-
138128
private void applyHandshakeTimeout() {
139129
final Promise<Void> localHandshakePromise = handshakePromise;
140130
final long handshakeTimeoutMillis = serverConfig.handshakeTimeoutMillis();

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.