2019-07-31 19:39:01 +00:00
|
|
|
{- git-lfs API
|
|
|
|
-
|
|
|
|
- https://github.com/git-lfs/git-lfs/blob/master/docs/api
|
|
|
|
-
|
2019-08-01 17:29:43 +00:00
|
|
|
- Copyright 2019 Joey Hess <id@joeyh.name>
|
2019-07-31 19:39:01 +00:00
|
|
|
-
|
|
|
|
- Licensed under the GNU AGPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
2019-09-24 22:14:07 +00:00
|
|
|
-- | This implementation of the git-lfs API uses http Request and Response,
|
|
|
|
-- but leaves actually connecting up the http client to the user.
|
|
|
|
--
|
|
|
|
-- You'll want to use a Manager that supports https, since the protocol
|
|
|
|
-- uses http basic auth.
|
|
|
|
--
|
|
|
|
-- Some LFS servers, notably Github's, may require a User-Agent header
|
|
|
|
-- in some of the requests, in order to allow eg, uploads. No such header
|
|
|
|
-- is added by default, so be sure to add your own.
|
|
|
|
|
2019-08-01 17:29:43 +00:00
|
|
|
{-# LANGUAGE DeriveGeneric, FlexibleInstances, FlexibleContexts #-}
|
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
|
|
|
{-# LANGUAGE LambdaCase #-}
|
|
|
|
|
2020-06-22 15:21:25 +00:00
|
|
|
-- This is a vendored copy of Network.GitLFS from the git-lfs package,
|
|
|
|
-- and will be removed once that package is available in all build
|
|
|
|
-- environments.
|
2019-08-01 17:29:43 +00:00
|
|
|
module Utility.GitLFS (
|
2019-09-24 22:10:34 +00:00
|
|
|
-- * Transfer requests
|
2019-08-01 17:29:43 +00:00
|
|
|
TransferRequest(..),
|
2019-08-02 16:38:14 +00:00
|
|
|
TransferRequestOperation(..),
|
2019-08-01 17:29:43 +00:00
|
|
|
TransferAdapter(..),
|
|
|
|
TransferRequestObject(..),
|
|
|
|
startTransferRequest,
|
2019-09-24 22:10:34 +00:00
|
|
|
|
|
|
|
-- * Responses to transfer requests
|
2019-08-01 17:29:43 +00:00
|
|
|
TransferResponse(..),
|
|
|
|
TransferResponseOperation(..),
|
|
|
|
IsTransferResponseOperation,
|
2019-08-05 19:40:36 +00:00
|
|
|
DownloadOperation(..),
|
|
|
|
UploadOperation(..),
|
|
|
|
OperationParams(..),
|
2019-08-03 16:21:28 +00:00
|
|
|
ParsedTransferResponse(..),
|
2019-08-01 17:29:43 +00:00
|
|
|
parseTransferResponse,
|
2019-09-24 22:10:34 +00:00
|
|
|
|
|
|
|
-- * Making transfers
|
2019-08-01 17:29:43 +00:00
|
|
|
downloadOperationRequest,
|
|
|
|
uploadOperationRequests,
|
2021-11-10 17:51:11 +00:00
|
|
|
ServerSupportsChunks(..),
|
2019-09-24 22:10:34 +00:00
|
|
|
|
|
|
|
-- * Endpoint discovery
|
2019-09-24 21:59:49 +00:00
|
|
|
Endpoint,
|
2019-08-01 17:29:43 +00:00
|
|
|
guessEndpoint,
|
2019-09-24 21:59:49 +00:00
|
|
|
modifyEndpointRequest,
|
2019-08-02 14:57:40 +00:00
|
|
|
sshDiscoverEndpointCommand,
|
|
|
|
parseSshDiscoverEndpointResponse,
|
2019-09-24 22:10:34 +00:00
|
|
|
|
|
|
|
-- * Errors
|
2019-08-01 17:29:43 +00:00
|
|
|
TransferResponseError(..),
|
|
|
|
TransferResponseObjectError(..),
|
2019-09-24 22:10:34 +00:00
|
|
|
|
|
|
|
-- * Additional data types
|
2019-08-01 17:29:43 +00:00
|
|
|
Url,
|
|
|
|
SHA256,
|
|
|
|
GitRef(..),
|
|
|
|
NumSeconds,
|
|
|
|
HTTPHeader,
|
|
|
|
HTTPHeaderValue,
|
|
|
|
) where
|
|
|
|
|
2019-07-29 19:47:17 +00:00
|
|
|
import Data.Aeson
|
|
|
|
import Data.Aeson.Types
|
|
|
|
import GHC.Generics
|
2019-07-31 19:39:01 +00:00
|
|
|
import Network.HTTP.Client
|
2019-07-31 20:25:13 +00:00
|
|
|
import Data.List
|
2019-07-29 19:47:17 +00:00
|
|
|
import qualified Data.Map as M
|
|
|
|
import qualified Data.Text as T
|
2019-07-31 16:27:27 +00:00
|
|
|
import qualified Data.Text.Encoding as E
|
2019-07-31 16:06:56 +00:00
|
|
|
import qualified Data.ByteString.Lazy as L
|
2019-07-31 16:27:27 +00:00
|
|
|
import qualified Data.CaseInsensitive as CI
|
2019-07-31 20:25:13 +00:00
|
|
|
import qualified Network.URI as URI
|
2019-07-31 16:06:56 +00:00
|
|
|
|
2019-07-29 19:47:17 +00:00
|
|
|
data TransferRequest = TransferRequest
|
|
|
|
{ req_operation :: TransferRequestOperation
|
|
|
|
, req_transfers :: [TransferAdapter]
|
|
|
|
, req_ref :: Maybe GitRef
|
|
|
|
, req_objects :: [TransferRequestObject]
|
|
|
|
}
|
|
|
|
deriving (Generic, Show)
|
|
|
|
|
|
|
|
instance ToJSON TransferRequest where
|
|
|
|
toJSON = genericToJSON transferRequestOptions
|
|
|
|
toEncoding = genericToEncoding transferRequestOptions
|
|
|
|
|
|
|
|
instance FromJSON TransferRequest where
|
|
|
|
parseJSON = genericParseJSON transferRequestOptions
|
|
|
|
|
|
|
|
transferRequestOptions :: Options
|
2019-07-31 18:55:15 +00:00
|
|
|
transferRequestOptions = stripFieldPrefix nonNullOptions
|
2019-07-29 19:47:17 +00:00
|
|
|
|
|
|
|
data TransferRequestObject = TransferRequestObject
|
2019-07-31 18:55:15 +00:00
|
|
|
{ req_oid :: SHA256
|
|
|
|
, req_size :: Integer
|
2019-07-29 19:47:17 +00:00
|
|
|
}
|
|
|
|
deriving (Generic, Show)
|
|
|
|
|
2019-07-31 18:55:15 +00:00
|
|
|
instance ToJSON TransferRequestObject where
|
|
|
|
toJSON = genericToJSON transferRequestObjectOptions
|
|
|
|
toEncoding = genericToEncoding transferRequestObjectOptions
|
|
|
|
|
|
|
|
instance FromJSON TransferRequestObject where
|
|
|
|
parseJSON = genericParseJSON transferRequestObjectOptions
|
|
|
|
|
|
|
|
transferRequestObjectOptions :: Options
|
|
|
|
transferRequestObjectOptions = stripFieldPrefix defaultOptions
|
2019-07-29 19:47:17 +00:00
|
|
|
|
|
|
|
data TransferRequestOperation = RequestDownload | RequestUpload
|
|
|
|
deriving (Show)
|
|
|
|
|
|
|
|
instance ToJSON TransferRequestOperation where
|
|
|
|
toJSON RequestDownload = "download"
|
|
|
|
toJSON RequestUpload = "upload"
|
|
|
|
|
|
|
|
instance FromJSON TransferRequestOperation where
|
|
|
|
parseJSON (String "download") = pure RequestDownload
|
|
|
|
parseJSON (String "upload") = pure RequestUpload
|
|
|
|
parseJSON invalid = typeMismatch "TransferRequestOperation" invalid
|
|
|
|
|
|
|
|
data TransferResponse op = TransferResponse
|
2019-07-31 16:06:56 +00:00
|
|
|
{ transfer :: Maybe TransferAdapter
|
2019-07-29 19:47:17 +00:00
|
|
|
, objects :: [TransferResponseOperation op]
|
|
|
|
}
|
|
|
|
deriving (Generic, Show)
|
|
|
|
|
2019-07-31 16:06:56 +00:00
|
|
|
instance IsTransferResponseOperation op => ToJSON (TransferResponse op) where
|
|
|
|
toJSON = genericToJSON nonNullOptions
|
|
|
|
toEncoding = genericToEncoding nonNullOptions
|
|
|
|
|
|
|
|
instance IsTransferResponseOperation op => FromJSON (TransferResponse op)
|
2019-07-29 19:47:17 +00:00
|
|
|
|
2019-07-31 18:55:15 +00:00
|
|
|
-- | This is an error with a TransferRequest as a whole. It's also possible
|
|
|
|
-- for a TransferRequest to overall succeed, but fail for some
|
|
|
|
-- objects; such failures use TransferResponseObjectError.
|
2019-07-29 19:47:17 +00:00
|
|
|
data TransferResponseError = TransferResponseError
|
2019-07-31 18:55:15 +00:00
|
|
|
{ resperr_message :: T.Text
|
|
|
|
, resperr_request_id :: Maybe T.Text
|
|
|
|
, resperr_documentation_url :: Maybe Url
|
2019-07-29 19:47:17 +00:00
|
|
|
}
|
|
|
|
deriving (Generic, Show)
|
|
|
|
|
|
|
|
instance ToJSON TransferResponseError where
|
2019-07-31 18:55:15 +00:00
|
|
|
toJSON = genericToJSON transferResponseErrorOptions
|
|
|
|
toEncoding = genericToEncoding transferResponseErrorOptions
|
2019-07-29 19:47:17 +00:00
|
|
|
|
2019-07-31 18:55:15 +00:00
|
|
|
instance FromJSON TransferResponseError where
|
|
|
|
parseJSON = genericParseJSON transferResponseErrorOptions
|
|
|
|
|
|
|
|
transferResponseErrorOptions :: Options
|
|
|
|
transferResponseErrorOptions = stripFieldPrefix nonNullOptions
|
|
|
|
|
|
|
|
-- | An error with a single object within a TransferRequest.
|
|
|
|
data TransferResponseObjectError = TransferResponseObjectError
|
|
|
|
{ respobjerr_code :: Int
|
|
|
|
, respobjerr_message :: T.Text
|
|
|
|
}
|
|
|
|
deriving (Generic, Show)
|
|
|
|
|
|
|
|
instance ToJSON TransferResponseObjectError where
|
|
|
|
toJSON = genericToJSON transferResponseObjectErrorOptions
|
|
|
|
toEncoding = genericToEncoding transferResponseObjectErrorOptions
|
|
|
|
|
|
|
|
instance FromJSON TransferResponseObjectError where
|
|
|
|
parseJSON = genericParseJSON transferResponseObjectErrorOptions
|
|
|
|
|
|
|
|
transferResponseObjectErrorOptions :: Options
|
|
|
|
transferResponseObjectErrorOptions = stripFieldPrefix nonNullOptions
|
2019-07-29 19:47:17 +00:00
|
|
|
|
|
|
|
data TransferAdapter = Basic
|
|
|
|
deriving (Show)
|
|
|
|
|
|
|
|
instance ToJSON TransferAdapter where
|
|
|
|
toJSON Basic = "basic"
|
|
|
|
|
|
|
|
instance FromJSON TransferAdapter where
|
|
|
|
parseJSON (String "basic") = pure Basic
|
|
|
|
parseJSON invalid = typeMismatch "basic" invalid
|
|
|
|
|
|
|
|
data TransferResponseOperation op = TransferResponseOperation
|
|
|
|
{ resp_oid :: SHA256
|
|
|
|
, resp_size :: Integer
|
2019-07-31 16:06:56 +00:00
|
|
|
, resp_authenticated :: Maybe Bool
|
2019-07-31 18:55:15 +00:00
|
|
|
, resp_actions :: Maybe op
|
|
|
|
, resp_error :: Maybe TransferResponseObjectError
|
2019-07-29 19:47:17 +00:00
|
|
|
}
|
|
|
|
deriving (Generic, Show)
|
|
|
|
|
|
|
|
instance ToJSON op => ToJSON (TransferResponseOperation op) where
|
|
|
|
toJSON = genericToJSON transferResponseOperationOptions
|
|
|
|
toEncoding = genericToEncoding transferResponseOperationOptions
|
|
|
|
|
|
|
|
instance FromJSON op => FromJSON (TransferResponseOperation op) where
|
|
|
|
parseJSON = genericParseJSON transferResponseOperationOptions
|
|
|
|
|
|
|
|
transferResponseOperationOptions :: Options
|
2019-07-31 18:55:15 +00:00
|
|
|
transferResponseOperationOptions = stripFieldPrefix nonNullOptions
|
2019-07-29 19:47:17 +00:00
|
|
|
|
2019-07-31 16:06:56 +00:00
|
|
|
-- | Class of types that can be responses to a transfer request,
|
|
|
|
-- that contain an operation to use to make the transfer.
|
|
|
|
class (FromJSON op, ToJSON op) => IsTransferResponseOperation op
|
|
|
|
|
2019-07-29 19:47:17 +00:00
|
|
|
data DownloadOperation = DownloadOperation
|
|
|
|
{ download :: OperationParams }
|
|
|
|
deriving (Generic, Show)
|
|
|
|
|
2019-07-31 16:06:56 +00:00
|
|
|
instance IsTransferResponseOperation DownloadOperation
|
2019-07-29 19:47:17 +00:00
|
|
|
instance ToJSON DownloadOperation
|
|
|
|
instance FromJSON DownloadOperation
|
|
|
|
|
|
|
|
data UploadOperation = UploadOperation
|
2019-07-31 18:55:15 +00:00
|
|
|
{ upload :: OperationParams
|
|
|
|
, verify :: Maybe OperationParams
|
|
|
|
}
|
2019-07-29 19:47:17 +00:00
|
|
|
deriving (Generic, Show)
|
|
|
|
|
2019-07-31 16:06:56 +00:00
|
|
|
instance IsTransferResponseOperation UploadOperation
|
2019-07-31 18:55:15 +00:00
|
|
|
|
|
|
|
instance ToJSON UploadOperation where
|
|
|
|
toJSON = genericToJSON nonNullOptions
|
|
|
|
toEncoding = genericToEncoding nonNullOptions
|
|
|
|
|
2019-07-29 19:47:17 +00:00
|
|
|
instance FromJSON UploadOperation
|
|
|
|
|
|
|
|
data OperationParams = OperationParams
|
|
|
|
{ href :: Url
|
2019-07-31 16:06:56 +00:00
|
|
|
, header :: Maybe (M.Map HTTPHeader HTTPHeaderValue)
|
2019-07-29 19:47:17 +00:00
|
|
|
, expires_in :: Maybe NumSeconds
|
|
|
|
, expires_at :: Maybe T.Text
|
|
|
|
}
|
|
|
|
deriving (Generic, Show)
|
|
|
|
|
|
|
|
instance ToJSON OperationParams where
|
|
|
|
toJSON = genericToJSON nonNullOptions
|
|
|
|
toEncoding = genericToEncoding nonNullOptions
|
|
|
|
|
|
|
|
instance FromJSON OperationParams
|
|
|
|
|
2019-07-31 19:04:37 +00:00
|
|
|
data Verification = Verification
|
|
|
|
{ verification_oid :: SHA256
|
|
|
|
, verification_size :: Integer
|
|
|
|
}
|
|
|
|
deriving (Generic, Show)
|
|
|
|
|
|
|
|
instance ToJSON Verification where
|
2019-08-01 17:29:43 +00:00
|
|
|
toJSON = genericToJSON verificationOptions
|
|
|
|
toEncoding = genericToEncoding verificationOptions
|
2019-07-31 19:04:37 +00:00
|
|
|
|
|
|
|
instance FromJSON Verification where
|
2019-08-01 17:29:43 +00:00
|
|
|
parseJSON = genericParseJSON verificationOptions
|
2019-07-31 19:04:37 +00:00
|
|
|
|
2019-08-01 17:29:43 +00:00
|
|
|
verificationOptions :: Options
|
|
|
|
verificationOptions = stripFieldPrefix defaultOptions
|
2019-07-31 19:04:37 +00:00
|
|
|
|
2019-07-31 19:39:01 +00:00
|
|
|
-- | Sent over ssh connection when using that to find the endpoint.
|
|
|
|
data SshDiscoveryResponse = SshDiscoveryResponse
|
|
|
|
{ endpoint_href :: Url
|
|
|
|
, endpoint_header :: Maybe (M.Map HTTPHeader HTTPHeaderValue)
|
|
|
|
, endpoint_expires_in :: Maybe NumSeconds
|
|
|
|
, endpoint_expires_at :: Maybe T.Text
|
|
|
|
} deriving (Generic, Show)
|
|
|
|
|
|
|
|
instance ToJSON SshDiscoveryResponse where
|
|
|
|
toJSON = genericToJSON sshDiscoveryResponseOptions
|
|
|
|
toEncoding = genericToEncoding sshDiscoveryResponseOptions
|
|
|
|
|
|
|
|
instance FromJSON SshDiscoveryResponse where
|
|
|
|
parseJSON = genericParseJSON sshDiscoveryResponseOptions
|
|
|
|
|
|
|
|
sshDiscoveryResponseOptions :: Options
|
|
|
|
sshDiscoveryResponseOptions = stripFieldPrefix nonNullOptions
|
|
|
|
|
2019-07-29 19:47:17 +00:00
|
|
|
data GitRef = GitRef
|
|
|
|
{ name :: T.Text }
|
|
|
|
deriving (Generic, Show)
|
|
|
|
|
|
|
|
instance FromJSON GitRef
|
|
|
|
instance ToJSON GitRef
|
|
|
|
|
|
|
|
type SHA256 = T.Text
|
|
|
|
|
2019-07-31 20:06:59 +00:00
|
|
|
-- | The endpoint of a git-lfs server.
|
2019-09-24 21:59:49 +00:00
|
|
|
data Endpoint = Endpoint Request
|
2019-09-24 18:46:20 +00:00
|
|
|
deriving (Show)
|
|
|
|
|
2019-08-02 14:57:40 +00:00
|
|
|
-- | Command to run via ssh with to discover an endpoint. The FilePath is
|
|
|
|
-- the location of the git repository on the ssh server.
|
2019-07-31 19:39:01 +00:00
|
|
|
--
|
2019-08-02 14:57:40 +00:00
|
|
|
-- Note that, when sshing to the server, you should take care that the
|
|
|
|
-- hostname you pass to ssh is really a hostname and not something that ssh
|
|
|
|
-- will parse an an option, such as -oProxyCommand=".
|
|
|
|
sshDiscoverEndpointCommand :: FilePath -> TransferRequestOperation -> [String]
|
|
|
|
sshDiscoverEndpointCommand remotepath tro =
|
|
|
|
[ "git-lfs-authenticate"
|
|
|
|
, remotepath
|
|
|
|
, case tro of
|
|
|
|
RequestDownload -> "download"
|
|
|
|
RequestUpload -> "upload"
|
|
|
|
]
|
|
|
|
|
2019-09-24 21:59:49 +00:00
|
|
|
-- Internal smart constructor for an Endpoint.
|
|
|
|
--
|
|
|
|
-- Since this uses the LFS batch API, it adds /objects/batch
|
|
|
|
-- to the endpoint url. It also adds the necessary headers to use JSON.
|
|
|
|
mkEndpoint :: URI.URI -> Maybe Endpoint
|
|
|
|
mkEndpoint uri = do
|
|
|
|
r <- requestFromURI uri
|
|
|
|
let r' = addLfsJsonHeaders $ r { path = path r <> "/objects/batch" }
|
|
|
|
return (Endpoint r')
|
|
|
|
|
2019-08-02 14:57:40 +00:00
|
|
|
-- | Parse the json output when doing ssh endpoint discovery.
|
|
|
|
parseSshDiscoverEndpointResponse :: L.ByteString -> Maybe Endpoint
|
2019-09-24 21:59:49 +00:00
|
|
|
parseSshDiscoverEndpointResponse resp = do
|
|
|
|
sr <- decode resp
|
|
|
|
uri <- URI.parseURI (T.unpack (endpoint_href sr))
|
|
|
|
endpoint <- mkEndpoint uri
|
|
|
|
return $ modifyEndpointRequest endpoint $ case endpoint_header sr of
|
|
|
|
Nothing -> id
|
|
|
|
Just headers ->
|
|
|
|
let headers' = map convheader (M.toList headers)
|
|
|
|
in \req -> req
|
|
|
|
{ requestHeaders = requestHeaders req ++ headers' }
|
|
|
|
where
|
|
|
|
convheader (k, v) = (CI.mk (E.encodeUtf8 k), E.encodeUtf8 v)
|
2019-07-31 19:39:01 +00:00
|
|
|
|
2019-07-31 20:25:13 +00:00
|
|
|
-- | Guesses the LFS endpoint from the http url of a git remote.
|
|
|
|
--
|
|
|
|
-- https://github.com/git-lfs/git-lfs/blob/master/docs/api/server-discovery.md
|
2019-09-24 21:59:49 +00:00
|
|
|
guessEndpoint :: URI.URI -> Maybe Endpoint
|
2019-08-02 16:38:14 +00:00
|
|
|
guessEndpoint uri = case URI.uriScheme uri of
|
2019-09-24 21:59:49 +00:00
|
|
|
"https:" -> endpoint
|
|
|
|
"http:" -> endpoint
|
2019-08-02 16:38:14 +00:00
|
|
|
_ -> Nothing
|
|
|
|
where
|
2019-09-24 21:59:49 +00:00
|
|
|
endpoint = mkEndpoint $ uri
|
2019-08-03 16:21:28 +00:00
|
|
|
-- force https because the git-lfs protocol uses http
|
|
|
|
-- basic auth tokens, which should not be exposed
|
|
|
|
{ URI.uriScheme = "https:"
|
2019-08-02 16:38:14 +00:00
|
|
|
, URI.uriPath = guessedpath
|
|
|
|
}
|
|
|
|
|
|
|
|
guessedpath
|
2019-07-31 20:25:13 +00:00
|
|
|
| ".git" `isSuffixOf` URI.uriPath uri =
|
|
|
|
URI.uriPath uri ++ "/info/lfs"
|
|
|
|
| ".git/" `isSuffixOf` URI.uriPath uri =
|
|
|
|
URI.uriPath uri ++ "info/lfs"
|
|
|
|
| otherwise = (droptrailing '/' (URI.uriPath uri)) ++ ".git/info/lfs"
|
2019-08-02 16:38:14 +00:00
|
|
|
|
2019-07-31 20:25:13 +00:00
|
|
|
droptrailing c = reverse . dropWhile (== c) . reverse
|
|
|
|
|
2019-09-24 21:59:49 +00:00
|
|
|
-- | When an Endpoint is used to generate a Request, this allows adjusting
|
|
|
|
-- that Request.
|
|
|
|
--
|
|
|
|
-- This can be used to add http basic authentication to an Endpoint:
|
|
|
|
--
|
|
|
|
-- > modifyEndpointRequest (guessEndpoint u) (applyBasicAuth "user" "pass")
|
|
|
|
modifyEndpointRequest :: Endpoint -> (Request -> Request) -> Endpoint
|
|
|
|
modifyEndpointRequest (Endpoint r) f = Endpoint (f r)
|
|
|
|
|
2019-07-31 20:06:59 +00:00
|
|
|
-- | Makes a Request that will start the process of making a transfer to or
|
|
|
|
-- from the LFS endpoint.
|
2019-09-24 21:59:49 +00:00
|
|
|
startTransferRequest :: Endpoint -> TransferRequest -> Request
|
|
|
|
startTransferRequest (Endpoint r) tr = r
|
|
|
|
{ method = "POST"
|
|
|
|
, requestBody = RequestBodyLBS (encode tr)
|
|
|
|
}
|
2019-07-31 20:06:59 +00:00
|
|
|
|
2019-07-31 18:55:15 +00:00
|
|
|
addLfsJsonHeaders :: Request -> Request
|
|
|
|
addLfsJsonHeaders r = r
|
2019-08-03 15:15:08 +00:00
|
|
|
{ requestHeaders = requestHeaders r ++
|
2019-07-31 17:22:33 +00:00
|
|
|
[ ("Accept", lfsjson)
|
|
|
|
, ("Content-Type", lfsjson)
|
|
|
|
]
|
|
|
|
}
|
|
|
|
where
|
|
|
|
lfsjson = "application/vnd.git-lfs+json"
|
|
|
|
|
2019-08-03 16:21:28 +00:00
|
|
|
data ParsedTransferResponse op
|
|
|
|
= ParsedTransferResponse (TransferResponse op)
|
|
|
|
| ParsedTransferResponseError TransferResponseError
|
|
|
|
| ParseFailed String
|
2019-07-31 17:22:33 +00:00
|
|
|
|
|
|
|
-- | Parse the body of a response to a transfer request.
|
2019-08-01 17:29:43 +00:00
|
|
|
parseTransferResponse
|
2019-07-31 17:22:33 +00:00
|
|
|
:: IsTransferResponseOperation op
|
|
|
|
=> L.ByteString
|
|
|
|
-> ParsedTransferResponse op
|
2019-08-01 17:29:43 +00:00
|
|
|
parseTransferResponse resp = case eitherDecode resp of
|
2019-08-03 16:21:28 +00:00
|
|
|
Right tr -> ParsedTransferResponse tr
|
2019-07-31 17:22:33 +00:00
|
|
|
-- If unable to decode as a TransferResponse, try to decode
|
|
|
|
-- as a TransferResponseError instead, in case the LFS server
|
|
|
|
-- sent an error message.
|
2019-08-03 16:21:28 +00:00
|
|
|
Left err ->
|
|
|
|
either (const $ ParseFailed err) ParsedTransferResponseError $
|
|
|
|
eitherDecode resp
|
2019-07-31 17:22:33 +00:00
|
|
|
|
2019-07-31 18:55:15 +00:00
|
|
|
-- | Builds a http request to perform a download.
|
|
|
|
downloadOperationRequest :: DownloadOperation -> Maybe Request
|
2021-11-10 17:51:11 +00:00
|
|
|
downloadOperationRequest = fmap fst . operationParamsRequest . download
|
2019-07-31 17:22:33 +00:00
|
|
|
|
2019-07-31 18:55:15 +00:00
|
|
|
-- | Builds http request to perform an upload. The content to upload is
|
2021-11-10 17:51:11 +00:00
|
|
|
-- provided, along with its SHA256 and size.
|
2019-07-31 17:22:33 +00:00
|
|
|
--
|
2019-08-02 17:56:55 +00:00
|
|
|
-- When the LFS server requested verification, there will be a second
|
2019-07-31 18:55:15 +00:00
|
|
|
-- Request that does that; it should be run only after the upload has
|
|
|
|
-- succeeded.
|
2019-08-02 17:56:55 +00:00
|
|
|
--
|
2019-08-03 15:31:02 +00:00
|
|
|
-- When the LFS server already contains the object, an empty list may be
|
2019-08-02 17:56:55 +00:00
|
|
|
-- returned.
|
2021-11-10 17:51:11 +00:00
|
|
|
uploadOperationRequests :: UploadOperation -> (ServerSupportsChunks -> RequestBody) -> SHA256 -> Integer -> Maybe [Request]
|
|
|
|
uploadOperationRequests op mkcontent oid size =
|
2019-07-31 18:55:15 +00:00
|
|
|
case (mkdlreq, mkverifyreq) of
|
|
|
|
(Nothing, _) -> Nothing
|
|
|
|
(Just dlreq, Nothing) -> Just [dlreq]
|
|
|
|
(Just dlreq, Just verifyreq) -> Just [dlreq, verifyreq]
|
|
|
|
where
|
|
|
|
mkdlreq = mkdlreq'
|
|
|
|
<$> operationParamsRequest (upload op)
|
2021-11-10 17:51:11 +00:00
|
|
|
mkdlreq' (r, ssc) = r
|
2019-07-31 18:55:15 +00:00
|
|
|
{ method = "PUT"
|
2021-11-10 17:51:11 +00:00
|
|
|
, requestBody = mkcontent ssc
|
2019-07-31 18:55:15 +00:00
|
|
|
}
|
|
|
|
mkverifyreq = mkverifyreq'
|
|
|
|
<$> (operationParamsRequest =<< verify op)
|
2021-11-10 17:51:11 +00:00
|
|
|
mkverifyreq' (r, _ssc) = addLfsJsonHeaders $ r
|
2019-07-31 18:55:15 +00:00
|
|
|
{ method = "POST"
|
|
|
|
, requestBody = RequestBodyLBS $ encode $
|
2019-07-31 19:04:37 +00:00
|
|
|
Verification oid size
|
2019-07-31 18:55:15 +00:00
|
|
|
}
|
|
|
|
|
2021-11-10 17:51:11 +00:00
|
|
|
-- | When the LFS server indicates that it supports Transfer-Encoding chunked,
|
|
|
|
-- this will contain a true value, and the RequestBody provided to
|
|
|
|
-- uploadOperationRequests may be created using RequestBodyStreamChunked.
|
|
|
|
-- Otherwise, that should be avoided as the server may not support the
|
|
|
|
-- chunked encoding.
|
|
|
|
newtype ServerSupportsChunks = ServerSupportsChunks Bool
|
|
|
|
|
|
|
|
operationParamsRequest :: OperationParams -> Maybe (Request, ServerSupportsChunks)
|
2019-07-31 18:55:15 +00:00
|
|
|
operationParamsRequest ps = do
|
|
|
|
r <- parseRequest (T.unpack (href ps))
|
|
|
|
let headers = map convheader $ maybe [] M.toList (header ps)
|
2021-11-10 17:51:11 +00:00
|
|
|
let headers' = filter allowedheader headers
|
|
|
|
let ssc = ServerSupportsChunks $
|
|
|
|
any (== ("Transfer-Encoding", "chunked")) headers
|
|
|
|
return (r { requestHeaders = headers' }, ssc)
|
2019-07-31 17:22:33 +00:00
|
|
|
where
|
|
|
|
convheader (k, v) = (CI.mk (E.encodeUtf8 k), E.encodeUtf8 v)
|
2021-11-10 17:51:11 +00:00
|
|
|
-- requestHeaders is not allowed to set Transfer-Encoding or
|
|
|
|
-- Content-Length; copying those over blindly could request in a
|
|
|
|
-- malformed request.
|
|
|
|
allowedheader (k, _) = k /= "Transfer-Encoding"
|
|
|
|
&& k /= "Content-Length"
|
2019-07-31 17:22:33 +00:00
|
|
|
|
2019-07-29 19:47:17 +00:00
|
|
|
type Url = T.Text
|
|
|
|
|
|
|
|
type NumSeconds = Integer
|
|
|
|
|
|
|
|
type HTTPHeader = T.Text
|
|
|
|
|
|
|
|
type HTTPHeaderValue = T.Text
|
|
|
|
|
|
|
|
-- Prevent Nothing from serializing to null.
|
|
|
|
nonNullOptions :: Options
|
|
|
|
nonNullOptions = defaultOptions { omitNothingFields = True }
|
2019-07-31 18:55:15 +00:00
|
|
|
|
|
|
|
-- Remove prefix from field names.
|
|
|
|
stripFieldPrefix :: Options -> Options
|
|
|
|
stripFieldPrefix o =
|
|
|
|
o { fieldLabelModifier = drop 1 . dropWhile (/= '_') }
|