Hmm, after posting this, I realized that my solution might be 
over-engineered! We can just use "take" function of lazy bytestring since 
we don't really use any thing in Streaming. 

On Friday, June 10, 2016 at 11:14:10 AM UTC-4, Sal wrote:
>
> This question is regarding Streaming library. What is an efficient way to 
> take n bytes from lazy bytestring? 
>
> What I have is an Aeson decoder that decodes lazy bytestring from network 
> bytes. Since it will read the whole thing into memory which is fine btw, 
> one might want to place a limit on max bytes consumed (to prevent malicious 
> OOM attacks involving large payloads). Here is my attempt at limiting the 
> number of bytes - in the example below, we limit the max bytes to 4:
>
> Prelude> import Data.ByteString.Streaming as SBS
> Prelude SBS> import Streaming as S -- to import Of constructor 
> Prelude SBS S> :set -XBangPatterns 
> Prelude SBS S> :set -XOverloadedStrings 
> Prelude SBS S>  (\(!x :> y) -> return x) =<< (SBS.toLazy . (SBS.take 4) . 
> SBS.fromLazy $ "hello") 
> "hell"
>
>
>
> Just want to check if above code can be improved in any way.
>
>
> Currently, I have Aeson `decode` function consuming from websocket network 
> bytes without any limit on message size. With a "limitBytes" function like 
> above, I can do something like below:
>
>
> -- before: decode x
> -- After the changes - if the bytes exceed the limit, rest of the bytes 
> are dropped, aeson decode will fail and return Nothing
> return . decode =<< limitBytes $ x
>
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Haskell Pipes" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].

Reply via email to