Skip to content

Commit

Permalink
unsafeReceiveResponse that does not consume body
Browse files Browse the repository at this point in the history
This works when you know there is no body, no matter what the headers
say (HEAD requests, aesiniath#114)

This also works when the handler consumes whatever part of the body you
actually want to use, and then you close the connection afterwards (no
reusing for pipelining!)  Sometimes this is a lot faster than using
pipelining if you can, for example, skip a many GB download (aesiniath#113)
  • Loading branch information
singpolyma committed Aug 23, 2018
1 parent ec9b58b commit 5d48210
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/Network/Http/Client.hs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ module Network.Http.Client (
-- * Processing HTTP response
receiveResponse,
receiveResponseRaw,
unsafeReceiveResponse,
UnexpectedCompression,
StatusCode,
getStatusCode,
Expand Down
16 changes: 16 additions & 0 deletions lib/Network/Http/Connection.hs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ module Network.Http.Connection (
sendRequest,
receiveResponse,
receiveResponseRaw,
unsafeReceiveResponse,
UnexpectedCompression,
emptyBody,
fileBody,
Expand Down Expand Up @@ -490,6 +491,21 @@ receiveResponseRaw c handler = do
where
i = cIn c

--
-- | Handle the response coming back from the server. This function
-- is the same as receiveResponse, but it does not consume the body for
-- you after the handler is done. This means that it can only be safely used
-- if the handler will fully consume the body, there is no body, or when
-- the connection is not being reused (no pipelining).
--
unsafeReceiveResponse :: Connection -> (Response -> InputStream ByteString -> IO β) -> IO β
unsafeReceiveResponse c handler = do
p <- readResponseHeader i
i' <- readResponseBody p i

handler p i'
where
i = cIn c

--
-- | Use this for the common case of the HTTP methods that only send
Expand Down

0 comments on commit 5d48210

Please sign in to comment.