-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLocalMapHostServer.cs
More file actions
118 lines (103 loc) · 4.55 KB
/
Copy pathLocalMapHostServer.cs
File metadata and controls
118 lines (103 loc) · 4.55 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
110
111
112
113
114
115
116
117
118
using System.Net;
using System.Net.Sockets;
using System.Text;
namespace SquadTools;
internal sealed class LocalMapHostServer : IDisposable
{
private readonly string rootDirectory;
private readonly TcpListener listener;
private readonly CancellationTokenSource cancellation = new();
private readonly Task acceptLoop;
private LocalMapHostServer(string rootDirectory, TcpListener listener)
{
this.rootDirectory = Path.GetFullPath(rootDirectory).TrimEnd(Path.DirectorySeparatorChar) + Path.DirectorySeparatorChar;
this.listener = listener;
Port = ((IPEndPoint)listener.LocalEndpoint).Port;
acceptLoop = Task.Run(AcceptLoopAsync);
}
internal int Port { get; }
internal string BaseUrl => $"http://127.0.0.1:{Port}/";
internal static LocalMapHostServer Start(string rootDirectory)
{
if (!File.Exists(Path.Combine(rootDirectory, "index.html")))
{
throw new DirectoryNotFoundException($"本地地图工具资源不存在:{rootDirectory}");
}
TcpListener listener = new(IPAddress.Loopback, 0);
listener.Start();
return new LocalMapHostServer(rootDirectory, listener);
}
private async Task AcceptLoopAsync()
{
try
{
while (!cancellation.IsCancellationRequested)
{
TcpClient client = await listener.AcceptTcpClientAsync(cancellation.Token);
_ = HandleClientAsync(client);
}
}
catch (OperationCanceledException) { }
catch (ObjectDisposedException) { }
}
private async Task HandleClientAsync(TcpClient client)
{
using (client)
using (NetworkStream stream = client.GetStream())
{
try
{
byte[] buffer = new byte[16 * 1024];
int count = await stream.ReadAsync(buffer, cancellation.Token);
if (count == 0) return;
string[] requestLine = Encoding.ASCII.GetString(buffer, 0, count)
.Split("\r\n", 2, StringSplitOptions.None)[0]
.Split(' ', 3, StringSplitOptions.RemoveEmptyEntries);
if (requestLine.Length < 2 || !requestLine[0].Equals("GET", StringComparison.OrdinalIgnoreCase))
{
await WriteResponseAsync(stream, 405, "Method Not Allowed", "text/plain", [], cancellation.Token);
return;
}
string requestPath = requestLine[1].Split('?', 2)[0];
string relativePath = Uri.UnescapeDataString(requestPath.TrimStart('/'));
if (relativePath.Length == 0) relativePath = "index.html";
string fullPath = Path.GetFullPath(Path.Combine(rootDirectory, relativePath));
if (!fullPath.StartsWith(rootDirectory, StringComparison.OrdinalIgnoreCase) || !File.Exists(fullPath))
{
await WriteResponseAsync(stream, 404, "Not Found", "text/plain", [], cancellation.Token);
return;
}
byte[] content = await File.ReadAllBytesAsync(fullPath, cancellation.Token);
await WriteResponseAsync(stream, 200, "OK", GetContentType(fullPath), content, cancellation.Token);
}
catch (OperationCanceledException) { }
catch (IOException) { }
}
}
private static async Task WriteResponseAsync(NetworkStream stream, int code, string text, string type, byte[] content, CancellationToken token)
{
string headers = $"HTTP/1.1 {code} {text}\r\nContent-Type: {type}\r\nContent-Length: {content.Length}\r\nCache-Control: no-cache\r\nConnection: close\r\n\r\n";
await stream.WriteAsync(Encoding.ASCII.GetBytes(headers), token);
if (content.Length > 0) await stream.WriteAsync(content, token);
}
private static string GetContentType(string path) => Path.GetExtension(path).ToLowerInvariant() switch
{
".html" => "text/html; charset=utf-8",
".js" => "text/javascript; charset=utf-8",
".css" => "text/css; charset=utf-8",
".json" => "application/json; charset=utf-8",
".webp" => "image/webp",
".png" => "image/png",
".svg" => "image/svg+xml",
".mp3" => "audio/mpeg",
".ico" => "image/x-icon",
_ => "application/octet-stream"
};
public void Dispose()
{
cancellation.Cancel();
listener.Stop();
try { acceptLoop.Wait(TimeSpan.FromSeconds(2)); } catch (AggregateException) { }
cancellation.Dispose();
}
}