1
1
using System . Collections . Concurrent ;
2
2
using System . Net ;
3
3
using System . Net . Sockets ;
4
+ using ColorDesktop . Api ;
4
5
using Microsoft . AspNetCore . Builder ;
5
6
using Microsoft . AspNetCore . Hosting ;
6
7
using Microsoft . AspNetCore . Http ;
11
12
12
13
namespace ColorDesktop . Web ;
13
14
14
- public static class HttpWeb
15
+ internal static class HttpWeb
15
16
{
16
- private static readonly ConcurrentDictionary < string , StaticFileOptions > s_dynamicFileOptions = [ ] ;
17
+ private static readonly ConcurrentDictionary < string , StaticFileOptions > s_file = [ ] ;
18
+ private static readonly ConcurrentDictionary < string , IHttpRoute > s_routes = [ ] ;
17
19
private static readonly FileExtensionContentTypeProvider s_typeProvider = new ( ) ;
18
20
19
21
private static WebApplication s_app ;
@@ -36,16 +38,11 @@ public static void Start()
36
38
37
39
var app = builder . Build ( ) ;
38
40
39
- //if (env.IsDevelopment())
40
- //{
41
- // app.UseDeveloperExceptionPage();
42
- //}
43
-
44
41
app . Use ( async ( context , next ) =>
45
42
{
46
43
string path = context . Request . Path . Value ! . Trim ( '/' ) ;
47
44
string file ;
48
- if ( context . Request . Cookies . TryGetValue ( "colordesktop" , out var uuid ) )
45
+ if ( context . Request . Cookies . TryGetValue ( "colordesktop" , out var uuid ) && path != uuid )
49
46
{
50
47
file = path ;
51
48
}
@@ -58,15 +55,24 @@ public static void Start()
58
55
file = "index.html" ;
59
56
}
60
57
}
58
+ if ( uuid == null || ! InstanceManager . Instances . TryGetValue ( uuid , out var key ) )
59
+ {
60
+ context . Response . StatusCode = 301 ;
61
+ await context . Response . WriteAsJsonAsync ( new { msg = "uuid error" } ) ;
62
+ return ;
63
+ }
61
64
62
- if ( context . Request . Method == "POST" )
65
+ if ( s_routes . TryGetValue ( key . Plugin , out var route ) )
63
66
{
64
- if ( uuid == null )
67
+ context . Response . Cookies . Append ( "colordesktop" , uuid ) ;
68
+ var res = await route . Process ( context ) ;
69
+ if ( res )
65
70
{
66
- context . Response . StatusCode = 301 ;
67
- await context . Response . WriteAsJsonAsync ( new { msg = "uuid error" } ) ;
68
71
return ;
69
72
}
73
+ }
74
+ if ( context . Request . Method == "POST" )
75
+ {
70
76
if ( file == "getConfig" )
71
77
{
72
78
if ( ! context . Request . Form . TryGetValue ( "file" , out var file1 ) )
@@ -79,6 +85,7 @@ public static void Start()
79
85
80
86
context . Response . Cookies . Append ( "colordesktop" , uuid ) ;
81
87
context . Response . ContentType = "application/json" ;
88
+ context . Response . StatusCode = 200 ;
82
89
83
90
if ( File . Exists ( file2 ) )
84
91
{
@@ -118,29 +125,27 @@ public static void Start()
118
125
await context . Response . WriteAsJsonAsync ( new { msg = "ok" } ) ;
119
126
}
120
127
}
121
- else if ( InstanceManager . Instances . TryGetValue ( uuid , out var key ) )
128
+ else if ( s_file . TryGetValue ( key . Plugin , out var options ) )
122
129
{
123
- if ( s_dynamicFileOptions . TryGetValue ( key . Plugin , out var options ) )
124
- {
125
- var fileProvider = options . FileProvider ! ;
130
+ var fileProvider = options . FileProvider ! ;
126
131
127
- var fileInfo = fileProvider . GetFileInfo ( file ) ;
132
+ var fileInfo = fileProvider . GetFileInfo ( file ) ;
128
133
129
- if ( fileInfo . Exists )
134
+ if ( fileInfo . Exists )
135
+ {
136
+ if ( ! s_typeProvider . TryGetContentType ( fileInfo . Name , out var contentType ) )
130
137
{
131
- if ( ! s_typeProvider . TryGetContentType ( fileInfo . Name , out var contentType ) )
132
- {
133
- contentType = "application/octet-stream" ;
134
- }
135
-
136
- context . Response . Cookies . Append ( "colordesktop" , uuid ) ;
137
- context . Response . ContentType = contentType ;
138
- using var stream = fileInfo . CreateReadStream ( ) ;
139
- await stream . CopyToAsync ( context . Response . Body ) ;
140
- return ;
138
+ contentType = "application/octet-stream" ;
141
139
}
140
+
141
+ context . Response . Cookies . Append ( "colordesktop" , uuid ) ;
142
+ context . Response . ContentType = contentType ;
143
+ using var stream = fileInfo . CreateReadStream ( ) ;
144
+ await stream . CopyToAsync ( context . Response . Body ) ;
145
+ return ;
142
146
}
143
147
}
148
+
144
149
await next ( ) ;
145
150
} ) ;
146
151
@@ -160,7 +165,27 @@ public static void AddPlugin(string id, string dir)
160
165
FileProvider = fileProvider ,
161
166
RequestPath = "/" + folderName
162
167
} ;
163
- s_dynamicFileOptions . TryAdd ( id , staticFileOptions ) ;
168
+ s_file . TryAdd ( id , staticFileOptions ) ;
169
+ }
170
+
171
+ public static void AddRoute ( IPlugin plugin , IHttpRoute route )
172
+ {
173
+ var id = LauncherApi . Hook . GetPluginId ( plugin ) ;
174
+ if ( id == null )
175
+ {
176
+ return ;
177
+ }
178
+ s_routes [ id ] = route ;
179
+ }
180
+
181
+ public static void RemoveRoute ( IPlugin plugin )
182
+ {
183
+ var id = LauncherApi . Hook . GetPluginId ( plugin ) ;
184
+ if ( id == null )
185
+ {
186
+ return ;
187
+ }
188
+ s_routes . Remove ( id , out _ ) ;
164
189
}
165
190
166
191
private static int GetAvailablePort ( )
@@ -176,4 +201,9 @@ public static void Stop()
176
201
{
177
202
s_app . StopAsync ( ) ;
178
203
}
204
+
205
+ public static void Clear ( )
206
+ {
207
+ s_file . Clear ( ) ;
208
+ }
179
209
}
0 commit comments