Skip to content

Commit acc23b9

Browse files
committed
Add wpsModifyResponseHeaders function
This commit adds a new function to the `WaiProxySettings` that allows modifying the response headers before they are sent upstream.
1 parent 77a936e commit acc23b9

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

Network/HTTP/ReverseProxy.hs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ module Network.HTTP.ReverseProxy
2727
, wpsUpgradeToRaw
2828
, wpsGetDest
2929
, wpsLogRequest
30+
, wpsModifyResponseHeaders
3031
, SetIpHeader (..)
3132
-- *** Local settings
3233
, LocalWaiProxySettings
@@ -272,6 +273,13 @@ data WaiProxySettings = WaiProxySettings
272273
-- Default: no op
273274
--
274275
-- @since 0.6.0.1
276+
, wpsModifyResponseHeaders :: WAI.Request -> HC.Response () -> HT.ResponseHeaders -> HT.ResponseHeaders
277+
-- ^ Allow to override the response headers before the response is returned upstream. Useful for example
278+
-- to override overly-strict 'Content-Security-Policy' when the source is known to be trustworthy.
279+
--
280+
-- Default: no op
281+
--
282+
-- @since 0.6.0.4
275283
}
276284

277285
-- | How to set the X-Real-IP request header.
@@ -294,6 +302,7 @@ defaultWaiProxySettings = WaiProxySettings
294302
(CI.mk <$> lookup "upgrade" (WAI.requestHeaders req)) == Just "websocket"
295303
, wpsGetDest = Nothing
296304
, wpsLogRequest = const (pure ())
305+
, wpsModifyResponseHeaders = \_ _ -> id
297306
}
298307

299308
renderHeaders :: WAI.Request -> HT.RequestHeaders -> Builder
@@ -422,9 +431,10 @@ waiProxyToSettings getDest wps' manager req0 sendResponse = do
422431
$ \case
423432
Left e -> wpsOnExc wps e req sendResponse
424433
Right res -> do
425-
let conduit = fromMaybe
434+
let res' = const () <$> res
435+
conduit = fromMaybe
426436
(awaitForever (\bs -> yield (Chunk $ fromByteString bs) >> yield Flush))
427-
(wpsProcessBody wps req $ const () <$> res)
437+
(wpsProcessBody wps req res')
428438
src = bodyReaderSource $ HC.responseBody res
429439
headers = HC.responseHeaders res
430440
notEncoded = isNothing (lookup "content-encoding" headers)
@@ -433,7 +443,7 @@ waiProxyToSettings getDest wps' manager req0 sendResponse = do
433443
(HC.responseStatus res)
434444
(filter (\(key, v) -> not (key `Set.member` strippedHeaders) ||
435445
key == "content-length" && (notEncoded && notChunked || v == "0"))
436-
headers)
446+
(wpsModifyResponseHeaders wps req res' headers))
437447
(\sendChunk flush -> runConduit $ src .| conduit .| CL.mapM_ (\mb ->
438448
case mb of
439449
Flush -> flush

0 commit comments

Comments
 (0)