Introduction to CGI programming with ruby

Time:2022-5-7

Write CGI script:

The most basicRubyThe CGI script looks like this:

?
1
2
3
4
5
#!/usr/bin/ruby
 
puts "HTTP/1.0 200 OK"
puts "Content-type: text/html\n\n"
puts "<html><body>This is a test</body></html>"

If you call this script, test If CGI is uploaded to a UNIX / Linux based web hosting service provider and has appropriate permissions, it can be used as a CGI script.

For example, if there is a website hosted by a Linux web hosting service provider, such as: http://www.yiibai.com/test.cgi And give it execution permission, then access http://www.yiibai.com/test.cgi An HTML page should be returned showing this is a test

Here when test CGI requests from the web browser, and the web server looks and executes it using the ruby interpreter. The ruby script returns a basic HTTP header and then a basic HTML document.
Use CGI rb:

Ruby has a special library called CGI, which enables more complex interactions than the previous CGI scripts.

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 "<html><body>This is a test</body></html>"

Here, a CGI object is created and used to print the title line.
Form processing:

There are two ways to access HTML query parameters using CGI classes. Suppose we give / CGI bin / test cgi? FirstName=Zara&LastName=Ali.

You can access the parameters firstname and LastName and use 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 will give the hash value 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"]

Here is the code to retrieve all 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 will be returned to the script as an array. The [] accessor returns only the first of these.

In this example, suppose 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 automatically handles get and post methods. There is no separate way to deal with these two different methods.

A related but basic form that can send 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 forms and HTML:

CGI contains a number of methods for creating HTML. One of the ways to find each label. To make these methods, you must call CGI New creates a CGI object.

To make tags easier to nest, these methods take their own content as blocks of code. The code block returns a string that will be used as the content of the label. 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: CGI class methods can accept method parameters, which will set the form submission used on HTTP methods (get, post, etc.). By default, post is used in this example.

This will produce 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 dealing with URLs and HTML code, you must carefully reference some characters. For example, a slash character (/) has a special meaning in a URL, so it must be escaped if it is not part of the pathname.

For example, the / query URL part will be translated into the string% 2F /, which must be translated when used. Space and symbols and special characters. To deal with this problem, CGI provides: in the routine CGI Escape and CGI unescape.

?
1
2
3
4
#!/usr/bin/ruby
 
require 'cgi'
puts CGI.escape(Zara Ali/A Sweet & Sour Girl")

This will produce the following results:

?
1
2
3
4
5
6
Zara+Ali%2FA Sweet+%26+Sour+Girl")
 
#!/usr/bin/ruby
 
require 'cgi'
puts CGI.escapeHTML('<h1>Zara Ali/A Sweet & Sour Girl</h1>')

This will produce the following results:

?
1
<h1>Zara Ali/A Sweet & Sour Girl</h1>'

 

Recommended Today

You have to know about JVM garbage collection

catalogue 1、 Four citation methods1.1 strong reference1.2 soft reference1.3 weak reference1.4 phantom reference 2、 How to judge whether the object is garbage2.1 reference counting method2.2 root accessibility analysis 3、 Garbage collection algorithm3.1 mark sweep3.2 mark compact3.3 mark copy 4、 Garbage collector4.1 classification and characteristics4.1.1 serial4.1.2 throughput priority4.1.3 priority of response time4.2 serial garbage collector details […]