{-# LANGUAGE CPP #-}
module Hackage.Security.Client.Repository.HttpLib (
HttpLib(..)
, HttpRequestHeader(..)
, HttpResponseHeader(..)
, HttpStatus(..)
, ProxyConfig(..)
, BodyReader
, bodyReaderFromBS
) where
import MyPrelude
import Data.IORef
import Network.URI hiding (uriPath, path)
import qualified Data.ByteString as BS
import qualified Data.ByteString.Lazy as BS.L
import Hackage.Security.Util.Checked
import Hackage.Security.Client.Repository (SomeRemoteError)
data HttpLib = HttpLib {
HttpLib
-> forall a.
Throws SomeRemoteError =>
[HttpRequestHeader]
-> URI -> ([HttpResponseHeader] -> BodyReader -> IO a) -> IO a
httpGet :: forall a. Throws SomeRemoteError
=> [HttpRequestHeader]
-> URI
-> ([HttpResponseHeader] -> BodyReader -> IO a)
-> IO a
, HttpLib
-> forall a.
Throws SomeRemoteError =>
[HttpRequestHeader]
-> URI
-> (Int, Int)
-> (HttpStatus -> [HttpResponseHeader] -> BodyReader -> IO a)
-> IO a
httpGetRange :: forall a. Throws SomeRemoteError
=> [HttpRequestHeader]
-> URI
-> (Int, Int)
-> (HttpStatus -> [HttpResponseHeader] -> BodyReader -> IO a)
-> IO a
}
data =
HttpRequestMaxAge0
| HttpRequestNoTransform
deriving (HttpRequestHeader -> HttpRequestHeader -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: HttpRequestHeader -> HttpRequestHeader -> Bool
$c/= :: HttpRequestHeader -> HttpRequestHeader -> Bool
== :: HttpRequestHeader -> HttpRequestHeader -> Bool
$c== :: HttpRequestHeader -> HttpRequestHeader -> Bool
Eq, Eq HttpRequestHeader
HttpRequestHeader -> HttpRequestHeader -> Bool
HttpRequestHeader -> HttpRequestHeader -> Ordering
HttpRequestHeader -> HttpRequestHeader -> HttpRequestHeader
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: HttpRequestHeader -> HttpRequestHeader -> HttpRequestHeader
$cmin :: HttpRequestHeader -> HttpRequestHeader -> HttpRequestHeader
max :: HttpRequestHeader -> HttpRequestHeader -> HttpRequestHeader
$cmax :: HttpRequestHeader -> HttpRequestHeader -> HttpRequestHeader
>= :: HttpRequestHeader -> HttpRequestHeader -> Bool
$c>= :: HttpRequestHeader -> HttpRequestHeader -> Bool
> :: HttpRequestHeader -> HttpRequestHeader -> Bool
$c> :: HttpRequestHeader -> HttpRequestHeader -> Bool
<= :: HttpRequestHeader -> HttpRequestHeader -> Bool
$c<= :: HttpRequestHeader -> HttpRequestHeader -> Bool
< :: HttpRequestHeader -> HttpRequestHeader -> Bool
$c< :: HttpRequestHeader -> HttpRequestHeader -> Bool
compare :: HttpRequestHeader -> HttpRequestHeader -> Ordering
$ccompare :: HttpRequestHeader -> HttpRequestHeader -> Ordering
Ord, Int -> HttpRequestHeader -> ShowS
[HttpRequestHeader] -> ShowS
HttpRequestHeader -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [HttpRequestHeader] -> ShowS
$cshowList :: [HttpRequestHeader] -> ShowS
show :: HttpRequestHeader -> String
$cshow :: HttpRequestHeader -> String
showsPrec :: Int -> HttpRequestHeader -> ShowS
$cshowsPrec :: Int -> HttpRequestHeader -> ShowS
Show)
data HttpStatus =
HttpStatus200OK
| HttpStatus206PartialContent
data =
HttpResponseAcceptRangesBytes
deriving (HttpResponseHeader -> HttpResponseHeader -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: HttpResponseHeader -> HttpResponseHeader -> Bool
$c/= :: HttpResponseHeader -> HttpResponseHeader -> Bool
== :: HttpResponseHeader -> HttpResponseHeader -> Bool
$c== :: HttpResponseHeader -> HttpResponseHeader -> Bool
Eq, Eq HttpResponseHeader
HttpResponseHeader -> HttpResponseHeader -> Bool
HttpResponseHeader -> HttpResponseHeader -> Ordering
HttpResponseHeader -> HttpResponseHeader -> HttpResponseHeader
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: HttpResponseHeader -> HttpResponseHeader -> HttpResponseHeader
$cmin :: HttpResponseHeader -> HttpResponseHeader -> HttpResponseHeader
max :: HttpResponseHeader -> HttpResponseHeader -> HttpResponseHeader
$cmax :: HttpResponseHeader -> HttpResponseHeader -> HttpResponseHeader
>= :: HttpResponseHeader -> HttpResponseHeader -> Bool
$c>= :: HttpResponseHeader -> HttpResponseHeader -> Bool
> :: HttpResponseHeader -> HttpResponseHeader -> Bool
$c> :: HttpResponseHeader -> HttpResponseHeader -> Bool
<= :: HttpResponseHeader -> HttpResponseHeader -> Bool
$c<= :: HttpResponseHeader -> HttpResponseHeader -> Bool
< :: HttpResponseHeader -> HttpResponseHeader -> Bool
$c< :: HttpResponseHeader -> HttpResponseHeader -> Bool
compare :: HttpResponseHeader -> HttpResponseHeader -> Ordering
$ccompare :: HttpResponseHeader -> HttpResponseHeader -> Ordering
Ord, Int -> HttpResponseHeader -> ShowS
[HttpResponseHeader] -> ShowS
HttpResponseHeader -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [HttpResponseHeader] -> ShowS
$cshowList :: [HttpResponseHeader] -> ShowS
show :: HttpResponseHeader -> String
$cshow :: HttpResponseHeader -> String
showsPrec :: Int -> HttpResponseHeader -> ShowS
$cshowsPrec :: Int -> HttpResponseHeader -> ShowS
Show)
data ProxyConfig a =
ProxyConfigNone
| ProxyConfigUse a
| ProxyConfigAuto
type BodyReader = IO BS.ByteString
bodyReaderFromBS :: BS.L.ByteString -> IO BodyReader
bodyReaderFromBS :: ByteString -> IO BodyReader
bodyReaderFromBS ByteString
lazyBS = do
IORef [ByteString]
chunks <- forall a. a -> IO (IORef a)
newIORef forall a b. (a -> b) -> a -> b
$ ByteString -> [ByteString]
BS.L.toChunks ByteString
lazyBS
let br :: BodyReader
br = do [ByteString]
bss <- forall a. IORef a -> IO a
readIORef IORef [ByteString]
chunks
case [ByteString]
bss of
[] -> forall (m :: * -> *) a. Monad m => a -> m a
return ByteString
BS.empty
(ByteString
bs:[ByteString]
bss') -> forall a. IORef a -> a -> IO ()
writeIORef IORef [ByteString]
chunks [ByteString]
bss' forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> forall (m :: * -> *) a. Monad m => a -> m a
return ByteString
bs
forall (m :: * -> *) a. Monad m => a -> m a
return BodyReader
br