@@ -1,20 +1,9 @@
1−{-# LANGUAGE BangPatterns #-}
2−{-# LANGUAGE CPP #-}
31{-# LANGUAGE DataKinds #-}
4−{-# LANGUAGE DeriveDataTypeable #-}
5−{-# LANGUAGE DeriveGeneric #-}
6−{-# LANGUAGE DeriveLift #-}
7−{-# LANGUAGE FlexibleInstances #-}
8−{-# LANGUAGE GeneralizedNewtypeDeriving #-}
92{-# LANGUAGE LambdaCase #-}
10−{-# LANGUAGE MultiParamTypeClasses #-}
113{-# LANGUAGE OverloadedStrings #-}
124{-# LANGUAGE QuasiQuotes #-}
13−{-# LANGUAGE RankNTypes #-}
145{-# LANGUAGE RecordWildCards #-}
156{-# LANGUAGE RoleAnnotations #-}
16−{-# LANGUAGE ScopedTypeVariables #-}
17−{-# LANGUAGE StandaloneDeriving #-}
187{-# LANGUAGE TemplateHaskellQuotes #-}
198{-# LANGUAGE TypeFamilies #-}
209{-# LANGUAGE UndecidableInstances #-}
@@ -161,6 +150,7 @@ module Network.HTTP.Req
161150 -- *** Headers
162151 header,
163152 attachHeader,
153+ headerRedacted,
164154
165155 -- *** Cookies
166156 -- $cookies
@@ -213,69 +203,72 @@ module Network.HTTP.Req
213203 )
214204where
215205
216−import qualified Blaze.ByteString.Builder as BB
206+import Blaze.ByteString.Builder qualified as BB
217207import Control.Applicative
218208import Control.Arrow (first, second)
219209import Control.Exception hiding (Handler (..), TypeError)
210+import Control.Monad (guard, void, (>=>))
220211import Control.Monad.Base
221212import Control.Monad.Catch (Handler (..), MonadCatch, MonadMask, MonadThrow)
222213import Control.Monad.IO.Class
223214import Control.Monad.IO.Unlift
224−import Control.Monad.Reader
215+import Control.Monad.Reader (ReaderT (ReaderT), ask, lift, runReaderT)
225216import Control.Monad.Trans.Accum (AccumT)
226217import Control.Monad.Trans.Cont (ContT)
227218import Control.Monad.Trans.Control
228219import Control.Monad.Trans.Except (ExceptT)
229220import Control.Monad.Trans.Identity (IdentityT)
230221import Control.Monad.Trans.Maybe (MaybeT)
231−import qualified Control.Monad.Trans.RWS.CPS as RWS.CPS
232−import qualified Control.Monad.Trans.RWS.Lazy as RWS.Lazy
233−import qualified Control.Monad.Trans.RWS.Strict as RWS.Strict
222+import Control.Monad.Trans.RWS.CPS qualified as RWS.CPS
223+import Control.Monad.Trans.RWS.Lazy qualified as RWS.Lazy
224+import Control.Monad.Trans.RWS.Strict qualified as RWS.Strict
234225import Control.Monad.Trans.Select (SelectT)
235−import qualified Control.Monad.Trans.State.Lazy as State.Lazy
236−import qualified Control.Monad.Trans.State.Strict as State.Strict
237−import qualified Control.Monad.Trans.Writer.CPS as Writer.CPS
238−import qualified Control.Monad.Trans.Writer.Lazy as Writer.Lazy
239−import qualified Control.Monad.Trans.Writer.Strict as Writer.Strict
226+import Control.Monad.Trans.State.Lazy qualified as State.Lazy
227+import Control.Monad.Trans.State.Strict qualified as State.Strict
228+import Control.Monad.Trans.Writer.CPS qualified as Writer.CPS
229+import Control.Monad.Trans.Writer.Lazy qualified as Writer.Lazy
230+import Control.Monad.Trans.Writer.Strict qualified as Writer.Strict
240231import Control.Retry
241232import Data.Aeson (FromJSON (..), ToJSON (..))
242−import qualified Data.Aeson as A
233+import Data.Aeson qualified as A
243234import Data.ByteString (ByteString)
244−import qualified Data.ByteString as B
245−import qualified Data.ByteString.Lazy as BL
246−import qualified Data.CaseInsensitive as CI
235+import Data.ByteString qualified as B
236+import Data.ByteString.Lazy qualified as BL
237+import Data.CaseInsensitive qualified as CI
247238import Data.Data (Data)
239+import Data.Default.Class (def)
248240import Data.Function (on)
249241import Data.IORef
250242import Data.Kind (Constraint, Type)
251243import Data.List (foldl', nubBy)
252244import Data.List.NonEmpty (NonEmpty (..))
253−import qualified Data.List.NonEmpty as NE
245+import Data.List.NonEmpty qualified as NE
254246import Data.Maybe (fromMaybe)
255247import Data.Proxy
256248import Data.Semigroup (Endo (..))
249+import Data.Set qualified as S
257250import Data.Text (Text)
258−import qualified Data.Text as T
259−import qualified Data.Text.Encoding as T
251+import Data.Text qualified as T
252+import Data.Text.Encoding qualified as T
260253import Data.Typeable (Typeable, cast)
261254import GHC.Generics
262255import GHC.TypeLits
263−import qualified Language.Haskell.TH as TH
264−import qualified Language.Haskell.TH.Quote as TH
265−import qualified Language.Haskell.TH.Syntax as TH
266−import qualified Network.Connection as NC
267−import qualified Network.HTTP.Client as L
268−import qualified Network.HTTP.Client.Internal as LI
269−import qualified Network.HTTP.Client.MultipartFormData as LM
270−import qualified Network.HTTP.Client.TLS as L
271−import qualified Network.HTTP.Types as Y
256+import Language.Haskell.TH qualified as TH
257+import Language.Haskell.TH.Quote qualified as TH
258+import Language.Haskell.TH.Syntax qualified as TH
259+import Network.Connection qualified as NC
260+import Network.HTTP.Client qualified as L
261+import Network.HTTP.Client.Internal qualified as LI
262+import Network.HTTP.Client.MultipartFormData qualified as LM
263+import Network.HTTP.Client.TLS qualified as L
264+import Network.HTTP.Types qualified as Y
272265import System.IO.Unsafe (unsafePerformIO)
273266import Text.URI (URI)
274−import qualified Text.URI as URI
275−import qualified Text.URI.QQ as QQ
276−import qualified Web.Authenticate.OAuth as OAuth
267+import Text.URI qualified as URI
268+import Text.URI.QQ qualified as QQ
269+import Web.Authenticate.OAuth qualified as OAuth
277270import Web.FormUrlEncoded (FromForm (..), ToForm (..))
278−import qualified Web.FormUrlEncoded as Form
271+import Web.FormUrlEncoded qualified as Form
279272import Web.HttpApiData (ToHttpApiData (..))
280273
281274----------------------------------------------------------------------------
@@ -496,7 +489,7 @@ reqCb method url body responseProxy options adjustRequest =
496489--
497490-- @since 3.7.0
498491reqHandler ::
499− MonadHttp m =>
492+ (MonadHttp m) =>
500493 -- | How to get final result from a 'L.Response'
501494 (L.Response L.BodyReader -> IO b) ->
502495 -- | 'L.Request' to perform
@@ -592,10 +585,10 @@ req' method url body options m = do
592585 withReqManager (m request)
593586
594587-- | Perform an action using the global implicit 'L.Manager' that the rest
595−-- of the library uses. This allows to reuse connections that the
588+-- of the library uses. This allows us to reuse connections that the
596589-- 'L.Manager' controls.
597−withReqManager :: MonadIO m => (L.Manager -> m a) -> m a
598−withReqManager m = liftIO (readIORef globalManager) >>= m
590+withReqManager :: (MonadIO m) => (L.Manager -> m a) -> m a
591+withReqManager m = m globalManager
599592
600593-- | The global 'L.Manager' that 'req' uses. Here we just go with the
601594-- default settings, so users don't need to deal with this manager stuff at
@@ -608,16 +601,16 @@ withReqManager m = liftIO (readIORef globalManager) >>= m
608601-- manager. From that moment on the 'IORef' will be just reused—exactly the
609602-- behavior we want here in order to maximize connection sharing. GHC could
610603-- spoil the plan by inlining the definition, hence the @NOINLINE@ pragma.
611−globalManager :: IORef L.Manager
604+globalManager :: L.Manager
612605globalManager = unsafePerformIO $ do
613606 context <- NC.initConnectionContext
614607 let settings =
615608 L.mkManagerSettingsContext
616609 (Just context)
617− (NC.TLSSettingsSimple False False False)
610+ def
618611 Nothing
619612 manager <- L.newManager settings
620− newIORef manager
613+ return manager
621614{-# NOINLINE globalManager #-}
622615
623616----------------------------------------------------------------------------
@@ -637,7 +630,7 @@ globalManager = unsafePerformIO $ do
637630-- | A type class for monads that support performing HTTP requests.
638631-- Typically, you only need to define the 'handleHttpException' method
639632-- unless you want to tweak 'HttpConfig'.
640−class MonadIO m => MonadHttp m where
633+class (MonadIO m) => MonadHttp m where
641634 -- | This method describes how to deal with 'HttpException' that was
642635 -- caught by the library. One option is to re-throw it if you are OK with
643636 -- exceptions, but if you prefer working with something like
@@ -721,9 +714,8 @@ data HttpConfig = HttpConfig
721714 -- | Max length of preview fragment of response body.
722715 --
723716 -- @since 3.6.0
724− httpConfigBodyPreviewLength :: forall a. Num a => a
717+ httpConfigBodyPreviewLength :: forall a. (Num a) => a
725718 }
726− deriving (Typeable)
727719
728720-- | The default value of 'HttpConfig'.
729721--
@@ -811,27 +803,27 @@ instance (MonadHttp m, Monoid w) => MonadHttp (AccumT w m) where
811803 getHttpConfig = lift getHttpConfig
812804
813805-- | @since 3.10.0
814−instance MonadHttp m => MonadHttp (ContT r m) where
806+instance (MonadHttp m) => MonadHttp (ContT r m) where
815807 handleHttpException = lift . handleHttpException
816808 getHttpConfig = lift getHttpConfig
817809
818810-- | @since 3.10.0
819−instance MonadHttp m => MonadHttp (ExceptT e m) where
811+instance (MonadHttp m) => MonadHttp (ExceptT e m) where
820812 handleHttpException = lift . handleHttpException
821813 getHttpConfig = lift getHttpConfig
822814
823815-- | @since 3.10.0
824−instance MonadHttp m => MonadHttp (IdentityT m) where
816+instance (MonadHttp m) => MonadHttp (IdentityT m) where
825817 handleHttpException = lift . handleHttpException
826818 getHttpConfig = lift getHttpConfig
827819
828820-- | @since 3.10.0
829−instance MonadHttp m => MonadHttp (MaybeT m) where
821+instance (MonadHttp m) => MonadHttp (MaybeT m) where
830822 handleHttpException = lift . handleHttpException
831823 getHttpConfig = lift getHttpConfig
832824
833825-- | @since 3.10.0
834−instance MonadHttp m => MonadHttp (ReaderT r m) where
826+instance (MonadHttp m) => MonadHttp (ReaderT r m) where
835827 handleHttpException = lift . handleHttpException
836828 getHttpConfig = lift getHttpConfig
837829
@@ -851,17 +843,17 @@ instance (MonadHttp m, Monoid w) => MonadHttp (RWS.Strict.RWST r w s m) where
851843 getHttpConfig = lift getHttpConfig
852844
853845-- | @since 3.10.0
854−instance MonadHttp m => MonadHttp (SelectT r m) where
846+instance (MonadHttp m) => MonadHttp (SelectT r m) where
855847 handleHttpException = lift . handleHttpException
856848 getHttpConfig = lift getHttpConfig
857849
858850-- | @since 3.10.0
859−instance MonadHttp m => MonadHttp (State.Lazy.StateT s m) where
851+instance (MonadHttp m) => MonadHttp (State.Lazy.StateT s m) where
860852 handleHttpException = lift . handleHttpException
861853 getHttpConfig = lift getHttpConfig
862854
863855-- | @since 3.10.0
864−instance MonadHttp m => MonadHttp (State.Strict.StateT s m) where
856+instance (MonadHttp m) => MonadHttp (State.Strict.StateT s m) where
865857 handleHttpException = lift . handleHttpException
866858 getHttpConfig = lift getHttpConfig
867859
@@ -885,7 +877,7 @@ instance (MonadHttp m, Monoid w) => MonadHttp (Writer.Strict.WriterT w m) where
885877--
886878-- @since 0.4.0
887879runReq ::
888− MonadIO m =>
880+ (MonadIO m) =>
889881 -- | 'HttpConfig' to use
890882 HttpConfig ->
891883 -- | Computation to run
@@ -990,7 +982,7 @@ class HttpMethod a where
990982 -- | Return name of the method as a 'ByteString'.
991983 httpMethodName :: Proxy a -> ByteString
992984
993−instance HttpMethod method => RequestComponent (Tagged "method" method) where
985+instance (HttpMethod method) => RequestComponent (Tagged "method" method) where
994986 getRequestMod _ = Endo $ \x ->
995987 x {L.method = httpMethodName (Proxy :: Proxy method)}
996988
@@ -1032,25 +1024,20 @@ instance HttpMethod method => RequestComponent (Tagged "method" method) where
10321024-- > -- https://%D1%8E%D0%BD%D0%B8%D0%BA%D0%BE%D0%B4.%D1%80%D1%84
10331025data Url (scheme :: Scheme) = Url Scheme (NonEmpty Text)
10341026 -- NOTE The second value is the path segments in reversed order.
1035− deriving (Eq, Ord, Show, Data, Typeable, Generic)
1027+ deriving (Eq, Ord, Show, Data, Generic)
10361028
10371029type role Url nominal
10381030
10391031-- With template-haskell >=2.15 and text >=1.2.4 Lift can be derived, however
10401032-- the derived lift forgets the type of the scheme.
1041−instance Typeable scheme => TH.Lift (Url scheme) where
1033+instance (Typeable scheme) => TH.Lift (Url scheme) where
10421034 lift url =
10431035 TH.dataToExpQ (fmap liftText . cast) url `TH.sigE` case url of
10441036 Url Http _ -> [t|Url 'Http|]
10451037 Url Https _ -> [t|Url 'Https|]
10461038 where
10471039 liftText t = TH.AppE (TH.VarE 'T.pack) <$> TH.lift (T.unpack t)
1048−
1049−#if MIN_VERSION_template_haskell(2,17,0)
10501040 liftTyped = TH.Code . TH.unsafeTExpCoerce . TH.lift
1051−#elif MIN_VERSION_template_haskell(2,16,0)
1052− liftTyped = TH.unsafeTExpCoerce . TH.lift
1053−#endif
10541041
10551042-- | Given host name, produce a 'Url' which has “http” as its scheme and
10561043-- empty path. This also sets port to @80@.
@@ -1066,7 +1053,7 @@ https = Url Https . pure
10661053-- path segment can be of any type that is an instance of 'ToHttpApiData'.
10671054infixl 5 /~
10681055
1069−(/~) :: ToHttpApiData a => Url scheme -> a -> Url scheme
1056+(/~) :: (ToHttpApiData a) => Url scheme -> a -> Url scheme
10701057Url secure path /~ segment = Url secure (NE.cons (toUrlPiece segment) path)
10711058
10721059-- | A type-constrained version of @('/~')@ to remove ambiguity in the cases
@@ -1254,7 +1241,7 @@ instance HttpBody NoReqBody where
12541241-- charset=utf-8\"@ value.
12551242newtype ReqBodyJson a = ReqBodyJson a
12561243
1257−instance ToJSON a => HttpBody (ReqBodyJson a) where
1244+instance (ToJSON a) => HttpBody (ReqBodyJson a) where
12581245 getRequestBody (ReqBodyJson a) = L.RequestBodyLBS (A.encode a)
12591246 getRequestContentType _ = pure "application/json; charset=utf-8"
12601247
@@ -1353,7 +1340,7 @@ instance HttpBody ReqBodyMultipart where
13531340-- | Create 'ReqBodyMultipart' request body from a collection of 'LM.Part's.
13541341--
13551342-- @since 0.2.0
1356−reqBodyMultipart :: MonadIO m => [LM.Part] -> m ReqBodyMultipart
1343+reqBodyMultipart :: (MonadIO m) => [LM.Part] -> m ReqBodyMultipart
13571344reqBodyMultipart parts = liftIO $ do
13581345 boundary <- LM.webkitBoundary
13591346 body <- LM.renderParts boundary parts
@@ -1393,7 +1380,7 @@ type family
13931380 TypeError
13941381 ('Text "This HTTP method does not allow attaching a request body.")
13951382
1396−instance HttpBody body => RequestComponent (Tagged "body" body) where
1383+instance (HttpBody body) => RequestComponent (Tagged "body" body) where
13971384 getRequestMod (Tagged body) = Endo $ \x ->
13981385 x
13991386 { L.requestBody = getRequestBody body,
@@ -1463,7 +1450,7 @@ instance RequestComponent (Option scheme) where
14631450
14641451-- | Finalize given 'L.Request' by applying a finalizer from the given
14651452-- 'Option' (if it has any).
1466−finalizeRequest :: MonadIO m => Option scheme -> L.Request -> m L.Request
1453+finalizeRequest :: (MonadIO m) => Option scheme -> L.Request -> m L.Request
14671454finalizeRequest (Option _ mfinalizer) = liftIO . fromMaybe pure mfinalizer
14681455
14691456----------------------------------------------------------------------------
@@ -1496,7 +1483,7 @@ name =: value = queryParam name (pure value)
14961483-- This operator is defined in terms of 'queryParam':
14971484--
14981485-- > queryFlag name = queryParam name (Nothing :: Maybe ())
1499−queryFlag :: QueryParam param => Text -> param
1486+queryFlag :: (QueryParam param) => Text -> param
15001487queryFlag name = queryParam name (Nothing :: Maybe ())
15011488
15021489-- | Construct query parameters from a 'ToForm' instance. This function
@@ -1526,7 +1513,7 @@ class QueryParam param where
15261513 -- 'Nothing', it won't be included at all (i.e. you create a flag this
15271514 -- way). It's recommended to use @('=:')@ and 'queryFlag' instead of this
15281515 -- method, because they are easier to read.
1529− queryParam :: ToHttpApiData a => Text -> Maybe a -> param
1516+ queryParam :: (ToHttpApiData a) => Text -> Maybe a -> param
15301517
15311518 -- | Get the query parameter names and values set by 'queryParam'.
15321519 --
@@ -1560,6 +1547,14 @@ attachHeader :: ByteString -> ByteString -> L.Request -> L.Request
15601547attachHeader name value x =
15611548 x {L.requestHeaders = (CI.mk name, value) : L.requestHeaders x}
15621549
1550+-- | Same as 'header', but with redacted values on print.
1551+--
1552+-- @since 3.13.0
1553+headerRedacted :: ByteString -> ByteString -> Option scheme
1554+headerRedacted name value = withRequest $ \x ->
1555+ let y = attachHeader name value x
1556+ in y {L.redactHeaders = CI.mk name `S.insert` L.redactHeaders y}
1557+
15631558----------------------------------------------------------------------------
15641559-- Request—Optional parameters—Cookies
15651560
@@ -1767,7 +1762,7 @@ ignoreResponse = Proxy
17671762newtype JsonResponse a = JsonResponse (L.Response a)
17681763 deriving (Show)
17691764
1770−instance FromJSON a => HttpResponse (JsonResponse a) where
1765+instance (FromJSON a) => HttpResponse (JsonResponse a) where
17711766 type HttpResponseBody (JsonResponse a) = a
17721767 toVanillaResponse (JsonResponse r) = r
17731768 getHttpResponse r = do
@@ -1883,14 +1878,14 @@ brReadN br n = go 0 id id
18831878
18841879-- | Get the response body.
18851880responseBody ::
1886− HttpResponse response =>
1881+ (HttpResponse response) =>
18871882 response ->
18881883 HttpResponseBody response
18891884responseBody = L.responseBody . toVanillaResponse
18901885
18911886-- | Get the response status code.
18921887responseStatusCode ::
1893− HttpResponse response =>
1888+ (HttpResponse response) =>
18941889 response ->
18951890 Int
18961891responseStatusCode =
@@ -1898,7 +1893,7 @@ responseStatusCode =
18981893
18991894-- | Get the response status message.
19001895responseStatusMessage ::
1901− HttpResponse response =>
1896+ (HttpResponse response) =>
19021897 response ->
19031898 ByteString
19041899responseStatusMessage =
@@ -1906,7 +1901,7 @@ responseStatusMessage =
19061901
19071902-- | Lookup a particular header from a response.
19081903responseHeader ::
1909− HttpResponse response =>
1904+ (HttpResponse response) =>
19101905 -- | Response interpretation
19111906 response ->
19121907 -- | Header to lookup
@@ -1918,7 +1913,7 @@ responseHeader r h =
19181913
19191914-- | Get the response 'L.CookieJar'.
19201915responseCookieJar ::
1921− HttpResponse response =>
1916+ (HttpResponse response) =>
19221917 response ->
19231918 L.CookieJar
19241919responseCookieJar = L.responseCookieJar . toVanillaResponse
@@ -2016,7 +2011,7 @@ data HttpException
20162011 | -- | A wrapper with Aeson-produced 'String' describing why decoding
20172012 -- failed
20182013 JsonHttpException String
2019− deriving (Show, Typeable, Generic)
2014+ deriving (Show, Generic)
20202015
20212016instance Exception HttpException
20222017
@@ -2053,4 +2048,4 @@ data Scheme
20532048 Http
20542049 | -- | HTTPS
20552050 Https
2056− deriving (Eq, Ord, Show, Data, Typeable, Generic, TH.Lift)
2051+ deriving (Eq, Ord, Show, Data, Generic, TH.Lift)