@@ -27,6 +27,7 @@ module Network.HTTP.ReverseProxy
27
27
, wpsUpgradeToRaw
28
28
, wpsGetDest
29
29
, wpsLogRequest
30
+ , wpsModifyResponseHeaders
30
31
, SetIpHeader (.. )
31
32
-- *** Local settings
32
33
, LocalWaiProxySettings
@@ -272,6 +273,13 @@ data WaiProxySettings = WaiProxySettings
272
273
-- Default: no op
273
274
--
274
275
-- @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
275
283
}
276
284
277
285
-- | How to set the X-Real-IP request header.
@@ -294,6 +302,7 @@ defaultWaiProxySettings = WaiProxySettings
294
302
(CI. mk <$> lookup " upgrade" (WAI. requestHeaders req)) == Just " websocket"
295
303
, wpsGetDest = Nothing
296
304
, wpsLogRequest = const (pure () )
305
+ , wpsModifyResponseHeaders = \ _ _ -> id
297
306
}
298
307
299
308
renderHeaders :: WAI. Request -> HT. RequestHeaders -> Builder
@@ -422,9 +431,10 @@ waiProxyToSettings getDest wps' manager req0 sendResponse = do
422
431
$ \ case
423
432
Left e -> wpsOnExc wps e req sendResponse
424
433
Right res -> do
425
- let conduit = fromMaybe
434
+ let res' = const () <$> res
435
+ conduit = fromMaybe
426
436
(awaitForever (\ bs -> yield (Chunk $ fromByteString bs) >> yield Flush ))
427
- (wpsProcessBody wps req $ const () <$> res)
437
+ (wpsProcessBody wps req res' )
428
438
src = bodyReaderSource $ HC. responseBody res
429
439
headers = HC. responseHeaders res
430
440
notEncoded = isNothing (lookup " content-encoding" headers)
@@ -433,7 +443,7 @@ waiProxyToSettings getDest wps' manager req0 sendResponse = do
433
443
(HC. responseStatus res)
434
444
(filter (\ (key, v) -> not (key `Set.member` strippedHeaders) ||
435
445
key == " content-length" && (notEncoded && notChunked || v == " 0" ))
436
- headers)
446
+ (wpsModifyResponseHeaders wps req res' headers) )
437
447
(\ sendChunk flush -> runConduit $ src .| conduit .| CL. mapM_ (\ mb ->
438
448
case mb of
439
449
Flush -> flush
0 commit comments