Skip to content

Commit

Permalink
feat(main): 增加StripPrefix中间件
Browse files Browse the repository at this point in the history
  • Loading branch information
BadKid90s committed Feb 1, 2024
1 parent d80afc8 commit 4c320ce
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions knife/middleware/strip_prefix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package middleware

import (
"knife"
"net/url"
"strings"
)

func StripPrefix(prefix string) knife.MiddlewareFunc {
return func(context *knife.Context) {
r := context.Req
p := strings.TrimPrefix(r.URL.Path, prefix)
rp := strings.TrimPrefix(r.URL.RawPath, prefix)
if len(p) < len(r.URL.Path) && (r.URL.RawPath == "" || len(rp) < len(r.URL.RawPath)) {
r.URL = new(url.URL)
r.URL.Path = p
r.URL.RawPath = rp
}
context.Next()
}
}

0 comments on commit 4c320ce

Please sign in to comment.