Set the MIME-Type using the Content-Type Header in Happstack
In an old thread on the HAppS group Morten Krogh points out that Happstack’s fileServe is very slow. He shows a small piece of example code that does things a different way:
import Happstack.Server
import qualified Data.ByteString.Lazy.Char8 as B
main = do
content <- B.readFile "index.html"
simpleHTTP nullConf {port=8002} $ return $ (toResponse "") {rsBody=content}
I tried to make this serve an image instead of an html file but it didn’t work ass expected. When navigating to localhost:8002 I was asked if I wanted to download a BIN file instead of beig shown the image.
The solution is to set the Content-Type header to match the MIME of the file you want to serve. The following code works for me:
import Happstack.Server
import qualified Data.ByteString.Lazy.Char8 as B
main = do
content <- B.readFile "hello.png"
simpleHTTP nullConf {port=8002} $ return $ (toResponse "")
{rsBody=content, rsHeaders=(mkHeaders [("Content-Type","image/png")])} 