6 -- |An internal module for generating Haskell modules eith an
7 -- arbitrary file implanted.
8 module Network.HTTP.Lucu.Implant
18 import Codec.Compression.GZip
19 import Control.Applicative
20 import qualified Data.ByteString.Lazy as L
21 import Data.Convertible.Base
22 import Data.Convertible.Instances.Ascii ()
23 import Data.Digest.Pure.SHA
26 import Network.HTTP.Lucu.ETag
27 import Network.HTTP.Lucu.MIMEType hiding (mimeType)
28 import Network.HTTP.Lucu.MIMEType.DefaultExtensionMap
29 import Network.HTTP.Lucu.MIMEType.Guess
30 import Network.HTTP.Lucu.MIMEType.TH
31 import Network.HTTP.Lucu.Utils
32 import Prelude.Unicode
40 , iRawData ∷ !L.ByteString
41 , iGZipped ∷ !L.ByteString
44 originalLen ∷ Input → Integer
45 originalLen (Input {..})
46 = fromIntegral $ L.length iRawData
48 gzippedLen ∷ Input → Integer
49 gzippedLen (Input {..})
50 = fromIntegral $ L.length iGZipped
52 useGZip ∷ Input → Bool
54 = originalLen i ≥ gzippedLen i
56 openInput ∷ FilePath → Maybe MIMEType → Maybe ETag → IO Input
57 openInput fpath ctype etag
58 = do lastMod ← lastModified fpath
59 input ← openInputFile fpath
63 , iType = fromMaybe octetStream
64 $ ctype <|> guessType fpath
65 , iETag = fromMaybe (mkETagFromInput input) etag
67 , iGZipped = compressWith compParams input
70 octetStream ∷ MIMEType
71 octetStream = [mimeType| application/octet-stream |]
73 compParams ∷ CompressParams
74 compParams = defaultCompressParams {
75 compressLevel = bestCompression
78 lastModified ∷ FilePath → IO UTCTime
79 lastModified "-" = getCurrentTime
80 lastModified fpath = getLastModified fpath
82 openInputFile ∷ FilePath → IO L.ByteString
83 openInputFile "-" = L.getContents
84 openInputFile fpath = L.readFile fpath
86 guessType ∷ FilePath → Maybe MIMEType
87 guessType = guessTypeByFileName defaultExtensionMap
89 mkETagFromInput ∷ L.ByteString → ETag
91 = strongETag $ convertUnsafe
92 $ "SHA-1:" ⧺ showDigest (sha1 input)