Skip to content

Commit

Permalink
Merge pull request #1348 from MangoIV/mangoiv/fix-documentation-upload
Browse files Browse the repository at this point in the history
fix expected content-types for documentation tarballs
  • Loading branch information
gbaz authored Dec 29, 2024
2 parents 03afc62 + a3f7229 commit 5252a9c
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 19 deletions.
18 changes: 10 additions & 8 deletions src/Distribution/Server/Features/Documentation.hs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import Distribution.Server.Framework.BlobStorage (BlobId)
import qualified Distribution.Server.Framework.BlobStorage as BlobStorage
import qualified Distribution.Server.Util.ServeTarball as ServerTarball
import qualified Distribution.Server.Util.DocMeta as DocMeta
import qualified Distribution.Server.Util.GZip as Gzip
import Distribution.Server.Features.BuildReports.BuildReport (PkgDetails(..), BuildStatus(..))
import Data.TarIndex (TarIndex)
import qualified Codec.Archive.Tar as Tar
Expand All @@ -46,7 +47,6 @@ import Data.Time.Clock (NominalDiffTime, diffUTCTime, getCurrentTime)
import System.Directory (getModificationTime)
import Control.Applicative
import Distribution.Server.Features.PreferredVersions
import Distribution.Server.Features.PreferredVersions.State (getVersionStatus)
import Distribution.Server.Packages.Types
-- TODO:
-- 1. Write an HTML view for organizing uploads
Expand Down Expand Up @@ -327,8 +327,10 @@ documentationFeature name
-- \* Generate the new index
-- \* Drop the index for the old tar-file
-- \* Link the new documentation to the package
fileContents <- expectUncompressedTarball
mres <- liftIO $ BlobStorage.addWith store fileContents
fileContents <- expectCompressedTarball
let filename = display pkgid ++ "-docs" <.> "tar.gz"
unpacked = Gzip.decompressNamed filename fileContents
mres <- liftIO $ BlobStorage.addWith store unpacked
(\content -> return (checkDocTarball pkgid content))
case mres of
Left err -> errBadRequest "Invalid documentation tarball" [MText err]
Expand Down Expand Up @@ -377,15 +379,15 @@ documentationFeature name
helper (pkg:pkgs) = do
hasDoc <- queryHasDocumentation (pkgInfoId pkg)
let status = getVersionStatus prefInfo (packageVersion pkg)
if hasDoc && status == NormalVersion
then pure (Just (packageId pkg))
if hasDoc && status == NormalVersion
then pure (Just (packageId pkg))
else helper pkgs

helper2 [] = pure Nothing
helper2 (pkg:pkgs) = do
hasDoc <- queryHasDocumentation (pkgInfoId pkg)
if hasDoc
then pure (Just (packageId pkg))
then pure (Just (packageId pkg))
else helper2 pkgs

withDocumentation :: Resource -> DynamicPath
Expand All @@ -400,7 +402,7 @@ documentationFeature name
then (var, unPackageName $ pkgName pkgid)
else e
| e@(var, _) <- dpath ]
basePkgPath = (renderResource' self basedpath)
basePkgPath = renderResource' self basedpath
canonicalLink = show serverBaseURI ++ basePkgPath
canonicalHeader = "<" ++ canonicalLink ++ ">; rel=\"canonical\""

Expand Down Expand Up @@ -484,7 +486,7 @@ checkDocTarball pkgid =
------------------------------------------------------------------------------}

mapParaM :: Monad m => (a -> m b) -> [a] -> m [(a, b)]
mapParaM f = mapM (\x -> (,) x `liftM` f x)
mapParaM f = mapM (\x -> (,) x <$> f x)

getFileAge :: FilePath -> IO NominalDiffTime
getFileAge file = diffUTCTime <$> getCurrentTime <*> getModificationTime file
12 changes: 1 addition & 11 deletions src/Distribution/Server/Framework/RequestContentTypes.hs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ module Distribution.Server.Framework.RequestContentTypes (

-- * various specific content types
expectTextPlain,
expectUncompressedTarball,
expectCompressedTarball,
expectAesonContent,
expectCSV,
Expand Down Expand Up @@ -102,15 +101,6 @@ gzipDecompress content = go content decompressor
expectTextPlain :: ServerPartE LBS.ByteString
expectTextPlain = expectContentType "text/plain"

-- | Expect an uncompressed @.tar@ file.
--
-- The tar file is not validated.
--
-- A content-encoding of \"gzip\" is handled transparently.
--
expectUncompressedTarball :: ServerPartE LBS.ByteString
expectUncompressedTarball = expectContentType "application/x-tar"

-- | Expect a compressed @.tar.gz@ file.
--
-- Neither the gzip encoding nor the tar format are validated.
Expand All @@ -128,7 +118,7 @@ expectCompressedTarball = do
Just actual
| actual == "application/x-tar"
, contentEncoding == Just "gzip" -> consumeRequestBody
| actual == "application/x-gzip"
| actual == "application/gzip" || actual == "application/x-gzip"
, contentEncoding == Nothing -> consumeRequestBody
_ -> errExpectedTarball
where
Expand Down

0 comments on commit 5252a9c

Please sign in to comment.