From d2e10b83065523801aee2d2b1bd2c86f27a8a962 Mon Sep 17 00:00:00 2001 From: Gregor Kleen Date: Wed, 1 May 2019 19:14:08 +0200 Subject: [PATCH] Fix tests & restore Common handlers --- src/Handler/Common.hs | 26 +++++++++++++++++++++----- src/Utils.hs | 4 +--- 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/src/Handler/Common.hs b/src/Handler/Common.hs index de1e706e0..f11a76cfb 100644 --- a/src/Handler/Common.hs +++ b/src/Handler/Common.hs @@ -1,10 +1,26 @@ -- | Common handler functions. module Handler.Common where -import Import +import Data.FileEmbed (embedFile) +import Import hiding (embedFile) -getFaviconR :: Handler () -getFaviconR = redirectWith movedPermanently301 $ StaticR favicon_ico +-- These handlers embed files in the executable at compile time to avoid a +-- runtime dependency, and for efficiency. -getRobotsR :: Handler () -getRobotsR = redirectWith movedPermanently301 $ StaticR robots_txt +getFaviconR :: Handler TypedContent +getFaviconR = do + let content = $(embedFile "static/favicon.ico") + + setEtagHashable content + + return $ TypedContent "image/x-icon" + $ toContent content + +getRobotsR :: Handler TypedContent +getRobotsR = do + let content = $(embedFile "static/robots.txt") + + setEtagHashable content + + return $ TypedContent typePlain + $ toContent content diff --git a/src/Utils.hs b/src/Utils.hs index bb2efad89..0d656c1fd 100644 --- a/src/Utils.hs +++ b/src/Utils.hs @@ -73,8 +73,6 @@ import Data.Ratio ((%)) import qualified Data.Binary as Binary -import qualified Data.ByteString.Base64.URL as Base64 (encode) - import Network.Wai (requestMethod) import Data.Time.Clock @@ -778,7 +776,7 @@ setLastModified lastModified = do when (rMethod `elem` safeMethods) $ do ifModifiedSince <- (=<<) (parseTimeM True defaultTimeLocale "%a, %d %b %Y %X %Z" . unpack <=< either (const Nothing) Just . Text.decodeUtf8') <$> lookupHeader "If-Modified-Since" $logDebugS "LastModified" $ tshow (lastModified, ifModifiedSince) - when (maybe False ((lastModified <=) . addUTCTime precision) ifModifiedSince) $ + when (maybe False ((lastModified <=) . addUTCTime precision) ifModifiedSince) notModified addHeader "Last-Modified" $ formatRFC1123 lastModified