diff --git a/src/Configuration/AssetServerConfig.cs b/src/Configuration/AssetServerConfig.cs index ec78b3f..585cd92 100644 --- a/src/Configuration/AssetServerConfig.cs +++ b/src/Configuration/AssetServerConfig.cs @@ -4,6 +4,7 @@ public class AssetServerConfig { public string ListenIP { get; set; } = string.Empty; public int Port { get; set; } = 5001; public string URLPrefix { get; set; } = string.Empty; + public bool UseAnyURLPrefix { get; set; } = true; public AssetServerMode Mode { get; set; } public string ProviderURL { get; set; } = string.Empty; public string AutoEncryptRegexp { get; set; } = string.Empty; diff --git a/src/Middleware/AssetMiddleware.cs b/src/Middleware/AssetMiddleware.cs index ed59369..1a27280 100644 --- a/src/Middleware/AssetMiddleware.cs +++ b/src/Middleware/AssetMiddleware.cs @@ -34,12 +34,27 @@ public class AssetMiddleware { string path = context.Request.Path; - if (path is null || !string.IsNullOrEmpty(config.Value.URLPrefix) && !path.StartsWith("/" + config.Value.URLPrefix) || config.Value.Mode == AssetServerMode.None) { + if (path is null || config.Value.Mode == AssetServerMode.None) { context.Response.StatusCode = 400; return; } - string assetPath = path.Remove(0, config.Value.URLPrefix.Length + 1); + string assetPath; + if (config.Value.UseAnyURLPrefix) { + int firstSlash = path.IndexOf('/', 1); + if (firstSlash < 0) { + context.Response.StatusCode = 400; + return; + } + assetPath = path.Remove(0, firstSlash + 1); + } else { + if (!string.IsNullOrEmpty(config.Value.URLPrefix) && !path.StartsWith("/" + config.Value.URLPrefix)) { + context.Response.StatusCode = 400; + return; + } + assetPath = path.Remove(0, config.Value.URLPrefix.Length + 1); + } + string localPath = GetLocalPath("assets/" + assetPath); if (localPath == string.Empty && config.Value.Mode == AssetServerMode.Partial && config.Value.UseCache) diff --git a/src/appsettings.json b/src/appsettings.json index 349937c..b60629e 100644 --- a/src/appsettings.json +++ b/src/appsettings.json @@ -17,9 +17,15 @@ "// Port": "Listening port number for the asset server. Should be different than for the server API", "Port": 5001, - "// URLPrefix": "Extra prefix in the URL; omitted while retrieving the path from the requested URL. For example, if set to '.com', then a request to http://localhost/.com/abc will return the 'abc' file from the assets folder", + "// URLPrefix (1)": "Extra prefix in the URL; omitted while retrieving the path from the requested URL. See also: UseAnyURLPrefix setting.", + "// URLPrefix (2)": "For example, if set to '.com', then a request to 'http://localhost/.com/abc' will return the 'abc' file from the assets folder.", "URLPrefix": ".com", + "// UseAnyURLPrefix (1)": "Extended version of URLPrefix (if set to true, then URLPrefix setting will be ignored).", + "// UseAnyURLPrefix (2)": "When true requires that requested URL contained extra prefix (one directory at path start), that will be omitted while retrieving the path from the requested URL.", + "// UseAnyURLPrefix (3)": "If set to true, then (for example) a request to 'http://localhost/any.com/abc' will return the 'abc' file from the assets folder ('any.com' will be omitted).", + "UseAnyURLPrefix": true, + "// Mode": "Two modes: full - everything is local, partial - downloads assets from ProviderURL if not found locally", "Mode": "partial",