on master help sign in
Add interceptWith for wai-websockets
commitf14bb805db85107d229fc05f170aa1cb663cabf2 authorJasper Van der Jeugt <m@jaspervdj.be> dateFri 13 Jan 2012, 13:08 (15 years ago) parentbd340ff
2 files changed+17−7
wai-websockets/Network/Wai/Handler/WebSockets.hs+15−5 wai-websockets/wai-websockets.cabal+2−2
wai-websockets/Network/Wai/Handler/WebSockets.hs+15−5
@@ -1,6 +1,7 @@
11{-# LANGUAGE OverloadedStrings #-}
22module Network.Wai.Handler.WebSockets
33 ( intercept
4+ , interceptWith
45 ) where
56
67import Control.Monad.IO.Class (liftIO)
@@ -19,9 +20,17 @@ intercept :: WS.Protocol p
1920 => (WS.Request -> WS.WebSockets p ())
2021 -> Request
2122 -> Maybe (C.BufferedSource IO ByteString -> Socket -> C.ResourceT IO ())
22−intercept app req = case lookup "upgrade" $ requestHeaders req of
23+intercept = interceptWith WS.defaultWebSocketsOptions
24+
25+-- | Variation of 'intercept' which allows custom options.
26+interceptWith :: WS.Protocol p
27+ => WS.WebSocketsOptions
28+ -> (WS.Request -> WS.WebSockets p ())
29+ -> Request
30+ -> Maybe (C.BufferedSource IO ByteString -> Socket -> C.ResourceT IO ())
31+interceptWith opts app req = case lookup "upgrade" $ requestHeaders req of
2332 Just s
24− | S.map toLower s == "websocket" -> Just $ runWebSockets req' app
33+ | S.map toLower s == "websocket" -> Just $ runWebSockets opts req' app
2534 | otherwise -> Nothing
2635 _ -> Nothing
2736 where
@@ -29,13 +38,14 @@ intercept app req = case lookup "upgrade" $ requestHeaders req of
2938
3039-- | Internal function to run the WebSocket iteratee using the conduit library
3140runWebSockets :: WS.Protocol p
32− => WS.RequestHttpPart
41+ => WS.WebSocketsOptions
42+ -> WS.RequestHttpPart
3343 -> (WS.Request -> WS.WebSockets p ())
3444 -> C.BufferedSource IO ByteString
3545 -> Socket
3646 -> C.ResourceT IO ()
37−runWebSockets req app source sock = do
38− step <- liftIO $ E.runIteratee $ WS.runWebSockets req app send
47+runWebSockets opts req app source sock = do
48+ step <- liftIO $ E.runIteratee $ WS.runWebSocketsWith opts req app send
3949 source C.$$ C.sinkState (E.returnI step) push close
4050 where
4151 send = WS.iterSocket sock
wai-websockets/wai-websockets.cabal+2−2
@@ -1,9 +1,9 @@
11Name: wai-websockets
2−Version: 1.1.0
2+Version: 1.1.1
33Synopsis: Provide a bridge betweeen WAI and the websockets package.
44License: BSD3
55License-file: LICENSE
6−Author: Michael Snoyman
6+Author: Michael Snoyman, Jasper Van der Jeugt
77Maintainer: michael@snoyman.com
88Homepage: http://github.com/yesodweb/wai
99Category: Web, Yesod