jckuri

DownloadURL.hs (Inspired by the book Real World Haskell)

Jan 2nd, 2014
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- DownloadURL.hs (Inspired by the book Real World Haskell)
  2.  
  3. module DownloadURL where
  4.  
  5. import Network.HTTP
  6. import System.IO
  7. import Data.Maybe
  8. import Network.URI
  9.  
  10. processRedirect r =
  11.  case findHeader HdrLocation r of
  12.   Nothing -> return $ Left (show r)
  13.   Just url -> downloadURL url
  14.  
  15. processRspCode r =
  16.  case rspCode r of
  17.   (2,_,_) -> return $ Right (rspBody r)
  18.   (3,_,_) -> processRedirect r
  19.   _ -> return $ Left (show r)
  20.  
  21. processResponse resp =
  22.  case resp of
  23.   Left error -> return $ Left ("Error connecting: " ++ show error)
  24.   Right r -> processRspCode r
  25.  
  26. downloadURL :: String -> IO (Either String String)
  27. downloadURL url =
  28.  simpleHTTP request >>=
  29.  processResponse
  30.  where
  31.   request = Request {rqURI = uri, rqMethod = GET, rqHeaders = [], rqBody = ""}
  32.   uri = fromJust $ parseURI url
Add Comment
Please, Sign In to add comment