Build-Depends:
Crypto, FileManip, HUnit, HsHyperEstraier, HsSVN, Lucu, base,
bytestring, containers, directory, utf8-string, filepath,
- hslogger, hxt, mtl, network, parsec, stm, time, unix, zlib
+ hslogger, hxt, magic, mtl, network, parsec, stm, time, unix,
+ zlib
Main-Is:
Main.hs
Other-Modules:
import Network.HTTP.Lucu.Utils
import Rakka.Environment
import Rakka.Page
+import Rakka.Utils
import Rakka.Wiki
import Rakka.Wiki.Engine
import Rakka.Wiki.Parser
-}
handleRender :: Environment -> PageName -> Resource ()
handleRender env name
- = do cType <- guessTypeIfNeeded =<< getContentType
- bin <- inputLBS defaultLimit
+ = do bin <- inputLBS defaultLimit
+ cTypeM <- getContentType
+
+ let cType = case cTypeM of
+ Just t -> t
+ Nothing -> guessMIMEType bin
setContentType $ read "text/xml"
[xmlStr] <- liftIO $ runX ( setErrorMsgHandler False fail
writeDocumentToString [ (a_indent, v_1) ]
)
output xmlStr
- where
- guessTypeIfNeeded :: Maybe MIMEType -> Resource MIMEType
- guessTypeIfNeeded (Just t) = return t
- guessTypeIfNeeded Nothing = fail "not impl"
render :: (ArrowXml a, ArrowChoice a, ArrowIO a) =>
, maybeA
, deleteIfEmpty
, chomp
+ , guessMIMEType
)
where
import Control.Arrow
import Control.Arrow.ArrowList
+import qualified Data.ByteString.Lazy as Lazy (ByteString)
+import qualified Data.ByteString.Lazy.Char8 as L8 hiding (ByteString)
+import Magic
+import Network.HTTP.Lucu
+import System.IO.Unsafe
yesOrNo :: Bool -> String
chomp :: String -> String
chomp = reverse . snd . break (/= '\n') . reverse
+
+
+guessMIMEType :: Lazy.ByteString -> MIMEType
+guessMIMEType = read . unsafePerformIO . magicString magic . L8.unpack
+ where
+ magic :: Magic
+ magic = unsafePerformIO
+ $ do m <- magicOpen [MagicMime]
+ magicLoadDefault m
+ return m