use netstrings for framing binary data with json at the end
authorJoey Hess <joeyh@joeyh.name>
Fri, 5 Jul 2024 15:53:03 +0000 (11:53 -0400)
committerJoey Hess <joeyh@joeyh.name>
Fri, 5 Jul 2024 15:53:03 +0000 (11:53 -0400)
commit5e564947d7ce6b1dc18022b86cda55ff894e7070
treedc1c8c3040261c5eb82dd86d2a3fb950b3ded34d
parent95ba4d4480d975c8c0df325ae0bfb22a25b60788
use netstrings for framing binary data with json at the end

This will be easy to implement with servant. It's also very efficient,
and fairly future-proof. Eg, could add another frame with other data.

This does make it a bit harder to use this protocol, but netstrings
probably take about 5 minutes to implement? Let's see...

import Text.Read
import Data.List

toNetString :: String -> String
toNetString s = show (length s) ++ ":" ++ s ++ ","

nextNetString :: String -> Maybe (String, String)
nextNetString s = case break (== ':') s of
        ([], _) -> Nothing
        (sn, rest) -> do
                n <- readMaybe sn
                let (v, rest') = splitAt n (drop 1 rest)
                return (v, drop 1 rest')

Ok, well, that took about 10 minutes ;-)
doc/design/p2p_protocol_over_http/draft1.mdwn