on master help sign in
implemanting Date.
commit3198682e8c6cb8ab8833228afb3025eef3385d6e authorKazu Yamamoto <kazu@iij.ad.jp> dateTue 5 Nov 2013, 14:45 (13 years ago) parent71e6bd2
2 files changed+63−1
warp/Network/Wai/Handler/Warp/Date.hs+55 warp/warp.cabal+8−1
warp/Network/Wai/Handler/Warp/Date.hsnew file+55
@@ -0,0 +1,55 @@
1+{-# LANGUAGE CPP #-}
2+
3+module Network.Wai.Handler.Warp.Date (
4+ withDateCache
5+ , getDate
6+ , DateCache
7+ , GMTDate
8+ ) where
9+
10+import Control.Applicative
11+import Control.Concurrent
12+import Control.Exception
13+import Control.Monad
14+import Data.ByteString.Char8
15+import Data.IORef
16+
17+#if WINDOWS
18+import Data.Time
19+import System.Locale
20+#else
21+import Network.HTTP.Date
22+import System.Posix (epochTime)
23+#endif
24+
25+type GMTDate = ByteString
26+
27+data DateCache = DateCache (IORef GMTDate)
28+
29+withDateCache :: (DateCache -> IO a) -> IO a
30+withDateCache action = bracket initialize
31+ (\(t,_) -> killThread t)
32+ (\(_,dc) -> action dc)
33+
34+initialize :: IO (ThreadId, DateCache)
35+initialize = do
36+ dc <- DateCache <$> (getCurrentGMTDate >>= newIORef)
37+ t <- forkIO $ forever $ do
38+ threadDelay 1000000
39+ update dc
40+ return (t, dc)
41+
42+getDate :: DateCache -> IO GMTDate
43+getDate (DateCache ref) = readIORef ref
44+
45+update :: DateCache -> IO ()
46+update (DateCache ref) = getCurrentGMTDate >>= writeIORef ref
47+
48+getCurrentGMTDate :: IO GMTDate
49+#ifdef WINDOWS
50+getCurrentGMTDate = formatDate <$> getCurrentTime
51+ where
52+ formatDate = pack . formatTime defaultTimeLocale "%a, %d %b %Y %H:%M:%S GMT"
53+#else
54+getCurrentGMTDate = formatHTTPDate . epochTimeToHTTPDate <$> epochTime
55+#endif
warp/warp.cabal+8−1
@@ -55,6 +55,7 @@ Library
5555 Exposed-modules: Network.Wai.Handler.Warp
5656 Network.Wai.Handler.Warp.Timeout
5757 Other-modules: Network.Wai.Handler.Warp.Conduit
58+ Network.Wai.Handler.Warp.Date
5859 Network.Wai.Handler.Warp.FdCache
5960 Network.Wai.Handler.Warp.Header
6061 Network.Wai.Handler.Warp.ReadInt
@@ -76,8 +77,11 @@ Library
7677 Other-modules: Network.Wai.Handler.Warp.MultiMap
7778 if os(windows)
7879 Cpp-Options: -DWINDOWS
80+ Build-Depends: time
81+ , old-locale
7982 else
8083 Build-Depends: unix
84+ , http-date
8185
8286Test-Suite doctest
8387 Type: exitcode-stdio-1.0
@@ -126,6 +130,8 @@ Test-Suite spec
126130 , QuickCheck
127131 , hspec >= 1.3
128132 , resourcet
133+ , time
134+ , old-locale
129135
130136 -- Yes, this means that the test suite will no longer work on Windows.
131137 -- Unfortunately there is a bug in older versions of cabal, and this conditional
@@ -133,7 +139,8 @@ Test-Suite spec
133139 --if (os(linux) || os(freebsd) || os(darwin)) && flag(allow-sendfilefd)
134140 Cpp-Options: -DSENDFILEFD
135141 Build-Depends: unix
136− , hashable
142+ , hashable
143+ , http-date
137144 --if os(windows)
138145 -- Cpp-Options: -DWINDOWS
139146