2020-04-09 14:59:25 +00:00
|
|
|
{-# LANGUAGE DataKinds #-}
|
|
|
|
{-# LANGUAGE FlexibleContexts #-}
|
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
|
|
|
{-# LANGUAGE TypeFamilies #-}
|
|
|
|
|
|
|
|
|
|
|
|
module GHCup.Download.IOStreams where
|
|
|
|
|
|
|
|
|
|
|
|
import GHCup.Download.Utils
|
|
|
|
import GHCup.Errors
|
|
|
|
import GHCup.Types.JSON ( )
|
2022-05-21 20:54:18 +00:00
|
|
|
import GHCup.Prelude
|
2024-01-20 10:23:08 +00:00
|
|
|
import GHCup.Utils.URI
|
2020-04-09 14:59:25 +00:00
|
|
|
|
|
|
|
import Control.Applicative
|
|
|
|
import Control.Exception.Safe
|
|
|
|
import Control.Monad
|
|
|
|
import Control.Monad.Reader
|
|
|
|
import Data.ByteString ( ByteString )
|
2021-07-24 14:36:31 +00:00
|
|
|
import Data.CaseInsensitive ( CI, original, mk )
|
2020-04-09 14:59:25 +00:00
|
|
|
import Data.IORef
|
|
|
|
import Data.Maybe
|
|
|
|
import Data.Text.Read
|
|
|
|
import Haskus.Utils.Variant.Excepts
|
|
|
|
import Network.Http.Client hiding ( URL )
|
|
|
|
import Prelude hiding ( abs
|
|
|
|
, readFile
|
|
|
|
, writeFile
|
|
|
|
)
|
|
|
|
import System.ProgressBar
|
2024-01-20 10:23:08 +00:00
|
|
|
import URI.ByteString hiding (parseURI)
|
2020-04-09 14:59:25 +00:00
|
|
|
|
|
|
|
import qualified Data.ByteString as BS
|
|
|
|
import qualified Data.Map.Strict as M
|
|
|
|
import qualified System.IO.Streams as Streams
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
----------------------------
|
|
|
|
--[ Low-level (non-curl) ]--
|
|
|
|
----------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
downloadToFile :: (MonadMask m, MonadIO m)
|
|
|
|
=> Bool -- ^ https?
|
|
|
|
-> ByteString -- ^ host (e.g. "www.example.com")
|
|
|
|
-> ByteString -- ^ path (e.g. "/my/file") including query
|
|
|
|
-> Maybe Int -- ^ optional port (e.g. 3000)
|
2021-05-14 21:09:45 +00:00
|
|
|
-> FilePath -- ^ destination file to create and write to
|
2021-07-24 14:36:31 +00:00
|
|
|
-> M.Map (CI ByteString) ByteString -- ^ additional headers
|
2022-12-21 16:31:41 +00:00
|
|
|
-> Maybe Integer -- ^ expected content length
|
2021-07-24 14:36:31 +00:00
|
|
|
-> Excepts '[DownloadFailed, HTTPNotModified] m Response
|
2022-12-21 16:31:41 +00:00
|
|
|
downloadToFile https host fullPath port destFile addHeaders eCSize = do
|
2021-07-24 14:36:31 +00:00
|
|
|
let stepper = BS.appendFile destFile
|
|
|
|
setup = BS.writeFile destFile mempty
|
|
|
|
catchAllE (\case
|
|
|
|
(V (HTTPStatusError i headers))
|
|
|
|
| i == 304
|
|
|
|
, Just e <- M.lookup (mk "etag") headers -> throwE $ HTTPNotModified (decUTF8Safe e)
|
|
|
|
v -> throwE $ DownloadFailed v
|
2022-12-21 16:31:41 +00:00
|
|
|
) $ downloadInternal True https host fullPath port stepper setup addHeaders eCSize
|
2020-04-09 14:59:25 +00:00
|
|
|
|
|
|
|
|
|
|
|
downloadInternal :: MonadIO m
|
|
|
|
=> Bool -- ^ whether to show a progress bar
|
|
|
|
-> Bool -- ^ https?
|
|
|
|
-> ByteString -- ^ host
|
|
|
|
-> ByteString -- ^ path with query
|
|
|
|
-> Maybe Int -- ^ optional port
|
|
|
|
-> (ByteString -> IO a) -- ^ the consuming step function
|
2021-07-24 14:36:31 +00:00
|
|
|
-> IO a -- ^ setup action
|
|
|
|
-> M.Map (CI ByteString) ByteString -- ^ additional headers
|
2022-12-21 16:31:41 +00:00
|
|
|
-> Maybe Integer
|
2020-04-09 14:59:25 +00:00
|
|
|
-> Excepts
|
|
|
|
'[ HTTPStatusError
|
|
|
|
, URIParseError
|
|
|
|
, UnsupportedScheme
|
|
|
|
, NoLocationHeader
|
|
|
|
, TooManyRedirs
|
2022-12-21 16:31:41 +00:00
|
|
|
, ContentLengthError
|
2020-04-09 14:59:25 +00:00
|
|
|
]
|
|
|
|
m
|
2021-07-24 14:36:31 +00:00
|
|
|
Response
|
2020-04-09 14:59:25 +00:00
|
|
|
downloadInternal = go (5 :: Int)
|
|
|
|
|
|
|
|
where
|
2022-12-21 16:31:41 +00:00
|
|
|
go redirs progressBar https host path port consumer setup addHeaders eCSize = do
|
2020-04-09 14:59:25 +00:00
|
|
|
r <- liftIO $ withConnection' https host port action
|
|
|
|
veitherToExcepts r >>= \case
|
2021-07-24 14:36:31 +00:00
|
|
|
Right r' ->
|
2020-04-09 14:59:25 +00:00
|
|
|
if redirs > 0 then followRedirectURL r' else throwE TooManyRedirs
|
2021-07-24 14:36:31 +00:00
|
|
|
Left res -> pure res
|
2020-04-09 14:59:25 +00:00
|
|
|
where
|
|
|
|
action c = do
|
2021-07-24 14:36:31 +00:00
|
|
|
let q = buildRequest1 $ do
|
|
|
|
http GET path
|
|
|
|
flip M.traverseWithKey addHeaders $ \key val -> setHeader (original key) val
|
2020-04-09 14:59:25 +00:00
|
|
|
|
|
|
|
sendRequest c q emptyBody
|
|
|
|
|
|
|
|
receiveResponse
|
|
|
|
c
|
|
|
|
(\r i' -> runE $ do
|
|
|
|
let scode = getStatusCode r
|
|
|
|
if
|
2021-07-24 14:36:31 +00:00
|
|
|
| scode >= 200 && scode < 300 -> liftIO $ downloadStream r i' >> pure (Left r)
|
|
|
|
| scode == 304 -> throwE $ HTTPStatusError scode (getHeaderMap r)
|
2020-04-09 14:59:25 +00:00
|
|
|
| scode >= 300 && scode < 400 -> case getHeader r "Location" of
|
2021-07-24 14:36:31 +00:00
|
|
|
Just r' -> pure $ Right r'
|
2020-04-09 14:59:25 +00:00
|
|
|
Nothing -> throwE NoLocationHeader
|
2021-07-24 14:36:31 +00:00
|
|
|
| otherwise -> throwE $ HTTPStatusError scode (getHeaderMap r)
|
2020-04-09 14:59:25 +00:00
|
|
|
)
|
|
|
|
|
2024-01-20 10:23:08 +00:00
|
|
|
followRedirectURL bs = case parseURI bs of
|
2020-04-09 14:59:25 +00:00
|
|
|
Right uri' -> do
|
|
|
|
(https', host', fullPath', port') <- liftE $ uriToQuadruple uri'
|
2022-12-21 16:31:41 +00:00
|
|
|
go (redirs - 1) progressBar https' host' fullPath' port' consumer setup addHeaders eCSize
|
2020-04-09 14:59:25 +00:00
|
|
|
Left e -> throwE e
|
|
|
|
|
|
|
|
downloadStream r i' = do
|
2021-07-24 14:36:31 +00:00
|
|
|
void setup
|
2020-04-09 14:59:25 +00:00
|
|
|
let size = case getHeader r "Content-Length" of
|
2020-04-17 07:30:45 +00:00
|
|
|
Just x' -> case decimal $ decUTF8Safe x' of
|
2022-12-21 16:31:41 +00:00
|
|
|
Left _ -> Nothing
|
|
|
|
Right (r', _) -> Just r'
|
|
|
|
Nothing -> Nothing
|
|
|
|
|
|
|
|
forM_ size $ \s -> forM_ eCSize $ \es -> when (es /= s) $ throwIO (ContentLengthError Nothing (Just s) es)
|
|
|
|
let size' = eCSize <|> size
|
|
|
|
|
|
|
|
(mpb :: Maybe (ProgressBar ())) <- case (progressBar, size') of
|
|
|
|
(True, Just size'') -> Just <$> newProgressBar defStyle 10 (Progress 0 (fromInteger size'') ())
|
|
|
|
_ -> pure Nothing
|
2020-04-09 14:59:25 +00:00
|
|
|
|
2022-12-21 16:31:41 +00:00
|
|
|
ior <- liftIO $ newIORef 0
|
2020-04-09 14:59:25 +00:00
|
|
|
|
|
|
|
outStream <- liftIO $ Streams.makeOutputStream
|
|
|
|
(\case
|
|
|
|
Just bs -> do
|
2022-12-21 16:31:41 +00:00
|
|
|
let len = BS.length bs
|
|
|
|
forM_ mpb $ \pb -> incProgress pb len
|
|
|
|
|
|
|
|
-- check we don't exceed size
|
|
|
|
forM_ size' $ \s -> do
|
|
|
|
cs <- readIORef ior
|
|
|
|
when ((cs + toInteger len) > s) $ throwIO (ContentLengthError Nothing (Just (cs + toInteger len)) s)
|
|
|
|
|
|
|
|
modifyIORef ior (+ toInteger len)
|
|
|
|
|
2020-04-09 14:59:25 +00:00
|
|
|
void $ consumer bs
|
|
|
|
Nothing -> pure ()
|
|
|
|
)
|
|
|
|
liftIO $ Streams.connect i' outStream
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
withConnection' :: Bool
|
|
|
|
-> ByteString
|
|
|
|
-> Maybe Int
|
|
|
|
-> (Connection -> IO a)
|
|
|
|
-> IO a
|
2021-03-11 16:03:51 +00:00
|
|
|
withConnection' https host port = bracket acquire closeConnection
|
2020-04-09 14:59:25 +00:00
|
|
|
|
|
|
|
where
|
|
|
|
acquire = case https of
|
|
|
|
True -> do
|
|
|
|
ctx <- baselineContextSSL
|
|
|
|
openConnectionSSL ctx host (fromIntegral $ fromMaybe 443 port)
|
|
|
|
False -> openConnection host (fromIntegral $ fromMaybe 80 port)
|