Skip to content

/etc/mime.types: hIsEOF: illegal operation (handle is closed) #70

@mhitza

Description

@mhitza

I'm wrapping the initialization of the Data.MIME.Types.guessType call with the following function

import Data.MIME.Types         

makeMimeTypeGuesser :: IO (FilePath -> Maybe String)
makeMimeTypeGuesser = do
  system_mimetype <- readSystemMIMETypes defaultmtd
  pure $ \filepath ->          
    case guessType system_mimetype True filepath of
      (Nothing, _) -> Nothing  
      (r@(Just _), _) -> r

And I'm calling the function in a wai + warp context, where I get the exception in the issue title.

data Env = Env { guessMimeType :: FilePath -> Maybe String } -- a couple more lines to build the Env that are omitted

app :: Env -> Application
app env request respond = do
    cwd <- FS.getCurrentDirectory
    let path = (cwd <>) $ unpack $ rawPathInfo request
    exists <- FS.doesFileExist path
    respond $ if exists
      then case guessMimeType env path of
        Nothing -> responseLBS status415 [] "Unsupported media type"
        Just mimetype -> responseFile status200 [("Content-Type", pack mimetype)] path Nothing
      else responseLBS status404 [] "Not found"

I'm no expert in laziness, or Lazy IO for that matter, but if I where to guess it's because the underlying functions use foldls without any strictness in the accumulator.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions