module Network.WebSockets.Extensions.StrictUnicode
( strictUnicode
) where
import Control.Exception (throwIO)
import qualified Data.ByteString.Lazy as BL
import Network.WebSockets.Extensions
import Network.WebSockets.Types
strictUnicode :: Extension
strictUnicode = Extension
{ extHeaders = []
, extParse = \parseRaw -> return (parseRaw >>= strictParse)
, extWrite = return
}
strictParse :: Maybe Message -> IO (Maybe Message)
strictParse Nothing = return Nothing
strictParse (Just (DataMessage rsv1 rsv2 rsv3 (Text bl _))) =
case decodeUtf8Strict bl of
Left err -> throwIO err
Right txt ->
return (Just (DataMessage rsv1 rsv2 rsv3 (Text bl (Just txt))))
strictParse (Just msg@(ControlMessage (Close _ bl))) =
case decodeUtf8Strict (BL.drop 2 bl) of
Left err -> throwIO err
Right _ -> return (Just msg)
strictParse (Just msg) = return (Just msg)