Call MultipartForm.RemoveAll when request finishes (#19606)

This commit is contained in:
wxiaoguang 2022-05-05 22:13:23 +08:00 committed by GitHub
parent 7b089c465d
commit 04fc4b7e05
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 21 additions and 0 deletions

View File

@ -256,6 +256,7 @@ func APIContexter() func(http.Handler) http.Handler {
}, },
Org: &APIOrganization{}, Org: &APIOrganization{},
} }
defer ctx.Close()
ctx.Req = WithAPIContext(WithContext(req, ctx.Context), &ctx) ctx.Req = WithAPIContext(WithContext(req, ctx.Context), &ctx)

View File

@ -75,6 +75,16 @@ type Context struct {
Package *Package 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. // 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. // This is useful if the locale message is intended to only produce HTML content.
func (ctx *Context) TrHTMLEscapeArgs(msg string, args ...string) string { func (ctx *Context) TrHTMLEscapeArgs(msg string, args ...string) string {
@ -693,6 +703,8 @@ func Contexter() func(next http.Handler) http.Handler {
"RunModeIsProd": setting.IsProd, "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 // 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.PageData = map[string]interface{}{}
ctx.Data["PageData"] = ctx.PageData ctx.Data["PageData"] = ctx.PageData

View File

@ -100,6 +100,7 @@ func PackageContexter() func(next http.Handler) http.Handler {
Resp: NewResponse(resp), Resp: NewResponse(resp),
Data: map[string]interface{}{}, Data: map[string]interface{}{},
} }
defer ctx.Close()
ctx.Req = WithContext(req, &ctx) ctx.Req = WithContext(req, &ctx)

View File

@ -66,6 +66,8 @@ func PrivateContexter() func(http.Handler) http.Handler {
Data: map[string]interface{}{}, Data: map[string]interface{}{},
}, },
} }
defer ctx.Close()
ctx.Req = WithPrivateContext(req, ctx) ctx.Req = WithPrivateContext(req, ctx)
ctx.Data["Context"] = ctx ctx.Data["Context"] = ctx
next.ServeHTTP(ctx.Resp, ctx.Req) next.ServeHTTP(ctx.Resp, ctx.Req)

View File

@ -38,6 +38,7 @@ func MockContext(t *testing.T, path string) *context.Context {
Resp: context.NewResponse(resp), Resp: context.NewResponse(resp),
Locale: &mockLocale{}, Locale: &mockLocale{},
} }
defer ctx.Close()
requestURL, err := url.Parse(path) requestURL, err := url.Parse(path)
assert.NoError(t, err) assert.NoError(t, err)

View File

@ -37,6 +37,8 @@ func createContext(req *http.Request) (*context.Context, *httptest.ResponseRecor
Render: rnd, Render: rnd,
Data: make(map[string]interface{}), Data: make(map[string]interface{}),
} }
defer c.Close()
return c, resp return c, resp
} }

View File

@ -79,6 +79,8 @@ func Init(next http.Handler) http.Handler {
"PasswordHashAlgorithms": user_model.AvailableHashAlgorithms, "PasswordHashAlgorithms": user_model.AvailableHashAlgorithms,
}, },
} }
defer ctx.Close()
ctx.Req = context.WithContext(req, &ctx) ctx.Req = context.WithContext(req, &ctx)
next.ServeHTTP(resp, ctx.Req) next.ServeHTTP(resp, ctx.Req)
}) })