From b80a3e62797e5d9d5d1c574f9013c87dfcb5b2c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20van=20Br=C3=BCgge?= Date: Wed, 23 May 2018 11:28:12 +0200 Subject: [PATCH] Add `NoFraming` strategy --- servant-client/test/Servant/StreamSpec.hs | 5 +++-- servant/src/Servant/API.hs | 4 ++-- servant/src/Servant/API/Stream.hs | 12 ++++++++++++ 3 files changed, 17 insertions(+), 4 deletions(-) diff --git a/servant-client/test/Servant/StreamSpec.hs b/servant-client/test/Servant/StreamSpec.hs index ad4a2664..66276623 100644 --- a/servant-client/test/Servant/StreamSpec.hs +++ b/servant-client/test/Servant/StreamSpec.hs @@ -41,7 +41,8 @@ import Test.QuickCheck import Servant.API ((:<|>) ((:<|>)), (:>), JSON, NetstringFraming, NewlineFraming, OctetStream, ResultStream (..), - StreamGenerator (..), StreamGet) + StreamGenerator (..), StreamGet, + NoFraming) import Servant.Client import Servant.ClientSpec (Person (..)) import qualified Servant.ClientSpec as CS @@ -55,7 +56,7 @@ spec = describe "Servant.Stream" $ do type StreamApi f = "streamGetNewline" :> StreamGet NewlineFraming JSON (f Person) :<|> "streamGetNetstring" :> StreamGet NetstringFraming JSON (f Person) - :<|> "streamALot" :> StreamGet NewlineFraming OctetStream (f BS.ByteString) + :<|> "streamALot" :> StreamGet NoFraming OctetStream (f BS.ByteString) capi :: Proxy (StreamApi ResultStream) diff --git a/servant/src/Servant/API.hs b/servant/src/Servant/API.hs index d236b0da..4ae2b8ef 100644 --- a/servant/src/Servant/API.hs +++ b/servant/src/Servant/API.hs @@ -117,8 +117,8 @@ import Servant.API.Stream (BoundaryStrategy (..), BuildFromStream (..), ByteStringParser (..), FramingRender (..), FramingUnrender (..), NetstringFraming, NewlineFraming, - ResultStream (..), Stream, StreamGenerator (..), StreamGet, - StreamPost, ToStreamGenerator (..)) + NoFraming, ResultStream (..), Stream, StreamGenerator (..), + StreamGet, StreamPost, ToStreamGenerator (..)) import Servant.API.Sub ((:>)) import Servant.API.Vault diff --git a/servant/src/Servant/API/Stream.hs b/servant/src/Servant/API/Stream.hs index bf12bc83..40dd1402 100644 --- a/servant/src/Servant/API/Stream.hs +++ b/servant/src/Servant/API/Stream.hs @@ -81,6 +81,18 @@ data ByteStringParser a = ByteStringParser { class FramingUnrender strategy a where unrenderFrames :: Proxy strategy -> Proxy a -> ByteStringParser (ByteStringParser (Either String ByteString)) +-- | A framing strategy that does not do any framing at all, it just passes the input data +-- This will be used most of the time with binary data, such as files +data NoFraming + +instance FramingRender NoFraming a where + header _ _ = empty + boundary _ _ = BoundaryStrategyGeneral id + trailer _ _ = empty + +instance FramingUnrender NoFraming a where + unrenderFrames _ _ = ByteStringParser (Just . (go,)) (go,) + where go = ByteStringParser (Just . (, empty) . Right) ((, empty) . Right) -- | A simple framing strategy that has no header or termination, and inserts a newline character between each frame. -- This assumes that it is used with a Content-Type that encodes without newlines (e.g. JSON).