Skip to content

Commit

Permalink
Issue #421: Close websocket connection on failure
Browse files Browse the repository at this point in the history
The websocket proxy is implemented as a raw tcp proxy
which relies on the client and server to close the
connection. When a websocket upgrade fails the upstream
server may keep the connection open.

If a proxy like nginx is used in front of fabio
it will keep its connection to fabio open effectively
establishing a direct channel between nginx and the
upstream server which will be used for any request
forwarded by nginx to fabio.

Adding a 'Connection: close' header to the upstream
request should indicate to the server to close the
connection. If that works then we can keep the raw
tcp proxy for websockets. Otherwise, fabio needs
to handle the handshake and close the connection
itself.

Fixes #421
  • Loading branch information
magiconair committed Mar 22, 2018
1 parent f6be07e commit a2ba01f
Showing 1 changed file with 1 addition and 0 deletions.
1 change: 1 addition & 0 deletions proxy/http_proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ func (p *HTTPProxy) ServeHTTP(w http.ResponseWriter, r *http.Request) {
} else {
h = newRawProxy(targetURL.Host, net.Dial)
}
r.Header.Set("Connection", "close")

case accept == "text/event-stream":
// use the flush interval for SSE (server-sent events)
Expand Down

1 comment on commit a2ba01f

@craigday
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This violates the RFC, and looking at our Websocket engine, will definitely not work.

5. The request MUST contain an |Upgrade| header field whose value MUST include the "websocket" keyword.
6. The request MUST contain a |Connection| header field whose value MUST include the "Upgrade" token.

https://tools.ietf.org/html/rfc6455 Page 16/17

You might get away with r.Header.Add("Connection", "close") but ultimately I think you should be interpreting the HTTP response to the upgrade request, to know if you should keep the raw proxy in place or close.

Any status code other than 101 indicates that the WebSocket handshake has not completed and that the semantics of HTTP still apply.

https://tools.ietf.org/html/rfc6455 Page 7

Please sign in to comment.