2024-07-23 13:12:21 +00:00
|
|
|
{- P2P protocol over HTTP, urls
|
|
|
|
-
|
|
|
|
- Copyright 2024 Joey Hess <id@joeyh.name>
|
|
|
|
-
|
|
|
|
- Licensed under the GNU AGPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
2024-07-23 13:55:14 +00:00
|
|
|
{-# LANGUAGE CPP #-}
|
|
|
|
|
2024-07-23 13:12:21 +00:00
|
|
|
module P2P.Http.Url where
|
|
|
|
|
|
|
|
import Data.List
|
2024-07-23 13:55:14 +00:00
|
|
|
import Network.URI
|
|
|
|
#ifdef WITH_SERVANT
|
2024-07-31 18:07:30 +00:00
|
|
|
import System.FilePath.Posix as P
|
2024-07-23 13:55:14 +00:00
|
|
|
import Servant.Client (BaseUrl(..), Scheme(..))
|
|
|
|
import Text.Read
|
|
|
|
#endif
|
2024-07-23 13:12:21 +00:00
|
|
|
|
|
|
|
defaultP2PHttpProtocolPort :: Int
|
|
|
|
defaultP2PHttpProtocolPort = 9417 -- Git protocol is 9418
|
|
|
|
|
|
|
|
isP2PHttpProtocolUrl :: String -> Bool
|
|
|
|
isP2PHttpProtocolUrl s =
|
|
|
|
"annex+http://" `isPrefixOf` s ||
|
|
|
|
"annex+https://" `isPrefixOf` s
|
2024-07-23 13:55:14 +00:00
|
|
|
|
2024-07-23 16:30:27 +00:00
|
|
|
data P2PHttpUrl = P2PHttpUrl
|
|
|
|
{ p2pHttpUrlString :: String
|
2024-07-23 13:55:14 +00:00
|
|
|
#ifdef WITH_SERVANT
|
2024-07-23 16:30:27 +00:00
|
|
|
, p2pHttpBaseUrl :: BaseUrl
|
2024-07-23 13:55:14 +00:00
|
|
|
#endif
|
2024-07-23 16:30:27 +00:00
|
|
|
}
|
|
|
|
deriving (Show)
|
2024-07-23 13:55:14 +00:00
|
|
|
|
|
|
|
parseP2PHttpUrl :: String -> Maybe P2PHttpUrl
|
|
|
|
parseP2PHttpUrl us
|
|
|
|
| isP2PHttpProtocolUrl us = case parseURI (drop prefixlen us) of
|
|
|
|
Nothing -> Nothing
|
|
|
|
#ifdef WITH_SERVANT
|
2024-07-31 18:07:30 +00:00
|
|
|
Just u ->
|
2024-07-23 13:55:14 +00:00
|
|
|
case uriScheme u of
|
|
|
|
"http:" -> mkbaseurl Http u
|
|
|
|
"https:" -> mkbaseurl Https u
|
|
|
|
_ -> Nothing
|
|
|
|
#else
|
2024-07-31 18:07:30 +00:00
|
|
|
Just _u ->
|
2024-07-29 00:29:42 +00:00
|
|
|
Just $ P2PHttpUrl us
|
2024-07-23 13:55:14 +00:00
|
|
|
#endif
|
|
|
|
| otherwise = Nothing
|
|
|
|
where
|
|
|
|
prefixlen = length "annex+"
|
2024-07-23 16:30:27 +00:00
|
|
|
|
2024-07-23 13:55:14 +00:00
|
|
|
#ifdef WITH_SERVANT
|
|
|
|
mkbaseurl s u = do
|
|
|
|
auth <- uriAuthority u
|
|
|
|
port <- if null (uriPort auth)
|
|
|
|
then Just defaultP2PHttpProtocolPort
|
|
|
|
else readMaybe (dropWhile (== ':') (uriPort auth))
|
2024-07-29 00:29:42 +00:00
|
|
|
return $ P2PHttpUrl us $ BaseUrl
|
2024-07-23 13:55:14 +00:00
|
|
|
{ baseUrlScheme = s
|
|
|
|
, baseUrlHost = uriRegName auth
|
2024-07-23 18:31:32 +00:00
|
|
|
, baseUrlPath = basepath u
|
2024-07-23 13:55:14 +00:00
|
|
|
, baseUrlPort = port
|
|
|
|
}
|
2024-07-24 19:12:16 +00:00
|
|
|
|
|
|
|
-- The servant server uses urls that start with "/git-annex/",
|
|
|
|
-- and so the servant client adds that to the base url. So remove
|
|
|
|
-- it from the url that the user provided. However, it may not be
|
|
|
|
-- present, eg if some other server is speaking the git-annex
|
|
|
|
-- protocol. The UUID is also removed from the end of the url.
|
2024-07-29 00:29:42 +00:00
|
|
|
basepath u = case reverse $ P.splitDirectories (uriPath u) of
|
2024-07-24 19:12:16 +00:00
|
|
|
("git-annex":"/":rest) -> P.joinPath (reverse rest)
|
|
|
|
rest -> P.joinPath (reverse rest)
|
2024-07-23 13:55:14 +00:00
|
|
|
#endif
|
2024-07-23 22:21:01 +00:00
|
|
|
|
2024-07-24 18:36:37 +00:00
|
|
|
unavailableP2PHttpUrl :: P2PHttpUrl -> P2PHttpUrl
|
|
|
|
unavailableP2PHttpUrl p = p
|
|
|
|
#ifdef WITH_SERVANT
|
|
|
|
{ p2pHttpBaseUrl = (p2pHttpBaseUrl p) { baseUrlHost = "!dne!" } }
|
|
|
|
#endif
|