Skip to content

Commit 08d3b8e

Browse files
authored
Merge pull request #48 from adinapoli/adinapoli/wpsModifyResponseHeaders
Add `wpsModifyResponseHeaders` function
2 parents 77a936e + 22c6027 commit 08d3b8e

File tree

3 files changed

+18
-4
lines changed

3 files changed

+18
-4
lines changed

ChangeLog.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11

2+
## 0.6.1.0
3+
4+
* Add the `wpsModifyResponseHeaders` option to `WaiProxySettings` to tweak response headers before they are returned upstream. [#48](https://github.com/fpco/http-reverse-proxy/pull/48)
5+
26
## 0.6.0.3
37

48
* Fix a regression introduced in 0.6.0.2: wrong 'Content-Length' header is preserved for responses with encoded content. [#47](https://github.com/fpco/http-reverse-proxy/pull/47)

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.1.0
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

http-reverse-proxy.cabal

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: http-reverse-proxy
2-
version: 0.6.0.3
2+
version: 0.6.1.0
33
synopsis: Reverse proxy HTTP requests, either over raw sockets or with WAI
44
description: Provides a simple means of reverse-proxying HTTP requests. The raw approach uses the same technique as leveraged by keter, whereas the WAI approach performs full request/response parsing via WAI and http-conduit.
55
homepage: https://github.com/fpco/http-reverse-proxy

0 commit comments

Comments
 (0)