]> source.dussan.org Git - gitea.git/commitdiff
Make modules/context.Context a context.Context (#16031)
authorzeripath <art27@cantab.net>
Mon, 31 May 2021 06:18:11 +0000 (07:18 +0100)
committerGitHub <noreply@github.com>
Mon, 31 May 2021 06:18:11 +0000 (02:18 -0400)
* 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>
14 files changed:
modules/context/context.go
modules/graceful/server_http.go
routers/admin/users.go
routers/api/v1/admin/user.go
routers/events/events.go
routers/install.go
routers/private/manager.go
routers/private/restore_repo.go
routers/repo/blame.go
routers/repo/lfs.go
routers/user/auth.go
routers/user/auth_openid.go
routers/user/setting/account.go
services/archiver/archiver.go

index d812d7b58cddc0864c6cf597e67525dd10bc86e1..d45e9ff87cd89bff0a42d0f837dc8807c571b3f6 100644 (file)
@@ -509,7 +509,7 @@ func (ctx *Context) ParamsInt64(p string) int64 {
 
 // SetParams set params into routes
 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))
 }
 
@@ -528,6 +528,26 @@ func (ctx *Context) Status(status int) {
        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
 type Handler func(*Context)
 
index b101a10d9197d9850e7573c83ed076451c4abfe3..4471e379ef445bf09f54d2696b702d33173d0754 100644 (file)
@@ -5,7 +5,9 @@
 package graceful
 
 import (
+       "context"
        "crypto/tls"
+       "net"
        "net/http"
 )
 
@@ -16,6 +18,7 @@ func newHTTPServer(network, address, name string, handler http.Handler) (*Server
                WriteTimeout:   DefaultWriteTimeOut,
                MaxHeaderBytes: DefaultMaxHeaderBytes,
                Handler:        handler,
+               BaseContext:    func(net.Listener) context.Context { return GetManager().HammerContext() },
        }
        server.OnShutdown = func() {
                httpServer.SetKeepAlivesEnabled(false)
index 3b29eeefc1afd6b9c142142b56c989c865717bb8..a71a11dd8a225baa774a02af3428c90cf3fadc5f 100644 (file)
@@ -113,7 +113,7 @@ func NewUserPost(ctx *context.Context) {
                        ctx.RenderWithErr(password.BuildComplexityError(ctx), tplUserNew, &form)
                        return
                }
-               pwned, err := password.IsPwned(ctx.Req.Context(), form.Password)
+               pwned, err := password.IsPwned(ctx, form.Password)
                if pwned {
                        ctx.Data["Err_Password"] = true
                        errMsg := ctx.Tr("auth.password_pwned")
@@ -256,7 +256,7 @@ func EditUserPost(ctx *context.Context) {
                        ctx.RenderWithErr(password.BuildComplexityError(ctx), tplUserEdit, &form)
                        return
                }
-               pwned, err := password.IsPwned(ctx.Req.Context(), form.Password)
+               pwned, err := password.IsPwned(ctx, form.Password)
                if pwned {
                        ctx.Data["Err_Password"] = true
                        errMsg := ctx.Tr("auth.password_pwned")
index 2d4a3815f4ca4244898a61688a6d051eacb23098..4bbe7f77ba2ea3fa4557402d5b74ad886543c14a 100644 (file)
@@ -88,7 +88,7 @@ func CreateUser(ctx *context.APIContext) {
                ctx.Error(http.StatusBadRequest, "PasswordComplexity", err)
                return
        }
-       pwned, err := password.IsPwned(ctx.Req.Context(), form.Password)
+       pwned, err := password.IsPwned(ctx, form.Password)
        if pwned {
                if err != nil {
                        log.Error(err.Error())
@@ -162,7 +162,7 @@ func EditUser(ctx *context.APIContext) {
                        ctx.Error(http.StatusBadRequest, "PasswordComplexity", err)
                        return
                }
-               pwned, err := password.IsPwned(ctx.Req.Context(), form.Password)
+               pwned, err := password.IsPwned(ctx, form.Password)
                if pwned {
                        if err != nil {
                                log.Error(err.Error())
index 2c1034038fbb2e0526547f0fa6f74ada3269cc0d..b140bf660caceb75a30cfa0e3591fb36a95a6f7f 100644 (file)
@@ -42,7 +42,7 @@ func Events(ctx *context.Context) {
        }
 
        // Listen to connection close and un-register messageChan
-       notify := ctx.Req.Context().Done()
+       notify := ctx.Done()
        ctx.Resp.Flush()
 
        shutdownCtx := graceful.GetManager().ShutdownContext()
index ef53422c4e128c65f1b3dd475c1c13adc0f846ca..30340e99cd6362a3c4be589cc2ab1128aa223695 100644 (file)
@@ -400,7 +400,7 @@ func InstallPost(ctx *context.Context) {
        }
 
        // Re-read settings
-       PostInstallInit(ctx.Req.Context())
+       PostInstallInit(ctx)
 
        // Create admin account
        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
        // 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() {
                if err := srv.Shutdown(graceful.GetManager().HammerContext()); err != nil {
                        log.Error("Unable to shutdown the install server! Error: %v", err)
index 192c4947e76a1f5654be2172682fb3964ea393cc..1ccb1843630ce5f74ca3c5a3316bdd6181403dd8 100644 (file)
@@ -35,7 +35,7 @@ func FlushQueues(ctx *context.PrivateContext) {
                })
                return
        }
-       err := queue.GetManager().FlushAll(ctx.Req.Context(), opts.Timeout)
+       err := queue.GetManager().FlushAll(ctx, opts.Timeout)
        if err != nil {
                ctx.JSON(http.StatusRequestTimeout, map[string]interface{}{
                        "err": fmt.Sprintf("%v", err),
index c002de874a96e013f3d0ca90294fc7a1645331f3..df787e1b330895e7f33000062c273f19172d7bff 100644 (file)
@@ -36,7 +36,7 @@ func RestoreRepo(ctx *myCtx.PrivateContext) {
        }
 
        if err := migrations.RestoreRepository(
-               ctx.Req.Context(),
+               ctx,
                params.RepoDir,
                params.OwnerName,
                params.RepoName,
index f5b228bdfe136bf8e4c036e3f638128cdc7956a9..1a3e1dcb9c5703433369a6208c43cc34967a01e5 100644 (file)
@@ -124,7 +124,7 @@ func RefBlame(ctx *context.Context) {
                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 {
                ctx.NotFound("CreateBlameReader", err)
                return
index 3a7ce2e23bd51a04fe909e79ebfd722de2731744..c17bd2f87a2d8a99ea44ce50a7910459eb26adcb 100644 (file)
@@ -414,7 +414,7 @@ func LFSPointerFiles(ctx *context.Context) {
        err = func() error {
                pointerChan := make(chan lfs.PointerBlob)
                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
                var numAssociated, numNoExist, numAssociatable int
index 5f8b1a6b99a7bc92da83647a9f2d6cf652f9fb29..827b7cdef0651d5931667f502e07676b0997bb88 100644 (file)
@@ -1011,9 +1011,9 @@ func LinkAccountPostRegister(ctx *context.Context) {
                case setting.ImageCaptcha:
                        valid = context.GetImageCaptcha().VerifyReq(ctx.Req)
                case setting.ReCaptcha:
-                       valid, err = recaptcha.Verify(ctx.Req.Context(), form.GRecaptchaResponse)
+                       valid, err = recaptcha.Verify(ctx, form.GRecaptchaResponse)
                case setting.HCaptcha:
-                       valid, err = hcaptcha.Verify(ctx.Req.Context(), form.HcaptchaResponse)
+                       valid, err = hcaptcha.Verify(ctx, form.HcaptchaResponse)
                default:
                        ctx.ServerError("Unknown Captcha Type", fmt.Errorf("Unknown Captcha Type: %s", setting.Service.CaptchaType))
                        return
@@ -1153,9 +1153,9 @@ func SignUpPost(ctx *context.Context) {
                case setting.ImageCaptcha:
                        valid = context.GetImageCaptcha().VerifyReq(ctx.Req)
                case setting.ReCaptcha:
-                       valid, err = recaptcha.Verify(ctx.Req.Context(), form.GRecaptchaResponse)
+                       valid, err = recaptcha.Verify(ctx, form.GRecaptchaResponse)
                case setting.HCaptcha:
-                       valid, err = hcaptcha.Verify(ctx.Req.Context(), form.HcaptchaResponse)
+                       valid, err = hcaptcha.Verify(ctx, form.HcaptchaResponse)
                default:
                        ctx.ServerError("Unknown Captcha Type", fmt.Errorf("Unknown Captcha Type: %s", setting.Service.CaptchaType))
                        return
@@ -1191,7 +1191,7 @@ func SignUpPost(ctx *context.Context) {
                ctx.RenderWithErr(password.BuildComplexityError(ctx), tplSignUp, &form)
                return
        }
-       pwned, err := password.IsPwned(ctx.Req.Context(), form.Password)
+       pwned, err := password.IsPwned(ctx, form.Password)
        if pwned {
                errMsg := ctx.Tr("auth.password_pwned")
                if err != nil {
@@ -1620,7 +1620,7 @@ func ResetPasswdPost(ctx *context.Context) {
                ctx.Data["Err_Password"] = true
                ctx.RenderWithErr(password.BuildComplexityError(ctx), tplResetPassword, nil)
                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")
                if err != nil {
                        log.Error(err.Error())
index b1dfc6ada05db141f7c25134259ddff6e2b417c0..1a73a08c4862d0eb20f822ebb3acf110980f5fe6 100644 (file)
@@ -385,13 +385,13 @@ func RegisterOpenIDPost(ctx *context.Context) {
                                ctx.ServerError("", err)
                                return
                        }
-                       valid, err = recaptcha.Verify(ctx.Req.Context(), form.GRecaptchaResponse)
+                       valid, err = recaptcha.Verify(ctx, form.GRecaptchaResponse)
                case setting.HCaptcha:
                        if err := ctx.Req.ParseForm(); err != nil {
                                ctx.ServerError("", err)
                                return
                        }
-                       valid, err = hcaptcha.Verify(ctx.Req.Context(), form.HcaptchaResponse)
+                       valid, err = hcaptcha.Verify(ctx, form.HcaptchaResponse)
                default:
                        ctx.ServerError("Unknown Captcha Type", fmt.Errorf("Unknown Captcha Type: %s", setting.Service.CaptchaType))
                        return
index e12d63ee029ee4a66d1b882e5f2f729bba6dd740..48ab37d9369e660f651a5a6e34c1d1e68946b0b0 100644 (file)
@@ -58,7 +58,7 @@ func AccountPost(ctx *context.Context) {
                ctx.Flash.Error(ctx.Tr("form.password_not_match"))
        } else if !password.IsComplexEnough(form.Password) {
                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")
                if err != nil {
                        log.Error(err.Error())
index 359fc8b627d9164e135544fc7870feaff26ae2fe..dfa6334d9536c135fe5447b5dfd660be8182a41e 100644 (file)
@@ -76,7 +76,7 @@ func (aReq *ArchiveRequest) IsComplete() bool {
 func (aReq *ArchiveRequest) WaitForCompletion(ctx *context.Context) bool {
        select {
        case <-aReq.cchan:
-       case <-ctx.Req.Context().Done():
+       case <-ctx.Done():
        }
 
        return aReq.IsComplete()
@@ -92,7 +92,7 @@ func (aReq *ArchiveRequest) TimedWaitForCompletion(ctx *context.Context, dur tim
        case <-time.After(dur):
                timeout = true
        case <-aReq.cchan:
-       case <-ctx.Req.Context().Done():
+       case <-ctx.Done():
        }
 
        return aReq.IsComplete(), timeout