@@ -1,10 +1,11 @@
1+{-# LANGUAGE OverloadedStrings #-}
12module Network.Wai.Handler.CGI
23 ( run
34 , run'
45 ) where
56
67import Network.Wai
7−import Network.Wai.Enumerator (fromEitherFile)
8+import Network.Wai.Enumerator (fromResponseBody)
89import Network.Wai.Handler.Helper
910import System.Environment (getEnvironment)
1011import Data.Maybe (fromMaybe)
@@ -12,6 +13,7 @@ import qualified Data.ByteString.Char8 as B
1213import Control.Arrow ((***))
1314import Data.Char (toLower)
1415import qualified System.IO
16+import Data.String (fromString)
1517
1618safeRead :: Read a => a -> String -> a
1719safeRead d s =
@@ -33,7 +35,7 @@ run' :: [(String, String)] -- ^ all variables
3335 -> Application
3436 -> IO ()
3537run' vars inputH outputH app = do
36− let rmethod = safeRead GET $ lookup' "REQUEST_METHOD" vars
38+ let rmethod = B.pack $ lookup' "REQUEST_METHOD" vars
3739 pinfo = lookup' "PATH_INFO" vars
3840 qstring = lookup' "QUERY_STRING" vars
3941 servername = lookup' "SERVER_NAME" vars
@@ -46,10 +48,10 @@ run' vars inputH outputH app = do
4648 case lookup "REMOTE_ADDR" vars of
4749 Just x -> x
4850 Nothing -> ""
49− urlScheme' =
51+ isSecure' =
5052 case map toLower $ lookup' "SERVER_PROTOCOL" vars of
51− "https" -> HTTPS
52− _ -> HTTP
53+ "https" -> True
54+ _ -> False
5355 let env = Request
5456 { requestMethod = rmethod
5557 , pathInfo = B.pack pinfo
@@ -57,16 +59,16 @@ run' vars inputH outputH app = do
5759 , serverName = B.pack servername
5860 , serverPort = serverport
5961 , requestHeaders = map (cleanupVarName *** B.pack) vars
60− , urlScheme = urlScheme'
62+ , isSecure = isSecure'
6163 , requestBody = requestBodyHandle inputH contentLength
6264 , errorHandler = System.IO.hPutStr System.IO.stderr
6365 , remoteHost = B.pack remoteHost'
64− , httpVersion = HttpVersion B.empty
66+ , httpVersion = "1.1" -- FIXME
6567 }
6668 res <- app env
6769 let h = responseHeaders res
68− let h' = case lookup ContentType h of
69− Nothing -> (ContentType, B.pack "text/html; charset=utf-8")
70+ let h' = case lookup "Content-Type" h of
71+ Nothing -> ("Content-Type", "text/html; charset=utf-8")
7072 : h
7173 Just _ -> h
7274 let hPut = B.hPut outputH
@@ -75,7 +77,7 @@ run' vars inputH outputH app = do
7577 hPut $ B.singleton '\n'
7678 mapM_ (printHeader hPut) h'
7779 hPut $ B.singleton '\n'
78− _ <- runEnumerator (fromEitherFile (responseBody res)) (myPut outputH) ()
80+ _ <- runEnumerator (fromResponseBody (responseBody res)) (myPut outputH) ()
7981 return ()
8082
8183myPut :: System.IO.Handle -> () -> B.ByteString -> IO (Either () ())
@@ -85,18 +87,19 @@ printHeader :: (B.ByteString -> IO ())
8587 -> (ResponseHeader, B.ByteString)
8688 -> IO ()
8789printHeader f (x, y) = do
88− f $ responseHeaderToBS x
90+ f $ ciOriginal x
8991 f $ B.pack ": "
9092 f y
9193 f $ B.singleton '\n'
9294
9395cleanupVarName :: String -> RequestHeader
9496cleanupVarName ('H':'T':'T':'P':'_':a:as) =
95− requestHeaderFromBS $ B.pack $ a : helper' as where
97+ fromString $ a : helper' as
98+ where
9699 helper' ('_':x:rest) = '-' : x : helper' rest
97100 helper' (x:rest) = toLower x : helper' rest
98101 helper' [] = []
99−cleanupVarName "CONTENT_TYPE" = ReqContentType
100−cleanupVarName "CONTENT_LENGTH" = ReqContentLength
101−cleanupVarName "SCRIPT_NAME" = requestHeaderFromBS $ B.pack "CGI-Script-Name"
102−cleanupVarName x = requestHeaderFromBS $ B.pack x -- FIXME remove?
102+cleanupVarName "CONTENT_TYPE" = "Content-Type"
103+cleanupVarName "CONTENT_LENGTH" = "Content-Length"
104+cleanupVarName "SCRIPT_NAME" = "CGI-Script-Name"
105+cleanupVarName x = fromString x -- FIXME remove?