Skip to content

Commit 071a678

Browse files
committed
* lib/webrick: Add Documentation
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@31499 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
1 parent 7e3ec1d commit 071a678

12 files changed

Lines changed: 591 additions & 18 deletions

File tree

ChangeLog

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
Tue May 10 09:13:21 2011 Eric Hodel <drbrain@segment7.net>
2+
3+
* lib/webrick: Add Documentation
4+
15
Tue May 10 04:22:09 Eric Hodel <drbrain@segment7.net>
26

37
* lib/webrick/log.rb: Hide copyright info from ri

lib/webrick.rb

Lines changed: 194 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,205 @@
1+
##
2+
# = WEB server toolkit.
13
#
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
3191
#
4192
# Author: IPR -- Internet Programming with Ruby -- writers
193+
#
5194
# Copyright (c) 2000 TAKAHASHI Masayoshi, GOTOU YUUZOU
6195
# Copyright (c) 2002 Internet Programming with Ruby writers. All rights
7196
# reserved.
8-
#
197+
#--
9198
# $IPR: webrick.rb,v 1.12 2002/10/01 17:16:31 gotoyuzo Exp $
10199

200+
module WEBrick
201+
end
202+
11203
require 'webrick/compat.rb'
12204

13205
require 'webrick/version.rb'

lib/webrick/accesslog.rb

Lines changed: 72 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,90 @@
88
# $IPR: accesslog.rb,v 1.1 2002/10/01 17:16:32 gotoyuzo Exp $
99

1010
module WEBrick
11+
12+
##
13+
# AccessLog provides logging to various files in various formats.
14+
#
15+
# Multiple logs may be written to at the same time:
16+
#
17+
# access_log = [
18+
# [$stderr, WEBrick::AccessLog::COMMON_LOG_FORMAT],
19+
# [$stderr, WEBrick::AccessLog::REFERER_LOG_FORMAT],
20+
# ]
21+
#
22+
# server = WEBrick::HTTPServer.new :AccessLog => access_log
23+
#
24+
# Custom log formats may be defined. WEBrick::AccessLog provides a subset
25+
# of the formatting from Apache's mod_log_config
26+
# http://httpd.apache.org/docs/mod/mod_log_config.html#formats. See
27+
# AccessLog::setup_params for a list of supported options
28+
1129
module AccessLog
30+
31+
##
32+
# Raised if a parameter such as %e, %i, %o or %n is used without fetching
33+
# a specific field.
34+
1235
class AccessLogError < StandardError; end
1336

37+
##
38+
# The Common Log Format's time format
39+
1440
CLF_TIME_FORMAT = "[%d/%b/%Y:%H:%M:%S %Z]"
41+
42+
##
43+
# Common Log Format
44+
1545
COMMON_LOG_FORMAT = "%h %l %u %t \"%r\" %s %b"
46+
47+
##
48+
# Short alias for Common Log Format
49+
1650
CLF = COMMON_LOG_FORMAT
51+
52+
##
53+
# Referer Log Format
54+
1755
REFERER_LOG_FORMAT = "%{Referer}i -> %U"
56+
57+
##
58+
# User-Agent Log Format
59+
1860
AGENT_LOG_FORMAT = "%{User-Agent}i"
61+
62+
##
63+
# Combined Log Format
64+
1965
COMBINED_LOG_FORMAT = "#{CLF} \"%{Referer}i\" \"%{User-agent}i\""
2066

2167
module_function
2268

23-
# This format specification is a subset of mod_log_config of Apache.
24-
# http://httpd.apache.org/docs/mod/mod_log_config.html#formats
25-
def setup_params(config, req, res)
69+
# This format specification is a subset of mod_log_config of Apache:
70+
#
71+
# %a:: Remote IP address
72+
# %b:: Total response size
73+
# %e{variable}:: Given variable in ENV
74+
# %f:: Response filename
75+
# %h:: Remote host name
76+
# %{header}i:: Given request header
77+
# %l:: Remote logname, always "-"
78+
# %m:: Request method
79+
# %{attr}n:: Given request attribute from <tt>req.attributes</tt>
80+
# %{header}o:: Given response header
81+
# %p:: Server's request port
82+
# %{format}p:: The canonical port of the server serving the request or the
83+
# actual port or the client's actual port. Valid formats are
84+
# canonical, local or remote.
85+
# %q:: Request query string
86+
# %r:: First line of the request
87+
# %s:: Request status
88+
# %t:: Time the request was recieved
89+
# %T:: Time taken to process the request
90+
# %u:: Remote user from auth
91+
# %U:: Unparsed URI
92+
# %%:: Literal %
93+
94+
def setup_params(config, req, res)
2695
params = Hash.new("")
2796
params["a"] = req.peeraddr[3]
2897
params["b"] = res.sent_size

lib/webrick/htmlutils.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
module WEBrick
1212
module HTMLUtils
1313

14+
##
15+
# Escapes &, ", > and < in +string+
16+
1417
def escape(string)
1518
str = string ? string.dup : ""
1619
str.gsub!(/&/n, '&amp;')

lib/webrick/httpproxy.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,24 @@ def method_missing(meth, *args)
3333
end
3434
end
3535

36+
##
37+
# An HTTP Proxy server which proxies GET, HEAD and POST requests.
38+
3639
class HTTPProxyServer < HTTPServer
40+
41+
##
42+
# Proxy server configurations. The proxy server handles the following
43+
# configuration items in addition to those supported by HTTPServer:
44+
#
45+
# :ProxyAuthProc:: Called with a request and response to authorize a
46+
# request
47+
# :ProxyVia:: Appended to the via header
48+
# :ProxyURI:: The proxy server's URI
49+
# :ProxyContentHandler:: Called with a request and resopnse and allows
50+
# modification of the response
51+
# :ProxyTimeout:: Sets the proxy timeouts to 30 seconds for open and 60
52+
# seconds for read operations
53+
3754
def initialize(config={}, default=Config::HTTP)
3855
super(config, default)
3956
c = @config

0 commit comments

Comments
 (0)