mirror of
https://github.com/caddyserver/caddy.git
synced 2025-01-08 11:58:49 +03:00
add supported for ws in reverse proxy
This commit is contained in:
parent
9bdd9bdecc
commit
2d6ff40649
2 changed files with 35 additions and 3 deletions
|
@ -4,8 +4,9 @@ import (
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/mholt/caddy/middleware/rewrite"
|
|
||||||
"regexp"
|
"regexp"
|
||||||
|
|
||||||
|
"github.com/mholt/caddy/middleware/rewrite"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestRewrite(t *testing.T) {
|
func TestRewrite(t *testing.T) {
|
||||||
|
|
|
@ -16,6 +16,8 @@ import (
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const HTTPSwitchProtocols = 101
|
||||||
|
|
||||||
// onExitFlushLoop is a callback set by tests to detect the state of the
|
// onExitFlushLoop is a callback set by tests to detect the state of the
|
||||||
// flushLoop() goroutine.
|
// flushLoop() goroutine.
|
||||||
var onExitFlushLoop func()
|
var onExitFlushLoop func()
|
||||||
|
@ -153,8 +155,37 @@ func (p *ReverseProxy) ServeHTTP(rw http.ResponseWriter, req *http.Request, extr
|
||||||
|
|
||||||
copyHeader(rw.Header(), res.Header)
|
copyHeader(rw.Header(), res.Header)
|
||||||
|
|
||||||
rw.WriteHeader(res.StatusCode)
|
if res.StatusCode == HTTPSwitchProtocols {
|
||||||
p.copyResponse(rw, res.Body)
|
hj, ok := rw.(http.Hijacker)
|
||||||
|
if !ok {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
conn, _, err := hj.Hijack()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
backendConn, err := net.Dial("tcp", outreq.Host)
|
||||||
|
if err != nil {
|
||||||
|
conn.Close()
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
outreq.Write(backendConn)
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
io.Copy(backendConn, conn) // write tcp stream to backend.
|
||||||
|
backendConn.Close()
|
||||||
|
}()
|
||||||
|
|
||||||
|
io.Copy(conn, backendConn) // read tcp stream from backend.
|
||||||
|
conn.Close()
|
||||||
|
} else {
|
||||||
|
rw.WriteHeader(res.StatusCode)
|
||||||
|
p.copyResponse(rw, res.Body)
|
||||||
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue