import qualified Data.ByteString.Lazy as Lazy (ByteString)
import qualified Data.ByteString.Lazy as L hiding (ByteString)
import Data.Char
-import Data.Digest.SHA1
import Data.Int
import Data.Maybe
import Data.Time
import Data.Time.Clock.POSIX
-import Data.Word
import Language.Haskell.Pretty
import Language.Haskell.Syntax
import Network.HTTP.Lucu.MIMEType
import Network.HTTP.Lucu.MIMEType.DefaultExtensionMap
import Network.HTTP.Lucu.MIMEType.Guess
+import OpenSSL
+import OpenSSL.EVP.Digest
import System.Console.GetOpt
import System.Environment
import System.Exit
main :: IO ()
-main = do (opts, sources, errors) <- return . getOpt Permute options =<< getArgs
+main = withOpenSSL $
+ do (opts, sources, errors) <- return . getOpt Permute options =<< getArgs
when (not $ null errors)
$ do mapM_ putStr errors
output <- openOutput opts
eTag <- getETag opts input
- let gzippedData = compressWith BestCompression input
+ let compParams = defaultCompressParams { compressLevel = BestCompression }
+ gzippedData = compressWith compParams input
originalLen = L.length input
gzippedLen = L.length gzippedData
useGZip = originalLen > gzippedLen
False Nothing Nothing
, HsImportDecl undefined (Module "Data.ByteString.Lazy")
True (Just (Module "L")) Nothing
+ , HsImportDecl undefined (Module "Data.Maybe")
+ False Nothing Nothing
, HsImportDecl undefined (Module "Data.Time")
False Nothing Nothing
, HsImportDecl undefined (Module "Network.HTTP.Lucu")
defGZippedData
= HsApp (HsVar (Qual (Module "L") (HsIdent "pack")))
(HsParen
- (HsApp (HsVar (UnQual (HsIdent "decode")))
- (HsLit (HsString gzippedB64))))
+ (HsApp (HsVar (UnQual (HsIdent "fromJust")))
+ (HsParen
+ (HsApp (HsVar (UnQual (HsIdent "decode")))
+ (HsLit (HsString gzippedB64))))))
declRawData :: [HsDecl]
declRawData
defRawData
= HsApp (HsVar (Qual (Module "L") (HsIdent "pack")))
(HsParen
- (HsApp (HsVar (UnQual (HsIdent "decode")))
- (HsLit (HsString rawB64))))
+ (HsApp (HsVar (UnQual (HsIdent "fromJust")))
+ (HsParen
+ (HsApp (HsVar (UnQual (HsIdent "decode")))
+ (HsLit (HsString rawB64))))))
hPutStrLn output header
hPutStrLn output (prettyPrint hsModule)
getLastModified :: FilePath -> IO UTCTime
getLastModified "-" = getCurrentTime
getLastModified fpath = getFileStatus fpath
- >>= return . posixSecondsToUTCTime . toEnum . fromEnum . modificationTime
+ >>= return . posixSecondsToUTCTime . fromRational . toRational . modificationTime
getETag :: [CmdOpt] -> Lazy.ByteString -> IO String
_ -> False) opts
in
case eTagOpts of
- [] -> return mkETagFromInput
+ [] -> getDigestByName "SHA1" >>= return . mkETagFromInput . fromJust
(OptETag str):[] -> return str
_ -> error "too many --etag options."
where
- mkETagFromInput :: String
- mkETagFromInput = "SHA-1:" ++ (toHex $ hash $ L.unpack input)
+ mkETagFromInput :: Digest -> String
+ mkETagFromInput sha1 = "SHA-1:" ++ (toHex $ digestLBS sha1 input)
- toHex :: [Word8] -> String
+ toHex :: [Char] -> String
toHex [] = ""
- toHex (x:xs) = hexByte (fromIntegral x) ++ toHex xs
+ toHex (x:xs) = hexByte (fromEnum x) ++ toHex xs
hexByte :: Int -> String
hexByte n
module Foo.Bar.Baz (baz) where
import Codec.Binary.Base64
import qualified Data.ByteString.Lazy as L
+ import Data.Maybe
import Data.Time
import Network.HTTP.Lucu
contentType = read "image/png"
rawData :: L.ByteString
- rawData = L.pack (decode "IyEvdXNyL2Jpbi9lbnYgcnVuZ2hjCgppbXBvcnQgRGlzdHJ...")
+ rawData = L.pack (fromJust (decode "IyEvdXNyL2Jpbi9lbnYgcnVuZ2hjCgppbXBvcnQgRGlzdHJ..."))
------------------------------------------------------------------------------
壓縮される場合は次のやうに變はる:
-- rawData の代はりに gzippedData
gzippedData :: L.ByteString
- gzippedData = L.pack (decode "Otb/+DniOlRgAAAAYAAAAGAAAAB/6QOmToAEIGAAAAB...")
+ gzippedData = L.pack (fromJust (decode "Otb/+DniOlRgAAAAYAAAAGAAAAB/6QOmToAEIGAAAAB..."))
------------------------------------------------------------------------------
-}