Skip to content

Commit 16291a0

Browse files
author
gotoyuzo
committed
* sample/webrick/*: new files.
* MANIFEST: add sample/webrick/* git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@5403 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
1 parent b33d3cf commit 16291a0

File tree

10 files changed

+185
-0
lines changed

10 files changed

+185
-0
lines changed

ChangeLog

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
Wed Jan 7 21:15:07 2004 GOTOU Yuuzou <gotoyuzo@notwork.org>
2+
3+
* sample/webrick/*: new files.
4+
5+
* MANIFEST: add sample/webrick/*
6+
17
Wed Jan 7 20:51:51 2004 Minero Aoki <aamine@loveruby.net>
28

39
* test/net/test_httpheader.rb: new file.

MANIFEST

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -663,6 +663,14 @@ sample/time.rb
663663
sample/trojan.rb
664664
sample/tsvr.rb
665665
sample/uumerge.rb
666+
sample/webrick/demo-app.rb
667+
sample/webrick/demo-multipart.cgi
668+
sample/webrick/demo-servlet.rb
669+
sample/webrick/demo-urlencoded.cgi
670+
sample/webrick/hello.cgi
671+
sample/webrick/hello.rb
672+
sample/webrick/httpd.rb
673+
sample/webrick/httpsd.rb
666674
sample/wsdl/amazon/AmazonSearch.rb
667675
sample/wsdl/amazon/AmazonSearchDriver.rb
668676
sample/wsdl/amazon/sampleClient.rb

sample/webrick/demo-app.rb

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
require "pp"
2+
3+
module DemoApplication
4+
def initialize(config, enctype)
5+
super
6+
@enctype = enctype
7+
end
8+
9+
def do_GET(req, res)
10+
if req.path_info != "/"
11+
res.set_redirect(WEBrick::HTTPStatus::Found, req.script_name + "/")
12+
end
13+
res.body =<<-_end_of_html_
14+
<HTML>
15+
<FORM method="POST" enctype=#{@enctype}>
16+
text: <INPUT type="text" name="text"><BR>
17+
file: <INPUT type="file" name="file"><BR>
18+
check:
19+
<INPUT type="checkbox" name="check" value="a">a,
20+
<INPUT type="checkbox" name="check" value="b">b,
21+
<INPUT type="checkbox" name="check" value="c">c,
22+
<BR>
23+
<INPUT type="submit">
24+
</FORM>
25+
</HTML>
26+
_end_of_html_
27+
res['content-type'] = 'text/html; charset=iso-8859-1'
28+
end
29+
30+
def do_POST(req, res)
31+
if req["content-length"].to_i > 1024*10
32+
raise WEBrick::HTTPStatus::Forbidden, "file size too large"
33+
end
34+
res.body =<<-_end_of_html_
35+
<HTML>
36+
<H2>Query Parameters</H2>
37+
#{display_query(req.query)}
38+
<A href="#{req.path}">return</A>
39+
<H2>Request</H2>
40+
<PRE>#{WEBrick::HTMLUtils::escape(PP::pp(req, "", 80))}</PRE>
41+
<H2>Response</H2>
42+
<PRE>#{WEBrick::HTMLUtils::escape(PP::pp(res, "", 80))}</PRE>
43+
</HTML>
44+
_end_of_html_
45+
res['content-type'] = 'text/html; charset=iso-8859-1'
46+
end
47+
48+
private
49+
50+
def display_query(q)
51+
ret = ""
52+
q.each{|key, val|
53+
ret << "<H3>#{WEBrick::HTMLUtils::escape(key)}</H3>"
54+
ret << "<TABLE border=1>"
55+
ret << make_tr("val", val.inspect)
56+
ret << make_tr("val.to_a", val.to_a.inspect)
57+
ret << make_tr("val.to_ary", val.to_ary.inspect)
58+
ret << "</TABLE>"
59+
}
60+
ret
61+
end
62+
63+
def make_tr(arg0, arg1)
64+
"<TR><TD>#{arg0}</TD><TD>#{WEBrick::HTMLUtils::escape(arg1)}</TD></TR>"
65+
end
66+
end

sample/webrick/demo-multipart.cgi

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/usr/bin/env ruby
2+
require "webrick/cgi"
3+
require "webrick/https" # should load if it runs on HTTPS server
4+
require "./demo-app"
5+
6+
class DemoCGI < WEBrick::CGI
7+
include DemoApplication
8+
end
9+
10+
config = { :NPH => false }
11+
cgi = DemoCGI.new(config, "multipart/form-data")
12+
cgi.start

sample/webrick/demo-servlet.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
require "webrick"
2+
require "./demo-app"
3+
4+
class DemoServlet < WEBrick::HTTPServlet::AbstractServlet
5+
include DemoApplication
6+
end

sample/webrick/demo-urlencoded.cgi

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/usr/bin/env ruby
2+
require "webrick/cgi"
3+
require "webrick/https" # should load if it runs on HTTPS server
4+
require "./demo-app"
5+
6+
class DemoCGI < WEBrick::CGI
7+
include DemoApplication
8+
end
9+
10+
config = { :NPH => false }
11+
cgi = DemoCGI.new(config, "application/x-www-form-urlencoded")
12+
cgi.start

sample/webrick/hello.cgi

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/usr/bin/env ruby
2+
require "webrick/cgi"
3+
4+
class HelloCGI < WEBrick::CGI
5+
def do_GET(req, res)
6+
res["content-type"] = "text/plain"
7+
res.body = "Hello, world.\n"
8+
end
9+
end
10+
11+
HelloCGI.new.start

sample/webrick/hello.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
require "webrick"
2+
3+
class HelloServlet < WEBrick::HTTPServlet::AbstractServlet
4+
def do_GET(req, res)
5+
res["content-type"] = "text/plain"
6+
res.body = "Hello, world.\n"
7+
end
8+
end

sample/webrick/httpd.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
require "webrick"
2+
3+
httpd = WEBrick::HTTPServer.new(
4+
:DocumentRoot => File::dirname(__FILE__),
5+
:Port => 10080,
6+
:Logger => WEBrick::Log.new($stderr, WEBrick::Log::DEBUG),
7+
:AccessLog => [
8+
[ $stderr, WEBrick::AccessLog::COMMON_LOG_FORMAT ],
9+
[ $stderr, WEBrick::AccessLog::REFERER_LOG_FORMAT ],
10+
[ $stderr, WEBrick::AccessLog::AGENT_LOG_FORMAT ],
11+
],
12+
:CGIPathEnv => ENV["PATH"] # PATH environment variable for CGI.
13+
)
14+
15+
require "./hello"
16+
httpd.mount("/hello", HelloServlet)
17+
18+
require "./demo-servlet"
19+
httpd.mount("/urlencoded", DemoServlet, "application/x-www-form-urlencoded")
20+
httpd.mount("/multipart", DemoServlet, "multipart/form-data")
21+
22+
trap(:INT){ httpd.shutdown }
23+
httpd.start

sample/webrick/httpsd.rb

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
require "webrick"
2+
require "webrick/https"
3+
4+
hostname = WEBrick::Utils::getservername
5+
subject = [["O", "ruby-lang.org"], ["OU", "sample"], ["CN", hostname]]
6+
comment = "Comment for self-signed certificate"
7+
8+
httpd = WEBrick::HTTPServer.new(
9+
:DocumentRoot => File::dirname(__FILE__),
10+
:Port => 10443,
11+
:SSLEnable => true,
12+
13+
# Specify key pair and server certificate.
14+
# :SSLPrivateKey => OpenSSL::PKey::RSA.new("server.key"),
15+
# :SSLCertificate => OpenSSL::X509::Certificate.new("server.crt"),
16+
17+
# specify the following SSL options if you want to use auto
18+
# generated self-signed certificate.
19+
:SSLCertName => subject,
20+
:SSLComment => comment,
21+
22+
:CGIPathEnv => ENV["PATH"] # PATH environment variable for CGI.
23+
)
24+
25+
require "./hello"
26+
httpd.mount("/hello", HelloServlet)
27+
28+
require "./demo-servlet"
29+
httpd.mount("/urlencoded", DemoServlet, "application/x-www-form-urlencoded")
30+
httpd.mount("/multipart", DemoServlet, "multipart/form-data")
31+
32+
trap(:INT){ httpd.shutdown }
33+
httpd.start

0 commit comments

Comments
 (0)