You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

53 lines
1.5 KiB

  1. #if NETFRAMEWORK
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Web;
  5. namespace Apewer.Web.HttpProxy
  6. {
  7. /// <summary></summary>
  8. sealed class HttpModule : IHttpModule
  9. {
  10. /// <summary></summary>
  11. public void Dispose() { }
  12. /// <summary></summary>
  13. public void Init(HttpApplication context)
  14. {
  15. context.PreSendRequestHeaders += new EventHandler(ByContext);
  16. }
  17. /// <summary></summary>
  18. void ByContext(object sender, EventArgs e)
  19. {
  20. var context = HttpContext.Current;
  21. if (context == null) return;
  22. var keys = new List<string>();
  23. keys.AddRange(context.Response.Headers.AllKeys);
  24. if (keys.Contains("Server")) context.Response.Headers.Remove("Server");
  25. if (keys.Contains("X-Powered-By")) context.Response.Headers.Remove("X-Powered-By");
  26. }
  27. /// <summary></summary>
  28. void ByApplication(object sender, EventArgs e)
  29. {
  30. var app = sender as HttpApplication;
  31. if (app == null) return;
  32. if (app.Request == null) return;
  33. if (app.Request.IsLocal == false) return;
  34. if (app.Context == null) return;
  35. if (app.Context.Response == null) return;
  36. if (app.Context.Response.Headers == null) return;
  37. var headers = app.Context.Response.Headers;
  38. app.Context.Response.Headers.Remove("Server");
  39. }
  40. }
  41. }
  42. #endif