packages feed

hack-handler-cgi (empty) → 0.0.0

raw patch · 4 files changed

+125/−0 lines, 4 filesdep +basedep +bytestringdep +data-defaultsetup-changed

Dependencies added: base, bytestring, data-default, hack

Files

+ Hack/Handler/CGI.hs view
@@ -0,0 +1,76 @@+module Hack.Handler.CGI+    ( run+    , helper+    ) where++import Hack+import Data.Default+import System.Environment (getEnvironment)+import Data.Maybe (fromMaybe)+import qualified Data.ByteString.Lazy as BS+import Data.Word (Word8)++safeRead :: Read a => a -> String -> a+safeRead d s =+  case reads s of+    ((x, _):_) -> x+    [] -> d++lookup' :: String -> [(String, String)] -> String+lookup' key pairs = fromMaybe "" $ lookup key pairs++run :: Application -> IO ()+run app = do+    vars <- getEnvironment+    body' <- BS.getContents+    helper vars body' app >>= BS.putStr++helper :: [(String, String)] -- ^ all variables+       -> BS.ByteString -- ^ body of input+       -> Application+       -> IO BS.ByteString -- ^ full output+helper vars body' app = do+    let rmethod = safeRead GET $ lookup' "REQUEST_METHOD" vars+        sname = lookup' "SCRIPT_NAME" vars+        pinfo = lookup' "PATH_INFO" vars+        qstring = lookup' "QUERY_STRING" vars+        servername = lookup' "SERVER_NAME" vars+        serverport = safeRead 80 $ lookup' "SERVER_PORT" vars+    let env = def+            { requestMethod = rmethod+            , scriptName = sname+            , pathInfo = pinfo+            , queryString = qstring+            , serverName = servername+            , serverPort = serverport+            , http = vars+            , hackInput = body'+            }+    res <- app env+    let h = headers res+    let h' = case lookup "Content-type" h of+                Nothing -> ("Content-type", "text/html; charset=utf-8") : h+                Just _ -> h+    let s = case status res of+                0 -> 200+                x -> x+    return $ BS.concat+        [ pack "Status: "+        , pack $ show s+        , pack "\n"+        , BS.concat $ map showHeader h'+        , pack "\n"+        , body res+        ]++showHeader :: (String, String) -> BS.ByteString+showHeader (x, y) = pack $ x ++ ": " ++ y ++ "\n"++pack :: String -> BS.ByteString+pack = BS.pack . map safeCharToWord8++safeCharToWord8 :: Char -> Word8+safeCharToWord8 c+    | fromEnum c < 256 = toEnum $ fromEnum c+    | otherwise = error $ "Out of bound character, value: " +++                          show (fromEnum c)
+ 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.
+ Setup.lhs view
@@ -0,0 +1,7 @@+#!/usr/bin/env runhaskell++> module Main where+> import Distribution.Simple++> main :: IO ()+> main = defaultMain
+ hack-handler-cgi.cabal view
@@ -0,0 +1,17 @@+name:            hack-handler-cgi+version:         0.0.0+license:         BSD3+license-file:    LICENSE+author:          Michael Snoyman <michael@snoyman.com>+maintainer:      Michael Snoyman <michael@snoyman.com>+synopsis:        Hack handler using network.cgi+category:        Web+stability:       stable+cabal-version:   >= 1.2+build-type:      Simple+homepage:        http://github.com/snoyberg/hack-handler-cgi/tree/master++library+    build-depends:   base, bytestring, hack >= 2009.5.19 , data-default+    exposed-modules: Hack.Handler.CGI+    ghc-options:     -Wall