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 unnecessary String allocation in WebSocketServerProtocolHandsha… · netty/netty@03eeaca

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
@@ -58,6 +58,12 @@ public abstract class WebSocketServerHandshaker {
5858

5959
private final String uri;
6060

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

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

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

132160
/**
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-14Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,10 @@
1818
import io.netty.channel.ChannelFuture;
1919
import io.netty.channel.ChannelHandlerContext;
2020
import io.netty.channel.ChannelInboundHandlerAdapter;
21-
import io.netty.channel.ChannelPipeline;
2221
import io.netty.channel.ChannelPromise;
2322
import io.netty.handler.codec.http.HttpHeaderNames;
2423
import io.netty.handler.codec.http.HttpObject;
2524
import io.netty.handler.codec.http.HttpRequest;
26-
import io.netty.handler.codec.http.HttpResponse;
2725
import io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler.ServerHandshakeStateEvent;
2826
import io.netty.handler.ssl.SslHandler;
2927
import io.netty.util.ReferenceCountUtil;
@@ -67,9 +65,10 @@ public void channelRead(final ChannelHandlerContext ctx, Object msg) throws Exce
6765
}
6866

6967
try {
68+
final boolean isSecure = ctx.pipeline().get(SslHandler.class) != null;
69+
final String host = req.headers().get(HttpHeaderNames.HOST);
7070
final WebSocketServerHandshaker handshaker = WebSocketServerHandshakerFactory.resolveHandshaker(
71-
req,
72-
getWebSocketLocation(ctx.pipeline(), req, serverConfig.websocketPath()),
71+
req, isSecure, host, serverConfig.websocketPath(),
7372
serverConfig.subprotocols(), serverConfig.decoderConfig());
7473
final ChannelPromise localHandshakePromise = handshakePromise;
7574
if (handshaker == null) {
@@ -128,16 +127,6 @@ private boolean checkNextUri(String uri, String websocketPath) {
128127
return true;
129128
}
130129

131-
private static String getWebSocketLocation(ChannelPipeline cp, HttpRequest req, String path) {
132-
String protocol = "ws";
133-
if (cp.get(SslHandler.class) != null) {
134-
// SSL in use so use Secure WebSockets
135-
protocol = "wss";
136-
}
137-
String host = req.headers().get(HttpHeaderNames.HOST);
138-
return protocol + "://" + host + path;
139-
}
140-
141130
private void applyHandshakeTimeout() {
142131
final ChannelPromise localHandshakePromise = handshakePromise;
143132
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.