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

Function BaseURL

core/http/middleware/baseurl.go:57–86  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

55// The returned URL is guaranteed to end with `/`.
56// The method should be used in conjunction with the StripPathPrefix middleware.
57func 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

Callers 13

notFoundHandlerFunction · 0.92
RegisterLocalAIRoutesFunction · 0.92
DashboardFunction · 0.92
ImageEndpointFunction · 0.92
InpaintingEndpointFunction · 0.92
WelcomeEndpointFunction · 0.92
ApplyBackendEndpointMethod · 0.92
DeleteBackendEndpointMethod · 0.92
VideoEndpointFunction · 0.92

Calls 5

BasePathPrefixFunction · 0.85
parseForwardedFunction · 0.85
firstTokenFunction · 0.85
GetMethod · 0.65
RequestMethod · 0.65

Tested by

no test coverage detected