class CGI
|
Parent:
|
Object
|
Version:
|
1.6
|
|
Index:
escape
escapeElement
escapeHTML
new
parse
pretty
rfc1123_date
unescape
unescapeElement
unescapeHTML
[ ]
cookies
has_key?
header
keys
out
params
require "cgi"
cgi = CGI.new("html3") # add HTML generation methods
cgi.out {
CGI.pretty (
cgi.html {
cgi.head { cgi.title{"TITLE"} } +
cgi.body {
cgi.form {
cgi.textarea("get_text") +
cgi.br +
cgi.submit
} +
cgi.h1 { "This is big!" } +
cgi.center { "Jazz Greats of the 20" +
cgi.small {"th"} + " century" + cgi.hr
} + cgi.p + cgi.table ('BORDER' => '5') {
cgi.tr { cgi.td {"Artist"} + cgi.td {"Album"} } +
cgi.tr { cgi.td {"Davis, Miles"} +
cgi.td {"Kind of Blue"} }
}
}
}
) # CGI.pretty is a method call, not a block
}
|
(The output of this script is shown in Figure 26.2 on page 499.)
The
CGI
class provides support for programs used as a Web server
CGI (Common Gateway Interface) script. It contains several
methods for accessing fields in a CGI form, manipulating ``cookies'' and
the environment, and outputting formatted HTML.
Since environment variables contain a lot of useful information for
a CGI script,
CGI
makes accessing them very easy---environment
variables are accessible as attributes of
CGI
objects. For
instance,
cgi.auth_type
returns the value of
ENV["AUTH_TYPE"]
. To create the method name, the environment
variable name is translated to all lowercase, and the
``
HTTP_
'' prefix is stripped off. Thus,
HTTP_USER_AGENT
would be available as the method
user_agent
.
Cookies are represented using a separate object of class
CGI::Cookie
, containing the following accessors:
Accessor
|
Description
|
name
|
Name of this cookie |
value
|
Array of values |
path
|
Path (optional) |
domain
|
Domain (optional) |
expires
|
Time of expiry, defaults to
Time.now
(optional) |
secure
|
true for a secure cookie |
 |
You create a cookie object using
CGI_Cookie.new
, which takes
as arguments the accessors listed above, or
CGI_Cookie.parse
,
which takes an encoded string and returns a cookie object.
class methods
|
escape
|
CGI.escape( aString )
-> aNewString
|
|
Returns a URL-encoded string made from the given argument, where unsafe
characters (not alphanumeric, ``_'', ``-'', or ``.'') are encoded
using ``%xx'' escapes.
|
escapeElement
|
CGI.escapeElement( aString
[, elements
]*
)
-> aNewString
|
|
Returns a string made from the given argument with certain
HTML-special characters escaped. The HTML elements given in
elements will be escaped; other HTML elements will not
be affected.
print CGI::escapeElement('<BR><A HREF="url"></A><P>', "A", "IMG")
|
produces:
<BR><A HREF="url"></A><P>
|
|
escapeHTML
|
CGI.escapeHTML( aString )
-> aNewString
|
|
Returns a string made from the given argument with
HTML-special characters (such as
``& '',``" '',``< '',``> '') quoted using
``& '', ``" '', ``< '',
``> '', and so on.
|
new
|
CGI.new( [
aString
]*
)
-> aSession
|
|
Returns a new CGI object. If HTML output is required, the
desired standards level must be given in aString
(otherwise, no output routines will be created). The level may be
one of:
String
|
Standards Level
|
String
|
Standards Level
|
``html3'' |
HTML 3.2 |
``html4'' |
HTML 4.0 Strict |
``html4Tr'' |
HTML 4.0 Transitional |
``html4Fr'' |
HTML 4.0 Frameset |
 |
|
parse
|
CGI.parse( aString )
-> aHash
|
|
Parses a query string and returns
a hash of its key-value pairs.
|
pretty
|
CGI.pretty( anHTMLString,
aLeaderString=" " )
-> aSession
|
|
Formats the given anHTMLString in a nice, readable
format, optionally prefixing each line with
aLeaderString.
|
rfc1123_date
|
CGI.rfc1123_date( aTime )
-> aString
|
|
Returns a string representing the given time according to
RFC 1123 (for instance, Mon, 1 Jan 2001 00:00:00 GMT ).
|
unescape
|
CGI.unescape( aString )
-> aNewString
|
|
Returns a string containing ``unsafe'' characters made from
the given URL-encoded argument, where unsafe characters were
encoded using ``%'' escapes.
|
unescapeElement
|
CGI.unescapeElement( aString
[, elements
]*
)
-> aNewString
|
|
Returns a string with the selected escaped HTML elements
expanded to the actual characters.
|
unescapeHTML
|
CGI.unescapeHTML( aString )
-> aNewString
|
|
Returns a string made from the given argument with
HTML-special quoted characters expanded to the actual
characters.
|
instance methods
|
[ ]
|
aSession[ [
aString
]+
]
-> anArray
|
|
Returns the values of the given field names from the CGI
form in an Array . See the note on multipart forms on page 503.
|
cookies
|
aSession.cookies
-> aHash
|
|
Returns a new Hash object containing key-value
pairs of cookie keys and values.
|
has_key?
|
aSession.has_key( aString )
-> true or false
|
|
Returns true if the form contains a field named
aString.
|
header
|
aSession.header( aContentType="text/html" )
-> aString
aSession.header( aHash )
-> aString
|
|
Returns a string containing the given headers
(in the MOD_RUBY environment, the resulting header is
sent immediately instead). If a hash is given as an
argument, then the key-value pairs will be used to
generate headers.
|
keys
|
aSession.keys
-> anArray
|
|
Returns an array of all existing field names for the form.
|
out
|
aSession.out( aContentType="text/html" ) { block }
-> nil
aSession.out( aHash ) { block }
-> nil
|
|
Generates HTML output using the results of the block as the
content. Headers are generated as with CGI#header .
See the example at the start of this section.
|
params
|
aSession.params
-> aHash
|
|
Returns a new Hash object containing key-value
pairs of field names and values from the form.
|