Skip to content

Commit ecfe5de

Browse files
committed
Rename proxyRequestModifier field to proxyHttpRequestModifier
This helps remind users that only HTTP requests can be modified.
1 parent d899360 commit ecfe5de

File tree

3 files changed

+23
-18
lines changed

3 files changed

+23
-18
lines changed

Network/HTTP/Proxy.hs

+3-3
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ data Settings = Settings
115115
-- application-generated applications to stderr.
116116
, proxyTimeout :: Int
117117
-- ^ Timeout value in seconds. Default value: 30
118-
, proxyRequestModifier :: Request -> IO (Either Response Request)
118+
, proxyHttpRequestModifier :: Request -> IO (Either Response Request)
119119
-- ^ A function that allows the request to be modified before being run. Default: 'return . Right'.
120120
-- This only works for unencrypted HTTP requests (eg to upgrade the request to HTTPS) because
121121
-- HTTPS requests are encrypted.
@@ -154,7 +154,7 @@ defaultProxySettings = Settings
154154
, proxyHost = "*"
155155
, proxyOnException = defaultExceptionResponse
156156
, proxyTimeout = 30
157-
, proxyRequestModifier = return . Right
157+
, proxyHttpRequestModifier = return . Right
158158
, proxyLogger = const $ return ()
159159
, proxyUpstream = Nothing
160160
}
@@ -170,7 +170,7 @@ defaultExceptionResponse e =
170170

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

176176

example/request-rewrite-proxy.hs

+17-12
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,27 @@
11
{-# LANGUAGE OverloadedStrings #-}
22

3-
import Network.HTTP.Proxy
3+
import Network.HTTP.Proxy (ProxySettings (..), Request (..))
4+
import qualified Network.HTTP.Proxy as Proxy
45

56
main :: IO ()
6-
main = runProxySettings $ defaultProxySettings
7-
{ proxyPort = 31081
8-
, proxyRequestModifier = Just secureGoogle
9-
}
7+
main =
8+
Proxy.runProxySettings $
9+
Proxy.defaultProxySettings
10+
{ proxyPort = 31081
11+
, proxyHttpRequestModifier = Just secureGoogle
12+
}
1013

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

1419
secureGoogle :: Request -> IO Request
1520
secureGoogle req
16-
| requestHost req == "www.google.com" =
17-
return $ req
18-
{ requestHost = "encrypted.google.com"
19-
, requestPort = 443
21+
| "www.google.com" `BS.isInfixOf` requestPath req
22+
&& not ("https" `BS.isprefixOf` requestPath req =
23+
pure $ req
24+
{ requestPath = "encrypted.google.com"
2025
}
2126

22-
| otherwise = return req
27+
| otherwise = pure req

test/test-io.hs

+3-3
Original file line numberDiff line numberDiff line change
@@ -173,14 +173,14 @@ withTestProxy settings expectation = do
173173

174174
proxySettingsAddHeader :: Settings
175175
proxySettingsAddHeader = defaultProxySettings
176-
{ proxyRequestModifier = \ req -> return . Right $ req
176+
{ proxyHttpRequestModifier = \ req -> return . Right $ req
177177
{ requestHeaders = (CI.mk "X-Test-Header", "Blah") : requestHeaders req
178178
}
179179
}
180180

181181
proxySettingsHttpsUpgrade :: Settings
182182
proxySettingsHttpsUpgrade = defaultProxySettings
183-
{ proxyRequestModifier = \ req -> return . Right $ req { requestPath = httpsUpgrade $ requestPath req }
183+
{ proxyHttpRequestModifier = \ req -> return . Right $ req { requestPath = httpsUpgrade $ requestPath req }
184184
}
185185
where
186186
httpsUpgrade bs =
@@ -191,7 +191,7 @@ proxySettingsHttpsUpgrade = defaultProxySettings
191191

192192
proxySettingsProxyResponse :: Settings
193193
proxySettingsProxyResponse = defaultProxySettings
194-
{ proxyRequestModifier = const . return $ Left proxyResponse
194+
{ proxyHttpRequestModifier = const . return $ Left proxyResponse
195195
}
196196
where
197197
proxyResponse :: Wai.Response

0 commit comments

Comments
 (0)