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

-- | The product identity shared by outbound registry requests and advisory feeds.
module Ecluse.Core.BuildIdentity (
    OperatorContact,
    parseOperatorContact,
    renderUserAgent,
    identityManagerSettings,
    productName,
    productVersion,
    userAgent,
    identifyRequest,
) where

import Data.Text qualified as T
import Data.Version (showVersion)
import Network.HTTP.Client (ManagerSettings (managerModifyRequest), Request (requestHeaders))
import Network.HTTP.Types.Header (hUserAgent)
import Paths_ecluse (version)

-- | The name Écluse identifies itself by to another service.
productName :: Text
productName :: Text
productName = Text
"ecluse"

-- | The running build's version, as the cabal file declares it.
productVersion :: Text
productVersion :: Text
productVersion = String -> Text
forall a. ToText a => a -> Text
toText (Version -> String
showVersion Version
version)

-- | An operator contact that cannot alter the HTTP comment or inject a header.
newtype OperatorContact = OperatorContact Text
    deriving stock (OperatorContact -> OperatorContact -> Bool
(OperatorContact -> OperatorContact -> Bool)
-> (OperatorContact -> OperatorContact -> Bool)
-> Eq OperatorContact
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: OperatorContact -> OperatorContact -> Bool
== :: OperatorContact -> OperatorContact -> Bool
$c/= :: OperatorContact -> OperatorContact -> Bool
/= :: OperatorContact -> OperatorContact -> Bool
Eq, Int -> OperatorContact -> ShowS
[OperatorContact] -> ShowS
OperatorContact -> String
(Int -> OperatorContact -> ShowS)
-> (OperatorContact -> String)
-> ([OperatorContact] -> ShowS)
-> Show OperatorContact
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> OperatorContact -> ShowS
showsPrec :: Int -> OperatorContact -> ShowS
$cshow :: OperatorContact -> String
show :: OperatorContact -> String
$cshowList :: [OperatorContact] -> ShowS
showList :: [OperatorContact] -> ShowS
Show)

-- | Parse 1-256 printable ASCII characters, excluding comment delimiters and backslash.
parseOperatorContact :: Text -> Either Text OperatorContact
parseOperatorContact :: Text -> Either Text OperatorContact
parseOperatorContact Text
value
    | Text -> Bool
T.null Text
value Bool -> Bool -> Bool
|| Text -> Int -> Ordering
T.compareLength Text
value Int
256 Ordering -> Ordering -> Bool
forall a. Eq a => a -> a -> Bool
== Ordering
GT Bool -> Bool -> Bool
|| (Char -> Bool) -> Text -> Bool
T.any Char -> Bool
forbidden Text
value =
        Text -> Either Text OperatorContact
forall a b. a -> Either a b
Left Text
"must contain 1-256 printable ASCII characters, excluding parentheses and backslash"
    | Bool
otherwise = OperatorContact -> Either Text OperatorContact
forall a b. b -> Either a b
Right (Text -> OperatorContact
OperatorContact Text
value)
  where
    forbidden :: Char -> Bool
forbidden Char
character = Char
character Char -> Char -> Bool
forall a. Ord a => a -> a -> Bool
< Char
' ' Bool -> Bool -> Bool
|| Char
character Char -> Char -> Bool
forall a. Ord a => a -> a -> Bool
> Char
'~' Bool -> Bool -> Bool
|| Char
character Char -> String -> Bool
forall (f :: * -> *) a.
(Foldable f, DisallowElem f, Eq a) =>
a -> f a -> Bool
`elem` [Char
'(', Char
')', Char
'\\']

-- | Render a validated contact into the outbound identity before requests start.
renderUserAgent :: Maybe OperatorContact -> ByteString
renderUserAgent :: Maybe OperatorContact -> ByteString
renderUserAgent Maybe OperatorContact
contact =
    Text -> ByteString
forall a b. ConvertUtf8 a b => a -> b
encodeUtf8 (Text
productName Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"/" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
productVersion Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
" (+https://ecluse-proxy.com" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
suffix Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
")")
  where
    suffix :: Text
suffix = Text -> (OperatorContact -> Text) -> Maybe OperatorContact -> Text
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Text
"" (\(OperatorContact Text
value) -> Text
"; contact: " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
value) Maybe OperatorContact
contact

-- | The default identity, including the project's public URL.
userAgent :: ByteString
userAgent :: ByteString
userAgent = Maybe OperatorContact -> ByteString
renderUserAgent Maybe OperatorContact
forall a. Maybe a
Nothing

-- | Identify an outbound request unless the caller supplies its own identity.
identifyRequest :: Request -> Request
identifyRequest :: Request -> Request
identifyRequest = ByteString -> Request -> Request
identifyWith ByteString
userAgent

-- | Chain request callbacks and replace only the default product identity or an absent identity.
identityManagerSettings :: ByteString -> ManagerSettings -> ManagerSettings
identityManagerSettings :: ByteString -> ManagerSettings -> ManagerSettings
identityManagerSettings ByteString
renderedAgent ManagerSettings
settings =
    ManagerSettings
settings{managerModifyRequest = fmap (identifyWith renderedAgent) . managerModifyRequest settings}

identifyWith :: ByteString -> Request -> Request
identifyWith :: ByteString -> Request -> Request
identifyWith ByteString
renderedAgent Request
request
    | ((HeaderName, ByteString) -> Bool)
-> [(HeaderName, ByteString)] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any (HeaderName, ByteString) -> Bool
customIdentity [(HeaderName, ByteString)]
headers = Request
request
    | Bool
otherwise = Request
request{requestHeaders = (hUserAgent, renderedAgent) : filter ((/= hUserAgent) . fst) headers}
  where
    headers :: [(HeaderName, ByteString)]
headers = Request -> [(HeaderName, ByteString)]
requestHeaders Request
request
    customIdentity :: (HeaderName, ByteString) -> Bool
customIdentity (HeaderName
name, ByteString
value) = HeaderName
name HeaderName -> HeaderName -> Bool
forall a. Eq a => a -> a -> Bool
== HeaderName
hUserAgent Bool -> Bool -> Bool
&& ByteString
value ByteString -> ByteString -> Bool
forall a. Eq a => a -> a -> Bool
/= ByteString
userAgent Bool -> Bool -> Bool
&& ByteString
value ByteString -> ByteString -> Bool
forall a. Eq a => a -> a -> Bool
/= ByteString
renderedAgent