diff options
author | Sam Truzjan <pxqr.sta@gmail.com> | 2013-12-03 17:39:50 +0400 |
---|---|---|
committer | Sam Truzjan <pxqr.sta@gmail.com> | 2013-12-03 17:39:50 +0400 |
commit | ac8cd57442c970aa76e30d5f48dcb5c5cefe0adf (patch) | |
tree | 9ba446d94022ce4bf451018f7607ec6dfd1a0eea /src/Network/BitTorrent | |
parent | 9b49605d01a1895aecbc20aa22cc13230c4ec27d (diff) |
Add parsers & renderers for AnnounceRequest
Diffstat (limited to 'src/Network/BitTorrent')
-rw-r--r-- | src/Network/BitTorrent/Tracker/RPC/Message.hs | 43 |
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 | |||
71 | import Data.ByteString.Char8 as BC | 71 | import Data.ByteString.Char8 as BC |
72 | import Data.Char as Char | 72 | import Data.Char as Char |
73 | import Data.Convertible | 73 | import Data.Convertible |
74 | import Data.Default | ||
74 | import Data.List as L | 75 | import Data.List as L |
75 | import Data.Maybe | 76 | import Data.Maybe |
76 | import Data.Serialize as S hiding (Result) | 77 | import Data.Serialize as S hiding (Result) |
@@ -246,6 +247,7 @@ renderAnnounceQuery :: AnnounceQuery -> SimpleQuery | |||
246 | renderAnnounceQuery = queryToSimpleQuery . toQuery | 247 | renderAnnounceQuery = queryToSimpleQuery . toQuery |
247 | 248 | ||
248 | data QueryParam | 249 | data 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 | ||
260 | paramName :: QueryParam -> BS.ByteString | 265 | paramName :: QueryParam -> BS.ByteString |
@@ -267,6 +272,9 @@ paramName ParamDownloaded = "downloaded" | |||
267 | paramName ParamIP = "ip" | 272 | paramName ParamIP = "ip" |
268 | paramName ParamNumWant = "numwant" | 273 | paramName ParamNumWant = "numwant" |
269 | paramName ParamEvent = "event" | 274 | paramName ParamEvent = "event" |
275 | paramName ParamCompact = "compact" | ||
276 | paramName ParamNoPeerId = "no_peer_id" | ||
277 | {-# INLINE paramName #-} | ||
270 | 278 | ||
271 | class FromParam a where | 279 | class 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 | ||
374 | instance Default AnnounceQueryExt where | ||
375 | def = AnnounceQueryExt Nothing Nothing | ||
376 | |||
377 | instance 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 | |||
386 | instance 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. |
367 | parseAnnounceQueryExt :: SimpleQuery -> AnnounceQueryExt | 392 | parseAnnounceQueryExt :: SimpleQuery -> AnnounceQueryExt |
368 | parseAnnounceQueryExt = undefined | 393 | parseAnnounceQueryExt 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. |
371 | renderAnnounceQueryExt :: AnnounceQueryExt -> SimpleQuery | 399 | renderAnnounceQueryExt :: AnnounceQueryExt -> SimpleQuery |
372 | renderAnnounceQueryExt = undefined | 400 | renderAnnounceQueryExt = queryToSimpleQuery . toQuery |
373 | 401 | ||
374 | -- | HTTP tracker request with extensions. | 402 | -- | HTTP tracker request with extensions. |
375 | data AnnounceRequest = AnnounceRequest | 403 | data 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 | ||
408 | instance 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. |
381 | parseAnnounceRequest :: SimpleQuery -> ParseResult AnnounceRequest | 412 | parseAnnounceRequest :: SimpleQuery -> ParseResult AnnounceRequest |
382 | parseAnnounceRequest = undefined | 413 | parseAnnounceRequest 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. |
385 | renderAnnounceRequest :: AnnounceRequest -> SimpleQuery | 418 | renderAnnounceRequest :: AnnounceRequest -> SimpleQuery |
386 | renderAnnounceRequest = undefined | 419 | renderAnnounceRequest = queryToSimpleQuery . toQuery |
387 | 420 | ||
388 | {----------------------------------------------------------------------- | 421 | {----------------------------------------------------------------------- |
389 | -- Announce response | 422 | -- Announce response |