-- SPDX-FileCopyrightText: 2026 Alexandra de Wit
--
-- SPDX-License-Identifier: MIT
{-# LANGUAGE TupleSections #-}

-- | Checked per-distribution Core Metadata for the PyPI upload protocol.
module Ecluse.Core.Registry.PyPI.UploadMetadata (
    PyPIUploadMetadata,
    parseUploadMetadata,
    uploadMetadataMemoryBytes,
    uploadFields,
) where

import Data.ByteString qualified as BS
import Data.Char (isAsciiLower, isAsciiUpper, isDigit)
import Data.Map.Strict qualified as Map
import Data.Text qualified as T
import Data.Text.Encoding qualified as TE

import Ecluse.Core.Package (PackageName)
import Ecluse.Core.Registry (ParseError (..), parseRefusal)
import Ecluse.Core.Registry.PyPI.Project (projectName)
import Ecluse.Core.Version (Version, canonicalPep440)

-- | Ordered upload fields, preserving repeated values and author spelling.
newtype PyPIUploadMetadata = PyPIUploadMetadata [(Text, Text)]
    deriving stock (PyPIUploadMetadata -> PyPIUploadMetadata -> Bool
(PyPIUploadMetadata -> PyPIUploadMetadata -> Bool)
-> (PyPIUploadMetadata -> PyPIUploadMetadata -> Bool)
-> Eq PyPIUploadMetadata
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: PyPIUploadMetadata -> PyPIUploadMetadata -> Bool
== :: PyPIUploadMetadata -> PyPIUploadMetadata -> Bool
$c/= :: PyPIUploadMetadata -> PyPIUploadMetadata -> Bool
/= :: PyPIUploadMetadata -> PyPIUploadMetadata -> Bool
Eq, Int -> PyPIUploadMetadata -> ShowS
[PyPIUploadMetadata] -> ShowS
PyPIUploadMetadata -> String
(Int -> PyPIUploadMetadata -> ShowS)
-> (PyPIUploadMetadata -> String)
-> ([PyPIUploadMetadata] -> ShowS)
-> Show PyPIUploadMetadata
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> PyPIUploadMetadata -> ShowS
showsPrec :: Int -> PyPIUploadMetadata -> ShowS
$cshow :: PyPIUploadMetadata -> String
show :: PyPIUploadMetadata -> String
$cshowList :: [PyPIUploadMetadata] -> ShowS
showList :: [PyPIUploadMetadata] -> ShowS
Show)

-- | Reject malformed headers, ambiguous single-use fields, and inconsistent package identity.
parseUploadMetadata :: Int -> PackageName -> Version -> ByteString -> Either ParseError PyPIUploadMetadata
parseUploadMetadata :: Int
-> PackageName
-> Version
-> ByteString
-> Either ParseError PyPIUploadMetadata
parseUploadMetadata Int
cap PackageName
package Version
version ByteString
bytes = do
    Bool -> Either ParseError () -> Either ParseError ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Int
cap Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
0 Bool -> Bool -> Bool
|| ByteString -> Int
BS.length ByteString
bytes Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
cap) (Text -> Either ParseError ()
forall a. Text -> Either ParseError a
parseRefusal Text
"Core Metadata exceeds its byte bound")
    document <- (UnicodeException -> ParseError)
-> Either UnicodeException Text -> Either ParseError Text
forall a b c. (a -> b) -> Either a c -> Either b c
forall (p :: * -> * -> *) a b c.
Bifunctor p =>
(a -> b) -> p a c -> p b c
first (ParseError -> UnicodeException -> ParseError
forall a b. a -> b -> a
const (Text -> ParseError
ParseError Text
"Core Metadata is not UTF-8")) (ByteString -> Either UnicodeException Text
TE.decodeUtf8' ByteString
bytes)
    when (T.any invalidControl document) (parseRefusal "Core Metadata contains a control character")
    fields <- parseHeaders document
    checked <- traverse projectField fields
    let counts = (Int -> Int -> Int) -> [(Text, Int)] -> Map Text Int
forall k a. Ord k => (a -> a -> a) -> [(k, a)] -> Map k a
Map.fromListWith Int -> Int -> Int
forall a. Num a => a -> a -> a
(+) [(Text
name, Int
1 :: Int) | (Text
name, Text
_) <- [(Text, Text)]
fields]
    for_ (Map.toList counts) $ \(Text
name, Int
count) ->
        Bool -> Either ParseError () -> Either ParseError ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Int
count Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
1 Bool -> Bool -> Bool
&& Text
name Text -> [Text] -> Bool
forall (f :: * -> *) a.
(Foldable f, DisallowElem f, Eq a) =>
a -> f a -> Bool
`notElem` [Text]
repeatedFields) (Text -> Either ParseError ()
forall a. Text -> Either ParseError a
parseRefusal (Text
"repeated single-use Core Metadata field: " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
name))
    format <- required "metadata-version" fields
    unless (format `elem` ["1.0", "1.1", "1.2", "2.1", "2.2", "2.3", "2.4", "2.5", "2.6"]) $
        parseRefusal "unsupported Core Metadata version"
    declaredName <- required "name" fields >>= projectName
    unless (declaredName == package) (parseRefusal "Core Metadata names another package")
    declaredVersion <- required "version" fields
    unless (canonicalPep440 declaredVersion == Just version) (parseRefusal "Core Metadata names another version")
    for_ [value | ("dynamic", value) <- fields] $ \Text
value ->
        Bool -> Either ParseError () -> Either ParseError ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Text -> Text
T.toLower Text
value Text -> [Text] -> Bool
forall (f :: * -> *) a.
(Foldable f, DisallowElem f, Eq a) =>
a -> f a -> Bool
`elem` [Text
"name", Text
"version", Text
"metadata-version", Text
"dynamic"] Bool -> Bool -> Bool
|| Maybe Text -> Bool
forall a. Maybe a -> Bool
isNothing (Text -> Maybe Text
fieldName (Text -> Text
T.toLower Text
value))) (Either ParseError () -> Either ParseError ())
-> Either ParseError () -> Either ParseError ()
forall a b. (a -> b) -> a -> b
$
            Text -> Either ParseError ()
forall a. Text -> Either ParseError a
parseRefusal Text
"invalid Dynamic declaration"
    pure (PyPIUploadMetadata checked)

-- | Charge the actual raw, decoded, and encoded metadata plus bounded header structures before preparation.
uploadMetadataMemoryBytes :: ByteString -> Either ParseError Int
uploadMetadataMemoryBytes :: ByteString -> Either ParseError Int
uploadMetadataMemoryBytes ByteString
bytes
    | Integer
wanted Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
> Int -> Integer
forall a. Integral a => a -> Integer
toInteger (Int
forall a. Bounded a => a
maxBound :: Int) = Text -> Either ParseError Int
forall a. Text -> Either ParseError a
parseRefusal Text
"Core Metadata memory charge exceeds the platform bound"
    | Bool
otherwise = Int -> Either ParseError Int
forall a b. b -> Either a b
Right (Integer -> Int
forall a. Num a => Integer -> a
fromInteger Integer
wanted)
  where
    wanted :: Integer
wanted = Integer
1024 Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ Integer
6 Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
* Int -> Integer
forall a. Integral a => a -> Integer
toInteger (ByteString -> Int
BS.length ByteString
bytes) Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ Integer
512 Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
* Int -> Integer
forall a. Integral a => a -> Integer
toInteger (Int -> ByteString -> Int
headerCount Int
0 ByteString
bytes)
    headerCount :: Int -> ByteString -> Int
headerCount Int
seen ByteString
remaining
        | ByteString -> Bool
BS.null ByteString
remaining Bool -> Bool -> Bool
|| Int
seen Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= (Int
65537 :: Int) = Int
seen
        | Bool
otherwise =
            let (ByteString
line, ByteString
suffix) = (Word8 -> Bool) -> ByteString -> (ByteString, ByteString)
BS.break (Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== Word8
10) ByteString
remaining
             in if ByteString
line ByteString -> ByteString -> Bool
forall a. Eq a => a -> a -> Bool
== ByteString
"" Bool -> Bool -> Bool
|| ByteString
line ByteString -> ByteString -> Bool
forall a. Eq a => a -> a -> Bool
== ByteString
"\r" then Int
seen else Int -> ByteString -> Int
headerCount (Int
seen Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) (Int -> ByteString -> ByteString
BS.drop Int
1 ByteString
suffix)

-- | Form names follow the native upload API, including its pluralisation exceptions.
uploadFields :: PyPIUploadMetadata -> [(Text, Text)]
uploadFields :: PyPIUploadMetadata -> [(Text, Text)]
uploadFields (PyPIUploadMetadata [(Text, Text)]
fields) = [(Text, Text)]
fields

invalidControl :: Char -> Bool
invalidControl :: Char -> Bool
invalidControl 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. Eq a => a -> a -> Bool
/= Char
'\n' Bool -> Bool -> Bool
&& Char
character Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
/= Char
'\r' Bool -> Bool -> Bool
&& Char
character Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
/= Char
'\t') Bool -> Bool -> Bool
|| Char
character Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
'\DEL'

parseHeaders :: Text -> Either ParseError [(Text, Text)]
parseHeaders :: Text -> Either ParseError [(Text, Text)]
parseHeaders = Int
-> Text
-> [(Text, [Text])]
-> Text
-> Either ParseError [(Text, Text)]
go Int
0 Text
"" []
  where
    go :: Int
-> Text
-> [(Text, [Text])]
-> Text
-> Either ParseError [(Text, Text)]
go Int
count Text
previousEnding [(Text, [Text])]
held Text
remaining
        | Text -> Bool
T.null Text
remaining = [(Text, Text)] -> Either ParseError [(Text, Text)]
forall a b. b -> Either a b
Right ([(Text, [Text])] -> [(Text, Text)]
finishFields [(Text, [Text])]
held)
        | Bool
otherwise =
            let (Text
rawLine, Text
suffix) = HasCallStack => Text -> Text -> (Text, Text)
Text -> Text -> (Text, Text)
T.breakOn Text
"\n" Text
remaining
                rest :: Text
rest = Int -> Text -> Text
T.drop Int
1 Text
suffix
                (Text
line, Text
ending) = (Text, Text)
-> (Text -> (Text, Text)) -> Maybe Text -> (Text, Text)
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (Text
rawLine, Text
"\n") (,Text
"\r\n") (Text -> Text -> Maybe Text
T.stripSuffix Text
"\r" Text
rawLine)
             in if Text
rawLine Text -> [Text] -> Bool
forall (f :: * -> *) a.
(Foldable f, DisallowElem f, Eq a) =>
a -> f a -> Bool
`elem` [Text
"", Text
"\r"]
                    then [(Text, [Text])] -> Text -> Either ParseError [(Text, Text)]
finish [(Text, [Text])]
held Text
rest
                    else do
                        Bool -> Either ParseError () -> Either ParseError ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Int
count Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= (Int
65536 :: Int)) (Text -> Either ParseError ()
forall a. Text -> Either ParseError a
parseRefusal Text
"Core Metadata header count exceeds its bound")
                        Bool -> Either ParseError () -> Either ParseError ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Text -> Int -> Ordering
T.compareLength Text
rawLine Int
16384 Ordering -> Ordering -> Bool
forall a. Eq a => a -> a -> Bool
== Ordering
GT) (Text -> Either ParseError ()
forall a. Text -> Either ParseError a
parseRefusal Text
"Core Metadata header line exceeds its bound")
                        Bool -> Either ParseError () -> Either ParseError ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Text -> Bool
T.null Text
suffix Bool -> Bool -> Bool
&& Text
ending Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== Text
"\r\n") (Text -> Either ParseError ()
forall a. Text -> Either ParseError a
parseRefusal Text
"bare carriage return in Core Metadata")
                        Int
-> Text
-> Text
-> [(Text, [Text])]
-> Text
-> Text
-> Either ParseError [(Text, Text)]
addLine (Int
count Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) Text
previousEnding Text
ending [(Text, [Text])]
held Text
line Text
rest
    finish :: [(Text, [Text])] -> Text -> Either ParseError [(Text, Text)]
finish [(Text, [Text])]
held Text
body
        | Text -> Bool
T.null Text
body = [(Text, Text)] -> Either ParseError [(Text, Text)]
forall a b. b -> Either a b
Right ([(Text, [Text])] -> [(Text, Text)]
finishFields [(Text, [Text])]
held)
        | ((Text, [Text]) -> Bool) -> [(Text, [Text])] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any ((Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== Text
"description") (Text -> Bool)
-> ((Text, [Text]) -> Text) -> (Text, [Text]) -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Text, [Text]) -> Text
forall a b. (a, b) -> a
fst) [(Text, [Text])]
held = Text -> Either ParseError [(Text, Text)]
forall a. Text -> Either ParseError a
parseRefusal Text
"description appears in both header and body"
        | Bool
otherwise = [(Text, Text)] -> Either ParseError [(Text, Text)]
forall a b. b -> Either a b
Right ([(Text, [Text])] -> [(Text, Text)]
finishFields [(Text, [Text])]
held [(Text, Text)] -> [(Text, Text)] -> [(Text, Text)]
forall a. Semigroup a => a -> a -> a
<> [(Text
"description", Text
body)])
    finishFields :: [(Text, [Text])] -> [(Text, Text)]
finishFields = ((Text, [Text]) -> (Text, Text))
-> [(Text, [Text])] -> [(Text, Text)]
forall a b. (a -> b) -> [a] -> [b]
map (Text, [Text]) -> (Text, Text)
forall {a}. (Eq a, IsString a) => (a, [Text]) -> (a, Text)
finishField ([(Text, [Text])] -> [(Text, Text)])
-> ([(Text, [Text])] -> [(Text, [Text])])
-> [(Text, [Text])]
-> [(Text, Text)]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [(Text, [Text])] -> [(Text, [Text])]
forall a. [a] -> [a]
reverse
    finishField :: (a, [Text]) -> (a, Text)
finishField (a
name, [Text]
pieces) =
        let value :: Text
value = [Text] -> Text
T.concat ([Text] -> [Text]
forall a. [a] -> [a]
reverse [Text]
pieces)
         in (a
name, if a
name a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
"description" then HasCallStack => Text -> Text -> Text -> Text
Text -> Text -> Text -> Text
T.replace Text
"\n       |" Text
"\n" (HasCallStack => Text -> Text -> Text -> Text
Text -> Text -> Text -> Text
T.replace Text
"\r\n       |" Text
"\r\n" Text
value) else Text
value)
    addLine :: Int
-> Text
-> Text
-> [(Text, [Text])]
-> Text
-> Text
-> Either ParseError [(Text, Text)]
addLine Int
count Text
previousEnding Text
nextEnding [(Text, [Text])]
held Text
line Text
remaining
        | (Char -> Bool) -> Text -> Bool
T.any (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
'\r') Text
line = Text -> Either ParseError [(Text, Text)]
forall a. Text -> Either ParseError a
parseRefusal Text
"bare carriage return in Core Metadata"
        | Text -> Text -> Bool
T.isPrefixOf Text
" " Text
line Bool -> Bool -> Bool
|| Text -> Text -> Bool
T.isPrefixOf Text
"\t" Text
line = case [(Text, [Text])]
held of
            [] -> Text -> Either ParseError [(Text, Text)]
forall a. Text -> Either ParseError a
parseRefusal Text
"Core Metadata starts with a continuation"
            (Text
name, [Text]
pieces) : [(Text, [Text])]
rest -> Int
-> Text
-> [(Text, [Text])]
-> Text
-> Either ParseError [(Text, Text)]
go Int
count Text
nextEnding ((Text
name, (Text
previousEnding Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
line) Text -> [Text] -> [Text]
forall a. a -> [a] -> [a]
: [Text]
pieces) (Text, [Text]) -> [(Text, [Text])] -> [(Text, [Text])]
forall a. a -> [a] -> [a]
: [(Text, [Text])]
rest) Text
remaining
        | Bool
otherwise = do
            let (Text
name, Text
suffix) = HasCallStack => Text -> Text -> (Text, Text)
Text -> Text -> (Text, Text)
T.breakOn Text
":" Text
line
            Bool -> Either ParseError () -> Either ParseError ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Text -> Bool
T.null Text
name Bool -> Bool -> Bool
|| Text -> Bool
T.null Text
suffix Bool -> Bool -> Bool
|| Bool -> Bool
not ((Char -> Bool) -> Text -> Bool
T.all Char -> Bool
headerChar Text
name)) (Text -> Either ParseError ()
forall a. Text -> Either ParseError a
parseRefusal Text
"malformed Core Metadata header")
            let value :: Text
value = (Char -> Bool) -> Text -> Text
T.dropWhile (Char -> String -> Bool
forall (f :: * -> *) a.
(Foldable f, DisallowElem f, Eq a) =>
a -> f a -> Bool
`elem` [Char
' ', Char
'\t']) (Int -> Text -> Text
T.drop Int
1 Text
suffix)
            Int
-> Text
-> [(Text, [Text])]
-> Text
-> Either ParseError [(Text, Text)]
go Int
count Text
nextEnding ((Text -> Text
T.toLower Text
name, [Text
value]) (Text, [Text]) -> [(Text, [Text])] -> [(Text, [Text])]
forall a. a -> [a] -> [a]
: [(Text, [Text])]
held) Text
remaining

headerChar :: Char -> Bool
headerChar :: Char -> Bool
headerChar Char
character = Char -> Bool
isAsciiLower Char
character Bool -> Bool -> Bool
|| Char -> Bool
isAsciiUpper Char
character Bool -> Bool -> Bool
|| Char -> Bool
isDigit Char
character Bool -> Bool -> Bool
|| Char
character Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
'-'

required :: Text -> [(Text, Text)] -> Either ParseError Text
required :: Text -> [(Text, Text)] -> Either ParseError Text
required Text
name [(Text, Text)]
fields = case [Text
value | (Text
key, Text
value) <- [(Text, Text)]
fields, Text
key Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== Text
name] of
    [Text
value] | Bool -> Bool
not (Text -> Bool
T.null Text
value) -> Text -> Either ParseError Text
forall a b. b -> Either a b
Right Text
value
    [Text]
_ -> Text -> Either ParseError Text
forall a. Text -> Either ParseError a
parseRefusal (Text
"missing or ambiguous Core Metadata field: " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
name)

projectField :: (Text, Text) -> Either ParseError (Text, Text)
projectField :: (Text, Text) -> Either ParseError (Text, Text)
projectField (Text
name, Text
value) = case Text -> Maybe Text
fieldName Text
name of
    Maybe Text
Nothing -> Text -> Either ParseError (Text, Text)
forall a. Text -> Either ParseError a
parseRefusal (Text
"unsupported Core Metadata field: " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
name)
    Just Text
target -> (Text, Text) -> Either ParseError (Text, Text)
forall a b. b -> Either a b
Right (Text
target, Text
value)

fieldName :: Text -> Maybe Text
fieldName :: Text -> Maybe Text
fieldName Text
name
    | Text
name Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== Text
"classifier" = Text -> Maybe Text
forall a. a -> Maybe a
Just Text
"classifiers"
    | Text
name Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== Text
"project-url" = Text -> Maybe Text
forall a. a -> Maybe a
Just Text
"project_urls"
    | Text
name Text -> [Text] -> Bool
forall (f :: * -> *) a.
(Foldable f, DisallowElem f, Eq a) =>
a -> f a -> Bool
`elem` [Text]
singleFields Bool -> Bool -> Bool
|| Text
name Text -> [Text] -> Bool
forall (f :: * -> *) a.
(Foldable f, DisallowElem f, Eq a) =>
a -> f a -> Bool
`elem` [Text]
repeatedFields = Text -> Maybe Text
forall a. a -> Maybe a
Just (HasCallStack => Text -> Text -> Text -> Text
Text -> Text -> Text -> Text
T.replace Text
"-" Text
"_" Text
name)
    | Bool
otherwise = Maybe Text
forall a. Maybe a
Nothing

singleFields :: [Text]
singleFields :: [Text]
singleFields =
    [ Text
"metadata-version"
    , Text
"name"
    , Text
"version"
    , Text
"summary"
    , Text
"description"
    , Text
"description-content-type"
    , Text
"keywords"
    , Text
"home-page"
    , Text
"download-url"
    , Text
"author"
    , Text
"author-email"
    , Text
"maintainer"
    , Text
"maintainer-email"
    , Text
"license"
    , Text
"license-expression"
    , Text
"requires-python"
    ]

repeatedFields :: [Text]
repeatedFields :: [Text]
repeatedFields =
    [ Text
"platform"
    , Text
"supported-platform"
    , Text
"classifier"
    , Text
"requires"
    , Text
"provides"
    , Text
"obsoletes"
    , Text
"requires-dist"
    , Text
"provides-dist"
    , Text
"obsoletes-dist"
    , Text
"requires-external"
    , Text
"project-url"
    , Text
"provides-extra"
    , Text
"dynamic"
    , Text
"license-file"
    , Text
"import-name"
    , Text
"import-namespace"
    ]