File: CGI.hs

package info (click to toggle)
haskell-cgi 3001.1.5.1-5
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 136 kB
  • ctags: 9
  • sloc: haskell: 849; makefile: 79; sh: 43
file content (575 lines) | stat: -rw-r--r-- 21,251 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
{-# OPTIONS_GHC -fallow-overlapping-instances #-}
-----------------------------------------------------------------------------
-- |
-- Module      :  Network.CGI
-- Copyright   :  (c) The University of Glasgow 2001
--                (c) Bjorn Bringert 2004-2006
--                (c) Ian Lynagh 2005
--                (c) Jeremy Shaw 2005
-- License     :  BSD-style
--
-- Maintainer  :  bjorn@bringert.net
-- Stability   :  experimental
-- Portability :  non-portable (uses Control.Monad.State)
--
-- Simple Library for writing CGI programs.
-- See <http://hoohoo.ncsa.uiuc.edu/cgi/interface.html> for the
-- CGI specification.
--
-- This version of the library is for systems with version 2.0 or greater
-- of the network package. This includes GHC 6.6 and later. For older
-- systems, see <http://www.cs.chalmers.se/~bringert/darcs/cgi-compat/doc/>
--
-- Based on the original Haskell binding for CGI:
--
-- Original Version by Erik Meijer <mailto:erik@cs.ruu.nl>.
-- Further hacked on by Sven Panne <mailto:sven.panne@aedion.de>.
-- Further hacking by Andy Gill <mailto:andy@galconn.com>.
-- A new, hopefully more flexible, interface
-- and support for file uploads by Bjorn Bringert <mailto:bjorn@bringert.net>.
--
-- Here is a simple example, including error handling (not that there is 
-- much that can go wrong with Hello World):
--
-- > import Network.CGI
-- >
-- > cgiMain :: CGI CGIResult
-- > cgiMain = output "Hello World!"
-- >
-- > main :: IO ()
-- > main = runCGI (handleErrors cgiMain)
--
--
-----------------------------------------------------------------------------

module Network.CGI (
  -- * CGI monad
    MonadCGI, CGIT, CGIResult, CGI
  , MonadIO, liftIO
  , runCGI
  -- * Error handling
  , throwCGI, catchCGI, tryCGI, handleExceptionCGI
  , handleErrors
  -- * Logging
  , logCGI
  -- * Output
  , output, outputFPS, outputNothing, redirect
  , setHeader, setStatus
  -- * Error pages
  , outputError, outputException 
  , outputNotFound, outputMethodNotAllowed, outputInternalServerError
  -- * Input
  , getInput, getInputFPS, readInput
  , getInputs, getInputNames
  , getMultiInput
  , getInputFilename, getInputContentType
  -- * Environment
  , getVar, getVarWithDefault, getVars
  , serverName, serverPort
  , requestMethod, pathInfo
  , pathTranslated, scriptName
  , queryString
  , remoteHost, remoteAddr
  , authType, remoteUser
  , requestContentType, requestContentLength
  , requestHeader
  -- * Program and request URI
  , progURI, queryURI, requestURI
  -- * Content type
  , ContentType(..), showContentType, parseContentType
  -- * Cookies
  , Cookie(..), newCookie
  , getCookie, readCookie
  , setCookie, deleteCookie
  -- * URL encoding
  , formEncode, urlEncode, formDecode, urlDecode
  ) where

import Control.Exception (Exception(..))
import Control.Monad (liftM)
import Control.Monad.Trans (MonadIO, liftIO)
import Data.Char (toUpper)
import Data.List (intersperse, sort, group)
import Data.Maybe (fromMaybe)
import qualified Data.Map as Map
import Network.URI (URI(..), URIAuth(..), nullURI, parseRelativeReference, 
                    escapeURIString, isUnescapedInURI)
import System.IO (stdin, stdout)
import System.IO.Error (isUserError, ioeGetErrorString)

import qualified Data.ByteString.Lazy.Char8 as BS
import Data.ByteString.Lazy.Char8 (ByteString)

import Network.CGI.Cookie (Cookie(..), showCookie, newCookie, findCookie)
import qualified Network.CGI.Cookie as Cookie (deleteCookie)
import Network.CGI.RFC822Headers (ContentType(..), 
                                  parseContentType, showContentType)
import Network.CGI.Monad
import Network.CGI.Protocol

import Text.XHtml (Html, renderHtml, header, (<<), thetitle, (+++), 
                   body, h1, paragraph, hr, address)


-- | Run a CGI action. Typically called by the main function.
--   Reads input from stdin and writes to stdout. Gets
--   CGI environment variables from the program environment.
runCGI :: MonadIO m => CGIT m CGIResult -> m ()
runCGI f = do env <- getCGIVars
              hRunCGI env stdin stdout (runCGIT f)


--
-- * Output \/ redirect
--

-- | Output a 'String'. The output is assumed to be text\/html, encoded using
--   ISO-8859-1. To change this, set the Content-type header using
--   'setHeader'.
output :: MonadCGI m =>
          String        -- ^ The string to output.
       -> m CGIResult
output = return . CGIOutput . BS.pack

-- | Output a 'ByteString'. The output is assumed to be text\/html, 
--   encoded using ISO-8859-1. To change this, set the 
--   Content-type header using 'setHeader'.
outputFPS :: MonadCGI m =>
             ByteString        -- ^ The string to output.
          -> m CGIResult
outputFPS = return . CGIOutput

-- | Do not output anything (except headers).
outputNothing :: MonadCGI m => m CGIResult
outputNothing = return CGINothing

-- | Redirect to some location.
redirect :: MonadCGI m =>
            String        -- ^ A URL to redirect to.
         -> m CGIResult
redirect url = do setHeader "Location" url
                  outputNothing

--
-- * Error handling
--

-- | Catches any exception thrown by the given CGI action,
--   returns an error page with a 500 Internal Server Error,
--   showing the exception information, and logs the error.
--   
--   Typical usage:
--
-- > cgiMain :: CGI CGIResult
-- > cgiMain = ...
-- >
-- > main :: IO ()
-- > main = runCGI (handleErrors cgiMain)
handleErrors :: CGI CGIResult -> CGI CGIResult
handleErrors = flip catchCGI outputException

--
-- * Error output
--

-- | Output a 500 Internal Server Error with information from
--   an 'Exception'.
outputException :: (MonadCGI m,MonadIO m) => Exception -> m CGIResult
outputException e = outputInternalServerError es
    where es = case e of
                 ErrorCall msg  -> [msg]
                 IOException ie -> ioe ie
                 _              -> [show e]
          ioe ie = if isUserError ie then [ioeGetErrorString ie] else [show ie]

-- | Output an error page to the user, with the given
--   HTTP status code in the response. Also logs the error information
--   using 'logCGI'.
outputError :: (MonadCGI m, MonadIO m) =>
               Int      -- ^ HTTP Status code
            -> String   -- ^ Status message
            -> [String] -- ^ Error information
            -> m CGIResult
outputError c m es = 
      do logCGI $ show (c,m,es)
         setStatus c m
         setHeader "Content-type" "text/html; charset=ISO-8859-1"
         page <- errorPage c m es 
         output $ renderHtml page

-- | Create an HTML error page.
errorPage :: MonadCGI m => 
             Int      -- ^ Status code
          -> String   -- ^ Status message
          -> [String] -- ^ Error information
          -> m Html
errorPage c m es = 
    do server <- getVar "SERVER_SOFTWARE"
       host   <- getVar "SERVER_NAME"
       port   <- getVar "SERVER_PORT"
       let tit = show c ++ " " ++ m
           sig = "Haskell CGI" 
                 ++ " on " ++ fromMaybe "" server
                 ++ " at " ++ fromMaybe "" host ++ maybe "" (", port "++) port
       return $ header << thetitle << tit 
                  +++ body << (h1 << tit +++ map (paragraph <<) es 
                               +++ hr +++ address << sig)

--
-- * Specific HTTP errors
--

-- | Use 'outputError' to output and log a 404 Not Found error.
outputNotFound :: (MonadIO m, MonadCGI m) => 
                 String -- ^ The name of the requested resource.
              -> m CGIResult
outputNotFound r =
    outputError 404 "Not Found" ["The requested resource was not found: " ++ r]

-- | Use 'outputError' to output and log a 405 Method Not Allowed error.
outputMethodNotAllowed :: (MonadIO m, MonadCGI m) => 
                          [String] -- ^ The allowed methods.
                       -> m CGIResult
outputMethodNotAllowed ms = 
    do let allow = concat $ intersperse ", " ms
       setHeader "Allow" allow
       outputError 405 "Method Not Allowed" ["Allowed methods : " ++ allow]

-- | Use 'outputError' to output and log a 500 Internal Server Error.
outputInternalServerError :: (MonadIO m, MonadCGI m) =>
                             [String] -- ^ Error information.
                          -> m CGIResult
outputInternalServerError es = outputError 500 "Internal Server Error" es


--
-- * Environment variables
--

-- | Get the value of a CGI environment variable. Example:
--
-- > remoteAddr <- getVar "REMOTE_ADDR"
getVar :: MonadCGI m =>
          String             -- ^ The name of the variable.
       -> m (Maybe String)
getVar name = liftM (Map.lookup name) $ cgiGet cgiVars

getVarWithDefault :: MonadCGI m =>
                     String -- ^ The name of the variable.
                  -> String -- ^ Default value 
                  -> m String
getVarWithDefault name def = liftM (fromMaybe def) $ getVar name

-- | Get all CGI environment variables and their values.
getVars :: MonadCGI m =>
           m [(String,String)]
getVars = liftM Map.toList $ cgiGet cgiVars


-- | The server\'s hostname, DNS alias, or IP address as it would 
--   appear in self-referencing URLs.
serverName :: MonadCGI m => m String
serverName = getVarWithDefault "SERVER_NAME" ""

-- | The port number to which the request was sent.
serverPort :: MonadCGI m => m Int
serverPort = liftM (fromMaybe 80 . (>>= maybeRead)) (getVar "SERVER_PORT")

-- |  The method with which the request was made. 
--    For HTTP, this is \"GET\", \"HEAD\", \"POST\", etc.
requestMethod :: MonadCGI m => m String
requestMethod = getVarWithDefault "REQUEST_METHOD" "GET"

-- | The extra path information, as given by the client.
--   This is any part of the request path that follows the
--   CGI program path.
--   If the string returned by this function is not empty,
--   it is guaranteed to start with a @\'\/\'@.
--
-- Note that this function returns an unencoded string.
-- Make sure to percent-encode any characters
-- that are not allowed in URI paths before using the result of
-- this function to construct a URI.
-- See 'progURI', 'queryURI' and 'requestURI' for a higher-level
-- interface.
pathInfo :: MonadCGI m => m String
pathInfo = liftM slash $ getVarWithDefault "PATH_INFO" ""
  where slash s = if not (null s) && head s /= '/' then '/':s else s

-- | The path returned by 'pathInfo', but with virtual-to-physical
--   mapping applied to it.
pathTranslated :: MonadCGI m => m String
pathTranslated = getVarWithDefault "PATH_TRANSLATED" ""

-- | A virtual path to the script being executed,  
-- used for self-referencing URIs. 
--
-- Note that this function returns an unencoded string.
-- Make sure to percent-encode any characters
-- that are not allowed in URI paths before using the result of
-- this function to construct a URI.
-- See 'progURI', 'queryURI' and 'requestURI' for a higher-level
-- interface.
scriptName :: MonadCGI m => m String
scriptName = getVarWithDefault "SCRIPT_NAME" ""

-- | The information which follows the ? in the URL which referenced 
--   this program. This is the percent-encoded query information.
--   For most normal uses, 'getInput' and friends are probably
--   more convenient.
queryString :: MonadCGI m => m String
queryString = getVarWithDefault "QUERY_STRING" ""

-- | The hostname making the request. If the server does not have
--   this information, Nothing is returned. See also 'remoteAddr'.
remoteHost :: MonadCGI m => m (Maybe String)
remoteHost = getVar "REMOTE_HOST"

-- | The IP address of the remote host making the request.
remoteAddr :: MonadCGI m => m String
remoteAddr = getVarWithDefault "REMOTE_ADDR" ""

-- | If the server supports user authentication, and the script is 
-- protected, this is the protocol-specific authentication method 
-- used to validate the user.
authType :: MonadCGI m => m (Maybe String)
authType = getVar "AUTH_TYPE"

-- | If the server supports user authentication, and the script is 
--   protected, this is the username they have authenticated as.
remoteUser :: MonadCGI m => m (Maybe String)
remoteUser = getVar "REMOTE_USER"

-- | For queries which have attached information, such as 
--   HTTP POST and PUT, this is the content type of the data.
--   You can use 'parseContentType' to get a structured
--   representation of the the content-type value.
requestContentType :: MonadCGI m => m (Maybe String)
requestContentType = getVar "CONTENT_TYPE"

-- | For queries which have attached information, such as 
--   HTTP POST and PUT, this is the length of the content 
--   given by the client.
requestContentLength :: MonadCGI m => m (Maybe Int)
requestContentLength = liftM (>>= maybeRead) $ getVar "CONTENT_LENGTH"

-- | Gets the value of the request header with the given name.
--   The header name is case-insensitive.
--   Example:
--
-- > requestHeader "User-Agent"
requestHeader :: MonadCGI m => String -> m (Maybe String)
requestHeader name = getVar var
  where var = "HTTP_" ++ map toUpper (replace '-' '_' name)


--
-- * Program and request URI
--

-- | Attempts to reconstruct the absolute URI of this program. 
--   This does not include
--   any extra path information or query parameters. See
--   'queryURI' for that.
--   If the server is rewriting request URIs, this URI can
--   be different from the one requested by the client.
--   See also 'requestURI'.
--
-- Characters in the components of the returned URI are escaped 
-- when needed, as required by "Network.URI".
progURI :: MonadCGI m => m URI
progURI =
    do -- Use HTTP_HOST if available, otherwise SERVER_NAME
       h <- requestHeader "Host" >>= maybe serverName return
       p <- serverPort
       name <- scriptName
       https <- liftM (maybe False (const True)) (getVar "HTTPS")
       -- SERVER_PORT might not be the port that the client used
       -- if the server listens on multiple ports, so we give priority
       -- to the port in HTTP_HOST.
       -- HTTP_HOST should include the port according to RFC2616 sec 14.23
       -- Some servers (e.g. lighttpd) also seem to include the port in 
       -- SERVER_NAME. 
       -- We include the port if it is in HTTP_HOST or SERVER_NAME, or if
       -- it is a non-standard port.
       let (host,port) = case break (==':') h of
                           (_,"")  -> (h, if (not https && p == 80) 
                                            || (https && p == 443) 
                                           then "" else ':':show p)
                           (h',p') -> (h',p')
       let auth = URIAuth { uriUserInfo = "", 
                            uriRegName = host,
                            uriPort = port }
       return $ nullURI { uriScheme = if https then "https:" else "http:", 
                          uriAuthority = Just auth,
                          uriPath = escapePath name }

-- | Like 'progURI', but the returned 'URI' also includes
--   any extra path information, and any query parameters.
--   If the server is rewriting request URIs, this URI can
--   be different from the one requested by the client.
--   See also 'requestURI'.
--
-- Characters in the components of the returned URI are escaped 
-- when needed, as required by "Network.URI".
queryURI :: MonadCGI m => m URI
queryURI = 
    do uri  <- progURI
       path <- pathInfo
       qs   <- liftM (\q -> if null q then q else '?':q) $ queryString
       return $ uri { uriPath = uriPath uri ++ escapePath path, 
                      uriQuery = qs } 

-- | Does percent-encoding as needed for URI path components.
escapePath :: String -> String
escapePath = escapeURIString isUnescapedInURIPath
  where isUnescapedInURIPath c = isUnescapedInURI c && c `notElem` "?#"

-- | Attempts to reconstruct the absolute URI requested by the client,
--   including extra path information and query parameters.
--   If no request URI rewriting is done, or if the web server does not
--   provide the information needed to reconstruct the request URI,
--   this function returns the same value as 'queryURI'.
--
-- Characters in the components of the returned URI are escaped 
-- when needed, as required by "Network.URI".
requestURI :: MonadCGI m => m URI
requestURI =
    do uri <- queryURI
       -- Apache sets REQUEST_URI to the original request URI,
       -- with percent-encoding intact.
       mreq <- liftM (>>= parseRelativeReference) $ getVar "REQUEST_URI"
       return $ case mreq of
                 Nothing  -> uri
                 Just req -> uri { 
                                  uriPath  = uriPath req,
                                  uriQuery = uriQuery req
                                 }


--
-- * Inputs
--

-- | Get the value of an input variable, for example from a form.
--   If the variable has multiple values, the first one is returned.
--   Example:
--
-- > query <- getInput "query"
getInput :: MonadCGI m =>
            String           -- ^ The name of the variable.
         -> m (Maybe String) -- ^ The value of the variable,
                             --   or Nothing, if it was not set.
getInput = liftM (fmap BS.unpack) . getInputFPS

-- | Like 'getInput', but returns a 'ByteString'.
getInputFPS :: MonadCGI m =>
            String           -- ^ The name of the variable.
         -> m (Maybe ByteString) -- ^ The value of the variable,
                             --   or Nothing, if it was not set.
getInputFPS = liftM (fmap inputValue) . getInput_

-- | Get all the values of an input variable, for example from a form.
-- This can be used to get all the values from form controls
-- which allow multiple values to be selected.
-- Example:
--
-- > vals <- getMultiInput "my_checkboxes"
getMultiInput :: MonadCGI m => 
                 String -- ^ The name of the variable.
              -> m [String] -- ^ The values of the variable,
                            -- or the empty list if the variable was not set.
getMultiInput n = do is <- cgiGet cgiInputs
                     return [BS.unpack (inputValue v) | (p,v) <- is, p == n]

-- | Get the file name of an input.
getInputFilename :: MonadCGI m =>
                    String           -- ^ The name of the variable.
                 -> m (Maybe String) -- ^ The file name corresponding to the
                                     -- input, if there is one.
getInputFilename = liftM (>>= inputFilename) . getInput_

-- | Get the content-type of an input, if the input exists, e.g. "image\/jpeg".
--   For non-file inputs, this function returns "text\/plain".
--   You can use 'parseContentType' to get a structured
--   representation of the the content-type value.
getInputContentType :: MonadCGI m =>
                       String   -- ^ The name of the variable.
                    -> m (Maybe String) -- ^ The content type, formatted as a string.
getInputContentType = 
    liftM (fmap (showContentType . inputContentType)) . getInput_

-- | Same as 'getInput', but tries to read the value to the desired type.
readInput :: (Read a, MonadCGI m) =>
             String        -- ^ The name of the variable.
          -> m (Maybe a) -- ^ 'Nothing' if the variable does not exist
                           --   or if the value could not be interpreted
                           --   at the desired type.
readInput = liftM (>>= maybeRead) . getInput

-- | Get the names and values of all inputs.
--   Note: the same name may occur more than once in the output,
--   if there are several values for the name.
getInputs :: MonadCGI m => m [(String,String)]
getInputs = do is <- cgiGet cgiInputs
               return [ (n, BS.unpack (inputValue i)) | (n,i) <- is ]

-- | Get the names of all input variables.
getInputNames :: MonadCGI m => m [String]
getInputNames = (sortNub . map fst) `liftM` cgiGet cgiInputs
    where sortNub = map head . group . sort

-- Internal stuff

getInput_ ::  MonadCGI m => String -> m (Maybe Input)
getInput_ n = lookup n `liftM` cgiGet cgiInputs


--
-- * Cookies
--

-- | Get the value of a cookie.
getCookie :: MonadCGI m =>
             String           -- ^ The name of the cookie.
          -> m (Maybe String) -- ^ 'Nothing' if the cookie does not exist.
getCookie name = liftM (>>= findCookie name) (getVar "HTTP_COOKIE")

-- | Same as 'getCookie', but tries to read the value to the desired type.
readCookie :: (Read a, MonadCGI m) =>
              String       -- ^ The name of the cookie.
            -> m (Maybe a) -- ^ 'Nothing' if the cookie does not exist
                           --   or if the value could not be interpreted
                           --   at the desired type.
readCookie = liftM (>>= maybeRead) . getCookie

-- | Set a cookie.
setCookie :: MonadCGI m => Cookie -> m ()
setCookie = setHeader "Set-cookie" . showCookie

-- | Delete a cookie from the client
deleteCookie :: MonadCGI m => Cookie -> m ()
deleteCookie = setCookie . Cookie.deleteCookie


--
-- * Headers
--

-- | Add a response header. 
--   Example:
--
-- > setHeader "Content-type" "text/plain"
setHeader :: MonadCGI m =>
             String -- ^ Header name.
          -> String -- ^ Header value.
          -> m ()
setHeader n v = cgiAddHeader (HeaderName n) v

-- | Set the HTTP response status.
setStatus :: MonadCGI m =>
             Int -- ^  HTTP status code, e.g. @404@
          -> String -- ^ HTTP status message, e.g. @"Not Found"@
          -> m ()
setStatus c m = setHeader "Status" (show c ++ " " ++ m)