Use WebSocketVersion.toAsciiString() as header value when possible (#10105)

Motivation:

In our WebSocketClientHandshaker* implementations we "hardcode" the version number to use. This is error-prone, we should better use the WebSocketVersion so we dont need to maintain the value multiple times. Beside this we can also use an AsciiString to improve performance

Modifications:

- Use WebSocketVersion.toAsciiString

Result:

Less stuff to maintain and small performance win
This commit is contained in:
Norman Maurer 2020-03-13 07:48:58 +01:00
parent b1a9d923ce
commit b595ceab49
4 changed files with 19 additions and 13 deletions

View File

@ -241,7 +241,7 @@ public class WebSocketClientHandshaker07 extends WebSocketClientHandshaker {
headers.set(HttpHeaderNames.SEC_WEBSOCKET_PROTOCOL, expectedSubprotocol);
}
headers.set(HttpHeaderNames.SEC_WEBSOCKET_VERSION, "7");
headers.set(HttpHeaderNames.SEC_WEBSOCKET_VERSION, version().toAsciiString());
return request;
}

View File

@ -243,7 +243,7 @@ public class WebSocketClientHandshaker08 extends WebSocketClientHandshaker {
headers.set(HttpHeaderNames.SEC_WEBSOCKET_PROTOCOL, expectedSubprotocol);
}
headers.set(HttpHeaderNames.SEC_WEBSOCKET_VERSION, "8");
headers.set(HttpHeaderNames.SEC_WEBSOCKET_VERSION, version().toAsciiString());
return request;
}

View File

@ -244,7 +244,7 @@ public class WebSocketClientHandshaker13 extends WebSocketClientHandshaker {
headers.set(HttpHeaderNames.SEC_WEBSOCKET_PROTOCOL, expectedSubprotocol);
}
headers.set(HttpHeaderNames.SEC_WEBSOCKET_VERSION, "13");
headers.set(HttpHeaderNames.SEC_WEBSOCKET_VERSION, version().toAsciiString());
return request;
}

View File

@ -15,6 +15,8 @@
*/
package io.netty.handler.codec.http.websocketx;
import io.netty.util.AsciiString;
/**
* <p>
* Versions of the web socket specification.
@ -25,43 +27,47 @@ package io.netty.handler.codec.http.websocketx;
* </p>
*/
public enum WebSocketVersion {
UNKNOWN("unknown"),
UNKNOWN(AsciiString.cached("unknown")),
/**
* <a href= "http://tools.ietf.org/html/draft-ietf-hybi-thewebsocketprotocol-00"
* >draft-ietf-hybi-thewebsocketprotocol- 00</a>.
*/
V00("0"),
V00(AsciiString.cached("0")),
/**
* <a href= "http://tools.ietf.org/html/draft-ietf-hybi-thewebsocketprotocol-07"
* >draft-ietf-hybi-thewebsocketprotocol- 07</a>
*/
V07("7"),
V07(AsciiString.cached("7")),
/**
* <a href= "http://tools.ietf.org/html/draft-ietf-hybi-thewebsocketprotocol-10"
* >draft-ietf-hybi-thewebsocketprotocol- 10</a>
*/
V08("8"),
V08(AsciiString.cached("8")),
/**
* <a href="http://tools.ietf.org/html/rfc6455 ">RFC 6455</a>. This was originally <a href=
* "http://tools.ietf.org/html/draft-ietf-hybi-thewebsocketprotocol-17" >draft-ietf-hybi-thewebsocketprotocol-
* 17</a>
*/
V13("13");
V13(AsciiString.cached("13"));
private final String version;
private final AsciiString headerValue;
WebSocketVersion(String version) {
this.version = version;
WebSocketVersion(AsciiString headerValue) {
this.headerValue = headerValue;
}
/**
* @return Value for HTTP Header 'Sec-WebSocket-Version'
*/
public String toHttpHeaderValue() {
return version;
return toAsciiString().toString();
}
AsciiString toAsciiString() {
return headerValue;
}
}