Skip to content

Commit c302468

Browse files
authored
fix: upgrade headers if hop headers are removed (zalando#3553)
fix: upgrade headers if hop headers are removed fix: zalando#3548 Signed-off-by: Sandor Szücs <[email protected]>
1 parent d389762 commit c302468

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

proxy/proxy.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -967,6 +967,8 @@ func (p *Proxy) makeUpgradeRequest(ctx *context, req *http.Request) {
967967
}
968968

969969
func (p *Proxy) makeBackendRequest(ctx *context, requestContext stdlibcontext.Context) (*http.Response, *proxyError) {
970+
payloadProtocol := getUpgradeRequest(ctx.Request())
971+
970972
req, endpointMetrics, err := p.mapRequest(ctx, requestContext)
971973
if err != nil {
972974
return nil, &proxyError{err: fmt.Errorf("could not map backend request: %w", err)}
@@ -981,7 +983,10 @@ func (p *Proxy) makeBackendRequest(ctx *context, requestContext stdlibcontext.Co
981983
defer endpointMetrics.DecInflightRequest()
982984
}
983985

984-
if p.experimentalUpgrade && isUpgradeRequest(req) {
986+
if p.experimentalUpgrade && payloadProtocol != "" {
987+
// see also https://github.com/golang/go/blob/9159cd4ec6b0e9475dc9c71c830035c1c4c13483/src/net/http/httputil/reverseproxy.go#L423-L428
988+
req.Header.Set("Connection", "Upgrade")
989+
req.Header.Set("Upgrade", payloadProtocol)
985990
p.makeUpgradeRequest(ctx, req)
986991

987992
// We are not owner of the connection anymore.

proxy/upgrade_test.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ func TestServeHTTP(t *testing.T) {
9595
backendClosesConnection bool
9696
backendHangs bool
9797
noBackend bool
98+
dropHopHeaders bool
9899
backendStatusCode int
99100
expectedResponseStatusCode int
100101
expectedResponseBody string
@@ -112,6 +113,13 @@ func TestServeHTTP(t *testing.T) {
112113
method: http.MethodGet,
113114
backendStatusCode: http.StatusSwitchingProtocols,
114115
},
116+
{
117+
msg: "Simple route with hop-headers dropped",
118+
route: `route: Path("/ws") -> "%s";`,
119+
method: http.MethodGet,
120+
dropHopHeaders: true,
121+
backendStatusCode: http.StatusSwitchingProtocols,
122+
},
115123
{
116124
msg: "Wrong method",
117125
route: `route: Path("/ws") -> "%s";`,
@@ -247,7 +255,14 @@ func TestServeHTTP(t *testing.T) {
247255
defer backend.Close()
248256
}
249257

250-
tp, err := newTestProxyWithParams(routes, Params{ExperimentalUpgrade: true})
258+
flags := FlagsNone
259+
if ti.dropHopHeaders {
260+
flags = HopHeadersRemoval
261+
}
262+
tp, err := newTestProxyWithParams(routes, Params{
263+
ExperimentalUpgrade: true,
264+
Flags: flags,
265+
})
251266
require.NoError(t, err)
252267

253268
defer tp.close()

0 commit comments

Comments
 (0)