Wrap application in baseurl

pull/4035/head
crobibero 4 years ago
parent 7504f067ef
commit 4e52fe1060

@ -44,11 +44,7 @@ namespace Jellyfin.Server.Middleware
var localPath = httpContext.Request.Path.ToString(); var localPath = httpContext.Request.Path.ToString();
var baseUrlPrefix = serverConfigurationManager.Configuration.BaseUrl; var baseUrlPrefix = serverConfigurationManager.Configuration.BaseUrl;
if (string.Equals(localPath, baseUrlPrefix + "/", StringComparison.OrdinalIgnoreCase) if (!localPath.StartsWith(baseUrlPrefix, StringComparison.OrdinalIgnoreCase))
|| string.Equals(localPath, baseUrlPrefix, StringComparison.OrdinalIgnoreCase)
|| string.Equals(localPath, "/", StringComparison.OrdinalIgnoreCase)
|| string.IsNullOrEmpty(localPath)
|| !localPath.StartsWith(baseUrlPrefix, StringComparison.OrdinalIgnoreCase))
{ {
// Always redirect back to the default path if the base prefix is invalid or missing // Always redirect back to the default path if the base prefix is invalid or missing
_logger.LogDebug("Normalizing an URL at {LocalPath}", localPath); _logger.LogDebug("Normalizing an URL at {LocalPath}", localPath);

@ -88,71 +88,78 @@ namespace Jellyfin.Server
/// </summary> /// </summary>
/// <param name="app">The application builder.</param> /// <param name="app">The application builder.</param>
/// <param name="env">The webhost environment.</param> /// <param name="env">The webhost environment.</param>
/// <param name="serverApplicationHost">The server application host.</param>
/// <param name="appConfig">The application config.</param> /// <param name="appConfig">The application config.</param>
public void Configure( public void Configure(
IApplicationBuilder app, IApplicationBuilder app,
IWebHostEnvironment env, IWebHostEnvironment env,
IServerApplicationHost serverApplicationHost,
IConfiguration appConfig) IConfiguration appConfig)
{ {
if (env.IsDevelopment()) // Only add base url redirection if a base url is set.
if (!string.IsNullOrEmpty(_serverConfigurationManager.Configuration.BaseUrl))
{ {
app.UseDeveloperExceptionPage(); app.UseBaseUrlRedirection();
} }
app.UseMiddleware<ExceptionMiddleware>(); // Wrap rest of configuration so everything only listens on BaseUrl.
app.Map(_serverConfigurationManager.Configuration.BaseUrl, mainApp =>
{
if (env.IsDevelopment())
{
mainApp.UseDeveloperExceptionPage();
}
app.UseMiddleware<ResponseTimeMiddleware>(); mainApp.UseMiddleware<ExceptionMiddleware>();
app.UseWebSockets(); mainApp.UseMiddleware<ResponseTimeMiddleware>();
app.UseResponseCompression(); mainApp.UseWebSockets();
app.UseCors(ServerCorsPolicy.DefaultPolicyName); mainApp.UseResponseCompression();
if (_serverConfigurationManager.Configuration.RequireHttps mainApp.UseCors(ServerCorsPolicy.DefaultPolicyName);
&& _serverApplicationHost.ListenWithHttps)
{
app.UseHttpsRedirection();
}
app.UseStaticFiles(); if (_serverConfigurationManager.Configuration.RequireHttps
app.UsePathBase(_serverConfigurationManager.Configuration.BaseUrl); && _serverApplicationHost.ListenWithHttps)
if (appConfig.HostWebClient())
{
app.UseStaticFiles(new StaticFileOptions
{ {
FileProvider = new PhysicalFileProvider(_serverConfigurationManager.ApplicationPaths.WebPath), mainApp.UseHttpsRedirection();
RequestPath = "/web" }
});
}
app.UseAuthentication();
app.UseJellyfinApiSwagger(_serverConfigurationManager);
app.UseRouting();
app.UseAuthorization();
if (_serverConfigurationManager.Configuration.EnableMetrics)
{
// Must be registered after any middleware that could change HTTP response codes or the data will be bad
app.UseHttpMetrics();
}
app.UseLanFiltering(); mainApp.UsePathBase(_serverConfigurationManager.Configuration.BaseUrl);
app.UseIpBasedAccessValidation(); mainApp.UseStaticFiles();
app.UseBaseUrlRedirection(); if (appConfig.HostWebClient())
app.UseWebSocketHandler(); {
app.UseServerStartupMessage(); mainApp.UseStaticFiles(new StaticFileOptions
{
FileProvider = new PhysicalFileProvider(_serverConfigurationManager.ApplicationPaths.WebPath),
RequestPath = "/web"
});
}
app.UseEndpoints(endpoints => mainApp.UseAuthentication();
{ mainApp.UseJellyfinApiSwagger(_serverConfigurationManager);
endpoints.MapControllers(); mainApp.UseRouting();
mainApp.UseAuthorization();
if (_serverConfigurationManager.Configuration.EnableMetrics) if (_serverConfigurationManager.Configuration.EnableMetrics)
{ {
endpoints.MapMetrics(_serverConfigurationManager.Configuration.BaseUrl.TrimStart('/') + "/metrics"); // Must be registered after any middleware that could change HTTP response codes or the data will be bad
mainApp.UseHttpMetrics();
} }
endpoints.MapHealthChecks(_serverConfigurationManager.Configuration.BaseUrl.TrimStart('/') + "/health"); mainApp.UseLanFiltering();
mainApp.UseIpBasedAccessValidation();
mainApp.UseWebSocketHandler();
mainApp.UseServerStartupMessage();
mainApp.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
if (_serverConfigurationManager.Configuration.EnableMetrics)
{
endpoints.MapMetrics("/metrics");
}
endpoints.MapHealthChecks("/health");
});
}); });
// Add type descriptor for legacy datetime parsing. // Add type descriptor for legacy datetime parsing.

Loading…
Cancel
Save