mirror of
https://github.com/go-gitea/gitea
synced 2025-07-22 18:28:37 +00:00
Refactor HTTP request context (#17979)
This commit is contained in:
@@ -65,7 +65,7 @@ func goGet(ctx *context.Context) {
|
||||
if appURL.Scheme == string(setting.HTTP) {
|
||||
insecure = "--insecure "
|
||||
}
|
||||
ctx.Header().Set("Content-Type", "text/html")
|
||||
ctx.RespHeader().Set("Content-Type", "text/html")
|
||||
ctx.Status(http.StatusOK)
|
||||
_, _ = ctx.Write([]byte(com.Expand(`<!doctype html>
|
||||
<html>
|
||||
|
@@ -379,7 +379,7 @@ func CreateBranch(ctx *context.Context) {
|
||||
if len(e.Message) == 0 {
|
||||
ctx.Flash.Error(ctx.Tr("repo.editor.push_rejected_no_message"))
|
||||
} else {
|
||||
flashError, err := ctx.HTMLString(string(tplAlertDetails), map[string]interface{}{
|
||||
flashError, err := ctx.RenderToString(tplAlertDetails, map[string]interface{}{
|
||||
"Message": ctx.Tr("repo.editor.push_rejected"),
|
||||
"Summary": ctx.Tr("repo.editor.push_rejected_summary"),
|
||||
"Details": utils.SanitizeFlashErrorString(e.Message),
|
||||
|
@@ -307,7 +307,7 @@ func editFilePost(ctx *context.Context, form forms.EditRepoFileForm, isNewFile b
|
||||
if len(errPushRej.Message) == 0 {
|
||||
ctx.RenderWithErr(ctx.Tr("repo.editor.push_rejected_no_message"), tplEditFile, &form)
|
||||
} else {
|
||||
flashError, err := ctx.HTMLString(string(tplAlertDetails), map[string]interface{}{
|
||||
flashError, err := ctx.RenderToString(tplAlertDetails, map[string]interface{}{
|
||||
"Message": ctx.Tr("repo.editor.push_rejected"),
|
||||
"Summary": ctx.Tr("repo.editor.push_rejected_summary"),
|
||||
"Details": utils.SanitizeFlashErrorString(errPushRej.Message),
|
||||
@@ -319,7 +319,7 @@ func editFilePost(ctx *context.Context, form forms.EditRepoFileForm, isNewFile b
|
||||
ctx.RenderWithErr(flashError, tplEditFile, &form)
|
||||
}
|
||||
} else {
|
||||
flashError, err := ctx.HTMLString(string(tplAlertDetails), map[string]interface{}{
|
||||
flashError, err := ctx.RenderToString(tplAlertDetails, map[string]interface{}{
|
||||
"Message": ctx.Tr("repo.editor.fail_to_update_file", form.TreePath),
|
||||
"Summary": ctx.Tr("repo.editor.fail_to_update_file_summary"),
|
||||
"Details": utils.SanitizeFlashErrorString(err.Error()),
|
||||
@@ -376,7 +376,7 @@ func DiffPreviewPost(ctx *context.Context) {
|
||||
}
|
||||
|
||||
if diff.NumFiles == 0 {
|
||||
ctx.PlainText(200, []byte(ctx.Tr("repo.editor.no_changes_to_show")))
|
||||
ctx.PlainText(http.StatusOK, ctx.Tr("repo.editor.no_changes_to_show"))
|
||||
return
|
||||
}
|
||||
ctx.Data["File"] = diff.Files[0]
|
||||
@@ -501,7 +501,7 @@ func DeleteFilePost(ctx *context.Context) {
|
||||
if len(errPushRej.Message) == 0 {
|
||||
ctx.RenderWithErr(ctx.Tr("repo.editor.push_rejected_no_message"), tplDeleteFile, &form)
|
||||
} else {
|
||||
flashError, err := ctx.HTMLString(string(tplAlertDetails), map[string]interface{}{
|
||||
flashError, err := ctx.RenderToString(tplAlertDetails, map[string]interface{}{
|
||||
"Message": ctx.Tr("repo.editor.push_rejected"),
|
||||
"Summary": ctx.Tr("repo.editor.push_rejected_summary"),
|
||||
"Details": utils.SanitizeFlashErrorString(errPushRej.Message),
|
||||
@@ -704,7 +704,7 @@ func UploadFilePost(ctx *context.Context) {
|
||||
if len(errPushRej.Message) == 0 {
|
||||
ctx.RenderWithErr(ctx.Tr("repo.editor.push_rejected_no_message"), tplUploadFile, &form)
|
||||
} else {
|
||||
flashError, err := ctx.HTMLString(string(tplAlertDetails), map[string]interface{}{
|
||||
flashError, err := ctx.RenderToString(tplAlertDetails, map[string]interface{}{
|
||||
"Message": ctx.Tr("repo.editor.push_rejected"),
|
||||
"Summary": ctx.Tr("repo.editor.push_rejected_summary"),
|
||||
"Details": utils.SanitizeFlashErrorString(errPushRej.Message),
|
||||
|
@@ -126,7 +126,7 @@ func httpBase(ctx *context.Context) (h *serviceHandler) {
|
||||
return
|
||||
}
|
||||
if !owner.IsOrganization() && !owner.IsActive {
|
||||
ctx.HandleText(http.StatusForbidden, "Repository cannot be accessed. You cannot push or open issues/pull-requests.")
|
||||
ctx.PlainText(http.StatusForbidden, "Repository cannot be accessed. You cannot push or open issues/pull-requests.")
|
||||
return
|
||||
}
|
||||
|
||||
@@ -147,7 +147,7 @@ func httpBase(ctx *context.Context) (h *serviceHandler) {
|
||||
|
||||
// Don't allow pushing if the repo is archived
|
||||
if repoExist && repo.IsArchived && !isPull {
|
||||
ctx.HandleText(http.StatusForbidden, "This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.")
|
||||
ctx.PlainText(http.StatusForbidden, "This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.")
|
||||
return
|
||||
}
|
||||
|
||||
@@ -182,7 +182,7 @@ func httpBase(ctx *context.Context) (h *serviceHandler) {
|
||||
_, err = login.GetTwoFactorByUID(ctx.User.ID)
|
||||
if err == nil {
|
||||
// TODO: This response should be changed to "invalid credentials" for security reasons once the expectation behind it (creating an app token to authenticate) is properly documented
|
||||
ctx.HandleText(http.StatusUnauthorized, "Users with two-factor authentication enabled cannot perform HTTP/HTTPS operations via plain username and password. Please create and use a personal access token on the user settings page")
|
||||
ctx.PlainText(http.StatusUnauthorized, "Users with two-factor authentication enabled cannot perform HTTP/HTTPS operations via plain username and password. Please create and use a personal access token on the user settings page")
|
||||
return
|
||||
} else if !login.IsErrTwoFactorNotEnrolled(err) {
|
||||
ctx.ServerError("IsErrTwoFactorNotEnrolled", err)
|
||||
@@ -191,7 +191,7 @@ func httpBase(ctx *context.Context) (h *serviceHandler) {
|
||||
}
|
||||
|
||||
if !ctx.User.IsActive || ctx.User.ProhibitLogin {
|
||||
ctx.HandleText(http.StatusForbidden, "Your account is disabled.")
|
||||
ctx.PlainText(http.StatusForbidden, "Your account is disabled.")
|
||||
return
|
||||
}
|
||||
|
||||
@@ -208,12 +208,12 @@ func httpBase(ctx *context.Context) (h *serviceHandler) {
|
||||
}
|
||||
|
||||
if !p.CanAccess(accessMode, unitType) {
|
||||
ctx.HandleText(http.StatusForbidden, "User permission denied")
|
||||
ctx.PlainText(http.StatusForbidden, "User permission denied")
|
||||
return
|
||||
}
|
||||
|
||||
if !isPull && repo.IsMirror {
|
||||
ctx.HandleText(http.StatusForbidden, "mirror repository is read-only")
|
||||
ctx.PlainText(http.StatusForbidden, "mirror repository is read-only")
|
||||
return
|
||||
}
|
||||
}
|
||||
@@ -240,21 +240,21 @@ func httpBase(ctx *context.Context) (h *serviceHandler) {
|
||||
|
||||
if !repoExist {
|
||||
if !receivePack {
|
||||
ctx.HandleText(http.StatusNotFound, "Repository not found")
|
||||
ctx.PlainText(http.StatusNotFound, "Repository not found")
|
||||
return
|
||||
}
|
||||
|
||||
if isWiki { // you cannot send wiki operation before create the repository
|
||||
ctx.HandleText(http.StatusNotFound, "Repository not found")
|
||||
ctx.PlainText(http.StatusNotFound, "Repository not found")
|
||||
return
|
||||
}
|
||||
|
||||
if owner.IsOrganization() && !setting.Repository.EnablePushCreateOrg {
|
||||
ctx.HandleText(http.StatusForbidden, "Push to create is not enabled for organizations.")
|
||||
ctx.PlainText(http.StatusForbidden, "Push to create is not enabled for organizations.")
|
||||
return
|
||||
}
|
||||
if !owner.IsOrganization() && !setting.Repository.EnablePushCreateUser {
|
||||
ctx.HandleText(http.StatusForbidden, "Push to create is not enabled for users.")
|
||||
ctx.PlainText(http.StatusForbidden, "Push to create is not enabled for users.")
|
||||
return
|
||||
}
|
||||
|
||||
@@ -276,7 +276,7 @@ func httpBase(ctx *context.Context) (h *serviceHandler) {
|
||||
// Ensure the wiki is enabled before we allow access to it
|
||||
if _, err := repo.GetUnit(unit.TypeWiki); err != nil {
|
||||
if repo_model.IsErrUnitTypeNotExist(err) {
|
||||
ctx.HandleText(http.StatusForbidden, "repository wiki is disabled")
|
||||
ctx.PlainText(http.StatusForbidden, "repository wiki is disabled")
|
||||
return
|
||||
}
|
||||
log.Error("Failed to get the wiki unit in %-v Error: %v", repo, err)
|
||||
@@ -338,10 +338,10 @@ func dummyInfoRefs(ctx *context.Context) {
|
||||
infoRefsCache = refs
|
||||
})
|
||||
|
||||
ctx.Header().Set("Expires", "Fri, 01 Jan 1980 00:00:00 GMT")
|
||||
ctx.Header().Set("Pragma", "no-cache")
|
||||
ctx.Header().Set("Cache-Control", "no-cache, max-age=0, must-revalidate")
|
||||
ctx.Header().Set("Content-Type", "application/x-git-receive-pack-advertisement")
|
||||
ctx.RespHeader().Set("Expires", "Fri, 01 Jan 1980 00:00:00 GMT")
|
||||
ctx.RespHeader().Set("Pragma", "no-cache")
|
||||
ctx.RespHeader().Set("Cache-Control", "no-cache, max-age=0, must-revalidate")
|
||||
ctx.RespHeader().Set("Content-Type", "application/x-git-receive-pack-advertisement")
|
||||
_, _ = ctx.Write(packetWrite("# service=git-receive-pack\n"))
|
||||
_, _ = ctx.Write([]byte("0000"))
|
||||
_, _ = ctx.Write(infoRefsCache)
|
||||
|
@@ -2343,7 +2343,7 @@ func ChangeIssueReaction(ctx *context.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
html, err := ctx.HTMLString(string(tplReactions), map[string]interface{}{
|
||||
html, err := ctx.RenderToString(tplReactions, map[string]interface{}{
|
||||
"ctx": ctx.Data,
|
||||
"ActionURL": fmt.Sprintf("%s/issues/%d/reactions", ctx.Repo.RepoLink, issue.Index),
|
||||
"Reactions": issue.Reactions.GroupByType(),
|
||||
@@ -2443,7 +2443,7 @@ func ChangeCommentReaction(ctx *context.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
html, err := ctx.HTMLString(string(tplReactions), map[string]interface{}{
|
||||
html, err := ctx.RenderToString(tplReactions, map[string]interface{}{
|
||||
"ctx": ctx.Data,
|
||||
"ActionURL": fmt.Sprintf("%s/comments/%d/reactions", ctx.Repo.RepoLink, comment.ID),
|
||||
"Reactions": comment.Reactions.GroupByType(),
|
||||
@@ -2565,7 +2565,7 @@ func updateAttachments(item interface{}, files []string) error {
|
||||
}
|
||||
|
||||
func attachmentsHTML(ctx *context.Context, attachments []*repo_model.Attachment, content string) string {
|
||||
attachHTML, err := ctx.HTMLString(string(tplAttachment), map[string]interface{}{
|
||||
attachHTML, err := ctx.RenderToString(tplAttachment, map[string]interface{}{
|
||||
"ctx": ctx.Data,
|
||||
"Attachments": attachments,
|
||||
"Content": content,
|
||||
|
@@ -771,7 +771,7 @@ func UpdatePullRequest(ctx *context.Context) {
|
||||
if err = pull_service.Update(issue.PullRequest, ctx.User, message, rebase); err != nil {
|
||||
if models.IsErrMergeConflicts(err) {
|
||||
conflictError := err.(models.ErrMergeConflicts)
|
||||
flashError, err := ctx.HTMLString(string(tplAlertDetails), map[string]interface{}{
|
||||
flashError, err := ctx.RenderToString(tplAlertDetails, map[string]interface{}{
|
||||
"Message": ctx.Tr("repo.pulls.merge_conflict"),
|
||||
"Summary": ctx.Tr("repo.pulls.merge_conflict_summary"),
|
||||
"Details": utils.SanitizeFlashErrorString(conflictError.StdErr) + "<br>" + utils.SanitizeFlashErrorString(conflictError.StdOut),
|
||||
@@ -785,7 +785,7 @@ func UpdatePullRequest(ctx *context.Context) {
|
||||
return
|
||||
} else if models.IsErrRebaseConflicts(err) {
|
||||
conflictError := err.(models.ErrRebaseConflicts)
|
||||
flashError, err := ctx.HTMLString(string(tplAlertDetails), map[string]interface{}{
|
||||
flashError, err := ctx.RenderToString(tplAlertDetails, map[string]interface{}{
|
||||
"Message": ctx.Tr("repo.pulls.rebase_conflict", utils.SanitizeFlashErrorString(conflictError.CommitSHA)),
|
||||
"Summary": ctx.Tr("repo.pulls.rebase_conflict_summary"),
|
||||
"Details": utils.SanitizeFlashErrorString(conflictError.StdErr) + "<br>" + utils.SanitizeFlashErrorString(conflictError.StdOut),
|
||||
@@ -940,7 +940,7 @@ func MergePullRequest(ctx *context.Context) {
|
||||
return
|
||||
} else if models.IsErrMergeConflicts(err) {
|
||||
conflictError := err.(models.ErrMergeConflicts)
|
||||
flashError, err := ctx.HTMLString(string(tplAlertDetails), map[string]interface{}{
|
||||
flashError, err := ctx.RenderToString(tplAlertDetails, map[string]interface{}{
|
||||
"Message": ctx.Tr("repo.editor.merge_conflict"),
|
||||
"Summary": ctx.Tr("repo.editor.merge_conflict_summary"),
|
||||
"Details": utils.SanitizeFlashErrorString(conflictError.StdErr) + "<br>" + utils.SanitizeFlashErrorString(conflictError.StdOut),
|
||||
@@ -954,7 +954,7 @@ func MergePullRequest(ctx *context.Context) {
|
||||
return
|
||||
} else if models.IsErrRebaseConflicts(err) {
|
||||
conflictError := err.(models.ErrRebaseConflicts)
|
||||
flashError, err := ctx.HTMLString(string(tplAlertDetails), map[string]interface{}{
|
||||
flashError, err := ctx.RenderToString(tplAlertDetails, map[string]interface{}{
|
||||
"Message": ctx.Tr("repo.pulls.rebase_conflict", utils.SanitizeFlashErrorString(conflictError.CommitSHA)),
|
||||
"Summary": ctx.Tr("repo.pulls.rebase_conflict_summary"),
|
||||
"Details": utils.SanitizeFlashErrorString(conflictError.StdErr) + "<br>" + utils.SanitizeFlashErrorString(conflictError.StdOut),
|
||||
@@ -983,7 +983,7 @@ func MergePullRequest(ctx *context.Context) {
|
||||
if len(message) == 0 {
|
||||
ctx.Flash.Error(ctx.Tr("repo.pulls.push_rejected_no_message"))
|
||||
} else {
|
||||
flashError, err := ctx.HTMLString(string(tplAlertDetails), map[string]interface{}{
|
||||
flashError, err := ctx.RenderToString(tplAlertDetails, map[string]interface{}{
|
||||
"Message": ctx.Tr("repo.pulls.push_rejected"),
|
||||
"Summary": ctx.Tr("repo.pulls.push_rejected_summary"),
|
||||
"Details": utils.SanitizeFlashErrorString(pushrejErr.Message),
|
||||
@@ -1143,7 +1143,7 @@ func CompareAndPullRequestPost(ctx *context.Context) {
|
||||
if len(message) == 0 {
|
||||
ctx.Flash.Error(ctx.Tr("repo.pulls.push_rejected_no_message"))
|
||||
} else {
|
||||
flashError, err := ctx.HTMLString(string(tplAlertDetails), map[string]interface{}{
|
||||
flashError, err := ctx.RenderToString(tplAlertDetails, map[string]interface{}{
|
||||
"Message": ctx.Tr("repo.pulls.push_rejected"),
|
||||
"Summary": ctx.Tr("repo.pulls.push_rejected_summary"),
|
||||
"Details": utils.SanitizeFlashErrorString(pushrejErr.Message),
|
||||
|
@@ -506,7 +506,7 @@ func U2FSign(ctx *context.Context) {
|
||||
if redirect == "" {
|
||||
redirect = setting.AppSubURL + "/"
|
||||
}
|
||||
ctx.PlainText(200, []byte(redirect))
|
||||
ctx.PlainText(http.StatusOK, redirect)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
@@ -839,7 +839,7 @@ func ShowSSHKeys(ctx *context.Context, uid int64) {
|
||||
buf.WriteString(keys[i].OmitEmail())
|
||||
buf.WriteString("\n")
|
||||
}
|
||||
ctx.PlainText(200, buf.Bytes())
|
||||
ctx.PlainTextBytes(http.StatusOK, buf.Bytes())
|
||||
}
|
||||
|
||||
// ShowGPGKeys output all the public GPG keys of user by uid
|
||||
@@ -878,7 +878,7 @@ func ShowGPGKeys(ctx *context.Context, uid int64) {
|
||||
}
|
||||
}
|
||||
writer.Close()
|
||||
ctx.PlainText(200, buf.Bytes())
|
||||
ctx.PlainTextBytes(http.StatusOK, buf.Bytes())
|
||||
}
|
||||
|
||||
// Email2User show user page via email
|
||||
|
@@ -251,7 +251,7 @@ type userInfoResponse struct {
|
||||
func InfoOAuth(ctx *context.Context) {
|
||||
if ctx.User == nil || ctx.Data["AuthedMethod"] != (&auth.OAuth2{}).Name() {
|
||||
ctx.Resp.Header().Set("WWW-Authenticate", `Bearer realm=""`)
|
||||
ctx.HandleText(http.StatusUnauthorized, "no valid authorization")
|
||||
ctx.PlainText(http.StatusUnauthorized, "no valid authorization")
|
||||
return
|
||||
}
|
||||
|
||||
@@ -301,7 +301,7 @@ func getOAuthGroupsForUser(user *user_model.User) ([]string, error) {
|
||||
func IntrospectOAuth(ctx *context.Context) {
|
||||
if ctx.User == nil {
|
||||
ctx.Resp.Header().Set("WWW-Authenticate", `Bearer realm=""`)
|
||||
ctx.HandleText(http.StatusUnauthorized, "no valid authorization")
|
||||
ctx.PlainText(http.StatusUnauthorized, "no valid authorization")
|
||||
return
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user