forked from elm/compiler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCors.hs
48 lines (34 loc) · 1.09 KB
/
Cors.hs
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
{-# OPTIONS_GHC -Wall #-}
module Cors
( allow
)
where
import qualified Data.HashSet as HashSet
import Network.URI (parseURI)
import Snap.Core (Snap, Method, method)
import Snap.Util.CORS (CORSOptions(..), HashableMethod(..), OriginList(Origins), applyCORS, mkOriginSet)
-- ALLOW
allow :: Method -> [String] -> Snap () -> Snap ()
allow method_ origins snap =
applyCORS (toOptions method_ origins) $ method method_ $
snap
-- TO OPTIONS
toOptions :: (Monad m) => Method -> [String] -> CORSOptions m
toOptions method_ origins =
let
allowedOrigins = toOriginList origins
allowedMethods = HashSet.singleton (HashableMethod method_)
in
CORSOptions
{ corsAllowOrigin = return allowedOrigins
, corsAllowCredentials = return True
, corsExposeHeaders = return HashSet.empty
, corsAllowedMethods = return allowedMethods
, corsAllowedHeaders = return
}
toOriginList :: [String] -> OriginList
toOriginList origins =
Origins $ mkOriginSet $
case traverse parseURI origins of
Just uris -> uris
Nothing -> error "invalid entry given to toOriginList list"