ToMiddleware converts AddTrailingSlashConfig to middleware or returns an error for invalid configuration
()
| 37 | |
| 38 | // ToMiddleware converts AddTrailingSlashConfig to middleware or returns an error for invalid configuration |
| 39 | func (config AddTrailingSlashConfig) ToMiddleware() (echo.MiddlewareFunc, error) { |
| 40 | if config.Skipper == nil { |
| 41 | config.Skipper = DefaultSkipper |
| 42 | } |
| 43 | if config.RedirectCode != 0 && (config.RedirectCode < http.StatusMultipleChoices || config.RedirectCode > http.StatusPermanentRedirect) { |
| 44 | // this is same check as `echo.context.Redirect()` does, but we can check this before even serving the request. |
| 45 | return nil, errors.New("invalid redirect code for add trailing slash middleware") |
| 46 | } |
| 47 | |
| 48 | return func(next echo.HandlerFunc) echo.HandlerFunc { |
| 49 | return func(c *echo.Context) error { |
| 50 | if config.Skipper(c) { |
| 51 | return next(c) |
| 52 | } |
| 53 | |
| 54 | req := c.Request() |
| 55 | url := req.URL |
| 56 | path := url.Path |
| 57 | qs := c.QueryString() |
| 58 | if !strings.HasSuffix(path, "/") { |
| 59 | path += "/" |
| 60 | uri := path |
| 61 | if qs != "" { |
| 62 | uri += "?" + qs |
| 63 | } |
| 64 | |
| 65 | // Redirect |
| 66 | if config.RedirectCode != 0 { |
| 67 | return c.Redirect(config.RedirectCode, sanitizeURI(uri)) |
| 68 | } |
| 69 | |
| 70 | // Forward |
| 71 | req.RequestURI = uri |
| 72 | url.Path = path |
| 73 | } |
| 74 | return next(c) |
| 75 | } |
| 76 | }, nil |
| 77 | } |
| 78 | |
| 79 | // RemoveTrailingSlashConfig is the middleware config for removing trailing slash from the request. |
| 80 | type RemoveTrailingSlashConfig struct { |
nothing calls this directly
no test coverage detected