| 1 |
{-# LANGUAGE OverloadedStrings #-} |
| 2 |
{-# LANGUAGE CPP #-} |
| 3 |
|
| 4 |
module Network.Wai.Application.Classic.RevProxy (revProxyApp) where |
| 5 |
|
| 6 |
#if __GLASGOW_HASKELL__ < 709 |
| 7 |
import Control.Applicative |
| 8 |
#endif |
| 9 |
import Control.Monad |
| 10 |
import Control.Monad.IO.Class (liftIO) |
| 11 |
import Data.ByteString (ByteString) |
| 12 |
import qualified Data.ByteString as BS (uncons) |
| 13 |
import Data.ByteString.Builder (Builder) |
| 14 |
import qualified Data.ByteString.Char8 as BS hiding (uncons) |
| 15 |
import Data.Conduit |
| 16 |
import qualified Network.HTTP.Client as H |
| 17 |
import Network.HTTP.Types |
| 18 |
import Network.Wai.Application.Classic.Conduit |
| 19 |
import Network.Wai.Application.Classic.EventSource |
| 20 |
import Network.Wai.Application.Classic.Field |
| 21 |
import Network.Wai.Application.Classic.Header |
| 22 |
import Network.Wai.Application.Classic.Path |
| 23 |
import Network.Wai.Application.Classic.Types |
| 24 |
import Network.Wai.Conduit |
| 25 |
|
| 26 |
---------------------------------------------------------------- |
| 27 |
|
| 28 |
-- | Relaying any requests as reverse proxy. |
| 29 |
|
| 30 |
revProxyApp :: ClassicAppSpec -> RevProxyAppSpec -> RevProxyRoute -> Application |
| 31 |
revProxyApp cspec spec route req respond = H.withResponse httpClientRequest mgr proxy |
| 32 |
where |
| 33 |
proxy hrsp = do |
| 34 |
let status = H.responseStatus hrsp |
| 35 |
hdr = fixHeader $ H.responseHeaders hrsp |
| 36 |
clientBody = H.responseBody hrsp |
| 37 |
ct = lookup hContentType hdr |
| 38 |
src = toSource ct clientBody |
| 39 |
respond $ responseSource status hdr src |
| 40 |
|
| 41 |
httpClientRequest = reqToHReq req route |
| 42 |
mgr = revProxyManager spec |
| 43 |
fixHeader = addVia cspec req . filter headerToBeRelay |
| 44 |
|
| 45 |
headerToBeRelay :: Header -> Bool |
| 46 |
headerToBeRelay (k,_) |
| 47 |
| k == hTransferEncoding = False |
| 48 |
| k == hAcceptEncoding = False |
| 49 |
| k == hContentLength = False |
| 50 |
| k == hContentEncoding = False -- See H.decompress. |
| 51 |
| otherwise = True |
| 52 |
|
| 53 |
---------------------------------------------------------------- |
| 54 |
|
| 55 |
reqToHReq :: Request -> RevProxyRoute -> H.Request |
| 56 |
reqToHReq req route = H.defaultRequest { |
| 57 |
H.host = revProxyDomain route |
| 58 |
, H.port = revProxyPort route |
| 59 |
, H.secure = False -- FIXME: upstream is HTTP only |
| 60 |
, H.requestHeaders = addForwardedFor req $ filter headerToBeRelay hdr |
| 61 |
, H.path = path' |
| 62 |
, H.queryString = dropQuestion query |
| 63 |
, H.requestBody = bodyToHBody len body |
| 64 |
, H.method = requestMethod req |
| 65 |
, H.proxy = Nothing |
| 66 |
-- , H.rawBody = False |
| 67 |
, H.decompress = const True |
| 68 |
, H.checkResponse = \_ _ -> return () |
| 69 |
, H.redirectCount = 0 |
| 70 |
} |
| 71 |
where |
| 72 |
path = rawPathInfo req |
| 73 |
src = revProxySrc route |
| 74 |
dst = revProxyDst route |
| 75 |
hdr = requestHeaders req |
| 76 |
query = rawQueryString req |
| 77 |
len = requestBodyLength req |
| 78 |
body = getRequestBodyChunk req |
| 79 |
path' = dst </> (path <\> src) |
| 80 |
dropQuestion q = case BS.uncons q of |
| 81 |
Just (63, q') -> q' -- '?' is 63 |
| 82 |
_ -> q |
| 83 |
|
| 84 |
bodyToHBody :: RequestBodyLength -> IO ByteString -> H.RequestBody |
| 85 |
bodyToHBody ChunkedBody src = H.RequestBodyStreamChunked ($ src) |
| 86 |
bodyToHBody (KnownLength len) src = H.RequestBodyStream (fromIntegral len) ($ src) |
| 87 |
|
| 88 |
---------------------------------------------------------------- |
| 89 |
|
| 90 |
#if MIN_VERSION_conduit(1,3,0) |
| 91 |
toSource :: Maybe ByteString -> H.BodyReader -> ConduitT () (Flush Builder) IO () |
| 92 |
#else |
| 93 |
toSource :: Maybe ByteString -> H.BodyReader -> Source IO (Flush Builder) |
| 94 |
#endif |
| 95 |
toSource (Just "text/event-stream") = bodyToEventSource |
| 96 |
toSource _ = bodyToSource |
| 97 |
|
| 98 |
#if MIN_VERSION_conduit(1,3,0) |
| 99 |
bodyToSource :: H.BodyReader -> ConduitT () (Flush Builder) IO () |
| 100 |
#else |
| 101 |
bodyToSource :: H.BodyReader -> Source IO (Flush Builder) |
| 102 |
#endif |
| 103 |
bodyToSource br = loop |
| 104 |
where |
| 105 |
loop = do |
| 106 |
bs <- liftIO $ H.brRead br |
| 107 |
unless (BS.null bs) $ do |
| 108 |
yield $ Chunk $ byteStringToBuilder bs |
| 109 |
loop |
| 110 |
{- |
| 111 |
|
| 112 |
FIXME: |
| 113 |
badGateway :: ClassicAppSpec -> Request-> SomeException -> IO Response |
| 114 |
badGateway cspec req _ = |
| 115 |
return $ responseBuilder st hdr bdy |
| 116 |
where |
| 117 |
hdr = addServer cspec textPlainHeader |
| 118 |
bdy = byteStringToBuilder "Bad Gateway\r\n" |
| 119 |
st = badGateway502 |
| 120 |
-} |