diff options
Diffstat (limited to 'src/Network/BitTorrent/Tracker/Message.hs')
-rw-r--r-- | src/Network/BitTorrent/Tracker/Message.hs | 30 |
1 files changed, 21 insertions, 9 deletions
diff --git a/src/Network/BitTorrent/Tracker/Message.hs b/src/Network/BitTorrent/Tracker/Message.hs index 0df889d3..4b09d367 100644 --- a/src/Network/BitTorrent/Tracker/Message.hs +++ b/src/Network/BitTorrent/Tracker/Message.hs | |||
@@ -36,7 +36,7 @@ module Network.BitTorrent.Tracker.Message | |||
36 | , PeerList (..) | 36 | , PeerList (..) |
37 | , AnnounceInfo(..) | 37 | , AnnounceInfo(..) |
38 | , defaultNumWant | 38 | , defaultNumWant |
39 | , paramFailureCode | 39 | , parseFailureStatus |
40 | 40 | ||
41 | -- * Scrape | 41 | -- * Scrape |
42 | , ScrapeQuery | 42 | , ScrapeQuery |
@@ -68,6 +68,7 @@ import Data.Word | |||
68 | import Network | 68 | import Network |
69 | import Network.HTTP.Types.QueryLike | 69 | import Network.HTTP.Types.QueryLike |
70 | import Network.HTTP.Types.URI hiding (urlEncode) | 70 | import Network.HTTP.Types.URI hiding (urlEncode) |
71 | import Network.HTTP.Types.Status | ||
71 | import Network.Socket | 72 | import Network.Socket |
72 | import Network.URI | 73 | import Network.URI |
73 | import Text.Read (readMaybe) | 74 | import Text.Read (readMaybe) |
@@ -252,11 +253,11 @@ data QueryParam | |||
252 | deriving (Show, Eq, Ord, Enum) | 253 | deriving (Show, Eq, Ord, Enum) |
253 | 254 | ||
254 | data ParamParseFailure | 255 | data ParamParseFailure |
255 | = Missing QueryParam -- ^ param not found in query string; | 256 | = Missing QueryParam -- ^ param not found in query string; |
256 | | Invalid QueryParam ByteString -- ^ param present but not valid. | 257 | | Invalid QueryParam BS.ByteString -- ^ param present but not valid. |
257 | deriving (Show, Eq) | 258 | deriving (Show, Eq) |
258 | 259 | ||
259 | paramName :: QueryParam -> ByteString | 260 | paramName :: QueryParam -> BS.ByteString |
260 | paramName ParamInfoHash = "info_hash" | 261 | paramName ParamInfoHash = "info_hash" |
261 | paramName ParamPeerId = "peer_id" | 262 | paramName ParamPeerId = "peer_id" |
262 | paramName ParamPort = "port" | 263 | paramName ParamPort = "port" |
@@ -322,6 +323,7 @@ parseAnnounceQuery params = AnnounceQuery | |||
322 | <*> optParam ParamEvent params | 323 | <*> optParam ParamEvent params |
323 | 324 | ||
324 | -- TODO add extension datatype | 325 | -- TODO add extension datatype |
326 | type AnnounceRequest = () | ||
325 | 327 | ||
326 | {----------------------------------------------------------------------- | 328 | {----------------------------------------------------------------------- |
327 | -- Announce response | 329 | -- Announce response |
@@ -455,17 +457,27 @@ missingOffset = 101 | |||
455 | invalidOffset :: Int | 457 | invalidOffset :: Int |
456 | invalidOffset = 150 | 458 | invalidOffset = 150 |
457 | 459 | ||
458 | -- TODO use Network.HTTP.Types.Status | ||
459 | |||
460 | -- | Get HTTP response error code from a announce params parse | 460 | -- | Get HTTP response error code from a announce params parse |
461 | -- failure. | 461 | -- failure. |
462 | -- | 462 | -- |
463 | -- For more info see: | 463 | -- For more info see: |
464 | -- <https://wiki.theory.org/BitTorrent_Tracker_Protocol#Response_Codes> | 464 | -- <https://wiki.theory.org/BitTorrent_Tracker_Protocol#Response_Codes> |
465 | -- | 465 | -- |
466 | paramFailureCode :: ParamParseFailure -> Int | 466 | parseFailureCode :: ParamParseFailure -> Int |
467 | paramFailureCode (Missing param ) = missingOffset + fromEnum param | 467 | parseFailureCode (Missing param ) = missingOffset + fromEnum param |
468 | paramFailureCode (Invalid param _) = invalidOffset + fromEnum param | 468 | parseFailureCode (Invalid param _) = invalidOffset + fromEnum param |
469 | |||
470 | -- | Human readable message | ||
471 | parseFailureMessage :: ParamParseFailure -> BS.ByteString | ||
472 | parseFailureMessage e = BS.concat $ case e of | ||
473 | Missing p -> ["Missing parameter: ", paramName p] | ||
474 | Invalid p v -> ["Invalid parameter: ", paramName p, " = ", v] | ||
475 | |||
476 | parseFailureStatus :: ParamParseFailure -> Status | ||
477 | parseFailureStatus = mkStatus <$> parseFailureCode <*> parseFailureMessage | ||
478 | |||
479 | type AnnounceResponse = Either Status AnnounceInfo -- TODO | ||
480 | type TrackerResponse = () -- TODO | ||
469 | 481 | ||
470 | {----------------------------------------------------------------------- | 482 | {----------------------------------------------------------------------- |
471 | Scrape message | 483 | Scrape message |