@@ -8,7 +8,8 @@ import Prelude hiding (log)
8import Control.Monad (forever)
8import Control.Monad (forever)
9import Control.Monad.Reader (ReaderT, runReaderT, ask, liftIO)
9import Control.Monad.Reader (ReaderT, runReaderT, ask, liftIO)
10import Network
10import Network
11−import System.IO
11+import System.IO (stderr, Handle, IOMode(..), openFile, hFileSize, hClose)
12+import qualified System.IO.UTF8 as U
12import System.Directory (doesFileExist, doesDirectoryExist)
13import System.Directory (doesFileExist, doesDirectoryExist)
13import Control.Concurrent (forkIO)
14import Control.Concurrent (forkIO)
14import Control.Concurrent.Chan (Chan, newChan, readChan, writeChan)
15import Control.Concurrent.Chan (Chan, newChan, readChan, writeChan)
@@ -21,7 +22,7 @@ import Text.Hakyll.Regex
22-- | Function to log from a chan.
23-- | Function to log from a chan.
23log :: Chan String -> IO ()
24log :: Chan String -> IO ()
24−log logChan = forever (readChan logChan >>= hPutStrLn stderr)
25+log logChan = forever (readChan logChan >>= U.hPutStrLn stderr)
26-- | General server configuration.
27-- | General server configuration.
27data ServerConfig = ServerConfig { documentRoot :: FilePath
28data ServerConfig = ServerConfig { documentRoot :: FilePath
@@ -47,7 +48,7 @@ instance Show Request where
47-- headers and body.
48-- headers and body.
48readRequest :: Handle -> Server Request
49readRequest :: Handle -> Server Request
49readRequest handle = do
50readRequest handle = do
50− requestLine <- liftIO $ hGetLine handle
51+ requestLine <- liftIO $ U.hGetLine handle
51 let [method, uri, version] = map trim $ splitRegex " " requestLine
52 let [method, uri, version] = map trim $ splitRegex " " requestLine
52 return $ Request { requestMethod = method
53 return $ Request { requestMethod = method
53 , requestURI = uri
54 , requestURI = uri
@@ -147,7 +148,7 @@ getMIMEHeader fileName =
147 result = lookup (takeExtension fileName) [ (".css", "text/css")
148 result = lookup (takeExtension fileName) [ (".css", "text/css")
148 , (".gif", "image/gif")
149 , (".gif", "image/gif")
149 , (".htm", "text/html")
150 , (".htm", "text/html")
150− , (".html", "text/html")
151+ , (".html", "text/html; charset=utf8")
151 , (".jpeg", "image/jpeg")
152 , (".jpeg", "image/jpeg")
152 , (".jpg", "image/jpeg")
153 , (".jpg", "image/jpeg")
153 , (".js", "text/javascript")
154 , (".js", "text/javascript")
@@ -171,21 +172,21 @@ respond handle = do
171 -- Send the response back to the handle.
172 -- Send the response back to the handle.
172 liftIO $ putResponse response
173 liftIO $ putResponse response
174− putResponse response = do hPutStr handle $ intercalate " "
175+ putResponse response = do U.hPutStr handle $ intercalate " "
175 [ responseVersion response
176 [ responseVersion response
176 , show $ responseStatusCode response
177 , show $ responseStatusCode response
177 , responsePhrase response
178 , responsePhrase response
179− hPutStr handle "\r\n"
180+ U.hPutStr handle "\r\n"
180 mapM_ putHeader
181 mapM_ putHeader
181 (M.toList $ responseHeaders response)
182 (M.toList $ responseHeaders response)
182− hPutStr handle "\r\n"
183+ U.hPutStr handle "\r\n"
183− hPutStr handle $ responseBody response
184+ U.hPutStr handle $ responseBody response
184− hPutStr handle "\r\n"
185+ U.hPutStr handle "\r\n"
185 hClose handle
186 hClose handle
187 putHeader (key, value) =
188 putHeader (key, value) =
188− hPutStr handle $ key ++ ": " ++ value ++ "\r\n"
189+ U.hPutStr handle $ key ++ ": " ++ value ++ "\r\n"
190-- | Start a simple http server on the given 'PortNumber', serving the given
191-- | Start a simple http server on the given 'PortNumber', serving the given
191-- directory.
192-- directory.