From: Jaseem Abid Date: Thu, 23 Apr 2015 15:26:02 +0000 (+0530) Subject: Move network code to own file X-Git-Url: https://git.rkrishnan.org/components/com_hotproperty/flags/%22doc.html/status?a=commitdiff_plain;h=829ff32e8b3fef3c3b0e8461cda3613365ca91cb;p=functorrent.git Move network code to own file --- diff --git a/functorrent.cabal b/functorrent.cabal index c3492db..6bfb1c8 100644 --- a/functorrent.cabal +++ b/functorrent.cabal @@ -20,6 +20,7 @@ library FuncTorrent.Bencode, FuncTorrent.Logger, FuncTorrent.Metainfo, + FuncTorrent.Network FuncTorrent.Peer, FuncTorrent.Tracker diff --git a/src/FuncTorrent/Network.hs b/src/FuncTorrent/Network.hs new file mode 100644 index 0000000..eac69c9 --- /dev/null +++ b/src/FuncTorrent/Network.hs @@ -0,0 +1,25 @@ +{-# LANGUAGE OverloadedStrings #-} +module FuncTorrent.Network + ( + get, + mkParams + ) where + +import Prelude hiding (concat) + +import Data.ByteString (ByteString) +import Data.ByteString.Char8 as BC (pack, unpack, concat, intercalate) +import Network.HTTP (simpleHTTP, defaultGETRequest_, getResponseBody) +import Network.URI (parseURI) + +-- | Make a query string from a alist of k, v +-- TODO: Url encode each argument +mkParams :: [(String, ByteString)] -> ByteString +mkParams params = BC.intercalate "&" [concat [pack f, "=", s] | (f,s) <- params] + +get :: String -> [(String, ByteString)] -> IO ByteString +get url args = simpleHTTP (defaultGETRequest_ url') >>= getResponseBody + where url' = case parseURI $ unpack $ concat [pack url, "?", qstr] of + Just x -> x + _ -> error "Bad tracker URL" + qstr = mkParams args diff --git a/src/FuncTorrent/Tracker.hs b/src/FuncTorrent/Tracker.hs index 749f75d..07bf7f7 100644 --- a/src/FuncTorrent/Tracker.hs +++ b/src/FuncTorrent/Tracker.hs @@ -3,28 +3,25 @@ module FuncTorrent.Tracker (TrackerResponse(..), connect, mkArgs, - mkParams, mkTrackerResponse, urlEncodeHash ) where -import Prelude hiding (lookup, concat, replicate, splitAt) +import Prelude hiding (lookup, splitAt) import Data.ByteString (ByteString) -import Data.ByteString.Char8 as BC (pack, unpack, splitAt, concat, intercalate) +import Data.ByteString.Char8 as BC (pack, unpack, splitAt) import Data.Char (chr) import Data.List (intercalate) import Data.Map as M (lookup) -import Network.HTTP (simpleHTTP, defaultGETRequest_, getResponseBody) import Network.HTTP.Base (urlEncode) -import Network.URI (parseURI) import qualified Data.ByteString.Base16 as B16 (encode) import FuncTorrent.Bencode (BVal(..)) +import FuncTorrent.Metainfo (Info(..), Metainfo(..)) +import FuncTorrent.Network (get) import FuncTorrent.Peer (Peer(..)) import FuncTorrent.Utils (splitN) -import FuncTorrent.Metainfo (Info(..), Metainfo(..)) - -- | Tracker response data TrackerResponse = TrackerResponse { @@ -97,15 +94,3 @@ mkArgs m peer_id = [("info_hash", pack . urlEncodeHash . B16.encode . infoHash $ ("left", pack . show . lengthInBytes $ info m), ("compact", "1"), ("event", "started")] - --- | Make a query string from a alist of k, v --- TODO: Url encode each argument -mkParams :: [(String, ByteString)] -> ByteString -mkParams params = BC.intercalate "&" [concat [pack f, "=", s] | (f,s) <- params] - -get :: String -> [(String, ByteString)] -> IO ByteString -get url args = simpleHTTP (defaultGETRequest_ url') >>= getResponseBody - where url' = case parseURI $ unpack $ concat [pack url, "?", qstr] of - Just x -> x - _ -> error "Bad tracker URL" - qstr = mkParams args