rewrite request body

This commit is contained in:
WeidiDeng 2024-09-10 22:11:37 +08:00 committed by GitHub
parent feac4b0bef
commit ebd589ce9c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 5 additions and 2 deletions

View File

@ -403,8 +403,10 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddyht
// websocket over http2, assuming backend doesn't support this, the request will be modifided to http1.1 upgrade
// TODO: once we can reliably detect backend support this, it can be removed for those backends
if r.ProtoMajor == 2 && r.Method == http.MethodConnect && r.Header.Get(":protocol") != "" {
caddyhttp.SetVar(clonedReq.Context(), "h2_websocket", true)
clonedReq.Header.Del(":protocol")
// keep the body for later use. http1.1 upgrade uses http.NoBody
caddyhttp.SetVar(clonedReq.Context(), "h2_websocket_body", clonedReq.Body)
clonedReq.Body = http.NoBody
clonedReq.Method = http.MethodGet
clonedReq.Header.Set("Upgrade", r.Header.Get(":protocol"))
clonedReq.Header.Set("Connection", "Upgrade")

View File

@ -88,7 +88,8 @@ func (h *Handler) handleUpgradeResponse(logger *zap.Logger, wg *sync.WaitGroup,
)
// websocket over http2, assuming backend doesn't support this, the request will be modifided to http1.1 upgrade
// TODO: once we can reliably detect backend support this, it can be removed for those backends
if b, ok := caddyhttp.GetVar(req.Context(), "h2_websocket").(bool); ok && b {
if body, ok := caddyhttp.GetVar(req.Context(), "h2_websocket_body").(io.ReadCloser); ok {
req.Body = body
rw.Header().Del("Upgrade")
rw.Header().Del("Connection")
rw.Header().Del("Sec-Websocket-Accept")