split out Utility.Url.Parse
authorJoey Hess <joeyh@joeyh.name>
Mon, 14 Aug 2023 16:28:10 +0000 (12:28 -0400)
committerJoey Hess <joeyh@joeyh.name>
Mon, 14 Aug 2023 16:28:10 +0000 (12:28 -0400)
This is mostly for git-repair which can't include all of Utility.Url
without adding many dependencies that are not really necessary.

13 files changed:
Annex/Url.hs
Assistant/Restart.hs
Assistant/WebApp/Configurators/WebDAV.hs
Command/AddUrl.hs
Command/FromKey.hs
Git/Construct.hs
Git/Credential.hs
Remote/BitTorrent.hs
Remote/External/Types.hs
RemoteDaemon/Types.hs
Utility/Url.hs
Utility/Url/Parse.hs [new file with mode: 0644]
git-annex.cabal

index 49a90e72211db831cae3757092e55e9aa3ccaf3f..61c52c8a0f2c38d552d9aea11c3f061a17be1064 100644 (file)
@@ -34,6 +34,7 @@ module Annex.Url (
 import Annex.Common
 import qualified Annex
 import qualified Utility.Url as U
+import qualified Utility.Url.Parse as U
 import Utility.Hash (IncrementalVerifier)
 import Utility.IPAddress
 import Network.HTTP.Client.Restricted
index c9a4fbecf56d912a746f446743591b24c863e7db..65b6fe64aa64bd130e50144f19c71406ec1d57b2 100644 (file)
@@ -16,6 +16,7 @@ import Assistant.NamedThread
 import Utility.ThreadScheduler
 import Utility.NotificationBroadcaster
 import Utility.Url
+import Utility.Url.Parse
 import Utility.PID
 import qualified Utility.RawFilePath as R
 import qualified Git.Construct
index e0ada7d914749329131fdda8e32d2825ab457598..473740fa7430d7f7b486f3c65d24ae82b401d828 100644 (file)
@@ -22,7 +22,7 @@ import Assistant.Gpg
 import Types.GitConfig
 import Annex.SpecialRemote.Config
 import Types.ProposedAccepted
-import Utility.Url
+import Utility.Url.Parse
 
 import qualified Data.Map as M
 import qualified Data.Text as T
index 018dedc8828bb4a58062ff9f5d8dbc04c51c09c7..842a854831963f9df31a89a334d3c64617d55b7a 100644 (file)
@@ -33,7 +33,7 @@ import Logs.Location
 import Utility.Metered
 import Utility.HtmlDetect
 import Utility.Path.Max
-import Utility.Url (parseURIPortable)
+import Utility.Url.Parse
 import qualified Utility.RawFilePath as R
 import qualified Annex.Transfer as Transfer
 
index 6d3cc4da60b9f00201fc5a265ee076321938ab75..501ff41a99101286c990821d8f1fb123d6ad4ad0 100644 (file)
@@ -20,7 +20,7 @@ import Annex.Link
 import Annex.FileMatcher
 import Annex.Ingest
 import Git.FilePath
-import Utility.Url
+import Utility.Url.Parse
 
 import Network.URI
 
index 82929d9076d787aa7ae16f608113cca37c9ed5d7..bdab8edba1a0d9ac9f13b190ad3f979562365d9f 100644 (file)
@@ -39,7 +39,7 @@ import Git.Remote
 import Git.FilePath
 import qualified Git.Url as Url
 import Utility.UserInfo
-import Utility.Url (parseURIPortable)
+import Utility.Url.Parse
 
 import qualified Data.ByteString as B
 import qualified System.FilePath.ByteString as P
index fae8905c479c3a77eec104d5fcb62a6b3c8d94e0..3be4f438b204a720e417c2be7ecf5959ad32f98a 100644 (file)
@@ -15,6 +15,7 @@ import Git.Types
 import Git.Command
 import qualified Git.Config as Config
 import Utility.Url
+import Utility.Url.Parse
 
 import qualified Data.Map as M
 import Network.URI
index a237539904192e0f81043c6cbd19a89213ed5e09..dcc6f2900408ed346ba26543e84c58007a5fe2df 100644 (file)
@@ -23,7 +23,7 @@ import Types.CleanupActions
 import Messages.Progress
 import Utility.Metered
 import Utility.Tmp
-import Utility.Url (parseURIPortable)
+import Utility.Url.Parse
 import Backend.URL
 import Annex.Perms
 import Annex.Tmp
index 633dc641bdb2231fd486ac28e600529bb77561e3..ed8aa20346ea2245fe3d3a2f9e180045209af030 100644 (file)
@@ -53,7 +53,8 @@ import Types.Export
 import Types.Availability (Availability(..))
 import Types.Key
 import Git.Types
-import Utility.Url (URLString, parseURIPortable)
+import Utility.Url (URLString)
+import Utility.Url.Parse
 import qualified Utility.SimpleProtocol as Proto
 
 import Control.Concurrent.STM
index fd15c263cd004a1b55eee5c1a8d19ff57854571a..95fd47861c1c6f4e73e5d5db6859955d54f12d84 100644 (file)
@@ -16,6 +16,7 @@ import qualified Utility.SimpleProtocol as Proto
 import Types.GitConfig
 import Annex.ChangedRefs (ChangedRefs)
 import Utility.Url
+import Utility.Url.Parse
 
 import Network.URI
 import Control.Concurrent
index e74ee0a26c6cd05e65131bd6920804c3e80fb7a7..dbe464752789ed2eff07e50a2b668d26d03ea248 100644 (file)
@@ -9,7 +9,6 @@
 {-# LANGUAGE RankNTypes #-}
 {-# LANGUAGE FlexibleContexts #-}
 {-# LANGUAGE BangPatterns #-}
-{-# LANGUAGE CPP #-}
 
 module Utility.Url (
        newManager,
@@ -33,8 +32,6 @@ module Utility.Url (
        downloadConduit,
        sinkResponseFile,
        downloadPartial,
-       parseURIPortable,
-       parseURIRelaxed,
        matchStatusCodeException,
        matchHttpExceptionContent,
        BasicAuth(..),
@@ -52,6 +49,7 @@ import Network.HTTP.Client.Restricted
 import Utility.IPAddress
 import qualified Utility.RawFilePath as R
 import Utility.Hash (IncrementalVerifier(..))
+import Utility.Url.Parse
 
 import Network.URI
 import Network.HTTP.Types
@@ -72,9 +70,6 @@ import Network.BSD (getProtocolNumber)
 import Data.Either
 import Data.Conduit
 import Text.Read
-#ifdef mingw32_HOST_OS
-import qualified System.FilePath.Windows as PW
-#endif
 
 type URLString = String
 
@@ -612,30 +607,6 @@ downloadPartial url uo n = case parseURIRelaxed url of
                                        then Just <$> brReadSome (responseBody resp) n
                                        else return Nothing
 
-{- On unix this is the same as parseURI. But on Windows,
- - it can parse urls such as file:///C:/path/to/file
- - parseURI normally parses that as a path /C:/path/to/file
- - and this simply removes the excess leading slash when there is a
- - drive letter after it. -}
-parseURIPortable :: URLString -> Maybe URI
-#ifndef mingw32_HOST_OS
-parseURIPortable = parseURI
-#else
-parseURIPortable s
-       | "file:" `isPrefixOf` s = do
-               u <- parseURI s
-               return $ case PW.splitDirectories (uriPath u) of
-                       (p:d:_) | all PW.isPathSeparator p && PW.isDrive d ->
-                               u { uriPath = dropWhile PW.isPathSeparator (uriPath u) }
-                       _ -> u
-       | otherwise = parseURI s
-#endif
-
-{- Allows for spaces and other stuff in urls, properly escaping them. -}
-parseURIRelaxed :: URLString -> Maybe URI
-parseURIRelaxed s = maybe (parseURIRelaxed' s) Just $
-       parseURIPortable $ escapeURIString isAllowedInURI s
-
 {- Generate a http-conduit Request for an URI. This is able
  - to deal with some urls that parseRequest would usually reject. 
  -}
@@ -649,23 +620,6 @@ parseRequestRelaxed u = case uriAuthority u of
                        u { uriAuthority = Just $ ua { uriPort = "" } }
        _ -> parseRequest (show u)
 
-{- Some characters like '[' are allowed in eg, the address of
- - an uri, but cannot appear unescaped further along in the uri.
- - This handles that, expensively, by successively escaping each character
- - from the back of the url until the url parses.
- -}
-parseURIRelaxed' :: URLString -> Maybe URI
-parseURIRelaxed' s = go [] (reverse s)
-  where
-       go back [] = parseURI back
-       go back (c:cs) = case parseURI (escapeURIString isAllowedInURI (reverse (c:cs)) ++ back) of
-               Just u -> Just u
-               Nothing -> go (escapeURIChar escapemore c ++ back) cs
-
-       escapemore '[' = False
-       escapemore ']' = False
-       escapemore c = isAllowedInURI c
-
 hAcceptEncoding :: CI.CI B.ByteString
 hAcceptEncoding = "Accept-Encoding"
 
diff --git a/Utility/Url/Parse.hs b/Utility/Url/Parse.hs
new file mode 100644 (file)
index 0000000..c282bb8
--- /dev/null
@@ -0,0 +1,89 @@
+{- Url parsing.
+ -
+ - Copyright 2011-2023 Joey Hess <id@joeyh.name>
+ -
+ - License: BSD-2-clause
+ -}
+
+{-# LANGUAGE OverloadedStrings #-}
+{-# LANGUAGE RankNTypes #-}
+{-# LANGUAGE FlexibleContexts #-}
+{-# LANGUAGE BangPatterns #-}
+{-# LANGUAGE CPP #-}
+
+module Utility.Url.Parse (
+       parseURIPortable,
+       parseURIRelaxed,
+) where
+
+import Common
+import Utility.Debug
+import Utility.Metered
+import Network.HTTP.Client.Restricted
+import Utility.IPAddress
+import qualified Utility.RawFilePath as R
+import Utility.Hash (IncrementalVerifier(..))
+
+import Network.URI
+import Network.HTTP.Types
+import qualified Data.CaseInsensitive as CI
+import qualified Data.ByteString as B
+import qualified Data.ByteString.UTF8 as B8
+import qualified Data.ByteString.Lazy as L
+import qualified Data.Set as S
+import Control.Exception (throwIO, evaluate)
+import Control.Monad.Trans.Resource
+import Control.Monad.IO.Class (MonadIO)
+import Control.DeepSeq
+import Network.HTTP.Conduit
+import Network.HTTP.Client
+import Network.HTTP.Simple (getResponseHeader)
+import Network.Socket
+import Network.BSD (getProtocolNumber)
+import Data.Either
+import Data.Conduit
+import Text.Read
+#ifdef mingw32_HOST_OS
+import qualified System.FilePath.Windows as PW
+#endif
+
+{- On unix this is the same as parseURI. But on Windows,
+ - it can parse urls such as file:///C:/path/to/file
+ - parseURI normally parses that as a path /C:/path/to/file
+ - and this simply removes the excess leading slash when there is a
+ - drive letter after it. -}
+parseURIPortable :: String -> Maybe URI
+#ifndef mingw32_HOST_OS
+parseURIPortable = parseURI
+#else
+parseURIPortable s
+       | "file:" `isPrefixOf` s = do
+               u <- parseURI s
+               return $ case PW.splitDirectories (uriPath u) of
+                       (p:d:_) | all PW.isPathSeparator p && PW.isDrive d ->
+                               u { uriPath = dropWhile PW.isPathSeparator (uriPath u) }
+                       _ -> u
+       | otherwise = parseURI s
+#endif
+
+{- Allows for spaces and other stuff in urls, properly escaping them. -}
+parseURIRelaxed :: String -> Maybe URI
+parseURIRelaxed s = maybe (parseURIRelaxed' s) Just $
+       parseURIPortable $ escapeURIString isAllowedInURI s
+
+{- Some characters like '[' are allowed in eg, the address of
+ - an uri, but cannot appear unescaped further along in the uri.
+ - This handles that, expensively, by successively escaping each character
+ - from the back of the url until the url parses.
+ -}
+parseURIRelaxed' :: String -> Maybe URI
+parseURIRelaxed' s = go [] (reverse s)
+  where
+       go back [] = parseURI back
+       go back (c:cs) = case parseURI (escapeURIString isAllowedInURI (reverse (c:cs)) ++ back) of
+               Just u -> Just u
+               Nothing -> go (escapeURIChar escapemore c ++ back) cs
+
+       escapemore '[' = False
+       escapemore ']' = False
+       escapemore c = isAllowedInURI c
index 837680cbd5aaac92450eae65b55256852cff22b5..68c456a8491573dacffb2f498c0238974c195ad5 100644 (file)
@@ -1175,6 +1175,7 @@ Executable git-annex
     Utility.Touch
     Utility.Tuple
     Utility.Url
+    Utility.Url.Parse
     Utility.UserInfo
     Utility.Verifiable