Avoid resource exhaustion in yesod-static
This commit is contained in:
parent
4070b8e49a
commit
8e1d49aa59
@ -65,6 +65,9 @@ import qualified Data.ByteString as S
|
|||||||
import Network.HTTP.Types (status301)
|
import Network.HTTP.Types (status301)
|
||||||
import System.PosixCompat.Files (getFileStatus, modificationTime)
|
import System.PosixCompat.Files (getFileStatus, modificationTime)
|
||||||
import System.Posix.Types (EpochTime)
|
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
|
import Network.Wai.Application.Static
|
||||||
( StaticSettings (..)
|
( 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 :: Prelude.FilePath -> IO String
|
||||||
base64md5File file = do
|
base64md5File file = do
|
||||||
contents <- L.readFile file
|
bss <- E.run_ $ EB.enumFile file E.$$ EL.consume
|
||||||
return $ base64md5 contents
|
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
|
-- | md5-hashes the given lazy bytestring and returns the hash as
|
||||||
-- base64url-encoded string.
|
-- base64url-encoded string.
|
||||||
--
|
--
|
||||||
-- This function returns the first 8 characters of the hash.
|
-- This function returns the first 8 characters of the hash.
|
||||||
base64md5 :: L.ByteString -> String
|
base64md5 :: L.ByteString -> String
|
||||||
base64md5 = map tr
|
base64md5 = base64 . md5
|
||||||
. take 8
|
|
||||||
. S8.unpack
|
base64 :: MD5Digest -> String
|
||||||
. Data.ByteString.Base64.encode
|
base64 = map tr
|
||||||
. Data.Serialize.encode
|
. take 8
|
||||||
. md5
|
. S8.unpack
|
||||||
|
. Data.ByteString.Base64.encode
|
||||||
|
. Data.Serialize.encode
|
||||||
where
|
where
|
||||||
tr '+' = '-'
|
tr '+' = '-'
|
||||||
tr '/' = '_'
|
tr '/' = '_'
|
||||||
|
|||||||
@ -1,5 +1,5 @@
|
|||||||
name: yesod-static
|
name: yesod-static
|
||||||
version: 0.3.1
|
version: 0.3.1.1
|
||||||
license: BSD3
|
license: BSD3
|
||||||
license-file: LICENSE
|
license-file: LICENSE
|
||||||
author: Michael Snoyman <michael@snoyman.com>
|
author: Michael Snoyman <michael@snoyman.com>
|
||||||
@ -36,6 +36,7 @@ library
|
|||||||
, file-embed >= 0.0.4.1 && < 0.5
|
, file-embed >= 0.0.4.1 && < 0.5
|
||||||
, http-types >= 0.6.5 && < 0.7
|
, http-types >= 0.6.5 && < 0.7
|
||||||
, unix-compat >= 0.2 && < 0.3
|
, unix-compat >= 0.2 && < 0.3
|
||||||
|
, enumerator >= 0.4.14 && < 0.5
|
||||||
exposed-modules: Yesod.Static
|
exposed-modules: Yesod.Static
|
||||||
ghc-options: -Wall
|
ghc-options: -Wall
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user