Ruby CGI programming
Ruby is a universal language, not just a language used for WEB development, but Ruby is the most common in the development of WEB applications and WEB tools.
With Ruby you can not only write your own SMTP server, FTP program, or Ruby web server, but you can also use Ruby for CGI programming.
Next, let’s take a moment to learn Ruby’s CGI editing.
Web browsing
In order to better understand how CGI works, we can start with the process of clicking a link or URL on a web page:
1. Use your browser to access the URL and connect to the HTTP web server.
2. After receiving the request information, the web server will parse the URL and check whether the accessed file exists on the server. If the file exists, it will return the content of the file, otherwise it will return an error message.
3. The browser receives information from the server and displays the received file or error message.
CGI program can be a Ruby script, Python script, PERL script, SHELL script, C or C++ program, etc.
CGI architecture diagram
Web server support and configuration
Before you perform CGI programming, make sure you The web server supports CGI and has CGI handlers configured.
Apache supports CGI configuration:
Set the CGI directory:
ScriptAlias /cgi-bin/ /var/www/cgi-bin/
All HTTP server execution CGI programs are saved in a pre-configured directory. This directory is called the CGI directory, and by convention, it is named /var/www/cgi-bin.
The extension of CGI files is .cgi, and Ruby can also use the .rb extension.
By default, the cgi-bin directory where the Linux server is configured to run is /var/www.
If you want to specify other directories for running CGI scripts, you can modify the httpd.conf configuration file as follows:
<Directory "/var/www/cgi-bin"> AllowOverride None Options +ExecCGI Order allow,deny Allow from all </Directory>
Add the .rb suffix in AddHandler so that we can access. Ruby script files ending with rb:
AddHandler cgi-script .cgi .pl .rb
Writing CGI scripts
The most basic Ruby CGI code is as follows:
#!/usr/bin/ruby puts "Content-type: text/html\n\n" puts "<html><body>This is a test</body></html>"
You can keep this code until In the test.cgi file, you can execute it as a CGI script after going to the server last time and granting sufficient permissions.
If the address of your site is http://www.example.com/, you can use http://www.example.com/test.cgi to access the program. The output result is: "This is a test."
After the browser accesses the URL, the web server will find the test.cgi file in the site directory, and then use the Ruby parser to parse the script code and access the HTML document.
Using cgi.rb
Ruby can call the CGI library to write more complex CGI scripts.
The following code calls the CGI library to create a CGI script for the script.
#!/usr/bin/ruby require 'cgi' cgi = CGI.new puts cgi.header puts "<html><body>This is a test</body></html>"
In the following code, a CGI object is created and the header information is printed.
Form processing
Using the CGI library, you can obtain the data submitted by the form (or parameters in the URL) in two ways, For example URL:/cgi-bin/test.cgi?FirstName=Zara&LastName=Ali.
You can use CGI#[] to directly obtain the parameters FirstName and LastName:
#!/usr/bin/ruby require 'cgi' cgi = CGI.new cgi['FirstName'] # => ["Zara"] cgi['LastName'] # => ["Ali"]
Another way to obtain form data:
#!/usr/bin/ruby require 'cgi' cgi = CGI.new h = cgi.params # => {"FirstName"=>["Zara"],"LastName"=>["Ali"]} h['FirstName'] # => ["Zara"] h['LastName'] # => ["Ali"]
The following code is used to retrieve all key values :
#!/usr/bin/ruby require 'cgi' cgi = CGI.new cgi.keys # => ["FirstName", "LastName"]
If the form contains multiple fields with the same name, the values of the same fields will be saved in the array.
In the following example, three identical fields "name" are specified in the form, and the values are "Zara", "Huma" and "Nuha":
#!/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 determine the GET and POST methods, so there is no need to treat the two methods differently.
The following is the relevant HML code:
<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>
Creating Form forms and HTML
CGI contains a large number of methods to create HTML, each HTML tag has corresponding method. Before using these methods, a CGI object must be created via CGI.new.
In order to make the nesting of tags easier, these methods use the content as a code block, and the code block will return a string as the content of the tag. As shown below:
#!/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 } } } }
String escape
When you are processing parameters in the URL or HTML form data, you need to escape the specified special characters, such as: Quotation marks ("), backslash (/).
Ruby CGI objects provide CGI.escape and CGI.unescape methods to handle the escaping of these special characters:
#!/usr/bin/ruby require 'cgi' puts CGI.escape(Zara Ali/A Sweet & Sour Girl")
The above code is executed The results are as follows:
#!/usr/bin/ruby require 'cgi' puts CGI.escape(Zara Ali/A Sweet & Sour Girl")
Another set of examples:
#!/usr/bin/ruby require 'cgi' puts CGI.escapeHTML('<h1>Zara Ali/A Sweet & Sour Girl</h1>')
The above code execution results are as follows:
<h1>Zara Ali/A Sweet & Sour Girl</h1>'
Commonly used methods in CGI classes
The following are the related methods of the complete CGI class in Ruby
Ruby CGI - Standard CGI library related methods
Cookies and Sessions
Ruby CGI Cookies - How to handle CGI Cookies.
Ruby CGI Sessions - How to handle CGI sessions.