(rewriteRegex map[*regexp.Regexp]string, req *http.Request)
| 49 | } |
| 50 | |
| 51 | func 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. |
| 87 | func DefaultSkipper(c *echo.Context) bool { |
searching dependent graphs…