-- SPDX-FileCopyrightText: 2026 Alexandra de Wit
--
-- SPDX-License-Identifier: MIT

{- | Whether a request's @Accept@ header admits a media type a route serves, the one question
the router answers @406 Not Acceptable@ on. Nothing is selected by quality: each route serves
one form, so a client that wants another is refused rather than given a substitute.

The reading follows RFC 9110. An absent @Accept@ admits anything, a range matches exactly, by
its type half under @type\/*@, or universally under @*\/*@, and a range parameter is ignored
except @q=0@, which rejects rather than ranks.
-}
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)

-- | Whether the request admits one of the media types a route serves. No @Accept@ header admits them all.
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)

{- Whether one range admits one served type. The range's own parameters are ignored, because
Écluse serves one representation per route and negotiates no parameter, but @q=0@ rejects. -}
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
    -- A quality is a fixed-point number, so every zero spelling ("0", "0.", "0.000") rejects.
    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)

-- Whether a parameterless range names a served type: exactly, by its type half, or universally.
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)