module Network.HTTP.Client.Core
( withResponse
, httpLbs
, httpNoBody
, httpRaw
, httpRaw'
, responseOpen
, responseClose
, applyCheckStatus
, httpRedirect
, httpRedirect'
) where
#if !MIN_VERSION_base(4,6,0)
import Prelude hiding (catch)
#endif
import Network.HTTP.Types
import Network.HTTP.Client.Manager
import Network.HTTP.Client.Types
import Network.HTTP.Client.Body
import Network.HTTP.Client.Request
import Network.HTTP.Client.Response
import Network.HTTP.Client.Cookies
import Data.Maybe (fromMaybe, isJust)
import Data.Time
import Control.Exception
import qualified Data.ByteString as S
import qualified Data.ByteString.Char8 as S8
import qualified Data.ByteString.Lazy as L
import Data.Monoid
import Control.Monad (void)
withResponse :: Request
-> Manager
-> (Response BodyReader -> IO a)
-> IO a
withResponse req man f = bracket (responseOpen req man) responseClose f
httpLbs :: Request -> Manager -> IO (Response L.ByteString)
httpLbs req man = withResponse req man $ \res -> do
bss <- brConsume $ responseBody res
return res { responseBody = L.fromChunks bss }
httpNoBody :: Request -> Manager -> IO (Response ())
httpNoBody req man = withResponse req man $ return . void
httpRaw
:: Request
-> Manager
-> IO (Response BodyReader)
httpRaw = fmap (fmap snd) . httpRaw'
httpRaw'
:: Request
-> Manager
-> IO (Request, Response BodyReader)
httpRaw' req0 m = do
req' <- mModifyRequest m $ mSetProxy m req0
(req, cookie_jar') <- case cookieJar req' of
Just cj -> do
now <- getCurrentTime
return $ insertCookiesIntoRequest req' (evictExpiredCookies cj now) now
Nothing -> return (req', mempty)
(timeout', (connRelease, ci, isManaged)) <- getConnectionWrapper
req
(responseTimeout' req)
(failedConnectionException req)
(getConn req m)
ex <- try $ do
cont <- requestBuilder (dropProxyAuthSecure req) ci
getResponse connRelease timeout' req ci cont
case (ex, isManaged) of
(Left e, Reused) | mRetryableException m e -> do
connRelease DontReuse
res <- responseOpen req m
return (req, res)
(Left e, _) -> throwIO e
(Right res, _) -> case cookieJar req' of
Just _ -> do
now' <- getCurrentTime
let (cookie_jar, _) = updateCookieJar res req now' cookie_jar'
return (req, res {responseCookieJar = cookie_jar})
Nothing -> return (req, res)
where
responseTimeout' req
| rt == useDefaultTimeout = mResponseTimeout m
| otherwise = rt
where
rt = responseTimeout req
responseOpen :: Request -> Manager -> IO (Response BodyReader)
responseOpen req0 manager = handle addTlsHostPort $ mWrapIOException manager $ do
(req, res) <-
if redirectCount req0 == 0
then httpRaw' req0 manager
else go (redirectCount req0) req0
maybe (return res) throwIO =<< applyCheckStatus req (checkStatus req) res
where
addTlsHostPort (TlsException e) = throwIO $ TlsExceptionHostPort e (host req0) (port req0)
addTlsHostPort e = throwIO e
go count req' = httpRedirect'
count
(\req -> do
(req'', res) <- httpRaw' req manager
let mreq = getRedirectedRequest req'' (responseHeaders res) (responseCookieJar res) (statusCode (responseStatus res))
return (res, fromMaybe req'' mreq, isJust mreq))
req'
applyCheckStatus
:: Request
-> (Status -> ResponseHeaders -> CookieJar -> Maybe SomeException)
-> Response BodyReader
-> IO (Maybe SomeException)
applyCheckStatus req checkStatus' res =
case checkStatus' (responseStatus res) (responseHeaders res) (responseCookieJar res) of
Nothing -> return Nothing
Just exc -> do
exc' <-
case fromException exc of
Just (StatusCodeException s hdrs cookie_jar) -> do
lbs <- brReadSome (responseBody res) 1024
return $ toException $ StatusCodeException s (hdrs ++
[ ("X-Response-Body-Start", toStrict' lbs)
, ("X-Request-URL", S.concat
[ method req
, " "
, S8.pack $ show $ getUri req
])
]) cookie_jar
_ -> return exc
responseClose res
return (Just exc')
where
#ifndef MIN_VERSION_bytestring
#define MIN_VERSION_bytestring(x,y,z) 1
#endif
#if MIN_VERSION_bytestring(0,10,0)
toStrict' = L.toStrict
#else
toStrict' = S.concat . L.toChunks
#endif
httpRedirect
:: Int
-> (Request -> IO (Response BodyReader, Maybe Request))
-> Request
-> IO (Response BodyReader)
httpRedirect count0 http0 req0 = fmap snd $ httpRedirect' count0 http' req0
where
http' req' = do
(res, mbReq) <- http0 req'
return (res, fromMaybe req0 mbReq, isJust mbReq)
httpRedirect'
:: Int
-> (Request -> IO (Response BodyReader, Request, Bool))
-> Request
-> IO (Request, Response BodyReader)
httpRedirect' count0 http' req0 = go count0 req0 []
where
go count _ ress | count < 0 = throwIO $ TooManyRedirects ress
go count req' ress = do
(res, req, isRedirect) <- http' req'
if isRedirect then do
let maxFlush = 1024
lbs <- brReadSome (responseBody res) maxFlush
`catch` \(_ :: ConnectionClosed) -> return L.empty
responseClose res
go (count 1) req (res { responseBody = lbs }:ress)
else
return (req, res)
responseClose :: Response a -> IO ()
responseClose = runResponseClose . responseClose'