implemented escapeString

This commit is contained in:
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 (
encodeRequest
encodeRequest,
escapeString
) where
import Data.Char (ord)
import Data.List (intercalate)
import Network.GemServ.Types
@@ -45,4 +47,21 @@ encodeRequest req =
"" -> ""
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