From: Ramakrishnan Muthukrishnan Date: Mon, 16 Feb 2015 04:43:22 +0000 (+0530) Subject: refactoring X-Git-Url: https://git.rkrishnan.org/pf/components/com_hotproperty/nxhtml.html?a=commitdiff_plain;h=8e00704f8c2cc64f28cf8853d81026a6444d4342;p=functorrent.git refactoring --- diff --git a/src/Main.hs b/src/Main.hs index f08c295..f5a0a50 100644 --- a/src/Main.hs +++ b/src/Main.hs @@ -5,11 +5,11 @@ import qualified Data.ByteString.Char8 as BC import qualified Bencode as Benc import qualified Metainfo as MInfo import qualified Tracker as T +import qualified Text.ParserCombinators.Parsec as Parsec +import Data.Functor -import Text.ParserCombinators.Parsec - -printError :: ParseError -> IO () -printError e = putStrLn "parse error" +printError :: Parsec.ParseError -> IO () +printError e = putStrLn $ "parse error: " ++ show e genPeerId :: String genPeerId = "-HS0001-20150215" @@ -22,10 +22,7 @@ main = do Right d -> case (MInfo.mkMetaInfo d) of Nothing -> putStrLn "parse error" Just m -> do - let (Benc.Bdict d') = d --- putStrLn (show m) --- putStrLn (T.urlEncode (T.infoHash d')) - do body <- T.connect (MInfo.announce m) (T.prepareRequest d genPeerId) - putStrLn body + body <- (Benc.decode . BC.pack) <$> T.connect (MInfo.announce m) (T.prepareRequest d genPeerId) + putStrLn (show body) Left e -> printError e putStrLn "done" diff --git a/src/Tracker.hs b/src/Tracker.hs index 7d5525f..70e1563 100644 --- a/src/Tracker.hs +++ b/src/Tracker.hs @@ -5,24 +5,21 @@ import qualified Data.Map as M import qualified Data.List as List import qualified Network.HTTP as HTTP import qualified Bencode as Benc -import qualified Crypto.Hash as H import qualified Crypto.Hash.SHA1 as SHA1 import qualified Data.ByteString.Base16 as B16 +import qualified Utils as U import Data.Char -- import Network.HTTP type Url = String -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 = concatMap (encode . BC.unpack) (splitN 2 bs) +urlEncode bs = concatMap (encode . BC.unpack) (U.splitN 2 bs) where encode b@(c1 : c2 : []) = let c = chr (read ("0x" ++ b)) in escape c c1 c2 diff --git a/src/Utils.hs b/src/Utils.hs new file mode 100644 index 0000000..4a2fc6a --- /dev/null +++ b/src/Utils.hs @@ -0,0 +1,7 @@ +module Utils where + +import qualified Data.ByteString.Char8 as BC + +splitN :: Int -> BC.ByteString -> [BC.ByteString] +splitN n bs | BC.null bs = [] + | otherwise = (BC.take n bs) : splitN n (BC.drop n bs)