on master help sign in
wai 0.3
5316347Michael Snoyman
Network/Wai/Handler/FastCGI.hsc+20−21
@@ -24,19 +24,21 @@ import Control.Monad ( liftM, forever )
24import Data.Word (Word8)
24import Data.Word (Word8)
25import Foreign ( Ptr, castPtr, nullPtr, peekArray0
25import Foreign ( Ptr, castPtr, nullPtr, peekArray0
26 , throwIfNeg_, mallocBytes, free )
26 , throwIfNeg_, mallocBytes, free )
27−import Foreign.C ( CInt, CString, CStringLen
27+import Foreign.C (CInt, CString, CStringLen)
28− , peekCString )
29import Control.Exception (finally)
28import Control.Exception (finally)
30import Foreign.Storable ( Storable (..) )
29import Foreign.Storable ( Storable (..) )
31
30
32import qualified Network.Wai as W
31import qualified Network.Wai as W
33import qualified Network.Wai.Handler.CGI as CGI
32import qualified Network.Wai.Handler.CGI as CGI
34−import qualified Network.Wai.Handler.Helper as CGI
35
33
34+import qualified Data.ByteString as S
35+import qualified Data.ByteString.Char8 as S8
36import qualified Data.ByteString as BS
36import qualified Data.ByteString as BS
37import qualified Data.ByteString.Internal as BSB
37import qualified Data.ByteString.Internal as BSB
38import qualified Data.ByteString.Unsafe as BSB
38import qualified Data.ByteString.Unsafe as BSB
39
39
40+import Control.Arrow ((***))
41+
40#include <fcgiapp.h>
42#include <fcgiapp.h>
41
43
42------------------------------------------------------------------------
44------------------------------------------------------------------------
@@ -59,7 +61,7 @@ foreign import ccall unsafe "fcgiapp.h FCGX_PutStr" fcgx_putStr
59foreign import ccall unsafe "fcgiapp.h FCGX_Init" fcgx_init
61foreign import ccall unsafe "fcgiapp.h FCGX_Init" fcgx_init
60 :: IO CInt
62 :: IO CInt
61
63
62−runFork :: Maybe String -> (IO () -> IO a) -> Int -> W.Application -> IO ()
64+runFork :: Maybe S.ByteString -> (IO () -> IO a) -> Int -> W.Application -> IO ()
63runFork sf fork threads app = do
65runFork sf fork threads app = do
64 testReturn "FCGX_Init" $ fcgx_init
66 testReturn "FCGX_Init" $ fcgx_init
65 let oneThread = forever $ oneRequest app sf
67 let oneThread = forever $ oneRequest app sf
@@ -72,11 +74,11 @@ run = runFork Nothing id 1
72
74
73-- | Handle FastCGI requests in an infinite loop. For a server which supports
75-- | Handle FastCGI requests in an infinite loop. For a server which supports
74-- the X-Sendfile header.
76-- the X-Sendfile header.
75−runSendfile :: String -> W.Application -> IO ()
77+runSendfile :: S.ByteString -> W.Application -> IO ()
76runSendfile sf = runFork (Just sf) id 1
78runSendfile sf = runFork (Just sf) id 1
77
79
78oneRequest :: W.Application
80oneRequest :: W.Application
79− -> Maybe String -- X-Sendfile
81+ -> Maybe S.ByteString -- X-Sendfile
80 -> IO ()
82 -> IO ()
81oneRequest app xsendfile = withRequest $ \r -> do
83oneRequest app xsendfile = withRequest $ \r -> do
82 putStrLn "Received 1 request"
84 putStrLn "Received 1 request"
@@ -124,13 +126,14 @@ handleRequest :: W.Application
124 -> StreamPtr
126 -> StreamPtr
125 -> StreamPtr
127 -> StreamPtr
126 -> Environ
128 -> Environ
127− -> Maybe String -- sendfile
129+ -> Maybe S.ByteString -- sendfile
128 -> IO ()
130 -> IO ()
129handleRequest f ins outs env xsendfile = do
131handleRequest f ins outs env xsendfile = do
130 vars <- environToTable env
132 vars <- environToTable env
133+ let vars' = map (S8.unpack *** S8.unpack) vars
131 let input = const $ sRead ins
134 let input = const $ sRead ins
132 let hPut = sPutStr' outs
135 let hPut = sPutStr' outs
133− CGI.run'' vars (CGI.requestBodyFunc input) hPut xsendfile f
136+ CGI.run'' vars' (CGI.requestBodyFunc input) hPut xsendfile f
134
137
135data FCGX_Request
138data FCGX_Request
136
139
@@ -173,16 +176,12 @@ buildByteString f k = do
173testReturn :: String -> IO CInt -> IO ()
176testReturn :: String -> IO CInt -> IO ()
174testReturn e = throwIfNeg_ (\n -> e ++ " failed with error code: "++ show n)
177testReturn e = throwIfNeg_ (\n -> e ++ " failed with error code: "++ show n)
175
178
176−environToTable :: Environ -> IO [(String,String)]
179+environToTable :: Environ -> IO [(S.ByteString, S.ByteString)]
177−environToTable arr =
180+environToTable arr = do
178− do css <- peekArray0 nullPtr arr
181+ css <- peekArray0 nullPtr arr
179− ss <- mapM peekCString css
182+ ss <- mapM S.packCString css
180− return $ map (splitBy '=') ss
183+ return $ map splitEq ss
181−
184+ where
182−-- | Split a list at the first occurence of a marker.
185+ splitEq s =
183−-- Do not include the marker in any of the resulting lists.
186+ let (a, b) = S.breakByte 61 s
184−-- If the marker does not occur in the list, the entire
187+ in (a, S.drop 1 b)
185−-- input with be in the first list.
186−splitBy :: Eq a => a -> [a] -> ([a],[a])
187−splitBy x xs = (y, drop 1 z)
188− where (y,z) = break (==x) xs
wai-handler-fastcgi.cabal+6−6
@@ -1,5 +1,5 @@
1name: wai-handler-fastcgi
1name: wai-handler-fastcgi
2−version: 0.2.2
2+version: 0.3.0
3license: BSD3
3license: BSD3
4license-file: LICENSE
4license-file: LICENSE
5author: Michael Snoyman <michael@snoyman.com>
5author: Michael Snoyman <michael@snoyman.com>
@@ -10,13 +10,13 @@ stability: stable
10cabal-version: >= 1.2
10cabal-version: >= 1.2
11build-type: Configure
11build-type: Configure
12extra-source-files: configure fastcgi.buildinfo.in
12extra-source-files: configure fastcgi.buildinfo.in
13−homepage: http://github.com/snoyberg/hack-handler-fastcgi/tree/master
13+homepage: http://github.com/snoyberg/wai-handler-fastcgi
14
14
15library
15library
16− build-depends: base >= 4 && < 5,
16+ build-depends: base >= 4 && < 5
17− wai >= 0.2.0 && < 0.3.0,
17+ , wai >= 0.3 && < 0.4
18− wai-extra >= 0.2.1 && < 0.3.0,
18+ , wai-extra >= 0.3 && < 0.4
19− bytestring >= 0.9.1.4 && < 0.10
19+ , bytestring >= 0.9 && < 0.10
20 exposed-modules: Network.Wai.Handler.FastCGI
20 exposed-modules: Network.Wai.Handler.FastCGI
21 ghc-options: -Wall
21 ghc-options: -Wall
22 includes: fcgiapp.h
22 includes: fcgiapp.h