Follow Techotopia on Twitter

On-line Guides
All Guides
eBook Store
iOS / Android
Linux for Beginners
Office Productivity
Linux Installation
Linux Security
Linux Utilities
Linux Virtualization
Linux Kernel
System/Network Admin
Scripting Languages
Development Tools
Web Development
GUI Toolkits/Desktop
Mail Systems
Eclipse Documentation

How To Guides
General System Admin
Linux Security
Linux Filesystems
Web Servers
Graphics & Desktop
PC Hardware
Problem Solutions
Privacy Policy




Ruby Programming
Previous Page Home Next Page

CGI Development

class CGI
Parent: Object
Version: 1.6


escape escapeElement escapeHTML new parse pretty rfc1123_date unescape unescapeElement unescapeHTML [ ] cookies has_key? header keys out params

require "cgi"
cgi ="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.h1 { "This is big!" } + { "Jazz Greats of the 20" +
          cgi.small {"th"} + " century" +
        } + cgi.p + cgi.table ('BORDER' => '5') {
 { {"Artist"} + {"Album"} } +
 { {"Davis, Miles"} +
 {"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 (optional)
secure true for a secure cookie

Figure not available...

You create a cookie object using, 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")
<BR>&lt;A HREF=&quot;url&quot;&gt;&lt;/A&gt;<P>

escapeHTML CGI.escapeHTML( aString ) -> aNewString

Returns a string made from the given argument with HTML-special characters (such as ``&'',``"'',``<'',``>'') quoted using ``&amp;'', ``&quot;'', ``&lt;'', ``&gt;'', and so on.

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.

Ruby Programming
Previous Page Home Next Page

  Published under the terms of the Open Publication License Design by Interspire