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 sa ...
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, initiate ...
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 i ...
and
digest access authentication Digest access authentication is one of the agreed-upon methods a web server can use to negotiate credentials, such as username or password, with a user's web browser. This can be used to confirm the identity of a user before sending sensitive info ...
for different kinds of
server Server may refer to: Computing *Server (computing), a computer program or a device that provides functionality for other programs or devices, called clients Role * Waiting staff, those who work at a restaurant or a bar attending customers and su ...
s 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 enc ...
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 request ...
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 p ...
and Padrino 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 Production may refer to: Economics and business * Production (economics) * Production, the act of manufacturing goods * Production, in the outline of industrial organization, the act of making products (goods and services) * Production as a stati ...
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 Open source is source code that is made freely available for possible modification and redistribution. Products include permission to use the source code, design documents, or content of the product. The open-source model is a decentralized sof ...
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 Islan ...
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 sy ...
.


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 Islan ...
; 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 In cryptography and computer security, self-signed certificates are public key certificates that are not issued by a certificate authority (CA). These self-signed certificates are easy to make and do not cost money. However, they do not provide any ...
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.html"_;"title="N_localhost.html"_;"title="w[CN_localhost">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_
w[CN_localhost.html"_;"title="N_localhost.html"_;"title="w[CN_localhost">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_HTTP_Requests">requests_ Request_may_refer_to: *_a_question,_a_request_for_information *_a_petition,_a_formal_document_demanding_something_that_is_submitted_to_an_authority._ Request_may_also_refer_to: _Computing_and_technology *_in_computer_science,_a_message_sent_be_...
: #_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_Multithreading_(software).html" "title="HTTP_Requests.html" "title="N_localhost">w[CN_localhost.html" ;"title="N_localhost.html" ;"title="w[CN localhost">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 HTTP Requests">requests Request may refer to: * a question, a request for information * a petition, a formal document demanding something that is submitted to an authority. Request may also refer to: Computing and technology * in computer science, a message sent be ...
: # 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 Multithreading (software)">multi-threaded In computer science, a thread of execution is the smallest sequence of programmed instructions that can be managed independently by a scheduler, which is typically a part of the operating system. The implementation of threads and processes dif ...
full-fledged web servers like
Lighttpd lighttpd (pronounced "lighty") 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 †...
and Mongrel (web server), Mongrel 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) 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 pr ...
*
Apache The Apache () are a group of culturally related Native American tribes in the Southwestern United States, which include the Chiricahua, Jicarilla, Lipan, Mescalero, Mimbreño, Ndendahe (Bedonkohe or Mogollon and Nednhi or Carrizaleño an ...
*
nginx Nginx (pronounced "engine x" ) is a web server that can also be used as a reverse proxy, load balancer, mail proxy and HTTP cache. The software was created by Igor Sysoev and publicly released in 2004. Nginx is free and open-source software ...
*
Lighttpd lighttpd (pronounced "lighty") 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 †...


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