wai-frontend-monadcgi (empty) → 0.0.0
raw patch · 4 files changed
+120/−0 lines, 4 filesdep +basedep +bytestringdep +cgisetup-changed
Dependencies added: base, bytestring, cgi, containers, wai
Files
- LICENSE +25/−0
- Network/Wai/Frontend/MonadCGI.hs +68/−0
- Setup.lhs +7/−0
- wai-frontend-monadcgi.cabal +20/−0
+ LICENSE view
@@ -0,0 +1,25 @@+The following license covers this documentation, and the source code, except+where otherwise indicated.++Copyright 2008, Michael Snoyman. All rights reserved.++Redistribution and use in source and binary forms, with or without+modification, are permitted provided that the following conditions are met:++* Redistributions of source code must retain the above copyright notice, this+ list of conditions and the following disclaimer.++* Redistributions in binary form must reproduce the above copyright notice,+ this list of conditions and the following disclaimer in the documentation+ and/or other materials provided with the distribution.++THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS "AS IS" AND ANY EXPRESS OR+IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF+MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO+EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY DIRECT, INDIRECT,+INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT+NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,+OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF+LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE+OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF+ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ Network/Wai/Frontend/MonadCGI.hs view
@@ -0,0 +1,68 @@+module Network.Wai.Frontend.MonadCGI+ ( cgiToApp+ , cgiToAppGeneric+ ) where++import Network.Wai+import Network.Wai.Source+import Network.CGI.Monad+import Network.CGI.Protocol++import qualified Data.Map as Map+import qualified Data.ByteString.Lazy as BS+import qualified Data.ByteString.Char8 as S8++import Control.Arrow (first)+import Data.Char (toUpper)+import Data.String (fromString)++safeRead :: Read a => a -> String -> a+safeRead d s = case reads s of+ ((x, _):_) -> x+ _ -> d++cgiToApp :: CGI CGIResult -> Application+cgiToApp = cgiToAppGeneric id++cgiToAppGeneric :: Monad m+ => (m (Headers, CGIResult) -> IO (Headers, CGIResult))+ -> CGIT m CGIResult+ -> Application+cgiToAppGeneric toIO cgi env = do+ input <- toLBS $ requestBody env+ let vars = map (first fixVarName . go) (requestHeaders env)+ ++ getCgiVars env+ (inputs, body') = decodeInput vars input+ req = CGIRequest+ { cgiVars = Map.fromList $ vars+ , cgiInputs = inputs+ , cgiRequestBody = body'+ }+ (headers'', output') <- toIO $ runCGIT cgi req+ let output = case output' of+ CGIOutput bs -> bs+ CGINothing -> BS.empty+ let headers' = map (\(HeaderName x, y) ->+ (fromString x, S8.pack y)) headers''+ let status' = case lookup (fromString "Status") headers' of+ Nothing -> 200+ Just s -> safeRead 200 $ S8.unpack s+ return $ Response (Status status' S8.empty) headers' $ ResponseLBS output+ where+ go (x, y) = (S8.unpack $ ciOriginal x, S8.unpack y)++fixVarName :: String -> String+fixVarName = ((++) $ "HTTP_") . map fixVarNameChar++fixVarNameChar :: Char -> Char+fixVarNameChar '-' = '_'+fixVarNameChar c = toUpper c++getCgiVars :: Request -> [(String, String)]+getCgiVars e =+ [ ("PATH_INFO", S8.unpack $ pathInfo e)+ , ("REQUEST_METHOD", show $ requestMethod e)+ , ("QUERY_STRING", S8.unpack $ queryString e)+ , ("SERVER_NAME", S8.unpack $ serverName e)+ , ("SERVER_PORT", show $ serverPort e)+ ]
+ Setup.lhs view
@@ -0,0 +1,7 @@+#!/usr/bin/env runhaskell++> module Main where+> import Distribution.Simple++> main :: IO ()+> main = defaultMain
+ wai-frontend-monadcgi.cabal view
@@ -0,0 +1,20 @@+name: wai-frontend-monadcgi+version: 0.0.0+license: BSD3+license-file: LICENSE+author: Michael Snoyman <michael@snoyman.com>+maintainer: Michael Snoyman <michael@snoyman.com>+synopsis: Allows programs written against MonadCGI to run with any WAI handler.+category: Web+stability: stable+cabal-version: >= 1.2+build-type: Simple++library+ build-depends: base >= 4 && < 5,+ bytestring,+ containers >= 0.2,+ cgi,+ wai >= 0.2 && < 0.3+ exposed-modules: Network.Wai.Frontend.MonadCGI+ ghc-options: -Wall