Ruby Tutorial
Ruby CGI
Ruby Advanced
CGI (Common Gateway Interface) is a standard for interfacing external applications with information servers, such as HTTP or Web servers. In the context of web development with Ruby, you can use CGI to create dynamic web content.
Note: Ruby CGI is not commonly used in modern web development due to the rise of web frameworks like Ruby on Rails, Sinatra, etc., which provide a higher level of abstraction and more features. Nevertheless, understanding how to use Ruby CGI can be beneficial for learning purposes.
Here's a basic example of a CGI script in Ruby:
#!/usr/bin/env ruby require 'cgi' cgi = CGI.new puts cgi.header puts "<html><body>" puts "<h1>Hello CGI</h1>" puts "</body></html>"
Let's break down this script:
#!/usr/bin/env ruby
: This is the shebang line, which tells the system to use Ruby to interpret this script.
require 'cgi'
: This loads the CGI library, which provides methods for handling CGI tasks.
cgi = CGI.new
: This creates a new CGI object.
puts cgi.header
: This prints out an HTTP header, which tells the browser that the server is returning an HTML document.
puts "<html><body>"
and puts "</body></html>"
: These lines print out the start and end of an HTML document.
puts "<h1>Hello CGI</h1>"
: This prints out an HTML heading.
This script can be run from the command line or from a web server that supports CGI scripts. When run, it generates a simple HTML document with a single heading.
To use this with a web server, you'd typically put the script in the server's CGI directory, make sure the script has execute permissions (chmod +x scriptname.rb
), and access it via a URL like http://yourserver.com/cgi-bin/scriptname.rb
.
Remember that each server may have different requirements for running CGI scripts, and you may need to configure your server to allow running CGI scripts in the first place.
Important: When developing CGI scripts, ensure that your scripts are secure and do not expose any sensitive information or system resources. CGI scripts can be a potential security risk if not handled properly.
Getting started with CGI in Ruby:
#!/usr/bin/env ruby puts "Content-type: text/html\n\n" puts "<html><body><h1>Hello, CGI in Ruby!</h1></body></html>"
Creating CGI scripts with Ruby:
#!/usr/bin/env ruby
) and output headers followed by HTML content.#!/usr/bin/env ruby puts "Content-type: text/html\n\n" puts "<html><body><h1>Ruby CGI Script</h1></body></html>"
CGI environment variables in Ruby:
#!/usr/bin/env ruby puts "Content-type: text/html\n\n" puts "<html><body>" puts "<p>Query Parameter: #{ENV['QUERY_STRING']}</p>" puts "</body></html>"
Form handling in Ruby CGI scripts:
#!/usr/bin/env ruby puts "Content-type: text/html\n\n" puts "<html><body>" puts "<form method='post'>" puts "<input type='text' name='user_input'>" puts "<input type='submit' value='Submit'>" puts "</form>" user_input = ENV['REQUEST_METHOD'] == 'POST' ? gets : '' puts "<p>User Input: #{user_input}</p>" puts "</body></html>"
Working with cookies in Ruby CGI:
#!/usr/bin/env ruby puts "Content-type: text/html" puts "Set-Cookie: user_id=12345" puts "\n\n" puts "<html><body>" puts "<p>Cookie Value: #{ENV['HTTP_COOKIE']}</p>" puts "</body></html>"
Handling file uploads in Ruby CGI:
#!/usr/bin/env ruby puts "Content-type: text/html\n\n" puts "<html><body>" puts "<form method='post' enctype='multipart/form-data'>" puts "<input type='file' name='uploaded_file'>" puts "<input type='submit' value='Upload'>" puts "</form>" uploaded_file = ENV['REQUEST_METHOD'] == 'POST' ? ENV['rack.input'].read : '' puts "<p>File Content: #{uploaded_file}</p>" puts "</body></html>"
Security considerations in Ruby CGI programming:
#!/usr/bin/env ruby user_input = ENV['QUERY_STRING'] sanitized_input = CGI.escapeHTML(user_input) puts "Content-type: text/html\n\n" puts "<html><body>" puts "<p>Sanitized Input: #{sanitized_input}</p>" puts "</body></html>"