implemented escapeString

This commit is contained in:
Jonathan Lamothe 2021-10-22 16:40:44 -04:00
parent d56116d4c9
commit 8590219126
2 changed files with 34 additions and 2 deletions

View File

@ -25,9 +25,11 @@ License along with this program. If not, see
-} -}
module Network.GemServ ( module Network.GemServ (
encodeRequest encodeRequest,
escapeString
) where ) where
import Data.Char (ord)
import Data.List (intercalate) import Data.List (intercalate)
import Network.GemServ.Types import Network.GemServ.Types
@ -45,4 +47,21 @@ encodeRequest req =
"" -> "" "" -> ""
q -> '?' : q q -> '?' : q
-- | add required escape sequences to a string
escapeString :: String -> String
escapeString = concatMap $ \ch ->
if ch `elem` unescaped
then [ch]
else '%' : toHex ch
where
unescaped = ['0'..'9'] ++ ['A'..'Z'] ++ ['a'..'z'] ++ "~-_."
toHex ch = let
n = ord ch
high = n `div` 16
low = n `mod` 16
in [hexDigits !! high, hexDigits !! low]
hexDigits :: String
hexDigits = ['0'..'9'] ++ ['a'..'f']
--jl --jl

View File

@ -28,8 +28,9 @@ import Network.GemServ
import Network.GemServ.Types import Network.GemServ.Types
spec :: Spec spec :: Spec
spec = describe "Network.GemServ" spec = describe "Network.GemServ" $ do
encodeRequestSpec encodeRequestSpec
escapeStringSpec
encodeRequestSpec :: Spec encodeRequestSpec :: Spec
encodeRequestSpec = describe "encodeRequest" $ mapM_ encodeRequestSpec = describe "encodeRequest" $ mapM_
@ -55,4 +56,16 @@ encodeRequestSpec = describe "encodeRequest" $ mapM_
withQueryReq = simpleReq { reqQuery = "foo" } withQueryReq = simpleReq { reqQuery = "foo" }
withQueryExp = "gemini://example.com/?foo" withQueryExp = "gemini://example.com/?foo"
escapeStringSpec :: Spec
escapeStringSpec = describe "escapeString" $ mapM_
( \(input, expected) -> context (show input) $
it ("should be " ++ show expected) $
escapeString input `shouldBe` expected
)
-- input, expected
[ ( "~foo-bar_baz.quux", "~foo-bar_baz.quux" )
, ( "foo:/?=&#%", "foo%3a%2f%3f%3d%26%23%25" )
]
--jl --jl