From ba3f53ff6185de7d1a2626a12f6b8e4eb94acf86 Mon Sep 17 00:00:00 2001 From: Ossi Herrala Date: Sun, 5 Jun 2016 20:52:53 +0300 Subject: [PATCH] Catch IOException from openStream and turn it into Result openStream can fail for example when connecting to host fails. Catch this and return error as Result with message from IOException instead. While there, remove unnecessary normalizeRequest since it's called in simpleHTTP_. Fixes #92 --- Network/HTTP.hs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/Network/HTTP.hs b/Network/HTTP.hs index 1ab6dd2..101feee 100644 --- a/Network/HTTP.hs +++ b/Network/HTTP.hs @@ -79,9 +79,10 @@ import Network.HTTP.Base import qualified Network.HTTP.HandleStream as S -- old implementation: import Network.HTTP.Stream import Network.TCP -import Network.Stream ( Result ) +import Network.Stream ( Result, failMisc ) import Network.URI ( parseURI ) +import Control.Exception ( IOException, try ) import Data.Maybe ( fromMaybe ) {- @@ -106,9 +107,10 @@ simpleHTTP :: (HStream ty) => Request ty -> IO (Result (Response ty)) simpleHTTP r = do auth <- getAuth r failHTTPS (rqURI r) - c <- openStream (host auth) (fromMaybe 80 (port auth)) - let norm_r = normalizeRequest defaultNormalizeRequestOptions{normDoClose=True} r - simpleHTTP_ c norm_r + c <- try (openStream (host auth) (fromMaybe 80 (port auth))) + case c of + Left err -> return . failMisc $ show (err :: IOException) + Right c' -> simpleHTTP_ c' r -- | Identical to 'simpleHTTP', but acting on an already opened stream. simpleHTTP_ :: HStream ty => HandleStream ty -> Request ty -> IO (Result (Response ty))