HttpServer.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. using System;
  2. using System.Net;
  3. using System.IO;
  4. namespace NetCoreServer
  5. {
  6. /// <summary>
  7. /// HTTP server is used to create HTTP Web server and communicate with clients using HTTP protocol. It allows to receive GET, POST, PUT, DELETE requests and send HTTP responses.
  8. /// </summary>
  9. /// <remarks>Thread-safe.</remarks>
  10. public class HttpServer : TcpServer
  11. {
  12. /// <summary>
  13. /// Initialize HTTP server with a given IP address and port number
  14. /// </summary>
  15. /// <param name="address">IP address</param>
  16. /// <param name="port">Port number</param>
  17. public HttpServer(IPAddress address, int port) : base(address, port) { Cache = new FileCache(); }
  18. /// <summary>
  19. /// Initialize HTTP server with a given IP address and port number
  20. /// </summary>
  21. /// <param name="address">IP address</param>
  22. /// <param name="port">Port number</param>
  23. public HttpServer(string address, int port) : base(address, port) { Cache = new FileCache(); }
  24. /// <summary>
  25. /// Initialize HTTP server with a given DNS endpoint
  26. /// </summary>
  27. /// <param name="endpoint">DNS endpoint</param>
  28. public HttpServer(DnsEndPoint endpoint) : base(endpoint) { Cache = new FileCache(); }
  29. /// <summary>
  30. /// Initialize HTTP server with a given IP endpoint
  31. /// </summary>
  32. /// <param name="endpoint">IP endpoint</param>
  33. public HttpServer(IPEndPoint endpoint) : base(endpoint) { Cache = new FileCache(); }
  34. /// <summary>
  35. /// Get the static content cache
  36. /// </summary>
  37. public FileCache Cache { get; }
  38. /// <summary>
  39. /// Add static content cache
  40. /// </summary>
  41. /// <param name="path">Static content path</param>
  42. /// <param name="prefix">Cache prefix (default is "/")</param>
  43. /// <param name="filter">Cache filter (default is "*.*")</param>
  44. /// <param name="timeout">Refresh cache timeout (default is 1 hour)</param>
  45. public void AddStaticContent(string path, string prefix = "/", string filter = "*.*", TimeSpan? timeout = null)
  46. {
  47. timeout ??= TimeSpan.FromHours(1);
  48. bool Handler(FileCache cache, string key, byte[] value, TimeSpan timespan)
  49. {
  50. var response = new HttpResponse();
  51. response.SetBegin(200);
  52. response.SetContentType(Path.GetExtension(key));
  53. response.SetHeader("Cache-Control", $"max-age={timespan.Seconds}");
  54. response.SetBody(value);
  55. return cache.Add(key, response.Cache.Data, timespan);
  56. }
  57. Cache.InsertPath(path, prefix, filter, timeout.Value, Handler);
  58. }
  59. /// <summary>
  60. /// Remove static content cache
  61. /// </summary>
  62. /// <param name="path">Static content path</param>
  63. public void RemoveStaticContent(string path) { Cache.RemovePath(path); }
  64. /// <summary>
  65. /// Clear static content cache
  66. /// </summary>
  67. public void ClearStaticContent() { Cache.Clear(); }
  68. protected override TcpSession CreateSession() { return new HttpSession(this); }
  69. #region IDisposable implementation
  70. // Disposed flag.
  71. private bool _disposed;
  72. protected override void Dispose(bool disposingManagedResources)
  73. {
  74. if (!_disposed)
  75. {
  76. if (disposingManagedResources)
  77. {
  78. // Dispose managed resources here...
  79. Cache.Dispose();
  80. }
  81. // Dispose unmanaged resources here...
  82. // Set large fields to null here...
  83. // Mark as disposed.
  84. _disposed = true;
  85. }
  86. // Call Dispose in the base class.
  87. base.Dispose(disposingManagedResources);
  88. }
  89. // The derived class does not have a Finalize method
  90. // or a Dispose method without parameters because it inherits
  91. // them from the base class.
  92. #endregion
  93. }
  94. }