module Ecluse.Core.Registry.RubyGems.Handlers.Coordinate (
serveGemArtifact,
serveCompactSpec,
) where
import Data.Map.Strict qualified as Map
import Data.Set qualified as Set
import Network.HTTP.Types (Method)
import Network.Wai (Request, ResponseReceived)
import UnliftIO (try)
import Ecluse.Core.Cve.Types (DbEtag)
import Ecluse.Core.Mirror.State (recordVersion, versionFiles)
import Ecluse.Core.Package (Artifact (artFilename), PackageDetails (pkgArtifacts, pkgName, pkgVersion), PackageName)
import Ecluse.Core.Package.Merge (Provenance (..))
import Ecluse.Core.Registry (BodyOutcome (..), FetchFault (FetchUrlUnformable))
import Ecluse.Core.Registry.Adapter.Capability (AdapterMetadata (metadataChargeFactors))
import Ecluse.Core.Registry.CachedDocument (rubyGemsCached)
import Ecluse.Core.Registry.Exchange (boundedChargedRead, chargedRead, formThen, withSuccessBodyUsing)
import Ecluse.Core.Registry.Public (ReadControl (..))
import Ecluse.Core.Registry.Request (artifactRequestByUrl)
import Ecluse.Core.Registry.RubyGems.CompactSpec (compactSpecBytes, validateCompactSpec)
import Ecluse.Core.Registry.RubyGems.Credential (rubyReadCredential)
import Ecluse.Core.Registry.RubyGems.Document (GemDocument (..), GemRow (..))
import Ecluse.Core.Registry.RubyGems.Handlers.Budget (newNativeDocumentBudget, newNativeReadBudget, withNativeRequest)
import Ecluse.Core.Registry.RubyGems.Project (candidateCoordinates, coordinateFilename, coordinateName, coordinateVersion)
import Ecluse.Core.Registry.RubyGems.Request (gemCompactSpecUrl)
import Ecluse.Core.Registry.RubyGems.Route.Response (RubyResponse, rubyBadRequest, rubyReplies)
import Ecluse.Core.Rules.Types (EvalContext (ctxAdvisoryEtag))
import Ecluse.Core.Security.Egress (RegistryUrl, registryUrlText)
import Ecluse.Core.Security.Limits (BodyLimit (MetadataBodyLimit), Limits (..))
import Ecluse.Core.Server.Admission.Budget (scaleCharge)
import Ecluse.Core.Server.Admission.Meter (MemoryTicket, charge, newChargeRegion)
import Ecluse.Core.Server.Admission.Types (ChargeFactors (cfOutputPermille))
import Ecluse.Core.Server.Context (Handler, MountBinding (bindingPackumentDeps), PackumentDeps (..), ServeRuntime (..), ctxMount, ctxRuntime, pdPrivateBaseUrl, pdPublicBaseUrl)
import Ecluse.Core.Server.MirrorView (readMirrorCompanion, validateMirrorProject)
import Ecluse.Core.Server.Path (Filename, unFilename)
import Ecluse.Core.Server.Pipeline.Internal (VersionVerdict (VersionVerdict), logDenials, recordDenials, statusServeDecision)
import Ecluse.Core.Server.Pipeline.Origin (ObservedReadBoundExceeded (ObservedReadBoundExceeded), ReadFailure (ReadInvalid, ReadUnavailable), fetchReadFailure, statusReadFailure)
import Ecluse.Core.Server.Pipeline.Packument (DocumentRejectionObserver (..), PackumentReplies (..), answerMetadataBytes, mapPackumentReplies, noSurvivors, notFoundReply, withObservedAdmittedDocument)
import Ecluse.Core.Server.Pipeline.Shared (forwardedCredential, retryAfterHeaders, withListingAdmission, withMetadataAdmission)
import Ecluse.Core.Server.Pipeline.Tarball (headTarball, serveTarball)
import Ecluse.Core.Server.Pipeline.Tarball.Types (TarballReplies)
import Ecluse.Core.Server.Response (RejectReason (..), Rejection (rejectionReason), ServeDecision (Reject), mkRefusal, packumentStatus)
import Ecluse.Core.Telemetry.Metrics qualified as Metric
import Ecluse.Core.Telemetry.Record (MetricsPort (mpServeDecision))
import Ecluse.Core.Telemetry.Upstream (PublicReadKind (Companion))
import Ecluse.Core.Version (renderVersion)
serveGemArtifact :: TarballReplies response -> Method -> Filename -> Request -> (response -> IO ResponseReceived) -> (RubyResponse -> IO ResponseReceived) -> Handler ResponseReceived
serveGemArtifact :: forall response.
TarballReplies response
-> Method
-> Filename
-> Request
-> (response -> IO ResponseReceived)
-> (RubyResponse -> IO ResponseReceived)
-> Handler ResponseReceived
serveGemArtifact TarballReplies response
replies Method
method Filename
file Request
request response -> IO ResponseReceived
respond RubyResponse -> IO ResponseReceived
refuse =
(ServeRuntime
-> (ResponseHeaders -> Handler ResponseReceived)
-> (MemoryTicket
-> Handler (Either RubyResponse (GemRow, Provenance)))
-> (Either RubyResponse (GemRow, Provenance)
-> Handler ResponseReceived)
-> Handler ResponseReceived)
-> Request
-> (RubyResponse -> Handler ResponseReceived)
-> (PackumentDeps -> Either RubyResponse (GemRow, Provenance))
-> (MemoryTicket
-> PackumentDeps
-> Handler (Either RubyResponse (GemRow, Provenance)))
-> (Either RubyResponse (GemRow, Provenance)
-> Handler ResponseReceived)
-> Handler ResponseReceived
forall a.
(ServeRuntime
-> (ResponseHeaders -> Handler ResponseReceived)
-> (MemoryTicket -> Handler a)
-> (a -> Handler ResponseReceived)
-> Handler ResponseReceived)
-> Request
-> (RubyResponse -> Handler ResponseReceived)
-> (PackumentDeps -> a)
-> (MemoryTicket -> PackumentDeps -> Handler a)
-> (a -> Handler ResponseReceived)
-> Handler ResponseReceived
withNativeRequest
ServeRuntime
-> (ResponseHeaders -> Handler ResponseReceived)
-> (MemoryTicket
-> Handler (Either RubyResponse (GemRow, Provenance)))
-> (Either RubyResponse (GemRow, Provenance)
-> Handler ResponseReceived)
-> Handler ResponseReceived
forall (m :: * -> *) received a.
MonadUnliftIO m =>
ServeRuntime
-> (ResponseHeaders -> m received)
-> (MemoryTicket -> m a)
-> (a -> m received)
-> m received
withMetadataAdmission
Request
request
(IO ResponseReceived -> Handler ResponseReceived
forall a. IO a -> Handler a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO ResponseReceived -> Handler ResponseReceived)
-> (RubyResponse -> IO ResponseReceived)
-> RubyResponse
-> Handler ResponseReceived
forall b c a. (b -> c) -> (a -> b) -> a -> c
. RubyResponse -> IO ResponseReceived
refuse)
(Either RubyResponse (GemRow, Provenance)
-> PackumentDeps -> Either RubyResponse (GemRow, Provenance)
forall a b. a -> b -> a
const (RubyResponse -> Either RubyResponse (GemRow, Provenance)
forall a b. a -> Either a b
Left (PackumentReplies RubyResponse
-> ResponseHeaders -> Refusal -> RubyResponse
forall response.
PackumentReplies response -> ResponseHeaders -> Refusal -> response
packumentUnavailable PackumentReplies RubyResponse
rubyReplies [] (Maybe HelpMessage -> Text -> Refusal
mkRefusal Maybe HelpMessage
forall a. Maybe a
Nothing Text
"the coordinate lookup exceeded its deadline"))))
( \MemoryTicket
ticket PackumentDeps
deps -> do
observe <- IO (Int -> IO ()) -> Handler (Int -> IO ())
forall a. IO a -> Handler a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (Int -> IO (Int -> IO ())
newNativeReadBudget (Limits -> Int
maxMetadataBytes (PackumentDeps -> Limits
pdLimits PackumentDeps
deps)))
resolveCoordinate ticket observe request file
)
( \case
Left RubyResponse
failure -> IO ResponseReceived -> Handler ResponseReceived
forall a. IO a -> Handler a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (RubyResponse -> IO ResponseReceived
refuse RubyResponse
failure)
Right (GemRow
row, Provenance
_) ->
let coordinate :: GemCoordinate
coordinate = GemRow -> GemCoordinate
grCoordinate GemRow
row
in (if Method
method Method -> Method -> Bool
forall a. Eq a => a -> a -> Bool
== Method
"HEAD" then TarballReplies response
-> PackageName
-> Version
-> Filename
-> Request
-> (response -> IO ResponseReceived)
-> Handler ResponseReceived
forall response.
TarballReplies response
-> PackageName
-> Version
-> Filename
-> Request
-> (response -> IO ResponseReceived)
-> Handler ResponseReceived
headTarball else TarballReplies response
-> PackageName
-> Version
-> Filename
-> Request
-> (response -> IO ResponseReceived)
-> Handler ResponseReceived
forall response.
TarballReplies response
-> PackageName
-> Version
-> Filename
-> Request
-> (response -> IO ResponseReceived)
-> Handler ResponseReceived
serveTarball) TarballReplies response
replies (GemCoordinate -> PackageName
coordinateName GemCoordinate
coordinate) (GemCoordinate -> Version
coordinateVersion GemCoordinate
coordinate) Filename
file Request
request response -> IO ResponseReceived
respond
)
serveCompactSpec :: Filename -> Request -> (RubyResponse -> IO ResponseReceived) -> Handler ResponseReceived
serveCompactSpec :: Filename
-> Request
-> (RubyResponse -> IO ResponseReceived)
-> Handler ResponseReceived
serveCompactSpec Filename
file Request
request RubyResponse -> IO ResponseReceived
respond =
(ServeRuntime
-> (ResponseHeaders -> Handler ResponseReceived)
-> (MemoryTicket -> Handler RubyResponse)
-> (RubyResponse -> Handler ResponseReceived)
-> Handler ResponseReceived)
-> Request
-> (RubyResponse -> Handler ResponseReceived)
-> (PackumentDeps -> RubyResponse)
-> (MemoryTicket -> PackumentDeps -> Handler RubyResponse)
-> (RubyResponse -> Handler ResponseReceived)
-> Handler ResponseReceived
forall a.
(ServeRuntime
-> (ResponseHeaders -> Handler ResponseReceived)
-> (MemoryTicket -> Handler a)
-> (a -> Handler ResponseReceived)
-> Handler ResponseReceived)
-> Request
-> (RubyResponse -> Handler ResponseReceived)
-> (PackumentDeps -> a)
-> (MemoryTicket -> PackumentDeps -> Handler a)
-> (a -> Handler ResponseReceived)
-> Handler ResponseReceived
withNativeRequest
ServeRuntime
-> (ResponseHeaders -> Handler ResponseReceived)
-> (MemoryTicket -> Handler RubyResponse)
-> (RubyResponse -> Handler ResponseReceived)
-> Handler ResponseReceived
forall (m :: * -> *) received a.
MonadUnliftIO m =>
ServeRuntime
-> (ResponseHeaders -> m received)
-> (MemoryTicket -> m a)
-> (a -> m received)
-> m received
withListingAdmission
Request
request
(IO ResponseReceived -> Handler ResponseReceived
forall a. IO a -> Handler a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO ResponseReceived -> Handler ResponseReceived)
-> (RubyResponse -> IO ResponseReceived)
-> RubyResponse
-> Handler ResponseReceived
forall b c a. (b -> c) -> (a -> b) -> a -> c
. RubyResponse -> IO ResponseReceived
respond)
(\PackumentDeps
deps -> PackumentReplies RubyResponse
-> ResponseHeaders -> Refusal -> RubyResponse
forall response.
PackumentReplies response -> ResponseHeaders -> Refusal -> response
packumentUnavailable PackumentReplies RubyResponse
rubyReplies [] (Maybe HelpMessage -> Text -> Refusal
mkRefusal (PackumentDeps -> Maybe HelpMessage
pdHelp PackumentDeps
deps) Text
"the compact specification request exceeded its deadline"))
( \MemoryTicket
ticket PackumentDeps
deps -> do
observe <- IO (Int -> IO ()) -> Handler (Int -> IO ())
forall a. IO a -> Handler a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (Int -> IO (Int -> IO ())
newNativeReadBudget (Limits -> Int
maxMetadataBytes (PackumentDeps -> Limits
pdLimits PackumentDeps
deps)))
resolveCoordinate ticket observe request file >>= either pure (fetchCompact ticket observe request)
)
(IO ResponseReceived -> Handler ResponseReceived
forall a. IO a -> Handler a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO ResponseReceived -> Handler ResponseReceived)
-> (RubyResponse -> IO ResponseReceived)
-> RubyResponse
-> Handler ResponseReceived
forall b c a. (b -> c) -> (a -> b) -> a -> c
. RubyResponse -> IO ResponseReceived
respond)
data CandidateRead a
= CandidateAbsent
|
CandidateOwnedAbsent
| CandidateDenied RubyResponse
| CandidateRefused RubyResponse
| CandidateRejected CoordinateRejection
| CandidateAdmitted a
data CoordinateRejection = CoordinateRejection PackageName Text (Maybe DbEtag) Rejection
resolveCoordinate :: MemoryTicket -> (Int -> IO ()) -> Request -> Filename -> Handler (Either RubyResponse (GemRow, Provenance))
resolveCoordinate :: MemoryTicket
-> (Int -> IO ())
-> Request
-> Filename
-> Handler (Either RubyResponse (GemRow, Provenance))
resolveCoordinate MemoryTicket
ticket Int -> IO ()
observe Request
request Filename
file = do
deps <- (RequestCtx -> PackumentDeps) -> Handler PackumentDeps
forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks (MountBinding -> PackumentDeps
bindingPackumentDeps (MountBinding -> PackumentDeps)
-> (RequestCtx -> MountBinding) -> RequestCtx -> PackumentDeps
forall b c a. (b -> c) -> (a -> b) -> a -> c
. RequestCtx -> MountBinding
ctxMount)
case candidateCoordinates (maxMetadataBytes (pdLimits deps)) file of
Left ParseError
_ -> Either RubyResponse (GemRow, Provenance)
-> Handler (Either RubyResponse (GemRow, Provenance))
forall a. a -> Handler a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (RubyResponse -> Either RubyResponse (GemRow, Provenance)
forall a b. a -> Either a b
Left (Text -> RubyResponse
rubyBadRequest Text
"the native coordinate is invalid or ambiguous"))
Right [GemCoordinate]
candidates -> do
checkDocument <- IO
(Int
-> IO
(Either (CandidateRead (CachedDoc, Int, Map Text Provenance)) ()))
-> Handler
(Int
-> IO
(Either (CandidateRead (CachedDoc, Int, Map Text Provenance)) ()))
forall a. IO a -> Handler a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (Int
-> CandidateRead (CachedDoc, Int, Map Text Provenance)
-> IO
(Int
-> IO
(Either (CandidateRead (CachedDoc, Int, Map Text Provenance)) ()))
forall response.
Int -> response -> IO (Int -> IO (Either response ()))
newNativeDocumentBudget (Limits -> Int
maxMetadataBytes (PackumentDeps -> Limits
pdLimits PackumentDeps
deps)) (RubyResponse -> CandidateRead (CachedDoc, Int, Map Text Provenance)
forall a. RubyResponse -> CandidateRead a
CandidateRefused (Text -> RubyResponse
rubyBadRequest Text
"the coordinate lookup exceeds its cumulative metadata bound")))
collect deps observe checkDocument candidates 0 [] False Nothing (Set.toAscList (Set.fromList (map coordinateName candidates)))
where
collect :: PackumentDeps
-> (Int -> IO ())
-> (Int
-> IO
(Either (CandidateRead (CachedDoc, Int, Map Text Provenance)) ()))
-> [GemCoordinate]
-> Integer
-> [(GemRow, Provenance)]
-> Bool
-> Maybe (Either RubyResponse CoordinateRejection)
-> [PackageName]
-> Handler (Either RubyResponse (GemRow, Provenance))
collect PackumentDeps
deps Int -> IO ()
observer Int
-> IO
(Either (CandidateRead (CachedDoc, Int, Map Text Provenance)) ())
checkDocument [GemCoordinate]
candidates Integer
spent [(GemRow, Provenance)]
matches Bool
owned Maybe (Either RubyResponse CoordinateRejection)
denied = \case
[] -> case [(GemRow, Provenance)]
matches of
[(GemRow, Provenance)
selected] -> Either RubyResponse (GemRow, Provenance)
-> Handler (Either RubyResponse (GemRow, Provenance))
forall a. a -> Handler a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ((GemRow, Provenance) -> Either RubyResponse (GemRow, Provenance)
forall a b. b -> Either a b
Right (GemRow, Provenance)
selected)
[] -> case Maybe (Either RubyResponse CoordinateRejection)
denied of
Maybe (Either RubyResponse CoordinateRejection)
Nothing -> RubyResponse -> Either RubyResponse (GemRow, Provenance)
forall a b. a -> Either a b
Left (RubyResponse -> Either RubyResponse (GemRow, Provenance))
-> Handler RubyResponse
-> Handler (Either RubyResponse (GemRow, Provenance))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Bool -> Handler RubyResponse
absentCoordinate Bool
owned
Just (Left RubyResponse
refusal) -> Either RubyResponse (GemRow, Provenance)
-> Handler (Either RubyResponse (GemRow, Provenance))
forall a. a -> Handler a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (RubyResponse -> Either RubyResponse (GemRow, Provenance)
forall a b. a -> Either a b
Left RubyResponse
refusal)
Just (Right CoordinateRejection
rejection) -> RubyResponse -> Either RubyResponse (GemRow, Provenance)
forall a b. a -> Either a b
Left (RubyResponse -> Either RubyResponse (GemRow, Provenance))
-> Handler RubyResponse
-> Handler (Either RubyResponse (GemRow, Provenance))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> PackumentDeps -> CoordinateRejection -> Handler RubyResponse
coordinateRefusal PackumentDeps
deps CoordinateRejection
rejection
[(GemRow, Provenance)]
_ -> Either RubyResponse (GemRow, Provenance)
-> Handler (Either RubyResponse (GemRow, Provenance))
forall a. a -> Handler a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (RubyResponse -> Either RubyResponse (GemRow, Provenance)
forall a b. a -> Either a b
Left (Text -> RubyResponse
rubyBadRequest Text
"the native coordinate names more than one admitted artifact"))
PackageName
name : [PackageName]
names -> do
rejected <- IO (IORef (Maybe CoordinateRejection))
-> Handler (IORef (Maybe CoordinateRejection))
forall a. IO a -> Handler a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (Maybe CoordinateRejection -> IO (IORef (Maybe CoordinateRejection))
forall (m :: * -> *) a. MonadIO m => a -> m (IORef a)
newIORef Maybe CoordinateRejection
forall a. Maybe a
Nothing)
let candidateDenied ResponseHeaders
headers Refusal
refusal = RubyResponse -> CandidateRead a
forall a. RubyResponse -> CandidateRead a
CandidateDenied (PackumentReplies RubyResponse
-> ResponseHeaders -> Refusal -> RubyResponse
forall response.
PackumentReplies response -> ResponseHeaders -> Refusal -> response
packumentForbidden PackumentReplies RubyResponse
rubyReplies ResponseHeaders
headers Refusal
refusal)
replies =
((RubyResponse -> CandidateRead a)
-> PackumentReplies RubyResponse
-> PackumentReplies (CandidateRead a)
forall a b. (a -> b) -> PackumentReplies a -> PackumentReplies b
mapPackumentReplies RubyResponse -> CandidateRead a
forall a. RubyResponse -> CandidateRead a
CandidateRefused PackumentReplies RubyResponse
rubyReplies)
{ packumentNotFound = \ResponseHeaders
_ Refusal
_ -> CandidateRead a
forall a. CandidateRead a
CandidateOwnedAbsent
, packumentDenied = candidateDenied
, packumentForbidden = candidateDenied
}
recordRejected EvalContext
ctx PackageDetails
details Rejection
rejection =
Bool -> f () -> f ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when ((GemCoordinate -> Bool) -> [GemCoordinate] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any (\GemCoordinate
candidate -> GemCoordinate -> PackageName
coordinateName GemCoordinate
candidate PackageName -> PackageName -> Bool
forall a. Eq a => a -> a -> Bool
== PackageDetails -> PackageName
pkgName PackageDetails
details Bool -> Bool -> Bool
&& GemCoordinate -> Version
coordinateVersion GemCoordinate
candidate Version -> Version -> Bool
forall a. Eq a => a -> a -> Bool
== PackageDetails -> Version
pkgVersion PackageDetails
details) [GemCoordinate]
candidates Bool -> Bool -> Bool
&& (Artifact -> Bool) -> NonEmpty Artifact -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any ((Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== Filename -> Text
unFilename Filename
file) (Text -> Bool) -> (Artifact -> Text) -> Artifact -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Artifact -> Text
artFilename) (PackageDetails -> NonEmpty Artifact
pkgArtifacts PackageDetails
details)) (f () -> f ()) -> f () -> f ()
forall a b. (a -> b) -> a -> b
$
IORef (Maybe CoordinateRejection)
-> (Maybe CoordinateRejection -> (Maybe CoordinateRejection, ()))
-> f ()
forall (m :: * -> *) a b.
MonadIO m =>
IORef a -> (a -> (a, b)) -> m b
atomicModifyIORef' IORef (Maybe CoordinateRejection)
rejected ((Maybe CoordinateRejection -> (Maybe CoordinateRejection, ()))
-> f ())
-> (Maybe CoordinateRejection -> (Maybe CoordinateRejection, ()))
-> f ()
forall a b. (a -> b) -> a -> b
$ \Maybe CoordinateRejection
previous ->
let current :: CoordinateRejection
current = PackageName
-> Text -> Maybe DbEtag -> Rejection -> CoordinateRejection
CoordinateRejection (PackageDetails -> PackageName
pkgName PackageDetails
details) (Version -> Text
renderVersion (PackageDetails -> Version
pkgVersion PackageDetails
details)) (EvalContext -> Maybe DbEtag
ctxAdvisoryEtag EvalContext
ctx) Rejection
rejection
in (if Rejection -> Bool
terminalRejection Rejection
rejection then CoordinateRejection -> Maybe CoordinateRejection
forall a. a -> Maybe a
Just CoordinateRejection
current else Maybe CoordinateRejection
previous Maybe CoordinateRejection
-> Maybe CoordinateRejection -> Maybe CoordinateRejection
forall a. Maybe a -> Maybe a -> Maybe a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> CoordinateRejection -> Maybe CoordinateRejection
forall a. a -> Maybe a
Just CoordinateRejection
current, ())
rejectionObserver =
DocumentRejectionObserver
{ observeVersionRejection :: EvalContext -> PackageDetails -> Rejection -> IO ()
observeVersionRejection = EvalContext -> PackageDetails -> Rejection -> IO ()
forall {f :: * -> *}.
MonadIO f =>
EvalContext -> PackageDetails -> Rejection -> f ()
recordRejected
, onCheckedNoSurvivorAdmission :: IO (CandidateRead a)
onCheckedNoSurvivorAdmission = CandidateRead a
-> (CoordinateRejection -> CandidateRead a)
-> Maybe CoordinateRejection
-> CandidateRead a
forall b a. b -> (a -> b) -> Maybe a -> b
maybe CandidateRead a
forall a. CandidateRead a
CandidateAbsent CoordinateRejection -> CandidateRead a
forall a. CoordinateRejection -> CandidateRead a
CandidateRejected (Maybe CoordinateRejection -> CandidateRead a)
-> IO (Maybe CoordinateRejection) -> IO (CandidateRead a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> IORef (Maybe CoordinateRejection) -> IO (Maybe CoordinateRejection)
forall (m :: * -> *) a. MonadIO m => IORef a -> m a
readIORef IORef (Maybe CoordinateRejection)
rejected
, coordinateVersions :: Set Text
coordinateVersions = [Text] -> Set Text
forall a. Ord a => [a] -> Set a
Set.fromList [Version -> Text
renderVersion (GemCoordinate -> Version
coordinateVersion GemCoordinate
candidate) | GemCoordinate
candidate <- [GemCoordinate]
candidates, GemCoordinate -> PackageName
coordinateName GemCoordinate
candidate PackageName -> PackageName -> Bool
forall a. Eq a => a -> a -> Bool
== PackageName
name]
}
continueRejected current :: CoordinateRejection
current@(CoordinateRejection PackageName
_ Text
_ Maybe DbEtag
_ Rejection
rejection) Maybe (Either RubyResponse CoordinateRejection)
-> [PackageName] -> Handler (Either RubyResponse b)
next
| Rejection -> Bool
terminalRejection Rejection
rejection = RubyResponse -> Either RubyResponse b
forall a b. a -> Either a b
Left (RubyResponse -> Either RubyResponse b)
-> Handler RubyResponse -> Handler (Either RubyResponse b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> PackumentDeps -> CoordinateRejection -> Handler RubyResponse
coordinateRefusal PackumentDeps
deps CoordinateRejection
current
| Bool
otherwise = Maybe (Either RubyResponse CoordinateRejection)
-> [PackageName] -> Handler (Either RubyResponse b)
next (Maybe (Either RubyResponse CoordinateRejection)
denied Maybe (Either RubyResponse CoordinateRejection)
-> Maybe (Either RubyResponse CoordinateRejection)
-> Maybe (Either RubyResponse CoordinateRejection)
forall a. Maybe a -> Maybe a -> Maybe a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Either RubyResponse CoordinateRejection
-> Maybe (Either RubyResponse CoordinateRejection)
forall a. a -> Maybe a
Just (CoordinateRejection -> Either RubyResponse CoordinateRejection
forall a b. b -> Either a b
Right CoordinateRejection
current)) [PackageName]
names
result <- withObservedAdmittedDocument replies ticket observer checkDocument (Just rejectionObserver) name request (Just CandidateAbsent) $ \CachedDoc
cached Int
sourceBytes Map Text Provenance
origins -> do
IO () -> Handler ()
forall a. IO a -> Handler a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (MemoryTicket -> Int -> IO ()
charge MemoryTicket
ticket (Int -> Int -> Int
scaleCharge (ChargeFactors -> Int
cfOutputPermille (AdapterMetadata -> ChargeFactors
metadataChargeFactors (PackumentDeps -> AdapterMetadata
pdMetadata PackumentDeps
deps))) Int
sourceBytes))
CandidateRead (CachedDoc, Int, Map Text Provenance)
-> Handler (CandidateRead (CachedDoc, Int, Map Text Provenance))
forall a. a -> Handler a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ((CachedDoc, Int, Map Text Provenance)
-> CandidateRead (CachedDoc, Int, Map Text Provenance)
forall a. a -> CandidateRead a
CandidateAdmitted (CachedDoc
cached, Int
sourceBytes, Map Text Provenance
origins))
case result of
CandidateRefused RubyResponse
refusal -> Either RubyResponse (GemRow, Provenance)
-> Handler (Either RubyResponse (GemRow, Provenance))
forall a. a -> Handler a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (RubyResponse -> Either RubyResponse (GemRow, Provenance)
forall a b. a -> Either a b
Left RubyResponse
refusal)
CandidateRead (CachedDoc, Int, Map Text Provenance)
CandidateAbsent -> PackumentDeps
-> (Int -> IO ())
-> (Int
-> IO
(Either (CandidateRead (CachedDoc, Int, Map Text Provenance)) ()))
-> [GemCoordinate]
-> Integer
-> [(GemRow, Provenance)]
-> Bool
-> Maybe (Either RubyResponse CoordinateRejection)
-> [PackageName]
-> Handler (Either RubyResponse (GemRow, Provenance))
collect PackumentDeps
deps Int -> IO ()
observer Int
-> IO
(Either (CandidateRead (CachedDoc, Int, Map Text Provenance)) ())
checkDocument [GemCoordinate]
candidates Integer
spent [(GemRow, Provenance)]
matches Bool
owned Maybe (Either RubyResponse CoordinateRejection)
denied [PackageName]
names
CandidateRead (CachedDoc, Int, Map Text Provenance)
CandidateOwnedAbsent -> PackumentDeps
-> (Int -> IO ())
-> (Int
-> IO
(Either (CandidateRead (CachedDoc, Int, Map Text Provenance)) ()))
-> [GemCoordinate]
-> Integer
-> [(GemRow, Provenance)]
-> Bool
-> Maybe (Either RubyResponse CoordinateRejection)
-> [PackageName]
-> Handler (Either RubyResponse (GemRow, Provenance))
collect PackumentDeps
deps Int -> IO ()
observer Int
-> IO
(Either (CandidateRead (CachedDoc, Int, Map Text Provenance)) ())
checkDocument [GemCoordinate]
candidates Integer
spent [(GemRow, Provenance)]
matches Bool
True Maybe (Either RubyResponse CoordinateRejection)
denied [PackageName]
names
CandidateDenied RubyResponse
refusal -> PackumentDeps
-> (Int -> IO ())
-> (Int
-> IO
(Either (CandidateRead (CachedDoc, Int, Map Text Provenance)) ()))
-> [GemCoordinate]
-> Integer
-> [(GemRow, Provenance)]
-> Bool
-> Maybe (Either RubyResponse CoordinateRejection)
-> [PackageName]
-> Handler (Either RubyResponse (GemRow, Provenance))
collect PackumentDeps
deps Int -> IO ()
observer Int
-> IO
(Either (CandidateRead (CachedDoc, Int, Map Text Provenance)) ())
checkDocument [GemCoordinate]
candidates Integer
spent [(GemRow, Provenance)]
matches Bool
owned (Maybe (Either RubyResponse CoordinateRejection)
denied Maybe (Either RubyResponse CoordinateRejection)
-> Maybe (Either RubyResponse CoordinateRejection)
-> Maybe (Either RubyResponse CoordinateRejection)
forall a. Maybe a -> Maybe a -> Maybe a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Either RubyResponse CoordinateRejection
-> Maybe (Either RubyResponse CoordinateRejection)
forall a. a -> Maybe a
Just (RubyResponse -> Either RubyResponse CoordinateRejection
forall a b. a -> Either a b
Left RubyResponse
refusal)) [PackageName]
names
CandidateRejected CoordinateRejection
current -> CoordinateRejection
-> (Maybe (Either RubyResponse CoordinateRejection)
-> [PackageName]
-> Handler (Either RubyResponse (GemRow, Provenance)))
-> Handler (Either RubyResponse (GemRow, Provenance))
forall {b}.
CoordinateRejection
-> (Maybe (Either RubyResponse CoordinateRejection)
-> [PackageName] -> Handler (Either RubyResponse b))
-> Handler (Either RubyResponse b)
continueRejected CoordinateRejection
current (PackumentDeps
-> (Int -> IO ())
-> (Int
-> IO
(Either (CandidateRead (CachedDoc, Int, Map Text Provenance)) ()))
-> [GemCoordinate]
-> Integer
-> [(GemRow, Provenance)]
-> Bool
-> Maybe (Either RubyResponse CoordinateRejection)
-> [PackageName]
-> Handler (Either RubyResponse (GemRow, Provenance))
collect PackumentDeps
deps Int -> IO ()
observer Int
-> IO
(Either (CandidateRead (CachedDoc, Int, Map Text Provenance)) ())
checkDocument [GemCoordinate]
candidates Integer
spent [(GemRow, Provenance)]
matches Bool
owned)
CandidateAdmitted (CachedDoc
cached, Int
sourceBytes, Map Text Provenance
origins)
| Integer
spent Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ Int -> Integer
forall a. Integral a => a -> Integer
toInteger Int
sourceBytes Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
> Int -> Integer
forall a. Integral a => a -> Integer
toInteger (Limits -> Int
maxMetadataBytes (PackumentDeps -> Limits
pdLimits PackumentDeps
deps)) -> Either RubyResponse (GemRow, Provenance)
-> Handler (Either RubyResponse (GemRow, Provenance))
forall a. a -> Handler a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (RubyResponse -> Either RubyResponse (GemRow, Provenance)
forall a b. a -> Either a b
Left (Text -> RubyResponse
rubyBadRequest Text
"the coordinate lookup exceeds its cumulative metadata bound"))
| Bool
otherwise -> case (GemDocument -> CachedDoc, CachedDoc -> Maybe GemDocument)
-> CachedDoc -> Maybe GemDocument
forall a b. (a, b) -> b
snd (GemDocument -> CachedDoc, CachedDoc -> Maybe GemDocument)
rubyGemsCached CachedDoc
cached of
Maybe GemDocument
Nothing -> Either RubyResponse (GemRow, Provenance)
-> Handler (Either RubyResponse (GemRow, Provenance))
forall a. a -> Handler a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (RubyResponse -> Either RubyResponse (GemRow, Provenance)
forall a b. a -> Either a b
Left (PackumentReplies RubyResponse
-> ResponseHeaders -> Refusal -> RubyResponse
forall response.
PackumentReplies response -> ResponseHeaders -> Refusal -> response
packumentInternal PackumentReplies RubyResponse
rubyReplies [] (Maybe HelpMessage -> Text -> Refusal
mkRefusal Maybe HelpMessage
forall a. Maybe a
Nothing Text
"the admitted coordinate source is not RubyGems metadata")))
Just GemDocument
document -> do
let found :: [(GemRow, Provenance)]
found = [(GemRow
row, Provenance
origin) | GemRow
row <- GemDocument -> [GemRow]
gemRows GemDocument
document, GemRow -> GemCoordinate
grCoordinate GemRow
row GemCoordinate -> [GemCoordinate] -> Bool
forall (f :: * -> *) a.
(Foldable f, DisallowElem f, Eq a) =>
a -> f a -> Bool
`elem` [GemCoordinate]
candidates, GemCoordinate -> Filename
coordinateFilename (GemRow -> GemCoordinate
grCoordinate GemRow
row) Filename -> Filename -> Bool
forall a. Eq a => a -> a -> Bool
== Filename
file, Just Provenance
origin <- [Text -> Map Text Provenance -> Maybe Provenance
forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup (Version -> Text
renderVersion (GemCoordinate -> Version
coordinateVersion (GemRow -> GemCoordinate
grCoordinate GemRow
row))) Map Text Provenance
origins]]
next :: Maybe (Either RubyResponse CoordinateRejection)
-> [PackageName]
-> Handler (Either RubyResponse (GemRow, Provenance))
next = PackumentDeps
-> (Int -> IO ())
-> (Int
-> IO
(Either (CandidateRead (CachedDoc, Int, Map Text Provenance)) ()))
-> [GemCoordinate]
-> Integer
-> [(GemRow, Provenance)]
-> Bool
-> Maybe (Either RubyResponse CoordinateRejection)
-> [PackageName]
-> Handler (Either RubyResponse (GemRow, Provenance))
collect PackumentDeps
deps Int -> IO ()
observer Int
-> IO
(Either (CandidateRead (CachedDoc, Int, Map Text Provenance)) ())
checkDocument [GemCoordinate]
candidates (Integer
spent Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ Int -> Integer
forall a. Integral a => a -> Integer
toInteger Int
sourceBytes) ([(GemRow, Provenance)]
matches [(GemRow, Provenance)]
-> [(GemRow, Provenance)] -> [(GemRow, Provenance)]
forall a. Semigroup a => a -> a -> a
<> [(GemRow, Provenance)]
found) Bool
owned
refusal <- if [(GemRow, Provenance)] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [(GemRow, Provenance)]
found then IO (Maybe CoordinateRejection)
-> Handler (Maybe CoordinateRejection)
forall a. IO a -> Handler a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IORef (Maybe CoordinateRejection) -> IO (Maybe CoordinateRejection)
forall (m :: * -> *) a. MonadIO m => IORef a -> m a
readIORef IORef (Maybe CoordinateRejection)
rejected) else Maybe CoordinateRejection -> Handler (Maybe CoordinateRejection)
forall a. a -> Handler a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe CoordinateRejection
forall a. Maybe a
Nothing
case refusal of
Just CoordinateRejection
current -> CoordinateRejection
-> (Maybe (Either RubyResponse CoordinateRejection)
-> [PackageName]
-> Handler (Either RubyResponse (GemRow, Provenance)))
-> Handler (Either RubyResponse (GemRow, Provenance))
forall {b}.
CoordinateRejection
-> (Maybe (Either RubyResponse CoordinateRejection)
-> [PackageName] -> Handler (Either RubyResponse b))
-> Handler (Either RubyResponse b)
continueRejected CoordinateRejection
current Maybe (Either RubyResponse CoordinateRejection)
-> [PackageName]
-> Handler (Either RubyResponse (GemRow, Provenance))
next
Maybe CoordinateRejection
Nothing -> Maybe (Either RubyResponse CoordinateRejection)
-> [PackageName]
-> Handler (Either RubyResponse (GemRow, Provenance))
next Maybe (Either RubyResponse CoordinateRejection)
denied [PackageName]
names
absentCoordinate :: Bool -> Handler RubyResponse
absentCoordinate :: Bool -> Handler RubyResponse
absentCoordinate Bool
owned
| Bool
owned = RubyResponse -> Handler RubyResponse
forall a. a -> Handler a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (PackumentReplies RubyResponse
-> ResponseHeaders -> Refusal -> RubyResponse
forall response.
PackumentReplies response -> ResponseHeaders -> Refusal -> response
packumentNotFound PackumentReplies RubyResponse
rubyReplies [] (Maybe HelpMessage -> Text -> Refusal
mkRefusal Maybe HelpMessage
forall a. Maybe a
Nothing Text
message))
| Bool
otherwise = do
rt <- (RequestCtx -> ServeRuntime) -> Handler ServeRuntime
forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks RequestCtx -> ServeRuntime
ctxRuntime
liftIO (notFoundReply (srMetrics rt) rubyReplies Nothing message)
where
message :: Text
message = Text
"the native coordinate is absent"
terminalRejection :: Rejection -> Bool
terminalRejection :: Rejection -> Bool
terminalRejection Rejection
rejection = case Rejection -> RejectReason
rejectionReason Rejection
rejection of
Unavailable{} -> Bool
True
RejectReason
UpstreamInvalid -> Bool
True
ByPolicy{} -> Bool
False
RejectReason
MissingIntegrity -> Bool
False
RejectReason
BelowIntegrityFloor -> Bool
False
coordinateRefusal :: PackumentDeps -> CoordinateRejection -> Handler RubyResponse
coordinateRefusal :: PackumentDeps -> CoordinateRejection -> Handler RubyResponse
coordinateRefusal PackumentDeps
deps (CoordinateRejection PackageName
name Text
version Maybe DbEtag
etag Rejection
rejection) = do
rt <- (RequestCtx -> ServeRuntime) -> Handler ServeRuntime
forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks RequestCtx -> ServeRuntime
ctxRuntime
let decision = Rejection -> ServeDecision
Reject Rejection
rejection
status = [ServeDecision] -> PackumentStatus
packumentStatus [ServeDecision
decision]
liftIO $ do
recordDenials (srMetrics rt) [decision]
mpServeDecision (srMetrics rt) (statusServeDecision status)
logDenials name etag [VersionVerdict version decision]
pure (noSurvivors coordinateReplies (pdHelp deps) status [decision])
coordinateReplies :: PackumentReplies RubyResponse
coordinateReplies :: PackumentReplies RubyResponse
coordinateReplies = PackumentReplies RubyResponse
rubyReplies{packumentDenied = packumentForbidden rubyReplies}
fetchCompact :: MemoryTicket -> (Int -> IO ()) -> Request -> (GemRow, Provenance) -> Handler RubyResponse
fetchCompact :: MemoryTicket
-> (Int -> IO ())
-> Request
-> (GemRow, Provenance)
-> Handler RubyResponse
fetchCompact MemoryTicket
ticket Int -> IO ()
observe Request
request (GemRow
row, Provenance
provenance) = do
mount <- (RequestCtx -> MountBinding) -> Handler MountBinding
forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks RequestCtx -> MountBinding
ctxMount
rt <- asks ctxRuntime
liftIO (mpServeDecision (srMetrics rt) Metric.Admit)
let deps = MountBinding -> PackumentDeps
bindingPackumentDeps MountBinding
mount
coordinate = GemRow -> GemCoordinate
grCoordinate GemRow
row
limits = PackumentDeps -> Limits
pdLimits PackumentDeps
deps
token = MountBinding -> Request -> Maybe ClientCredential
forwardedCredential MountBinding
mount Request
request
unavailableAfter Maybe RetryAfter
retry = PackumentReplies RubyResponse
-> ResponseHeaders -> Refusal -> RubyResponse
forall response.
PackumentReplies response -> ResponseHeaders -> Refusal -> response
packumentUnavailable PackumentReplies RubyResponse
rubyReplies (Maybe RetryAfter -> ResponseHeaders
retryAfterHeaders Maybe RetryAfter
retry) (Maybe HelpMessage -> Text -> Refusal
mkRefusal (PackumentDeps -> Maybe HelpMessage
pdHelp PackumentDeps
deps) Text
"the compact specification source is unavailable")
unavailable = Maybe RetryAfter -> RubyResponse
unavailableAfter Maybe RetryAfter
forall a. Maybe a
Nothing
invalid = PackumentReplies RubyResponse
-> ResponseHeaders -> Refusal -> RubyResponse
forall response.
PackumentReplies response -> ResponseHeaders -> Refusal -> response
packumentBadGateway PackumentReplies RubyResponse
rubyReplies [] (Maybe HelpMessage -> Text -> Refusal
mkRefusal (PackumentDeps -> Maybe HelpMessage
pdHelp PackumentDeps
deps) Text
"the compact specification disagrees with its admitted coordinate")
privateFailure = \case
ReadUnavailable Maybe RetryAfter
retry -> Maybe RetryAfter -> RubyResponse
unavailableAfter Maybe RetryAfter
retry
ReadFailure
ReadInvalid -> PackumentReplies RubyResponse
-> ResponseHeaders -> Refusal -> RubyResponse
forall response.
PackumentReplies response -> ResponseHeaders -> Refusal -> response
packumentBadGateway PackumentReplies RubyResponse
rubyReplies [] (Maybe HelpMessage -> Text -> Refusal
mkRefusal (PackumentDeps -> Maybe HelpMessage
pdHelp PackumentDeps
deps) Text
"the private compact specification source returned an invalid or unusable reply")
retained <- case (provenance, pdMirrorState deps) of
(Provenance
TrustedSource, Just MirrorView
mirror) ->
IO
(Either
MirrorStateFault (Maybe (StateRevision, ProjectMirrorRecord)))
-> Handler
(Either
MirrorStateFault (Maybe (StateRevision, ProjectMirrorRecord)))
forall a. IO a -> Handler a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (MirrorView
-> MemoryTicket
-> PackageName
-> IO
(Either
MirrorStateFault (Maybe (StateRevision, ProjectMirrorRecord)))
validateMirrorProject MirrorView
mirror MemoryTicket
ticket (GemCoordinate -> PackageName
coordinateName GemCoordinate
coordinate)) Handler
(Either
MirrorStateFault (Maybe (StateRevision, ProjectMirrorRecord)))
-> (Either
MirrorStateFault (Maybe (StateRevision, ProjectMirrorRecord))
-> Handler (Maybe (Either RubyResponse Method)))
-> Handler (Maybe (Either RubyResponse Method))
forall a b. Handler a -> (a -> Handler b) -> Handler b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
Left MirrorStateFault
_ -> Maybe (Either RubyResponse Method)
-> Handler (Maybe (Either RubyResponse Method))
forall a. a -> Handler a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Either RubyResponse Method -> Maybe (Either RubyResponse Method)
forall a. a -> Maybe a
Just (RubyResponse -> Either RubyResponse Method
forall a b. a -> Either a b
Left RubyResponse
unavailable))
Right Maybe (StateRevision, ProjectMirrorRecord)
Nothing -> Maybe (Either RubyResponse Method)
-> Handler (Maybe (Either RubyResponse Method))
forall a. a -> Handler a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe (Either RubyResponse Method)
forall a. Maybe a
Nothing
Right (Just (StateRevision
_, ProjectMirrorRecord
record)) -> case Version -> ProjectMirrorRecord -> Maybe MirrorVersion
recordVersion (GemCoordinate -> Version
coordinateVersion GemCoordinate
coordinate) ProjectMirrorRecord
record of
Just MirrorVersion
version
| Text -> Map Text ExpectedMirrorFile -> Bool
forall k a. Ord k => k -> Map k a -> Bool
Map.member (Filename -> Text
unFilename (GemCoordinate -> Filename
coordinateFilename GemCoordinate
coordinate)) (MirrorVersion -> Map Text ExpectedMirrorFile
versionFiles MirrorVersion
version) ->
Either RubyResponse Method -> Maybe (Either RubyResponse Method)
forall a. a -> Maybe a
Just (Either RubyResponse Method -> Maybe (Either RubyResponse Method))
-> (Either MirrorStateFault Method -> Either RubyResponse Method)
-> Either MirrorStateFault Method
-> Maybe (Either RubyResponse Method)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (MirrorStateFault -> RubyResponse)
-> Either MirrorStateFault Method -> Either RubyResponse Method
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 (RubyResponse -> MirrorStateFault -> RubyResponse
forall a b. a -> b -> a
const RubyResponse
unavailable) (Either MirrorStateFault Method
-> Maybe (Either RubyResponse Method))
-> Handler (Either MirrorStateFault Method)
-> Handler (Maybe (Either RubyResponse Method))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> IO (Either MirrorStateFault Method)
-> Handler (Either MirrorStateFault Method)
forall a. IO a -> Handler a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (MirrorView
-> MemoryTicket
-> ProjectMirrorRecord
-> Version
-> Text
-> IO (Either MirrorStateFault Method)
readMirrorCompanion MirrorView
mirror MemoryTicket
ticket ProjectMirrorRecord
record (GemCoordinate -> Version
coordinateVersion GemCoordinate
coordinate) (Filename -> Text
unFilename (GemCoordinate -> Filename
coordinateFilename GemCoordinate
coordinate)))
Maybe MirrorVersion
_ -> Maybe (Either RubyResponse Method)
-> Handler (Maybe (Either RubyResponse Method))
forall a. a -> Handler a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe (Either RubyResponse Method)
forall a. Maybe a
Nothing
(Provenance, Maybe MirrorView)
_ -> Maybe (Either RubyResponse Method)
-> Handler (Maybe (Either RubyResponse Method))
forall a. a -> Handler a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe (Either RubyResponse Method)
forall a. Maybe a
Nothing
bytes <- case retained of
Just Either RubyResponse Method
result -> Either RubyResponse Method -> Handler (Either RubyResponse Method)
forall a. a -> Handler a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Either RubyResponse Method
result
Maybe (Either RubyResponse Method)
Nothing -> case PackumentDeps -> Provenance -> Maybe RegistryUrl
sourceLocation PackumentDeps
deps Provenance
provenance of
Maybe RegistryUrl
Nothing -> Either RubyResponse Method -> Handler (Either RubyResponse Method)
forall a. a -> Handler a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (RubyResponse -> Either RubyResponse Method
forall a b. a -> Either a b
Left RubyResponse
unavailable)
Just RegistryUrl
base -> do
let private :: Bool
private = Provenance
provenance Provenance -> Provenance -> Bool
forall a. Eq a => a -> a -> Bool
== Provenance
TrustedSource
client :: ExchangeClient
client = if Bool
private then ServeRuntime -> ExchangeClient
srPrivateClient ServeRuntime
rt else ServeRuntime -> ExchangeClient
srPublicClient ServeRuntime
rt
credential :: Maybe ClientCredential
credential = if Bool
private then Maybe ClientCredential
token else Maybe ClientCredential
forall a. Maybe a
Nothing
control :: ReadControl
control = if Bool
private then ReadControl
Uncontrolled else PublicControl -> PublicReadKind -> ReadControl
Controlled (ServeRuntime -> PublicControl
srPublicControl ServeRuntime
rt) PublicReadKind
Companion
pay <- IO (Int -> IO ()) -> Handler (Int -> IO ())
forall a. IO a -> Handler a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO ((Int -> IO ()) -> IO (Int -> IO ())
newChargeRegion (MemoryTicket -> Int -> IO ()
charge MemoryTicket
ticket))
liftIO
( try
( formThen
FetchUrlUnformable
(withSuccessBodyUsing control client (progressFloor limits) (boundedChargedRead pay (MetadataBodyLimit (maxMetadataBytes limits)) . chargedRead observe))
(gemCompactSpecUrl (registryUrlText base) coordinate >>= artifactRequestByUrl rubyReadCredential credential)
)
)
<&> \case
Left (ObservedReadBoundExceeded LimitError
fault) -> RubyResponse -> Either RubyResponse Method
forall a b. a -> Either a b
Left (PackumentReplies RubyResponse
-> ResponseHeaders -> Refusal -> RubyResponse
forall response.
PackumentReplies response -> ResponseHeaders -> Refusal -> response
packumentBadGateway PackumentReplies RubyResponse
rubyReplies [] (Maybe HelpMessage -> Text -> Refusal
mkRefusal Maybe HelpMessage
forall a. Maybe a
Nothing (Text
"the cumulative compact input exceeded its bound: " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> LimitError -> Text
forall b a. (Show a, IsString b) => a -> b
show LimitError
fault)))
Right (Right (SuccessBody Int
_ (Int
_, Method
body))) -> Method -> Either RubyResponse Method
forall a b. b -> Either a b
Right Method
body
Right (Right (UnreadStatus Int
code Maybe RetryAfter
_)) | Bool
private Bool -> Bool -> Bool
&& (Int
code Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
401 Bool -> Bool -> Bool
|| Int
code Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
403) -> RubyResponse -> Either RubyResponse Method
forall a b. a -> Either a b
Left (PackumentReplies RubyResponse
-> Int -> ResponseHeaders -> Refusal -> RubyResponse
forall response.
PackumentReplies response
-> Int -> ResponseHeaders -> Refusal -> response
packumentPrivateRefused PackumentReplies RubyResponse
rubyReplies Int
code [] (Maybe HelpMessage -> Text -> Refusal
mkRefusal Maybe HelpMessage
forall a. Maybe a
Nothing Text
"the private compact specification source refused access"))
Right (Right (UnreadStatus Int
404 Maybe RetryAfter
_)) -> RubyResponse -> Either RubyResponse Method
forall a b. a -> Either a b
Left (PackumentReplies RubyResponse
-> ResponseHeaders -> Refusal -> RubyResponse
forall response.
PackumentReplies response -> ResponseHeaders -> Refusal -> response
packumentNotFound PackumentReplies RubyResponse
rubyReplies [] (Maybe HelpMessage -> Text -> Refusal
mkRefusal Maybe HelpMessage
forall a. Maybe a
Nothing Text
"the compact specification is absent"))
Right (Right (UnreadStatus Int
code Maybe RetryAfter
retry)) | Bool
private -> RubyResponse -> Either RubyResponse Method
forall a b. a -> Either a b
Left (ReadFailure -> RubyResponse
privateFailure (Int -> Maybe RetryAfter -> ReadFailure
statusReadFailure Int
code Maybe RetryAfter
retry))
Right (Left FetchFault
fault) | Bool
private -> RubyResponse -> Either RubyResponse Method
forall a b. a -> Either a b
Left (ReadFailure -> RubyResponse
privateFailure (FetchFault -> ReadFailure
fetchReadFailure FetchFault
fault))
Either
ObservedReadBoundExceeded
(Either FetchFault (BodyOutcome (Int, Method)))
_ -> RubyResponse -> Either RubyResponse Method
forall a b. a -> Either a b
Left RubyResponse
unavailable
case bytes of
Left RubyResponse
refusal -> RubyResponse -> Handler RubyResponse
forall a. a -> Handler a
forall (f :: * -> *) a. Applicative f => a -> f a
pure RubyResponse
refusal
Right Method
body -> do
pay <- IO (Int -> IO ()) -> Handler (Int -> IO ())
forall a. IO a -> Handler a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO ((Int -> IO ()) -> IO (Int -> IO ())
newChargeRegion (MemoryTicket -> Int -> IO ()
charge MemoryTicket
ticket))
checked <- liftIO (validateCompactSpec limits pay coordinate row body)
case checked of
Left ParseError
_ -> RubyResponse -> Handler RubyResponse
forall a. a -> Handler a
forall (f :: * -> *) a. Applicative f => a -> f a
pure RubyResponse
invalid
Right CompactSpec
compact -> PackumentReplies RubyResponse
-> PackumentDeps
-> MemoryTicket
-> Request
-> Method
-> Handler RubyResponse
forall response.
PackumentReplies response
-> PackumentDeps
-> MemoryTicket
-> Request
-> Method
-> Handler response
answerMetadataBytes PackumentReplies RubyResponse
rubyReplies PackumentDeps
deps MemoryTicket
ticket Request
request (CompactSpec -> Method
compactSpecBytes CompactSpec
compact)
sourceLocation :: PackumentDeps -> Provenance -> Maybe RegistryUrl
sourceLocation :: PackumentDeps -> Provenance -> Maybe RegistryUrl
sourceLocation PackumentDeps
deps Provenance
TrustedSource = PackumentDeps -> Maybe RegistryUrl
pdPrivateBaseUrl PackumentDeps
deps
sourceLocation PackumentDeps
deps Provenance
GatedSource = RegistryUrl -> Maybe RegistryUrl
forall a. a -> Maybe a
Just (PackumentDeps -> RegistryUrl
pdPublicBaseUrl PackumentDeps
deps)