1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950 |
- using System.Text;
- namespace EVCB_OCPP.DBAPI.Middleware;
- public sealed class BodyRewindMiddleware
- {
- private readonly RequestDelegate _next;
- public BodyRewindMiddleware(RequestDelegate next)
- {
- _next = next;
- }
- public async Task Invoke(HttpContext context)
- {
- string body = await new StreamReader(context.Request.Body).ReadToEndAsync();
- context.Items.Add("PreloadBody", body);
- context.Request.Body = new MemoryStream(Encoding.UTF8.GetBytes(body));
- await _next(context);
- //using (var injectedRequestStream = new MemoryStream())
- //{
- // var bytesToWrite = System.Text.Encoding.UTF8.GetBytes(body);
- // injectedRequestStream.Write(bytesToWrite, 0, bytesToWrite.Length);
- // injectedRequestStream.Seek(0, SeekOrigin.Begin);
- // context.Request.Body = injectedRequestStream;
- // await _next(context);
- //}
- }
- }
- public static class BodyRewindExtensions
- {
- public static IApplicationBuilder EnableRequestBodyRewind(this IApplicationBuilder app)
- {
- if (app == null)
- {
- throw new ArgumentNullException(nameof(app));
- }
- return app.UseMiddleware<BodyRewindMiddleware>();
- }
- public static string GetPreLoadBody(this HttpContext httpContext)
- {
- return httpContext.Items["PreloadBody"] as string;
- }
- }
|