2024-07-23 18:12:03 +00:00
|
|
|
{- P2P protocol over HTTP, client
|
2024-07-23 17:53:10 +00:00
|
|
|
-
|
|
|
|
- https://git-annex.branchable.com/design/p2p_protocol_over_http/
|
|
|
|
-
|
|
|
|
- Copyright 2024 Joey Hess <id@joeyh.name>
|
|
|
|
-
|
|
|
|
- Licensed under the GNU AGPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
2024-07-23 18:12:03 +00:00
|
|
|
{-# LANGUAGE BangPatterns #-}
|
|
|
|
{-# LANGUAGE DataKinds, TypeApplications #-}
|
2024-08-07 15:24:34 +00:00
|
|
|
{-# LANGUAGE ScopedTypeVariables #-}
|
2024-07-23 17:53:10 +00:00
|
|
|
{-# LANGUAGE CPP #-}
|
|
|
|
|
2024-07-24 13:45:14 +00:00
|
|
|
module P2P.Http.Client (
|
|
|
|
module P2P.Http.Client,
|
2024-07-24 16:05:10 +00:00
|
|
|
module P2P.Http.Types,
|
2024-07-24 13:45:14 +00:00
|
|
|
Validity(..),
|
|
|
|
) where
|
2024-07-23 17:53:10 +00:00
|
|
|
|
|
|
|
import Types
|
2024-07-24 19:12:16 +00:00
|
|
|
import P2P.Http.Types
|
|
|
|
import P2P.Protocol hiding (Offset, Bypass, auth, FileSize)
|
|
|
|
import Utility.Metered
|
|
|
|
import Utility.FileSize
|
|
|
|
import Types.NumCopies
|
2024-07-23 17:53:10 +00:00
|
|
|
|
2024-07-30 16:39:17 +00:00
|
|
|
import Annex.Common
|
2024-07-23 17:53:10 +00:00
|
|
|
#ifdef WITH_SERVANT
|
2024-07-23 22:45:02 +00:00
|
|
|
import qualified Annex
|
2024-07-23 17:53:10 +00:00
|
|
|
import Annex.UUID
|
2024-07-24 19:12:16 +00:00
|
|
|
import Annex.Url
|
2024-07-23 17:53:10 +00:00
|
|
|
import Types.Remote
|
2024-07-23 18:12:03 +00:00
|
|
|
import P2P.Http
|
2024-07-23 17:53:10 +00:00
|
|
|
import P2P.Http.Url
|
2024-07-23 18:12:03 +00:00
|
|
|
import Annex.Concurrent
|
2024-07-23 22:11:15 +00:00
|
|
|
import Utility.Url (BasicAuth(..))
|
2024-07-24 17:42:57 +00:00
|
|
|
import Utility.HumanTime
|
2024-10-18 00:55:31 +00:00
|
|
|
import Utility.STM
|
2024-07-23 22:11:15 +00:00
|
|
|
import qualified Git.Credential as Git
|
2024-07-23 18:12:03 +00:00
|
|
|
|
2024-07-23 22:11:15 +00:00
|
|
|
import Servant hiding (BasicAuthData(..))
|
2024-07-23 18:12:03 +00:00
|
|
|
import Servant.Client.Streaming
|
|
|
|
import qualified Servant.Types.SourceT as S
|
2024-07-23 18:58:49 +00:00
|
|
|
import Network.HTTP.Types.Status
|
|
|
|
import Network.HTTP.Client
|
2024-07-23 18:12:03 +00:00
|
|
|
import qualified Data.ByteString as B
|
|
|
|
import qualified Data.ByteString.Lazy.Internal as LI
|
2024-07-23 22:45:02 +00:00
|
|
|
import qualified Data.Map as M
|
2024-07-23 18:12:03 +00:00
|
|
|
import Control.Concurrent.Async
|
|
|
|
import Control.Concurrent
|
|
|
|
import System.IO.Unsafe
|
2024-07-23 17:53:10 +00:00
|
|
|
#endif
|
2024-07-30 16:39:17 +00:00
|
|
|
import Data.Time.Clock.POSIX
|
2024-07-24 19:12:16 +00:00
|
|
|
import qualified Data.ByteString.Lazy as L
|
2024-07-23 17:53:10 +00:00
|
|
|
|
2024-07-23 18:12:03 +00:00
|
|
|
type ClientAction a
|
2024-07-24 12:33:59 +00:00
|
|
|
#ifdef WITH_SERVANT
|
2024-07-23 18:12:03 +00:00
|
|
|
= ClientEnv
|
|
|
|
-> ProtocolVersion
|
|
|
|
-> B64UUID ServerSide
|
|
|
|
-> B64UUID ClientSide
|
|
|
|
-> [B64UUID Bypass]
|
|
|
|
-> Maybe Auth
|
2024-07-23 18:58:49 +00:00
|
|
|
-> Annex (Either ClientError a)
|
2024-07-24 12:33:59 +00:00
|
|
|
#else
|
|
|
|
= ()
|
|
|
|
#endif
|
2024-07-23 18:12:03 +00:00
|
|
|
|
2024-07-23 17:53:10 +00:00
|
|
|
p2pHttpClient
|
|
|
|
:: Remote
|
|
|
|
-> (String -> Annex a)
|
2024-07-23 18:12:03 +00:00
|
|
|
-> ClientAction a
|
2024-07-23 17:53:10 +00:00
|
|
|
-> Annex a
|
2024-07-25 14:11:09 +00:00
|
|
|
p2pHttpClient rmt fallback clientaction =
|
|
|
|
p2pHttpClientVersions (const True) rmt fallback clientaction >>= \case
|
|
|
|
Just res -> return res
|
|
|
|
Nothing -> fallback "git-annex HTTP API server is missing an endpoint"
|
|
|
|
|
|
|
|
p2pHttpClientVersions
|
|
|
|
:: (ProtocolVersion -> Bool)
|
|
|
|
-> Remote
|
|
|
|
-> (String -> Annex a)
|
|
|
|
-> ClientAction a
|
|
|
|
-> Annex (Maybe a)
|
2024-07-23 17:53:10 +00:00
|
|
|
#ifdef WITH_SERVANT
|
2024-07-25 14:11:09 +00:00
|
|
|
p2pHttpClientVersions allowedversion rmt fallback clientaction =
|
2024-07-23 17:53:10 +00:00
|
|
|
case p2pHttpBaseUrl <$> remoteAnnexP2PHttpUrl (gitconfig rmt) of
|
|
|
|
Nothing -> error "internal"
|
|
|
|
Just baseurl -> do
|
|
|
|
mgr <- httpManager <$> getUrlOptions
|
|
|
|
let clientenv = mkClientEnv mgr baseurl
|
2024-07-23 22:45:02 +00:00
|
|
|
ccv <- Annex.getRead Annex.gitcredentialcache
|
|
|
|
Git.CredentialCache cc <- liftIO $ atomically $
|
|
|
|
readTMVar ccv
|
|
|
|
case M.lookup (Git.CredentialBaseURL credentialbaseurl) cc of
|
2024-07-25 14:11:09 +00:00
|
|
|
Nothing -> go clientenv Nothing False Nothing versions
|
|
|
|
Just cred -> go clientenv (Just cred) True (credauth cred) versions
|
2024-07-23 18:58:49 +00:00
|
|
|
where
|
2024-07-25 14:11:09 +00:00
|
|
|
versions = filter allowedversion allProtocolVersions
|
2024-07-23 22:45:02 +00:00
|
|
|
go clientenv mcred credcached mauth (v:vs) = do
|
2024-07-23 18:58:49 +00:00
|
|
|
myuuid <- getUUID
|
2024-08-07 15:24:34 +00:00
|
|
|
res <- catchclienterror $ clientaction clientenv v
|
2024-07-23 18:58:49 +00:00
|
|
|
(B64UUID (uuid rmt))
|
|
|
|
(B64UUID myuuid)
|
|
|
|
[]
|
2024-07-23 22:11:15 +00:00
|
|
|
mauth
|
2024-07-23 18:58:49 +00:00
|
|
|
case res of
|
2024-07-23 22:11:15 +00:00
|
|
|
Right resp -> do
|
2024-07-23 22:45:02 +00:00
|
|
|
unless credcached $ cachecred mcred
|
2024-07-25 14:11:09 +00:00
|
|
|
return (Just resp)
|
2024-07-23 18:58:49 +00:00
|
|
|
Left (FailureResponse _ resp)
|
|
|
|
| statusCode (responseStatusCode resp) == 404 && not (null vs) ->
|
2024-07-23 22:45:02 +00:00
|
|
|
go clientenv mcred credcached mauth vs
|
2024-07-23 22:11:15 +00:00
|
|
|
| statusCode (responseStatusCode resp) == 401 ->
|
|
|
|
case mcred of
|
|
|
|
Nothing -> authrequired clientenv (v:vs)
|
|
|
|
Just cred -> do
|
|
|
|
inRepo $ Git.rejectUrlCredential cred
|
2024-07-25 14:11:09 +00:00
|
|
|
Just <$> fallback (showstatuscode resp)
|
|
|
|
| otherwise -> Just <$> fallback (showstatuscode resp)
|
2024-07-23 18:58:49 +00:00
|
|
|
Left (ConnectionError ex) -> case fromException ex of
|
2024-07-25 14:11:09 +00:00
|
|
|
Just (HttpExceptionRequest _ (ConnectionFailure err)) -> Just <$> fallback
|
|
|
|
("unable to connect to HTTP server: " ++ show err)
|
|
|
|
_ -> Just <$> fallback (show ex)
|
|
|
|
Left clienterror -> Just <$> fallback
|
|
|
|
("git-annex HTTP API server returned an unexpected response: " ++ show clienterror)
|
|
|
|
go _ _ _ _ [] = return Nothing
|
2024-07-23 22:11:15 +00:00
|
|
|
|
2024-08-07 15:24:34 +00:00
|
|
|
catchclienterror a = a `catch` \(ex :: ClientError) -> pure (Left ex)
|
|
|
|
|
2024-07-23 22:45:02 +00:00
|
|
|
authrequired clientenv vs = do
|
|
|
|
cred <- prompt $
|
|
|
|
inRepo $ Git.getUrlCredential credentialbaseurl
|
|
|
|
go clientenv (Just cred) False (credauth cred) vs
|
2024-07-23 22:11:15 +00:00
|
|
|
|
|
|
|
showstatuscode resp =
|
|
|
|
show (statusCode (responseStatusCode resp))
|
|
|
|
++ " " ++
|
|
|
|
decodeBS (statusMessage (responseStatusCode resp))
|
|
|
|
|
2024-07-23 22:45:02 +00:00
|
|
|
credentialbaseurl = case p2pHttpUrlString <$> remoteAnnexP2PHttpUrl (gitconfig rmt) of
|
|
|
|
Nothing -> error "internal"
|
2024-07-29 00:29:42 +00:00
|
|
|
Just url -> url
|
2024-07-23 22:45:02 +00:00
|
|
|
|
|
|
|
credauth cred = do
|
|
|
|
ba <- Git.credentialBasicAuth cred
|
|
|
|
return $ Auth
|
|
|
|
(encodeBS (basicAuthUser ba))
|
|
|
|
(encodeBS (basicAuthPassword ba))
|
|
|
|
|
|
|
|
cachecred mcred = case mcred of
|
|
|
|
Just cred -> do
|
|
|
|
inRepo $ Git.approveUrlCredential cred
|
|
|
|
ccv <- Annex.getRead Annex.gitcredentialcache
|
|
|
|
liftIO $ atomically $ do
|
|
|
|
Git.CredentialCache cc <- takeTMVar ccv
|
|
|
|
putTMVar ccv $ Git.CredentialCache $
|
|
|
|
M.insert (Git.CredentialBaseURL credentialbaseurl) cred cc
|
|
|
|
Nothing -> noop
|
2024-07-23 17:53:10 +00:00
|
|
|
#else
|
2024-07-30 16:39:17 +00:00
|
|
|
p2pHttpClientVersions _ _ fallback () = Just <$> fallback
|
2024-07-30 16:18:39 +00:00
|
|
|
"This remote uses an annex+http url, but this version of git-annex is not built with support for that."
|
2024-07-23 17:53:10 +00:00
|
|
|
#endif
|
2024-07-23 18:12:03 +00:00
|
|
|
|
|
|
|
clientGet
|
2024-07-24 15:10:19 +00:00
|
|
|
:: Key
|
2024-07-24 13:45:14 +00:00
|
|
|
-> AssociatedFile
|
2024-07-24 15:10:19 +00:00
|
|
|
-> (L.ByteString -> IO BytesProcessed)
|
|
|
|
-- ^ Must consume the entire ByteString before returning its
|
|
|
|
-- total size.
|
2024-07-24 15:03:59 +00:00
|
|
|
-> Maybe FileSize
|
2024-07-24 15:10:19 +00:00
|
|
|
-- ^ Size of existing file, when resuming.
|
2024-07-24 13:45:14 +00:00
|
|
|
-> ClientAction Validity
|
2024-07-24 19:12:16 +00:00
|
|
|
#ifdef WITH_SERVANT
|
2024-07-24 15:10:19 +00:00
|
|
|
clientGet k af consumer startsz clientenv (ProtocolVersion ver) su cu bypass auth = liftIO $ do
|
2024-07-24 13:45:14 +00:00
|
|
|
let offset = fmap (Offset . fromIntegral) startsz
|
|
|
|
withClientM (cli (B64Key k) cu bypass baf offset auth) clientenv $ \case
|
|
|
|
Left err -> return (Left err)
|
2024-07-24 15:03:59 +00:00
|
|
|
Right respheaders -> do
|
|
|
|
b <- S.unSourceT (getResponse respheaders) gather
|
2024-07-24 15:10:19 +00:00
|
|
|
BytesProcessed len <- consumer b
|
2024-07-24 15:03:59 +00:00
|
|
|
let DataLength dl = case lookupResponseHeader @DataLengthHeader' respheaders of
|
|
|
|
Header hdr -> hdr
|
|
|
|
_ -> error "missing data length header"
|
|
|
|
return $ Right $
|
|
|
|
if dl == len then Valid else Invalid
|
2024-07-23 18:12:03 +00:00
|
|
|
where
|
|
|
|
cli =case ver of
|
|
|
|
3 -> v3 su V3
|
|
|
|
2 -> v2 su V2
|
|
|
|
1 -> v1 su V1
|
|
|
|
0 -> v0 su V0
|
|
|
|
_ -> error "unsupported protocol version"
|
|
|
|
|
|
|
|
v3 :<|> v2 :<|> v1 :<|> v0 :<|> _ = client p2pHttpAPI
|
|
|
|
|
2024-07-24 12:33:59 +00:00
|
|
|
gather = unsafeInterleaveIO . gather'
|
|
|
|
gather' S.Stop = return LI.Empty
|
|
|
|
gather' (S.Error err) = giveup err
|
|
|
|
gather' (S.Skip s) = gather' s
|
|
|
|
gather' (S.Effect ms) = ms >>= gather'
|
|
|
|
gather' (S.Yield v s) = LI.Chunk v <$> unsafeInterleaveIO (gather' s)
|
2024-07-24 13:45:14 +00:00
|
|
|
|
|
|
|
baf = associatedFileToB64FilePath af
|
|
|
|
#else
|
2024-07-24 19:12:16 +00:00
|
|
|
clientGet _ _ _ _ = ()
|
2024-07-24 12:33:59 +00:00
|
|
|
#endif
|
2024-07-23 18:12:03 +00:00
|
|
|
|
|
|
|
clientCheckPresent :: Key -> ClientAction Bool
|
2024-07-24 12:33:59 +00:00
|
|
|
#ifdef WITH_SERVANT
|
2024-07-23 18:12:03 +00:00
|
|
|
clientCheckPresent key clientenv (ProtocolVersion ver) su cu bypass auth =
|
|
|
|
liftIO $ withClientM (cli su (B64Key key) cu bypass auth) clientenv $ \case
|
2024-07-23 18:58:49 +00:00
|
|
|
Left err -> return (Left err)
|
|
|
|
Right (CheckPresentResult res) -> return (Right res)
|
2024-07-23 18:12:03 +00:00
|
|
|
where
|
|
|
|
cli = case ver of
|
|
|
|
3 -> flip v3 V3
|
|
|
|
2 -> flip v2 V2
|
|
|
|
1 -> flip v1 V1
|
|
|
|
0 -> flip v0 V0
|
|
|
|
_ -> error "unsupported protocol version"
|
|
|
|
|
|
|
|
_ :<|> _ :<|> _ :<|> _ :<|>
|
|
|
|
v3 :<|> v2 :<|> v1 :<|> v0 :<|> _ = client p2pHttpAPI
|
2024-07-24 12:33:59 +00:00
|
|
|
#else
|
|
|
|
clientCheckPresent _ = ()
|
|
|
|
#endif
|
2024-07-23 18:12:03 +00:00
|
|
|
|
2024-07-25 14:11:09 +00:00
|
|
|
-- Similar to P2P.Protocol.remove.
|
|
|
|
clientRemoveWithProof
|
2024-07-24 16:33:26 +00:00
|
|
|
:: Maybe SafeDropProof
|
|
|
|
-> Key
|
2024-07-25 14:11:09 +00:00
|
|
|
-> Annex RemoveResultPlus
|
|
|
|
-> Remote
|
|
|
|
-> Annex RemoveResultPlus
|
|
|
|
clientRemoveWithProof proof k unabletoremove remote =
|
|
|
|
case safeDropProofEndTime =<< proof of
|
|
|
|
Nothing -> removeanytime
|
|
|
|
Just endtime -> removebefore endtime
|
|
|
|
where
|
|
|
|
removeanytime = p2pHttpClient remote giveup (clientRemove k)
|
|
|
|
|
|
|
|
removebefore endtime =
|
|
|
|
p2pHttpClientVersions useversion remote giveup clientGetTimestamp >>= \case
|
|
|
|
Just (GetTimestampResult (Timestamp remotetime)) ->
|
|
|
|
removebefore' endtime remotetime
|
|
|
|
-- Peer is too old to support REMOVE-BEFORE.
|
|
|
|
Nothing -> removeanytime
|
|
|
|
|
|
|
|
removebefore' endtime remotetime =
|
|
|
|
canRemoveBefore endtime remotetime (liftIO getPOSIXTime) >>= \case
|
|
|
|
Just remoteendtime -> p2pHttpClient remote giveup $
|
|
|
|
clientRemoveBefore k (Timestamp remoteendtime)
|
|
|
|
Nothing -> unabletoremove
|
|
|
|
|
|
|
|
useversion v = v >= ProtocolVersion 3
|
|
|
|
|
|
|
|
clientRemove :: Key -> ClientAction RemoveResultPlus
|
2024-07-24 19:12:16 +00:00
|
|
|
#ifdef WITH_SERVANT
|
2024-07-25 14:11:09 +00:00
|
|
|
clientRemove k clientenv (ProtocolVersion ver) su cu bypass auth =
|
2024-07-24 16:33:26 +00:00
|
|
|
liftIO $ withClientM cli clientenv return
|
2024-07-23 18:12:03 +00:00
|
|
|
where
|
2024-07-24 16:33:26 +00:00
|
|
|
bk = B64Key k
|
|
|
|
|
2024-07-23 18:12:03 +00:00
|
|
|
cli = case ver of
|
2024-07-24 16:33:26 +00:00
|
|
|
3 -> v3 su V3 bk cu bypass auth
|
|
|
|
2 -> v2 su V2 bk cu bypass auth
|
|
|
|
1 -> plus <$> v1 su V1 bk cu bypass auth
|
|
|
|
0 -> plus <$> v0 su V0 bk cu bypass auth
|
2024-07-23 18:12:03 +00:00
|
|
|
_ -> error "unsupported protocol version"
|
|
|
|
|
|
|
|
_ :<|> _ :<|> _ :<|> _ :<|>
|
|
|
|
_ :<|> _ :<|> _ :<|> _ :<|>
|
|
|
|
v3 :<|> v2 :<|> v1 :<|> v0 :<|> _ = client p2pHttpAPI
|
2024-07-24 16:33:26 +00:00
|
|
|
#else
|
2024-07-25 14:11:09 +00:00
|
|
|
clientRemove _ = ()
|
2024-07-24 12:33:59 +00:00
|
|
|
#endif
|
2024-07-23 18:12:03 +00:00
|
|
|
|
|
|
|
clientRemoveBefore
|
2024-07-24 19:12:16 +00:00
|
|
|
:: Key
|
2024-07-23 18:12:03 +00:00
|
|
|
-> Timestamp
|
2024-07-24 19:12:16 +00:00
|
|
|
-> ClientAction RemoveResultPlus
|
|
|
|
#ifdef WITH_SERVANT
|
|
|
|
clientRemoveBefore k ts clientenv (ProtocolVersion ver) su cu bypass auth =
|
|
|
|
liftIO $ withClientM (cli su (B64Key k) cu bypass ts auth) clientenv return
|
2024-07-23 18:12:03 +00:00
|
|
|
where
|
|
|
|
cli = case ver of
|
|
|
|
3 -> flip v3 V3
|
|
|
|
_ -> error "unsupported protocol version"
|
|
|
|
|
|
|
|
_ :<|> _ :<|> _ :<|> _ :<|>
|
|
|
|
_ :<|> _ :<|> _ :<|> _ :<|>
|
|
|
|
_ :<|> _ :<|> _ :<|> _ :<|>
|
|
|
|
v3 :<|> _ = client p2pHttpAPI
|
2024-07-24 19:12:16 +00:00
|
|
|
#else
|
|
|
|
clientRemoveBefore _ _ = ()
|
2024-07-24 12:33:59 +00:00
|
|
|
#endif
|
2024-07-23 18:12:03 +00:00
|
|
|
|
2024-07-24 19:12:16 +00:00
|
|
|
clientGetTimestamp :: ClientAction GetTimestampResult
|
2024-07-24 12:33:59 +00:00
|
|
|
#ifdef WITH_SERVANT
|
2024-07-23 18:12:03 +00:00
|
|
|
clientGetTimestamp clientenv (ProtocolVersion ver) su cu bypass auth =
|
2024-07-24 19:12:16 +00:00
|
|
|
liftIO $ withClientM (cli su cu bypass auth) clientenv return
|
2024-07-23 18:12:03 +00:00
|
|
|
where
|
|
|
|
cli = case ver of
|
|
|
|
3 -> flip v3 V3
|
|
|
|
_ -> error "unsupported protocol version"
|
|
|
|
|
|
|
|
_ :<|> _ :<|> _ :<|> _ :<|>
|
|
|
|
_ :<|> _ :<|> _ :<|> _ :<|>
|
|
|
|
_ :<|> _ :<|> _ :<|> _ :<|>
|
|
|
|
_ :<|>
|
|
|
|
v3 :<|> _ = client p2pHttpAPI
|
2024-07-24 19:12:16 +00:00
|
|
|
#else
|
|
|
|
clientGetTimestamp = ()
|
2024-07-24 12:33:59 +00:00
|
|
|
#endif
|
2024-07-23 18:12:03 +00:00
|
|
|
|
|
|
|
clientPut
|
2024-07-24 16:05:10 +00:00
|
|
|
:: MeterUpdate
|
|
|
|
-> Key
|
2024-07-23 18:12:03 +00:00
|
|
|
-> Maybe Offset
|
|
|
|
-> AssociatedFile
|
|
|
|
-> FilePath
|
|
|
|
-> FileSize
|
|
|
|
-> Annex Bool
|
2024-07-24 16:05:10 +00:00
|
|
|
-- ^ Called after sending the file to check if it's valid.
|
|
|
|
-> ClientAction PutResultPlus
|
2024-07-24 19:12:16 +00:00
|
|
|
#ifdef WITH_SERVANT
|
2024-07-24 16:05:10 +00:00
|
|
|
clientPut meterupdate k moffset af contentfile contentfilesize validitycheck clientenv (ProtocolVersion ver) su cu bypass auth = do
|
2024-07-23 18:12:03 +00:00
|
|
|
checkv <- liftIO newEmptyTMVarIO
|
|
|
|
checkresultv <- liftIO newEmptyTMVarIO
|
|
|
|
let checker = do
|
|
|
|
liftIO $ atomically $ takeTMVar checkv
|
|
|
|
validitycheck >>= liftIO . atomically . putTMVar checkresultv
|
|
|
|
checkerthread <- liftIO . async =<< forkState checker
|
|
|
|
v <- liftIO $ withBinaryFile contentfile ReadMode $ \h -> do
|
|
|
|
when (offset /= 0) $
|
|
|
|
hSeek h AbsoluteSeek offset
|
|
|
|
withClientM (cli (stream h checkv checkresultv)) clientenv return
|
|
|
|
case v of
|
|
|
|
Left err -> do
|
|
|
|
void $ liftIO $ atomically $ tryPutTMVar checkv ()
|
|
|
|
join $ liftIO (wait checkerthread)
|
2024-07-24 16:05:10 +00:00
|
|
|
return (Left err)
|
2024-07-23 18:12:03 +00:00
|
|
|
Right res -> do
|
|
|
|
join $ liftIO (wait checkerthread)
|
2024-07-24 16:05:10 +00:00
|
|
|
return (Right res)
|
2024-07-23 18:12:03 +00:00
|
|
|
where
|
|
|
|
stream h checkv checkresultv = S.SourceT $ \a -> do
|
2024-07-24 16:14:56 +00:00
|
|
|
bl <- hGetContentsMetered h meterupdate
|
2024-07-23 18:12:03 +00:00
|
|
|
v <- newMVar (0, filter (not . B.null) (L.toChunks bl))
|
|
|
|
a (go v)
|
|
|
|
where
|
|
|
|
go v = S.fromActionStep B.null $ modifyMVar v $ \case
|
|
|
|
(n, (b:[])) -> do
|
|
|
|
let !n' = n + B.length b
|
|
|
|
ifM (checkvalid n')
|
|
|
|
( return ((n', []), b)
|
|
|
|
-- The key's content is invalid, but
|
|
|
|
-- the amount of data is the same as
|
|
|
|
-- the DataLengthHeader indicates.
|
|
|
|
-- Truncate the stream by one byte to
|
|
|
|
-- indicate to the server that it's
|
|
|
|
-- not valid.
|
|
|
|
, return
|
|
|
|
( (n' - 1, [])
|
|
|
|
, B.take (B.length b - 1) b
|
|
|
|
)
|
|
|
|
)
|
|
|
|
(n, []) -> do
|
|
|
|
void $ checkvalid n
|
|
|
|
return ((n, []), mempty)
|
|
|
|
(n, (b:bs)) ->
|
|
|
|
let !n' = n + B.length b
|
|
|
|
in return ((n', bs), b)
|
|
|
|
|
|
|
|
checkvalid n = do
|
|
|
|
void $ liftIO $ atomically $ tryPutTMVar checkv ()
|
|
|
|
valid <- liftIO $ atomically $ readTMVar checkresultv
|
|
|
|
if not valid
|
|
|
|
then return (n /= fromIntegral nlen)
|
|
|
|
else return True
|
|
|
|
|
|
|
|
baf = case af of
|
|
|
|
AssociatedFile Nothing -> Nothing
|
|
|
|
AssociatedFile (Just f) -> Just (B64FilePath f)
|
|
|
|
|
|
|
|
len = DataLength nlen
|
|
|
|
|
|
|
|
nlen = contentfilesize - offset
|
|
|
|
|
|
|
|
offset = case moffset of
|
|
|
|
Nothing -> 0
|
|
|
|
Just (Offset o) -> fromIntegral o
|
2024-07-24 16:05:10 +00:00
|
|
|
|
|
|
|
bk = B64Key k
|
2024-07-23 18:12:03 +00:00
|
|
|
|
|
|
|
cli src = case ver of
|
2024-07-24 16:05:10 +00:00
|
|
|
3 -> v3 su V3 len bk cu bypass baf moffset src auth
|
|
|
|
2 -> v2 su V2 len bk cu bypass baf moffset src auth
|
|
|
|
1 -> plus <$> v1 su V1 len bk cu bypass baf moffset src auth
|
|
|
|
0 -> plus <$> v0 su V0 len bk cu bypass baf moffset src auth
|
2024-07-23 18:12:03 +00:00
|
|
|
_ -> error "unsupported protocol version"
|
|
|
|
|
|
|
|
_ :<|> _ :<|> _ :<|> _ :<|>
|
|
|
|
_ :<|> _ :<|> _ :<|> _ :<|>
|
|
|
|
_ :<|> _ :<|> _ :<|> _ :<|>
|
|
|
|
_ :<|>
|
|
|
|
_ :<|>
|
|
|
|
v3 :<|> v2 :<|> v1 :<|> v0 :<|> _ = client p2pHttpAPI
|
2024-07-24 16:05:10 +00:00
|
|
|
#else
|
|
|
|
clientPut _ _ _ _ _ _ _ = ()
|
2024-07-24 12:33:59 +00:00
|
|
|
#endif
|
2024-07-23 18:12:03 +00:00
|
|
|
|
|
|
|
clientPutOffset
|
2024-07-24 16:05:10 +00:00
|
|
|
:: Key
|
|
|
|
-> ClientAction PutOffsetResultPlus
|
2024-07-24 19:12:16 +00:00
|
|
|
#ifdef WITH_SERVANT
|
2024-07-24 16:05:10 +00:00
|
|
|
clientPutOffset k clientenv (ProtocolVersion ver) su cu bypass auth
|
|
|
|
| ver == 0 = return (Right (PutOffsetResultPlus (Offset 0)))
|
|
|
|
| otherwise = liftIO $ withClientM cli clientenv return
|
2024-07-23 18:12:03 +00:00
|
|
|
where
|
2024-07-24 16:05:10 +00:00
|
|
|
bk = B64Key k
|
|
|
|
|
2024-07-23 18:12:03 +00:00
|
|
|
cli = case ver of
|
2024-07-24 16:05:10 +00:00
|
|
|
3 -> v3 su V3 bk cu bypass auth
|
|
|
|
2 -> v2 su V2 bk cu bypass auth
|
|
|
|
1 -> plus <$> v1 su V1 bk cu bypass auth
|
2024-07-23 18:12:03 +00:00
|
|
|
_ -> error "unsupported protocol version"
|
|
|
|
|
|
|
|
_ :<|> _ :<|> _ :<|> _ :<|>
|
|
|
|
_ :<|> _ :<|> _ :<|> _ :<|>
|
|
|
|
_ :<|> _ :<|> _ :<|> _ :<|>
|
|
|
|
_ :<|>
|
|
|
|
_ :<|>
|
|
|
|
_ :<|> _ :<|> _ :<|> _ :<|>
|
|
|
|
v3 :<|> v2 :<|> v1 :<|> _ = client p2pHttpAPI
|
2024-07-24 16:05:10 +00:00
|
|
|
#else
|
|
|
|
clientPutOffset _ = ()
|
2024-07-24 12:33:59 +00:00
|
|
|
#endif
|
2024-07-23 18:12:03 +00:00
|
|
|
|
|
|
|
clientLockContent
|
2024-07-24 17:42:57 +00:00
|
|
|
:: Key
|
|
|
|
-> ClientAction LockResult
|
2024-07-24 19:12:16 +00:00
|
|
|
#ifdef WITH_SERVANT
|
2024-07-24 17:42:57 +00:00
|
|
|
clientLockContent k clientenv (ProtocolVersion ver) su cu bypass auth =
|
|
|
|
liftIO $ withClientM (cli (B64Key k) cu bypass auth) clientenv return
|
2024-07-23 18:12:03 +00:00
|
|
|
where
|
|
|
|
cli = case ver of
|
|
|
|
3 -> v3 su V3
|
|
|
|
2 -> v2 su V2
|
|
|
|
1 -> v1 su V1
|
|
|
|
0 -> v0 su V0
|
|
|
|
_ -> error "unsupported protocol version"
|
|
|
|
|
|
|
|
_ :<|> _ :<|> _ :<|> _ :<|>
|
|
|
|
_ :<|> _ :<|> _ :<|> _ :<|>
|
|
|
|
_ :<|> _ :<|> _ :<|> _ :<|>
|
|
|
|
_ :<|>
|
|
|
|
_ :<|>
|
|
|
|
_ :<|> _ :<|> _ :<|> _ :<|>
|
|
|
|
_ :<|> _ :<|> _ :<|>
|
|
|
|
v3 :<|> v2 :<|> v1 :<|> v0 :<|> _ = client p2pHttpAPI
|
2024-07-24 17:42:57 +00:00
|
|
|
#else
|
|
|
|
clientLockContent _ = ()
|
2024-07-24 12:33:59 +00:00
|
|
|
#endif
|
2024-07-23 18:12:03 +00:00
|
|
|
|
|
|
|
clientKeepLocked
|
2024-07-24 17:42:57 +00:00
|
|
|
:: LockID
|
|
|
|
-> UUID
|
|
|
|
-> a
|
|
|
|
-> (VerifiedCopy -> Annex a)
|
|
|
|
-- ^ Callback is run only after successfully connecting to the http
|
|
|
|
-- server. The lock will remain held until the callback returns,
|
|
|
|
-- and then will be dropped.
|
|
|
|
-> ClientAction a
|
2024-07-24 19:12:16 +00:00
|
|
|
#ifdef WITH_SERVANT
|
2024-07-24 17:42:57 +00:00
|
|
|
clientKeepLocked lckid remoteuuid unablelock callback clientenv (ProtocolVersion ver) su cu bypass auth = do
|
|
|
|
readyv <- liftIO newEmptyTMVarIO
|
|
|
|
keeplocked <- liftIO newEmptyTMVarIO
|
2024-07-23 18:12:03 +00:00
|
|
|
let cli' = cli lckid (Just cu) bypass auth
|
|
|
|
(Just connectionKeepAlive) (Just keepAlive)
|
2024-07-24 17:42:57 +00:00
|
|
|
(S.fromStepT (unlocksender readyv keeplocked))
|
|
|
|
starttime <- liftIO getPOSIXTime
|
|
|
|
tid <- liftIO $ async $ withClientM cli' clientenv $ \case
|
|
|
|
Right (LockResult _ _) ->
|
|
|
|
atomically $ writeTMVar readyv (Right False)
|
|
|
|
Left err ->
|
|
|
|
atomically $ writeTMVar readyv (Left err)
|
|
|
|
let releaselock = liftIO $ do
|
|
|
|
atomically $ putTMVar keeplocked False
|
|
|
|
wait tid
|
|
|
|
liftIO (atomically $ takeTMVar readyv) >>= \case
|
|
|
|
Left err -> do
|
|
|
|
liftIO $ wait tid
|
|
|
|
return (Left err)
|
|
|
|
Right False -> do
|
|
|
|
liftIO $ wait tid
|
|
|
|
return (Right unablelock)
|
|
|
|
Right True -> do
|
|
|
|
let checker = return $ Left $ starttime + retentionduration
|
|
|
|
Right
|
|
|
|
<$> withVerifiedCopy LockedCopy remoteuuid checker callback
|
|
|
|
`finally` releaselock
|
2024-07-23 18:12:03 +00:00
|
|
|
where
|
2024-07-24 17:42:57 +00:00
|
|
|
retentionduration = fromIntegral $
|
|
|
|
durationSeconds p2pDefaultLockContentRetentionDuration
|
|
|
|
|
|
|
|
unlocksender readyv keeplocked =
|
2024-07-23 18:12:03 +00:00
|
|
|
S.Yield (UnlockRequest False) $ S.Effect $ do
|
|
|
|
return $ S.Effect $ do
|
2024-07-24 17:42:57 +00:00
|
|
|
liftIO $ atomically $ void $
|
|
|
|
tryPutTMVar readyv (Right True)
|
|
|
|
stilllocked <- liftIO $ atomically $
|
|
|
|
takeTMVar keeplocked
|
2024-07-23 18:12:03 +00:00
|
|
|
return $ if stilllocked
|
2024-07-24 17:42:57 +00:00
|
|
|
then unlocksender readyv keeplocked
|
2024-07-23 18:12:03 +00:00
|
|
|
else S.Yield (UnlockRequest True) S.Stop
|
|
|
|
|
|
|
|
cli = case ver of
|
|
|
|
3 -> v3 su V3
|
|
|
|
2 -> v2 su V2
|
|
|
|
1 -> v1 su V1
|
|
|
|
0 -> v0 su V0
|
|
|
|
_ -> error "unsupported protocol version"
|
|
|
|
|
|
|
|
_ :<|> _ :<|> _ :<|> _ :<|>
|
|
|
|
_ :<|> _ :<|> _ :<|> _ :<|>
|
|
|
|
_ :<|> _ :<|> _ :<|> _ :<|>
|
|
|
|
_ :<|>
|
|
|
|
_ :<|>
|
|
|
|
_ :<|> _ :<|> _ :<|> _ :<|>
|
|
|
|
_ :<|> _ :<|> _ :<|>
|
|
|
|
_ :<|> _ :<|> _ :<|> _ :<|>
|
|
|
|
v3 :<|> v2 :<|> v1 :<|> v0 :<|> _ = client p2pHttpAPI
|
2024-07-24 17:42:57 +00:00
|
|
|
#else
|
|
|
|
clientKeepLocked _ _ _ _ = ()
|
2024-07-23 18:12:03 +00:00
|
|
|
#endif
|