on master help sign in
Implemented feedback from @pbrisbin
commit731c328fae67daa772609af298536bc9e2974534 authorMichael Snoyman <michael@snoyman.com> dateThu 9 Apr 2015, 17:23 (12 years ago) parentfb6823f
2 files changed+29−11
wai-extra/Network/Wai/Middleware/Approot.hs+13−10 wai-extra/Network/Wai/Request.hs+16−1
wai-extra/Network/Wai/Middleware/Approot.hs+13−10
@@ -24,6 +24,7 @@ module Network.Wai.Middleware.Approot
2424 , fromRequest
2525 -- * Functions for applications
2626 , getApproot
27+ , getApprootMay
2728 ) where
2829
2930import Control.Exception (Exception, throw)
@@ -34,7 +35,7 @@ import Data.Maybe (fromMaybe)
3435import Data.Typeable (Typeable)
3536import qualified Data.Vault.Lazy as V
3637import Network.Wai (Request, vault, Middleware, requestHeaderHost)
37−import Network.Wai.Request (appearsSecure)
38+import Network.Wai.Request (appearsSecure, guessApproot)
3839import System.Environment (getEnvironment)
3940import System.IO.Unsafe (unsafePerformIO)
4041
@@ -100,21 +101,23 @@ hardcoded ar = approotMiddleware (const $ return ar)
100101--
101102-- Since 3.0.7
102103fromRequest :: Middleware
103−fromRequest = approotMiddleware (return . extract)
104−
105−extract :: Request -> ByteString
106−extract req =
107− (if appearsSecure req then "https://" else "http://") `S.append`
108− (fromMaybe "localhost" (requestHeaderHost req))
104+fromRequest = approotMiddleware (return . guessApproot)
109105
110106data ApprootMiddlewareNotSetup = ApprootMiddlewareNotSetup
111107 deriving (Show, Typeable)
112108instance Exception ApprootMiddlewareNotSetup
113109
114110-- | Get the approot set by the middleware. If the middleware is not in use,
115−-- then this function will return an exception.
111+-- then this function will return an exception. For a total version of the
112+-- function, see 'getApprootMay'.
116113--
117114-- Since 3.0.7
118115getApproot :: Request -> ByteString
119−getApproot req = fromMaybe (throw ApprootMiddlewareNotSetup)
120− $ V.lookup approotKey $ vault req
116+getApproot = fromMaybe (throw ApprootMiddlewareNotSetup) . getApprootMay
117+
118+-- | A total version of 'getApproot', which returns 'Nothing' if the middleware
119+-- is not in use.
120+--
121+-- Since 3.0.7
122+getApprootMay :: Request -> Maybe ByteString
123+getApprootMay req = V.lookup approotKey $ vault req
wai-extra/Network/Wai/Request.hs+16−1
@@ -3,12 +3,15 @@
33
44module Network.Wai.Request
55 ( appearsSecure
6+ , guessApproot
67 ) where
78
89import Data.ByteString (ByteString)
10+import Data.Maybe (fromMaybe)
911import Network.HTTP.Types (HeaderName)
10−import Network.Wai (Request, isSecure, requestHeaders)
12+import Network.Wai (Request, isSecure, requestHeaders, requestHeaderHost)
1113
14+import qualified Data.ByteString as S
1215import qualified Data.ByteString.Char8 as C
1316
1417-- | Does this request appear to have been made over an SSL connection?
@@ -34,3 +37,15 @@ appearsSecure request = isSecure request || any (uncurry matchHeader)
3437 where
3538 matchHeader :: HeaderName -> (ByteString -> Bool) -> Bool
3639 matchHeader h f = maybe False f $ lookup h $ requestHeaders request
40+
41+-- | Guess the \"application root\" based on the given request.
42+--
43+-- The application root is the basis for forming URLs pointing at the current
44+-- application. For more information and relevant caveats, please see
45+-- "Network.Wai.Middleware.Approot".
46+--
47+-- Since 3.0.7
48+guessApproot :: Request -> ByteString
49+guessApproot req =
50+ (if appearsSecure req then "https://" else "http://") `S.append`
51+ (fromMaybe "localhost" $ requestHeaderHost req)