GeezerVison is showing github.com — Avoid unnecessary String allocation in WebSocketServerProtocolHandsha… · netty/netty@03eeaca
File treeExpand file treeCollapse file tree Open diff view settings codec-http/src/main/java/io/netty/handler/codec/http/websocketx
Expand file treeCollapse file tree Open diff view settings Collapse file 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 number | Diff line number | Diff line change |
|---|
@@ -58,6 +58,12 @@ public abstract class WebSocketServerHandshaker { | 58 | 58 |
| 59 | 59 | private final String uri; | 60 | 60 |
| | 61 | + private final boolean isSecure; | | 62 | + | | 63 | + private final String host; | | 64 | + | | 65 | + private final String path; | | 66 | + | 61 | 67 | private final String[] subprotocols; | 62 | 68 |
| 63 | 69 | private final WebSocketVersion version; | @@ -109,24 +115,46 @@ protected WebSocketServerHandshaker( | 109 | 115 | WebSocketVersion version, String uri, String subprotocols, WebSocketDecoderConfig decoderConfig) { | 110 | 116 | this.version = version; | 111 | 117 | 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) { | 112 | 139 | if (subprotocols != null) { | 113 | 140 | String[] subprotocolArray = subprotocols.split(","); | 114 | 141 | for (int i = 0; i < subprotocolArray.length; i++) { | 115 | 142 | subprotocolArray[i] = subprotocolArray[i].trim(); | 116 | 143 | } | 117 | | - this.subprotocols = subprotocolArray; | 118 | | - } else { | 119 | | - this.subprotocols = EmptyArrays.EMPTY_STRINGS; | | 144 | + return subprotocolArray; | 120 | 145 | } | 121 | | - this.decoderConfig = ObjectUtil.checkNotNull(decoderConfig, "decoderConfig"); | | 146 | + return EmptyArrays.EMPTY_STRINGS; | 122 | 147 | } | 123 | 148 |
| 124 | 149 | /** | 125 | 150 | * Returns the URL of the web socket | 126 | 151 | */ | 127 | 152 | @Deprecated | 128 | 153 | public String uri() { | 129 | | - return uri; | | 154 | + if (uri != null) { | | 155 | + return uri; | | 156 | + } | | 157 | + return (isSecure ? "wss" : "ws") + "://" + host + path; | 130 | 158 | } | 131 | 159 |
| 132 | 160 | /** | |
Collapse file 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 number | Diff line number | Diff line change |
|---|
@@ -83,6 +83,11 @@ public WebSocketServerHandshaker00(String webSocketURL, String subprotocols, Web | 83 | 83 | super(WebSocketVersion.V00, webSocketURL, subprotocols, decoderConfig); | 84 | 84 | } | 85 | 85 |
| | 86 | + WebSocketServerHandshaker00(boolean isSecure, String host, String path, | | 87 | + String subprotocols, WebSocketDecoderConfig decoderConfig) { | | 88 | + super(WebSocketVersion.V00, isSecure, host, path, subprotocols, decoderConfig); | | 89 | + } | | 90 | + | 86 | 91 | /** | 87 | 92 | * <p> | 88 | 93 | * Handle the web socket handshake for the web socket specification <a href= | |
Collapse file 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 number | Diff line number | Diff line change |
|---|
@@ -95,6 +95,11 @@ public WebSocketServerHandshaker07(String webSocketURL, String subprotocols, Web | 95 | 95 | super(WebSocketVersion.V07, webSocketURL, subprotocols, decoderConfig); | 96 | 96 | } | 97 | 97 |
| | 98 | + WebSocketServerHandshaker07(boolean isSecure, String host, String path, | | 99 | + String subprotocols, WebSocketDecoderConfig decoderConfig) { | | 100 | + super(WebSocketVersion.V07, isSecure, host, path, subprotocols, decoderConfig); | | 101 | + } | | 102 | + | 98 | 103 | /** | 99 | 104 | * <p> | 100 | 105 | * Handle the web socket handshake for the web socket specification <a href= | |
Collapse file 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 number | Diff line number | Diff line change |
|---|
@@ -101,6 +101,11 @@ public WebSocketServerHandshaker08( | 101 | 101 | super(WebSocketVersion.V08, webSocketURL, subprotocols, decoderConfig); | 102 | 102 | } | 103 | 103 |
| | 104 | + WebSocketServerHandshaker08(boolean isSecure, String host, String path, | | 105 | + String subprotocols, WebSocketDecoderConfig decoderConfig) { | | 106 | + super(WebSocketVersion.V08, isSecure, host, path, subprotocols, decoderConfig); | | 107 | + } | | 108 | + | 104 | 109 | /** | 105 | 110 | * <p> | 106 | 111 | * Handle the web socket handshake for the web socket specification <a href= | |
Collapse file 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 number | Diff line number | Diff line change |
|---|
@@ -103,6 +103,11 @@ public WebSocketServerHandshaker13( | 103 | 103 | super(WebSocketVersion.V13, webSocketURL, subprotocols, decoderConfig); | 104 | 104 | } | 105 | 105 |
| | 106 | + WebSocketServerHandshaker13(boolean isSecure, String host, String path, | | 107 | + String subprotocols, WebSocketDecoderConfig decoderConfig) { | | 108 | + super(WebSocketVersion.V13, isSecure, host, path, subprotocols, decoderConfig); | | 109 | + } | | 110 | + | 106 | 111 | /** | 107 | 112 | * <p> | 108 | 113 | * Handle the web socket handshake for the web socket specification <a href= | |
Collapse file 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 number | Diff line number | Diff line change |
|---|
@@ -155,6 +155,13 @@ public static WebSocketServerHandshaker resolveHandshaker(HttpRequest req, Strin | 155 | 155 | return resolveHandshaker0(req, webSocketURL, subprotocols, decoderConfig); | 156 | 156 | } | 157 | 157 |
| | 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 | + | 158 | 165 | private static WebSocketServerHandshaker resolveHandshaker0(HttpRequest req, | 159 | 166 | String webSocketURL, | 160 | 167 | String subprotocols, | @@ -182,6 +189,35 @@ private static WebSocketServerHandshaker resolveHandshaker0(HttpRequest req, | 182 | 189 | } | 183 | 190 | } | 184 | 191 |
| | 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 | + | 185 | 221 | /** | 186 | 222 | * @deprecated use {@link #sendUnsupportedVersionResponse(Channel)} | 187 | 223 | */ | |
Collapse file 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 number | Diff line number | Diff line change |
|---|
| 18 | 18 | import io.netty.channel.ChannelFuture; | 19 | 19 | import io.netty.channel.ChannelHandlerContext; | 20 | 20 | import io.netty.channel.ChannelInboundHandlerAdapter; | 21 | | -import io.netty.channel.ChannelPipeline; | 22 | 21 | import io.netty.channel.ChannelPromise; | 23 | 22 | import io.netty.handler.codec.http.HttpHeaderNames; | 24 | 23 | import io.netty.handler.codec.http.HttpObject; | 25 | 24 | import io.netty.handler.codec.http.HttpRequest; | 26 | | -import io.netty.handler.codec.http.HttpResponse; | 27 | 25 | import io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler.ServerHandshakeStateEvent; | 28 | 26 | import io.netty.handler.ssl.SslHandler; | 29 | 27 | import io.netty.util.ReferenceCountUtil; | @@ -67,9 +65,10 @@ public void channelRead(final ChannelHandlerContext ctx, Object msg) throws Exce | 67 | 65 | } | 68 | 66 |
| 69 | 67 | try { | | 68 | + final boolean isSecure = ctx.pipeline().get(SslHandler.class) != null; | | 69 | + final String host = req.headers().get(HttpHeaderNames.HOST); | 70 | 70 | final WebSocketServerHandshaker handshaker = WebSocketServerHandshakerFactory.resolveHandshaker( | 71 | | - req, | 72 | | - getWebSocketLocation(ctx.pipeline(), req, serverConfig.websocketPath()), | | 71 | + req, isSecure, host, serverConfig.websocketPath(), | 73 | 72 | serverConfig.subprotocols(), serverConfig.decoderConfig()); | 74 | 73 | final ChannelPromise localHandshakePromise = handshakePromise; | 75 | 74 | if (handshaker == null) { | @@ -128,16 +127,6 @@ private boolean checkNextUri(String uri, String websocketPath) { | 128 | 127 | return true; | 129 | 128 | } | 130 | 129 |
| 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 | | - | 141 | 130 | private void applyHandshakeTimeout() { | 142 | 131 | final ChannelPromise localHandshakePromise = handshakePromise; | 143 | 132 | final long handshakeTimeoutMillis = serverConfig.handshakeTimeoutMillis(); | |
|