Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion Network/HTTP/ReverseProxy.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE TupleSections #-}
{-# LANGUAGE CPP #-}

module Network.HTTP.ReverseProxy
( -- * Types
ProxyDest (..)
Expand Down Expand Up @@ -78,6 +79,23 @@ data ProxyDest = ProxyDest
, pdPort :: !Int
} deriving (Read, Show, Eq, Ord, Generic)

-- | Trim optional whitespace (OWS) from both ends of a strict 'ByteString'.

-- Per RFC 7230 §3.2.3, OWS = SP / HTAB. This function removes only leading
-- and trailing space (0x20) and horizontal tab (0x09) bytes. It does not
-- modify internal whitespace.

-- On newer @bytestring@ versions (those that provide
-- 'Data.ByteString.Char8.strip') this is a thin wrapper around that
-- function; on older versions we provide a compatible fallback.
trimOWS :: S8.ByteString -> S8.ByteString
#if MIN_VERSION_bytestring(0,12,0)
trimOWS = S8.strip
#else
trimOWS = S8.reverse . S8.dropWhile isOWS . S8.reverse . S8.dropWhile isOWS
where isOWS c = c == ' ' || c == '\t'
#endif

-- | Set up a reverse proxy server, which will have a minimal overhead.
--
-- This function uses raw sockets, parsing as little of the request as
Expand Down Expand Up @@ -361,7 +379,7 @@ fixReqHeaders wps req =
fromSocket = (("X-Real-IP", S8.pack $ showSockAddr $ WAI.remoteHost req):)
fromForwardedFor = do
h <- lookup "x-forwarded-for" (WAI.requestHeaders req)
listToMaybe $ map S8.strip $ S8.split ',' h
listToMaybe $ map trimOWS $ S8.split ',' h
addXRealIP =
case wpsSetIpHeader wps of
SIHFromSocket -> fromSocket
Expand Down
36 changes: 18 additions & 18 deletions http-reverse-proxy.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,23 @@ library

build-depends:
base >=4.11 && <5
, blaze-builder >=0.3
, bytestring >=0.9
, case-insensitive >=0.4
, conduit >=1.3
, conduit-extra
, containers
, http-client >=0.3
, http-types >=0.6
, network
, resourcet
, streaming-commons
, text >=0.11
, transformers
, unliftio >=0.2
, wai >=3.0
, wai-logger
, word8 >=0.0
, blaze-builder >=0.3 && <0.5
, bytestring >=0.9 && <0.13
, case-insensitive >=0.4 && <1.3
, conduit >=1.3 && <1.4
, conduit-extra <1.4
, containers <0.9
, http-client >=0.3 && <0.8
, http-types >=0.6 && <0.13
, network <3.3
, resourcet <1.4
, streaming-commons <0.3
, text >=0.11 && <2.2
, transformers <0.7
, unliftio >=0.2 && <0.3
, wai >=3.0 && <3.3
, wai-logger >=2.0 && <2.6
, word8 >=0.0 && <0.2

test-suite test
default-language: Haskell2010
Expand All @@ -71,4 +71,4 @@ test-suite test

source-repository head
type: git
location: git://github.com/fpco/http-reverse-proxy.git
location: https://github.com/fpco/http-reverse-proxy.git
Loading