Avoid running annex.http-headers-command more than once.

This commit is contained in:
Joey Hess 2018-04-04 15:00:51 -04:00
parent 98cc34c211
commit 2ec07bc29f
No known key found for this signature in database
GPG key ID: DB12DB0FF05F8F38
11 changed files with 31 additions and 28 deletions

View file

@ -1,7 +1,7 @@
{- Url downloading, with git-annex user agent and configured http
- headers and wget/curl options.
-
- Copyright 2013-2014 Joey Hess <id@joeyh.name>
- Copyright 2013-2018 Joey Hess <id@joeyh.name>
-
- Licensed under the GNU GPL version 3 or higher.
-}
@ -9,7 +9,6 @@
module Annex.Url (
module U,
withUrlOptions,
getUrlOptions,
getUserAgent,
) where
@ -25,16 +24,20 @@ getUserAgent :: Annex (Maybe U.UserAgent)
getUserAgent = Annex.getState $
Just . fromMaybe defaultUserAgent . Annex.useragent
getUrlOptions :: Annex U.UrlOptions
getUrlOptions = mkUrlOptions
<$> getUserAgent
<*> headers
<*> options
withUrlOptions :: (U.UrlOptions -> Annex a) -> Annex a
withUrlOptions a = Annex.getState Annex.urloptions >>= \case
Just uo -> a uo
Nothing -> do
uo <- mk
Annex.changeState $ \s -> s
{ Annex.urloptions = Just uo }
a uo
where
mk = mkUrlOptions
<$> getUserAgent
<*> headers
<*> options
headers = annexHttpHeadersCommand <$> Annex.getGitConfig >>= \case
Just cmd -> lines <$> liftIO (readProcess "sh" ["-c", cmd])
Nothing -> annexHttpHeaders <$> Annex.getGitConfig
options = map Param . annexWebOptions <$> Annex.getGitConfig
withUrlOptions :: (U.UrlOptions -> IO a) -> Annex a
withUrlOptions a = liftIO . a =<< getUrlOptions

View file

@ -124,8 +124,7 @@ youtubeDlTo key url dest = do
-- without it. So, this first downloads part of the content and checks
-- if it's a html page; only then is youtube-dl used.
htmlOnly :: URLString -> a -> Annex a -> Annex a
htmlOnly url fallback a = do
uo <- getUrlOptions
htmlOnly url fallback a = withUrlOptions $ \uo ->
liftIO (downloadPartial url uo htmlPrefixLength) >>= \case
Just bs | isHtmlBs bs -> a
_ -> return fallback