1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
|
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RankNTypes #-}
-- | HTTP\/2 client library.
--
-- Example:
--
-- > {-# LANGUAGE OverloadedStrings #-}
-- > {-# LANGUAGE RankNTypes #-}
-- >
-- > module Main where
-- >
-- > import Control.Concurrent.Async
-- > import qualified Control.Exception as E
-- > import qualified Data.ByteString.Char8 as C8
-- > import Network.HTTP.Types
-- > import Network.Run.TCP (runTCPClient) -- network-run
-- >
-- > import Network.HTTP2.Client
-- >
-- > serverName :: String
-- > serverName = "127.0.0.1"
-- >
-- > main :: IO ()
-- > main = runTCPClient serverName "80" $ runHTTP2Client serverName
-- > where
-- > cliconf host = defaultClientConfig { authority = C8.pack host }
-- > runHTTP2Client host s = E.bracket (allocSimpleConfig s 4096)
-- > freeSimpleConfig
-- > (\conf -> run (cliconf host) conf client)
-- > client :: Client ()
-- > client sendRequest _aux = do
-- > let req0 = requestNoBody methodGet "/" []
-- > client0 = sendRequest req0 $ \rsp -> do
-- > print rsp
-- > getResponseBodyChunk rsp >>= C8.putStrLn
-- > req1 = requestNoBody methodGet "/foo" []
-- > client1 = sendRequest req1 $ \rsp -> do
-- > print rsp
-- > getResponseBodyChunk rsp >>= C8.putStrLn
-- > ex <- E.try $ concurrently_ client0 client1
-- > case ex of
-- > Left e -> print (e :: HTTP2Error)
-- > Right () -> putStrLn "OK"
module Network.HTTP2.Client (
-- * Runner
run,
-- * Client configuration
Scheme,
Authority,
ClientConfig,
defaultClientConfig,
scheme,
authority,
cacheLimit,
connectionWindowSize,
settings,
-- * HTTP\/2 setting
Settings,
defaultSettings,
headerTableSize,
enablePush,
maxConcurrentStreams,
initialWindowSize,
maxFrameSize,
maxHeaderListSize,
-- * Common configuration
Config (..),
allocSimpleConfig,
freeSimpleConfig,
-- * HTTP\/2 client
Client,
-- * Request
Request,
-- * Creating request
requestNoBody,
requestFile,
requestStreaming,
requestStreamingUnmask,
requestBuilder,
-- ** Trailers maker
TrailersMaker,
NextTrailersMaker (..),
defaultTrailersMaker,
setRequestTrailersMaker,
-- * Response
Response,
-- ** Accessing response
responseStatus,
responseHeaders,
responseBodySize,
getResponseBodyChunk,
getResponseTrailers,
-- * Aux
Aux,
auxPossibleClientStreams,
-- * Types
Method,
Path,
FileSpec (..),
FileOffset,
ByteCount,
-- * Error
HTTP2Error (..),
ReasonPhrase,
ErrorCode (
ErrorCode,
NoError,
ProtocolError,
InternalError,
FlowControlError,
SettingsTimeout,
StreamClosed,
FrameSizeError,
RefusedStream,
Cancel,
CompressionError,
ConnectError,
EnhanceYourCalm,
InadequateSecurity,
HTTP11Required
),
-- * RecvN
defaultReadN,
-- * Position read for files
PositionReadMaker,
PositionRead,
Sentinel (..),
defaultPositionReadMaker,
) where
import Data.ByteString (ByteString)
import Data.ByteString.Builder (Builder)
import Data.IORef (readIORef)
import Network.HTTP.Types
import Network.HPACK
import Network.HTTP2.Client.Run
import Network.HTTP2.Client.Types
import Network.HTTP2.Frame
import Network.HTTP2.H2 hiding (authority, scheme)
----------------------------------------------------------------
-- | Creating request without body.
requestNoBody :: Method -> Path -> RequestHeaders -> Request
requestNoBody m p hdr = Request $ OutObj hdr' OutBodyNone defaultTrailersMaker
where
hdr' = addHeaders m p hdr
-- | Creating request with file.
requestFile :: Method -> Path -> RequestHeaders -> FileSpec -> Request
requestFile m p hdr fileSpec = Request $ OutObj hdr' (OutBodyFile fileSpec) defaultTrailersMaker
where
hdr' = addHeaders m p hdr
-- | Creating request with builder.
requestBuilder :: Method -> Path -> RequestHeaders -> Builder -> Request
requestBuilder m p hdr builder = Request $ OutObj hdr' (OutBodyBuilder builder) defaultTrailersMaker
where
hdr' = addHeaders m p hdr
-- | Creating request with streaming.
requestStreaming
:: Method
-> Path
-> RequestHeaders
-> ((Builder -> IO ()) -> IO () -> IO ())
-> Request
requestStreaming m p hdr strmbdy = Request $ OutObj hdr' (OutBodyStreaming strmbdy) defaultTrailersMaker
where
hdr' = addHeaders m p hdr
-- | Like 'requestStreaming', but run the action with exceptions masked
requestStreamingUnmask
:: Method
-> Path
-> RequestHeaders
-> ((forall x. IO x -> IO x) -> (Builder -> IO ()) -> IO () -> IO ())
-> Request
requestStreamingUnmask m p hdr strmbdy = Request $ OutObj hdr' (OutBodyStreamingUnmask strmbdy) defaultTrailersMaker
where
hdr' = addHeaders m p hdr
addHeaders :: Method -> Path -> RequestHeaders -> RequestHeaders
addHeaders m p hdr = (":method", m) : (":path", p) : hdr
-- | Setting 'TrailersMaker' to 'Response'.
setRequestTrailersMaker :: Request -> TrailersMaker -> Request
setRequestTrailersMaker (Request req) tm = Request req{outObjTrailers = tm}
----------------------------------------------------------------
-- | Getting the status of a response.
responseStatus :: Response -> Maybe Status
responseStatus (Response rsp) = getStatus $ inpObjHeaders rsp
-- | Getting the headers from a response.
responseHeaders :: Response -> HeaderTable
responseHeaders (Response rsp) = inpObjHeaders rsp
-- | Getting the body size from a response.
responseBodySize :: Response -> Maybe Int
responseBodySize (Response rsp) = inpObjBodySize rsp
-- | Reading a chunk of the response body.
-- An empty 'ByteString' returned when finished.
getResponseBodyChunk :: Response -> IO ByteString
getResponseBodyChunk (Response rsp) = inpObjBody rsp
-- | Reading response trailers.
-- This function must be called after 'getResponseBodyChunk'
-- returns an empty.
getResponseTrailers :: Response -> IO (Maybe HeaderTable)
getResponseTrailers (Response rsp) = readIORef (inpObjTrailers rsp)
|