diff options
author | 6543 <6543@obermui.de> | 2021-04-05 17:30:52 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-04-05 11:30:52 -0400 |
commit | 16dea6cebd375fc274fc7a9c216dbcc9e22bd5c7 (patch) | |
tree | 75d47012e9899ca24408aeef7fa3adfcd4beaed1 /modules | |
parent | e9fba18a26c9d0f8586254a83ef7afcd293a725a (diff) | |
download | gitea-16dea6cebd375fc274fc7a9c216dbcc9e22bd5c7.tar.gz gitea-16dea6cebd375fc274fc7a9c216dbcc9e22bd5c7.zip |
[refactor] replace int with httpStatusCodes (#15282)
* replace "200" (int) with "http.StatusOK" (const)
* ctx.Error & ctx.HTML
* ctx.JSON Part1
* ctx.JSON Part2
* ctx.JSON Part3
Diffstat (limited to 'modules')
-rw-r--r-- | modules/context/api.go | 8 | ||||
-rw-r--r-- | modules/context/auth.go | 24 | ||||
-rw-r--r-- | modules/context/context.go | 2 | ||||
-rw-r--r-- | modules/lfs/locks.go | 47 |
4 files changed, 42 insertions, 39 deletions
diff --git a/modules/context/api.go b/modules/context/api.go index 4757c2eeb4..cbd90c50e4 100644 --- a/modules/context/api.go +++ b/modules/context/api.go @@ -203,12 +203,12 @@ func (ctx *APIContext) CheckForOTP() { if models.IsErrTwoFactorNotEnrolled(err) { return // No 2FA enrollment for this user } - ctx.Context.Error(500) + ctx.Context.Error(http.StatusInternalServerError) return } ok, err := twofa.ValidateTOTP(otpHeader) if err != nil { - ctx.Context.Error(500) + ctx.Context.Error(http.StatusInternalServerError) return } if !ok { @@ -288,7 +288,7 @@ func ReferencesGitRepo(allowEmpty bool) func(http.Handler) http.Handler { repoPath := models.RepoPath(ctx.Repo.Owner.Name, ctx.Repo.Repository.Name) gitRepo, err := git.OpenRepository(repoPath) if err != nil { - ctx.Error(500, "RepoRef Invalid repo "+repoPath, err) + ctx.Error(http.StatusInternalServerError, "RepoRef Invalid repo "+repoPath, err) return } ctx.Repo.GitRepo = gitRepo @@ -324,7 +324,7 @@ func (ctx *APIContext) NotFound(objs ...interface{}) { } } - ctx.JSON(404, map[string]interface{}{ + ctx.JSON(http.StatusNotFound, map[string]interface{}{ "message": message, "documentation_url": setting.API.SwaggerURL, "errors": errors, diff --git a/modules/context/auth.go b/modules/context/auth.go index 3b4d7fc595..ed220d5420 100644 --- a/modules/context/auth.go +++ b/modules/context/auth.go @@ -6,6 +6,8 @@ package context import ( + "net/http" + "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/setting" @@ -27,13 +29,13 @@ func Toggle(options *ToggleOptions) func(ctx *Context) { if ctx.IsSigned { if !ctx.User.IsActive && setting.Service.RegisterEmailConfirm { ctx.Data["Title"] = ctx.Tr("auth.active_your_account") - ctx.HTML(200, "user/auth/activate") + ctx.HTML(http.StatusOK, "user/auth/activate") return } if !ctx.User.IsActive || ctx.User.ProhibitLogin { log.Info("Failed authentication attempt for %s from %s", ctx.User.Name, ctx.RemoteAddr()) ctx.Data["Title"] = ctx.Tr("auth.prohibit_login") - ctx.HTML(200, "user/auth/prohibit_login") + ctx.HTML(http.StatusOK, "user/auth/prohibit_login") return } @@ -76,7 +78,7 @@ func Toggle(options *ToggleOptions) func(ctx *Context) { return } else if !ctx.User.IsActive && setting.Service.RegisterEmailConfirm { ctx.Data["Title"] = ctx.Tr("auth.active_your_account") - ctx.HTML(200, "user/auth/activate") + ctx.HTML(http.StatusOK, "user/auth/activate") return } } @@ -93,7 +95,7 @@ func Toggle(options *ToggleOptions) func(ctx *Context) { if options.AdminRequired { if !ctx.User.IsAdmin { - ctx.Error(403) + ctx.Error(http.StatusForbidden) return } ctx.Data["PageIsAdmin"] = true @@ -108,7 +110,7 @@ func ToggleAPI(options *ToggleOptions) func(ctx *APIContext) { if ctx.IsSigned { if !ctx.User.IsActive && setting.Service.RegisterEmailConfirm { ctx.Data["Title"] = ctx.Tr("auth.active_your_account") - ctx.JSON(403, map[string]string{ + ctx.JSON(http.StatusForbidden, map[string]string{ "message": "This account is not activated.", }) return @@ -116,14 +118,14 @@ func ToggleAPI(options *ToggleOptions) func(ctx *APIContext) { if !ctx.User.IsActive || ctx.User.ProhibitLogin { log.Info("Failed authentication attempt for %s from %s", ctx.User.Name, ctx.RemoteAddr()) ctx.Data["Title"] = ctx.Tr("auth.prohibit_login") - ctx.JSON(403, map[string]string{ + ctx.JSON(http.StatusForbidden, map[string]string{ "message": "This account is prohibited from signing in, please contact your site administrator.", }) return } if ctx.User.MustChangePassword { - ctx.JSON(403, map[string]string{ + ctx.JSON(http.StatusForbidden, map[string]string{ "message": "You must change your password. Change it at: " + setting.AppURL + "/user/change_password", }) return @@ -139,13 +141,13 @@ func ToggleAPI(options *ToggleOptions) func(ctx *APIContext) { if options.SignInRequired { if !ctx.IsSigned { // Restrict API calls with error message. - ctx.JSON(403, map[string]string{ + ctx.JSON(http.StatusForbidden, map[string]string{ "message": "Only signed in user is allowed to call APIs.", }) return } else if !ctx.User.IsActive && setting.Service.RegisterEmailConfirm { ctx.Data["Title"] = ctx.Tr("auth.active_your_account") - ctx.HTML(200, "user/auth/activate") + ctx.HTML(http.StatusOK, "user/auth/activate") return } if ctx.IsSigned && ctx.IsBasicAuth { @@ -164,7 +166,7 @@ func ToggleAPI(options *ToggleOptions) func(ctx *APIContext) { return } if !ok { - ctx.JSON(403, map[string]string{ + ctx.JSON(http.StatusForbidden, map[string]string{ "message": "Only signed in user is allowed to call APIs.", }) return @@ -174,7 +176,7 @@ func ToggleAPI(options *ToggleOptions) func(ctx *APIContext) { if options.AdminRequired { if !ctx.User.IsAdmin { - ctx.JSON(403, map[string]string{ + ctx.JSON(http.StatusForbidden, map[string]string{ "message": "You have no permission to request for this.", }) return diff --git a/modules/context/context.go b/modules/context/context.go index a784032606..b876487d5e 100644 --- a/modules/context/context.go +++ b/modules/context/context.go @@ -213,7 +213,7 @@ func (ctx *Context) RenderWithErr(msg string, tpl base.TplName, form interface{} } ctx.Flash.ErrorMsg = msg ctx.Data["Flash"] = ctx.Flash - ctx.HTML(200, tpl) + ctx.HTML(http.StatusOK, tpl) } // NotFound displays a 404 (Not Found) page and prints the given error, if any. diff --git a/modules/lfs/locks.go b/modules/lfs/locks.go index f2688c3260..eaa8305cb4 100644 --- a/modules/lfs/locks.go +++ b/modules/lfs/locks.go @@ -5,6 +5,7 @@ package lfs import ( + "net/http" "strconv" "strings" @@ -21,19 +22,19 @@ import ( func checkIsValidRequest(ctx *context.Context) bool { if !setting.LFS.StartServer { log.Debug("Attempt to access LFS server but LFS server is disabled") - writeStatus(ctx, 404) + writeStatus(ctx, http.StatusNotFound) return false } if !MetaMatcher(ctx.Req) { log.Info("Attempt access LOCKs without accepting the correct media type: %s", metaMediaType) - writeStatus(ctx, 400) + writeStatus(ctx, http.StatusBadRequest) return false } if !ctx.IsSigned { user, _, _, err := parseToken(ctx.Req.Header.Get("Authorization")) if err != nil { ctx.Resp.Header().Set("WWW-Authenticate", "Basic realm=gitea-lfs") - writeStatus(ctx, 401) + writeStatus(ctx, http.StatusUnauthorized) return false } ctx.User = user @@ -44,23 +45,23 @@ func checkIsValidRequest(ctx *context.Context) bool { func handleLockListOut(ctx *context.Context, repo *models.Repository, lock *models.LFSLock, err error) { if err != nil { if models.IsErrLFSLockNotExist(err) { - ctx.JSON(200, api.LFSLockList{ + ctx.JSON(http.StatusOK, api.LFSLockList{ Locks: []*api.LFSLock{}, }) return } - ctx.JSON(500, api.LFSLockError{ + ctx.JSON(http.StatusInternalServerError, api.LFSLockError{ Message: "unable to list locks : Internal Server Error", }) return } if repo.ID != lock.RepoID { - ctx.JSON(200, api.LFSLockList{ + ctx.JSON(http.StatusOK, api.LFSLockList{ Locks: []*api.LFSLock{}, }) return } - ctx.JSON(200, api.LFSLockList{ + ctx.JSON(http.StatusOK, api.LFSLockList{ Locks: []*api.LFSLock{convert.ToLFSLock(lock)}, }) } @@ -86,7 +87,7 @@ func GetListLockHandler(ctx *context.Context) { authenticated := authenticate(ctx, repository, rv.Authorization, false) if !authenticated { ctx.Resp.Header().Set("WWW-Authenticate", "Basic realm=gitea-lfs") - ctx.JSON(401, api.LFSLockError{ + ctx.JSON(http.StatusUnauthorized, api.LFSLockError{ Message: "You must have pull access to list locks", }) return @@ -106,7 +107,7 @@ func GetListLockHandler(ctx *context.Context) { if id != "" { //Case where we request a specific id v, err := strconv.ParseInt(id, 10, 64) if err != nil { - ctx.JSON(400, api.LFSLockError{ + ctx.JSON(http.StatusBadRequest, api.LFSLockError{ Message: "bad request : " + err.Error(), }) return @@ -133,7 +134,7 @@ func GetListLockHandler(ctx *context.Context) { lockList, err := models.GetLFSLockByRepoID(repository.ID, cursor, limit) if err != nil { log.Error("Unable to list locks for repository ID[%d]: Error: %v", repository.ID, err) - ctx.JSON(500, api.LFSLockError{ + ctx.JSON(http.StatusInternalServerError, api.LFSLockError{ Message: "unable to list locks : Internal Server Error", }) return @@ -146,7 +147,7 @@ func GetListLockHandler(ctx *context.Context) { if limit > 0 && len(lockList) == limit { next = strconv.Itoa(cursor + 1) } - ctx.JSON(200, api.LFSLockList{ + ctx.JSON(http.StatusOK, api.LFSLockList{ Locks: lockListAPI, Next: next, }) @@ -175,7 +176,7 @@ func PostLockHandler(ctx *context.Context) { authenticated := authenticate(ctx, repository, authorization, true) if !authenticated { ctx.Resp.Header().Set("WWW-Authenticate", "Basic realm=gitea-lfs") - ctx.JSON(401, api.LFSLockError{ + ctx.JSON(http.StatusUnauthorized, api.LFSLockError{ Message: "You must have push access to create locks", }) return @@ -199,7 +200,7 @@ func PostLockHandler(ctx *context.Context) { }) if err != nil { if models.IsErrLFSLockAlreadyExist(err) { - ctx.JSON(409, api.LFSLockError{ + ctx.JSON(http.StatusConflict, api.LFSLockError{ Lock: convert.ToLFSLock(lock), Message: "already created lock", }) @@ -207,18 +208,18 @@ func PostLockHandler(ctx *context.Context) { } if models.IsErrLFSUnauthorizedAction(err) { ctx.Resp.Header().Set("WWW-Authenticate", "Basic realm=gitea-lfs") - ctx.JSON(401, api.LFSLockError{ + ctx.JSON(http.StatusUnauthorized, api.LFSLockError{ Message: "You must have push access to create locks : " + err.Error(), }) return } log.Error("Unable to CreateLFSLock in repository %-v at %s for user %-v: Error: %v", repository, req.Path, ctx.User, err) - ctx.JSON(500, api.LFSLockError{ + ctx.JSON(http.StatusInternalServerError, api.LFSLockError{ Message: "internal server error : Internal Server Error", }) return } - ctx.JSON(201, api.LFSLockResponse{Lock: convert.ToLFSLock(lock)}) + ctx.JSON(http.StatusCreated, api.LFSLockResponse{Lock: convert.ToLFSLock(lock)}) } // VerifyLockHandler list locks for verification @@ -244,7 +245,7 @@ func VerifyLockHandler(ctx *context.Context) { authenticated := authenticate(ctx, repository, authorization, true) if !authenticated { ctx.Resp.Header().Set("WWW-Authenticate", "Basic realm=gitea-lfs") - ctx.JSON(401, api.LFSLockError{ + ctx.JSON(http.StatusUnauthorized, api.LFSLockError{ Message: "You must have push access to verify locks", }) return @@ -263,7 +264,7 @@ func VerifyLockHandler(ctx *context.Context) { lockList, err := models.GetLFSLockByRepoID(repository.ID, cursor, limit) if err != nil { log.Error("Unable to list locks for repository ID[%d]: Error: %v", repository.ID, err) - ctx.JSON(500, api.LFSLockError{ + ctx.JSON(http.StatusInternalServerError, api.LFSLockError{ Message: "unable to list locks : Internal Server Error", }) return @@ -281,7 +282,7 @@ func VerifyLockHandler(ctx *context.Context) { lockTheirsListAPI = append(lockTheirsListAPI, convert.ToLFSLock(l)) } } - ctx.JSON(200, api.LFSLockListVerify{ + ctx.JSON(http.StatusOK, api.LFSLockListVerify{ Ours: lockOursListAPI, Theirs: lockTheirsListAPI, Next: next, @@ -311,7 +312,7 @@ func UnLockHandler(ctx *context.Context) { authenticated := authenticate(ctx, repository, authorization, true) if !authenticated { ctx.Resp.Header().Set("WWW-Authenticate", "Basic realm=gitea-lfs") - ctx.JSON(401, api.LFSLockError{ + ctx.JSON(http.StatusUnauthorized, api.LFSLockError{ Message: "You must have push access to delete locks", }) return @@ -332,16 +333,16 @@ func UnLockHandler(ctx *context.Context) { if err != nil { if models.IsErrLFSUnauthorizedAction(err) { ctx.Resp.Header().Set("WWW-Authenticate", "Basic realm=gitea-lfs") - ctx.JSON(401, api.LFSLockError{ + ctx.JSON(http.StatusUnauthorized, api.LFSLockError{ Message: "You must have push access to delete locks : " + err.Error(), }) return } log.Error("Unable to DeleteLFSLockByID[%d] by user %-v with force %t: Error: %v", ctx.ParamsInt64("lid"), ctx.User, req.Force, err) - ctx.JSON(500, api.LFSLockError{ + ctx.JSON(http.StatusInternalServerError, api.LFSLockError{ Message: "unable to delete lock : Internal Server Error", }) return } - ctx.JSON(200, api.LFSLockResponse{Lock: convert.ToLFSLock(lock)}) + ctx.JSON(http.StatusOK, api.LFSLockResponse{Lock: convert.ToLFSLock(lock)}) } |