HOME

TheInfoList



OR:

WEBrick is a
Ruby A ruby is a pinkish red to blood-red colored gemstone, a variety of the mineral corundum (aluminium oxide). Ruby is one of the most popular traditional jewelry gems and is very durable. Other varieties of gem-quality corundum are called sapp ...
library providing simple
HTTP The Hypertext Transfer Protocol (HTTP) is an application layer protocol in the Internet protocol suite model for distributed, collaborative, hypermedia information systems. HTTP is the foundation of data communication for the World Wide Web, ...
web server A web server is computer software and underlying hardware that accepts requests via HTTP (the network protocol created to distribute web content) or its secure variant HTTPS. A user agent, commonly a web browser or web crawler, initi ...
s. It uses
basic access authentication In the context of an HTTP transaction, basic access authentication is a method for an HTTP user agent (e.g. a web browser) to provide a user name and password when making a request. In basic HTTP authentication, a request contains a header field ...
and digest access authentication for different kinds of servers that it can create -
HTTP The Hypertext Transfer Protocol (HTTP) is an application layer protocol in the Internet protocol suite model for distributed, collaborative, hypermedia information systems. HTTP is the foundation of data communication for the World Wide Web, ...
based server,
HTTPS Hypertext Transfer Protocol Secure (HTTPS) is an extension of the Hypertext Transfer Protocol (HTTP). It is used for secure communication over a computer network, and is widely used on the Internet. In HTTPS, the communication protocol is e ...
server,
proxy server 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. Instead of connecting directly to a server that can fulfill a requ ...
and virtual-host server.Investigating the impacts of web servers on web application energy usage - IEEE
/ref> Construction of several non-HTTP servers such as the Day Time Server which uses the
Daytime Protocol The Daytime Protocol is a service in the Internet Protocol Suite, defined in 1983 in RFC 867. It is intended for testing and measurement purposes in computer networks. A host may connect to a server that supports the Daytime Protocol on either T ...
rather than the HTTP is also facilitated by WEBrick. It is used by the
Ruby on Rails Ruby on Rails (simplified as Rails) is a server-side web application framework written in Ruby under the MIT License. Rails is a model–view–controller (MVC) framework, providing default structures for a database, a web service, and web ...
and
Padrino In infant baptism and denominations of Christianity, a godparent (also known as a sponsor, or '' gossiprede'') is someone who bears witness to a child's christening and later is willing to help in their catechesis, as well as their lifelong ...
frameworks to test applications in a
development environment In software deployment, an environment or tier is a computer system or set of systems in which a computer program or software component is deployed and executed. In simple cases, such as developing and immediately executing a program on the same m ...
as well as production mode for small loads. It is now a part of Ruby standard library. 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. 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 o
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 Uri may refer to: Places * Canton of Uri, a canton in Switzerland * Úri, a village and commune in Hungary * Uri, Iran, a village in East Azerbaijan Province * Uri, Jammu and Kashmir, a town in India * Uri (island), an island off Malakula Isla ...
on a per-host or per-path basis. For example
HTTPServlet::FileHandler



ref name="gnome"/> 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 The HyperText Markup Language or HTML is the standard markup language for documents designed to be displayed in a web browser. It can be assisted by technologies such as Cascading Style Sheets (CSS) and scripting languages such as JavaScri ...
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 an
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. # Include WEBrick class with require require '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 folder server = WEBrick::HTTPServer.new :Port => 1234, :DocumentRoot => root # The following proc is used to customize the server operations server.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') # Start the server server.start Servlets can be mounted to provide advanced custom behavior as compared to a proc,
/ref> to increase the
modularity Broadly speaking, modularity is the degree to which a system's components may be separated and recombined, often with the benefit of flexibility and variety in use. The concept of modularity is used primarily to reduce complexity by breaking a s ...
.


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: #Creating a virtual host that doesn't listen vhost = WEBrick::HTTPServer.new :ServerName => 'vhost.example', :DoNotListen => true, # ... # 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 Uri may refer to: Places * Canton of Uri, a canton in Switzerland * Úri, a village and commune in Hungary * Uri, Iran, a village in East Azerbaijan Province * Uri, Jammu and Kashmir, a town in India * Uri (island), an island off Malakula Isla ...
; otherwise a 404 error will be returned.


Instantiating an HTTPS server

By just enabling
SSL SSL may refer to: Entertainment * RoboCup Small Size League, robotics football competition * ''Sesame Street Live'', a touring version of the children's television show * StarCraft II StarLeague, a Korean league in the video game Natural language ...
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 = w[CN localhost">N_localhost.html" ;"title="w[CN localhost">w[CN localhost # Enabling SSL and providing the certificate name will instantiate HTTPS server server = 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 keys require 'openssl' # Read the saved certificate and its signature key from the local directory cert = OpenSSL::X509::Certificate.new File.read '/var/myapp/cert.pem' pkey = OpenSSL::PKey::RSA.new File.read '/var/myapp/pkey.pem' # Pass the certificate and the key as separate parameters while instantiating with HTTPServer.new server = WEBrick::HTTPServer.new(:Port => 1234, :SSLEnable => true, :SSLCertificate => cert, :SSLPrivateKey => pkey)


Starting a proxy server

WEBrick can also proxy GET, HEAD and POST requests: # Instantiating a proxy server is similar, except that it is handled by HTTPProxyServer servlet require 'webrick/httpproxy' proxy = WEBrick::HTTPProxyServer.new :Port => 1234 # Providing the hook out from the current thread trap 'INT' do proxy.shutdown end


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.Heroku Ruby default web server
/ref> 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 Multithreading (software)">multi-threaded full-fledged web servers like Lighttpd and Mongrel (web server)">Mongrel A mongrel, mutt or mixed-breed dog is a dog that does not belong to one officially recognized breed and including those that are the result of intentional breeding. Although the term ''mixed-breed dog'' is sometimes preferred, many mong ...
for deploying their Ruby on Rails">Rails applications.NetBeans Ruby and Rails IDE with JRuby (FirstPress) ''By Chris Kutler, Brian Leonard''


See also

* Mongrel (web server) * Apache HTTP Server, Apache * nginx * Lighttpd


References


External links


Gnome's Guide to WEBrick



WEBrick High Performance
{{Ruby programming language Free web server software Ruby (programming language) Web server software for Linux