{-# LANGUAGE CPP #-}
{-# LANGUAGE RankNTypes #-}

-- | Backend for Common Gateway Interface. Almost all users should use the
-- 'run' function.
module Network.Wai.Handler.CGI (
    run,
    runSendfile,
    runGeneric,
    requestBodyFunc,
) where

import Control.Arrow ((***))
import Control.Monad (unless, void)
import Data.ByteString.Builder (byteString, string8, toLazyByteString, word8)
import Data.ByteString.Builder.Extra (flush)
import qualified Data.ByteString.Char8 as B
import qualified Data.ByteString.Lazy as L
import Data.ByteString.Lazy.Internal (defaultChunkSize)
import qualified Data.CaseInsensitive as CI
import Data.Char (toLower)
import Data.Function (fix)
import Data.IORef (newIORef, readIORef, writeIORef)
import Data.Maybe (fromMaybe)
#if __GLASGOW_HASKELL__ < 710
import Data.Monoid (mconcat, mempty, mappend)
#endif
import qualified Data.Streaming.ByteString.Builder as Builder
import qualified Data.String as String
import Data.Word8 (_lf, _space)
import Network.HTTP.Types (Status (..), hContentLength, hContentType, hRange)
import qualified Network.HTTP.Types as H
import Network.Socket (addrAddress, getAddrInfo)
import Network.Wai
import Network.Wai.Internal
import System.IO (Handle)
import qualified System.IO

#if WINDOWS
import System.Environment (getEnvironment)
#else
import qualified System.Posix.Env.ByteString as Env

getEnvironment :: IO [(String, String)]
getEnvironment :: IO [([Char], [Char])]
getEnvironment = ((ByteString, ByteString) -> ([Char], [Char]))
-> [(ByteString, ByteString)] -> [([Char], [Char])]
forall a b. (a -> b) -> [a] -> [b]
map (ByteString -> [Char]
B.unpack (ByteString -> [Char])
-> (ByteString -> [Char])
-> (ByteString, ByteString)
-> ([Char], [Char])
forall b c b' c'. (b -> c) -> (b' -> c') -> (b, b') -> (c, c')
forall (a :: * -> * -> *) b c b' c'.
Arrow a =>
a b c -> a b' c' -> a (b, b') (c, c')
*** ByteString -> [Char]
B.unpack) ([(ByteString, ByteString)] -> [([Char], [Char])])
-> IO [(ByteString, ByteString)] -> IO [([Char], [Char])]
forall a b. (a -> b) -> IO a -> IO b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
`fmap` IO [(ByteString, ByteString)]
Env.getEnvironment
#endif

safeRead :: Read a => a -> String -> a
safeRead :: forall a. Read a => a -> [Char] -> a
safeRead a
d [Char]
s =
    case ReadS a
forall a. Read a => ReadS a
reads [Char]
s of
        ((a
x, [Char]
_) : [(a, [Char])]
_) -> a
x
        [] -> a
d

lookup' :: String -> [(String, String)] -> String
lookup' :: [Char] -> [([Char], [Char])] -> [Char]
lookup' [Char]
key [([Char], [Char])]
pairs = [Char] -> Maybe [Char] -> [Char]
forall a. a -> Maybe a -> a
fromMaybe [Char]
"" (Maybe [Char] -> [Char]) -> Maybe [Char] -> [Char]
forall a b. (a -> b) -> a -> b
$ [Char] -> [([Char], [Char])] -> Maybe [Char]
forall a b. Eq a => a -> [(a, b)] -> Maybe b
lookup [Char]
key [([Char], [Char])]
pairs

-- | Run an application using CGI.
run :: Application -> IO ()
run :: Application -> IO ()
run Application
app = do
    vars <- IO [([Char], [Char])]
getEnvironment
    let input = Handle -> Int -> IO (IO ByteString)
requestBodyHandle Handle
System.IO.stdin
        output = Handle -> ByteString -> IO ()
B.hPut Handle
System.IO.stdout
    runGeneric vars input output Nothing app

-- | Some web servers provide an optimization for sending files via a sendfile
-- system call via a special header. To use this feature, provide that header
-- name here.
runSendfile
    :: B.ByteString
    -- ^ sendfile header
    -> Application
    -> IO ()
runSendfile :: ByteString -> Application -> IO ()
runSendfile ByteString
sf Application
app = do
    vars <- IO [([Char], [Char])]
getEnvironment
    let input = Handle -> Int -> IO (IO ByteString)
requestBodyHandle Handle
System.IO.stdin
        output = Handle -> ByteString -> IO ()
B.hPut Handle
System.IO.stdout
    runGeneric vars input output (Just sf) app

-- | A generic CGI helper, which allows other backends (FastCGI and SCGI) to
-- use the same code as CGI. Most users will not need this function, and can
-- stick with 'run' or 'runSendfile'.
runGeneric
    :: [(String, String)]
    -- ^ all variables
    -> (Int -> IO (IO B.ByteString))
    -- ^ responseBody of input
    -> (B.ByteString -> IO ())
    -- ^ destination for output
    -> Maybe B.ByteString
    -- ^ does the server support the X-Sendfile header?
    -> Application
    -> IO ()
runGeneric :: [([Char], [Char])]
-> (Int -> IO (IO ByteString))
-> (ByteString -> IO ())
-> Maybe ByteString
-> Application
-> IO ()
runGeneric [([Char], [Char])]
vars Int -> IO (IO ByteString)
inputH ByteString -> IO ()
outputH Maybe ByteString
xsendfile Application
app = do
    let rmethod :: ByteString
rmethod = [Char] -> ByteString
B.pack ([Char] -> ByteString) -> [Char] -> ByteString
forall a b. (a -> b) -> a -> b
$ [Char] -> [([Char], [Char])] -> [Char]
lookup' [Char]
"REQUEST_METHOD" [([Char], [Char])]
vars
        pinfo :: [Char]
pinfo = [Char] -> [([Char], [Char])] -> [Char]
lookup' [Char]
"PATH_INFO" [([Char], [Char])]
vars
        qstring :: [Char]
qstring = [Char] -> [([Char], [Char])] -> [Char]
lookup' [Char]
"QUERY_STRING" [([Char], [Char])]
vars
        contentLength :: Int
contentLength = Int -> [Char] -> Int
forall a. Read a => a -> [Char] -> a
safeRead Int
0 ([Char] -> Int) -> [Char] -> Int
forall a b. (a -> b) -> a -> b
$ [Char] -> [([Char], [Char])] -> [Char]
lookup' [Char]
"CONTENT_LENGTH" [([Char], [Char])]
vars
        remoteHost' :: [Char]
remoteHost' =
            case [Char] -> [([Char], [Char])] -> Maybe [Char]
forall a b. Eq a => a -> [(a, b)] -> Maybe b
lookup [Char]
"REMOTE_ADDR" [([Char], [Char])]
vars of
                Just [Char]
x -> [Char]
x
                Maybe [Char]
Nothing -> [Char] -> [([Char], [Char])] -> [Char]
lookup' [Char]
"REMOTE_HOST" [([Char], [Char])]
vars
        isSecure' :: Bool
isSecure' =
            case (Char -> Char) -> [Char] -> [Char]
forall a b. (a -> b) -> [a] -> [b]
map Char -> Char
toLower ([Char] -> [Char]) -> [Char] -> [Char]
forall a b. (a -> b) -> a -> b
$ [Char] -> [([Char], [Char])] -> [Char]
lookup' [Char]
"SERVER_PROTOCOL" [([Char], [Char])]
vars of
                [Char]
"https" -> Bool
True
                [Char]
_ -> Bool
False
    addrs <- Maybe AddrInfo -> Maybe [Char] -> Maybe [Char] -> IO [AddrInfo]
forall (t :: * -> *).
GetAddrInfo t =>
Maybe AddrInfo -> Maybe [Char] -> Maybe [Char] -> IO (t AddrInfo)
getAddrInfo Maybe AddrInfo
forall a. Maybe a
Nothing ([Char] -> Maybe [Char]
forall a. a -> Maybe a
Just [Char]
remoteHost') Maybe [Char]
forall a. Maybe a
Nothing
    requestBody' <- inputH contentLength
    let addr =
            case [AddrInfo]
addrs of
                AddrInfo
a : [AddrInfo]
_ -> AddrInfo -> SockAddr
addrAddress AddrInfo
a
                [] -> [Char] -> SockAddr
forall a. HasCallStack => [Char] -> a
error ([Char] -> SockAddr) -> [Char] -> SockAddr
forall a b. (a -> b) -> a -> b
$ [Char]
"Invalid REMOTE_ADDR or REMOTE_HOST: " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
remoteHost'
        reqHeaders = (([Char], [Char]) -> (CI ByteString, ByteString))
-> [([Char], [Char])] -> [(CI ByteString, ByteString)]
forall a b. (a -> b) -> [a] -> [b]
map ([Char] -> CI ByteString
cleanupVarName ([Char] -> CI ByteString)
-> ([Char] -> ByteString)
-> ([Char], [Char])
-> (CI ByteString, ByteString)
forall b c b' c'. (b -> c) -> (b' -> c') -> (b, b') -> (c, c')
forall (a :: * -> * -> *) b c b' c'.
Arrow a =>
a b c -> a b' c' -> a (b, b') (c, c')
*** [Char] -> ByteString
B.pack) [([Char], [Char])]
vars
        env =
            IO ByteString -> Request -> Request
setRequestBodyChunks IO ByteString
requestBody' (Request -> Request) -> Request -> Request
forall a b. (a -> b) -> a -> b
$
                Request
defaultRequest
                    { requestMethod = rmethod
                    , rawPathInfo = B.pack pinfo
                    , pathInfo = H.decodePathSegments $ B.pack pinfo
                    , rawQueryString = B.pack qstring
                    , queryString = H.parseQuery $ B.pack qstring
                    , requestHeaders = reqHeaders
                    , isSecure = isSecure'
                    , remoteHost = addr
                    , httpVersion = H.http11 -- FIXME
                    , vault = mempty
                    , requestBodyLength = KnownLength $ fromIntegral contentLength
                    , requestHeaderHost = lookup "host" reqHeaders
                    , requestHeaderRange = lookup hRange reqHeaders
#if MIN_VERSION_wai(3,2,0)
                    , requestHeaderReferer = lookup "referer" reqHeaders
                    , requestHeaderUserAgent = lookup "user-agent" reqHeaders
#endif
                    }
    void $ app env $ \Response
res ->
        case (Maybe ByteString
xsendfile, Response
res) of
            (Just ByteString
sf, ResponseFile Status
s [(CI ByteString, ByteString)]
hs [Char]
fp Maybe FilePart
Nothing) -> do
                (ByteString -> IO ()) -> [ByteString] -> IO ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ ByteString -> IO ()
outputH ([ByteString] -> IO ()) -> [ByteString] -> IO ()
forall a b. (a -> b) -> a -> b
$ LazyByteString -> [ByteString]
L.toChunks (LazyByteString -> [ByteString]) -> LazyByteString -> [ByteString]
forall a b. (a -> b) -> a -> b
$ Builder -> LazyByteString
toLazyByteString (Builder -> LazyByteString) -> Builder -> LazyByteString
forall a b. (a -> b) -> a -> b
$ Status
-> [(CI ByteString, ByteString)] -> ByteString -> [Char] -> Builder
sfBuilder Status
s [(CI ByteString, ByteString)]
hs ByteString
sf [Char]
fp
                ResponseReceived -> IO ResponseReceived
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return ResponseReceived
ResponseReceived
            (Maybe ByteString, Response)
_ -> do
                let (Status
s, [(CI ByteString, ByteString)]
hs, (StreamingBody -> IO a) -> IO a
wb) = Response
-> (Status, [(CI ByteString, ByteString)],
    (StreamingBody -> IO a) -> IO a)
forall a.
Response
-> (Status, [(CI ByteString, ByteString)],
    (StreamingBody -> IO a) -> IO a)
responseToStream Response
res
                (blazeRecv, blazeFinish) <- BufferAllocStrategy -> IO (BuilderRecv, BuilderFinish)
Builder.newBuilderRecv BufferAllocStrategy
Builder.defaultStrategy
                wb $ \StreamingBody
b -> do
                    let sendBuilder :: Builder -> IO ()
sendBuilder Builder
builder = do
                            popper <- BuilderRecv
blazeRecv Builder
builder
                            fix $ \IO ()
loop -> do
                                bs <- IO ByteString
popper
                                unless (B.null bs) $ do
                                    outputH bs
                                    loop
                    Builder -> IO ()
sendBuilder (Builder -> IO ()) -> Builder -> IO ()
forall a b. (a -> b) -> a -> b
$ Status -> [(CI ByteString, ByteString)] -> Builder
headers Status
s [(CI ByteString, ByteString)]
hs Builder -> Builder -> Builder
forall a. Monoid a => a -> a -> a
`mappend` Word8 -> Builder
word8 Word8
_lf
                    StreamingBody
b Builder -> IO ()
sendBuilder (Builder -> IO ()
sendBuilder Builder
flush)
                blazeFinish >>= maybe (return ()) outputH
                return ResponseReceived
  where
    headers :: Status -> [(CI ByteString, ByteString)] -> Builder
headers Status
s [(CI ByteString, ByteString)]
hs = [Builder] -> Builder
forall a. Monoid a => [a] -> a
mconcat (((Builder, Builder) -> Builder)
-> [(Builder, Builder)] -> [Builder]
forall a b. (a -> b) -> [a] -> [b]
map (Builder, Builder) -> Builder
header ([(Builder, Builder)] -> [Builder])
-> [(Builder, Builder)] -> [Builder]
forall a b. (a -> b) -> a -> b
$ Status -> (Builder, Builder)
status Status
s (Builder, Builder) -> [(Builder, Builder)] -> [(Builder, Builder)]
forall a. a -> [a] -> [a]
: ((CI ByteString, ByteString) -> (Builder, Builder))
-> [(CI ByteString, ByteString)] -> [(Builder, Builder)]
forall a b. (a -> b) -> [a] -> [b]
map (CI ByteString, ByteString) -> (Builder, Builder)
header' ([(CI ByteString, ByteString)] -> [(CI ByteString, ByteString)]
forall {b}.
IsString b =>
[(CI ByteString, b)] -> [(CI ByteString, b)]
fixHeaders [(CI ByteString, ByteString)]
hs))
    status :: Status -> (Builder, Builder)
status (Status Int
i ByteString
m) =
        ( ByteString -> Builder
byteString ByteString
"Status"
        , [Builder] -> Builder
forall a. Monoid a => [a] -> a
mconcat
            [ [Char] -> Builder
string8 ([Char] -> Builder) -> [Char] -> Builder
forall a b. (a -> b) -> a -> b
$ Int -> [Char]
forall a. Show a => a -> [Char]
show Int
i
            , Word8 -> Builder
word8 Word8
_space
            , ByteString -> Builder
byteString ByteString
m
            ]
        )
    header' :: (CI ByteString, ByteString) -> (Builder, Builder)
header' (CI ByteString
x, ByteString
y) = (ByteString -> Builder
byteString (ByteString -> Builder) -> ByteString -> Builder
forall a b. (a -> b) -> a -> b
$ CI ByteString -> ByteString
forall s. CI s -> s
CI.original CI ByteString
x, ByteString -> Builder
byteString ByteString
y)
    header :: (Builder, Builder) -> Builder
header (Builder
x, Builder
y) =
        [Builder] -> Builder
forall a. Monoid a => [a] -> a
mconcat
            [ Builder
x
            , ByteString -> Builder
byteString ByteString
": "
            , Builder
y
            , Word8 -> Builder
word8 Word8
_lf
            ]
    sfBuilder :: Status
-> [(CI ByteString, ByteString)] -> ByteString -> [Char] -> Builder
sfBuilder Status
s [(CI ByteString, ByteString)]
hs ByteString
sf [Char]
fp =
        [Builder] -> Builder
forall a. Monoid a => [a] -> a
mconcat
            [ Status -> [(CI ByteString, ByteString)] -> Builder
headers Status
s [(CI ByteString, ByteString)]
hs
            , (Builder, Builder) -> Builder
header (ByteString -> Builder
byteString ByteString
sf, [Char] -> Builder
string8 [Char]
fp)
            , Word8 -> Builder
word8 Word8
_lf
            , ByteString -> Builder
byteString ByteString
sf
            , ByteString -> Builder
byteString ByteString
" not supported"
            ]
    fixHeaders :: [(CI ByteString, b)] -> [(CI ByteString, b)]
fixHeaders [(CI ByteString, b)]
h =
        case CI ByteString -> [(CI ByteString, b)] -> Maybe b
forall a b. Eq a => a -> [(a, b)] -> Maybe b
lookup CI ByteString
hContentType [(CI ByteString, b)]
h of
            Maybe b
Nothing -> (CI ByteString
hContentType, b
"text/html; charset=utf-8") (CI ByteString, b) -> [(CI ByteString, b)] -> [(CI ByteString, b)]
forall a. a -> [a] -> [a]
: [(CI ByteString, b)]
h
            Just b
_ -> [(CI ByteString, b)]
h

cleanupVarName :: String -> CI.CI B.ByteString
cleanupVarName :: [Char] -> CI ByteString
cleanupVarName [Char]
"CONTENT_TYPE" = CI ByteString
hContentType
cleanupVarName [Char]
"CONTENT_LENGTH" = CI ByteString
hContentLength
cleanupVarName [Char]
"SCRIPT_NAME" = CI ByteString
"CGI-Script-Name"
cleanupVarName [Char]
s =
    case [Char]
s of
        Char
'H' : Char
'T' : Char
'T' : Char
'P' : Char
'_' : Char
a : [Char]
as -> [Char] -> CI ByteString
forall a. IsString a => [Char] -> a
String.fromString ([Char] -> CI ByteString) -> [Char] -> CI ByteString
forall a b. (a -> b) -> a -> b
$ Char
a Char -> [Char] -> [Char]
forall a. a -> [a] -> [a]
: [Char] -> [Char]
helper' [Char]
as
        [Char]
_ -> [Char] -> CI ByteString
forall a. IsString a => [Char] -> a
String.fromString [Char]
s -- FIXME remove?
  where
    helper' :: [Char] -> [Char]
helper' (Char
'_' : Char
x : [Char]
rest) = Char
'-' Char -> [Char] -> [Char]
forall a. a -> [a] -> [a]
: Char
x Char -> [Char] -> [Char]
forall a. a -> [a] -> [a]
: [Char] -> [Char]
helper' [Char]
rest
    helper' (Char
x : [Char]
rest) = Char -> Char
toLower Char
x Char -> [Char] -> [Char]
forall a. a -> [a] -> [a]
: [Char] -> [Char]
helper' [Char]
rest
    helper' [] = []

requestBodyHandle :: Handle -> Int -> IO (IO B.ByteString)
requestBodyHandle :: Handle -> Int -> IO (IO ByteString)
requestBodyHandle Handle
h = (Int -> BuilderFinish) -> Int -> IO (IO ByteString)
requestBodyFunc ((Int -> BuilderFinish) -> Int -> IO (IO ByteString))
-> (Int -> BuilderFinish) -> Int -> IO (IO ByteString)
forall a b. (a -> b) -> a -> b
$ \Int
i -> do
    bs <- Handle -> Int -> IO ByteString
B.hGet Handle
h Int
i
    return $ if B.null bs then Nothing else Just bs

requestBodyFunc
    :: (Int -> IO (Maybe B.ByteString)) -> Int -> IO (IO B.ByteString)
requestBodyFunc :: (Int -> BuilderFinish) -> Int -> IO (IO ByteString)
requestBodyFunc Int -> BuilderFinish
get Int
count0 = do
    ref <- Int -> IO (IORef Int)
forall a. a -> IO (IORef a)
newIORef Int
count0
    return $ do
        count <- readIORef ref
        if count <= 0
            then return B.empty
            else do
                mbs <- get $ min count defaultChunkSize
                writeIORef ref $ count - maybe 0 B.length mbs
                return $ fromMaybe B.empty mbs