Skip to content

Call MultipartForm.RemoveAll when request finishes #19606

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions modules/context/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,7 @@ func APIContexter() func(http.Handler) http.Handler {
},
Org: &APIOrganization{},
}
defer ctx.Close()

ctx.Req = WithAPIContext(WithContext(req, ctx.Context), &ctx)

Expand Down
12 changes: 12 additions & 0 deletions modules/context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,16 @@ type Context struct {
Package *Package
}

// Close frees all resources hold by Context
func (ctx *Context) Close() error {
var err error
if ctx.Req != nil && ctx.Req.MultipartForm != nil {
err = ctx.Req.MultipartForm.RemoveAll() // remove the temp files buffered to tmp directory
}
// TODO: close opened repo, and more
return err
}

// TrHTMLEscapeArgs runs Tr but pre-escapes all arguments with html.EscapeString.
// This is useful if the locale message is intended to only produce HTML content.
func (ctx *Context) TrHTMLEscapeArgs(msg string, args ...string) string {
Expand Down Expand Up @@ -693,6 +703,8 @@ func Contexter() func(next http.Handler) http.Handler {
"RunModeIsProd": setting.IsProd,
},
}
defer ctx.Close()

// PageData is passed by reference, and it will be rendered to `window.config.pageData` in `head.tmpl` for JavaScript modules
ctx.PageData = map[string]interface{}{}
ctx.Data["PageData"] = ctx.PageData
Expand Down
1 change: 1 addition & 0 deletions modules/context/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ func PackageContexter() func(next http.Handler) http.Handler {
Resp: NewResponse(resp),
Data: map[string]interface{}{},
}
defer ctx.Close()

ctx.Req = WithContext(req, &ctx)

Expand Down
2 changes: 2 additions & 0 deletions modules/context/private.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ func PrivateContexter() func(http.Handler) http.Handler {
Data: map[string]interface{}{},
},
}
defer ctx.Close()

ctx.Req = WithPrivateContext(req, ctx)
ctx.Data["Context"] = ctx
next.ServeHTTP(ctx.Resp, ctx.Req)
Expand Down
1 change: 1 addition & 0 deletions modules/test/context_tests.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ func MockContext(t *testing.T, path string) *context.Context {
Resp: context.NewResponse(resp),
Locale: &mockLocale{},
}
defer ctx.Close()

requestURL, err := url.Parse(path)
assert.NoError(t, err)
Expand Down
2 changes: 2 additions & 0 deletions routers/api/v1/misc/markdown_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ func createContext(req *http.Request) (*context.Context, *httptest.ResponseRecor
Render: rnd,
Data: make(map[string]interface{}),
}
defer c.Close()

return c, resp
}

Expand Down
2 changes: 2 additions & 0 deletions routers/install/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ func Init(next http.Handler) http.Handler {
"PasswordHashAlgorithms": user_model.AvailableHashAlgorithms,
},
}
defer ctx.Close()

ctx.Req = context.WithContext(req, &ctx)
next.ServeHTTP(resp, ctx.Req)
})
Expand Down