/
really_dumb_webserver.rb
113 lines (105 loc) · 3.38 KB
/
really_dumb_webserver.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#-- -*- mode: ruby; encoding: utf-8 -*-
# Copyright: Copyright (c) 2011 RightScale, Inc.
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# 'Software'), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#++
require 'webrick'
ssl_cert, ssl_key = ARGV[0], ARGV[1]
# Monkey patch bad User-Agent parsing
module WEBrick::AccessLog
module_function
def format(format_string, params)
format_string.gsub(/\%(?:\{(.*?)\})?>?([a-zA-Z%])/){
param, spec = $1, $2
case spec[0]
when ?e, ?i, ?n, ?o
raise AccessLogError,
"parameter is required for \"#{spec}\"" unless param
if params[spec][param]
escape(params[spec][param])
else
"-"
end
when ?t
params[spec].strftime(param || CLF_TIME_FORMAT)
when ?%
"%"
else
escape(params[spec].to_s)
end
}
end
end
logger = WEBrick::Log.new($stderr, WEBrick::Log::WARN)
config = {}
config[:Port] = 7890
config[:Logger] = logger
config[:AccessLog] = [[$stdout, WEBrick::AccessLog::COMBINED_LOG_FORMAT]]
unless ssl_cert.nil? || ssl_key.nil?
require 'webrick/https'
config[:SSLEnable] = true
config[:SSLVerifyClient] = OpenSSL::SSL::VERIFY_NONE
config[:SSLPrivateKey] = OpenSSL::PKey::RSA.new(File.open(ssl_key).read)
config[:SSLCertificate] = OpenSSL::X509::Certificate.new(File.open(ssl_cert).read)
config[:SSLCertName] = [["CN", "Graham Hughes"]]
end
$stdout.sync = true
server = WEBrick::HTTPServer.new(config)
server.mount_proc('/good') {|req, resp|
resp.status = 200
resp['Content-Type'] = "text/plain"
resp.body = "good"
}
intermittent_times = 0
server.mount_proc('/intermittent-hang') {|req, resp|
intermittent_times += 1
if intermittent_times % 2 == 1
sleep 5
resp.status = 403
resp['Content-Type'] = "text/plain"
resp.body = "bad"
else
resp.status = 200
resp['Content-Type'] = "text/plain"
resp.body = "good"
end
}
server.mount_proc('/hang') {|req, resp|
sleep 5
resp.status = 200
resp['Content-Type'] = "text/plain"
resp.body = "good"
}
server.mount_proc('/ugly') {|req, resp|
resp.status = 404
resp['Content-Type'] = "text/plain"
resp.body = "ugly"
}
server.mount_proc('/filename') {|req, resp|
resp.status = 200
resp['ETag'] = File.stat('filename').mtime
resp['Content-Type'] = "text/plain"
resp.body = File.open("filename").read
}
# trap signals to invoke shutdown cleanly
['INT', 'TERM'].each { |signal|
trap(signal) { server.shutdown }
}
server.start