### Create and Start a Basic HTTP Server with WEBrick Source: https://context7.com/ruby/webrick/llms.txt Initializes an HTTPServer instance with specific port, document root, and logging configurations. The server process is blocking and requires signal traps for graceful shutdown. ```ruby require 'webrick' # Create a basic HTTP server serving static files root = File.expand_path '~/public_html' server = WEBrick::HTTPServer.new( Port: 8000, DocumentRoot: root, Logger: WEBrick::Log.new($stderr, WEBrick::Log::DEBUG), AccessLog: [[$stderr, WEBrick::AccessLog::COMBINED_LOG_FORMAT]] ) # Graceful shutdown on interrupt trap('INT') { server.shutdown } trap('TERM') { server.shutdown } # Start the server (blocking call) server.start ``` -------------------------------- ### Implementing HTTP Basic Authentication with WEBrick Source: https://context7.com/ruby/webrick/llms.txt Set up HTTP Basic Authentication using WEBrick's HTTPAuth module. This example shows how to create or load an htpasswd file and set usernames and passwords with bcrypt hashing for secure storage. ```ruby require 'webrick' require 'webrick/httpauth' # Create or load password file htpasswd = WEBrick::HTTPAuth::Htpasswd.new('/path/to/htpasswd', password_hash: :bcrypt) htpasswd.set_passwd('SecureArea', 'admin', 'secret123') htpasswd.set_passwd('SecureArea', 'user', 'password456') htpasswd.flush ``` -------------------------------- ### Initialize CGI Script Source: https://context7.com/ruby/webrick/llms.txt Start a CGI script using the WEBrick::CGI module for deployment in external web server environments. ```ruby #!/usr/bin/env ruby ``` -------------------------------- ### Install WEBrick via Gemfile Source: https://github.com/ruby/webrick/blob/master/README.md Add the gem dependency to your project's Gemfile. ```ruby gem 'webrick' ``` -------------------------------- ### Start and Shutdown WEBrick Server Source: https://github.com/ruby/webrick/blob/master/README.md Register a signal trap for graceful shutdown and start the server process. ```ruby trap 'INT' do server.shutdown end server.start ``` -------------------------------- ### Initialize WEBrick HTTPServer Source: https://github.com/ruby/webrick/blob/master/README.md Configure a new server instance to listen on a specific port and serve files from a designated directory. ```ruby require 'webrick' root = File.expand_path '~/public_html' server = WEBrick::HTTPServer.new :Port => 8000, :DocumentRoot => root ``` -------------------------------- ### Create HTTPProxyServer Source: https://context7.com/ruby/webrick/llms.txt Demonstrates proxying with content modification, authentication, and upstream proxy routing. ```ruby require 'webrick' require 'webrick/httpproxy' # Basic proxy server proxy = WEBrick::HTTPProxyServer.new( Port: 8080, Logger: WEBrick::Log.new($stderr, WEBrick::Log::DEBUG) ) # Proxy with content modification content_handler = proc do |req, res| # Modify response content if res['content-type']&.include?('text/html') res.body = res.body.to_s.gsub('
Request Path: #{req.path_info}
Query String: #{req.query_string}
Remote Address: #{req.peeraddr[3]}
Server: #{req.server_name}:#{req.port}