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

{- | Lexing for HTTP request field values (RFC 9110, section 5): repeated fields, comma lists, and
@;@ parameters. Negotiation and conditional requests read their headers through it.
-}
module Ecluse.Core.Server.FieldValue (
    fieldValues,
    listMembers,
    memberParameters,
    trimWhitespace,
    lowerAscii,
) where

import Data.ByteString.Char8 qualified as BS8
import Data.Char (isAsciiUpper, toLower)

-- | Every value of a field, in request order. A field may legally repeat.
fieldValues :: (Eq name) => name -> [(name, value)] -> [value]
fieldValues :: forall name value. Eq name => name -> [(name, value)] -> [value]
fieldValues name
name = ((name, value) -> value) -> [(name, value)] -> [value]
forall a b. (a -> b) -> [a] -> [b]
map (name, value) -> value
forall a b. (a, b) -> b
snd ([(name, value)] -> [value])
-> ([(name, value)] -> [(name, value)])
-> [(name, value)]
-> [value]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((name, value) -> Bool) -> [(name, value)] -> [(name, value)]
forall a. (a -> Bool) -> [a] -> [a]
filter ((name -> name -> Bool
forall a. Eq a => a -> a -> Bool
== name
name) (name -> Bool) -> ((name, value) -> name) -> (name, value) -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (name, value) -> name
forall a b. (a, b) -> a
fst)

-- | The members of one comma-separated value, trimmed, with empty members dropped.
listMembers :: ByteString -> [ByteString]
listMembers :: ByteString -> [ByteString]
listMembers = (ByteString -> Bool) -> [ByteString] -> [ByteString]
forall a. (a -> Bool) -> [a] -> [a]
filter (Bool -> Bool
not (Bool -> Bool) -> (ByteString -> Bool) -> ByteString -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Bool
BS8.null) ([ByteString] -> [ByteString])
-> (ByteString -> [ByteString]) -> ByteString -> [ByteString]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (ByteString -> ByteString) -> [ByteString] -> [ByteString]
forall a b. (a -> b) -> [a] -> [b]
map ByteString -> ByteString
trimWhitespace ([ByteString] -> [ByteString])
-> (ByteString -> [ByteString]) -> ByteString -> [ByteString]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Char -> ByteString -> [ByteString]
BS8.split Char
','

-- | Split a list member into its token and parameters, names lowered. A parameter with no @=@ has an empty value.
memberParameters :: ByteString -> (ByteString, [(ByteString, ByteString)])
memberParameters :: ByteString -> (ByteString, [(ByteString, ByteString)])
memberParameters ByteString
member = case (ByteString -> ByteString) -> [ByteString] -> [ByteString]
forall a b. (a -> b) -> [a] -> [b]
map ByteString -> ByteString
trimWhitespace (Char -> ByteString -> [ByteString]
BS8.split Char
';' ByteString
member) of
    [] -> (ByteString
"", [])
    ByteString
token : [ByteString]
parameters -> (ByteString
token, (ByteString -> (ByteString, ByteString))
-> [ByteString] -> [(ByteString, ByteString)]
forall a b. (a -> b) -> [a] -> [b]
map ByteString -> (ByteString, ByteString)
parameter [ByteString]
parameters)
  where
    parameter :: ByteString -> (ByteString, ByteString)
parameter ByteString
raw = case (Char -> Bool) -> ByteString -> (ByteString, ByteString)
BS8.break (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
'=') ByteString
raw of
        (ByteString
name, ByteString
value) -> (ByteString -> ByteString
lowerAscii (ByteString -> ByteString
trimWhitespace ByteString
name), ByteString -> ByteString
trimWhitespace (Int -> ByteString -> ByteString
BS8.drop Int
1 ByteString
value))

-- | Drop the optional whitespace (spaces and tabs) around a value.
trimWhitespace :: ByteString -> ByteString
trimWhitespace :: ByteString -> ByteString
trimWhitespace = (Char -> Bool) -> ByteString -> ByteString
BS8.dropWhile Char -> Bool
isWhitespace (ByteString -> ByteString)
-> (ByteString -> ByteString) -> ByteString -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Char -> Bool) -> ByteString -> ByteString
BS8.dropWhileEnd Char -> Bool
isWhitespace
  where
    isWhitespace :: Char -> Bool
isWhitespace Char
c = Char
c Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
' ' Bool -> Bool -> Bool
|| Char
c Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
'\t'

-- | Lower ASCII letters, for the tokens the grammar compares case-insensitively.
lowerAscii :: ByteString -> ByteString
lowerAscii :: ByteString -> ByteString
lowerAscii = (Char -> Char) -> ByteString -> ByteString
BS8.map (\Char
c -> if Char -> Bool
isAsciiUpper Char
c then Char -> Char
toLower Char
c else Char
c)