tunnel: fix http switching protocols

This commit is contained in:
ginuerzh
2024-10-15 19:36:20 +08:00
parent 51ca9f620a
commit ea179a0ee6
+10 -6
View File
@@ -275,6 +275,10 @@ func (ep *entrypoint) httpRoundTrip(ctx context.Context, rw io.ReadWriter, req *
log.Trace(string(dump))
}
if resp.StatusCode == http.StatusSwitchingProtocols {
return ep.handleUpgradeResponse(rw, req, resp)
}
var respBody *xhttp.Body
if opts := ep.recorder.Options; opts != nil && opts.HTTPBody {
maxSize := opts.MaxBodySize
@@ -286,8 +290,7 @@ func (ep *entrypoint) httpRoundTrip(ctx context.Context, rw io.ReadWriter, req *
}
if err = resp.Write(rw); err != nil {
log.Errorf("write response: %v", err)
return
return fmt.Errorf("write response: %v", err)
}
if respBody != nil {
@@ -295,10 +298,6 @@ func (ep *entrypoint) httpRoundTrip(ctx context.Context, rw io.ReadWriter, req *
ro.HTTP.Response.ContentLength = respBody.Length()
}
if resp.StatusCode == http.StatusSwitchingProtocols {
return ep.handleUpgradeResponse(rw, req, resp)
}
return
}
@@ -321,6 +320,11 @@ func (ep *entrypoint) handleUpgradeResponse(rw io.ReadWriter, req *http.Request,
return fmt.Errorf("internal error: 101 switching protocols response with non-writable body")
}
res.Body = nil
if err := res.Write(rw); err != nil {
return fmt.Errorf("response write: %v", err)
}
return xnet.Transport(rw, backConn)
}