11namespace SqlStreamStore . HAL
22{
3+ using System . Net . Http ;
4+ using System . Threading . Tasks ;
35 using Microsoft . Owin ;
46 using Microsoft . Owin . Builder ;
57 using Owin ;
@@ -17,7 +19,9 @@ public static MidFunc UseStreamStore(IReadonlyStreamStore streamStore)
1719
1820 var builder = new AppBuilder ( )
1921 . MapWhen ( IsStream , inner => inner . Use ( GetStream ( allStream ) ) )
20- . MapWhen ( IsStreamMessage , inner => inner . Use ( GetStreamMessage ( allStream ) ) ) ;
22+ . MapWhen ( IsStreamOptions , inner => inner . Use ( GetStreamOptions ) )
23+ . MapWhen ( IsStreamMessage , inner => inner . Use ( GetStreamMessage ( allStream ) ) )
24+ . MapWhen ( IsStreamMessageOptions , inner => inner . Use ( GetStreamMessageOptions ) ) ;
2125
2226 return next =>
2327 {
@@ -27,11 +31,23 @@ public static MidFunc UseStreamStore(IReadonlyStreamStore streamStore)
2731 } ;
2832 }
2933
34+ private static bool IsStream ( PathString requestPath )
35+ => ! requestPath . HasValue ;
36+
3037 private static bool IsStream ( IOwinContext context )
31- => context . IsGetOrHead ( ) && ! context . Request . Path . HasValue ;
38+ => context . IsGetOrHead ( ) && IsStream ( context . Request . Path ) ;
39+
40+ private static bool IsStreamOptions ( IOwinContext context )
41+ => context . IsOptions ( ) && IsStream ( context . Request . Path ) ;
42+
43+ private static bool IsStreamMessage ( PathString requestPath )
44+ => long . TryParse ( requestPath . Value ? . Remove ( 0 , 1 ) , out var _ ) ;
3245
3346 private static bool IsStreamMessage ( IOwinContext context )
34- => context . IsGetOrHead ( ) && long . TryParse ( context . Request . Path . Value ? . Remove ( 0 , 1 ) , out var _ ) ;
47+ => context . IsGetOrHead ( ) && IsStreamMessage ( context . Request . Path ) ;
48+
49+ private static bool IsStreamMessageOptions ( IOwinContext context )
50+ => context . IsOptions ( ) && IsStreamMessage ( context . Request . Path ) ;
3551
3652 private static MidFunc GetStream ( AllStreamResource allStream ) => next => async env =>
3753 {
@@ -46,6 +62,18 @@ private static MidFunc GetStream(AllStreamResource allStream) => next => async e
4662 await context . WriteHalResponse ( response ) ;
4763 }
4864 } ;
65+
66+ private static MidFunc GetStreamOptions => next => env =>
67+ {
68+ var context = new OwinContext ( env ) ;
69+
70+ context . SetStandardCorsHeaders (
71+ HttpMethod . Get ,
72+ HttpMethod . Head ,
73+ HttpMethod . Options ) ;
74+
75+ return Task . CompletedTask ;
76+ } ;
4977
5078 private static MidFunc GetStreamMessage ( AllStreamResource allStream ) => next => async env =>
5179 {
@@ -60,5 +88,18 @@ private static MidFunc GetStreamMessage(AllStreamResource allStream) => next =>
6088 await context . WriteHalResponse ( response ) ;
6189 }
6290 } ;
91+
92+ private static MidFunc GetStreamMessageOptions => next => env =>
93+ {
94+ var context = new OwinContext ( env ) ;
95+
96+ context . SetStandardCorsHeaders (
97+ HttpMethod . Get ,
98+ HttpMethod . Head ,
99+ HttpMethod . Options ) ;
100+
101+ return Task . CompletedTask ;
102+ } ;
103+
63104 }
64105}
0 commit comments