Ruby Tutorial
Ruby CGI
Ruby Advanced
Ruby's CGI library has various methods to interact with the CGI protocol, helping to handle HTTP requests and responses. Here are some important methods:
cgi = CGI.new
cgi.out do "Hello, world!" end
This will send an HTTP response with "Hello, world!" as the body. You can also add optional parameters to send specific HTTP headers:
cgi.out("text/plain") do "Hello, world!" end
This will send an HTTP response with a Content-Type of "text/plain".
header = cgi.header
This will return a basic HTTP header as a string. You can also add optional parameters to specify the content type, status, and other HTTP header values.
params = cgi.params
This will return a hash where the keys are parameter names and the values are arrays of parameter values.
cookies = cgi.cookies
This will return a hash where the keys are cookie names and the values are arrays of cookie values.
html = cgi.html do cgi.head { cgi.title{"Hello Page"} } + cgi.body { cgi.h1 {"Hello, world!"} } end
This method helps to generate HTML content. Note that the plus operator is used to concatenate the head and body.
cgi.print "Hello, world!"
This will print "Hello, world!" to $stdout, which is usually the web server when running a CGI script.
Remember, the Ruby CGI library is not commonly used for modern web development. Frameworks like Ruby on Rails, Sinatra, or Hanami provide more features and a higher level of abstraction. However, understanding how CGI works can give you a better understanding of how HTTP and web servers work.
CGI methods for form handling in Ruby:
#!/usr/bin/env ruby require 'cgi' cgi = CGI.new # Retrieve form data using CGI methods user_name = cgi['user_name'] password = cgi['password'] puts "Content-type: text/html\n\n" puts "<html><body>" puts "<p>User Name: #{user_name}</p>" puts "<p>Password: #{password}</p>" puts "</body></html>"
HTTP methods in Ruby CGI scripts:
request_method
method.#!/usr/bin/env ruby require 'cgi' cgi = CGI.new # Check the HTTP method http_method = cgi.request_method puts "Content-type: text/html\n\n" puts "<html><body>" puts "<p>HTTP Method: #{http_method}</p>" puts "</body></html>"
Methods for working with parameters in Ruby CGI:
#!/usr/bin/env ruby require 'cgi' cgi = CGI.new # Retrieve parameters from URL query string or form user_name = cgi['user_name'] password = cgi['password'] puts "Content-type: text/html\n\n" puts "<html><body>" puts "<p>User Name: #{user_name}</p>" puts "<p>Password: #{password}</p>" puts "</body></html>"
Ruby CGI request and response methods:
#!/usr/bin/env ruby require 'cgi' cgi = CGI.new # Access request information user_agent = cgi.user_agent # Generate response puts "Content-type: text/html\n\n" puts "<html><body>" puts "<p>User Agent: #{user_agent}</p>" puts "</body></html>"
Handling GET and POST methods in Ruby CGI:
request_method
.#!/usr/bin/env ruby require 'cgi' cgi = CGI.new # Determine HTTP method http_method = cgi.request_method # Handle GET or POST accordingly case http_method when 'GET' # Handle GET parameters user_name = cgi['user_name'] when 'POST' # Handle POST parameters user_name = cgi.params['user_name'].first end puts "Content-type: text/html\n\n" puts "<html><body>" puts "<p>User Name: #{user_name}</p>" puts "</body></html>"
Security considerations for CGI methods in Ruby:
#!/usr/bin/env ruby require 'cgi' cgi = CGI.new # Validate and sanitize input user_name = cgi['user_name'] sanitized_name = CGI.escapeHTML(user_name) puts "Content-type: text/html\n\n" puts "<html><body>" puts "<p>Sanitized User Name: #{sanitized_name}</p>" puts "</body></html>"
Handling redirects with CGI methods in Ruby:
Location
header in the response.#!/usr/bin/env ruby require 'cgi' cgi = CGI.new # Redirect to a different URL redirect_url = 'https://example.com' puts "Location: #{redirect_url}\n\n"