module Curl ( readUrlPS, copyUrl, Cachable(Cachable, Uncachable, MaxAge) ) where import IO #ifdef HAVE_CURL import Foreign.C.String ( withCString, CString ) import Monad ( when ) #endif import FastPackedString ( PackedString, readFilePS ) import Lock ( withTemp ) data Cachable = Cachable | Uncachable | MaxAge !Int readUrlPS :: String -> Cachable -> IO PackedString readUrlPS u cache = withTemp $ \tf -> do copyUrl u tf cache readFilePS tf copyUrl :: String -> String -> Cachable -> IO () #ifdef HAVE_CURL copyUrl u f cache = withCString u $ \ustr -> withCString f $ \fstr -> do err <- get_curl fstr ustr (cachableToInt cache) when (err /= 0) $ fail $ "Failed to download URL "++ u ++ "\n" ++ curl_e err where curl_e 1 = "libcurl: unsupported protocol" curl_e 3 = "libcurl: malformed URL" curl_e 6 = "libcurl: couldn't resolve host" curl_e 7 = "libcurl: couldn't connect to host" curl_e 22 = "libcurl: HTTP error (404?)" curl_e 23 = "libcurl write error" curl_e err = "libcurl error code: "++show err #else copyUrl _ _ _ = fail "There is no libcurl!" #endif #ifdef HAVE_CURL cachableToInt :: Cachable -> Int cachableToInt Cachable = -1 cachableToInt Uncachable = 0 cachableToInt (MaxAge n) = n foreign import ccall "hscurl.h get_curl" get_curl :: CString -> CString -> Int -> IO Int #endif