From: Ramakrishnan Muthukrishnan Date: Sun, 15 Feb 2015 08:13:02 +0000 (+0530) Subject: urlencoding: ByteString -> String X-Git-Url: https://git.rkrishnan.org/pf/content/en/seg/priv/running.html?a=commitdiff_plain;h=f53654db41f602ae76c0a9f9d7657c95807dc306;p=functorrent.git urlencoding: ByteString -> String --- diff --git a/src/Tracker.hs b/src/Tracker.hs index 57b813d..75c3a00 100644 --- a/src/Tracker.hs +++ b/src/Tracker.hs @@ -12,13 +12,27 @@ splitN :: Int -> BC.ByteString -> [BC.ByteString] splitN n bs | BC.null bs = [] | otherwise = (BC.take n bs) : splitN n (BC.drop n bs) +-- | urlEncode +-- +-- >>> urlEncode $ BC.pack "123456789abcdef123456789abcdef123456789a" +-- "%124Vx%9a%bc%de%f1%23Eg%89%ab%cd%ef%124Vx%9a" urlEncode :: BC.ByteString -> String -urlEncode bs = let bss = splitN 2 bs - chars = map (chr . read . ("0x" ++) . BC.unpack) bss - in - HB.urlEncode chars - +urlEncode bs = concatMap (encode . BC.unpack) (splitN 2 bs) + where encode b@(c1 : c2 : []) = let c = chr (read ("0x" ++ b)) + in + escape c c1 c2 + encode _ = "" + escape i c1 c2 | isAsciiUpper i || + isAsciiLower i || + isDigit i || + i == '-' || + i == '_' || + i == '.' || + i == '~' + = [i] + | otherwise = "%" ++ [c1] ++ [c2] +-- (chr . read . ("0x" ++) . BC.unpack) -- connect :: Url -> String -> IO (Benc.BVal) -- connect url infoHash = case (parseUrl url) of -- Nothing -> putStrLn "invalid tracker URL"