BasePathPrefix returns the URL path prefix that the request was reached under (e.g. "/myprefix/"). It always returns a value that starts and ends with `/`, defaulting to "/" when the app is not behind a path prefix. It first looks at the path StripPathPrefix removed (when the proxy forwards the pre
(c echo.Context)
| 22 | // "//evil.com" turns the asset rewrite into a protocol-relative URL that |
| 23 | // loads JS from a foreign origin. |
| 24 | func BasePathPrefix(c echo.Context) string { |
| 25 | path := c.Path() |
| 26 | origPath := c.Request().URL.Path |
| 27 | |
| 28 | if storedPath, ok := c.Get("_original_path").(string); ok && storedPath != "" { |
| 29 | origPath = storedPath |
| 30 | } |
| 31 | |
| 32 | if path != origPath && strings.HasSuffix(origPath, path) && len(path) > 0 { |
| 33 | prefixLen := len(origPath) - len(path) |
| 34 | if prefixLen > 0 { |
| 35 | pathPrefix := origPath[:prefixLen] |
| 36 | if !strings.HasSuffix(pathPrefix, "/") { |
| 37 | pathPrefix += "/" |
| 38 | } |
| 39 | return pathPrefix |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | if validated, ok := SafeForwardedPrefix(c.Request().Header.Get("X-Forwarded-Prefix")); ok { |
| 44 | if !strings.HasSuffix(validated, "/") { |
| 45 | validated += "/" |
| 46 | } |
| 47 | return validated |
| 48 | } |
| 49 | |
| 50 | return "/" |
| 51 | } |
| 52 | |
| 53 | // BaseURL returns the base URL for the given HTTP request context. |
| 54 | // It takes into account that the app may be exposed by a reverse-proxy under a different protocol, host and path. |
no test coverage detected