@@ -17,6 +17,7 @@ public class SimpleServer
1717 private readonly IDictionary < string , Action < HttpRequest > > _requestSubscribers ;
1818 private readonly IDictionary < string , RequestDelegate > _routes ;
1919 private readonly IDictionary < string , ( string username , string password ) > _auths ;
20+ private readonly IDictionary < string , string > _csp ;
2021 private readonly IWebHost _webHost ;
2122
2223 public static SimpleServer Create ( int port , string contentRoot ) => new SimpleServer ( port , contentRoot , isHttps : false ) ;
@@ -27,6 +28,7 @@ public SimpleServer(int port, string contentRoot, bool isHttps)
2728 _requestSubscribers = new ConcurrentDictionary < string , Action < HttpRequest > > ( ) ;
2829 _routes = new ConcurrentDictionary < string , RequestDelegate > ( ) ;
2930 _auths = new ConcurrentDictionary < string , ( string username , string password ) > ( ) ;
31+ _csp = new ConcurrentDictionary < string , string > ( ) ;
3032 _webHost = new WebHostBuilder ( )
3133 . ConfigureAppConfiguration ( ( context , builder ) => builder
3234 . SetBasePath ( context . HostingEnvironment . ContentRootPath )
@@ -50,7 +52,16 @@ public SimpleServer(int port, string contentRoot, bool isHttps)
5052 }
5153 return next ( ) ;
5254 } )
53- . UseStaticFiles ( ) )
55+ . UseStaticFiles ( new StaticFileOptions
56+ {
57+ OnPrepareResponse = fileResponseContext =>
58+ {
59+ if ( _csp . TryGetValue ( fileResponseContext . Context . Request . Path , out var csp ) )
60+ {
61+ fileResponseContext . Context . Response . Headers [ "Content-Security-Policy" ] = csp ;
62+ }
63+ }
64+ } ) )
5465 . UseKestrel ( options =>
5566 {
5667 if ( isHttps )
@@ -66,10 +77,9 @@ public SimpleServer(int port, string contentRoot, bool isHttps)
6677 . Build ( ) ;
6778 }
6879
69- public void SetAuth ( string path , string username , string password )
70- {
71- _auths . Add ( path , ( username , password ) ) ;
72- }
80+ public void SetAuth ( string path , string username , string password ) => _auths . Add ( path , ( username , password ) ) ;
81+
82+ public void SetCSP ( string path , string csp ) => _csp . Add ( path , csp ) ;
7383
7484 public Task StartAsync ( ) => _webHost . StartAsync ( ) ;
7585
@@ -84,26 +94,21 @@ public void Reset()
8494 {
8595 _routes . Clear ( ) ;
8696 _auths . Clear ( ) ;
97+ _csp . Clear ( ) ;
8798 foreach ( var subscriber in _requestSubscribers . Values )
8899 {
89100 subscriber ( null ) ;
90101 }
91102 _requestSubscribers . Clear ( ) ;
92103 }
93104
94- public void SetRoute ( string path , RequestDelegate handler )
95- {
96- _routes . Add ( path , handler ) ;
97- }
105+ public void SetRoute ( string path , RequestDelegate handler ) => _routes . Add ( path , handler ) ;
98106
99- public void SetRedirect ( string from , string to )
107+ public void SetRedirect ( string from , string to ) => SetRoute ( from , context =>
100108 {
101- SetRoute ( from , context =>
102- {
103- context . Response . Redirect ( to ) ;
104- return Task . CompletedTask ;
105- } ) ;
106- }
109+ context . Response . Redirect ( to ) ;
110+ return Task . CompletedTask ;
111+ } ) ;
107112
108113 public async Task < T > WaitForRequest < T > ( string path , Func < HttpRequest , T > selector )
109114 {
0 commit comments