Avoid resource exhaustion in yesod-static

This commit is contained in:
Michael Snoyman 2011-10-05 13:36:10 +02:00
parent 4070b8e49a
commit 8e1d49aa59
2 changed files with 26 additions and 9 deletions

View File

@ -65,6 +65,9 @@ import qualified Data.ByteString as S
import Network.HTTP.Types (status301)
import System.PosixCompat.Files (getFileStatus, modificationTime)
import System.Posix.Types (EpochTime)
import qualified Data.Enumerator as E
import qualified Data.Enumerator.List as EL
import qualified Data.Enumerator.Binary as EB
import Network.Wai.Application.Static
( StaticSettings (..)
@ -271,22 +274,35 @@ mkStaticFilesList fp fs routeConName makeHash = do
]
]
-- don't use L.readFile here, since it doesn't close handles quickly enough if
-- there are lots of files in the static folder, it will cause exhausted file
-- descriptors
base64md5File :: Prelude.FilePath -> IO String
base64md5File file = do
contents <- L.readFile file
return $ base64md5 contents
bss <- E.run_ $ EB.enumFile file E.$$ EL.consume
return $ base64md5 $ L.fromChunks bss
-- FIXME I'd like something streaming instead
{-
fmap (base64 . finalize) $ E.run_ $
EB.enumFile file E.$$ EL.fold go (md5InitialContext, "")
where
go (context, prev) next = (md5Update context prev, next)
finalize (context, end) = md5Finalize context end
-}
-- | md5-hashes the given lazy bytestring and returns the hash as
-- base64url-encoded string.
--
-- This function returns the first 8 characters of the hash.
base64md5 :: L.ByteString -> String
base64md5 = map tr
. take 8
. S8.unpack
. Data.ByteString.Base64.encode
. Data.Serialize.encode
. md5
base64md5 = base64 . md5
base64 :: MD5Digest -> String
base64 = map tr
. take 8
. S8.unpack
. Data.ByteString.Base64.encode
. Data.Serialize.encode
where
tr '+' = '-'
tr '/' = '_'

View File

@ -1,5 +1,5 @@
name: yesod-static
version: 0.3.1
version: 0.3.1.1
license: BSD3
license-file: LICENSE
author: Michael Snoyman <michael@snoyman.com>
@ -36,6 +36,7 @@ library
, file-embed >= 0.0.4.1 && < 0.5
, http-types >= 0.6.5 && < 0.7
, unix-compat >= 0.2 && < 0.3
, enumerator >= 0.4.14 && < 0.5
exposed-modules: Yesod.Static
ghc-options: -Wall