WEBrick

Uses your system voice; voice and rate in settings.

Snapshot

≈ 4 min read

Names in other languages

No names in other languages are held for this entry yet.

Article bodies are English and are not machine-translated. A name in another language is not a translation of the article.

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. 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. WEBrick follows open-source distribution model.

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. 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. WEBrick follows open-source distribution model. 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') {server.shutdown} Start the server server.start Servlets can be mounted to provide advanced custom behavior as compared to a proc, to increase the modularity. 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; otherwise a 404 error will be returned. 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],] 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) 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

Text derived from https://en.wikipedia.org/w/index.php?oldid=1367431733 under CC BY-SA 4.0; modifications have been made.

Sources cited in this revision

Extracted from the cited revision's reference blocks; absent citations are not invented.