Skip to content

Commit

Permalink
Rename proxyRequestModifier field to proxyHttpRequestModifier
Browse files Browse the repository at this point in the history
This helps remind users that only HTTP requests can be modified.
  • Loading branch information
erikd committed Apr 7, 2019
1 parent d899360 commit ecfe5de
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 18 deletions.
6 changes: 3 additions & 3 deletions Network/HTTP/Proxy.hs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ data Settings = Settings
-- application-generated applications to stderr.
, proxyTimeout :: Int
-- ^ Timeout value in seconds. Default value: 30
, proxyRequestModifier :: Request -> IO (Either Response Request)
, proxyHttpRequestModifier :: Request -> IO (Either Response Request)
-- ^ A function that allows the request to be modified before being run. Default: 'return . Right'.
-- This only works for unencrypted HTTP requests (eg to upgrade the request to HTTPS) because
-- HTTPS requests are encrypted.
Expand Down Expand Up @@ -154,7 +154,7 @@ defaultProxySettings = Settings
, proxyHost = "*"
, proxyOnException = defaultExceptionResponse
, proxyTimeout = 30
, proxyRequestModifier = return . Right
, proxyHttpRequestModifier = return . Right
, proxyLogger = const $ return ()
, proxyUpstream = Nothing
}
Expand All @@ -170,7 +170,7 @@ defaultExceptionResponse e =

httpProxyApp :: Settings -> HC.Manager -> Application
httpProxyApp settings mgr wreq respond = do
mwreq <- proxyRequestModifier settings $ proxyRequest wreq
mwreq <- proxyHttpRequestModifier settings $ proxyRequest wreq
either respond (doUpstreamRequest settings mgr respond . waiRequest wreq) mwreq


Expand Down
29 changes: 17 additions & 12 deletions example/request-rewrite-proxy.hs
Original file line number Diff line number Diff line change
@@ -1,22 +1,27 @@
{-# LANGUAGE OverloadedStrings #-}

import Network.HTTP.Proxy
import Network.HTTP.Proxy (ProxySettings (..), Request (..))
import qualified Network.HTTP.Proxy as Proxy

main :: IO ()
main = runProxySettings $ defaultProxySettings
{ proxyPort = 31081
, proxyRequestModifier = Just secureGoogle
}
main =
Proxy.runProxySettings $
Proxy.defaultProxySettings
{ proxyPort = 31081
, proxyHttpRequestModifier = Just secureGoogle
}

-- We can modify the request so that instead of going to unsecured Google
-- search page, people get redirected to the encrypted version.
-- Modifying the request like this is only possible for unencrypted HTTP connections
-- by my be useful for eg redirecting HTTP to HTTPS.
-- HTTPS cnnections cannot be modified like this because the for HTTPS connections
-- even the request itself is encrypted.

secureGoogle :: Request -> IO Request
secureGoogle req
| requestHost req == "www.google.com" =
return $ req
{ requestHost = "encrypted.google.com"
, requestPort = 443
| "www.google.com" `BS.isInfixOf` requestPath req
&& not ("https" `BS.isprefixOf` requestPath req =
pure $ req
{ requestPath = "encrypted.google.com"
}

| otherwise = return req
| otherwise = pure req
6 changes: 3 additions & 3 deletions test/test-io.hs
Original file line number Diff line number Diff line change
Expand Up @@ -173,14 +173,14 @@ withTestProxy settings expectation = do

proxySettingsAddHeader :: Settings
proxySettingsAddHeader = defaultProxySettings
{ proxyRequestModifier = \ req -> return . Right $ req
{ proxyHttpRequestModifier = \ req -> return . Right $ req
{ requestHeaders = (CI.mk "X-Test-Header", "Blah") : requestHeaders req
}
}

proxySettingsHttpsUpgrade :: Settings
proxySettingsHttpsUpgrade = defaultProxySettings
{ proxyRequestModifier = \ req -> return . Right $ req { requestPath = httpsUpgrade $ requestPath req }
{ proxyHttpRequestModifier = \ req -> return . Right $ req { requestPath = httpsUpgrade $ requestPath req }
}
where
httpsUpgrade bs =
Expand All @@ -191,7 +191,7 @@ proxySettingsHttpsUpgrade = defaultProxySettings

proxySettingsProxyResponse :: Settings
proxySettingsProxyResponse = defaultProxySettings
{ proxyRequestModifier = const . return $ Left proxyResponse
{ proxyHttpRequestModifier = const . return $ Left proxyResponse
}
where
proxyResponse :: Wai.Response
Expand Down

0 comments on commit ecfe5de

Please sign in to comment.