{-# 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
(HttpRequestHeader -> HttpRequestHeader -> Bool)
-> (HttpRequestHeader -> HttpRequestHeader -> Bool)
-> Eq HttpRequestHeader
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: HttpRequestHeader -> HttpRequestHeader -> Bool
== :: HttpRequestHeader -> HttpRequestHeader -> Bool
$c/= :: HttpRequestHeader -> HttpRequestHeader -> Bool
/= :: HttpRequestHeader -> HttpRequestHeader -> Bool
Eq, Eq HttpRequestHeader
Eq HttpRequestHeader =>
(HttpRequestHeader -> HttpRequestHeader -> Ordering)
-> (HttpRequestHeader -> HttpRequestHeader -> Bool)
-> (HttpRequestHeader -> HttpRequestHeader -> Bool)
-> (HttpRequestHeader -> HttpRequestHeader -> Bool)
-> (HttpRequestHeader -> HttpRequestHeader -> Bool)
-> (HttpRequestHeader -> HttpRequestHeader -> HttpRequestHeader)
-> (HttpRequestHeader -> HttpRequestHeader -> HttpRequestHeader)
-> Ord 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
$ccompare :: HttpRequestHeader -> HttpRequestHeader -> Ordering
compare :: HttpRequestHeader -> HttpRequestHeader -> Ordering
$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
>= :: HttpRequestHeader -> HttpRequestHeader -> Bool
$cmax :: HttpRequestHeader -> HttpRequestHeader -> HttpRequestHeader
max :: HttpRequestHeader -> HttpRequestHeader -> HttpRequestHeader
$cmin :: HttpRequestHeader -> HttpRequestHeader -> HttpRequestHeader
min :: HttpRequestHeader -> HttpRequestHeader -> HttpRequestHeader
Ord, Int -> HttpRequestHeader -> ShowS
[HttpRequestHeader] -> ShowS
HttpRequestHeader -> String
(Int -> HttpRequestHeader -> ShowS)
-> (HttpRequestHeader -> String)
-> ([HttpRequestHeader] -> ShowS)
-> Show HttpRequestHeader
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> HttpRequestHeader -> ShowS
showsPrec :: Int -> HttpRequestHeader -> ShowS
$cshow :: HttpRequestHeader -> String
show :: HttpRequestHeader -> String
$cshowList :: [HttpRequestHeader] -> ShowS
showList :: [HttpRequestHeader] -> ShowS
Show)
data HttpStatus =
HttpStatus200OK
| HttpStatus206PartialContent
data =
HttpResponseAcceptRangesBytes
deriving (HttpResponseHeader -> HttpResponseHeader -> Bool
(HttpResponseHeader -> HttpResponseHeader -> Bool)
-> (HttpResponseHeader -> HttpResponseHeader -> Bool)
-> Eq HttpResponseHeader
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: HttpResponseHeader -> HttpResponseHeader -> Bool
== :: HttpResponseHeader -> HttpResponseHeader -> Bool
$c/= :: HttpResponseHeader -> HttpResponseHeader -> Bool
/= :: HttpResponseHeader -> HttpResponseHeader -> Bool
Eq, Eq HttpResponseHeader
Eq HttpResponseHeader =>
(HttpResponseHeader -> HttpResponseHeader -> Ordering)
-> (HttpResponseHeader -> HttpResponseHeader -> Bool)
-> (HttpResponseHeader -> HttpResponseHeader -> Bool)
-> (HttpResponseHeader -> HttpResponseHeader -> Bool)
-> (HttpResponseHeader -> HttpResponseHeader -> Bool)
-> (HttpResponseHeader -> HttpResponseHeader -> HttpResponseHeader)
-> (HttpResponseHeader -> HttpResponseHeader -> HttpResponseHeader)
-> Ord 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
$ccompare :: HttpResponseHeader -> HttpResponseHeader -> Ordering
compare :: HttpResponseHeader -> HttpResponseHeader -> Ordering
$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
>= :: HttpResponseHeader -> HttpResponseHeader -> Bool
$cmax :: HttpResponseHeader -> HttpResponseHeader -> HttpResponseHeader
max :: HttpResponseHeader -> HttpResponseHeader -> HttpResponseHeader
$cmin :: HttpResponseHeader -> HttpResponseHeader -> HttpResponseHeader
min :: HttpResponseHeader -> HttpResponseHeader -> HttpResponseHeader
Ord, Int -> HttpResponseHeader -> ShowS
[HttpResponseHeader] -> ShowS
HttpResponseHeader -> String
(Int -> HttpResponseHeader -> ShowS)
-> (HttpResponseHeader -> String)
-> ([HttpResponseHeader] -> ShowS)
-> Show HttpResponseHeader
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> HttpResponseHeader -> ShowS
showsPrec :: Int -> HttpResponseHeader -> ShowS
$cshow :: HttpResponseHeader -> String
show :: HttpResponseHeader -> String
$cshowList :: [HttpResponseHeader] -> ShowS
showList :: [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 <- [ByteString] -> IO (IORef [ByteString])
forall a. a -> IO (IORef a)
newIORef ([ByteString] -> IO (IORef [ByteString]))
-> [ByteString] -> IO (IORef [ByteString])
forall a b. (a -> b) -> a -> b
$ ByteString -> [ByteString]
BS.L.toChunks ByteString
lazyBS
let br :: BodyReader
br = do [ByteString]
bss <- IORef [ByteString] -> IO [ByteString]
forall a. IORef a -> IO a
readIORef IORef [ByteString]
chunks
case [ByteString]
bss of
[] -> ByteString -> BodyReader
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return ByteString
BS.empty
(ByteString
bs:[ByteString]
bss') -> IORef [ByteString] -> [ByteString] -> IO ()
forall a. IORef a -> a -> IO ()
writeIORef IORef [ByteString]
chunks [ByteString]
bss' IO () -> BodyReader -> BodyReader
forall a b. IO a -> IO b -> IO b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> ByteString -> BodyReader
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return ByteString
bs
BodyReader -> IO BodyReader
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return BodyReader
br