Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. // Copyright 2016 The Gogs Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package context
  5. import (
  6. "fmt"
  7. "strings"
  8. "github.com/go-macaron/csrf"
  9. "code.gitea.io/git"
  10. "code.gitea.io/gitea/models"
  11. "code.gitea.io/gitea/modules/base"
  12. "code.gitea.io/gitea/modules/log"
  13. "code.gitea.io/gitea/modules/setting"
  14. "github.com/Unknwon/paginater"
  15. macaron "gopkg.in/macaron.v1"
  16. )
  17. // APIContext is a specific macaron context for API service
  18. type APIContext struct {
  19. *Context
  20. Org *APIOrganization
  21. }
  22. // APIError is error format response
  23. // swagger:response error
  24. type APIError struct {
  25. Message string `json:"message"`
  26. URL string `json:"url"`
  27. }
  28. // APIValidationError is error format response related to input validation
  29. // swagger:response validationError
  30. type APIValidationError struct {
  31. Message string `json:"message"`
  32. URL string `json:"url"`
  33. }
  34. //APIEmpty is an empty response
  35. // swagger:response empty
  36. type APIEmpty struct{}
  37. //APIForbiddenError is a forbidden error response
  38. // swagger:response forbidden
  39. type APIForbiddenError struct {
  40. APIError
  41. }
  42. //APINotFound is a not found empty response
  43. // swagger:response notFound
  44. type APINotFound struct{}
  45. //APIRedirect is a redirect response
  46. // swagger:response redirect
  47. type APIRedirect struct{}
  48. // Error responses error message to client with given message.
  49. // If status is 500, also it prints error to log.
  50. func (ctx *APIContext) Error(status int, title string, obj interface{}) {
  51. var message string
  52. if err, ok := obj.(error); ok {
  53. message = err.Error()
  54. } else {
  55. message = obj.(string)
  56. }
  57. if status == 500 {
  58. log.Error(4, "%s: %s", title, message)
  59. }
  60. ctx.JSON(status, APIError{
  61. Message: message,
  62. URL: base.DocURL,
  63. })
  64. }
  65. // SetLinkHeader sets pagination link header by given total number and page size.
  66. func (ctx *APIContext) SetLinkHeader(total, pageSize int) {
  67. page := paginater.New(total, pageSize, ctx.QueryInt("page"), 0)
  68. links := make([]string, 0, 4)
  69. if page.HasNext() {
  70. links = append(links, fmt.Sprintf("<%s%s?page=%d>; rel=\"next\"", setting.AppURL, ctx.Req.URL.Path[1:], page.Next()))
  71. }
  72. if !page.IsLast() {
  73. links = append(links, fmt.Sprintf("<%s%s?page=%d>; rel=\"last\"", setting.AppURL, ctx.Req.URL.Path[1:], page.TotalPages()))
  74. }
  75. if !page.IsFirst() {
  76. links = append(links, fmt.Sprintf("<%s%s?page=1>; rel=\"first\"", setting.AppURL, ctx.Req.URL.Path[1:]))
  77. }
  78. if page.HasPrevious() {
  79. links = append(links, fmt.Sprintf("<%s%s?page=%d>; rel=\"prev\"", setting.AppURL, ctx.Req.URL.Path[1:], page.Previous()))
  80. }
  81. if len(links) > 0 {
  82. ctx.Header().Set("Link", strings.Join(links, ","))
  83. }
  84. }
  85. // RequireCSRF requires a validated a CSRF token
  86. func (ctx *APIContext) RequireCSRF() {
  87. headerToken := ctx.Req.Header.Get(ctx.csrf.GetHeaderName())
  88. formValueToken := ctx.Req.FormValue(ctx.csrf.GetFormName())
  89. if len(headerToken) > 0 || len(formValueToken) > 0 {
  90. csrf.Validate(ctx.Context.Context, ctx.csrf)
  91. } else {
  92. ctx.Context.Error(401)
  93. }
  94. }
  95. // APIContexter returns apicontext as macaron middleware
  96. func APIContexter() macaron.Handler {
  97. return func(c *Context) {
  98. ctx := &APIContext{
  99. Context: c,
  100. }
  101. c.Map(ctx)
  102. }
  103. }
  104. // ReferencesGitRepo injects the GitRepo into the Context
  105. func ReferencesGitRepo() macaron.Handler {
  106. return func(ctx *APIContext) {
  107. // Empty repository does not have reference information.
  108. if ctx.Repo.Repository.IsEmpty {
  109. return
  110. }
  111. // For API calls.
  112. if ctx.Repo.GitRepo == nil {
  113. repoPath := models.RepoPath(ctx.Repo.Owner.Name, ctx.Repo.Repository.Name)
  114. gitRepo, err := git.OpenRepository(repoPath)
  115. if err != nil {
  116. ctx.Error(500, "RepoRef Invalid repo "+repoPath, err)
  117. return
  118. }
  119. ctx.Repo.GitRepo = gitRepo
  120. }
  121. }
  122. }