diff options
-rw-r--r-- | src/Data/Torrent.hs | 2 | ||||
-rw-r--r-- | src/Data/Torrent/InfoHash.hs | 15 | ||||
-rw-r--r-- | src/Data/Torrent/Layout.hs | 4 | ||||
-rw-r--r-- | tests/Data/Torrent/MagnetSpec.hs | 5 |
4 files changed, 11 insertions, 15 deletions
diff --git a/src/Data/Torrent.hs b/src/Data/Torrent.hs index 59f11814..8411dc97 100644 --- a/src/Data/Torrent.hs +++ b/src/Data/Torrent.hs | |||
@@ -142,7 +142,7 @@ infoDictionary :: LayoutInfo -> PieceInfo -> Bool -> InfoDict | |||
142 | infoDictionary li pinfo private = InfoDict ih li pinfo private | 142 | infoDictionary li pinfo private = InfoDict ih li pinfo private |
143 | where | 143 | where |
144 | ih = IH.hashlazy $ encode $ InfoDict fake_ih li pinfo private | 144 | ih = IH.hashlazy $ encode $ InfoDict fake_ih li pinfo private |
145 | fake_ih = InfoHash "" | 145 | fake_ih = "0123456789012345678901234567890123456789" |
146 | 146 | ||
147 | getPrivate :: Get Bool | 147 | getPrivate :: Get Bool |
148 | getPrivate = (Just True ==) <$>? "private" | 148 | getPrivate = (Just True ==) <$>? "private" |
diff --git a/src/Data/Torrent/InfoHash.hs b/src/Data/Torrent/InfoHash.hs index a4d6a02e..fd57700a 100644 --- a/src/Data/Torrent/InfoHash.hs +++ b/src/Data/Torrent/InfoHash.hs | |||
@@ -10,8 +10,7 @@ | |||
10 | {-# LANGUAGE FlexibleInstances #-} | 10 | {-# LANGUAGE FlexibleInstances #-} |
11 | {-# LANGUAGE GeneralizedNewtypeDeriving #-} | 11 | {-# LANGUAGE GeneralizedNewtypeDeriving #-} |
12 | module Data.Torrent.InfoHash | 12 | module Data.Torrent.InfoHash |
13 | ( -- * Info hash | 13 | ( InfoHash |
14 | InfoHash(..) | ||
15 | 14 | ||
16 | -- * Parsing | 15 | -- * Parsing |
17 | , byteStringToInfoHash | 16 | , byteStringToInfoHash |
@@ -42,6 +41,7 @@ import qualified Data.ByteString.Lazy.Builder as B | |||
42 | import qualified Data.ByteString.Lazy.Builder.ASCII as B | 41 | import qualified Data.ByteString.Lazy.Builder.ASCII as B |
43 | import Data.Char | 42 | import Data.Char |
44 | import Data.List as L | 43 | import Data.List as L |
44 | import Data.Maybe | ||
45 | import Data.Hashable as Hashable | 45 | import Data.Hashable as Hashable |
46 | import Data.URLEncoded as URL | 46 | import Data.URLEncoded as URL |
47 | import Data.Serialize | 47 | import Data.Serialize |
@@ -85,14 +85,11 @@ instance Read InfoHash where | |||
85 | pair (a : b : xs) = (a, b) : pair xs | 85 | pair (a : b : xs) = (a, b) : pair xs |
86 | pair _ = [] | 86 | pair _ = [] |
87 | 87 | ||
88 | -- | for base16 (hex) encoded strings | 88 | -- | for base16/base32 encoded strings |
89 | instance IsString InfoHash where | 89 | instance IsString InfoHash where |
90 | fromString str | 90 | fromString str = fromMaybe err $ textToInfoHash $ T.pack str |
91 | | L.length str == 40 | 91 | where |
92 | , (ihStr, inv) <- Base16.decode $ BC.pack str | 92 | err = error $ "fromString: invalid infohash string" ++ str |
93 | = if BS.length inv == 0 then InfoHash ihStr | ||
94 | else error "fromString: invalid infohash string" | ||
95 | | otherwise = error "fromString: invalid infohash string length" | ||
96 | 93 | ||
97 | instance Hashable InfoHash where | 94 | instance Hashable InfoHash where |
98 | hash = Hashable.hash . getInfoHash | 95 | hash = Hashable.hash . getInfoHash |
diff --git a/src/Data/Torrent/Layout.hs b/src/Data/Torrent/Layout.hs index 70908c4e..8f35772d 100644 --- a/src/Data/Torrent/Layout.hs +++ b/src/Data/Torrent/Layout.hs | |||
@@ -68,6 +68,7 @@ import Data.Aeson.Types (FromJSON, ToJSON) | |||
68 | import Data.BEncode | 68 | import Data.BEncode |
69 | import Data.BEncode.Types | 69 | import Data.BEncode.Types |
70 | import Data.ByteString as BS | 70 | import Data.ByteString as BS |
71 | import Data.ByteString.Base16 as Base16 | ||
71 | import Data.ByteString.Char8 as BC | 72 | import Data.ByteString.Char8 as BC |
72 | import Data.Char as Char | 73 | import Data.Char as Char |
73 | import Data.Foldable as F | 74 | import Data.Foldable as F |
@@ -81,7 +82,6 @@ import System.FilePath | |||
81 | import System.Posix.Types | 82 | import System.Posix.Types |
82 | 83 | ||
83 | import Data.Torrent.Block | 84 | import Data.Torrent.Block |
84 | import Data.Torrent.InfoHash | ||
85 | 85 | ||
86 | 86 | ||
87 | {----------------------------------------------------------------------- | 87 | {----------------------------------------------------------------------- |
@@ -180,7 +180,7 @@ instance Pretty (FileInfo BS.ByteString) where | |||
180 | $$ "Size: " <> text (show fiLength) | 180 | $$ "Size: " <> text (show fiLength) |
181 | $$ maybe PP.empty ppMD5 fiMD5Sum | 181 | $$ maybe PP.empty ppMD5 fiMD5Sum |
182 | where | 182 | where |
183 | ppMD5 md5 = "MD5 : " <> text (show (InfoHash md5)) | 183 | ppMD5 md5 = "MD5 : " <> text (show (Base16.encode md5)) |
184 | 184 | ||
185 | -- | Join file path. | 185 | -- | Join file path. |
186 | joinFilePath :: FileInfo [ByteString] -> FileInfo ByteString | 186 | joinFilePath :: FileInfo [ByteString] -> FileInfo ByteString |
diff --git a/tests/Data/Torrent/MagnetSpec.hs b/tests/Data/Torrent/MagnetSpec.hs index 3ecf8705..048a1870 100644 --- a/tests/Data/Torrent/MagnetSpec.hs +++ b/tests/Data/Torrent/MagnetSpec.hs | |||
@@ -9,7 +9,6 @@ import Test.QuickCheck | |||
9 | import Test.QuickCheck.Instances () | 9 | import Test.QuickCheck.Instances () |
10 | import Network.URI | 10 | import Network.URI |
11 | 11 | ||
12 | import Data.Torrent.InfoHash | ||
13 | import Data.Torrent.Magnet | 12 | import Data.Torrent.Magnet |
14 | import Data.Torrent.InfoHashSpec () | 13 | import Data.Torrent.InfoHashSpec () |
15 | 14 | ||
@@ -36,10 +35,10 @@ spec = do | |||
36 | 35 | ||
37 | it "parse base32" $ do | 36 | it "parse base32" $ do |
38 | let magnet = "magnet:?xt=urn:btih:CT76LXJDDCH5LS2TUHKH6EUJ3NYKX4Y6" | 37 | let magnet = "magnet:?xt=urn:btih:CT76LXJDDCH5LS2TUHKH6EUJ3NYKX4Y6" |
39 | let ih = InfoHash "\DC4\255\229\221#\CAN\143\213\203S\161\212\DEL\DC2\137\219p\171\243\RS" | 38 | let ih = "CT76LXJDDCH5LS2TUHKH6EUJ3NYKX4Y6" |
40 | parseMagnet magnet `shouldBe` Just (nullMagnet ih) | 39 | parseMagnet magnet `shouldBe` Just (nullMagnet ih) |
41 | 40 | ||
42 | it "parse base16" $ do | 41 | it "parse base16" $ do |
43 | let magnet = "magnet:?xt=urn:btih:0123456789abcdef0123456789abcdef01234567" | 42 | let magnet = "magnet:?xt=urn:btih:0123456789abcdef0123456789abcdef01234567" |
44 | let ih = InfoHash "\SOH#Eg\137\171\205\239\SOH#Eg\137\171\205\239\SOH#Eg" | 43 | let ih = "0123456789abcdef0123456789abcdef01234567" |
45 | parseMagnet magnet `shouldBe` Just (nullMagnet ih) | 44 | parseMagnet magnet `shouldBe` Just (nullMagnet ih) |