WEBrick - a WWW server toolkit


Table of contents:


What's WEBrick

WEBrick is a Ruby library program to build HTTP servers.

Download

The Latest Release

WEBrick has been bundled as a standard library since Ruby-1.8.0. Our development and maintenance base was moved to The Repository of Ruby. Note that this release is for the users of Ruby-1.6.

webrick-1.3.1.tar.gz (Changes)

CVS

The repository | Snapshot tarball | Snapshot ZIP archive

In addition, Anonymous CVS is also available:

$ cvs -d :pserver:anonymous@cvs.webrick.org:/home/ipr/ncvs login
(Logging in to anonymous@cvs.webrick.org)
CVS password: (just hit Enter key)
$ cvs -z4 -d :pserver:anonymous@cvs.webrick.org:/home/ipr/ncvs co webrick
     

Optional Libraries

Related Libraries and Applications

Example

Daytime Server

#!/usr/local/bin/ruby
require 'webrick'

s = WEBrick::GenericServer.new( :Port => 2000 )
trap("INT"){ s.shutdown }
s.start{|sock|
  sock.print(Time.now.to_s + "\r\n")
}

or

#!/usr/local/bin/ruby
require 'webrick'

class DaytimeServer < WEBrick::GenericServer
  def run(sock)
    sock.print(Time.now.to_s + "\r\n")
  end
end

s = DaytimeServer.new( :Port => 2000 )
trap("INT"){ s.shutdown }
s.start

HTTP Server

#!/usr/local/bin/ruby
require 'webrick'
include WEBrick

s = HTTPServer.new(
  :Port            => 2000,
  :DocumentRoot    => Dir::pwd + "/htdocs"
)

## mount subdirectories
s.mount("/ipr", HTTPServlet::FileHandler, "/proj/ipr/public_html")
s.mount("/~gotoyuzo",
        HTTPServlet::FileHandler, "/home/gotoyuzo/public_html",
        true)  #<= allow to show directory index.

trap("INT"){ s.shutdown }
s.start

HTTPS Server

Note: This program requires Ruby/OpenSSL.

#!/usr/local/bin/ruby
require 'webrick'
require 'webrick/https'

s = WEBrick::HTTPServer.new(
  :Port            => 2000,
  :DocumentRoot    => Dir::pwd + "/htdocs",
  :SSLEnable       => true,
  :SSLVerifyClient => ::OpenSSL::SSL::VERIFY_NONE,
  :SSLCertName => [ ["C","JP"], ["O","WEBrick.Org"], ["CN", "WWW"] ]
)
trap("INT"){ s.shutdown }
s.start

Hello Servlet

#!/usr/local/bin/ruby
require 'webrick'
include WEBrick

s = HTTPServer.new( :Port => 2000 )

# HTTPServer#mount(path, servletclass)
#   When a request referring "/hello" is received,
#   the HTTPServer get an instance of servletclass
#   and then call a method named do_"a HTTP method".

class HelloServlet < HTTPServlet::AbstractServlet
  def do_GET(req, res)
    res.body = "<HTML>hello, world.</HTML>"
    res['Content-Type'] = "text/html"
  end
end
s.mount("/hello", HelloServlet)


# HTTPServer#mount_proc(path){|req, res| ...}
#   You can mount also a block by `mount_proc'.
#   This block is called when GET or POST.

s.mount_proc("/hello/again"){|req, res|
  res.body = "<HTML>hello (again)</HTML>"
  res['Content-Type'] = "text/html"
}

trap("INT"){ s.shutdown }
s.start

HTTP Proxy Server

#!/usr/local/bin/ruby
require 'webrick/httpproxy'

s = WEBrick::HTTPProxyServer.new(
  :Port => 2020,
  :RequestCallback => Proc.new{|req,res|
    puts "-"*70
    puts req.request_line, req.raw_header
    puts "-"*70
  }
)
trap("INT"){ s.shutdown }
s.start

Documentation

Changes

Version 1.3.1 (2003-08-14)

Version 1.2.3 (2002-09-20)

Version 1.2.2 (2002-09-09)

Version 1.1.5 (2002-02-15)

Version 1.1.4 (2002-02-06)

Version 1.1.3 (2002-01-03)

Version 1.1.2 (2002-01-02)

Version 1.1.1 (2001-12-17)

Version 1.1.0 (2001-12-16)

Version 1.0.0 (2001-12-07)

The first release.

Mailing List

<URL:mailto:webricken@notwork.org> (in English) and <URL:mailto:webrickja@notwork.org> (in Japanese) have been set up for a purpose to talk about WEBrick.

To subscribe this list, please send the following phrase

  subscribe Your-First-Name Your-Last-Name  

in the mail body (not subject) to the address
<URL:mailto:webricken-ctl@notwork.org> (for webricken) or
<URL:mailto:webrickja-ctl@notwork.org> (for webrickja).

Author

IPR -- Internet Programming with Ruby -- writers <ipr-feedback@notwork.org>

License and No Warranty

WEBrick is Copyright (c) 2002 Internet Programming with Ruby writers. It is free software, and may be redistributed under the terms specified in the COPYING file of the Ruby distribution.

WEBrick is provided by the writers and contributers ``as is'' and any express or implied warranties, including, but not limited to, the implied warranties of merchantability and fitness for a particular purpose are disclaimed. In no event shall the writer or contributors be liable for any direct, indirect, incidental, special, exemplary, or consequential damages (including, but not limited to, procurement of substitute goods or services; loss of use, data, or profits; or business interruption) however caused and on any theory of liability, whether in contract, strict liability, or tort (including negligence or otherwise) arising in any way out of the use of WEBrick, even if advised of the possibility of such damage.


Copyright(C) 2001 IPR -- Internet Programming with Ruby -- writers
$Id: index.rhtml,v 1.11 2008/03/22 17:55:09 webrick Exp $

Valid CSS! Valid HTML 4.0!