From 66bc49df15ba19d0f26bfef598a12e3ee0dc0d79 Mon Sep 17 00:00:00 2001 From: fr33domlover Date: Tue, 26 Apr 2016 05:58:05 +0000 Subject: [PATCH] Git upload-pack request handler, parses it but returns nothing --- src/Vervis/Content.hs | 28 ++++++++++++++++++---------- src/Vervis/Handler/Git.hs | 35 +++++++++++++++++++++++------------ 2 files changed, 41 insertions(+), 22 deletions(-) diff --git a/src/Vervis/Content.hs b/src/Vervis/Content.hs index 229c6b8..b4cbd83 100644 --- a/src/Vervis/Content.hs +++ b/src/Vervis/Content.hs @@ -16,27 +16,35 @@ -- | Custom HTTP response content types. module Vervis.Content ( GitRefDiscovery (..) + , GitUploadPackResult (..) ) where import Prelude +import Data.ByteString (ByteString) import Data.Monoid ((<>)) +import Network.Git.Put (serializeService) +import Network.Git.Transport.HTTP.Fetch.RefDiscovery import Yesod.Core.Content -import qualified Data.ByteString as B -import qualified Data.ByteString.Lazy as BL - -data GitRefDiscovery = GitRefDiscovery - { grdContent :: BL.ByteString - , grdService :: B.ByteString - } +newtype GitRefDiscovery = GitRefDiscovery { unGRD :: RefDiscover } instance ToContent GitRefDiscovery where - toContent (GitRefDiscovery c _) = toContent c + toContent = toContent . serializeRefDiscover . unGRD instance ToTypedContent GitRefDiscovery where - toTypedContent grd@(GitRefDiscovery _ s) = - let t = "application/x-" <> s <> "-advertisement" + toTypedContent grd = + let serv = rdService $ unGRD grd + t = "application/x-" <> serializeService serv <> "-advertisement" c = toContent grd in TypedContent t c + +newtype GitUploadPackResult = GitUploadPackResult { unGUPR :: () } + +instance ToContent GitUploadPackResult where + toContent = toContent . unGUPR + +instance ToTypedContent GitUploadPackResult where + toTypedContent gupr = + TypedContent "application/x-git-upload-pack-result" (toContent gupr) diff --git a/src/Vervis/Handler/Git.hs b/src/Vervis/Handler/Git.hs index 02e1616..402f6e5 100644 --- a/src/Vervis/Handler/Git.hs +++ b/src/Vervis/Handler/Git.hs @@ -25,10 +25,14 @@ import Control.Monad.IO.Class (liftIO) import Data.Git.Storage (isRepo, withRepo) import Data.String (fromString) import Data.Text (Text) +import Data.Text.Encoding (encodeUtf8) +import Network.Git.Get (parseService) import Network.Git.Transport.HTTP.Fetch.RefDiscovery +import Network.Git.Transport.HTTP.Fetch.UploadRequest import Yesod.Core.Handler -import Vervis.Content (GitRefDiscovery (..)) +import Vervis.BinaryBody (decodeRequestBody) +import Vervis.Content import Vervis.Foundation (Handler) import Vervis.Path (askRepoDir) @@ -41,19 +45,26 @@ getGitRefDiscoverR sharer repo = do then do rq <- getRequest case reqGetParams rq of - [("service", serv)] -> - if serv == "git-upload-pack" - then do + [("service", servT)] -> + case parseService $ encodeUtf8 servT of + Just serv -> do rd <- liftIO $ withRepo pathG $ - flip buildRefDiscover' "git-upload-pack" + flip buildRefDiscover' serv setHeader "Cache-Control" "no-cache" - return GitRefDiscovery - { grdContent = serializeRefDiscover rd - , grdService = "git-upload-pack" - } - else permissionDenied "Service not supported" + return $ GitRefDiscovery rd + Nothing -> permissionDenied "Service not supported" _ -> notFound else notFound -postGitUploadRequestR :: Text -> Text -> Handler Text -postGitUploadRequestR sharer repo = notFound +postGitUploadRequestR :: Text -> Text -> Handler GitUploadPackResult +postGitUploadRequestR sharer repo = do + path <- askRepoDir sharer repo + let pathG = fromString path + seemsThere <- liftIO $ isRepo pathG + if seemsThere + then do + ereq <- decodeRequestBody getUploadRequest + case ereq of + Left _ -> invalidArgs ["UploadRequest"] + Right ur -> return $ GitUploadPackResult () + else notFound