QueryStringDecodingMiddleware.cs 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. using System.Threading.Tasks;
  2. using Microsoft.AspNetCore.Http;
  3. using Microsoft.AspNetCore.Http.Features;
  4. namespace Jellyfin.Api.Middleware;
  5. /// <summary>
  6. /// URL decodes the querystring before binding.
  7. /// </summary>
  8. public class QueryStringDecodingMiddleware
  9. {
  10. private readonly RequestDelegate _next;
  11. /// <summary>
  12. /// Initializes a new instance of the <see cref="QueryStringDecodingMiddleware"/> class.
  13. /// </summary>
  14. /// <param name="next">The next delegate in the pipeline.</param>
  15. public QueryStringDecodingMiddleware(RequestDelegate next)
  16. {
  17. _next = next;
  18. }
  19. /// <summary>
  20. /// Executes the middleware action.
  21. /// </summary>
  22. /// <param name="httpContext">The current HTTP context.</param>
  23. /// <returns>The async task.</returns>
  24. public async Task Invoke(HttpContext httpContext)
  25. {
  26. var feature = httpContext.Features.Get<IQueryFeature>();
  27. if (feature is not null)
  28. {
  29. httpContext.Features.Set<IQueryFeature>(new UrlDecodeQueryFeature(feature));
  30. }
  31. await _next(httpContext).ConfigureAwait(false);
  32. }
  33. }