From dea6c9b2ea1037ee54f1908ebc6a5e193e0cfac6 Mon Sep 17 00:00:00 2001 From: Sam T Date: Sun, 2 Jun 2013 05:55:01 +0400 Subject: ~ Merge InfoHash to Torrent. This allow to provide better interface. --- src/Data/Torrent/InfoHash.hs | 84 -------------------------------------------- 1 file changed, 84 deletions(-) delete mode 100644 src/Data/Torrent/InfoHash.hs (limited to 'src/Data/Torrent') diff --git a/src/Data/Torrent/InfoHash.hs b/src/Data/Torrent/InfoHash.hs deleted file mode 100644 index 51ce0ecd..00000000 --- a/src/Data/Torrent/InfoHash.hs +++ /dev/null @@ -1,84 +0,0 @@ --- | --- Copyright : (c) Sam T. 2013 --- License : MIT --- Maintainer : pxqr.sta@gmail.com --- Stability : experimental --- Portability : portable --- -{-# LANGUAGE GeneralizedNewtypeDeriving, FlexibleInstances #-} -module Data.Torrent.InfoHash - ( InfoHash (getInfoHash) - , addHashToURI - - -- * Construction - , hash, hashlazy - - -- * Extra - , ppHex - ) where - -import Control.Applicative -import Data.BEncode -import Data.Char -import Data.List as L -import Data.Foldable -import Data.Map (Map) -import qualified Data.Map as M -import Data.ByteString (ByteString) -import qualified Data.ByteString as B -import qualified Data.ByteString.Char8 as BC -import qualified Data.ByteString.Builder as B -import qualified Data.ByteString.Builder.Prim as B -import qualified Data.ByteString.Lazy as Lazy -import Data.Serialize -import qualified Crypto.Hash.SHA1 as C -import Network.URI -import Numeric - - --- | Exactly 20 bytes long SHA1 hash. -newtype InfoHash = InfoHash { getInfoHash :: ByteString } - deriving (Eq, Ord, BEncodable) - -instance Show InfoHash where - show = BC.unpack . ppHex - -instance Serialize InfoHash where - put = putByteString . getInfoHash - get = InfoHash <$> getBytes 20 - -instance BEncodable a => BEncodable (Map InfoHash a) where - {-# SPECIALIZE instance BEncodable a => BEncodable (Map InfoHash a) #-} - fromBEncode b = M.mapKeys InfoHash <$> fromBEncode b - {-# INLINE fromBEncode #-} - - toBEncode = toBEncode . M.mapKeys getInfoHash - {-# INLINE toBEncode #-} - -hash :: ByteString -> InfoHash -hash = InfoHash . C.hash - -hashlazy :: Lazy.ByteString -> InfoHash -hashlazy = InfoHash . C.hashlazy - -ppHex :: InfoHash -> ByteString -ppHex = Lazy.toStrict . B.toLazyByteString . - foldMap (B.primFixed B.word8HexFixed) . B.unpack . getInfoHash - -addHashToURI :: URI -> InfoHash -> URI -addHashToURI uri s = uri { - uriQuery = uriQuery uri ++ mkPref (uriQuery uri) ++ - "info_hash=" ++ rfc1738Encode (BC.unpack (getInfoHash s)) - } - where - mkPref [] = "?" - mkPref ('?' : _) = "&" - mkPref _ = error "addHashToURI" - - rfc1738Encode = L.concatMap (\c -> if unreservedS c then [c] else encodeHex c) - where - unreservedS = (`L.elem` chars) - chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_./" - encodeHex c = '%' : pHex c - pHex c = let p = (showHex . ord $ c) "" - in if L.length p == 1 then '0' : p else p -- cgit v1.2.3