From 2f4e8a02c27dbf36daa57e3abaab529ab6dd278a Mon Sep 17 00:00:00 2001 From: Rory& Date: Thu, 29 Feb 2024 09:39:11 +0000 Subject: Dump content --- MatrixMediaGate/Program.cs | 31 ++++++++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/MatrixMediaGate/Program.cs b/MatrixMediaGate/Program.cs index 5f39e22..1785434 100644 --- a/MatrixMediaGate/Program.cs +++ b/MatrixMediaGate/Program.cs @@ -132,12 +132,37 @@ async Task ProxyDump(ProxyConfiguration cfg, HttpContext ctx, HttpRequestMessage Directory.CreateDirectory(dir); var path = Path.Combine(dir, $"{(int)resp?.StatusCode}-{DateTimeOffset.UtcNow.ToUnixTimeSeconds()}-{ctx.Request.GetEncodedPathAndQuery().Replace('/', '_')}.json"); await using var file = File.Create(path); + + //collect data + JsonObject? requestJsonContent = null; + try { + requestJsonContent = await ctx.Request.ReadFromJsonAsync(); + } + catch (Exception) { + // ignored, we might not have a request body... + } + + JsonObject? responseJsonContent = null; + string? responseRawContent = null; + try { + responseJsonContent = await resp.Content.ReadFromJsonAsync(); + } + catch (Exception) { + try { + responseRawContent = await resp.Content.ReadAsStringAsync(); + } + catch (Exception) { + // ignored, we might not have a response body... + } + } + await JsonSerializer.SerializeAsync(file, new { Self = new { Request = new { ctx.Request.Method, Url = ctx.Request.GetEncodedUrl(), - ctx.Request.Headers + ctx.Request.Headers, + JsonContent = requestJsonContent }, Response = new { ctx.Response.StatusCode, @@ -157,8 +182,8 @@ async Task ProxyDump(ProxyConfiguration cfg, HttpContext ctx, HttpRequestMessage resp.Headers, resp.Content.Headers.ContentType, resp.Content.Headers.ContentLength, - JsonContent = resp.Content.Headers.ContentType?.MediaType == "application/json" ? await resp.Content.ReadFromJsonAsync() : null, - TextContent = resp.Content.Headers.ContentType?.MediaType != "application/json" ? await resp.Content.ReadAsStringAsync() : null + JsonContent = responseJsonContent, + TextContent = responseRawContent } }, Exception = new { -- cgit 1.5.1