wai/wai-extra/proxy.hs

26 lines
971 B
Haskell
Raw Normal View History

{-# LANGUAGE OverloadedStrings #-}
import qualified Network.HTTP.Enumerator as H
import qualified Network.Wai as W
2010-12-15 20:51:13 +03:00
import Network.Wai.Middleware.Gzip (gzip)
import Network.Wai.Handler.SimpleServer (run)
import qualified Data.ByteString.Char8 as S8
import qualified Data.ByteString.Lazy.Char8 ()
import Data.Enumerator (($$), joinI, run_)
import Blaze.ByteString.Builder (fromByteString)
import qualified Data.Enumerator as E
main :: IO ()
2010-12-15 20:51:13 +03:00
main = run 3000 $ gzip False app
app :: W.Application
app W.Request { W.pathInfo = path } =
case H.parseUrl $ "http://wiki.yesodweb.com" ++ S8.unpack path of
Nothing -> return notFound
Just hreq -> return $ W.ResponseEnumerator $ run_ . H.http hreq . go
where
go f s h = joinI $ E.map fromByteString $$ f s $ filter safe h
safe (x, _) = not $ x `elem` ["Content-Encoding", "Transfer-Encoding"]
notFound :: W.Response
notFound = W.ResponseLBS W.status404 [("Content-Type", "text/plain")] "Not found"