gitea源码

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. // Copyright 2021 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package web
  4. import (
  5. "bytes"
  6. "net/http"
  7. "net/http/httptest"
  8. "strings"
  9. "testing"
  10. "code.gitea.io/gitea/modules/setting"
  11. "code.gitea.io/gitea/modules/test"
  12. "code.gitea.io/gitea/modules/util"
  13. "github.com/go-chi/chi/v5"
  14. "github.com/stretchr/testify/assert"
  15. )
  16. func chiURLParamsToMap(chiCtx *chi.Context) map[string]string {
  17. pathParams := chiCtx.URLParams
  18. m := make(map[string]string, len(pathParams.Keys))
  19. for i, key := range pathParams.Keys {
  20. if key == "*" && pathParams.Values[i] == "" {
  21. continue // chi router will add an empty "*" key if there is a "Mount"
  22. }
  23. m[key] = pathParams.Values[i]
  24. }
  25. return util.Iif(len(m) == 0, nil, m)
  26. }
  27. func TestPathProcessor(t *testing.T) {
  28. testProcess := func(pattern, uri string, expectedPathParams map[string]string) {
  29. chiCtx := chi.NewRouteContext()
  30. chiCtx.RouteMethod = "GET"
  31. p := newRouterPathMatcher("GET", patternRegexp(pattern), http.NotFound)
  32. assert.True(t, p.matchPath(chiCtx, uri), "use pattern %s to process uri %s", pattern, uri)
  33. assert.Equal(t, expectedPathParams, chiURLParamsToMap(chiCtx), "use pattern %s to process uri %s", pattern, uri)
  34. }
  35. // the "<...>" is intentionally designed to distinguish from chi's path parameters, because:
  36. // 1. their behaviors are totally different, we do not want to mislead developers
  37. // 2. we can write regexp in "<name:\w{3,4}>" easily and parse it easily
  38. testProcess("/<p1>/<p2>", "/a/b", map[string]string{"p1": "a", "p2": "b"})
  39. testProcess("/<p1:*>", "", map[string]string{"p1": ""}) // this is a special case, because chi router could use empty path
  40. testProcess("/<p1:*>", "/", map[string]string{"p1": ""})
  41. testProcess("/<p1:*>/<p2>", "/a", map[string]string{"p1": "", "p2": "a"})
  42. testProcess("/<p1:*>/<p2>", "/a/b", map[string]string{"p1": "a", "p2": "b"})
  43. testProcess("/<p1:*>/<p2>", "/a/b/c", map[string]string{"p1": "a/b", "p2": "c"})
  44. }
  45. func TestRouter(t *testing.T) {
  46. buff := &bytes.Buffer{}
  47. recorder := httptest.NewRecorder()
  48. recorder.Body = buff
  49. type resultStruct struct {
  50. method string
  51. pathParams map[string]string
  52. handlerMarks []string
  53. chiRoutePattern *string
  54. }
  55. var res resultStruct
  56. h := func(optMark ...string) func(resp http.ResponseWriter, req *http.Request) {
  57. mark := util.OptionalArg(optMark, "")
  58. return func(resp http.ResponseWriter, req *http.Request) {
  59. chiCtx := chi.RouteContext(req.Context())
  60. res.method = req.Method
  61. res.pathParams = chiURLParamsToMap(chiCtx)
  62. res.chiRoutePattern = util.ToPointer(chiCtx.RoutePattern())
  63. if mark != "" {
  64. res.handlerMarks = append(res.handlerMarks, mark)
  65. }
  66. }
  67. }
  68. stopMark := func(optMark ...string) func(resp http.ResponseWriter, req *http.Request) {
  69. mark := util.OptionalArg(optMark, "")
  70. return func(resp http.ResponseWriter, req *http.Request) {
  71. if stop := req.FormValue("stop"); stop != "" && (mark == "" || mark == stop) {
  72. h(stop)(resp, req)
  73. resp.WriteHeader(http.StatusOK)
  74. } else if mark != "" {
  75. res.handlerMarks = append(res.handlerMarks, mark)
  76. }
  77. }
  78. }
  79. r := NewRouter()
  80. r.NotFound(h("not-found:/"))
  81. r.Get("/{username}/{reponame}/{type:issues|pulls}", h("list-issues-a")) // this one will never be called
  82. r.Group("/{username}/{reponame}", func() {
  83. r.Get("/{type:issues|pulls}", h("list-issues-b"))
  84. r.Group("", func() {
  85. r.Get("/{type:issues|pulls}/{index}", h("view-issue"))
  86. }, stopMark())
  87. r.Group("/issues/{index}", func() {
  88. r.Post("/update", h("update-issue"))
  89. })
  90. })
  91. m := NewRouter()
  92. m.NotFound(h("not-found:/api/v1"))
  93. r.Mount("/api/v1", m)
  94. m.Group("/repos", func() {
  95. m.Group("/{username}/{reponame}", func() {
  96. m.Group("/branches", func() {
  97. m.Get("", h())
  98. m.Post("", h())
  99. m.Group("/{name}", func() {
  100. m.Get("", h())
  101. m.Patch("", h())
  102. m.Delete("", h())
  103. })
  104. m.PathGroup("/*", func(g *RouterPathGroup) {
  105. g.MatchPattern("GET", g.PatternRegexp(`/<dir:*>/<file:[a-z]{1,2}>`, stopMark("s2")), stopMark("s3"), h("match-path"))
  106. }, stopMark("s1"))
  107. })
  108. })
  109. })
  110. testRoute := func(t *testing.T, methodPath string, expected resultStruct) {
  111. t.Run(methodPath, func(t *testing.T) {
  112. res = resultStruct{}
  113. methodPathFields := strings.Fields(methodPath)
  114. req, err := http.NewRequest(methodPathFields[0], methodPathFields[1], nil)
  115. assert.NoError(t, err)
  116. r.ServeHTTP(recorder, req)
  117. if expected.chiRoutePattern == nil {
  118. res.chiRoutePattern = nil
  119. }
  120. assert.Equal(t, expected, res)
  121. })
  122. }
  123. t.Run("RootRouter", func(t *testing.T) {
  124. testRoute(t, "GET /the-user/the-repo/other", resultStruct{
  125. method: "GET",
  126. handlerMarks: []string{"not-found:/"},
  127. chiRoutePattern: util.ToPointer(""),
  128. })
  129. testRoute(t, "GET /the-user/the-repo/pulls", resultStruct{
  130. method: "GET",
  131. pathParams: map[string]string{"username": "the-user", "reponame": "the-repo", "type": "pulls"},
  132. handlerMarks: []string{"list-issues-b"},
  133. })
  134. testRoute(t, "GET /the-user/the-repo/issues/123", resultStruct{
  135. method: "GET",
  136. pathParams: map[string]string{"username": "the-user", "reponame": "the-repo", "type": "issues", "index": "123"},
  137. handlerMarks: []string{"view-issue"},
  138. chiRoutePattern: util.ToPointer("/{username}/{reponame}/{type:issues|pulls}/{index}"),
  139. })
  140. testRoute(t, "GET /the-user/the-repo/issues/123?stop=hijack", resultStruct{
  141. method: "GET",
  142. pathParams: map[string]string{"username": "the-user", "reponame": "the-repo", "type": "issues", "index": "123"},
  143. handlerMarks: []string{"hijack"},
  144. })
  145. testRoute(t, "POST /the-user/the-repo/issues/123/update", resultStruct{
  146. method: "POST",
  147. pathParams: map[string]string{"username": "the-user", "reponame": "the-repo", "index": "123"},
  148. handlerMarks: []string{"update-issue"},
  149. })
  150. })
  151. t.Run("Sub Router", func(t *testing.T) {
  152. testRoute(t, "GET /api/v1/other", resultStruct{
  153. method: "GET",
  154. handlerMarks: []string{"not-found:/api/v1"},
  155. })
  156. testRoute(t, "GET /api/v1/repos/the-user/the-repo/branches", resultStruct{
  157. method: "GET",
  158. pathParams: map[string]string{"username": "the-user", "reponame": "the-repo"},
  159. })
  160. testRoute(t, "POST /api/v1/repos/the-user/the-repo/branches", resultStruct{
  161. method: "POST",
  162. pathParams: map[string]string{"username": "the-user", "reponame": "the-repo"},
  163. })
  164. testRoute(t, "GET /api/v1/repos/the-user/the-repo/branches/master", resultStruct{
  165. method: "GET",
  166. pathParams: map[string]string{"username": "the-user", "reponame": "the-repo", "name": "master"},
  167. })
  168. testRoute(t, "PATCH /api/v1/repos/the-user/the-repo/branches/master", resultStruct{
  169. method: "PATCH",
  170. pathParams: map[string]string{"username": "the-user", "reponame": "the-repo", "name": "master"},
  171. })
  172. testRoute(t, "DELETE /api/v1/repos/the-user/the-repo/branches/master", resultStruct{
  173. method: "DELETE",
  174. pathParams: map[string]string{"username": "the-user", "reponame": "the-repo", "name": "master"},
  175. })
  176. })
  177. t.Run("MatchPath", func(t *testing.T) {
  178. testRoute(t, "GET /api/v1/repos/the-user/the-repo/branches/d1/d2/fn", resultStruct{
  179. method: "GET",
  180. pathParams: map[string]string{"username": "the-user", "reponame": "the-repo", "*": "d1/d2/fn", "dir": "d1/d2", "file": "fn"},
  181. handlerMarks: []string{"s1", "s2", "s3", "match-path"},
  182. })
  183. testRoute(t, "GET /api/v1/repos/the-user/the-repo/branches/d1%2fd2/fn", resultStruct{
  184. method: "GET",
  185. pathParams: map[string]string{"username": "the-user", "reponame": "the-repo", "*": "d1%2fd2/fn", "dir": "d1%2fd2", "file": "fn"},
  186. handlerMarks: []string{"s1", "s2", "s3", "match-path"},
  187. })
  188. testRoute(t, "GET /api/v1/repos/the-user/the-repo/branches/d1/d2/000", resultStruct{
  189. method: "GET",
  190. pathParams: map[string]string{"reponame": "the-repo", "username": "the-user", "*": "d1/d2/000"},
  191. handlerMarks: []string{"s1", "not-found:/api/v1"},
  192. })
  193. testRoute(t, "GET /api/v1/repos/the-user/the-repo/branches/d1/d2/fn?stop=s1", resultStruct{
  194. method: "GET",
  195. pathParams: map[string]string{"username": "the-user", "reponame": "the-repo", "*": "d1/d2/fn"},
  196. handlerMarks: []string{"s1"},
  197. })
  198. testRoute(t, "GET /api/v1/repos/the-user/the-repo/branches/d1/d2/fn?stop=s2", resultStruct{
  199. method: "GET",
  200. pathParams: map[string]string{"username": "the-user", "reponame": "the-repo", "*": "d1/d2/fn", "dir": "d1/d2", "file": "fn"},
  201. handlerMarks: []string{"s1", "s2"},
  202. })
  203. testRoute(t, "GET /api/v1/repos/the-user/the-repo/branches/d1/d2/fn?stop=s3", resultStruct{
  204. method: "GET",
  205. pathParams: map[string]string{"username": "the-user", "reponame": "the-repo", "*": "d1/d2/fn", "dir": "d1/d2", "file": "fn"},
  206. handlerMarks: []string{"s1", "s2", "s3"},
  207. chiRoutePattern: util.ToPointer("/api/v1/repos/{username}/{reponame}/branches/<dir:*>/<file:[a-z]{1,2}>"),
  208. })
  209. })
  210. }
  211. func TestRouteNormalizePath(t *testing.T) {
  212. type paths struct {
  213. EscapedPath, RawPath, Path string
  214. }
  215. testPath := func(reqPath string, expectedPaths paths) {
  216. recorder := httptest.NewRecorder()
  217. recorder.Body = bytes.NewBuffer(nil)
  218. actualPaths := paths{EscapedPath: "(none)", RawPath: "(none)", Path: "(none)"}
  219. r := NewRouter()
  220. r.Get("/*", func(resp http.ResponseWriter, req *http.Request) {
  221. actualPaths.EscapedPath = req.URL.EscapedPath()
  222. actualPaths.RawPath = req.URL.RawPath
  223. actualPaths.Path = req.URL.Path
  224. })
  225. req, err := http.NewRequest(http.MethodGet, reqPath, nil)
  226. assert.NoError(t, err)
  227. r.ServeHTTP(recorder, req)
  228. assert.Equal(t, expectedPaths, actualPaths, "req path = %q", reqPath)
  229. }
  230. // RawPath could be empty if the EscapedPath is the same as escape(Path) and it is already normalized
  231. testPath("/", paths{EscapedPath: "/", RawPath: "", Path: "/"})
  232. testPath("//", paths{EscapedPath: "/", RawPath: "/", Path: "/"})
  233. testPath("/%2f", paths{EscapedPath: "/%2f", RawPath: "/%2f", Path: "//"})
  234. testPath("///a//b/", paths{EscapedPath: "/a/b", RawPath: "/a/b", Path: "/a/b"})
  235. defer test.MockVariableValue(&setting.UseSubURLPath, true)()
  236. defer test.MockVariableValue(&setting.AppSubURL, "/sub-path")()
  237. testPath("/", paths{EscapedPath: "(none)", RawPath: "(none)", Path: "(none)"}) // 404
  238. testPath("/sub-path", paths{EscapedPath: "/", RawPath: "/", Path: "/"})
  239. testPath("/sub-path/", paths{EscapedPath: "/", RawPath: "/", Path: "/"})
  240. testPath("/sub-path//a/b///", paths{EscapedPath: "/a/b", RawPath: "/a/b", Path: "/a/b"})
  241. testPath("/sub-path/%2f/", paths{EscapedPath: "/%2f", RawPath: "/%2f", Path: "//"})
  242. // "/v2" is special for OCI container registry, it should always be in the root of the site
  243. testPath("/v2", paths{EscapedPath: "/v2", RawPath: "/v2", Path: "/v2"})
  244. testPath("/v2/", paths{EscapedPath: "/v2", RawPath: "/v2", Path: "/v2"})
  245. testPath("/v2/%2f", paths{EscapedPath: "/v2/%2f", RawPath: "/v2/%2f", Path: "/v2//"})
  246. }