MCPcopy
hub / github.com/mudler/LocalAI / BasePathPrefix

Function BasePathPrefix

core/http/middleware/baseurl.go:24–51  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

22// "//evil.com" turns the asset rewrite into a protocol-relative URL that
23// loads JS from a foreign origin.
24func 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.

Callers 1

BaseURLFunction · 0.85

Calls 3

SafeForwardedPrefixFunction · 0.85
RequestMethod · 0.65
GetMethod · 0.65

Tested by

no test coverage detected