Skip to content

Commit

Permalink
Add metadata to HeadersEndOfInput
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewthad committed Sep 11, 2023
1 parent 85c4208 commit 318b340
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 5 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Revision history for http-exchange

## 0.2.0.0 -- ????-??-??

* Add incomplete response to headers-end-of-input exception.

## 0.1.0.0 -- YYYY-mm-dd

* First version. Released on an unsuspecting world.
8 changes: 5 additions & 3 deletions http-exchange.cabal
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
cabal-version: 3.0
name: http-exchange
version: 0.1.1.0
version: 0.2.0.0
synopsis: Perform HTTP Requests
description: Perform HTTP requests. This uses backpack and is agnostic to the backend.
license: BSD-3-Clause
Expand All @@ -15,7 +15,9 @@ extra-doc-files: CHANGELOG.md
library types
ghc-options: -Wall
exposed-modules: Http.Exchange.Types
build-depends: base >=4.16.3.0 && <5
build-depends:
, base >=4.16.3.0 && <5
, byteslice >=0.2.11.1
hs-source-dirs: src-types
default-language: GHC2021

Expand Down Expand Up @@ -45,7 +47,7 @@ library
default-language: GHC2021

test-suite test
ghc-options: -Wall -O1
ghc-options: -Wall
default-language: GHC2021
type: exitcode-stdio-1.0
hs-source-dirs: test
Expand Down
23 changes: 22 additions & 1 deletion src-types/Http/Exchange/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ module Http.Exchange.Types
( HttpException(..)
) where

import Data.Bytes (Bytes)
import qualified Control.Exception as E

-- | Exceptions that occur when decoding an HTTP response.
Expand All @@ -23,6 +24,8 @@ data HttpException
| ExpectedCrlfBeforeChunkLength
| HeadersMalformed
| HeadersEndOfInput
{-# UNPACK #-} !Bytes
-- ^ The entire contents of the response.
| HeadersTooLarge
| ImplementationMistake
-- ^ If this one happens, there is a mistake in this
Expand All @@ -31,5 +34,23 @@ data HttpException
| PipelinedResponses
| TransferEncodingUnrecognized
| TransferEncodingDuplicated
deriving stock (Show)
deriving anyclass (E.Exception)

instance Show HttpException where
showsPrec _ ChunkTooLarge = showString "ChunkTooLarge"
showsPrec _ ChunkedBodyEndOfInput = showString "ChunkedBodyEndOfInput"
showsPrec _ NonchunkedBodyEndOfInput = showString "NonchunkedBodyEndOfInput"
showsPrec _ ContentLengthDuplicated = showString "ContentLengthDuplicated"
showsPrec _ ContentLengthMalformed = showString "ContentLengthMalformed"
showsPrec _ ContentLengthTooLarge = showString "ContentLengthTooLarge"
showsPrec _ ExpectedCrlfAfterChunk = showString "ExpectedCrlfAfterChunk"
showsPrec _ ExpectedCrlfAfterChunkLength = showString "ExpectedCrlfAfterChunkLength"
showsPrec _ ExpectedCrlfBeforeChunkLength = showString "ExpectedCrlfBeforeChunkLength"
showsPrec _ HeadersMalformed = showString "HeadersMalformed"
showsPrec _ HeadersEndOfInput{} = showString "HeadersEndOfInput{..}"
showsPrec _ HeadersTooLarge = showString "HeadersTooLarge"
showsPrec _ ImplementationMistake = showString "ImplementationMistake"
showsPrec _ NonNumericChunkLength = showString "NonNumericChunkLength"
showsPrec _ PipelinedResponses = showString "PipelinedResponses"
showsPrec _ TransferEncodingUnrecognized = showString "TransferEncodingUnrecognized"
showsPrec _ TransferEncodingDuplicated = showString "TransferEncodingDuplicated"
2 changes: 1 addition & 1 deletion src/Exchange.hs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ receiveResponse !ctx = do
let go !oldOutput = receive ctx >>= \case
Left err -> pure (Left (Receive err))
Right newOutput -> case Bytes.length newOutput of
0 -> pure (Left (Http E.HeadersEndOfInput))
0 -> pure (Left (Http (E.HeadersEndOfInput oldOutput)))
_ -> do
let output = oldOutput <> newOutput
case splitEndOfHeaders output of
Expand Down

0 comments on commit 318b340

Please sign in to comment.