GeezerVison is showing github.com — Port 5.0: Avoid unnecessary String allocation in WebSocketServerProto… · netty/netty@196bb3a
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 |
|---|
@@ -57,6 +57,12 @@ public abstract class WebSocketServerHandshaker { | 57 | 57 |
| 58 | 58 | private final String uri; | 59 | 59 |
| | 60 | + private final boolean isSecure; | | 61 | + | | 62 | + private final String host; | | 63 | + | | 64 | + private final String path; | | 65 | + | 60 | 66 | private final String[] subprotocols; | 61 | 67 |
| 62 | 68 | private final WebSocketVersion version; | @@ -108,24 +114,46 @@ protected WebSocketServerHandshaker( | 108 | 114 | WebSocketVersion version, String uri, String subprotocols, WebSocketDecoderConfig decoderConfig) { | 109 | 115 | this.version = version; | 110 | 116 | 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) { | 111 | 138 | if (subprotocols != null) { | 112 | 139 | String[] subprotocolArray = subprotocols.split(","); | 113 | 140 | for (int i = 0; i < subprotocolArray.length; i++) { | 114 | 141 | subprotocolArray[i] = subprotocolArray[i].trim(); | 115 | 142 | } | 116 | | - this.subprotocols = subprotocolArray; | 117 | | - } else { | 118 | | - this.subprotocols = EmptyArrays.EMPTY_STRINGS; | | 143 | + return subprotocolArray; | 119 | 144 | } | 120 | | - this.decoderConfig = ObjectUtil.checkNotNull(decoderConfig, "decoderConfig"); | | 145 | + return EmptyArrays.EMPTY_STRINGS; | 121 | 146 | } | 122 | 147 |
| 123 | 148 | /** | 124 | 149 | * Returns the URL of the web socket | 125 | 150 | */ | 126 | 151 | @Deprecated | 127 | 152 | public String uri() { | 128 | | - return uri; | | 153 | + if (uri != null) { | | 154 | + return uri; | | 155 | + } | | 156 | + return (isSecure ? "wss" : "ws") + "://" + host + path; | 129 | 157 | } | 130 | 158 |
| 131 | 159 | /** | |
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-13Lines changed: 3 additions & 13 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 17 | 17 |
| 18 | 18 | import io.netty.channel.ChannelHandlerContext; | 19 | 19 | import io.netty.channel.ChannelInboundHandler; | 20 | | -import io.netty.channel.ChannelPipeline; | 21 | 20 | import io.netty.handler.codec.http.HttpHeaderNames; | 22 | 21 | import io.netty.handler.codec.http.HttpObject; | 23 | 22 | import io.netty.handler.codec.http.HttpRequest; | @@ -64,9 +63,10 @@ public void channelRead(final ChannelHandlerContext ctx, Object msg) throws Exce | 64 | 63 | } | 65 | 64 |
| 66 | 65 | try { | | 66 | + final boolean isSecure = ctx.pipeline().get(SslHandler.class) != null; | | 67 | + final String host = req.headers().get(HttpHeaderNames.HOST); | 67 | 68 | final WebSocketServerHandshaker handshaker = WebSocketServerHandshakerFactory.resolveHandshaker( | 68 | | - req, | 69 | | - getWebSocketLocation(ctx.pipeline(), req, serverConfig.websocketPath()), | | 69 | + req, isSecure, host, serverConfig.websocketPath(), | 70 | 70 | serverConfig.subprotocols(), serverConfig.decoderConfig()); | 71 | 71 | final Promise<Void> localHandshakePromise = handshakePromise; | 72 | 72 | if (handshaker == null) { | @@ -125,16 +125,6 @@ private boolean checkNextUri(String uri, String websocketPath) { | 125 | 125 | return true; | 126 | 126 | } | 127 | 127 |
| 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 | | - | 138 | 128 | private void applyHandshakeTimeout() { | 139 | 129 | final Promise<Void> localHandshakePromise = handshakePromise; | 140 | 130 | final long handshakeTimeoutMillis = serverConfig.handshakeTimeoutMillis(); | |
|