HttpsServer.cs 4.4 KB

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