BaseURL returns the base URL for the given HTTP request context. It takes into account that the app may be exposed by a reverse-proxy under a different protocol, host and path. The returned URL is guaranteed to end with `/`. The method should be used in conjunction with the StripPathPrefix middlewar
(c echo.Context)
| 55 | // The returned URL is guaranteed to end with `/`. |
| 56 | // The method should be used in conjunction with the StripPathPrefix middleware. |
| 57 | func BaseURL(c echo.Context) string { |
| 58 | // An explicit external base URL (LOCALAI_BASE_URL) is authoritative for |
| 59 | // the origin. The proxy-derived path prefix is still appended so a |
| 60 | // reverse-proxy mount point keeps working. Trailing slashes are |
| 61 | // normalized via BasePathPrefix, which always starts and ends with "/". |
| 62 | if ext, ok := c.Get("_external_base_url").(string); ok && ext != "" { |
| 63 | return strings.TrimRight(ext, "/") + BasePathPrefix(c) |
| 64 | } |
| 65 | |
| 66 | fwdProto, fwdHost := parseForwarded(c.Request().Header.Get("Forwarded")) |
| 67 | |
| 68 | scheme := "http" |
| 69 | switch { |
| 70 | case c.Request().TLS != nil: |
| 71 | scheme = "https" |
| 72 | case strings.EqualFold(firstToken(c.Request().Header.Get("X-Forwarded-Proto")), "https"): |
| 73 | scheme = "https" |
| 74 | case strings.EqualFold(fwdProto, "https"): |
| 75 | scheme = "https" |
| 76 | } |
| 77 | |
| 78 | host := c.Request().Host |
| 79 | if forwardedHost := c.Request().Header.Get("X-Forwarded-Host"); forwardedHost != "" { |
| 80 | host = forwardedHost |
| 81 | } else if fwdHost != "" { |
| 82 | host = fwdHost |
| 83 | } |
| 84 | |
| 85 | return scheme + "://" + host + BasePathPrefix(c) |
| 86 | } |
| 87 | |
| 88 | // firstToken returns the first comma-separated token of v, trimmed of spaces. |
| 89 | // Reverse-proxy chains can emit X-Forwarded-Proto as "https,http"; only the |
no test coverage detected