Getting started with CGI programming using Ruby
This article mainly introduces how to use Ruby for CGI programming. Writing CGI scripts is also a basic function of Ruby for network programming. For more information, see
Compile the CGI script:
The most basic Ruby CGI script looks like this:
?
1 2 3 4 5 |
#! /Usr/bin/ruby Puts "HTTP/1.0 200 OK" Puts "Content-type: text/htmlnn" Puts " |
If you call this script test. cgi to upload it to a Web hosting service provider based on Unix/Linux and have the appropriate permissions, you can use it as a CGI script.
For example, if there is a Linux Web hosting service provider hosting a website, such as the home directory of the http://www.yiibai.com/test.cgi, and gives it the execution permission, then accessing the http://www.yiibai.com/test.cgi should return an HTML page display: this is a test.
Here, when test. cgi requests from a Web browser, the Web server will use the Ruby interpreter for execution. The Ruby script returns a basic HTTP header and a basic HTML document.
Use cgi. rb:
Ruby has a special CGI library that makes the interaction more complex than the previous CGI script.
Let's create a basic CGI Using CGI script:
?
1 2 3 4 5 6 7 |
#! /Usr/bin/ruby Require 'cgi' Cgi = CGI. new Puts cgi. header Puts " |
Here, a CGI object is created and used to print the title line.
Form Processing:
You can use CGI to access HTML query parameters in two ways. Suppose we give/cgi-bin/test. cgi? FirstName = Zara & LastName = Ali.
You can access the FirstName and LastName Parameters Using CGI # [] as follows:
?
1 2 3 4 5 6 |
#! /Usr/bin/ruby Require 'cgi' Cgi = CGI. new Cgi ['firstname'] # => ["Zara"] Cgi ['lastname'] # => ["Ali"] |
There is another way to access these form variables. This code returns the hash values of all items and values:
?
1 2 3 4 5 6 7 |
#! /Usr/bin/ruby Require 'cgi' Cgi = CGI. new H = cgi. params #=>{ "FirstName" => ["Zara"], "LastName" => ["Ali"]} H ['firstname'] # => ["Zara"] H ['lastname'] # => ["Ali"] |
The following code Retrieves all the keys:
?
1 2 3 4 5 |
#! /Usr/bin/ruby Require 'cgi' Cgi = CGI. new Cgi. keys # => ["FirstName", "LastName"] |
If the form contains multiple fields with the same name, the corresponding values are returned to the script as an array. [] The accessors return the first one.
In this example, assume that the form named "name" has three fields, and we enter three names: "Zara", "Huma" and "Nuha ":
?
1 2 3 4 5 6 7 8 |
#! /Usr/bin/ruby Require 'cgi' Cgi = CGI. new Cgi ['name'] # => "Zara" Cgi. params ['name'] # => ["Zara", "Huma", "Nuha"] Cgi. keys # => ["name"] Cgi. params # => {"name" => ["Zara", "Huma", "Nuha"]} |
Note: Ruby will automatically process the GET and POST methods. There are no separate methods to handle these two differences.
A related but basic form that can send the correct data will have HTML code like this:
?
1 2 3 4 5 6 7 8 9 10 11 |
<Html> <Body> <Form method = "POST" action = "http://www.example.com/test.cgi"> First Name: <input type = "text" name = "FirstName" value = ""/> <Br/> Last Name: <input type = "text" name = "LastName" value = ""/> <Input type = "submit" value = "Submit Data"/> </Form> </Body> </Html> |
Create a form and HTML:
CGI contains a large number of methods for creating HTML. You will find one of the methods for each label. To use these methods, you must call CGI. new to create a CGI object.
To make tags easier to nest, these methods use their own content as code blocks. The code block returns a string, which is in the content of the tag. For example:
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
#! /Usr/bin/ruby Require "cgi" Cgi = CGI. new ("html4 ") Cgi. out { Cgi.html { Cgi. head {"n" + cgi. title {"This Is a Test" }}+ Cgi. body {"n" + Cgi. form {"n" + Cgi. hr + Cgi. h1 {"A Form:"} + "n" + Cgi. textarea ("get_text") + "n" + Cgi.br + Cgi. submit } } } } |
Note: The method parameters that can be accepted by CGI methods are set to form submission used on HTTP methods (such as GET and POST. By default, POST is used in this example.
This produces the following results:
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
Content-Type: text/html Content-Length: 302 <! Doctype html public "-// W3C // dtd html 4.0 Final // EN"> <HTML> <HEAD> <TITLE> This Is a Test </TITLE> </HEAD> <BODY> <Form method = "post" ENCTYPE = "application/x-www-form-urlencoded"> <HR> <H1> A Form: </H1> <Textarea cols = "70" NAME = "get_text" ROWS = "10"> </TEXTAREA> <BR> <Input type = "submit"> </FORM> </BODY> </HTML> |
Reference string:
When processing URL and HTML code, you must carefully reference some characters. For example, a slash character (/) has special meaning in the URL, so it must be escaped if it is not part of the path name.
For example, the/query URL part will be translated into the string % 2F/, which must be translated when used. Space and special characters with symbols. To solve this problem, CGI provides: CGI. escape and CGI. unescape.
?
1 2 3 4 |
#! /Usr/bin/ruby Require 'cgi' Puts CGI. escape (Zara Ali/A Sweet & Sour Girl ") |
This produces the following results:
?
1 2 3 4 5 6 |
Zara + Ali % 2FA Sweet + % 26 + Sour + Girl ") #! /Usr/bin/ruby Require 'cgi' Puts CGI. escapeHTML (' |
This produces the following results:
?
1 |
<H1> Zara Ali/A Sweet & Sour Girl |