|
| 1 | +## |
| 2 | +# = WEB server toolkit. |
1 | 3 | # |
2 | | -# WEBrick -- WEB server toolkit. |
| 4 | +# WEBrick is an HTTP server toolkit that can be configured as an HTTPS server, |
| 5 | +# a proxy server, and a virtual-host server. WEBrick features complete |
| 6 | +# logging of both server operations and HTTP access. WEBrick supports both |
| 7 | +# basic and digest authentication in addition to algorithms not in RFC 2617. |
| 8 | +# |
| 9 | +# A WEBrick servers can be composed of multiple WEBrick servers or servlets to |
| 10 | +# provide differing behavior on a per-host or per-path basis. WEBrick |
| 11 | +# includes servlets for handling CGI scripts, ERb pages, ruby blocks and |
| 12 | +# directory listings. |
| 13 | +# |
| 14 | +# WEBrick also includes tools for daemonizing a process and starting a process |
| 15 | +# at a higher privilege level and dropping permissions. |
| 16 | +# |
| 17 | +# == Starting an HTTP server |
| 18 | +# |
| 19 | +# To create a new WEBrick::HTTPServer that will listen to connections on port |
| 20 | +# 8000 and serve documents from the current user's public_html folder: |
| 21 | +# |
| 22 | +# require 'webrick' |
| 23 | +# |
| 24 | +# root = File.expand_path '~/public_html' |
| 25 | +# server = WEBrick::HTTPServer.new :Port => 8000, :DocumentRoot => root |
| 26 | +# |
| 27 | +# To run the server you will need to provide a suitable shutdown hook as |
| 28 | +# starting the server blocks the current thread: |
| 29 | +# |
| 30 | +# trap 'INT' do server.shutdown end |
| 31 | +# |
| 32 | +# server.start |
| 33 | +# |
| 34 | +# == Custom Behavior |
| 35 | +# |
| 36 | +# The easiest way to have a server perform custom operations is through |
| 37 | +# WEBrick::HTTPServer#mount_proc. The block given will be called with a |
| 38 | +# WEBrick::HTTPRequest with request info and a WEBrick::HTTPResponse which |
| 39 | +# must be filled in appropriately: |
| 40 | +# |
| 41 | +# server.mount_proc '/' do |req, res| |
| 42 | +# res.body = 'Hello, world!' |
| 43 | +# end |
| 44 | +# |
| 45 | +# Remember that <tt>server.mount_proc</tt> must <tt>server.start</tt>. |
| 46 | +# |
| 47 | +# == Servlets |
| 48 | +# |
| 49 | +# Advanced custom behavior can be obtained through mounting a subclass of |
| 50 | +# WEBrick::HTTPServlet::AbstractServlet. Servlets provide more modularity |
| 51 | +# when writing an HTTP server than mount_proc allows. Here is a simple |
| 52 | +# servlet: |
| 53 | +# |
| 54 | +# class Simple < WEBrick::HTTPServlet::AbstractServlet |
| 55 | +# def do_GET request, response |
| 56 | +# status, content_type, body = do_stuff_with request |
| 57 | +# |
| 58 | +# response.status = 200 |
| 59 | +# response['Content-Type'] = 'text/plain' |
| 60 | +# response.body = 'Hello, World!' |
| 61 | +# end |
| 62 | +# end |
| 63 | +# |
| 64 | +# To initialize the servlet you mount it on the server: |
| 65 | +# |
| 66 | +# server.mount '/simple', Simple |
| 67 | +# |
| 68 | +# See WEBrick::HTTPServlet::AbstractServlet for more details. |
| 69 | +# |
| 70 | +# == Virtual Hosts |
| 71 | +# |
| 72 | +# A server can act as a virtual host for multiple host names. After creating |
| 73 | +# the listening host, additional hosts that do not listen can be created and |
| 74 | +# attached as virtual hosts: |
| 75 | +# |
| 76 | +# server = WEBrick::HTTPServer.new # ... |
| 77 | +# |
| 78 | +# vhost = WEBrick::HTTPServer.new :ServerName => 'vhost.example', |
| 79 | +# :DoNotListen => true, # ... |
| 80 | +# vhost.mount '/', ... |
| 81 | +# |
| 82 | +# server.virtual_host vhost |
| 83 | +# |
| 84 | +# If no +:DocumentRoot+ is provided and no servlets or procs are mounted on the |
| 85 | +# main server it will return 404 for all URLs. |
| 86 | +# |
| 87 | +# == HTTPS |
| 88 | +# |
| 89 | +# To create an HTTPS server you only need to enable SSL and provide an SSL |
| 90 | +# certificate name: |
| 91 | +# |
| 92 | +# require 'webrick' |
| 93 | +# require 'webrick/https' |
| 94 | +# |
| 95 | +# cert_name = [ |
| 96 | +# %w[CN localhost], |
| 97 | +# ] |
| 98 | +# |
| 99 | +# server = WEBrick::HTTPServer.new(:Port => 8000, |
| 100 | +# :SSLEnable => true, |
| 101 | +# :SSLCertName => cert_name) |
| 102 | +# |
| 103 | +# This will start the server with a self-generated self-signed certificate. |
| 104 | +# The certificate will be changed every time the server is restarted. |
| 105 | +# |
| 106 | +# To create a server with a pre-determined key and certificate you can provide |
| 107 | +# them: |
| 108 | +# |
| 109 | +# require 'webrick' |
| 110 | +# require 'webrick/https' |
| 111 | +# require 'openssl' |
| 112 | +# |
| 113 | +# cert = OpenSSL::X509::Certificate.new File.read '/path/to/cert.pem' |
| 114 | +# pkey = OpenSSL::PKey::RSA.new File.read '/path/to/pkey.pem' |
| 115 | +# |
| 116 | +# server = WEBrick::HTTPServer.new(:Port => 8000, |
| 117 | +# :SSLEnable => true, |
| 118 | +# :SSLCertificate => cert, |
| 119 | +# :SSLPrivateKey => pkey) |
| 120 | +# |
| 121 | +# == Proxy Server |
| 122 | +# |
| 123 | +# WEBrick can act as a proxy server: |
| 124 | +# |
| 125 | +# require 'webrick' |
| 126 | +# require 'webrick/httpproxy' |
| 127 | +# |
| 128 | +# proxy = WEBrick::HTTPProxyServer.new :Port => 8000 |
| 129 | +# |
| 130 | +# trap 'INT' do proxy.shutdown end |
| 131 | +# |
| 132 | +# Proxies may modifier the content of the response through the |
| 133 | +# +:ProxyContentHandler+ callback which will be invoked with the request and |
| 134 | +# respone after the remote content has been fetched. |
| 135 | +# |
| 136 | +# == WEBrick as a Production Web Server |
| 137 | +# |
| 138 | +# WEBrick can be run as a production server for small loads. |
| 139 | +# |
| 140 | +# === Daemonizing |
| 141 | +# |
| 142 | +# To start a WEBrick server as a daemon simple run WEBrick::Daemon.start |
| 143 | +# before starting the server. |
| 144 | +# |
| 145 | +# === Dropping Permissions |
| 146 | +# |
| 147 | +# WEBrick can be started as one user to gain permission to bind to port 80 or |
| 148 | +# 443 for serving HTTP or HTTPS traffic then can drop these permissions for |
| 149 | +# regular operation. To listen on all interfaces for HTTP traffic: |
| 150 | +# |
| 151 | +# sockets = WEBrick::Utils.create_listeners nil, 80 |
| 152 | +# |
| 153 | +# Then drop privileges: |
| 154 | +# |
| 155 | +# WEBrick::Utils.su 'www' |
| 156 | +# |
| 157 | +# Then create a server that does not listen by default: |
| 158 | +# |
| 159 | +# server = WEBrick::HTTPServer.new :DoNotListen => true, # ... |
| 160 | +# |
| 161 | +# Then overwrite the listening sockets with the port 80 sockets: |
| 162 | +# |
| 163 | +# server.listeners.replace sockets |
| 164 | +# |
| 165 | +# === Logging |
| 166 | +# |
| 167 | +# WEBrick can separately log server operations and end-user access. For |
| 168 | +# server operations: |
| 169 | +# |
| 170 | +# log_file = File.open '/var/log/webrick.log', 'a+' |
| 171 | +# log = WEBrick::Log.new log_file |
| 172 | +# |
| 173 | +# For user access logging: |
| 174 | +# |
| 175 | +# access_log = [ |
| 176 | +# [log_file, WEBrick::AccessLog::COMBINED_LOG_FORMAT], |
| 177 | +# ] |
| 178 | +# |
| 179 | +# server = WEBrick::HTTPServer.new :Logger => log, :AccessLog => access_log |
| 180 | +# |
| 181 | +# See WEBrick::AccessLog for further log formats. |
| 182 | +# |
| 183 | +# === Log Rotation |
| 184 | +# |
| 185 | +# To rotate logs in WEBrick on a HUP signal (like syslogd can send), open the |
| 186 | +# log file in 'a+' mode (as above) and trap 'HUP' to reopen the log file: |
| 187 | +# |
| 188 | +# trap 'HUP' do log_file.reopen '/path/to/webrick.log', 'a+' |
| 189 | +# |
| 190 | +# == Copyright |
3 | 191 | # |
4 | 192 | # Author: IPR -- Internet Programming with Ruby -- writers |
| 193 | +# |
5 | 194 | # Copyright (c) 2000 TAKAHASHI Masayoshi, GOTOU YUUZOU |
6 | 195 | # Copyright (c) 2002 Internet Programming with Ruby writers. All rights |
7 | 196 | # reserved. |
8 | | -# |
| 197 | +#-- |
9 | 198 | # $IPR: webrick.rb,v 1.12 2002/10/01 17:16:31 gotoyuzo Exp $ |
10 | 199 |
|
| 200 | +module WEBrick |
| 201 | +end |
| 202 | + |
11 | 203 | require 'webrick/compat.rb' |
12 | 204 |
|
13 | 205 | require 'webrick/version.rb' |
|
0 commit comments