forked from chronoxor/NetCoreServer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHttpServer.cs
More file actions
109 lines (94 loc) · 4.05 KB
/
Copy pathHttpServer.cs
File metadata and controls
109 lines (94 loc) · 4.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
using System;
using System.Net;
using System.IO;
namespace NetCoreServer
{
/// <summary>
/// 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.
/// </summary>
/// <remarks>Thread-safe.</remarks>
public class HttpServer : TcpServer
{
/// <summary>
/// Initialize HTTP server with a given IP address and port number
/// </summary>
/// <param name="address">IP address</param>
/// <param name="port">Port number</param>
public HttpServer(IPAddress address, int port) : base(address, port) { Cache = new FileCache(); }
/// <summary>
/// Initialize HTTP server with a given IP address and port number
/// </summary>
/// <param name="address">IP address</param>
/// <param name="port">Port number</param>
public HttpServer(string address, int port) : base(address, port) { Cache = new FileCache(); }
/// <summary>
/// Initialize HTTP server with a given DNS endpoint
/// </summary>
/// <param name="endpoint">DNS endpoint</param>
public HttpServer(DnsEndPoint endpoint) : base(endpoint) { Cache = new FileCache(); }
/// <summary>
/// Initialize HTTP server with a given IP endpoint
/// </summary>
/// <param name="endpoint">IP endpoint</param>
public HttpServer(IPEndPoint endpoint) : base(endpoint) { Cache = new FileCache(); }
/// <summary>
/// Get the static content cache
/// </summary>
public FileCache Cache { get; }
/// <summary>
/// Add static content cache
/// </summary>
/// <param name="path">Static content path</param>
/// <param name="prefix">Cache prefix (default is "/")</param>
/// <param name="filter">Cache filter (default is "*.*")</param>
/// <param name="timeout">Refresh cache timeout (default is 1 hour)</param>
public void AddStaticContent(string path, string prefix = "/", string filter = "*.*", TimeSpan? timeout = null)
{
timeout ??= TimeSpan.FromHours(1);
bool Handler(FileCache cache, string key, byte[] value, TimeSpan timespan)
{
var response = new HttpResponse();
response.SetBegin(200);
response.SetContentType(Path.GetExtension(key));
response.SetHeader("Cache-Control", $"max-age={timespan.Seconds}");
response.SetBody(value);
return cache.Add(key, response.Cache.Data, timespan);
}
Cache.InsertPath(path, prefix, filter, timeout.Value, Handler);
}
/// <summary>
/// Remove static content cache
/// </summary>
/// <param name="path">Static content path</param>
public void RemoveStaticContent(string path) { Cache.RemovePath(path); }
/// <summary>
/// Clear static content cache
/// </summary>
public void ClearStaticContent() { Cache.Clear(); }
protected override TcpSession CreateSession() { return new HttpSession(this); }
#region IDisposable implementation
// Disposed flag.
private bool _disposed;
protected override void Dispose(bool disposingManagedResources)
{
if (!_disposed)
{
if (disposingManagedResources)
{
// Dispose managed resources here...
Cache.Dispose();
}
// Dispose unmanaged resources here...
// Set large fields to null here...
// Mark as disposed.
_disposed = true;
}
// Call Dispose in the base class.
base.Dispose(disposingManagedResources);
}
// The derived class does not have a Finalize method
// or a Dispose method without parameters because it inherits
// them from the base class.
#endregion
}
}