From 215e478f5fbf90c528191beb211df3860f8d7ac4 Mon Sep 17 00:00:00 2001 From: Sam Truzjan Date: Sun, 2 Mar 2014 16:26:15 +0400 Subject: Fix scrapeURL: do not throw exception --- src/Network/BitTorrent/Tracker/RPC/HTTP.hs | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) (limited to 'src/Network/BitTorrent/Tracker') diff --git a/src/Network/BitTorrent/Tracker/RPC/HTTP.hs b/src/Network/BitTorrent/Tracker/RPC/HTTP.hs index 33ceb077..81199921 100644 --- a/src/Network/BitTorrent/Tracker/RPC/HTTP.hs +++ b/src/Network/BitTorrent/Tracker/RPC/HTTP.hs @@ -28,6 +28,7 @@ module Network.BitTorrent.Tracker.RPC.HTTP import Control.Applicative import Control.Exception +import Control.Monad import Control.Monad.Trans.Resource import Data.BEncode as BE import Data.ByteString as BS @@ -159,12 +160,12 @@ scrapeURL uri = do newPath <- replace (BC.pack (uriPath uri)) return uri { uriPath = BC.unpack newPath } where - replace p - | ps <- BC.splitWith (== '/') p - , "announce" `BS.isPrefixOf` L.last ps - = let newSuff = "scrape" <> BS.drop (BS.length "announce") (L.last ps) - in Just (BS.intercalate "/" (L.init ps ++ [newSuff])) - | otherwise = Nothing + replace p = do + let ps = BC.splitWith (== '/') p + guard (not (L.null ps)) + guard ("announce" `BS.isPrefixOf` L.last ps) + let newSuff = "scrape" <> BS.drop (BS.length "announce") (L.last ps) + return (BS.intercalate "/" (L.init ps ++ [newSuff])) -- | For each 'InfoHash' of torrents request scrape info from the tracker. -- However if the info hash list is 'null', the tracker should list -- cgit v1.2.3