QueryStringDecodingMiddleware.cs 1.2 KB

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