Skip to content

Commit 4bb364b

Browse files
committed
Vendor Data.ByteString.Char8.strip
This is for backwards compatibility, and allows us to continue building this package on LTS-16.
1 parent 120bbfb commit 4bb364b

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

Network/HTTP/ReverseProxy.hs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
{-# LANGUAGE LambdaCase #-}
77
{-# LANGUAGE TupleSections #-}
88
{-# LANGUAGE CPP #-}
9+
910
module Network.HTTP.ReverseProxy
1011
( -- * Types
1112
ProxyDest (..)
@@ -78,6 +79,23 @@ data ProxyDest = ProxyDest
7879
, pdPort :: !Int
7980
} deriving (Read, Show, Eq, Ord, Generic)
8081

82+
-- | Trim optional whitespace (OWS) from both ends of a strict 'ByteString'.
83+
84+
-- Per RFC 7230 §3.2.3, OWS = SP / HTAB. This function removes only leading
85+
-- and trailing space (0x20) and horizontal tab (0x09) bytes. It does not
86+
-- modify internal whitespace.
87+
88+
-- On newer @bytestring@ versions (those that provide
89+
-- 'Data.ByteString.Char8.strip') this is a thin wrapper around that
90+
-- function; on older versions we provide a compatible fallback.
91+
trimOWS :: S8.ByteString -> S8.ByteString
92+
#if MIN_VERSION_bytestring(0,12,0)
93+
trimOWS = S8.strip
94+
#else
95+
trimOWS = S8.reverse . S8.dropWhile isOWS . S8.reverse . S8.dropWhile isOWS
96+
where isOWS c = c == ' ' || c == '\t'
97+
#endif
98+
8199
-- | Set up a reverse proxy server, which will have a minimal overhead.
82100
--
83101
-- This function uses raw sockets, parsing as little of the request as
@@ -361,7 +379,7 @@ fixReqHeaders wps req =
361379
fromSocket = (("X-Real-IP", S8.pack $ showSockAddr $ WAI.remoteHost req):)
362380
fromForwardedFor = do
363381
h <- lookup "x-forwarded-for" (WAI.requestHeaders req)
364-
listToMaybe $ map S8.strip $ S8.split ',' h
382+
listToMaybe $ map trimOWS $ S8.split ',' h
365383
addXRealIP =
366384
case wpsSetIpHeader wps of
367385
SIHFromSocket -> fromSocket

0 commit comments

Comments
 (0)