Skip to content

Commit

Permalink
Fix: Ensure MultipartForm is properly restored to the original request
Browse files Browse the repository at this point in the history
  • Loading branch information
fanyiboaa committed Dec 22, 2024
1 parent db9d1d0 commit d4c3ff2
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions mux.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,14 @@ func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request) {
if r.Match(req, &match) {
handler = match.Handler
if handler != nil {
// Note: The defer statement is used to assign the value of req.MultipartForm back to the original request's MultipartForm (originReq) when the function returns.
// This is necessary because the 'req' variable may be modified later in the code, and there is a risk that MultipartForm may not be properly assigned to the original request (originReq).
// Since the 'req' is a shallow copy of the original request, any changes to req.MultipartForm during the request handling might not reflect back to the original request.
// By using 'defer', we ensure that the MultipartForm is correctly restored to the original request when the function exits.
defer func(originReq *http.Request) {
originReq.MultipartForm = req.MultipartForm
}(req)

// Populate context for custom handlers
if r.omitRouteFromContext {
// Only populate the match vars (if any) into the context.
Expand Down

0 comments on commit d4c3ff2

Please sign in to comment.