patrouter.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. package httprouter
  2. import (
  3. "context"
  4. "net/http"
  5. "path"
  6. "strings"
  7. "zero/core/search"
  8. )
  9. const (
  10. allowHeader = "Allow"
  11. allowMethodSeparator = ", "
  12. pathVars = "pathVars"
  13. )
  14. type PatRouter struct {
  15. trees map[string]*search.Tree
  16. notFound http.Handler
  17. }
  18. func NewPatRouter() Router {
  19. return &PatRouter{
  20. trees: make(map[string]*search.Tree),
  21. }
  22. }
  23. func (pr *PatRouter) Handle(method, reqPath string, handler http.Handler) error {
  24. if !validMethod(method) {
  25. return ErrInvalidMethod
  26. }
  27. if len(reqPath) == 0 || reqPath[0] != '/' {
  28. return ErrInvalidPath
  29. }
  30. cleanPath := path.Clean(reqPath)
  31. if tree, ok := pr.trees[method]; ok {
  32. return tree.Add(cleanPath, handler)
  33. } else {
  34. tree = search.NewTree()
  35. pr.trees[method] = tree
  36. return tree.Add(cleanPath, handler)
  37. }
  38. }
  39. func (pr *PatRouter) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  40. reqPath := path.Clean(r.URL.Path)
  41. if tree, ok := pr.trees[r.Method]; ok {
  42. if result, ok := tree.Search(reqPath); ok {
  43. if len(result.Params) > 0 {
  44. r = r.WithContext(context.WithValue(r.Context(), pathVars, result.Params))
  45. }
  46. result.Item.(http.Handler).ServeHTTP(w, r)
  47. return
  48. }
  49. }
  50. if allow, ok := pr.methodNotAllowed(r.Method, reqPath); ok {
  51. w.Header().Set(allowHeader, allow)
  52. w.WriteHeader(http.StatusMethodNotAllowed)
  53. } else {
  54. pr.handleNotFound(w, r)
  55. }
  56. }
  57. func (pr *PatRouter) SetNotFoundHandler(handler http.Handler) {
  58. pr.notFound = handler
  59. }
  60. func (pr *PatRouter) handleNotFound(w http.ResponseWriter, r *http.Request) {
  61. if pr.notFound != nil {
  62. pr.notFound.ServeHTTP(w, r)
  63. } else {
  64. http.NotFound(w, r)
  65. }
  66. }
  67. func (pr *PatRouter) methodNotAllowed(method, path string) (string, bool) {
  68. var allows []string
  69. for treeMethod, tree := range pr.trees {
  70. if treeMethod == method {
  71. continue
  72. }
  73. _, ok := tree.Search(path)
  74. if ok {
  75. allows = append(allows, treeMethod)
  76. }
  77. }
  78. if len(allows) > 0 {
  79. return strings.Join(allows, allowMethodSeparator), true
  80. } else {
  81. return "", false
  82. }
  83. }
  84. func Vars(r *http.Request) map[string]string {
  85. vars, ok := r.Context().Value(pathVars).(map[string]string)
  86. if ok {
  87. return vars
  88. }
  89. return nil
  90. }
  91. func validMethod(method string) bool {
  92. return method == http.MethodDelete || method == http.MethodGet ||
  93. method == http.MethodHead || method == http.MethodOptions ||
  94. method == http.MethodPatch || method == http.MethodPost ||
  95. method == http.MethodPut
  96. }