From e69d363fa517e8769ef4b43962065c827896ea43 Mon Sep 17 00:00:00 2001
From: Ramakrishnan Muthukrishnan <ram@rkrishnan.org>
Date: Mon, 5 Oct 2015 07:06:00 +0530
Subject: [PATCH] Tracker: identify tracker protocol from the tracker server
 url

---
 src/FuncTorrent/Tracker.hs | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)

diff --git a/src/FuncTorrent/Tracker.hs b/src/FuncTorrent/Tracker.hs
index 6ae0281..e0752e9 100644
--- a/src/FuncTorrent/Tracker.hs
+++ b/src/FuncTorrent/Tracker.hs
@@ -18,7 +18,7 @@ import Data.ByteString (ByteString, hGet, hPut)
 import Data.ByteString.Char8 as BC (pack, unpack, splitAt)
 import Data.ByteString.Lazy (fromStrict, toStrict)
 import Data.Char (chr)
-import Data.List (intercalate)
+import Data.List (intercalate, isPrefixOf)
 import Data.Map as M (lookup)
 import Network (PortNumber)
 import Network.HTTP.Base (urlEncode)
@@ -31,6 +31,12 @@ import FuncTorrent.Network (sendGetRequest)
 import FuncTorrent.Peer (Peer(..))
 import FuncTorrent.Utils (splitN)
 
+
+data TrackerProtocol = Http
+                     | Udp
+                     | UnknownProtocol
+                     deriving (Show)
+
 -- | Tracker response
 data TrackerResponse = TrackerResponse {
   interval :: Integer
@@ -243,3 +249,8 @@ getResponse h = do
 sendRequest :: Handle -> UDPRequest -> IO ()
 sendRequest h req = hPut h bsReq
   where bsReq = toStrict $ encode req
+
+getTrackerType :: String -> TrackerProtocol
+getTrackerType url | isPrefixOf "http://" url = Http
+                   | isPrefixOf "udp://" url  = Udp
+                   | otherwise                = UnknownProtocol
-- 
2.45.2