module Network.HTTP.ReverseProxy
(
ProxyDest (..)
, rawProxyTo
, waiProxyTo
, defaultOnExc
, waiProxyToSettings
, WaiProxyResponse (..)
, WaiProxySettings
, def
, wpsOnExc
, wpsTimeout
, wpsSetIpHeader
, wpsProcessBody
, SetIpHeader (..)
, waiToRaw
) where
import BasicPrelude
import Data.Conduit
import qualified Network.Wai as WAI
import qualified Network.HTTP.Conduit as HC
import Control.Exception.Lifted (try, finally)
import Blaze.ByteString.Builder (fromByteString, flush)
import Data.Word8 (isSpace, _colon, toLower, _cr)
import qualified Data.ByteString as S
import qualified Data.ByteString.Char8 as S8
import qualified Network.HTTP.Types as HT
import qualified Data.CaseInsensitive as CI
import qualified Data.Text.Encoding as TE
import qualified Data.Text.Lazy.Encoding as TLE
import qualified Data.Text.Lazy as TL
import qualified Data.Conduit.Network as DCN
import Control.Concurrent.MVar.Lifted (newEmptyMVar, putMVar, takeMVar)
import Control.Concurrent.Lifted (fork, killThread)
import Control.Monad.Trans.Control (MonadBaseControl)
import Network.Wai.Handler.Warp
import Data.Conduit.Binary (sourceFileRange)
import qualified Data.IORef as I
import Network.Socket (PortNumber (PortNum), SockAddr (SockAddrInet))
import Data.Default (Default (def))
import Data.Version (showVersion)
import qualified Paths_http_reverse_proxy
import Network.Wai.Logger.Utils (showSockAddr)
import Blaze.ByteString.Builder (Builder)
import qualified Data.Set as Set
data ProxyDest = ProxyDest
{ pdHost :: !ByteString
, pdPort :: !Int
}
rawProxyTo :: (MonadBaseControl IO m, MonadIO m)
=> (HT.RequestHeaders -> m (Either (DCN.Application m) ProxyDest))
-> DCN.Application m
rawProxyTo getDest appdata = do
(rsrc, headers) <- fromClient $$+ getHeaders
edest <- getDest headers
case edest of
Left app -> do
(fromClient', _) <- unwrapResumable rsrc
app appdata { DCN.appSource = fromClient' }
Right (ProxyDest host port) -> DCN.runTCPClient (DCN.clientSettings port host) (withServer rsrc)
where
fromClient = DCN.appSource appdata
toClient = DCN.appSink appdata
withServer rsrc appdataServer = do
x <- newEmptyMVar
tid1 <- fork $ (rsrc $$+- toServer) `finally` putMVar x True
tid2 <- fork $ (fromServer $$ toClient) `finally` putMVar x False
y <- takeMVar x
killThread $ if y then tid2 else tid1
where
fromServer = DCN.appSource appdataServer
toServer = DCN.appSink appdataServer
defaultOnExc :: SomeException -> WAI.Application
defaultOnExc exc _ = return $ WAI.responseLBS
HT.status502
[("content-type", "text/plain")]
("Error connecting to gateway:\n\n" ++ TLE.encodeUtf8 (TL.fromStrict $ show exc))
data WaiProxyResponse = WPRResponse WAI.Response
| WPRProxyDest ProxyDest
| WPRModifiedRequest WAI.Request ProxyDest
waiProxyTo :: (WAI.Request -> ResourceT IO WaiProxyResponse)
-> (SomeException -> WAI.Application)
-> HC.Manager
-> WAI.Application
waiProxyTo getDest onError = waiProxyToSettings getDest def { wpsOnExc = onError }
data WaiProxySettings = WaiProxySettings
{ wpsOnExc :: SomeException -> WAI.Application
, wpsTimeout :: Maybe Int
, wpsSetIpHeader :: SetIpHeader
, wpsProcessBody :: HC.Response () -> Maybe (Conduit ByteString (ResourceT IO) (Flush Builder))
}
data SetIpHeader = SIHNone
| SIHFromSocket
| SIHFromHeader
instance Default WaiProxySettings where
def = WaiProxySettings
{ wpsOnExc = defaultOnExc
, wpsTimeout = Nothing
, wpsSetIpHeader = SIHFromSocket
, wpsProcessBody = const Nothing
}
waiProxyToSettings getDest wps manager req0 = do
edest' <- getDest req0
let edest =
case edest' of
WPRResponse res -> Left res
WPRProxyDest pd -> Right (pd, req0)
WPRModifiedRequest req pd -> Right (pd, req)
case edest of
Left response -> return response
Right (ProxyDest host port, req) -> do
let req' = HC.def
{ HC.method = WAI.requestMethod req
, HC.host = host
, HC.port = port
, HC.path = WAI.rawPathInfo req
, HC.queryString = WAI.rawQueryString req
, HC.requestHeaders = filter (\(key, _) -> not $ key `Set.member` strippedHeaders) $
(case wpsSetIpHeader wps of
SIHFromSocket -> (("X-Real-IP", S8.pack $ showSockAddr $ WAI.remoteHost req):)
SIHFromHeader ->
case lookup "x-real-ip" (WAI.requestHeaders req) <|> lookup "X-Forwarded-For" (WAI.requestHeaders req) of
Nothing -> id
Just ip -> (("X-Real-IP", ip):)
SIHNone -> id)
$ WAI.requestHeaders req
, HC.requestBody = body
, HC.redirectCount = 0
#if MIN_VERSION_http_conduit(1, 9, 0)
, HC.checkStatus = \_ _ _ -> Nothing
#else
, HC.checkStatus = \_ _ -> Nothing
#endif
, HC.responseTimeout = wpsTimeout wps
}
fbs bs = fromByteString bs <> flush
bodySrc = mapOutput fbs $ WAI.requestBody req
bodyChunked = HC.RequestBodySourceChunked bodySrc
#if MIN_VERSION_wai(1, 4, 0)
body =
case WAI.requestBodyLength req of
WAI.KnownLength i -> HC.RequestBodySource
(fromIntegral i)
bodySrc
WAI.ChunkedBody -> bodyChunked
#else
body = bodyChunked
#endif
ex <- try $ HC.http req' manager
case ex of
Left e -> wpsOnExc wps e req
Right res -> do
(src, _) <- unwrapResumable $ HC.responseBody res
let conduit =
case wpsProcessBody wps $ fmap (const ()) res of
Nothing -> awaitForever (\bs -> yield (Chunk $ fromByteString bs) >> yield Flush)
Just conduit -> conduit
return $ WAI.ResponseSource
(HC.responseStatus res)
(filter (\(key, _) -> not $ key `Set.member` strippedHeaders) $ HC.responseHeaders res) $ do
yield Flush
src =$= conduit
where
strippedHeaders = Set.fromList ["content-length", "transfer-encoding", "accept-encoding", "content-encoding"]
getHeaders :: Monad m => Sink ByteString m HT.RequestHeaders
getHeaders =
toHeaders <$> go id
where
go front =
await >>= maybe close push
where
close = leftover bs >> return bs
where
bs = front empty
push bs'
| "\r\n\r\n" `S8.isInfixOf` bs
|| "\n\n" `S8.isInfixOf` bs
|| S8.length bs > 4096 = leftover bs >> return bs
| otherwise = go $ mappend bs
where
bs = front bs'
toHeaders = map toHeader . takeWhile (not . S8.null) . drop 1 . S8.lines
toHeader bs =
(CI.mk key, val)
where
(key, bs') = S.break (== _colon) bs
val = S.takeWhile (/= _cr) $ S.dropWhile isSpace $ S.drop 1 bs'
waiToRaw :: WAI.Application -> DCN.Application IO
waiToRaw app appdata0 =
loop $ transPipe lift fromClient0
where
fromClient0 = DCN.appSource appdata0
toClient = DCN.appSink appdata0
loop fromClient = do
mfromClient <- runResourceT $ do
ex <- try $ parseRequest conn 0 dummyAddr fromClient
case ex of
Left (_ :: SomeException) -> return Nothing
Right (req, fromClient') -> do
res <- app req
keepAlive <- sendResponse
#if MIN_VERSION_warp(1, 3, 8)
defaultSettings
{ settingsServerName = S8.pack $ concat
[ "Warp/"
, warpVersion
, " + http-reverse-proxy/"
, showVersion Paths_http_reverse_proxy.version
]
}
#endif
dummyCleaner req conn res
(fromClient'', _) <- liftIO fromClient' >>= unwrapResumable
return $ if keepAlive then Just fromClient'' else Nothing
maybe (return ()) loop mfromClient
dummyAddr = SockAddrInet (PortNum 0) 0
conn = Connection
{ connSendMany = \bss -> mapM_ yield bss $$ toClient
, connSendAll = \bs -> yield bs $$ toClient
, connSendFile = \fp offset len _th headers _cleaner ->
let src1 = mapM_ yield headers
src2 = sourceFileRange fp (Just offset) (Just len)
in runResourceT
$ (src1 >> src2)
$$ transPipe lift toClient
, connClose = return ()
, connRecv = error "connRecv should not be used"
}