on master help sign in
Converting Hack variables back to CGI variables.
396cd20Michael Snoyman
Hack/Frontend/MonadCGI.hs+22−1
@@ -9,6 +9,9 @@ import Network.CGI.Protocol
9import qualified Data.Map as Map
9import qualified Data.Map as Map
10import qualified Data.ByteString.Lazy as BS
10import qualified Data.ByteString.Lazy as BS
11
11
12+import Control.Arrow (first)
13+import Data.Char (toUpper)
14+
12safeRead :: Read a => a -> String -> a
15safeRead :: Read a => a -> String -> a
13safeRead d s = case reads s of
16safeRead d s = case reads s of
14 ((x, _):_) -> x
17 ((x, _):_) -> x
@@ -20,7 +23,8 @@ cgiToApp cgi env = do
20 input = hackInput env
23 input = hackInput env
21 (inputs, body') = decodeInput vars input
24 (inputs, body') = decodeInput vars input
22 req = CGIRequest
25 req = CGIRequest
23− { cgiVars = Map.fromList vars
26+ { cgiVars = Map.fromList $ map (first fixVarName) vars
27+ ++ getCgiVars env
24 , cgiInputs = inputs
28 , cgiInputs = inputs
25 , cgiRequestBody = body'
29 , cgiRequestBody = body'
26 }
30 }
@@ -33,3 +37,20 @@ cgiToApp cgi env = do
33 Nothing -> 200
37 Nothing -> 200
34 Just s -> safeRead 200 s
38 Just s -> safeRead 200 s
35 return $ Response status' headers' output
39 return $ Response status' headers' output
40+
41+fixVarName :: String -> String
42+fixVarName = (flip (++) $ "HTTP_") . map fixVarNameChar
43+
44+fixVarNameChar :: Char -> Char
45+fixVarNameChar '-' = '_'
46+fixVarNameChar c = toUpper c
47+
48+getCgiVars :: Env -> [(String, String)]
49+getCgiVars e =
50+ [ ("PATH_INFO", pathInfo e)
51+ , ("REQUEST_METHOD", show $ requestMethod e)
52+ , ("SCRIPT_NAME", scriptName e)
53+ , ("QUERY_STRING", queryString e)
54+ , ("SERVER_NAME", serverName e)
55+ , ("SERVER_PORT", show $ serverPort e)
56+ ]
hack-frontend-monadcgi.cabal+5−2
@@ -1,5 +1,5 @@
1name: hack-frontend-monadcgi
1name: hack-frontend-monadcgi
2−version: 0.0.0
2+version: 0.0.1
3license: BSD3
3license: BSD3
4license-file: LICENSE
4license-file: LICENSE
5author: Michael Snoyman <michael@snoyman.com>
5author: Michael Snoyman <michael@snoyman.com>
@@ -11,7 +11,10 @@ cabal-version: >= 1.2
11build-type: Simple
11build-type: Simple
12
12
13library
13library
14− build-depends: base >= 3, bytestring, containers >= 0.2, cgi,
14+ build-depends: base >= 4 && < 5,
15+ bytestring,
16+ containers >= 0.2,
17+ cgi,
15 hack >= 2009.5.19
18 hack >= 2009.5.19
16 exposed-modules: Hack.Frontend.MonadCGI
19 exposed-modules: Hack.Frontend.MonadCGI
17 ghc-options: -Wall
20 ghc-options: -Wall