Hello, I'm having a lot of trouble sending binary files through a socket (I'm trying to program a very minimalistic webserver) but I just can't find a way to read and send binary files, even though, plain text and html documents are sent correctly.
My code looks something like this:
sendResource handle name =
do exist <- doesFileExist name
isFile <- liftM not (doesDirectoryExist name)
if exist && isFile
then do resource <- Data.ByteString.readFile name
hPutStr handle (header ++ fileType ++ "\n")
hSetBinaryMode handle True
hPutStr handle resource
else do hPutStr handle page404
where fileType = maybe ("text/plain") (flip const [])
(lookup (takeExtension name) extensions)
Thanks for your help in advance.
PS: Yes, I'm terribly newbie to Haskell.