MCPcopy
hub / github.com/labstack/echo / rewriteURL

Function rewriteURL

middleware/middleware.go:51–84  ·  view source on GitHub ↗
(rewriteRegex map[*regexp.Regexp]string, req *http.Request)

Source from the content-addressed store, hash-verified

49}
50
51func rewriteURL(rewriteRegex map[*regexp.Regexp]string, req *http.Request) error {
52 if len(rewriteRegex) == 0 {
53 return nil
54 }
55
56 // Depending how HTTP request is sent RequestURI could contain Scheme://Host/path or be just /path.
57 // We only want to use path part for rewriting and therefore trim prefix if it exists
58 rawURI := req.RequestURI
59 if rawURI != "" && rawURI[0] != '/' {
60 prefix := ""
61 if req.URL.Scheme != "" {
62 prefix = req.URL.Scheme + "://"
63 }
64 if req.URL.Host != "" {
65 prefix += req.URL.Host // host or host:port
66 }
67 if prefix != "" {
68 rawURI = strings.TrimPrefix(rawURI, prefix)
69 }
70 }
71
72 for k, v := range rewriteRegex {
73 if replacer := captureTokens(k, rawURI); replacer != nil {
74 url, err := req.URL.Parse(replacer.Replace(v))
75 if err != nil {
76 return err
77 }
78 req.URL = url
79
80 return nil // rewrite only once
81 }
82 }
83 return nil
84}
85
86// DefaultSkipper returns false which processes the middleware.
87func DefaultSkipper(c *echo.Context) bool {

Callers 3

TestRewriteURLFunction · 0.85
ToMiddlewareMethod · 0.85
ToMiddlewareMethod · 0.85

Calls 1

captureTokensFunction · 0.85

Tested by 1

TestRewriteURLFunction · 0.68

Used in the wild real call sites across dependent graphs

searching dependent graphs…