mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-27 05:27:21 +00:00
Merge pull request #89857 from tedyu/hdr-protocol-ver
Correctly parse X-Stream-Protocol-Version header
This commit is contained in:
commit
b3db9d3c6c
@ -114,6 +114,18 @@ func negotiateProtocol(clientProtocols, serverProtocols []string) string {
|
|||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func commaSeparatedHeaderValues(header []string) []string {
|
||||||
|
var parsedClientProtocols []string
|
||||||
|
for i := range header {
|
||||||
|
for _, clientProtocol := range strings.Split(header[i], ",") {
|
||||||
|
if proto := strings.Trim(clientProtocol, " "); len(proto) > 0 {
|
||||||
|
parsedClientProtocols = append(parsedClientProtocols, proto)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return parsedClientProtocols
|
||||||
|
}
|
||||||
|
|
||||||
// Handshake performs a subprotocol negotiation. If the client did request a
|
// Handshake performs a subprotocol negotiation. If the client did request a
|
||||||
// subprotocol, Handshake will select the first common value found in
|
// subprotocol, Handshake will select the first common value found in
|
||||||
// serverProtocols. If a match is found, Handshake adds a response header
|
// serverProtocols. If a match is found, Handshake adds a response header
|
||||||
@ -121,7 +133,7 @@ func negotiateProtocol(clientProtocols, serverProtocols []string) string {
|
|||||||
// returned, along with a response header containing the list of protocols the
|
// returned, along with a response header containing the list of protocols the
|
||||||
// server can accept.
|
// server can accept.
|
||||||
func Handshake(req *http.Request, w http.ResponseWriter, serverProtocols []string) (string, error) {
|
func Handshake(req *http.Request, w http.ResponseWriter, serverProtocols []string) (string, error) {
|
||||||
clientProtocols := req.Header[http.CanonicalHeaderKey(HeaderProtocolVersion)]
|
clientProtocols := commaSeparatedHeaderValues(req.Header[http.CanonicalHeaderKey(HeaderProtocolVersion)])
|
||||||
if len(clientProtocols) == 0 {
|
if len(clientProtocols) == 0 {
|
||||||
return "", fmt.Errorf("unable to upgrade: %s is required", HeaderProtocolVersion)
|
return "", fmt.Errorf("unable to upgrade: %s is required", HeaderProtocolVersion)
|
||||||
}
|
}
|
||||||
|
@ -58,11 +58,22 @@ func TestHandshake(t *testing.T) {
|
|||||||
expectedProtocol: "",
|
expectedProtocol: "",
|
||||||
expectError: true,
|
expectError: true,
|
||||||
},
|
},
|
||||||
|
"no common protocol with comma separated list": {
|
||||||
|
clientProtocols: []string{"c, d"},
|
||||||
|
serverProtocols: []string{"a", "b"},
|
||||||
|
expectedProtocol: "",
|
||||||
|
expectError: true,
|
||||||
|
},
|
||||||
"common protocol": {
|
"common protocol": {
|
||||||
clientProtocols: []string{"b"},
|
clientProtocols: []string{"b"},
|
||||||
serverProtocols: []string{"a", "b"},
|
serverProtocols: []string{"a", "b"},
|
||||||
expectedProtocol: "b",
|
expectedProtocol: "b",
|
||||||
},
|
},
|
||||||
|
"common protocol with comma separated list": {
|
||||||
|
clientProtocols: []string{"b, c"},
|
||||||
|
serverProtocols: []string{"a", "b"},
|
||||||
|
expectedProtocol: "b",
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for name, test := range tests {
|
for name, test := range tests {
|
||||||
|
Loading…
Reference in New Issue
Block a user