Correct pipeline semantics.

master
vi 2014-08-24 12:40:06 +08:00
parent c6c5eae311
commit 5af781291f
3 changed files with 21 additions and 17 deletions

8
ref/pipes.txt Normal file
View File

@ -0,0 +1,8 @@
| Upstream output | Upstream input | Downstream input | Downstream output | Base monad | Return value
Proxy a' a b' b m r | a' | a | b' | b | m | r
Effect m r | Void | () | () | Void | m | r
Pipe a b m r | () | a | () | b | m | r
Producer b m r | Void | () | () | b | m | r
Consumer a m r | () | a | () | Void | m | r
Client a' a m r | a' | a | () | Void | m | r
Server b' b m r | Void | () | b' | b | m | r

View File

@ -3,23 +3,19 @@ module Data.HTTPSEverywhere.Rules (
rewriteCookie rewriteCookie
) where ) where
import Prelude hiding (take, null) import Prelude hiding (null, head)
import Control.Applicative ((<$)) import Control.Lens ((<&>),(&))
import Control.Lens ((<&>))
import Data.Bool (bool) import Data.Bool (bool)
import Network.HTTP.Client (Cookie) import Network.HTTP.Client (Cookie)
import Network.URI (URI) import Network.URI (URI)
import Pipes (runEffect, (>->)) import Pipes ((>->))
import Pipes.Prelude (take, null) import Pipes.Prelude (head, null)
import Control.Monad (join)
import Data.HTTPSEverywhere.Rules.Internal (getRulesetsMatching, havingRulesThatTrigger, havingCookieRulesThatTrigger, setSecureFlag) import Data.HTTPSEverywhere.Rules.Internal (getRulesetsMatching, havingRulesThatTrigger, havingCookieRulesThatTrigger, setSecureFlag)
rewriteURL :: URI -> IO (Maybe URI) rewriteURL :: URI -> IO (Maybe URI)
rewriteURL url = runEffect rewriteURL url = getRulesetsMatching url >-> havingRulesThatTrigger url & head <&> join
$ (Nothing <$ getRulesetsMatching url)
>-> (Nothing <$ havingRulesThatTrigger url)
>-> (Nothing <$ take 1)
rewriteCookie :: URI -> Cookie -> IO Cookie rewriteCookie :: URI -> Cookie -> IO Cookie
rewriteCookie url cookie = null producer <&> setSecureFlag cookie `bool` cookie rewriteCookie url cookie = null producer <&> setSecureFlag cookie `bool` cookie
where producer = getRulesetsMatching url >-> (() <$ havingCookieRulesThatTrigger cookie) where producer = getRulesetsMatching url >-> havingCookieRulesThatTrigger cookie

View File

@ -23,7 +23,7 @@ import Data.Maybe (isJust)
import qualified Data.HTTPSEverywhere.Rules.Raw as Raw (getRule, getRules) import qualified Data.HTTPSEverywhere.Rules.Raw as Raw (getRule, getRules)
import Network.HTTP.Client (Cookie(..)) import Network.HTTP.Client (Cookie(..))
import Network.URI (URI) import Network.URI (URI)
import Pipes (Producer, Consumer, for, each, await, yield, lift, (>->)) import Pipes (Pipe, Producer, for, each, await, yield, lift, (>->))
import Pipes.Prelude (filter) import Pipes.Prelude (filter)
import Data.HTTPSEverywhere.Rules.Internal.Parser (parseRuleSets) import Data.HTTPSEverywhere.Rules.Internal.Parser (parseRuleSets)
@ -38,13 +38,13 @@ getRulesetsMatching url = getRulesets
>-> filter (flip hasTargetMatching url) >-> filter (flip hasTargetMatching url)
>-> filter (not . flip hasExclusionMatching url) >-> filter (not . flip hasExclusionMatching url)
havingRulesThatTrigger :: URI -> Consumer RuleSet IO (Maybe URI) havingRulesThatTrigger :: URI -> Pipe RuleSet (Maybe URI) IO ()
havingRulesThatTrigger url = flip hasTriggeringRuleOn url <$> await havingRulesThatTrigger url = flip hasTriggeringRuleOn url <$> await
>>= maybe (havingRulesThatTrigger url) (return . Just) >>= maybe (havingRulesThatTrigger url) (yield . Just)
havingCookieRulesThatTrigger :: Cookie -> Consumer RuleSet IO Bool havingCookieRulesThatTrigger :: Cookie -> Pipe RuleSet Bool IO ()
havingCookieRulesThatTrigger cookie = flip hasTriggeringCookieRuleOn cookie <$> await havingCookieRulesThatTrigger cookie = flip hasTriggeringCookieRuleOn cookie <$> await
>>= bool (havingCookieRulesThatTrigger cookie) (return True) >>= bool (havingCookieRulesThatTrigger cookie) (yield True)
hasTargetMatching :: RuleSet -> URI -> Bool hasTargetMatching :: RuleSet -> URI -> Bool
hasTargetMatching ruleset url = getTargets ruleset <*> [url] & or hasTargetMatching ruleset url = getTargets ruleset <*> [url] & or