fix: websocket when the connection upgrade failed.

This commit is contained in:
Fernandez Ludovic 2017-06-24 14:25:23 +02:00 committed by Ludovic Fernandez
parent fe4d0e95b3
commit f99f3b987e
3 changed files with 24 additions and 11 deletions

6
glide.lock generated
View file

@ -1,5 +1,5 @@
hash: e59e8244152a823cd3633fb09cdd583c4e5be78d7b50fb7047ba6b6a9ed5e8ec hash: 6e206389bc4f381387be8b5f0cc5a41224329752654ddb3c2a805adde0333217
updated: 2017-05-19T23:30:19.890844996+02:00 updated: 2017-06-17T14:30:19.890844996+02:00
imports: imports:
- name: cloud.google.com/go - name: cloud.google.com/go
version: 2e6a95edb1071d750f6d7db777bf66cd2997af6c version: 2e6a95edb1071d750f6d7db777bf66cd2997af6c
@ -409,7 +409,7 @@ imports:
- name: github.com/vdemeester/docker-events - name: github.com/vdemeester/docker-events
version: be74d4929ec1ad118df54349fda4b0cba60f849b version: be74d4929ec1ad118df54349fda4b0cba60f849b
- name: github.com/vulcand/oxy - name: github.com/vulcand/oxy
version: f88530866c561d24a6b5aac49f76d6351b788b9f version: ad5bdb606fa9c64db267f0e43d63834908bdb05e
repo: https://github.com/containous/oxy.git repo: https://github.com/containous/oxy.git
vcs: git vcs: git
subpackages: subpackages:

View file

@ -8,7 +8,7 @@ import:
- package: github.com/cenk/backoff - package: github.com/cenk/backoff
- package: github.com/containous/flaeg - package: github.com/containous/flaeg
- package: github.com/vulcand/oxy - package: github.com/vulcand/oxy
version: f88530866c561d24a6b5aac49f76d6351b788b9f version: ad5bdb606fa9c64db267f0e43d63834908bdb05e
repo: https://github.com/containous/oxy.git repo: https://github.com/containous/oxy.git
vcs: git vcs: git
subpackages: subpackages:

View file

@ -4,6 +4,7 @@
package forward package forward
import ( import (
"bufio"
"crypto/tls" "crypto/tls"
"io" "io"
"net" "net"
@ -290,6 +291,15 @@ func (f *websocketForwarder) serveHTTP(w http.ResponseWriter, req *http.Request,
ctx.errHandler.ServeHTTP(w, req, err) ctx.errHandler.ServeHTTP(w, req, err)
return return
} }
br := bufio.NewReader(targetConn)
resp, err := http.ReadResponse(br, req)
resp.Write(underlyingConn)
defer resp.Body.Close()
// We connect the conn only if the switching protocol has not failed
if resp.StatusCode == http.StatusSwitchingProtocols {
ctx.log.Infof("Switching protocol success")
errc := make(chan error, 2) errc := make(chan error, 2)
replicate := func(dst io.Writer, src io.Reader) { replicate := func(dst io.Writer, src io.Reader) {
_, err := io.Copy(dst, src) _, err := io.Copy(dst, src)
@ -298,6 +308,9 @@ func (f *websocketForwarder) serveHTTP(w http.ResponseWriter, req *http.Request,
go replicate(targetConn, underlyingConn) go replicate(targetConn, underlyingConn)
go replicate(underlyingConn, targetConn) go replicate(underlyingConn, targetConn)
<-errc <-errc
} else {
ctx.log.Infof("Switching protocol failed")
}
} }
// copyRequest makes a copy of the specified request. // copyRequest makes a copy of the specified request.