WEBrick has originated from an idea in an article named "Internet Programming with Ruby" in Open Design, a Japanese Engineering magazine. It was initially developed as a toolkit for the development of HTTP servers using Ruby. Due to the nature of open source model and contributions from several Ruby developers across the world, WEBrick was greatly augmented and was eventually bundled as a standard library from Ruby 1.8.0. The WEBrick ERB Handler and WEBrick Proxy Server were first introduced in Ruby 1.9.3, while the WEBrick Virtual Host was included from Ruby 2.0.0.
Usage
A WEBrick server understands only the language of . It uses multiple independent servlets, joined together by the programmer, for handling CGI scripts, ERB pages, Ruby Blocks and directory listings to provide a web application or to service a request URI on a per-host or per-path basis. For example, , , , are the examples of the standard servlets that WEBrick comes with. WEBrick is included in Ruby and hence is available to the user at no additional cost. WEBrick has been written completely in Ruby and supports several standards such as HTTP, HTML and even RHTML. During the development stage, there is no necessity for the installation of a discrete web server since WEBrick is already built into the Rails framework. It is the default web server when the Ruby application is deployed without any on Rails. Furthermore, since being implemented entirely in Ruby, direct calls can be made from WEBrick to the Rails application. On the whole, it provides a reliable, low configuration option for testing in development.
Mounting the virtual host created above similar to the way HTTP server was mounted
vhost.mount '/',...
This host, when mounted to the listening server host, will now act as a virtual host
server.virtual_host vhost
:DocumentRoot should be provided or an instance of a servlet should be set up to service a request URI; otherwise a 404 error will be returned.
Instantiating an HTTPS server
By just enabling SSL and providing an SSL certificate name, an HTTPS server can be initiated with a self-signed certificate that changes with every restart of the server.
In addition to webrick, we will require webrick/https too for SSL functionalities
require 'webrick' require 'webrick/https'
Providing certificate name. This, however, will be a self-generated self-signed certificate
cert_name = ,]
Enabling SSL and providing the certificate name will instantiate HTTPS server
server = WEBrick::HTTPServer.new
However, a pre-determined key and certificate can also be provided for instantiating HTTPS Server as shown below:
In addition to the above two, we'll need openssl to read SSL certificates and keys
require 'openssl'
Read the saved certificate and its signature key from the local directory
Unlike most of the servers that are used in production, WEBrick is not scalable since it is a single threaded web server by default. Hence, multiple requests at the same time cannot be handled and the subsequent requests would have to wait till all the previous requests have been handled, incurring a large delay. Hence, developers prefer other multi-threaded full-fledged web servers like Lighttpd and Mongrel for deploying their Rails applications.