diff --git a/Network/HTTP/Proxy.hs b/Network/HTTP/Proxy.hs index c206305..7a426bf 100644 --- a/Network/HTTP/Proxy.hs +++ b/Network/HTTP/Proxy.hs @@ -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. @@ -154,7 +154,7 @@ defaultProxySettings = Settings , proxyHost = "*" , proxyOnException = defaultExceptionResponse , proxyTimeout = 30 - , proxyRequestModifier = return . Right + , proxyHttpRequestModifier = return . Right , proxyLogger = const $ return () , proxyUpstream = Nothing } @@ -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 diff --git a/example/request-rewrite-proxy.hs b/example/request-rewrite-proxy.hs index 9ea96a0..a070937 100644 --- a/example/request-rewrite-proxy.hs +++ b/example/request-rewrite-proxy.hs @@ -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 diff --git a/test/test-io.hs b/test/test-io.hs index d81d07d..959f306 100644 --- a/test/test-io.hs +++ b/test/test-io.hs @@ -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 = @@ -191,7 +191,7 @@ proxySettingsHttpsUpgrade = defaultProxySettings proxySettingsProxyResponse :: Settings proxySettingsProxyResponse = defaultProxySettings - { proxyRequestModifier = const . return $ Left proxyResponse + { proxyHttpRequestModifier = const . return $ Left proxyResponse } where proxyResponse :: Wai.Response