Name

CGI — CGI support class

Synopsis

CGI provides useful features to implement Common Gateway Interface (CGI) programs, such as retrieving CGI data from server, manipulating cookies, and generating the HTTP header and the HTML body.

Example

require 'cgi'

     cgi = CGI::new("html3")

     input, = cgi["input"]
     if input
       input = CGI::unescape(input)
     end
     p input

     begin
       value = Thread::new{
         $SAFE=4
         eval input
       }.value.inspect
     rescue SecurityError
       value = "Sorry, you can't do this"
     end

     cgi.out {
       cgi.html{
         cgi.head{cgi.title{"Walter's Web Arithmetic Page"}} +
         cgi.body{
           cgi.form("post", "/cgi-bin/arith.rb") {
             "input your favorite expression: " +
             cgi.text_field("input", input) +
             cgi.br +
             "the result of you input: " +
             CGI::escapeHTML(value) +
             cgi.br +
             cgi.submit
           }
         }
       }
     }

Required Library

require ‘cgi’

Class Methods

CGI::new([level="query"])

Creates a CGI object. level may be one of the following options. If one of the HTML levels is specified, the following methods are defined for generating output conforming to that level:

query

No HTML output generated

html3

HTML3.2

html4

HTML4.0 Strict

html4Tr

HTML4.0 Transitional

html4Fr

HTML4.0 Frameset

CGI::escape(str)

Escapes an unsafe string using URL-encoding.

CGI::unescape(str)

Expands a string that has been escaped using URL-encoding.

CGI::escapeHTML(str)

Escapes HTML special characters, including: & < >.

CGI::unescapeHTML(str)

Expands escaped HTML special characters, including: & < >.

CGI::escapeElement(str[, element...])

Escapes HTML special characters in the specified HTML elements. ...

Get Ruby in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.