2009-06-18 14:36:30 +02:00
|
|
|
DIR = File.dirname(__FILE__)
|
|
|
|
|
|
|
|
def fib(n)
|
|
|
|
return 1 if n <= 1
|
|
|
|
fib(n-1) + fib(n-2)
|
|
|
|
end
|
|
|
|
|
|
|
|
def wait(seconds)
|
|
|
|
n = (seconds / 0.01).to_i
|
2010-06-30 08:12:46 +02:00
|
|
|
n.times do
|
2009-06-18 14:36:30 +02:00
|
|
|
sleep(0.01)
|
2010-06-30 08:12:46 +02:00
|
|
|
#File.read(DIR + '/yahoo.html')
|
2009-06-18 14:36:30 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class SimpleApp
|
|
|
|
@@responses = {}
|
2010-06-30 08:12:46 +02:00
|
|
|
|
2009-06-18 14:36:30 +02:00
|
|
|
def initialize
|
|
|
|
@count = 0
|
|
|
|
end
|
2010-06-30 08:12:46 +02:00
|
|
|
|
2009-06-18 14:36:30 +02:00
|
|
|
def deferred?(env)
|
|
|
|
false
|
|
|
|
end
|
2010-06-30 08:12:46 +02:00
|
|
|
|
2009-06-18 14:36:30 +02:00
|
|
|
def call(env)
|
|
|
|
path = env['PATH_INFO'] || env['REQUEST_URI']
|
|
|
|
commands = path.split('/')
|
2010-06-30 08:12:46 +02:00
|
|
|
|
2009-06-18 14:36:30 +02:00
|
|
|
@count += 1
|
|
|
|
if commands.include?('periodical_activity') and @count % 10 != 1
|
|
|
|
return [200, {'Content-Type'=>'text/plain'}, "quick response!\r\n"]
|
|
|
|
end
|
2010-06-30 08:12:46 +02:00
|
|
|
|
2009-06-18 14:36:30 +02:00
|
|
|
if commands.include?('fibonacci')
|
|
|
|
n = commands.last.to_i
|
|
|
|
raise "fibonacci called with n <= 0" if n <= 0
|
|
|
|
body = (1..n).to_a.map { |i| fib(i).to_s }.join(' ')
|
|
|
|
status = 200
|
2010-06-30 08:12:46 +02:00
|
|
|
|
2009-06-18 14:36:30 +02:00
|
|
|
elsif commands.include?('wait')
|
|
|
|
n = commands.last.to_f
|
|
|
|
raise "wait called with n <= 0" if n <= 0
|
|
|
|
wait(n)
|
|
|
|
body = "waited about #{n} seconds"
|
|
|
|
status = 200
|
2010-06-30 08:12:46 +02:00
|
|
|
|
2009-06-18 14:36:30 +02:00
|
|
|
elsif commands.include?('bytes')
|
|
|
|
n = commands.last.to_i
|
|
|
|
raise "bytes called with n <= 0" if n <= 0
|
|
|
|
body = @@responses[n] || "C"*n
|
|
|
|
status = 200
|
|
|
|
|
|
|
|
elsif commands.include?('fixed')
|
|
|
|
n = 20 * 1024;
|
|
|
|
body = @@responses[n] || "C"*n
|
|
|
|
status = 200
|
2010-06-30 08:12:46 +02:00
|
|
|
|
2009-06-18 14:36:30 +02:00
|
|
|
elsif commands.include?('test_post_length')
|
|
|
|
input_body = ""
|
|
|
|
while chunk = env['rack.input'].read(512)
|
2010-06-30 08:12:46 +02:00
|
|
|
input_body << chunk
|
2009-06-18 14:36:30 +02:00
|
|
|
end
|
|
|
|
if env['CONTENT_LENGTH'].to_i == input_body.length
|
|
|
|
body = "Content-Length matches input length"
|
|
|
|
status = 200
|
|
|
|
else
|
2010-06-30 08:12:46 +02:00
|
|
|
body = "Content-Length doesn't matches input length!
|
2009-06-18 14:36:30 +02:00
|
|
|
content_length = #{env['CONTENT_LENGTH'].to_i}
|
|
|
|
input_body.length = #{input_body.length}"
|
|
|
|
status = 500
|
|
|
|
end
|
|
|
|
else
|
|
|
|
status = 404
|
|
|
|
body = "Undefined url"
|
|
|
|
end
|
2010-06-30 08:12:46 +02:00
|
|
|
|
2009-06-18 14:36:30 +02:00
|
|
|
body += "\r\n"
|
|
|
|
headers = {'Content-Type' => 'text/plain', 'Content-Length' => body.length.to_s }
|
|
|
|
[status, headers, [body]]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
if $0 == __FILE__
|
|
|
|
#require DIR + '/../lib/ebb'
|
|
|
|
require 'rubygems'
|
2010-03-05 23:36:13 +01:00
|
|
|
require 'rack'
|
2010-03-04 18:58:31 +01:00
|
|
|
require 'thin'
|
2010-03-05 23:36:13 +01:00
|
|
|
require 'ebb'
|
|
|
|
# Rack::Handler::Mongrel.run(SimpleApp.new, :Port => 8000)
|
2010-06-30 08:12:46 +02:00
|
|
|
Thin::Server.start("0.0.0.0", 8000, SimpleApp.new)
|
2009-06-18 14:36:30 +02:00
|
|
|
# Ebb::start_server(SimpleApp.new, :port => 8000)
|
|
|
|
end
|