You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

wrap_convert.go 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. // Copyright 2021 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package web
  4. import (
  5. goctx "context"
  6. "fmt"
  7. "net/http"
  8. "code.gitea.io/gitea/modules/context"
  9. "code.gitea.io/gitea/modules/web/routing"
  10. )
  11. type wrappedHandlerFunc func(resp http.ResponseWriter, req *http.Request, others ...wrappedHandlerFunc) (done bool, deferrable func())
  12. func convertHandler(handler interface{}) wrappedHandlerFunc {
  13. funcInfo := routing.GetFuncInfo(handler)
  14. switch t := handler.(type) {
  15. case http.HandlerFunc:
  16. return func(resp http.ResponseWriter, req *http.Request, others ...wrappedHandlerFunc) (done bool, deferrable func()) {
  17. routing.UpdateFuncInfo(req.Context(), funcInfo)
  18. if _, ok := resp.(context.ResponseWriter); !ok {
  19. resp = context.NewResponse(resp)
  20. }
  21. t(resp, req)
  22. if r, ok := resp.(context.ResponseWriter); ok && r.Status() > 0 {
  23. done = true
  24. }
  25. return done, deferrable
  26. }
  27. case func(http.ResponseWriter, *http.Request):
  28. return func(resp http.ResponseWriter, req *http.Request, others ...wrappedHandlerFunc) (done bool, deferrable func()) {
  29. routing.UpdateFuncInfo(req.Context(), funcInfo)
  30. t(resp, req)
  31. if r, ok := resp.(context.ResponseWriter); ok && r.Status() > 0 {
  32. done = true
  33. }
  34. return done, deferrable
  35. }
  36. case func(ctx *context.Context):
  37. return func(resp http.ResponseWriter, req *http.Request, others ...wrappedHandlerFunc) (done bool, deferrable func()) {
  38. routing.UpdateFuncInfo(req.Context(), funcInfo)
  39. ctx := context.GetContext(req)
  40. t(ctx)
  41. done = ctx.Written()
  42. return done, deferrable
  43. }
  44. case func(ctx *context.Context) goctx.CancelFunc:
  45. return func(resp http.ResponseWriter, req *http.Request, others ...wrappedHandlerFunc) (done bool, deferrable func()) {
  46. routing.UpdateFuncInfo(req.Context(), funcInfo)
  47. ctx := context.GetContext(req)
  48. deferrable = t(ctx)
  49. done = ctx.Written()
  50. return done, deferrable
  51. }
  52. case func(*context.APIContext):
  53. return func(resp http.ResponseWriter, req *http.Request, others ...wrappedHandlerFunc) (done bool, deferrable func()) {
  54. routing.UpdateFuncInfo(req.Context(), funcInfo)
  55. ctx := context.GetAPIContext(req)
  56. t(ctx)
  57. done = ctx.Written()
  58. return done, deferrable
  59. }
  60. case func(*context.APIContext) goctx.CancelFunc:
  61. return func(resp http.ResponseWriter, req *http.Request, others ...wrappedHandlerFunc) (done bool, deferrable func()) {
  62. routing.UpdateFuncInfo(req.Context(), funcInfo)
  63. ctx := context.GetAPIContext(req)
  64. deferrable = t(ctx)
  65. done = ctx.Written()
  66. return done, deferrable
  67. }
  68. case func(*context.PrivateContext):
  69. return func(resp http.ResponseWriter, req *http.Request, others ...wrappedHandlerFunc) (done bool, deferrable func()) {
  70. routing.UpdateFuncInfo(req.Context(), funcInfo)
  71. ctx := context.GetPrivateContext(req)
  72. t(ctx)
  73. done = ctx.Written()
  74. return done, deferrable
  75. }
  76. case func(*context.PrivateContext) goctx.CancelFunc:
  77. return func(resp http.ResponseWriter, req *http.Request, others ...wrappedHandlerFunc) (done bool, deferrable func()) {
  78. routing.UpdateFuncInfo(req.Context(), funcInfo)
  79. ctx := context.GetPrivateContext(req)
  80. deferrable = t(ctx)
  81. done = ctx.Written()
  82. return done, deferrable
  83. }
  84. case func(http.Handler) http.Handler:
  85. return func(resp http.ResponseWriter, req *http.Request, others ...wrappedHandlerFunc) (done bool, deferrable func()) {
  86. next := http.HandlerFunc(func(http.ResponseWriter, *http.Request) {})
  87. if len(others) > 0 {
  88. next = wrapInternal(others)
  89. }
  90. routing.UpdateFuncInfo(req.Context(), funcInfo)
  91. if _, ok := resp.(context.ResponseWriter); !ok {
  92. resp = context.NewResponse(resp)
  93. }
  94. t(next).ServeHTTP(resp, req)
  95. if r, ok := resp.(context.ResponseWriter); ok && r.Status() > 0 {
  96. done = true
  97. }
  98. return done, deferrable
  99. }
  100. default:
  101. panic(fmt.Sprintf("Unsupported handler type: %#v", t))
  102. }
  103. }