From 4c320ce0919b491168b3f4fbc7fe72a8cd54a5f7 Mon Sep 17 00:00:00 2001 From: wangruiyu Date: Thu, 1 Feb 2024 15:43:31 +0800 Subject: [PATCH] =?UTF-8?q?feat(main):=20=E5=A2=9E=E5=8A=A0StripPrefix?= =?UTF-8?q?=E4=B8=AD=E9=97=B4=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- knife/middleware/strip_prefix.go | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 knife/middleware/strip_prefix.go diff --git a/knife/middleware/strip_prefix.go b/knife/middleware/strip_prefix.go new file mode 100644 index 0000000..ee6529e --- /dev/null +++ b/knife/middleware/strip_prefix.go @@ -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() + } +}