User:Navisingh14/sandbox

WEBrick
WEBrick is a Ruby library providing simple HTTP web servers. WEBrick was primarily written by Masayoshi Takahashi and Yuuzou Gotou , with contributions from other developers via the open source model of software development. 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. 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.

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 of WEBrick
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, HTTPServlet::ProcHandler , HTTPServlet::CGIHandler , HTTPServlet::ERBHandler 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. Although two basic servers - gserver[] and WEbrick are provided by Ruby, building a server using the gserver library requires some socket programming to be done. Thus, WEBrick is preferred over gserver due to its ease of usage in development. 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 webserver 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 an HTTP server
The following commands are used to start an HTTP Server at the required port. Servlets can be mounted to provide advanced custom behavior as compared to a proc, 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: vhost = WEBrick::HTTPServer.new :ServerName => 'vhost.example', :DoNotListen => true, # ...
 * 1) Creating a virtual host that doesn't listen

vhost.mount '/', ...
 * 1) Mounting the virtual host created above similar to the way HTTP server was mounted

server.virtual_host vhost : DocumentRoot should be provided or  an instance of a servlet should be setup to service a request URI otherwise a 404 error will be returned.
 * 1) This host when mounted to the listening server host will now act as a virtual host

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. require 'webrick' require 'webrick/https'
 * 1) In addition to webrick, we will require webrick/https too for SSL functionalities

cert_name = [%w[CN localhost],]
 * 1) Providing certificate name. This, however, will be a self-generated self-signed certificate

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: require 'openssl'
 * 1) Enabling SSL and providing the certificate name will instantiate HTTPS server
 * 1) In addition to the above two, we'll need openssl to read SSL certificates and keys

cert = OpenSSL::X509::Certificate.new File.read '/var/myapp/cert.pem' pkey = OpenSSL::PKey::RSA.new File.read '/var/myapp/pkey.pem'
 * 1) Read the saved certificate and its signature key from the local directory

server = WEBrick::HTTPServer.new(:Port => 1234,                                :SSLEnable => true,                                 :SSLCertificate => cert,                                 :SSLPrivateKey => pkey)
 * 1) Pass the certificate and the key as separate parameters while instantiating with HTTPServer.new

Starting a proxy server
WEBrick can also proxy GET, HEAD and POST requests : require 'webrick/httpproxy' proxy = WEBrick::HTTPProxyServer.new :Port => 1234
 * 1) Instantiating a proxy server is similar, except that it is handled by HTTPProxyServer servlet

trap 'INT' do proxy.shutdown end
 * 1) Providing the hook out from the current thread

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. 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. Since WEBrick is written in an interpreted language like Ruby, it will never be as fast as a webserver which is written in a compiled language. Hence, developers prefer other multi-threaded full fledged webservers like Lighttpd and Mongrel for deploying their Rails applications.