diff options
| author | JP Dillingham <jpdillingham@gmail.com> | 2022-05-14 21:29:11 -0500 |
|---|---|---|
| committer | JP Dillingham <jpdillingham@gmail.com> | 2022-05-14 21:29:11 -0500 |
| commit | dbf354f0b5cd4c210977ddc593683ea164abee60 (patch) | |
| tree | ae2d788a6713e9bd26d2496a885ac1ae3f555bcf /src | |
| parent | 1cd8b1d11df6e2ef4377ef2c11a01ec0462a2dd4 (diff) | |
| parent | a33df63a0a23eeb1fae500f104c621e567b80f61 (diff) | |
| download | slskd-dbf354f0b5cd4c210977ddc593683ea164abee60.tar.gz | |
Merge remote-tracking branch 'origin/master' into search-list
Diffstat (limited to 'src')
| -rw-r--r-- | src/slskd/Common/Middleware/HTMLInjectionMiddleware.cs | 22 | ||||
| -rw-r--r-- | src/slskd/Common/Middleware/HTMLRewriteMiddleware.cs | 21 | ||||
| -rw-r--r-- | src/slskd/Options.cs | 2 | ||||
| -rw-r--r-- | src/slskd/Shares/SharedFileCache.cs | 12 |
4 files changed, 37 insertions, 20 deletions
diff --git a/src/slskd/Common/Middleware/HTMLInjectionMiddleware.cs b/src/slskd/Common/Middleware/HTMLInjectionMiddleware.cs index 217b2bce..3e2c003a 100644 --- a/src/slskd/Common/Middleware/HTMLInjectionMiddleware.cs +++ b/src/slskd/Common/Middleware/HTMLInjectionMiddleware.cs @@ -69,16 +69,14 @@ namespace slskd { context.Request.Headers.TryGetValue("accept", out var accept); var requestedTypes = accept.ToString().Split(','); - var injectableTypes = new[] { "text/html", "application/xhtml + xml", "application/xml" }; - - var isApiRoute = context.Request.Path.ToString().StartsWith("/api"); - var isGET = context.Request.Method == "GET"; - var isInjectableType = requestedTypes .Intersect(injectableTypes, StringComparer.InvariantCultureIgnoreCase) .Any(); + var isApiRoute = context.Request.Path.ToString().StartsWith("/api"); + var isGET = context.Request.Method == "GET"; + if (!isApiRoute && isGET && isInjectableType) { var originalStream = context.Response.Body; @@ -93,15 +91,25 @@ namespace slskd if (context.Response.StatusCode == 200) { // something downstream responded with a 200, meaning there's data in the body - // we need to read it, so we can reset then play it back with the appended HTML + // we need to read it, so we can reset then play it back with the modified HTML context.Response.Body.Seek(0, SeekOrigin.Begin); var body = await new StreamReader(context.Response.Body).ReadToEndAsync(); + var headers = context.Response.Headers + .ToDictionary(kvp => kvp.Key, kvp => kvp.Value) + .Where(kvp => !string.Equals(kvp.Key, "Content-Length", StringComparison.InvariantCultureIgnoreCase)); + context.Response.Clear(); + + foreach (var header in headers) + { + context.Response.Headers.Add(header); + } + await context.Response.WriteAsync(body + HTML); } - // rewind the stream we injected to the beginning, then replay the data to the + // rewind the stream we injected to the beginning, then replay the data to the // original stream stream.Seek(0, SeekOrigin.Begin); await stream.CopyToAsync(originalStream); diff --git a/src/slskd/Common/Middleware/HTMLRewriteMiddleware.cs b/src/slskd/Common/Middleware/HTMLRewriteMiddleware.cs index a15ba1e0..4b90d6da 100644 --- a/src/slskd/Common/Middleware/HTMLRewriteMiddleware.cs +++ b/src/slskd/Common/Middleware/HTMLRewriteMiddleware.cs @@ -74,16 +74,14 @@ namespace slskd { context.Request.Headers.TryGetValue("accept", out var accept); var requestedTypes = accept.ToString().Split(','); - - var rewriteableType = new[] { "text/html", "application/xhtml + xml", "application/xml" }; + var injectableTypes = new[] { "text/html", "application/xhtml + xml", "application/xml" }; + var isRewriteableType = requestedTypes + .Intersect(injectableTypes, StringComparer.InvariantCultureIgnoreCase) + .Any(); var isApiRoute = context.Request.Path.ToString().StartsWith("/api"); var isGET = context.Request.Method == "GET"; - var isRewriteableType = requestedTypes - .Intersect(rewriteableType, StringComparer.InvariantCultureIgnoreCase) - .Any(); - if (!isApiRoute && isGET && isRewriteableType) { var originalStream = context.Response.Body; @@ -98,12 +96,21 @@ namespace slskd if (context.Response.StatusCode == 200) { // something downstream responded with a 200, meaning there's data in the body - // we need to read it, so we can reset then play it back with the appended HTML + // we need to read it, so we can reset then play it back with the modified HTML context.Response.Body.Seek(0, SeekOrigin.Begin); var body = await new StreamReader(context.Response.Body).ReadToEndAsync(); + var headers = context.Response.Headers + .ToDictionary(kvp => kvp.Key, kvp => kvp.Value) + .Where(kvp => !string.Equals(kvp.Key, "Content-Length", StringComparison.InvariantCultureIgnoreCase)); + context.Response.Clear(); + foreach (var header in headers) + { + context.Response.Headers.Add(header); + } + await context.Response.WriteAsync(Regex.Replace(body, Pattern, Replacement)); } diff --git a/src/slskd/Options.cs b/src/slskd/Options.cs index a1493ca4..a8c3380c 100644 --- a/src/slskd/Options.cs +++ b/src/slskd/Options.cs @@ -342,7 +342,7 @@ namespace slskd bool IsRoot((string Raw, string Mask, string Alias, string Path) share) => share.Path == "/" || share.Path == "\\" || Path.GetPathRoot(share.Path) == share.Path; // starts with '/', 'X:\', or '\\' - bool IsAbsolutePath(string share) => Regex.IsMatch(share.ToLocalOSPath(), @"^(\[.*\])?(\/|[a-zA-Z]:\\|\\\\).*$"); + bool IsAbsolutePath(string share) => Regex.IsMatch(share.ToLocalOSPath(), @"^(!|-){0,1}(\[.*\])?(\/|[a-zA-Z]:\\|\\\\).*$"); var relativePaths = Shared.Where(share => !IsAbsolutePath(share)); foreach (var relativePath in relativePaths) diff --git a/src/slskd/Shares/SharedFileCache.cs b/src/slskd/Shares/SharedFileCache.cs index 3e305f08..ac996fa6 100644 --- a/src/slskd/Shares/SharedFileCache.cs +++ b/src/slskd/Shares/SharedFileCache.cs @@ -199,11 +199,13 @@ namespace slskd.Shares Log.Warning("Aborting shared file scan; no shares configured."); } - foreach (var share in Shares) - { - Log.Information($"Sharing {share.LocalPath} as {share.RemotePath}"); - Log.Debug($"Raw: {share.Raw}, Alias: {share.Alias}, Mask: {share.Mask}, Remote: {share.RemotePath})"); - } + Shares.ForEach(s => Log.Debug(s.ToJson())); + + Shares.Where(s => !s.IsExcluded).ToList() + .ForEach(s => Log.Information("Sharing {Local} as {Remote}", s.LocalPath, s.RemotePath)); + + Shares.Where(s => s.IsExcluded).ToList() + .ForEach(s => Log.Information("Excluding {Local}", s.LocalPath)); Log.Debug("Enumerating shared directories"); swSnapshot = sw.ElapsedMilliseconds; |
