Skip to content

Commit

Permalink
Add an unversioned package doc redirect
Browse files Browse the repository at this point in the history
That is /package/foo/docs/blah.html -> /package/foo-1.0/docs/blah.html
Using a temp redirect to whatever is the latest version.
Fixes issue #61
  • Loading branch information
dcoutts committed Sep 19, 2013
1 parent d814748 commit 5f311f1
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 33 deletions.
12 changes: 6 additions & 6 deletions Distribution/Server/Features/Core.hs
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,7 @@ data CoreFeature = CoreFeature {
packageChangeHook :: Hook PackageChange (),

-- | Notification of downloads
packageDownloadHook :: Hook PackageId (),

-- Find a package in the package DB
lookupPackageName :: PackageName -> ServerPartE [PkgInfo],
lookupPackageId :: PackageId -> ServerPartE PkgInfo
packageDownloadHook :: Hook PackageId ()
}

instance IsHackageFeature CoreFeature where
Expand Down Expand Up @@ -137,7 +133,11 @@ data CoreResource = CoreResource {

-- Check that a package exists (guard fails if version is empty)
guardValidPackageId :: PackageId -> ServerPartE (),
guardValidPackageName :: PackageName -> ServerPartE ()
guardValidPackageName :: PackageName -> ServerPartE (),

-- Find a package in the package DB
lookupPackageName :: PackageName -> ServerPartE [PkgInfo],
lookupPackageId :: PackageId -> ServerPartE PkgInfo
}

initCoreFeature :: ServerEnv -> UserFeature -> IO CoreFeature
Expand Down
40 changes: 29 additions & 11 deletions Distribution/Server/Features/Documentation.hs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import Data.TarIndex (TarIndex)

import Distribution.Text
import Distribution.Package
import Distribution.Version (Version(..))

import qualified Data.Map as Map
import Data.Function (fix)
Expand Down Expand Up @@ -117,6 +118,7 @@ documentationFeature name
, guardValidPackageId
, corePackagePage
, corePackagesPage
, lookupPackageId
}
getPackages
UploadFeature{..}
Expand Down Expand Up @@ -170,15 +172,17 @@ documentationFeature name

serveDocumentationTar :: DynamicPath -> ServerPartE Response
serveDocumentationTar dpath =
withDocumentation dpath $ \_ blob _ -> do
withDocumentation (packageDocsWhole documentationResource)
dpath $ \_ blob _ -> do
file <- liftIO $ BlobStorage.fetch store blob
return $ toResponse $ Resource.DocTarball file blob


-- return: not-found error or tarball
serveDocumentation :: DynamicPath -> ServerPartE Response
serveDocumentation dpath = do
withDocumentation dpath $ \pkgid blob index -> do
withDocumentation (packageDocsContent documentationResource)
dpath $ \pkgid blob index -> do
let tarball = BlobStorage.filepath store blob
etag = blobETag blob
-- if given a directory, the default page is index.html
Expand Down Expand Up @@ -228,16 +232,30 @@ documentationFeature name
..
-}

withDocumentation :: DynamicPath -> (PackageId -> BlobId -> TarIndex -> ServerPartE a) -> ServerPartE a
withDocumentation dpath func = do
withDocumentation :: Resource -> DynamicPath
-> (PackageId -> BlobId -> TarIndex -> ServerPartE Response)
-> ServerPartE Response
withDocumentation self dpath func = do
pkgid <- packageInPath dpath
guardValidPackageId pkgid
mdocs <- queryState documentationState $ LookupDocumentation pkgid
case mdocs of
Nothing -> errNotFound "Not Found" [MText $ "There is no documentation for " ++ display pkgid]
Just blob -> do
index <- liftIO $ cachedTarIndex blob
func pkgid blob index
-- lookupPackageId gives us the latest version if no version is specified:
pkginfo <- lookupPackageId pkgid
case pkgVersion pkgid of
-- if no version is given we want to redirect to the latest version
Version [] _ -> tempRedirect (renderResource' self dpath') (toResponse "")
where
latest = packageId pkginfo
dpath' = [ if var == "package"
then (var, display latest)
else e
| e@(var, _) <- dpath ]
_ -> do
mdocs <- queryState documentationState $ LookupDocumentation pkgid
case mdocs of
Nothing -> errNotFound "Not Found"
[MText $ "There is no documentation for " ++ display pkgid]
Just blob -> do
index <- liftIO $ cachedTarIndex blob
func pkgid blob index

{------------------------------------------------------------------------------
Auxiliary
Expand Down
8 changes: 5 additions & 3 deletions Distribution/Server/Features/EditCabalFiles.hs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ editCabalFilesFeature :: ServerEnv -> Templates
-> UserFeature -> CoreFeature -> UploadFeature
-> HackageFeature
editCabalFilesFeature _env templates
UserFeature{guardAuthorised} CoreFeature{..}
UserFeature{guardAuthorised}
CoreFeature{..}
UploadFeature{maintainersGroup, trusteesGroup} =
(emptyHackageFeature "edit-cabal-files") {
featureResources =
Expand All @@ -69,6 +70,7 @@ editCabalFilesFeature _env templates
}

where
CoreResource{..} = coreResource
editCabalFileResource =
(resourceAt "/package/:package/:cabal.cabal/edit") {
resourceDesc = [(GET, "Page to edit package metadata")
Expand All @@ -80,7 +82,7 @@ editCabalFilesFeature _env templates
serveEditCabalFileGet :: DynamicPath -> ServerPartE Response
serveEditCabalFileGet dpath = do
template <- getTemplate templates "cabalFileEditPage.html"
pkg <- packageInPath coreResource dpath >>= lookupPackageId
pkg <- packageInPath dpath >>= lookupPackageId
let pkgname = packageName pkg
pkgid = packageId pkg
-- check that the cabal name matches the package
Expand All @@ -94,7 +96,7 @@ editCabalFilesFeature _env templates
serveEditCabalFilePost :: DynamicPath -> ServerPartE Response
serveEditCabalFilePost dpath = do
template <- getTemplate templates "cabalFileEditPage.html"
pkg <- packageInPath coreResource dpath >>= lookupPackageId
pkg <- packageInPath dpath >>= lookupPackageId
let pkgname = packageName pkg
pkgid = packageId pkg
-- check that the cabal name matches the package
Expand Down
16 changes: 10 additions & 6 deletions Distribution/Server/Features/Html.hs
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ mkHtmlCore :: HtmlUtilities
-> Templates
-> HtmlCore
mkHtmlCore HtmlUtilities{..}
CoreFeature{lookupPackageName,coreResource}
CoreFeature{coreResource}
VersionsFeature{ versionsResource
, queryGetDeprecatedFor
, queryGetPreferredInfo
Expand All @@ -417,7 +417,7 @@ mkHtmlCore HtmlUtilities{..}
templates
= HtmlCore{..}
where
cores@CoreResource{packageInPath} = coreResource
cores@CoreResource{packageInPath, lookupPackageName} = coreResource
versions = versionsResource
docs = documentationResource

Expand Down Expand Up @@ -929,8 +929,10 @@ mkHtmlPreferred :: HtmlUtilities
-> VersionsFeature
-> HtmlPreferred
mkHtmlPreferred HtmlUtilities{..}
CoreFeature{ coreResource = CoreResource{packageInPath}
, lookupPackageName
CoreFeature{ coreResource = CoreResource{
packageInPath
, lookupPackageName
}
}
VersionsFeature{..} = HtmlPreferred{..}
where
Expand Down Expand Up @@ -1183,8 +1185,10 @@ mkHtmlTags :: HtmlUtilities
-> TagsFeature
-> HtmlTags
mkHtmlTags HtmlUtilities{..}
CoreFeature{ coreResource = CoreResource{packageInPath}
, lookupPackageName
CoreFeature{ coreResource = CoreResource{
packageInPath
, lookupPackageName
}
}
ListFeature{makeItemList}
TagsFeature{..} = HtmlTags{..}
Expand Down
9 changes: 5 additions & 4 deletions Distribution/Server/Features/Mirror.hs
Original file line number Diff line number Diff line change
Expand Up @@ -92,14 +92,15 @@ mirrorFeature :: ServerEnv
-> (MirrorFeature, UserGroup)

mirrorFeature ServerEnv{serverBlobStore = store}
CoreFeature{ coreResource = coreResource@CoreResource{ packageInPath
, packageTarballInPath
}
CoreFeature{ coreResource = coreResource@CoreResource{
packageInPath
, packageTarballInPath
, lookupPackageId
}
, updateAddPackageRevision
, updateAddPackageTarball
, updateSetPackageUploadTime
, updateSetPackageUploader
, lookupPackageId
}
UserFeature{..}
mirrorersState mirrorGroup mirrorGroupResource
Expand Down
6 changes: 4 additions & 2 deletions Distribution/Server/Features/PackageContents.hs
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,10 @@ packageContentsFeature :: ServerEnv
-> PackageContentsFeature

packageContentsFeature ServerEnv{serverBlobStore = store}
CoreFeature{ coreResource = CoreResource{packageInPath}
, lookupPackageId
CoreFeature{ coreResource = CoreResource{
packageInPath
, lookupPackageId
}
}
TarIndexCacheFeature{cachedPackageTarIndex}
= PackageContentsFeature{..}
Expand Down
2 changes: 1 addition & 1 deletion Distribution/Server/Features/PreferredVersions.hs
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,9 @@ versionsFeature :: CoreFeature

versionsFeature CoreFeature{ coreResource=CoreResource{ packageInPath
, guardValidPackageName
, lookupPackageName
}
, queryGetPackageIndex
, lookupPackageName
, updateArchiveIndexEntry
}
UploadFeature{..}
Expand Down

0 comments on commit 5f311f1

Please sign in to comment.