Haskell Hierarchical Libraries (cgi package)Source codeContentsIndex
Network.CGI
Portabilitynon-portable (uses Control.Monad.State)
Stabilityexperimental
Maintainerbjorn@bringert.net
Contents
CGI monad
Error handling
Logging
Output
Error pages
Input
Environment
Program and request URI
Content type
Cookies
URL encoding
Compatibility with the old API
Description

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)
Synopsis
class Monad m => MonadCGI m
data CGIT m a
data CGIResult
type CGI a = CGIT IO a
MonadIO (liftIO)
liftIO
runCGI :: MonadIO m => CGIT m CGIResult -> m ()
throwCGI :: (MonadCGI m, MonadIO m) => Exception -> m a
catchCGI :: CGI a -> (Exception -> CGI a) -> CGI a
tryCGI :: CGI a -> CGI (Either Exception a)
handleExceptionCGI :: CGI a -> (Exception -> CGI a) -> CGI a
handleErrors :: CGI CGIResult -> CGI CGIResult
logCGI :: MonadIO m => String -> m ()
output :: MonadCGI m => String -> m CGIResult
outputFPS :: MonadCGI m => ByteString -> m CGIResult
outputNothing :: MonadCGI m => m CGIResult
redirect :: MonadCGI m => String -> m CGIResult
setHeader :: MonadCGI m => String -> String -> m ()
setStatus :: MonadCGI m => Int -> String -> m ()
outputError :: (MonadCGI m, MonadIO m) => Int -> String -> [String] -> m CGIResult
outputException :: (MonadCGI m, MonadIO m) => Exception -> m CGIResult
outputNotFound :: (MonadIO m, MonadCGI m) => String -> m CGIResult
outputMethodNotAllowed :: (MonadIO m, MonadCGI m) => [String] -> m CGIResult
outputInternalServerError :: (MonadIO m, MonadCGI m) => [String] -> m CGIResult
getInput :: MonadCGI m => String -> m (Maybe String)
getInputFPS :: MonadCGI m => String -> m (Maybe ByteString)
readInput :: (Read a, MonadCGI m) => String -> m (Maybe a)
getInputs :: MonadCGI m => m [(String, String)]
getInputNames :: MonadCGI m => m [String]
getMultiInput :: MonadCGI m => String -> m [String]
getInputFilename :: MonadCGI m => String -> m (Maybe String)
getInputContentType :: MonadCGI m => String -> m (Maybe String)
getVar :: MonadCGI m => String -> m (Maybe String)
getVarWithDefault :: MonadCGI m => String -> String -> m String
getVars :: MonadCGI m => m [(String, String)]
serverName :: MonadCGI m => m String
serverPort :: MonadCGI m => m Int
requestMethod :: MonadCGI m => m String
pathInfo :: MonadCGI m => m String
pathTranslated :: MonadCGI m => m String
scriptName :: MonadCGI m => m String
queryString :: MonadCGI m => m String
remoteHost :: MonadCGI m => m (Maybe String)
remoteAddr :: MonadCGI m => m String
authType :: MonadCGI m => m (Maybe String)
remoteUser :: MonadCGI m => m (Maybe String)
requestContentType :: MonadCGI m => m (Maybe String)
requestContentLength :: MonadCGI m => m (Maybe Int)
requestHeader :: MonadCGI m => String -> m (Maybe String)
progURI :: MonadCGI m => m URI
queryURI :: MonadCGI m => m URI
requestURI :: MonadCGI m => m URI
data ContentType = ContentType {
ctType :: String
ctSubtype :: String
ctParamaters :: [(String, String)]
}
showContentType :: ContentType -> String
parseContentType :: Monad m => String -> m ContentType
data Cookie = Cookie {
cookieName :: String
cookieValue :: String
cookieExpires :: (Maybe CalendarTime)
cookieDomain :: (Maybe String)
cookiePath :: (Maybe String)
cookieSecure :: Bool
}
newCookie :: String -> String -> Cookie
getCookie :: MonadCGI m => String -> m (Maybe String)
readCookie :: (Read a, MonadCGI m) => String -> m (Maybe a)
setCookie :: MonadCGI m => Cookie -> m ()
deleteCookie :: MonadCGI m => Cookie -> m ()
formEncode :: [(String, String)] -> String
urlEncode :: String -> String
formDecode :: String -> [(String, String)]
urlDecode :: String -> String
module Network.CGI.Compat
CGI monad
class Monad m => MonadCGI m
The class of CGI monads. Most CGI actions can be run in any monad which is an instance of this class, which means that you can use your own monad transformers to add extra functionality.
show/hide Instances
Monad m => MonadCGI (CGIT m)
(MonadTrans t, MonadCGI m, Monad (t m)) => MonadCGI (t m)
data CGIT m a
The CGIT monad transformer.
show/hide Instances
data CGIResult
The result of a CGI program.
show/hide Instances
type CGI a = CGIT IO a
A simple CGI monad with just IO.
MonadIO (liftIO)
liftIO
runCGI :: MonadIO m => CGIT m CGIResult -> m ()
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.
Error handling
throwCGI :: (MonadCGI m, MonadIO m) => Exception -> m a
Throw an exception in a CGI monad. The monad is required to be a MonadIO, so that we can use throwIO to guarantee ordering.
catchCGI :: CGI a -> (Exception -> CGI a) -> CGI a
Catches any expection thrown by a CGI action, and uses the given exception handler if an exception is thrown.
tryCGI :: CGI a -> CGI (Either Exception a)
Catches any exception thrown by an CGI action, and returns either the exception, or if no exception was raised, the result of the action.
handleExceptionCGI :: CGI a -> (Exception -> CGI a) -> CGI a
Deprecated version of catchCGI. Use catchCGI instead.
handleErrors :: CGI CGIResult -> CGI CGIResult

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)
Logging
logCGI :: MonadIO m => String -> m ()
Log some message using the server's logging facility. FIXME: does this have to be more general to support FastCGI etc? Maybe we should store log messages in the CGIState?
Output
output
:: MonadCGI m
=> StringThe string to output.
-> m CGIResult
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.
outputFPS
:: MonadCGI m
=> ByteStringThe string to output.
-> m CGIResult
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.
outputNothing :: MonadCGI m => m CGIResult
Do not output anything (except headers).
redirect
:: MonadCGI m
=> StringA URL to redirect to.
-> m CGIResult
Redirect to some location.
setHeader
:: MonadCGI m
=> StringHeader name.
-> StringHeader value.
-> m ()

Add a response header. Example:

 setHeader "Content-type" "text/plain"
setStatus
:: MonadCGI m
=> IntHTTP status code, e.g. 404
-> StringHTTP status message, e.g. Not Found
-> m ()
Set the HTTP response status.
Error pages
outputError
:: (MonadCGI m, MonadIO m)
=> IntHTTP Status code
-> StringStatus message
-> [String]Error information
-> m CGIResult
Output an error page to the user, with the given HTTP status code in the response. Also logs the error information using logCGI.
outputException :: (MonadCGI m, MonadIO m) => Exception -> m CGIResult
Output a 500 Internal Server Error with information from an Exception.
outputNotFound
:: (MonadIO m, MonadCGI m)
=> StringThe name of the requested resource.
-> m CGIResult
Use outputError to output and log a 404 Not Found error.
outputMethodNotAllowed
:: (MonadIO m, MonadCGI m)
=> [String]The allowed methods.
-> m CGIResult
Use outputError to output and log a 405 Method Not Allowed error.
outputInternalServerError
:: (MonadIO m, MonadCGI m)
=> [String]Error information.
-> m CGIResult
Use outputError to output and log a 500 Internal Server Error.
Input
getInput
:: MonadCGI m
=> StringThe name of the variable.
-> m (Maybe String)The value of the variable, or Nothing, if it was not set.

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"
getInputFPS
:: MonadCGI m
=> StringThe name of the variable.
-> m (Maybe ByteString)The value of the variable, or Nothing, if it was not set.
Like getInput, but returns a ByteString.
readInput
:: (Read a, MonadCGI m)
=> StringThe 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.
Same as getInput, but tries to read the value to the desired type.
getInputs :: MonadCGI m => m [(String, String)]
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.
getInputNames :: MonadCGI m => m [String]
Get the names of all input variables.
getMultiInput
:: MonadCGI m
=> StringThe name of the variable.
-> m [String]The values of the variable, or the empty list if the variable was not set.

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"
getInputFilename
:: MonadCGI m
=> StringThe name of the variable.
-> m (Maybe String)The file name corresponding to the input, if there is one.
Get the file name of an input.
getInputContentType
:: MonadCGI m
=> StringThe name of the variable.
-> m (Maybe String)The content type, formatted as a string.
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.
Environment
getVar
:: MonadCGI m
=> StringThe name of the variable.
-> m (Maybe String)

Get the value of a CGI environment variable. Example:

 remoteAddr <- getVar "REMOTE_ADDR"
getVarWithDefault
:: MonadCGI m
=> StringThe name of the variable.
-> StringDefault value
-> m String
getVars :: MonadCGI m => m [(String, String)]
Get all CGI environment variables and their values.
serverName :: MonadCGI m => m String
The server's hostname, DNS alias, or IP address as it would appear in self-referencing URLs.
serverPort :: MonadCGI m => m Int
The port number to which the request was sent.
requestMethod :: MonadCGI m => m String
The method with which the request was made. For HTTP, this is "GET", "HEAD", "POST", etc.
pathInfo :: MonadCGI m => m String
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 '/'.
pathTranslated :: MonadCGI m => m String
The path returned by pathInfo, but with any virtual-to-physical mapping applied to it.
scriptName :: MonadCGI m => m String
A virtual path to the script being executed, used for self-referencing URLs.
queryString :: MonadCGI m => m String
The information which follows the ? in the URL which referenced this program. This is the encoded query information. For most normal uses, getInput and friends are probably more convenient.
remoteHost :: MonadCGI m => m (Maybe String)
The hostname making the request. If the server does not have this information, Nothing is returned. See also remoteAddr.
remoteAddr :: MonadCGI m => m String
The IP address of the remote host making the request.
authType :: MonadCGI m => m (Maybe String)
If the server supports user authentication, and the script is protected, this is the protocol-specific authentication method used to validate the user.
remoteUser :: MonadCGI m => m (Maybe String)
If the server supports user authentication, and the script is protected, this is the username they have authenticated as.
requestContentType :: MonadCGI m => m (Maybe String)
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.
requestContentLength :: MonadCGI m => m (Maybe Int)
For queries which have attached information, such as HTTP POST and PUT, this is the length of the content given by the client.
requestHeader :: MonadCGI m => String -> m (Maybe String)

Gets the value of the request header with the given name. The header name is case-insensitive. Example:

 requestHeader "User-Agent"
Program and request URI
progURI :: MonadCGI m => m 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.
queryURI :: MonadCGI m => m URI
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.
requestURI :: MonadCGI m => m URI
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.
Content type
data ContentType
A MIME media type value. The Show instance is derived automatically. Use showContentType to obtain the standard string representation. See http://www.ietf.org/rfc/rfc2046.txt for more information about MIME media types.
Constructors
ContentType
ctType :: StringThe top-level media type, the general type of the data. Common examples are "text", "image", "audio", "video", "multipart", and "application".
ctSubtype :: StringThe media subtype, the specific data format. Examples include "plain", "html", "jpeg", "form-data", etc.
ctParamaters :: [(String, String)]Media type parameters. On common example is the charset parameter for the "text" top-level type, e.g. ("charset","ISO-8859-1").
show/hide Instances
showContentType :: ContentType -> String
Produce the standard string representation of a content-type, e.g. "text/html; charset=ISO-8859-1".
parseContentType :: Monad m => String -> m ContentType
Parse the standard representation of a content-type. If the input cannot be parsed, this function calls fail with a (hopefully) informative error message.
Cookies
data Cookie
Contains all information about a cookie set by the server.
Constructors
Cookie
cookieName :: StringName of the cookie.
cookieValue :: StringValue of the cookie.
cookieExpires :: (Maybe CalendarTime)Expiry date of the cookie. If Nothing, the cookie expires when the browser sessions ends. If the date is in the past, the client should delete the cookie immediately.
cookieDomain :: (Maybe String)The domain suffix to which this cookie will be sent.
cookiePath :: (Maybe String)The path to which this cookie will be sent.
cookieSecure :: BoolTrue if this cookie should only be sent using secure means.
show/hide Instances
newCookie
:: StringName
-> StringValue
-> CookieCookie
Construct a cookie with only name and value set. This client will expire when the browser sessions ends, will only be sent to the server and path which set it and may be sent using any means.
getCookie
:: MonadCGI m
=> StringThe name of the cookie.
-> m (Maybe String)Nothing if the cookie does not exist.
Get the value of a cookie.
readCookie
:: (Read a, MonadCGI m)
=> StringThe 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.
Same as getCookie, but tries to read the value to the desired type.
setCookie :: MonadCGI m => Cookie -> m ()
Set a cookie.
deleteCookie :: MonadCGI m => Cookie -> m ()
Delete a cookie from the client
URL encoding
formEncode :: [(String, String)] -> String
Format name-value pairs as application/x-www-form-urlencoded.
urlEncode :: String -> String
Convert a single value to the application/x-www-form-urlencoded encoding.
formDecode :: String -> [(String, String)]
Get the name-value pairs from application/x-www-form-urlencoded data.
urlDecode :: String -> String
Convert a single value from the application/x-www-form-urlencoded encoding.
Compatibility with the old API
module Network.CGI.Compat
Produced by Haddock version 0.8