Naive implementation for readN.
NOTE: This function is intended to be used by a single thread only. (It is probably quite rare anyway to want concurrent reads from the same network socket.)
:: a typeCtrl KGHC 9.14.1 · lts/ghc-9.14.x · 3e2d568 · 2026-10-11
Modulehttp-semantics-0.4.1Haskell2010
Naive implementation for readN.
NOTE: This function is intended to be used by a single thread only. (It is probably quite rare anyway to want concurrent reads from the same network socket.)
Reading n bytes.
Position read based on Handle.
Position read for files.
Making a position read and its closer.
Action to run prior to terminating the stream
Write part of a streaming response to the write buffer
In http2 this will be used to construct a single HTTP2 DATA frame
(see discussion of the maximum number of bytes, below).
NotEndOfStreamThe stream is not yet terminated
EndOfStream (Maybe CleanupStream)The stream is terminated
In addition to indicating that the stream is terminated, we can also
specify an optional Cleanup handler to be run.
StreamingFinished (Maybe CleanupStream)Indicate that the stream is finished
StreamingCancelled (Maybe SomeException)Indicate that the stream is cancelled
StreamingFlushFlush the stream
This will cause the write buffer to be written to the network socket, without waiting for more data.
StreamingBuilder Builder IsEndOfStreamConstruct a DATA frame, optionally terminating the stream
TrailersMake to create no trailers.
Running trailers-maker.
bufferIO buf siz $ \bs -> tlrmkr (Just bs)Either the next trailers maker or final trailers.
Trailers maker. A chunks of the response body is passed with Just. The maker should update internal state with the ByteString and return the next trailers maker. When response body reaches its end, Nothing is passed and the maker should generate trailers. An example:
{-# LANGUAGE BangPatterns #-}
import Data.ByteString (ByteString)
import qualified Data.ByteString.Char8 as C8
import Crypto.Hash (Context, SHA1) -- cryptonite
import qualified Crypto.Hash as CH
-- Strictness is important for Context.
trailersMaker :: Context SHA1 -> Maybe ByteString -> IO NextTrailersMaker
trailersMaker ctx Nothing = return $ Trailers [("X-SHA1", sha1)]
where
!sha1 = C8.pack $ show $ CH.hashFinalize ctx
trailersMaker ctx (Just bs) = return $ NextTrailersMaker $ trailersMaker ctx'
where
!ctx' = CH.hashUpdate ctx bsUsage example:
let h2rsp = responseFile ...
maker = trailersMaker (CH.hashInit :: Context SHA1)
h2rsp' = setResponseTrailersMaker h2rsp maker