From: Ramakrishnan Muthukrishnan Date: Thu, 30 Apr 2015 11:29:59 +0000 (+0530) Subject: wrap network calls with withSocketsDo for multiplatform initialization X-Git-Url: https://git.rkrishnan.org/simplejson/components/com_hotproperty/frontends/webapi.txt?a=commitdiff_plain;h=161ab47b13c214ef4e92962fad1299164bd6fbde;p=functorrent.git wrap network calls with withSocketsDo for multiplatform initialization --- diff --git a/functorrent.cabal b/functorrent.cabal index 19a6c92..5e3bf0d 100644 --- a/functorrent.cabal +++ b/functorrent.cabal @@ -37,6 +37,7 @@ library cryptohash, directory, HTTP, + network, network-uri, parsec, QuickCheck, @@ -58,6 +59,7 @@ executable functorrent directory, HTTP, QuickCheck, + network, network-uri, parsec diff --git a/src/FuncTorrent/Network.hs b/src/FuncTorrent/Network.hs index eac69c9..001fe0e 100644 --- a/src/FuncTorrent/Network.hs +++ b/src/FuncTorrent/Network.hs @@ -11,6 +11,7 @@ import Data.ByteString (ByteString) import Data.ByteString.Char8 as BC (pack, unpack, concat, intercalate) import Network.HTTP (simpleHTTP, defaultGETRequest_, getResponseBody) import Network.URI (parseURI) +import Network.Socket (withSocketsDo) -- | Make a query string from a alist of k, v -- TODO: Url encode each argument @@ -18,7 +19,7 @@ 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 +get url args = withSocketsDo $ simpleHTTP (defaultGETRequest_ url') >>= getResponseBody where url' = case parseURI $ unpack $ concat [pack url, "?", qstr] of Just x -> x _ -> error "Bad tracker URL"