on master help sign in
Add `NoFraming` strategy
commitb80a3e62797e5d9d5d1c574f9013c87dfcb5b2c4 authorJan van Brügge <supermanitu@gmail.com> dateWed 23 May 2018, 11:28 (8 years ago) parent0ba09c9
3 files changed+17−4
servant-client/test/Servant/StreamSpec.hs+3−2 servant/src/Servant/API.hs+2−2 servant/src/Servant/API/Stream.hs+12
servant-client/test/Servant/StreamSpec.hs+3−2
@@ -41,7 +41,8 @@ import Test.QuickCheck
4141import Servant.API ((:<|>) ((:<|>)), (:>), JSON,
4242 NetstringFraming, NewlineFraming,
4343 OctetStream, ResultStream (..),
44− StreamGenerator (..), StreamGet)
44+ StreamGenerator (..), StreamGet,
45+ NoFraming)
4546import Servant.Client
4647import Servant.ClientSpec (Person (..))
4748import qualified Servant.ClientSpec as CS
@@ -55,7 +56,7 @@ spec = describe "Servant.Stream" $ do
5556type StreamApi f =
5657 "streamGetNewline" :> StreamGet NewlineFraming JSON (f Person)
5758 :<|> "streamGetNetstring" :> StreamGet NetstringFraming JSON (f Person)
58− :<|> "streamALot" :> StreamGet NewlineFraming OctetStream (f BS.ByteString)
59+ :<|> "streamALot" :> StreamGet NoFraming OctetStream (f BS.ByteString)
5960
6061
6162capi :: Proxy (StreamApi ResultStream)
servant/src/Servant/API.hs+2−2
@@ -117,8 +117,8 @@ import Servant.API.Stream
117117 (BoundaryStrategy (..), BuildFromStream (..),
118118 ByteStringParser (..), FramingRender (..),
119119 FramingUnrender (..), NetstringFraming, NewlineFraming,
120− ResultStream (..), Stream, StreamGenerator (..), StreamGet,
121− StreamPost, ToStreamGenerator (..))
120+ NoFraming, ResultStream (..), Stream, StreamGenerator (..),
121+ StreamGet, StreamPost, ToStreamGenerator (..))
122122import Servant.API.Sub
123123 ((:>))
124124import Servant.API.Vault
servant/src/Servant/API/Stream.hs+12
@@ -81,6 +81,18 @@ data ByteStringParser a = ByteStringParser {
8181class FramingUnrender strategy a where
8282 unrenderFrames :: Proxy strategy -> Proxy a -> ByteStringParser (ByteStringParser (Either String ByteString))
8383
84+-- | A framing strategy that does not do any framing at all, it just passes the input data
85+-- This will be used most of the time with binary data, such as files
86+data NoFraming
87+
88+instance FramingRender NoFraming a where
89+ header _ _ = empty
90+ boundary _ _ = BoundaryStrategyGeneral id
91+ trailer _ _ = empty
92+
93+instance FramingUnrender NoFraming a where
94+ unrenderFrames _ _ = ByteStringParser (Just . (go,)) (go,)
95+ where go = ByteStringParser (Just . (, empty) . Right) ((, empty) . Right)
8496
8597-- | A simple framing strategy that has no header or termination, and inserts a newline character between each frame.
8698-- This assumes that it is used with a Content-Type that encodes without newlines (e.g. JSON).