11namespace SqlStreamStore . HAL
22{
33 using System ;
4- using System . Linq ;
54 using System . Net . Http ;
6- using System . Threading . Tasks ;
75 using Microsoft . AspNetCore . Builder ;
8- using Microsoft . AspNetCore . Http ;
9- using Microsoft . Extensions . Primitives ;
10- using MidFunc = System . Func <
11- Microsoft . AspNetCore . Http . HttpContext ,
12- System . Func < System . Threading . Tasks . Task > ,
13- System . Threading . Tasks . Task
14- > ;
156
167 internal static class ApplicationBuilderExtensions
178 {
@@ -24,52 +15,5 @@ public static IApplicationBuilder MapWhen(
2415 method . Method ,
2516 StringComparison . OrdinalIgnoreCase ) ,
2617 configure ) ;
27-
28- public static IApplicationBuilder UseAllowedMethods ( this IApplicationBuilder builder , IResource resource )
29- {
30- var allowed = ResourceMethods . Discover ( resource ) ;
31-
32- var allowedMethodsHeaderValue = allowed . Aggregate (
33- StringValues . Empty ,
34- ( previous , method ) => StringValues . Concat ( previous , method . Method ) ) ;
35-
36- var allowedHeadersHeaderValue = new StringValues ( new [ ]
37- {
38- Constants . Headers . ContentType ,
39- Constants . Headers . XRequestedWith ,
40- Constants . Headers . Authorization
41- } ) ;
42-
43- Task Options ( HttpContext context , Func < Task > next )
44- {
45- context . Response . Headers . AppendCommaSeparatedValues (
46- Constants . Headers . AccessControl . AllowMethods ,
47- allowedMethodsHeaderValue ) ;
48- context . Response . Headers . AppendCommaSeparatedValues (
49- Constants . Headers . AccessControl . AllowHeaders ,
50- allowedHeadersHeaderValue ) ;
51- context . Response . Headers . AppendCommaSeparatedValues (
52- Constants . Headers . AccessControl . AllowOrigin ,
53- "*" ) ;
54-
55- return Task . CompletedTask ;
56- }
57-
58- Task AllowedMethods ( HttpContext context , Func < Task > next )
59- {
60- if ( ! allowed . Contains ( new HttpMethod ( context . Request . Method ) ) )
61- {
62- context . Response . StatusCode = 405 ;
63- context . Response . Headers . Add ( Constants . Headers . Allowed , allowedMethodsHeaderValue ) ;
64- return Task . CompletedTask ;
65- }
66-
67- return next ( ) ;
68- }
69-
70- return builder
71- . MapWhen ( HttpMethod . Options , inner => inner . Use ( Options ) )
72- . Use ( AllowedMethods ) ;
73- }
7418 }
7519}
0 commit comments