From ebdd85e0f8145867e621e928e851537b5b42c335 Mon Sep 17 00:00:00 2001 From: Rory& Date: Tue, 27 Feb 2024 22:07:15 +0100 Subject: Fix bug with URL encoding --- MatrixMediaGate/Program.cs | 15 +++++---------- MatrixMediaGate/appsettings.Development.json | 2 +- 2 files changed, 6 insertions(+), 11 deletions(-) (limited to 'MatrixMediaGate') diff --git a/MatrixMediaGate/Program.cs b/MatrixMediaGate/Program.cs index ec4e755..658ef89 100644 --- a/MatrixMediaGate/Program.cs +++ b/MatrixMediaGate/Program.cs @@ -1,13 +1,12 @@ -using System.Net; using System.Net.Http.Headers; using System.Text.Json; using MatrixMediaGate; using MatrixMediaGate.Services; +using Microsoft.AspNetCore.Http.Extensions; var builder = WebApplication.CreateBuilder(args); builder.Host.UseSystemd(); builder.Services.AddSingleton(); -builder.Services.AddSingleton(); builder.Services.AddSingleton(); builder.Services.AddSingleton(services => { var cfg = services.GetRequiredService(); @@ -20,11 +19,9 @@ builder.Services.AddSingleton(services => { var app = builder.Build(); async Task Proxy(HttpClient hc, ProxyConfiguration cfg, HttpContext ctx, ILogger logger) { - var path = ctx.Request.Path.Value; - if (path is null) return; + var path = ctx.Request.GetEncodedPathAndQuery(); if (path.StartsWith('/')) path = path[1..]; - path += ctx.Request.QueryString.Value; var method = new HttpMethod(ctx.Request.Method); using var req = new HttpRequestMessage(method, path); @@ -41,7 +38,7 @@ async Task Proxy(HttpClient hc, ProxyConfiguration cfg, HttpContext ctx, ILogger if (ctx.Request.ContentLength != null) req.Content.Headers.ContentLength = ctx.Request.ContentLength; } - logger.LogInformation($"Proxying {method} {path} to {hc.BaseAddress}{path}"); + logger.LogInformation("Proxying {method} {path} to {target}", method, path, hc.BaseAddress + path); using var response = await hc.SendAsync(req, HttpCompletionOption.ResponseHeadersRead); ctx.Response.Headers.Clear(); @@ -62,7 +59,6 @@ async Task Proxy(HttpClient hc, ProxyConfiguration cfg, HttpContext ctx, ILogger async Task ProxyMaybeAuth(HttpClient hc, ProxyConfiguration cfg, AuthValidator auth, HttpContext ctx, ILogger logger) { await auth.UpdateAuth(ctx); - await Proxy(hc, cfg, ctx, logger); } @@ -75,8 +71,7 @@ async Task ProxyMedia(string serverName, ProxyConfiguration cfg, HttpClient hc, ctx.Response.StatusCode = 403; ctx.Response.ContentType = "application/json"; await ctx.Response.StartAsync(); - var json = JsonSerializer.Serialize(new { errcode = "M_FORBIDDEN", error = "Unauthenticated access to remote media has been disabled on this server." }); - await ctx.Response.WriteAsync(json); + await JsonSerializer.SerializeAsync(ctx.Response.Body, new { errcode = "M_FORBIDDEN", error = "Unauthenticated access to remote media has been disabled on this server." }); await ctx.Response.Body.FlushAsync(); await ctx.Response.CompleteAsync(); } @@ -92,4 +87,4 @@ foreach (var route in (string[]) [ // Require recent auth for these routes ]) app.Map(route, ProxyMedia); -app.Run(); +app.Run(); \ No newline at end of file diff --git a/MatrixMediaGate/appsettings.Development.json b/MatrixMediaGate/appsettings.Development.json index 2763a9a..56b83e6 100644 --- a/MatrixMediaGate/appsettings.Development.json +++ b/MatrixMediaGate/appsettings.Development.json @@ -2,7 +2,7 @@ "Logging": { "LogLevel": { "Default": "Information", - "Microsoft.AspNetCore": "Information", + "Microsoft.AspNetCore": "Warning", "Microsoft.AspNetCore.Routing": "Warning", "Microsoft.AspNetCore.Mvc": "Warning" } -- cgit 1.5.1