~alcinnz/hurl

ref: e9e75cebf9d684a1552bcbe11e8de3f274424433 hurl/src/Network/MIME/Info.hs -rw-r--r-- 1.1 KiB
e9e75ceb — Adrian Cochrane Release HURL v2.3.0.1 1 year, 8 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
{-# LANGUAGE CPP #-}
module Network.MIME.Info(mimeInfo, MIME, Application(..)) where

#ifdef WITH_XDG
import Network.URI.XDG.MimeInfo (readMimeInfo)
#endif
import Network.URI.Locale (rfc2616Locale)
import Network.URI.Types (Application(..))

import qualified Data.Map as M
import Control.Concurrent.MVar (MVar, newMVar, readMVar, modifyMVar_)
import System.IO.Unsafe (unsafePerformIO)
import Data.Char (toLower)

type MIME = Application

{-# NOINLINE mimeInfo #-}
mimeInfo :: String -> MIME
mimeInfo = unsafePerformIO $ do
    (locales, _) <- rfc2616Locale
    cache <- newMVar M.empty :: IO (MVar (M.Map String MIME))
    return $ \mime -> unsafePerformIO $ do
        readMVar cache >>= inner mime locales cache
  where
    inner mime _ _ cache | Just val <- mime `M.lookup` cache = return val
    inner mime locales cache' cache = do
        ret <- readMimeInfo locales mime
        modifyMVar_ cache' $ return . M.insert mime ret
        return ret

#ifndef WITH_XDG
readMimeInfo _ mime = return Application {
        name = mime,
        icon = URI "about:" Nothing "invalid" "" "",
        description = "",
        appId = mime
    }
#endif