WEBrick

Last updated
WEBrick
Original author(s) Masayoshi Takahashi and Yuuzou Gotou and et al. [1] [2]
Developer(s) Ruby Community
Stable release
1.7.0 [3] / 11 December 2020;23 months ago (11 December 2020)
Repository
Written in Ruby
Operating system Cross-platform
Available in Ruby
Type Web Server
License 2-Clause BSD License
Website www.ruby-lang.org/en/

WEBrick is a Ruby library providing simple HTTP web servers. It uses basic access authentication and digest access authentication for different kinds of servers that it can create - HTTP based server, HTTPS server, proxy server and virtual-host server. [4] Construction of several non-HTTP servers such as the Day Time Server which uses the Daytime Protocol rather than the HTTP is also facilitated by WEBrick. It is used by the Ruby on Rails and Padrino frameworks to test applications in a development environment as well as production mode for small loads. It is now a part of Ruby standard library. [1]

Contents

WEBrick follows open-source distribution model.

History

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. [2] 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 servlets. 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, HTTPServlet::FileHandler, [2] HTTPServlet::ProcHandler, [2] HTTPServlet::CGIHandler, [2] HTTPServlet::ERBHandler [2] 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 procfile 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.

Instantiating servers

Instantiating an HTTP server

The following commands are used to start an HTTP Server at the required port. [1]

# Include WEBrick class with requirerequire'webrick'# FileHandler servlet provides the option to choose which files from user to serve# The following code shows how to serve them from the folder 'myapp'root=File.expand_path'/var/myapp/'# Instantiating a new server with HTTPServer.new on port 1234 serving the documents from root folderserver=WEBrick::HTTPServer.new:Port=>1234,:DocumentRoot=>root# The following proc is used to customize the server operationsserver.mount_proc'/'do|request,response|response.body='Hello, world!'end# The following command will provide a hook to shut down the server (often done with Ctrl+C)trap('INT'){server.shutdown}# Start the serverserver.start

Servlets can be mounted to provide advanced custom behavior as compared to a proc, [5] to increase the modularity.

Starting a virtual host

WEBrick creates a listening port. Various other ports as ‘virtual hosts’ can also be created at the same time which do not listen as shown below: [1]

#Creating a virtual host that doesn't listenvhost=WEBrick::HTTPServer.new:ServerName=>'vhost.example',:DoNotListen=>true,# ...# Mounting the virtual host created above similar to the way HTTP server was mountedvhost.mount'/',...# This host, when mounted to the listening server host, will now act as a virtual hostserver.virtual_hostvhost

: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. [1]

# In addition to webrick, we will require webrick/https too for SSL functionalitiesrequire'webrick'require'webrick/https'# Providing certificate name. This, however, will be a self-generated self-signed certificatecert_name=[%w[CN localhost],]# Enabling SSL and providing the certificate name will instantiate HTTPS serverserver=WEBrick::HTTPServer.new(:Port=>1234,:SSLEnable=>true,:SSLCertName=>cert_name)

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 keysrequire'openssl'# Read the saved certificate and its signature key from the local directorycert=OpenSSL::X509::Certificate.newFile.read'/var/myapp/cert.pem'pkey=OpenSSL::PKey::RSA.newFile.read'/var/myapp/pkey.pem'# Pass the certificate and the key as separate parameters while instantiating with HTTPServer.newserver=WEBrick::HTTPServer.new(:Port=>1234,:SSLEnable=>true,:SSLCertificate=>cert,:SSLPrivateKey=>pkey)

Starting a proxy server

WEBrick can also proxy GET, HEAD and POST requests: [1]

# Instantiating a proxy server is similar, except that it is handled by HTTPProxyServer servletrequire'webrick/httpproxy'proxy=WEBrick::HTTPProxyServer.new:Port=>1234# Providing the hook out from the current threadtrap'INT'doproxy.shutdownend

Limitations

Unlike most of the servers that are used in production, WEBrick is not scalable since it is a single threaded web server by default. [6] 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. [7]

See also

Related Research Articles

<span class="mw-page-title-main">Proxy server</span> Computer server that makes and receives requests on behalf of a user

In computer networking, a proxy server is a server application that acts as an intermediary between a client requesting a resource and the server providing that resource.

<span class="mw-page-title-main">Squid (software)</span> Caching and forwarding HTTP web proxy

Squid is a caching and forwarding HTTP web proxy. It has a wide variety of uses, including speeding up a web server by caching repeated requests, caching web, DNS and other computer network lookups for a group of people sharing network resources, and aiding security by filtering traffic. Although primarily used for HTTP and FTP, Squid includes limited support for several other protocols including Internet Gopher, SSL, TLS and HTTPS. Squid does not support the SOCKS protocol, unlike Privoxy, with which Squid can be used in order to provide SOCKS support.

<span class="mw-page-title-main">Internet Information Services</span> Extensible web server software by Microsoft

Internet Information Services is an extensible web server software created by Microsoft for use with the Windows NT family. IIS supports HTTP, HTTP/2, HTTPS, FTP, FTPS, SMTP and NNTP. It has been an integral part of the Windows NT family since Windows NT 4.0, though it may be absent from some editions, and is not active by default.

In cryptography, a public key certificate, also known as a digital certificate or identity certificate, is an electronic document used to prove the validity of a public key. The certificate includes information about the key, information about the identity of its owner, and the digital signature of an entity that has verified the certificate's contents. If the signature is valid, and the software examining the certificate trusts the issuer, then it can use that key to communicate securely with the certificate's subject. In email encryption, code signing, and e-signature systems, a certificate's subject is typically a person or organization. However, in Transport Layer Security (TLS) a certificate's subject is typically a computer or other device, though TLS certificates may identify organizations or individuals in addition to their core role in identifying devices. TLS, sometimes called by its older name Secure Sockets Layer (SSL), is notable for being a part of HTTPS, a protocol for securely browsing the web.

Virtual hosting is a method for hosting multiple domain names on a single server. This allows one server to share its resources, such as memory and processor cycles, without requiring all services provided to use the same host name. The term virtual hosting is usually used in reference to web servers but the principles do carry over to other Internet services.

<span class="mw-page-title-main">OpenSSL</span> Open-source implementation of the SSL and TLS protocols

OpenSSL is a software library for applications that secure communications over computer networks against eavesdropping or need to identify the party at the other end. It is widely used by Internet servers, including the majority of HTTPS websites.

netcat

netcat is a computer networking utility for reading from and writing to network connections using TCP or UDP. The command is designed to be a dependable back-end that can be used directly or easily driven by other programs and scripts. At the same time, it is a feature-rich network debugging and investigation tool, since it can produce almost any kind of connection its user could need and has a number of built-in capabilities.

lighttpd

lighttpd is an open-source web server optimized for speed-critical environments while remaining standards-compliant, secure and flexible. It was originally written by Jan Kneschke as a proof-of-concept of the c10k problem – how to handle 10,000 connections in parallel on one server, but has gained worldwide popularity. Its name is a portmanteau of "light" and "httpd".

In computer networks, a tunneling protocol is a communication protocol which allows for the movement of data from one network to another, by exploiting encapsulation. It involves allowing private network communications to be sent across a public network through a process called encapsulation.

Web server software allows computers to act as web servers. The first web servers supported only static files, such as HTML, but now they commonly allow embedding of server side applications.

<span class="mw-page-title-main">HTTP compression</span> Capability that can be built into web servers and web clients

HTTP compression is a capability that can be built into web servers and web clients to improve transfer speed and bandwidth utilization.

Pound is a lightweight open source reverse proxy program and application firewall suitable to be used as a web server load balancing solution. Developed by an IT security company, it has a strong emphasis on security. The original intent on developing Pound was to allow distributing the load among several Zope servers running on top of ZEO. However, Pound is not limited to Zope-based installations. Using regular expression matching on the requested URLs, Pound can pass different kinds of requests to different backend server groups. A few more of its most important features:

Mongrel is an open-source software HTTP library and web server written in Ruby by Zed Shaw. It is used to run Ruby web applications and presents a standard HTTP interface. This makes layering other servers in front of it possible using a web proxy, a load balancer, or a combination of both, instead of having to use more conventional methods employed to run scripts such as FastCGI or SCGI to communicate. This is made possible by integrating a custom high-performance HTTP request parser implemented using Ragel.

<span class="mw-page-title-main">HTTP 403</span> HTTP status code indicating that access is forbidden to a resource

HTTP 403 is an HTTP status code meaning access to the requested resource is forbidden. The server understood the request, but will not fulfill it.

HTTP tunneling is used to create a network link between two computers in conditions of restricted network connectivity including firewalls, NATs and ACLs, among other restrictions. The tunnel is created by an intermediary called a proxy server which is usually located in a DMZ.

Server Name Indication (SNI) is an extension to the Transport Layer Security (TLS) computer networking protocol by which a client indicates which hostname it is attempting to connect to at the start of the handshaking process. This allows a server to present one of multiple possible certificates on the same IP address and TCP port number and hence allows multiple secure (HTTPS) websites to be served by the same IP address without requiring all those sites to use the same certificate. It is the conceptual equivalent to HTTP/1.1 name-based virtual hosting, but for HTTPS. This also allows a proxy to forward client traffic to the right server during TLS/SSL handshake. The desired hostname is not encrypted in the original SNI extension, so an eavesdropper can see which site is being requested.

The front controller software design pattern is listed in several pattern catalogs and is related to the design of web applications. It is "a controller that handles all requests for a website," which is a useful structure for web application developers to achieve flexibility and reuse without code redundancy.

<span class="mw-page-title-main">Rack (web server interface)</span> API specification for web applications in programming language Ruby

Rack is a modular interface between web servers and web applications developed in the Ruby programming language. With Rack, application programming interfaces (APIs) for web frameworks and middleware are wrapped into a single method call handling HTTP requests and responses.

mod_proxy is an optional module for the Apache HTTP Server.

<span class="mw-page-title-main">Cherokee (web server)</span> Open source web server software application

Cherokee is an open-source cross-platform web server that runs on Linux, BSD variants, Solaris, OS X, and Windows. It is a lightweight, high-performance web server/reverse proxy licensed under the GNU General Public License. Its goal is to be fast and fully functional yet still light. Major features of Cherokee include a graphical administration interface named cherokee-admin, and a modular light-weight design.

References

  1. 1 2 3 4 5 6 "Module: WEBrick (Ruby 2.3.1)". ruby-doc.org. Retrieved 2016-09-22.
  2. 1 2 3 4 5 6 Gnome's guide to WEBrick
  3. "Release 1.7.0". 11 December 2020. Retrieved 18 December 2020.
  4. Investigating the impacts of web servers on web application energy usage - IEEE
  5. proc
  6. Heroku Ruby default web server
  7. NetBeans Ruby and Rails IDE with JRuby (FirstPress) By Chris Kutler, Brian Leonard