| 1 |
-------------------------------------------------------------------------------- |
| 2 |
module Network.WebSockets.Extensions.StrictUnicode |
| 3 |
( strictUnicode |
| 4 |
) where |
| 5 |
|
| 6 |
|
| 7 |
-------------------------------------------------------------------------------- |
| 8 |
import Control.Exception (throwIO) |
| 9 |
import qualified Data.ByteString.Lazy as BL |
| 10 |
import Network.WebSockets.Extensions |
| 11 |
import Network.WebSockets.Types |
| 12 |
|
| 13 |
|
| 14 |
-------------------------------------------------------------------------------- |
| 15 |
strictUnicode :: Extension |
| 16 |
strictUnicode = Extension |
| 17 |
{ extHeaders = [] |
| 18 |
, extParse = \parseRaw -> return (parseRaw >>= strictParse) |
| 19 |
, extWrite = return |
| 20 |
} |
| 21 |
|
| 22 |
|
| 23 |
-------------------------------------------------------------------------------- |
| 24 |
strictParse :: Maybe Message -> IO (Maybe Message) |
| 25 |
strictParse Nothing = return Nothing |
| 26 |
strictParse (Just (DataMessage rsv1 rsv2 rsv3 (Text bl _))) = |
| 27 |
case decodeUtf8Strict bl of |
| 28 |
Left err -> throwIO err |
| 29 |
Right txt -> |
| 30 |
return (Just (DataMessage rsv1 rsv2 rsv3 (Text bl (Just txt)))) |
| 31 |
strictParse (Just msg@(ControlMessage (Close _ bl))) = |
| 32 |
-- If there is a body, the first two bytes of the body MUST be a 2-byte |
| 33 |
-- unsigned integer (in network byte order) representing a status code with |
| 34 |
-- value /code/ defined in Section 7.4. Following the 2-byte integer, the |
| 35 |
-- body MAY contain UTF-8-encoded data with value /reason/, the |
| 36 |
-- interpretation of which is not defined by this specification. |
| 37 |
case decodeUtf8Strict (BL.drop 2 bl) of |
| 38 |
Left err -> throwIO err |
| 39 |
Right _ -> return (Just msg) |
| 40 |
strictParse (Just msg) = return (Just msg) |