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

View file

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

View file

@ -4,6 +4,7 @@
package forward
import (
"bufio"
"crypto/tls"
"io"
"net"
@ -290,14 +291,26 @@ func (f *websocketForwarder) serveHTTP(w http.ResponseWriter, req *http.Request,
ctx.errHandler.ServeHTTP(w, req, err)
return
}
errc := make(chan error, 2)
replicate := func(dst io.Writer, src io.Reader) {
_, err := io.Copy(dst, src)
errc <- err
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)
replicate := func(dst io.Writer, src io.Reader) {
_, err := io.Copy(dst, src)
errc <- err
}
go replicate(targetConn, underlyingConn)
go replicate(underlyingConn, targetConn)
<-errc
} else {
ctx.log.Infof("Switching protocol failed")
}
go replicate(targetConn, underlyingConn)
go replicate(underlyingConn, targetConn)
<-errc
}
// copyRequest makes a copy of the specified request.