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.

51 lines
1.4 KiB

  1. #if NETFX
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Web;
  5. namespace Apewer.Web
  6. {
  7. /// <summary>通用 HTTP 模块。</summary>
  8. internal class UniversalModule : 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. void ByContext(object sender, EventArgs e)
  18. {
  19. var context = HttpContext.Current;
  20. if (context == null) return;
  21. var keys = new List<string>();
  22. keys.AddRange(context.Response.Headers.AllKeys);
  23. if (keys.Contains("Server")) context.Response.Headers.Remove("Server");
  24. if (keys.Contains("X-Powered-By")) context.Response.Headers.Remove("X-Powered-By");
  25. }
  26. void ByApplication(object sender, EventArgs e)
  27. {
  28. var app = sender as HttpApplication;
  29. if (app == null) return;
  30. if (app.Request == null) return;
  31. if (app.Request.IsLocal == false) return;
  32. if (app.Context == null) return;
  33. if (app.Context.Response == null) return;
  34. if (app.Context.Response.Headers == null) return;
  35. var headers = app.Context.Response.Headers;
  36. app.Context.Response.Headers.Remove("Server");
  37. }
  38. }
  39. }
  40. #endif