using System.Text.RegularExpressions;
using Microsoft.AspNetCore.Http;
namespace RdtClient.Service.Middleware;
public partial class BaseHrefMiddleware(RequestDelegate next, String basePath)
{
private readonly String _basePath = $"/{basePath.TrimStart('/').TrimEnd('/')}/";
[GeneratedRegex(@")")]
private partial Regex ScriptRegex();
[GeneratedRegex("()")]
private partial Regex LinkRegex();
public async Task InvokeAsync(HttpContext context)
{
var originalBody = context.Response.Body;
try
{
using var newBody = new MemoryStream();
context.Response.Body = newBody;
await next(context);
context.Response.Body = originalBody;
newBody.Seek(0, SeekOrigin.Begin);
var responseBody = await new StreamReader(newBody).ReadToEndAsync();
if (context.Response.StatusCode == 200)
{
if (context.Response.ContentType?.Contains("text/html") == true)
{
responseBody = BodyRegex().Replace(responseBody, @$"