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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
|
# File 'lib/shellac/config.rb', line 62
def parse_command_line
call_list = TaskList.new
options = OptionParser.new do |opts|
opts.on( '-h', '--help' ) do
exe = File.basename( $PROGRAM_NAME )
@meta_configuration[:helptext] << "\#{exe} [OPTIONS]\n\n\#{exe} is a simple caching proxy server.\n\n-h, --help:\n Show this help.\n\n-b HOSTNAME[:PORT], --bind HOSTNAME[:PORT]:\n The hostname/IP and optionally the port to bind to. This defaults to 127.0.0.1:80 if it is not provided.\n\n-c FILENAME, --config FILENAME:\n The configuration file to load.\n\n-r ROUTESPEC, --route ROUTESPEC:\n Provides a routing specification for the proxy. A route spec is one or more\n host names or IPs, comma seperated, to match requests from, a regular\n expression to match against, and a target to proxy to:\n\n -r 'foo.bar.com::\\?(\\w+)$::https://github.com/\\\#{$1}'\n\n This can be specified multiple times. For complex route specs, it is better\n to use a configuration file.\n\n-s ENGINE, --storageengine ENGINE:\n The storage engine to use for storing cached content.\n\n-t MIN:MAX, --threads MIN:MAX:\n The minimum and maximum number of threads to run. Defaults to 0:10\n\n-w COUNT, --workers COUNT:\n The number of worker processes to start.\n\n-v, --version:\n Show the version of \#{exe}.\n"
call_list << Task.new(9999) { puts @meta_configuration[:helptext]; exit 0 }
end
opts.on( '-v', '--version') do
exe = File.basename( $PROGRAM_NAME )
@meta_configuration[:version] = "#{exe} v. #{Shellac::VERSION}"
call_list << Task.new(9999) { puts @meta_configuration[:version]; exit 0 }
end
opts.on( '-c', '--config FILENAME' ) do |configfile|
require 'yaml'
call_list << Task.new(0) do
parsed_config = YAML.load( File.read( File.expand_path( configfile ) ) )
@configuration = parsed_config.merge( @configuration ) if Hash === parsed_config
end
end
opts.on( '-s', '--storageengine ENGINE' ) do |storageengine|
@configuration[:storageengine] = storageengine
call_list << Task.new(1) do
libname = "shellac/storage_engine/#{@configuration[:storageengine]}"
setup_engine(:storageengine, libname)
end
end
opts.on( '-t', '--threads THREADSPEC' ) do |threadspec|
call_list << Task.new(9000) do
min = 1
max = 10
if threadspec =~ /\s*(\d+)\s*:\s*(\d+)/
min,max = [ $1.to_i > 0 ? $1.to_i : 1, $2.to_i > 0 ? $2.to_i : 10 ]
else
n = Integer( threadspec.to_i )
max = n > 0 ? n : 10
end
@configuration[:minimum_threads] = min
@configuration[:maximum_threads] = max
end
end
opts.on( '-w', '--workers COUNT' ) do |worker_count|
call_list << Task.new(9000) do
count = Integer( worker_count.to_i )
count = count > 0 ? count : 1
@configuration[:worker_count] = count
end
end
opts.on( '-r', '--route ROUTESPEC' ) do |routespec|
hosts,regexp,matchfunc = routespec.split(/::/,3)
hosts = hosts.split(/,/).collect {|h| h.strip}
regexp = Regexp.new( regexp )
if matchfunc =~ /^lambda:(.*)$/m
code = "#{$1}"
else
code = "\"#{matchfunc}\""
end
matchfunc = " lambda {|s,r|\nif s =~ r\n \#{code}\nelse\n nil\nend\n }\n ECODE\n matchfunc = Object.new.instance_eval(matchfunc)\n\n hosts.each do |h|\n @configuration[:routes][h] << {\n regexp: regexp,\n func: matchfunc\n }\n end\n end\n\n opts.on( '-b', '--bind HOST') do |host_and_port|\n if host_and_port =~ /^(\\w+:\\/\\/)/ \n protocol = $1\n host_and_port.gsub!(/^\\w+:\\/\\//,'')\n else\n protocol = 'tcp://'\n end\n h,p = host_and_port.split(/:/,2)\n h = '127.0.0.1' if h.empty?\n p = '80' if p.empty?\n call_list << Task.new(9000) { @configuration[:bind] << \"\#{protocol}\#{h}:\#{p}\" }\n end\n end\n\n leftover_argv = []\n\n begin\n options.parse!(ARGV)\n rescue OptionParser::InvalidOption => e\n e.recover ARGV\n leftover_argv << ARGV.shift\n leftover_argv << ARGV.shift if ARGV.any? && ( ARGV.first[0..0] != '-' )\n retry\n ensure\n puts \"adding default storage engine\"\n unless @configuration[:storageengine]\n @configuration[:storageengine] = 'hash'\n call_list << Task.new(100) do\n libname = \"shellac/storage_engine/\#{@configuration[:storageengine]}\"\n setup_engine(:storageengine, libname)\n end\n end\n end\n\n ARGV.replace( leftover_argv ) if leftover_argv.any?\n\n call_list\nend\n"
|