diff options
Diffstat (limited to 'mini-http-server/Network/HTTP/Server/Mini/Printer.hs')
| -rw-r--r-- | mini-http-server/Network/HTTP/Server/Mini/Printer.hs | 14 |
1 files changed, 11 insertions, 3 deletions
diff --git a/mini-http-server/Network/HTTP/Server/Mini/Printer.hs b/mini-http-server/Network/HTTP/Server/Mini/Printer.hs index 77b88ce..442d768 100644 --- a/mini-http-server/Network/HTTP/Server/Mini/Printer.hs +++ b/mini-http-server/Network/HTTP/Server/Mini/Printer.hs @@ -5,19 +5,27 @@ import Data.ByteString (ByteString) import Data.ByteString qualified as BS import Data.ByteString.Lazy qualified as BSL import Data.ByteString.Builder qualified as BSB +import GHC.IO.Exception (IOException(..), IOErrorType(..)) import Network.Socket import Network.Socket.ByteString +import System.IO.Error (catchIOError) import Network.HTTP.Server.Mini.Types -import System.IO.Error (catchIOError) sendResponse :: Socket -> Response -> IO () sendResponse conn (Response status hdrs (BodyLBS body)) = sendResponseChunks conn status hdrs body -sendResponse conn (Response status hdrs (BodyFile path)) = +sendResponse conn (Response status hdrs (BodyFile path)) = do -- lazy IO is fine here because the whole thing is traversed before the end of this function in sendMany - sendResponseChunks conn status hdrs =<< BSL.readFile path + elazyContents <- + catchIOError (Right <$> BSL.readFile path) + (\e -> case ioe_type e of + NoSuchThing -> return (Left (status404, "File not found")) + _ -> return (Left (status500, "Something went wrong"))) + case elazyContents of + Right lazyContents -> sendResponseChunks conn status hdrs lazyContents + Left (status', text) -> sendResponseChunks conn status' [] text sendResponseChunks :: Socket -> Status -> [(Header, ByteString)] -> BSL.ByteString -> IO () sendResponseChunks conn (Status code reason) hdrs body = |
