Make modules/context.Context a context.Context (#16031)

* Make modules/context.Context a context.Context

Signed-off-by: Andrew Thornton <art27@cantab.net>

* Simplify context calls

Signed-off-by: Andrew Thornton <art27@cantab.net>

* Set the base context for requests to the HammerContext

Signed-off-by: Andrew Thornton <art27@cantab.net>

Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
release/v1.15
zeripath 2021-05-31 07:18:11 +01:00 committed by GitHub
parent 518ed504ef
commit 3183a465d7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
14 changed files with 46 additions and 23 deletions

View File

@ -509,7 +509,7 @@ func (ctx *Context) ParamsInt64(p string) int64 {
// SetParams set params into routes // SetParams set params into routes
func (ctx *Context) SetParams(k, v string) { func (ctx *Context) SetParams(k, v string) {
chiCtx := chi.RouteContext(ctx.Req.Context()) chiCtx := chi.RouteContext(ctx)
chiCtx.URLParams.Add(strings.TrimPrefix(k, ":"), url.PathEscape(v)) chiCtx.URLParams.Add(strings.TrimPrefix(k, ":"), url.PathEscape(v))
} }
@ -528,6 +528,26 @@ func (ctx *Context) Status(status int) {
ctx.Resp.WriteHeader(status) ctx.Resp.WriteHeader(status)
} }
// Deadline is part of the interface for context.Context and we pass this to the request context
func (ctx *Context) Deadline() (deadline time.Time, ok bool) {
return ctx.Req.Context().Deadline()
}
// Done is part of the interface for context.Context and we pass this to the request context
func (ctx *Context) Done() <-chan struct{} {
return ctx.Req.Context().Done()
}
// Err is part of the interface for context.Context and we pass this to the request context
func (ctx *Context) Err() error {
return ctx.Req.Context().Err()
}
// Value is part of the interface for context.Context and we pass this to the request context
func (ctx *Context) Value(key interface{}) interface{} {
return ctx.Req.Context().Value(key)
}
// Handler represents a custom handler // Handler represents a custom handler
type Handler func(*Context) type Handler func(*Context)

View File

@ -5,7 +5,9 @@
package graceful package graceful
import ( import (
"context"
"crypto/tls" "crypto/tls"
"net"
"net/http" "net/http"
) )
@ -16,6 +18,7 @@ func newHTTPServer(network, address, name string, handler http.Handler) (*Server
WriteTimeout: DefaultWriteTimeOut, WriteTimeout: DefaultWriteTimeOut,
MaxHeaderBytes: DefaultMaxHeaderBytes, MaxHeaderBytes: DefaultMaxHeaderBytes,
Handler: handler, Handler: handler,
BaseContext: func(net.Listener) context.Context { return GetManager().HammerContext() },
} }
server.OnShutdown = func() { server.OnShutdown = func() {
httpServer.SetKeepAlivesEnabled(false) httpServer.SetKeepAlivesEnabled(false)

View File

@ -113,7 +113,7 @@ func NewUserPost(ctx *context.Context) {
ctx.RenderWithErr(password.BuildComplexityError(ctx), tplUserNew, &form) ctx.RenderWithErr(password.BuildComplexityError(ctx), tplUserNew, &form)
return return
} }
pwned, err := password.IsPwned(ctx.Req.Context(), form.Password) pwned, err := password.IsPwned(ctx, form.Password)
if pwned { if pwned {
ctx.Data["Err_Password"] = true ctx.Data["Err_Password"] = true
errMsg := ctx.Tr("auth.password_pwned") errMsg := ctx.Tr("auth.password_pwned")
@ -256,7 +256,7 @@ func EditUserPost(ctx *context.Context) {
ctx.RenderWithErr(password.BuildComplexityError(ctx), tplUserEdit, &form) ctx.RenderWithErr(password.BuildComplexityError(ctx), tplUserEdit, &form)
return return
} }
pwned, err := password.IsPwned(ctx.Req.Context(), form.Password) pwned, err := password.IsPwned(ctx, form.Password)
if pwned { if pwned {
ctx.Data["Err_Password"] = true ctx.Data["Err_Password"] = true
errMsg := ctx.Tr("auth.password_pwned") errMsg := ctx.Tr("auth.password_pwned")

View File

@ -88,7 +88,7 @@ func CreateUser(ctx *context.APIContext) {
ctx.Error(http.StatusBadRequest, "PasswordComplexity", err) ctx.Error(http.StatusBadRequest, "PasswordComplexity", err)
return return
} }
pwned, err := password.IsPwned(ctx.Req.Context(), form.Password) pwned, err := password.IsPwned(ctx, form.Password)
if pwned { if pwned {
if err != nil { if err != nil {
log.Error(err.Error()) log.Error(err.Error())
@ -162,7 +162,7 @@ func EditUser(ctx *context.APIContext) {
ctx.Error(http.StatusBadRequest, "PasswordComplexity", err) ctx.Error(http.StatusBadRequest, "PasswordComplexity", err)
return return
} }
pwned, err := password.IsPwned(ctx.Req.Context(), form.Password) pwned, err := password.IsPwned(ctx, form.Password)
if pwned { if pwned {
if err != nil { if err != nil {
log.Error(err.Error()) log.Error(err.Error())

View File

@ -42,7 +42,7 @@ func Events(ctx *context.Context) {
} }
// Listen to connection close and un-register messageChan // Listen to connection close and un-register messageChan
notify := ctx.Req.Context().Done() notify := ctx.Done()
ctx.Resp.Flush() ctx.Resp.Flush()
shutdownCtx := graceful.GetManager().ShutdownContext() shutdownCtx := graceful.GetManager().ShutdownContext()

View File

@ -400,7 +400,7 @@ func InstallPost(ctx *context.Context) {
} }
// Re-read settings // Re-read settings
PostInstallInit(ctx.Req.Context()) PostInstallInit(ctx)
// Create admin account // Create admin account
if len(form.AdminName) > 0 { if len(form.AdminName) > 0 {
@ -454,7 +454,7 @@ func InstallPost(ctx *context.Context) {
// Now get the http.Server from this request and shut it down // Now get the http.Server from this request and shut it down
// NB: This is not our hammerable graceful shutdown this is http.Server.Shutdown // NB: This is not our hammerable graceful shutdown this is http.Server.Shutdown
srv := ctx.Req.Context().Value(http.ServerContextKey).(*http.Server) srv := ctx.Value(http.ServerContextKey).(*http.Server)
go func() { go func() {
if err := srv.Shutdown(graceful.GetManager().HammerContext()); err != nil { if err := srv.Shutdown(graceful.GetManager().HammerContext()); err != nil {
log.Error("Unable to shutdown the install server! Error: %v", err) log.Error("Unable to shutdown the install server! Error: %v", err)

View File

@ -35,7 +35,7 @@ func FlushQueues(ctx *context.PrivateContext) {
}) })
return return
} }
err := queue.GetManager().FlushAll(ctx.Req.Context(), opts.Timeout) err := queue.GetManager().FlushAll(ctx, opts.Timeout)
if err != nil { if err != nil {
ctx.JSON(http.StatusRequestTimeout, map[string]interface{}{ ctx.JSON(http.StatusRequestTimeout, map[string]interface{}{
"err": fmt.Sprintf("%v", err), "err": fmt.Sprintf("%v", err),

View File

@ -36,7 +36,7 @@ func RestoreRepo(ctx *myCtx.PrivateContext) {
} }
if err := migrations.RestoreRepository( if err := migrations.RestoreRepository(
ctx.Req.Context(), ctx,
params.RepoDir, params.RepoDir,
params.OwnerName, params.OwnerName,
params.RepoName, params.RepoName,

View File

@ -124,7 +124,7 @@ func RefBlame(ctx *context.Context) {
return return
} }
blameReader, err := git.CreateBlameReader(ctx.Req.Context(), models.RepoPath(userName, repoName), commitID, fileName) blameReader, err := git.CreateBlameReader(ctx, models.RepoPath(userName, repoName), commitID, fileName)
if err != nil { if err != nil {
ctx.NotFound("CreateBlameReader", err) ctx.NotFound("CreateBlameReader", err)
return return

View File

@ -414,7 +414,7 @@ func LFSPointerFiles(ctx *context.Context) {
err = func() error { err = func() error {
pointerChan := make(chan lfs.PointerBlob) pointerChan := make(chan lfs.PointerBlob)
errChan := make(chan error, 1) errChan := make(chan error, 1)
go lfs.SearchPointerBlobs(ctx.Req.Context(), ctx.Repo.GitRepo, pointerChan, errChan) go lfs.SearchPointerBlobs(ctx, ctx.Repo.GitRepo, pointerChan, errChan)
numPointers := 0 numPointers := 0
var numAssociated, numNoExist, numAssociatable int var numAssociated, numNoExist, numAssociatable int

View File

@ -1011,9 +1011,9 @@ func LinkAccountPostRegister(ctx *context.Context) {
case setting.ImageCaptcha: case setting.ImageCaptcha:
valid = context.GetImageCaptcha().VerifyReq(ctx.Req) valid = context.GetImageCaptcha().VerifyReq(ctx.Req)
case setting.ReCaptcha: case setting.ReCaptcha:
valid, err = recaptcha.Verify(ctx.Req.Context(), form.GRecaptchaResponse) valid, err = recaptcha.Verify(ctx, form.GRecaptchaResponse)
case setting.HCaptcha: case setting.HCaptcha:
valid, err = hcaptcha.Verify(ctx.Req.Context(), form.HcaptchaResponse) valid, err = hcaptcha.Verify(ctx, form.HcaptchaResponse)
default: default:
ctx.ServerError("Unknown Captcha Type", fmt.Errorf("Unknown Captcha Type: %s", setting.Service.CaptchaType)) ctx.ServerError("Unknown Captcha Type", fmt.Errorf("Unknown Captcha Type: %s", setting.Service.CaptchaType))
return return
@ -1153,9 +1153,9 @@ func SignUpPost(ctx *context.Context) {
case setting.ImageCaptcha: case setting.ImageCaptcha:
valid = context.GetImageCaptcha().VerifyReq(ctx.Req) valid = context.GetImageCaptcha().VerifyReq(ctx.Req)
case setting.ReCaptcha: case setting.ReCaptcha:
valid, err = recaptcha.Verify(ctx.Req.Context(), form.GRecaptchaResponse) valid, err = recaptcha.Verify(ctx, form.GRecaptchaResponse)
case setting.HCaptcha: case setting.HCaptcha:
valid, err = hcaptcha.Verify(ctx.Req.Context(), form.HcaptchaResponse) valid, err = hcaptcha.Verify(ctx, form.HcaptchaResponse)
default: default:
ctx.ServerError("Unknown Captcha Type", fmt.Errorf("Unknown Captcha Type: %s", setting.Service.CaptchaType)) ctx.ServerError("Unknown Captcha Type", fmt.Errorf("Unknown Captcha Type: %s", setting.Service.CaptchaType))
return return
@ -1191,7 +1191,7 @@ func SignUpPost(ctx *context.Context) {
ctx.RenderWithErr(password.BuildComplexityError(ctx), tplSignUp, &form) ctx.RenderWithErr(password.BuildComplexityError(ctx), tplSignUp, &form)
return return
} }
pwned, err := password.IsPwned(ctx.Req.Context(), form.Password) pwned, err := password.IsPwned(ctx, form.Password)
if pwned { if pwned {
errMsg := ctx.Tr("auth.password_pwned") errMsg := ctx.Tr("auth.password_pwned")
if err != nil { if err != nil {
@ -1620,7 +1620,7 @@ func ResetPasswdPost(ctx *context.Context) {
ctx.Data["Err_Password"] = true ctx.Data["Err_Password"] = true
ctx.RenderWithErr(password.BuildComplexityError(ctx), tplResetPassword, nil) ctx.RenderWithErr(password.BuildComplexityError(ctx), tplResetPassword, nil)
return return
} else if pwned, err := password.IsPwned(ctx.Req.Context(), passwd); pwned || err != nil { } else if pwned, err := password.IsPwned(ctx, passwd); pwned || err != nil {
errMsg := ctx.Tr("auth.password_pwned") errMsg := ctx.Tr("auth.password_pwned")
if err != nil { if err != nil {
log.Error(err.Error()) log.Error(err.Error())

View File

@ -385,13 +385,13 @@ func RegisterOpenIDPost(ctx *context.Context) {
ctx.ServerError("", err) ctx.ServerError("", err)
return return
} }
valid, err = recaptcha.Verify(ctx.Req.Context(), form.GRecaptchaResponse) valid, err = recaptcha.Verify(ctx, form.GRecaptchaResponse)
case setting.HCaptcha: case setting.HCaptcha:
if err := ctx.Req.ParseForm(); err != nil { if err := ctx.Req.ParseForm(); err != nil {
ctx.ServerError("", err) ctx.ServerError("", err)
return return
} }
valid, err = hcaptcha.Verify(ctx.Req.Context(), form.HcaptchaResponse) valid, err = hcaptcha.Verify(ctx, form.HcaptchaResponse)
default: default:
ctx.ServerError("Unknown Captcha Type", fmt.Errorf("Unknown Captcha Type: %s", setting.Service.CaptchaType)) ctx.ServerError("Unknown Captcha Type", fmt.Errorf("Unknown Captcha Type: %s", setting.Service.CaptchaType))
return return

View File

@ -58,7 +58,7 @@ func AccountPost(ctx *context.Context) {
ctx.Flash.Error(ctx.Tr("form.password_not_match")) ctx.Flash.Error(ctx.Tr("form.password_not_match"))
} else if !password.IsComplexEnough(form.Password) { } else if !password.IsComplexEnough(form.Password) {
ctx.Flash.Error(password.BuildComplexityError(ctx)) ctx.Flash.Error(password.BuildComplexityError(ctx))
} else if pwned, err := password.IsPwned(ctx.Req.Context(), form.Password); pwned || err != nil { } else if pwned, err := password.IsPwned(ctx, form.Password); pwned || err != nil {
errMsg := ctx.Tr("auth.password_pwned") errMsg := ctx.Tr("auth.password_pwned")
if err != nil { if err != nil {
log.Error(err.Error()) log.Error(err.Error())

View File

@ -76,7 +76,7 @@ func (aReq *ArchiveRequest) IsComplete() bool {
func (aReq *ArchiveRequest) WaitForCompletion(ctx *context.Context) bool { func (aReq *ArchiveRequest) WaitForCompletion(ctx *context.Context) bool {
select { select {
case <-aReq.cchan: case <-aReq.cchan:
case <-ctx.Req.Context().Done(): case <-ctx.Done():
} }
return aReq.IsComplete() return aReq.IsComplete()
@ -92,7 +92,7 @@ func (aReq *ArchiveRequest) TimedWaitForCompletion(ctx *context.Context, dur tim
case <-time.After(dur): case <-time.After(dur):
timeout = true timeout = true
case <-aReq.cchan: case <-aReq.cchan:
case <-ctx.Req.Context().Done(): case <-ctx.Done():
} }
return aReq.IsComplete(), timeout return aReq.IsComplete(), timeout