Ruby CGI Method

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:

  • new: Creates a new CGI object.
cgi = CGI.new
  • out: Prints an HTTP header and body to $DEFAULT_OUTPUT ($stdout).
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: Returns a string that can be used as an HTTP header.
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: Returns a hash of all parameters in the request.
params = cgi.params

This will return a hash where the keys are parameter names and the values are arrays of parameter values.

  • cookies: Returns a hash of all cookies in the request.
cookies = cgi.cookies

This will return a hash where the keys are cookie names and the values are arrays of cookie values.

  • html: Returns an HTML string.
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.

  • print: Prints to $DEFAULT_OUTPUT ($stdout).
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.

  1. CGI methods for form handling in Ruby:

    • Description: CGI provides methods for handling form submissions, allowing you to retrieve data submitted through HTML forms.
    • Code example (retrieving form data):
      #!/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>"
      
  2. HTTP methods in Ruby CGI scripts:

    • Description: CGI scripts can determine the HTTP method (GET, POST, etc.) used in the request using the request_method method.
    • Code example (checking HTTP 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>"
      
  3. Methods for working with parameters in Ruby CGI:

    • Description: CGI provides methods for retrieving parameters from the URL query string or form submissions.
    • Code example (retrieving parameters):
      #!/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>"
      
  4. Ruby CGI request and response methods:

    • Description: CGI provides methods to access information about the request and generate the response.
    • Code example (accessing request and generating response):
      #!/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>"
      
  5. Handling GET and POST methods in Ruby CGI:

    • Description: CGI scripts can handle both GET and POST methods, and the method can be determined using request_method.
    • Code example (handling GET and POST):
      #!/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>"
      
  6. Security considerations for CGI methods in Ruby:

    • Description: CGI scripts should handle user input carefully, validate and sanitize data, and be aware of security considerations to prevent attacks.
    • Code example (basic input validation):
      #!/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>"
      
  7. Handling redirects with CGI methods in Ruby:

    • Description: CGI scripts can issue HTTP redirects using the Location header in the response.
    • Code example (performing a redirect):
      #!/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"