ServerHeaderConcern.cs 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  5. using GenHTTP.Api.Content;
  6. using GenHTTP.Api.Protocol;
  7. namespace Benchmarks.Utilities
  8. {
  9. public sealed class ServerHeaderConcern : IConcern
  10. {
  11. #region Get-/Setters
  12. public IHandler Content { get; }
  13. public IHandler Parent { get; }
  14. #endregion
  15. #region Initialization
  16. public ServerHeaderConcern(IHandler parent, Func<IHandler, IHandler> contentFactory)
  17. {
  18. Parent = parent;
  19. Content = contentFactory(this);
  20. }
  21. #endregion
  22. #region Functionality
  23. public IAsyncEnumerable<ContentElement> GetContentAsync(IRequest request) => Content.GetContentAsync(request);
  24. public ValueTask PrepareAsync() => Content.PrepareAsync();
  25. public async ValueTask<IResponse> HandleAsync(IRequest request)
  26. {
  27. var response = await Content.HandleAsync(request);
  28. if (response != null)
  29. {
  30. response.Headers.Add("Server", "TFB");
  31. }
  32. return response;
  33. }
  34. #endregion
  35. }
  36. }