module Ecluse.Core.Server.Accept (
acceptsAny,
) where
import Data.ByteString.Char8 qualified as BS8
import Data.List (lookup)
import Network.HTTP.Types.Header (RequestHeaders, hAccept)
import Ecluse.Core.Server.FieldValue (listMembers, lowerAscii, memberParameters)
acceptsAny :: RequestHeaders -> NonEmpty ByteString -> Bool
acceptsAny :: RequestHeaders -> NonEmpty ByteString -> Bool
acceptsAny RequestHeaders
headers NonEmpty ByteString
served = case HeaderName -> RequestHeaders -> Maybe ByteString
forall a b. Eq a => a -> [(a, b)] -> Maybe b
lookup HeaderName
hAccept RequestHeaders
headers of
Maybe ByteString
Nothing -> Bool
True
Just ByteString
raw -> (ByteString -> Bool) -> [ByteString] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any (\ByteString
range -> (ByteString -> Bool) -> NonEmpty ByteString -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any (ByteString -> ByteString -> Bool
admits ByteString
range) NonEmpty ByteString
served) (ByteString -> [ByteString]
listMembers ByteString
raw)
admits :: ByteString -> ByteString -> Bool
admits :: ByteString -> ByteString -> Bool
admits ByteString
member ByteString
served =
Bool -> Bool
not (((ByteString, ByteString) -> Bool)
-> [(ByteString, ByteString)] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any (ByteString, ByteString) -> Bool
forall {a}. (Eq a, IsString a) => (a, ByteString) -> Bool
isZeroQuality [(ByteString, ByteString)]
parameters) Bool -> Bool -> Bool
&& ByteString -> ByteString -> Bool
matches ByteString
range ByteString
served
where
(ByteString
range, [(ByteString, ByteString)]
parameters) = ByteString -> (ByteString, [(ByteString, ByteString)])
memberParameters ByteString
member
isZeroQuality :: (a, ByteString) -> Bool
isZeroQuality (a
name, ByteString
value) = a
name a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
"q" Bool -> Bool -> Bool
&& ByteString -> Bool
isZero ByteString
value
isZero :: ByteString -> Bool
isZero ByteString
value = (Char -> Bool) -> ByteString -> Bool
BS8.all (\Char
c -> Char
c Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
'0' Bool -> Bool -> Bool
|| Char
c Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
'.') ByteString
value Bool -> Bool -> Bool
&& Bool -> Bool
not (ByteString -> Bool
BS8.null ByteString
value)
matches :: ByteString -> ByteString -> Bool
matches :: ByteString -> ByteString -> Bool
matches ByteString
range ByteString
served
| ByteString
range ByteString -> ByteString -> Bool
forall a. Eq a => a -> a -> Bool
== ByteString
"*/*" = Bool
True
| ByteString -> ByteString
lowerAscii ByteString
range ByteString -> ByteString -> Bool
forall a. Eq a => a -> a -> Bool
== ByteString -> ByteString
lowerAscii ByteString
served = Bool
True
| Bool
otherwise = case (Char -> Bool) -> ByteString -> (ByteString, ByteString)
BS8.break (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
'/') ByteString
range of
(ByteString
typeHalf, ByteString
subtype) -> ByteString
subtype ByteString -> ByteString -> Bool
forall a. Eq a => a -> a -> Bool
== ByteString
"/*" Bool -> Bool -> Bool
&& ByteString -> ByteString
lowerAscii ByteString
typeHalf ByteString -> ByteString -> Bool
forall a. Eq a => a -> a -> Bool
== ByteString -> ByteString
lowerAscii ((Char -> Bool) -> ByteString -> ByteString
BS8.takeWhile (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
/= Char
'/') ByteString
served)