memoize construction of the Request -> Request function to apply the UrlOptions
This commit is contained in:
parent
dd619c7166
commit
e0227dfedf
2 changed files with 14 additions and 16 deletions
|
@ -26,7 +26,7 @@ getUserAgent = Annex.getState $
|
||||||
Just . fromMaybe defaultUserAgent . Annex.useragent
|
Just . fromMaybe defaultUserAgent . Annex.useragent
|
||||||
|
|
||||||
getUrlOptions :: Annex U.UrlOptions
|
getUrlOptions :: Annex U.UrlOptions
|
||||||
getUrlOptions = U.UrlOptions
|
getUrlOptions = mkUrlOptions
|
||||||
<$> getUserAgent
|
<$> getUserAgent
|
||||||
<*> headers
|
<*> headers
|
||||||
<*> options
|
<*> options
|
||||||
|
|
|
@ -10,7 +10,8 @@
|
||||||
module Utility.Url (
|
module Utility.Url (
|
||||||
URLString,
|
URLString,
|
||||||
UserAgent,
|
UserAgent,
|
||||||
UrlOptions(..),
|
UrlOptions,
|
||||||
|
mkUrlOptions,
|
||||||
check,
|
check,
|
||||||
checkBoth,
|
checkBoth,
|
||||||
exists,
|
exists,
|
||||||
|
@ -35,30 +36,27 @@ type Headers = [String]
|
||||||
|
|
||||||
type UserAgent = String
|
type UserAgent = String
|
||||||
|
|
||||||
data BaseUrlOptions = BaseUrlOptions
|
data UrlOptions = UrlOptions
|
||||||
{ userAgent :: Maybe UserAgent
|
{ userAgent :: Maybe UserAgent
|
||||||
, reqHeaders :: Headers
|
, reqHeaders :: Headers
|
||||||
, reqParams :: [CommandParam]
|
, reqParams :: [CommandParam]
|
||||||
}
|
|
||||||
|
|
||||||
instance Default BaseUrlOptions
|
|
||||||
where
|
|
||||||
def = UrlOptions Nothing [] []
|
|
||||||
|
|
||||||
data UrlOptions = UrlOptions
|
|
||||||
{ urlOptions :: BaseUrlOptions
|
|
||||||
, applyRequest :: Request -> Request
|
, applyRequest :: Request -> Request
|
||||||
}
|
}
|
||||||
|
|
||||||
mkUrlOptions :: BaseUrlOptions -> UrlOptions
|
instance Default UrlOptions
|
||||||
mkUrlOptions uo = UrlOptions uo applyrequest
|
where
|
||||||
|
def = UrlOptions Nothing [] [] id
|
||||||
|
|
||||||
|
mkUrlOptions :: Maybe UserAgent -> Headers -> [CommandParam] -> UrlOptions
|
||||||
|
mkUrlOptions useragent reqheaders reqparams =
|
||||||
|
UrlOptions useragent reqheaders reqparams applyrequest
|
||||||
where
|
where
|
||||||
applyrequest = \r -> r { requestHeaders = requestHeaders r ++ addedheaders }
|
applyrequest = \r -> r { requestHeaders = requestHeaders r ++ addedheaders }
|
||||||
addedheaders = uaheader ++ otherheaders
|
addedheaders = uaheader ++ otherheaders
|
||||||
uaheader = case userAgent uo of
|
uaheader = case useragent of
|
||||||
Nothing -> []
|
Nothing -> []
|
||||||
Just ua -> [(hUserAgent, B8.fromString ua)]
|
Just ua -> [(hUserAgent, B8.fromString ua)]
|
||||||
otherheaders = map toheader (reqHeaders uo)
|
otherheaders = map toheader reqheaders
|
||||||
toheader s =
|
toheader s =
|
||||||
let (h, v) = separate (== ':') s
|
let (h, v) = separate (== ':') s
|
||||||
h' = CI.mk (B8.fromString h)
|
h' = CI.mk (B8.fromString h)
|
||||||
|
@ -67,7 +65,7 @@ mkUrlOptions uo = UrlOptions uo applyrequest
|
||||||
_ -> (h', B8.fromString v)
|
_ -> (h', B8.fromString v)
|
||||||
|
|
||||||
addUserAgent :: UrlOptions -> [CommandParam] -> [CommandParam]
|
addUserAgent :: UrlOptions -> [CommandParam] -> [CommandParam]
|
||||||
addUserAgent (UrlOptions uo) ps = case userAgent uo of
|
addUserAgent uo ps = case userAgent uo of
|
||||||
Nothing -> ps
|
Nothing -> ps
|
||||||
-- --user-agent works for both wget and curl commands
|
-- --user-agent works for both wget and curl commands
|
||||||
Just ua -> ps ++ [Param "--user-agent", Param ua]
|
Just ua -> ps ++ [Param "--user-agent", Param ua]
|
||||||
|
|
Loading…
Reference in a new issue