wai/wai-extra/proxy.hs
Michael Snoyman 6ab2dfe739 Add 'wai-extra/' from commit '29076d018b0cc159c3ccf3050350e4b7469f2c6c'
git-subtree-dir: wai-extra
git-subtree-mainline: ebedc4d083
git-subtree-split: 29076d018b
2011-07-22 16:51:23 +03:00

26 lines
971 B
Haskell

{-# LANGUAGE OverloadedStrings #-}
import qualified Network.HTTP.Enumerator as H
import qualified Network.Wai as W
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 ()
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"