diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/Network/BitTorrent/Core/PeerId.hs | 15 | ||||
-rw-r--r-- | src/Network/BitTorrent/Tracker/Message.hs | 8 |
2 files changed, 13 insertions, 10 deletions
diff --git a/src/Network/BitTorrent/Core/PeerId.hs b/src/Network/BitTorrent/Core/PeerId.hs index 8bd175e5..148f550d 100644 --- a/src/Network/BitTorrent/Core/PeerId.hs +++ b/src/Network/BitTorrent/Core/PeerId.hs | |||
@@ -12,10 +12,11 @@ | |||
12 | -- /peer handshakes/ and used in DHT queries. | 12 | -- /peer handshakes/ and used in DHT queries. |
13 | -- | 13 | -- |
14 | {-# LANGUAGE GeneralizedNewtypeDeriving #-} | 14 | {-# LANGUAGE GeneralizedNewtypeDeriving #-} |
15 | {-# LANGUAGE MultiParamTypeClasses #-} | ||
16 | {-# LANGUAGE DeriveDataTypeable #-} | ||
15 | module Network.BitTorrent.Core.PeerId | 17 | module Network.BitTorrent.Core.PeerId |
16 | ( -- * PeerId | 18 | ( -- * PeerId |
17 | PeerId | 19 | PeerId |
18 | , byteStringToPeerId | ||
19 | 20 | ||
20 | -- * Generation | 21 | -- * Generation |
21 | , genPeerId | 22 | , genPeerId |
@@ -40,6 +41,7 @@ import Data.ByteString.Internal as BS | |||
40 | import Data.ByteString.Char8 as BC | 41 | import Data.ByteString.Char8 as BC |
41 | import qualified Data.ByteString.Lazy as BL | 42 | import qualified Data.ByteString.Lazy as BL |
42 | import qualified Data.ByteString.Lazy.Builder as BS | 43 | import qualified Data.ByteString.Lazy.Builder as BS |
44 | import Data.Convertible | ||
43 | import Data.Default | 45 | import Data.Default |
44 | import Data.Foldable (foldMap) | 46 | import Data.Foldable (foldMap) |
45 | import Data.List as L | 47 | import Data.List as L |
@@ -51,6 +53,7 @@ import Data.Serialize as S | |||
51 | import Data.String | 53 | import Data.String |
52 | import Data.Time.Clock (getCurrentTime) | 54 | import Data.Time.Clock (getCurrentTime) |
53 | import Data.Time.Format (formatTime) | 55 | import Data.Time.Format (formatTime) |
56 | import Data.Typeable | ||
54 | import Data.Version (Version(Version), versionBranch) | 57 | import Data.Version (Version(Version), versionBranch) |
55 | import Network.HTTP.Types.QueryLike | 58 | import Network.HTTP.Types.QueryLike |
56 | import System.Entropy (getEntropy) | 59 | import System.Entropy (getEntropy) |
@@ -65,7 +68,7 @@ import Data.Torrent.Client | |||
65 | 68 | ||
66 | -- | Peer identifier is exactly 20 bytes long bytestring. | 69 | -- | Peer identifier is exactly 20 bytes long bytestring. |
67 | newtype PeerId = PeerId { getPeerId :: ByteString } | 70 | newtype PeerId = PeerId { getPeerId :: ByteString } |
68 | deriving (Show, Eq, Ord, BEncode, ToJSON, FromJSON) | 71 | deriving (Show, Eq, Ord, BEncode, ToJSON, FromJSON, Typeable) |
69 | 72 | ||
70 | peerIdLen :: Int | 73 | peerIdLen :: Int |
71 | peerIdLen = 20 | 74 | peerIdLen = 20 |
@@ -92,10 +95,10 @@ instance IsString PeerId where | |||
92 | instance Pretty PeerId where | 95 | instance Pretty PeerId where |
93 | pretty = text . BC.unpack . getPeerId | 96 | pretty = text . BC.unpack . getPeerId |
94 | 97 | ||
95 | byteStringToPeerId :: BS.ByteString -> Maybe PeerId | 98 | instance Convertible BS.ByteString PeerId where |
96 | byteStringToPeerId bs | 99 | safeConvert bs |
97 | | BS.length bs == peerIdLen = Just (PeerId bs) | 100 | | BS.length bs == peerIdLen = pure (PeerId bs) |
98 | | otherwise = Nothing | 101 | | otherwise = convError "invalid length" bs |
99 | 102 | ||
100 | {----------------------------------------------------------------------- | 103 | {----------------------------------------------------------------------- |
101 | -- Encoding | 104 | -- Encoding |
diff --git a/src/Network/BitTorrent/Tracker/Message.hs b/src/Network/BitTorrent/Tracker/Message.hs index 046f7e57..9999d128 100644 --- a/src/Network/BitTorrent/Tracker/Message.hs +++ b/src/Network/BitTorrent/Tracker/Message.hs | |||
@@ -267,7 +267,7 @@ instance FromParam InfoHash where | |||
267 | fromParam = either (const Nothing) pure . safeConvert | 267 | fromParam = either (const Nothing) pure . safeConvert |
268 | 268 | ||
269 | instance FromParam PeerId where | 269 | instance FromParam PeerId where |
270 | fromParam = byteStringToPeerId | 270 | fromParam = either (const Nothing) pure . safeConvert |
271 | 271 | ||
272 | instance FromParam Word32 where | 272 | instance FromParam Word32 where |
273 | fromParam = readMaybe . BC.unpack | 273 | fromParam = readMaybe . BC.unpack |
@@ -282,9 +282,9 @@ instance FromParam PortNumber where | |||
282 | fromParam bs = fromIntegral <$> (fromParam bs :: Maybe Word32) | 282 | fromParam bs = fromIntegral <$> (fromParam bs :: Maybe Word32) |
283 | 283 | ||
284 | instance FromParam Event where | 284 | instance FromParam Event where |
285 | fromParam bs = case BC.uncons bs of | 285 | fromParam bs = do |
286 | Nothing -> Nothing | 286 | (x, xs) <- BC.uncons bs |
287 | Just (x, xs) -> readMaybe $ BC.unpack $ BC.cons (Char.toUpper x) xs | 287 | readMaybe $ BC.unpack $ BC.cons (Char.toUpper x) xs |
288 | 288 | ||
289 | type Result = Either ParamParseFailure | 289 | type Result = Either ParamParseFailure |
290 | 290 | ||