summaryrefslogtreecommitdiff
path: root/src/Network/BitTorrent/Tracker
diff options
context:
space:
mode:
Diffstat (limited to 'src/Network/BitTorrent/Tracker')
-rw-r--r--src/Network/BitTorrent/Tracker/RPC/Message.hs43
1 files changed, 38 insertions, 5 deletions
diff --git a/src/Network/BitTorrent/Tracker/RPC/Message.hs b/src/Network/BitTorrent/Tracker/RPC/Message.hs
index f13f7e97..4a5aa789 100644
--- a/src/Network/BitTorrent/Tracker/RPC/Message.hs
+++ b/src/Network/BitTorrent/Tracker/RPC/Message.hs
@@ -56,7 +56,7 @@ module Network.BitTorrent.Tracker.RPC.Message
56 , ScrapeEntry (..) 56 , ScrapeEntry (..)
57 , ScrapeInfo 57 , ScrapeInfo
58 58
59 -- ** Extra 59 -- * Extra
60 , queryToSimpleQuery 60 , queryToSimpleQuery
61 ) 61 )
62 where 62 where
@@ -71,6 +71,7 @@ import Data.ByteString as BS
71import Data.ByteString.Char8 as BC 71import Data.ByteString.Char8 as BC
72import Data.Char as Char 72import Data.Char as Char
73import Data.Convertible 73import Data.Convertible
74import Data.Default
74import Data.List as L 75import Data.List as L
75import Data.Maybe 76import Data.Maybe
76import Data.Serialize as S hiding (Result) 77import Data.Serialize as S hiding (Result)
@@ -246,6 +247,7 @@ renderAnnounceQuery :: AnnounceQuery -> SimpleQuery
246renderAnnounceQuery = queryToSimpleQuery . toQuery 247renderAnnounceQuery = queryToSimpleQuery . toQuery
247 248
248data QueryParam 249data QueryParam
250 -- announce query
249 = ParamInfoHash 251 = ParamInfoHash
250 | ParamPeerId 252 | ParamPeerId
251 | ParamPort 253 | ParamPort
@@ -255,6 +257,9 @@ data QueryParam
255 | ParamIP 257 | ParamIP
256 | ParamNumWant 258 | ParamNumWant
257 | ParamEvent 259 | ParamEvent
260 -- announce query ext
261 | ParamCompact
262 | ParamNoPeerId
258 deriving (Show, Eq, Ord, Enum) 263 deriving (Show, Eq, Ord, Enum)
259 264
260paramName :: QueryParam -> BS.ByteString 265paramName :: QueryParam -> BS.ByteString
@@ -267,6 +272,9 @@ paramName ParamDownloaded = "downloaded"
267paramName ParamIP = "ip" 272paramName ParamIP = "ip"
268paramName ParamNumWant = "numwant" 273paramName ParamNumWant = "numwant"
269paramName ParamEvent = "event" 274paramName ParamEvent = "event"
275paramName ParamCompact = "compact"
276paramName ParamNoPeerId = "no_peer_id"
277{-# INLINE paramName #-}
270 278
271class FromParam a where 279class FromParam a where
272 fromParam :: BS.ByteString -> Maybe a 280 fromParam :: BS.ByteString -> Maybe a
@@ -363,13 +371,33 @@ data AnnounceQueryExt = AnnounceQueryExt
363 , extNoPeerId :: !(Maybe Bool) 371 , extNoPeerId :: !(Maybe Bool)
364 } deriving (Show, Eq, Typeable) 372 } deriving (Show, Eq, Typeable)
365 373
374instance Default AnnounceQueryExt where
375 def = AnnounceQueryExt Nothing Nothing
376
377instance QueryLike AnnounceQueryExt where
378 toQuery AnnounceQueryExt {..} =
379 [ ("compact", toQueryFlag <$> extCompact)
380 , ("no_peer_id", toQueryFlag <$> extNoPeerId)
381 ]
382 where
383 toQueryFlag False = "0"
384 toQueryFlag True = "1"
385
386instance FromParam Bool where
387 fromParam "0" = Just False
388 fromParam "1" = Just True
389 fromParam _ = Nothing
390
366-- | Parse announce query extended part from query string. 391-- | Parse announce query extended part from query string.
367parseAnnounceQueryExt :: SimpleQuery -> AnnounceQueryExt 392parseAnnounceQueryExt :: SimpleQuery -> AnnounceQueryExt
368parseAnnounceQueryExt = undefined 393parseAnnounceQueryExt params = either (const def) id $
394 AnnounceQueryExt
395 <$> optParam ParamCompact params
396 <*> optParam ParamNoPeerId params
369 397
370-- | Render announce query extended part to query string. 398-- | Render announce query extended part to query string.
371renderAnnounceQueryExt :: AnnounceQueryExt -> SimpleQuery 399renderAnnounceQueryExt :: AnnounceQueryExt -> SimpleQuery
372renderAnnounceQueryExt = undefined 400renderAnnounceQueryExt = queryToSimpleQuery . toQuery
373 401
374-- | HTTP tracker request with extensions. 402-- | HTTP tracker request with extensions.
375data AnnounceRequest = AnnounceRequest 403data AnnounceRequest = AnnounceRequest
@@ -377,13 +405,18 @@ data AnnounceRequest = AnnounceRequest
377 , announceAdvises :: AnnounceQueryExt -- ^ Optional advises to the tracker. 405 , announceAdvises :: AnnounceQueryExt -- ^ Optional advises to the tracker.
378 } deriving (Show, Eq, Typeable) 406 } deriving (Show, Eq, Typeable)
379 407
408instance QueryLike AnnounceRequest where
409 toQuery AnnounceRequest{..} = toQuery announceAdvises ++ toQuery announceQuery
410
380-- | Parse announce request from query string. 411-- | Parse announce request from query string.
381parseAnnounceRequest :: SimpleQuery -> ParseResult AnnounceRequest 412parseAnnounceRequest :: SimpleQuery -> ParseResult AnnounceRequest
382parseAnnounceRequest = undefined 413parseAnnounceRequest params = AnnounceRequest
414 <$> parseAnnounceQuery params
415 <*> pure (parseAnnounceQueryExt params)
383 416
384-- | Render announce request to query string. 417-- | Render announce request to query string.
385renderAnnounceRequest :: AnnounceRequest -> SimpleQuery 418renderAnnounceRequest :: AnnounceRequest -> SimpleQuery
386renderAnnounceRequest = undefined 419renderAnnounceRequest = queryToSimpleQuery . toQuery
387 420
388{----------------------------------------------------------------------- 421{-----------------------------------------------------------------------
389-- Announce response 422-- Announce response