Skip to content

Commit 8aada68

Browse files
committed
Correctly parse X-Stream-Protocol-Version header
Signed-off-by: Ted Yu <[email protected]>
1 parent 7233908 commit 8aada68

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

staging/src/k8s.io/apimachinery/pkg/util/httpstream/httpstream.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,14 +114,26 @@ func negotiateProtocol(clientProtocols, serverProtocols []string) string {
114114
return ""
115115
}
116116

117+
func commaSeparatedHeaderValues(header []string) []string {
118+
var parsedClientProtocols []string
119+
for i := range header {
120+
for _, clientProtocol := range strings.Split(header[i], ",") {
121+
if proto := strings.Trim(clientProtocol, " "); len(proto) > 0 {
122+
parsedClientProtocols = append(parsedClientProtocols, proto)
123+
}
124+
}
125+
}
126+
return parsedClientProtocols
127+
}
128+
117129
// Handshake performs a subprotocol negotiation. If the client did request a
118130
// subprotocol, Handshake will select the first common value found in
119131
// serverProtocols. If a match is found, Handshake adds a response header
120132
// indicating the chosen subprotocol. If no match is found, HTTP forbidden is
121133
// returned, along with a response header containing the list of protocols the
122134
// server can accept.
123135
func Handshake(req *http.Request, w http.ResponseWriter, serverProtocols []string) (string, error) {
124-
clientProtocols := req.Header[http.CanonicalHeaderKey(HeaderProtocolVersion)]
136+
clientProtocols := commaSeparatedHeaderValues(req.Header[http.CanonicalHeaderKey(HeaderProtocolVersion)])
125137
if len(clientProtocols) == 0 {
126138
return "", fmt.Errorf("unable to upgrade: %s is required", HeaderProtocolVersion)
127139
}

staging/src/k8s.io/apimachinery/pkg/util/httpstream/httpstream_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,22 @@ func TestHandshake(t *testing.T) {
5858
expectedProtocol: "",
5959
expectError: true,
6060
},
61+
"no common protocol with comma separated list": {
62+
clientProtocols: []string{"c, d"},
63+
serverProtocols: []string{"a", "b"},
64+
expectedProtocol: "",
65+
expectError: true,
66+
},
6167
"common protocol": {
6268
clientProtocols: []string{"b"},
6369
serverProtocols: []string{"a", "b"},
6470
expectedProtocol: "b",
6571
},
72+
"common protocol with comma separated list": {
73+
clientProtocols: []string{"b, c"},
74+
serverProtocols: []string{"a", "b"},
75+
expectedProtocol: "b",
76+
},
6677
}
6778

6879
for name, test := range tests {

0 commit comments

Comments
 (0)