summaryrefslogtreecommitdiff
path: root/src/Network/BitTorrent
diff options
context:
space:
mode:
Diffstat (limited to 'src/Network/BitTorrent')
-rw-r--r--src/Network/BitTorrent/Tracker/Message.hs30
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
68import Network 68import Network
69import Network.HTTP.Types.QueryLike 69import Network.HTTP.Types.QueryLike
70import Network.HTTP.Types.URI hiding (urlEncode) 70import Network.HTTP.Types.URI hiding (urlEncode)
71import Network.HTTP.Types.Status
71import Network.Socket 72import Network.Socket
72import Network.URI 73import Network.URI
73import Text.Read (readMaybe) 74import Text.Read (readMaybe)
@@ -252,11 +253,11 @@ data QueryParam
252 deriving (Show, Eq, Ord, Enum) 253 deriving (Show, Eq, Ord, Enum)
253 254
254data ParamParseFailure 255data 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
259paramName :: QueryParam -> ByteString 260paramName :: QueryParam -> BS.ByteString
260paramName ParamInfoHash = "info_hash" 261paramName ParamInfoHash = "info_hash"
261paramName ParamPeerId = "peer_id" 262paramName ParamPeerId = "peer_id"
262paramName ParamPort = "port" 263paramName 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
326type AnnounceRequest = ()
325 327
326{----------------------------------------------------------------------- 328{-----------------------------------------------------------------------
327-- Announce response 329-- Announce response
@@ -455,17 +457,27 @@ missingOffset = 101
455invalidOffset :: Int 457invalidOffset :: Int
456invalidOffset = 150 458invalidOffset = 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--
466paramFailureCode :: ParamParseFailure -> Int 466parseFailureCode :: ParamParseFailure -> Int
467paramFailureCode (Missing param ) = missingOffset + fromEnum param 467parseFailureCode (Missing param ) = missingOffset + fromEnum param
468paramFailureCode (Invalid param _) = invalidOffset + fromEnum param 468parseFailureCode (Invalid param _) = invalidOffset + fromEnum param
469
470-- | Human readable message
471parseFailureMessage :: ParamParseFailure -> BS.ByteString
472parseFailureMessage e = BS.concat $ case e of
473 Missing p -> ["Missing parameter: ", paramName p]
474 Invalid p v -> ["Invalid parameter: ", paramName p, " = ", v]
475
476parseFailureStatus :: ParamParseFailure -> Status
477parseFailureStatus = mkStatus <$> parseFailureCode <*> parseFailureMessage
478
479type AnnounceResponse = Either Status AnnounceInfo -- TODO
480type TrackerResponse = () -- TODO
469 481
470{----------------------------------------------------------------------- 482{-----------------------------------------------------------------------
471 Scrape message 483 Scrape message