diff options
-rw-r--r-- | src/Data/Torrent/InfoHash.hs | 35 |
1 files changed, 3 insertions, 32 deletions
diff --git a/src/Data/Torrent/InfoHash.hs b/src/Data/Torrent/InfoHash.hs index 15682250..d840ac87 100644 --- a/src/Data/Torrent/InfoHash.hs +++ b/src/Data/Torrent/InfoHash.hs | |||
@@ -20,8 +20,6 @@ module Data.Torrent.InfoHash | |||
20 | -- * Rendering | 20 | -- * Rendering |
21 | , longHex | 21 | , longHex |
22 | , shortHex | 22 | , shortHex |
23 | |||
24 | , addHashToURI | ||
25 | ) where | 23 | ) where |
26 | 24 | ||
27 | import Control.Applicative | 25 | import Control.Applicative |
@@ -37,7 +35,6 @@ import Data.Char | |||
37 | import Data.Convertible.Base | 35 | import Data.Convertible.Base |
38 | import Data.Default | 36 | import Data.Default |
39 | import Data.List as L | 37 | import Data.List as L |
40 | import Data.Maybe | ||
41 | import Data.Hashable as Hashable | 38 | import Data.Hashable as Hashable |
42 | import Data.Serialize | 39 | import Data.Serialize |
43 | import Data.String | 40 | import Data.String |
@@ -45,8 +42,6 @@ import Data.Text as T | |||
45 | import Data.Text.Encoding as T | 42 | import Data.Text.Encoding as T |
46 | import Data.Typeable | 43 | import Data.Typeable |
47 | import Network.HTTP.Types.QueryLike | 44 | import Network.HTTP.Types.QueryLike |
48 | import Network.URI | ||
49 | import Numeric | ||
50 | import Text.ParserCombinators.ReadP as P | 45 | import Text.ParserCombinators.ReadP as P |
51 | import Text.PrettyPrint | 46 | import Text.PrettyPrint |
52 | import Text.PrettyPrint.Class | 47 | import Text.PrettyPrint.Class |
@@ -88,7 +83,7 @@ instance Serialize InfoHash where | |||
88 | put (InfoHash ih) = putByteString ih | 83 | put (InfoHash ih) = putByteString ih |
89 | {-# INLINE put #-} | 84 | {-# INLINE put #-} |
90 | 85 | ||
91 | get = InfoHash <$> getBytes 20 | 86 | get = InfoHash <$> getBytes infoHashLen |
92 | {-# INLINE get #-} | 87 | {-# INLINE get #-} |
93 | 88 | ||
94 | -- | Convert to raw query value. (no encoding) | 89 | -- | Convert to raw query value. (no encoding) |
@@ -107,7 +102,7 @@ instance Pretty InfoHash where | |||
107 | -- | Read base16 encoded string. | 102 | -- | Read base16 encoded string. |
108 | instance Read InfoHash where | 103 | instance Read InfoHash where |
109 | readsPrec _ = readP_to_S $ do | 104 | readsPrec _ = readP_to_S $ do |
110 | str <- replicateM 40 (satisfy isHexDigit) | 105 | str <- replicateM (infoHashLen * 2) (satisfy isHexDigit) |
111 | return $ InfoHash $ decodeIH str | 106 | return $ InfoHash $ decodeIH str |
112 | where | 107 | where |
113 | decodeIH = BS.pack . L.map fromHex . pair | 108 | decodeIH = BS.pack . L.map fromHex . pair |
@@ -173,30 +168,6 @@ textToInfoHash = ignoreErrorMsg . safeConvert | |||
173 | longHex :: InfoHash -> Text | 168 | longHex :: InfoHash -> Text |
174 | longHex = T.decodeUtf8 . Base16.encode . getInfoHash | 169 | longHex = T.decodeUtf8 . Base16.encode . getInfoHash |
175 | 170 | ||
176 | -- | The same as 'longHex', but 7 character long. | 171 | -- | The same as 'longHex', but only first 7 characters. |
177 | shortHex :: InfoHash -> Text | 172 | shortHex :: InfoHash -> Text |
178 | shortHex = T.take 7 . longHex | 173 | shortHex = T.take 7 . longHex |
179 | |||
180 | -- | TODO remove from API | ||
181 | -- | ||
182 | -- Add query info hash parameter to uri. | ||
183 | -- | ||
184 | -- > info_hash=<url_encoded_info_hash> | ||
185 | -- | ||
186 | addHashToURI :: URI -> InfoHash -> URI | ||
187 | addHashToURI uri s = uri { | ||
188 | uriQuery = uriQuery uri ++ mkPref (uriQuery uri) ++ | ||
189 | "info_hash=" ++ rfc1738Encode (BC.unpack (getInfoHash s)) | ||
190 | } | ||
191 | where | ||
192 | mkPref [] = "?" | ||
193 | mkPref ('?' : _) = "&" | ||
194 | mkPref _ = error "addHashToURI" | ||
195 | |||
196 | rfc1738Encode = L.concatMap (\c -> if unreservedS c then [c] else encodeHex c) | ||
197 | where | ||
198 | unreservedS = (`L.elem` chars) | ||
199 | chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_./" | ||
200 | encodeHex c = '%' : pHex c | ||
201 | pHex c = let p = (showHex . ord $ c) "" | ||
202 | in if L.length p == 1 then '0' : p else p | ||