on master help sign in
Got a working wrapper
commitc07c942605b14da4edffc0b43269e5eadb3c0f8d authorMichael Snoyman <Michael Snoyman> dateWed 20 May 2009, 23:22 (17 years ago) parente94c41f
10 files changed+112
.gitignore+2 Hack/Frontend/MonadCGI.hs+35 LICENSE+25 README+1 Setup.lhs+7 hack-frontend-monadcgi.cabal+17 samples/.htaccess+5 samples/hack_cgi.hs+7 samples/hack_fastcgi.hs+8 samples/plain_cgi.hs+5
.gitignorenew file+2
@@ -0,0 +1,2 @@
1+dist
2+*.swp
Hack/Frontend/MonadCGI.hsnew file+35
@@ -0,0 +1,35 @@
1+module Hack.Frontend.MonadCGI
2+ ( cgiToApp
3+ ) where
4+
5+import Hack
6+import Network.CGI.Monad
7+import Network.CGI.Protocol
8+
9+import qualified Data.Map as Map
10+import qualified Data.ByteString.Lazy as BS
11+
12+safeRead :: Read a => a -> String -> a
13+safeRead d s = case reads s of
14+ ((x, _):_) -> x
15+ _ -> d
16+
17+cgiToApp :: CGI CGIResult -> Application
18+cgiToApp cgi env = do
19+ let vars = http env
20+ input = hackInput env
21+ (inputs, body') = decodeInput vars input
22+ req = CGIRequest
23+ { cgiVars = Map.fromList vars
24+ , cgiInputs = inputs
25+ , cgiRequestBody = body'
26+ }
27+ (headers'', output') <- runCGIT cgi req
28+ let output = case output' of
29+ CGIOutput bs -> bs
30+ CGINothing -> BS.empty
31+ let headers' = map (\(HeaderName x, y) -> (x, y)) headers''
32+ let status' = case lookup "Status" headers' of
33+ Nothing -> 200
34+ Just s -> safeRead 200 s
35+ return $ Response status' headers' output
LICENSEnew file+25
@@ -0,0 +1,25 @@
1+The following license covers this documentation, and the source code, except
2+where otherwise indicated.
3+
4+Copyright 2008, Michael Snoyman. All rights reserved.
5+
6+Redistribution and use in source and binary forms, with or without
7+modification, are permitted provided that the following conditions are met:
8+
9+* Redistributions of source code must retain the above copyright notice, this
10+ list of conditions and the following disclaimer.
11+
12+* Redistributions in binary form must reproduce the above copyright notice,
13+ this list of conditions and the following disclaimer in the documentation
14+ and/or other materials provided with the distribution.
15+
16+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS "AS IS" AND ANY EXPRESS OR
17+IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18+MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19+EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY DIRECT, INDIRECT,
20+INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21+NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
22+OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
23+LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
24+OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25+ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
README+1
@@ -0,0 +1 @@
1+Allows programs written against MonadCGI to run with any hack handler.
Setup.lhsnew file+7
@@ -0,0 +1,7 @@
1+#!/usr/bin/env runhaskell
2+
3+> module Main where
4+> import Distribution.Simple
5+
6+> main :: IO ()
7+> main = defaultMain
hack-frontend-monadcgi.cabalnew file+17
@@ -0,0 +1,17 @@
1+name: hack-frontend-monadcgi
2+version: 0.0.0
3+license: BSD3
4+license-file: LICENSE
5+author: Michael Snoyman <michael@snoyman.com>
6+maintainer: Michael Snoyman <michael@snoyman.com>
7+synopsis: Allows programs written against MonadCGI to run with any hack handler.
8+category: Web
9+stability: stable
10+cabal-version: >= 1.2
11+build-type: Simple
12+
13+library
14+ build-depends: base >= 3, bytestring, containers >= 0.2, cgi,
15+ hack >= 2009.5.19
16+ exposed-modules: Hack.Frontend.MonadCGI
17+ ghc-options: -Wall
samples/.htaccessnew file+5
@@ -0,0 +1,5 @@
1+Options +ExecCGI
2+AddHandler cgi-script .cgi
3+AddHandler fastcgi-script .fcgi
4+
5+Options +FollowSymlinks
samples/hack_cgi.hsnew file+7
@@ -0,0 +1,7 @@
1+import qualified Network.CGI
2+import qualified Hack.Handler.CGI
3+import qualified Hack.Frontend.MonadCGI
4+
5+main = Hack.Handler.CGI.run $ Hack.Frontend.MonadCGI.cgiToApp mainCGI
6+
7+mainCGI = Network.CGI.output "This is a test"
samples/hack_fastcgi.hsnew file+8
@@ -0,0 +1,8 @@
1+import qualified Network.CGI
2+import qualified Hack.Handler.FastCGI
3+import qualified Hack.Frontend.MonadCGI
4+
5+main = Hack.Handler.FastCGI.runFastCGIorCGI
6+ $ Hack.Frontend.MonadCGI.cgiToApp mainCGI
7+
8+mainCGI = Network.CGI.output "This is a test"
samples/plain_cgi.hsnew file+5
@@ -0,0 +1,5 @@
1+import Network.CGI
2+
3+main = runCGI mainCGI
4+
5+mainCGI = output "This is a test"